You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何D3无法正确渲染该GeoJSON文件?

提问:为何D3无法正确渲染该GeoJSON?

以下是我的代码,目前D3无法正确渲染这份GeoJSON,求各位帮忙定位原因:

<!DOCTYPE html>
<meta charset="utf-8">

<body>
  <svg width="960" height="720"></svg>
  <div id='neighborhoodPopover'> </div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-array@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-geo@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-geo-projection@4"></script>
<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

const nycGeoJson = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "neighborhood": "Hamilton Heights",
        "boroughCode": null,
        "borough": null,
        "@id": null
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -73.96083942042421,
              40.81944719738439
            ],
            [
              -73.95280837990867,
              40.81581771326914
            ],
            [
              -73.9527010915481,
              40.81654806965256
            ],
            [
              -73.95171403863061,
              40.8179276097718
            ],
            [
              -73.95072698571312,
              40.81844695861872
            ],
            [
              -73.94803047114694,
              40.8221363829966
            ],
            [
              -73.94612073832833,
              40.82127625710196
            ],
            [
              -73.94475459746901,
              40.82419761694012
            ],
            [
              -73.94385337524001,
              40.82387305252829
            ],
            [
              -73.93918905697315,
              40.82999573657146
            ],
            [
              -73.948905,
              40.83406600000012
            ],
            [
              -73.949342,
              40.833982
            ],
            [
              -73.95015520996616,
              40.83439675981593
            ],
            [
              -73.95725896977153,
              40.82711730987318
            ],
            [
              -73.96127700238141,
              40.82169843098817
            ],
            [
              -73.96083942042421,
              40.81944719738439
            ]
          ]
        ]
      },
      "id": 169
    }
  ]
};

const projection = 
        d3.geoAlbers()
          .fitSize([width, height], nycGeoJson);

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


svg.selectAll("path")
    .data(nycGeoJson.features)
    .enter().append("path")
    .attr("d", path)


</script>
</body>

当前渲染结果(非正确路径):
当前渲染结果

正确效果应为:
正确渲染效果

原地图中的多个要素均出现此问题,推测是D3渲染地图数据的方式导致的异常,但无法定位具体原因,其他地图渲染库可正常渲染该GeoJSON。恳请各位提供相关见解,感谢您的时间与关注。


内容的提问来源于stack exchange,提问作者jbeard4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:02:09