geojson - d3.geoPath() 返回错误 : <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z"

标签 geojson d3.js

编辑:重新创建了 jsfiddle https://jsfiddle.net/exLtcgrq/1/ 上的逻辑

我正在尝试使用 D3 V4 API 将简单的 GeoJSON 文件解析为 D3。

我的 GeoJSON 很简单:

{ "type": "FeatureCollection",
  "features": [
    { "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0]
        ]
      },
      "properties": {
        "id": "1",
        "Type": "campingspot"
      }
    },
    { "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [20.0, 65.0], [50.0, 80.0], [50.0, 110.0],[20.0, 115.0]
        ]
      },
      "properties": {
        "id": "1",
        "Type": "campingspot"
      }
    }
  ]
}

我使用 d3.json() 方法进行加载,并尝试使用 d3-geo api 将其转换为具有以下代码的路径:

var jsonData2 = d3.json("campingGeojson.json", function(error, json){
        svg.selectAll("path")
          .data(json.features)
          .enter()
          .append("path")
          .attr("d", d3.geoPath())
          .attr("stroke", "black")
          .attr("stroke-width", 1)
          .attr("fill", "green")
      });

chrome 上的控制台输出告诉我以下内容

Error: <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z".

如果您对使用 geoPath 方法出现的问题提出任何建议,我们将不胜感激。

谢谢。

最佳答案

geoJson 多边形的坐标是坐标数组的数组(坐标本身就是数组)。第一个数组表示壳,后面的数组表示孔。 所以我认为你的 geoJson 应该看起来更像:

"coordinates": [
 [ [10.0, 10.0], [60.0, 40.0], [50.0, 75.0],[20.0, 60.0] ]
    ]

关于geojson - d3.geoPath() 返回错误 : <path> attribute d: Expected number, "M,ZM,ZM,ZM,Z",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40424385/

相关文章:

javascript - SVG 分组的 DOM 运动动画

javascript - 使用 d3 的相同 X 轴多折线图

javascript - 具有力布局的散点图

geojson - 将 Geometry 转换为 GeoJSON 时 Geotools GeometryJSON 舍入坐标

javascript - 如何使用 L.CRS.Simple 在传单中显示由 geojson-vt 生成的(反子午线)矢量切片?

javascript - Google Maps API - Map.data 范围问题

javascript - SVG 向圆环图添加径向渐变

javascript - d3.js 中的 d3.max() 与 math.max() 有什么区别?

django - 用 Django 返回 GeoJson

javascript - Leaflet divIcons 设置在多边形特征后面