d3.js - d3 和 Leaflet 之间的 GeoJSON 映射差异

标签 d3.js leaflet geojson

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

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

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

two maps

但是当我使用 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);
    }
});

查看 map 差异 here .

并引用完整代码here .

既然两个 map 都使用相同的特征集合,为什么d3版本会出错?

最佳答案

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 ] ] ] ]
    }
}

多边形不是封闭的,它的结束不等于开始。
我已经绘制了坐标,将第一个坐标标记为红色,最后一个坐标标记为绿色:
points in the multipolygon

如您所见,d3 丢弃了最后一个坐标。

GeoJSON specification

A LinearRing is closed LineString with 4 or more positions. The first and last positions are equivalent (they represent equivalent points).



所以 d3 有一个点(没有双关语)并且 MultiPolygon 应该通过在末尾添加开始坐标来关闭:
...[ -73.4862, 42.0500 ], [ -73.0535, 42.0390 ] ] ] ]

关于d3.js - d3 和 Leaflet 之间的 GeoJSON 映射差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456176/

相关文章:

javascript - 如何使用 React 创建 d3 力布局图

javascript - D3 V4多系列折线图: Adding data points to lines with same color as lines

javascript - 无法将网格线添加到条形图

javascript - 三位数字的 Mapbox 标记符号

javascript - 如何强制传单更新 map ?

javascript - 将 javascript 转换为 json?

javascript - d3.js if else 正则表达式字符串匹配以更改样式

javascript - 使用 Leaflet 和本地主机拒绝位置访问

python - 快速将大型 csv 转换为 geojson

geojson - 没有找到适合 GeoJsonPoint 类型的构造函数