2017-08-09 38 views
1

我有一個world-50m.json文件工作的投影,但是當我用一種顏色填充它時,有幾個國家的邊緣被切斷,這些國家會在邊緣創建水平填充部分/線條整個地圖。D3世界-50m.json投影填充不正確

這是對D3-地理例如投影這裏實際可見:https://github.com/d3/d3-geo/blob/master/test/data/world-50m.json

enter image description here

有沒有這些截止各國的另一個JSON文件?或者,也許我可以從我的填充中省略特定的多邊形?不太清楚我將如何找到每個國家/地區的問題。雖然大多數是微小的,如果省略不會錯過,主要的一個似乎是俄羅斯。

這裏是我的代碼以供參考:

var w = 960, 
    h = 660, 
    active = d3.select(null); 

var projection = d3.geoMercator() 
    .scale(150) 
    .translate([w/2, h/2]) 
    .precision(.1); 

var path = d3.geo.path() 
    .projection(projection); 

var countries = svg.append("svg:g").attr("id", "countries"); 

d3.json("world-50m.json", function(error, us) { 
    mapFeatures = topojson.feature(us, us.objects.countries).features; 
    mapFeatures.type = "countries"; 
    drawMap(); 
}); 

function drawMap() { 
    countries.selectAll("path") 
     .data(mapFeatures) 
     .enter().append("svg:path") 
     .attr("d", path) 
     .attr("class", "feature") 
     .attr("data-id", function(d) { return d.id; }) 
     .style("fill", "blue") 
     .style("stroke", "white") 
     .style("stroke-width", ".2px"); 
}; 

任何幫助將不勝感激!

+0

是否有可能包括在你的代碼塊你'path'和'projection'嗎? –

+0

你的問題讓我想到了答案!我正在使用錯誤的路徑投影。應該使用'geoPath()'而不是'geo.path()'請回答這個問題,這樣我可以標記它是正確的:) – MatOwen11

+0

@ MatOwen11這很可能不是正確的解釋。看看* Antimeridian Cutting *,例如比較此[示例](https://bl.ocks.org/mbostock/3788999)與此[示例](https://bl.ocks.org/mbostock/5735770)沒有antimeridian切割。 – altocumulus

回答

0

該解決方案在上述評論中有詳細說明,並且是在@Andrew Reid和@altocumulus的幫助下實現的。

觀察到的問題是逆子午線切割這是未正確處理D3,由於路徑和投影的不匹配D3 v3和v4的D3之間的語法調用的一個實例。

該問題已通過將geo.path()更改爲geoPath()解決,該更正了不匹配並使d3能夠正確渲染地圖的反子午線切割支持。

下面是正確的代碼:

var w = 960, 
    h = 660, 
    active = d3.select(null); 

var projection = d3.geoMercator() 
    .scale(150) 
    .translate([w/2, h/2]) 
    .precision(.1); 

var path = d3.geoPath() 
    .projection(projection); 

var countries = svg.append("svg:g").attr("id", "countries"); 

d3.json("world-50m.json", function(error, us) { 
    mapFeatures = topojson.feature(us, us.objects.countries).features; 
    mapFeatures.type = "countries"; 
    drawMap(); 
}); 

function drawMap() { 
    countries.selectAll("path") 
     .data(mapFeatures) 
     .enter().append("svg:path") 
     .attr("d", path) 
     .attr("class", "feature") 
     .attr("data-id", function(d) { return d.id; }) 
     .style("fill", "blue") 
     .style("stroke", "white") 
     .style("stroke-width", ".2px"); 
}; 
+0

我還是有點困惑。 'd3.geo.path()'調用必須給你一個類型錯誤,對吧?類似於*無法讀取未定義*的屬性「路徑」。另一方面,你說明你的工作。你能讓我滿意嗎? – altocumulus

+0

對不起,對於遲到的回覆,我沒有收到類型錯誤,我相信這是因爲我已經同時初始化了d3 v3和d3 v4。這意味着路徑和投影呼叫路由到它們各自的d3版本,但不能一起正常工作。爲了簡化問題,我忽略了自定義的d3變量,但是現在我意識到在我的問題中包含更多的上下文會有所幫助。 – MatOwen11