GeoJSON映射d3和Leaflet之间的差异

s2t*_*2t2 4 geojson d3.js leaflet

我的目标是使用d3为给定的GeoJSON特征集合中的每个特征生成svg路径.

当我使用传单映射路径时,所有功能看起来都很完美.

d3.json("ct_counties.geo.json", function(data) {
    var leaflet_paths = leaflet_map.addLayer(new L.GeoJSON(data));
});
Run Code Online (Sandbox Code Playgroud)

两张地图

但是当我使用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);
    }
});
Run Code Online (Sandbox Code Playgroud)

此处查看地图差异.

请参阅此处的完整代码.

由于两个地图都使用相同的功能集合,为什么d3版本错误?

flu*_*lup 22

D3没有错,数据不正确,Leaflet更宽松.

以Litchfield(左上角县)为例:

{
    "type" : "Feature",
    "properties" : {
        "kind" : "county",
        "name" : "Litchfield",
        "state" : "CT"
    },
    "geometry" : {
        "type" : "MultiPolygon",
        "coordinates" : [ [ [ [ -73.0535, 42.0390 ], [ -73.0097, 42.0390 ],
                [ -73.0316, 41.9678 ], [ -72.8892, 41.9733 ],
                [ -72.9385, 41.8966 ], [ -72.9495, 41.8090 ],
                [ -73.0152, 41.7981 ], [ -72.9823, 41.6392 ],
                [ -73.1631, 41.5571 ], [ -73.1576, 41.5133 ],
                [ -73.3219, 41.5078 ], [ -73.3109, 41.4694 ],
                [ -73.3876, 41.5133 ], [ -73.4424, 41.4914 ],
                [ -73.4862, 41.6447 ], [ -73.5191, 41.6666 ],
                [ -73.4862, 42.0500 ] ] ] ]
    }
}
Run Code Online (Sandbox Code Playgroud)

多边形未关闭,其末端不等于开头.我绘制了坐标,标记了第一个坐标红色,最后一个标记为绿色: 多边形中的点

如您所见,最后一个坐标被d3丢弃.

GeoJSON的规范

LinearRing是关闭的LineString,具有4个或更多位置.第一个和最后一个位置是等价的(它们代表等效点).

因此d3有一个点(没有双关语),并且应该通过在末尾添加开始坐标来关闭MultiPolygon:

...[ -73.4862, 42.0500 ], [ -73.0535, 42.0390 ] ] ] ]
Run Code Online (Sandbox Code Playgroud)