我的目標是使用d3爲給定的GeoJSON要素集合中的每個要素生成svg路徑。d3和小冊子之間的GeoJSON映射差異
當我使用傳單映射路徑時,所有功能看起來都很完美。
d3.json("ct_counties.geo.json", function(data) {
var leaflet_paths = leaflet_map.addLayer(new L.GeoJSON(data));
});
但是,當我使用映射D3的路徑,一些功能看上去是錯誤的。
d3.json("ct_counties.geo.json", function(collection) {
var bounds = d3.geo.bounds(collection);
var path = d3.geo.path().projection(project);
var feature = g.selectAll("path")
.data(collection.features)
.enter().append("path")
.attr('class','county');
d3_map.on("viewreset", reset);
reset();
function project(x) {
var point = d3_map.latLngToLayerPoint(new L.LatLng(x[1], x[0]));
return [point.x, point.y];
}
function reset() {
var bottomLeft = project(bounds[0]);
var topRight = project(bounds[1]);
svg.attr("width", topRight[0] - bottomLeft[0])
.attr("height", bottomLeft[1] - topRight[1])
.style("margin-left", bottomLeft[0] + "px")
.style("margin-top", topRight[1] + "px");
g.attr("transform", "translate(" + -bottomLeft[0] + "," + -topRight[1] + ")");
feature.attr("d", path);
}
});
由於兩張地圖都使用相同的特徵集合,爲什麼d3版本是錯誤的?
貌似多邊形不同的解釋。由於點的順序可能。仔細檢查後,看起來每個多邊形都缺少一個點。可能是結尾粘在一起的那一點? – flup 2013-03-17 08:45:01