【发布时间】:2018-08-07 16:23:56
【问题描述】:
我正在尝试使用 .json 文件绘制多边形。
*编辑添加样本坐标
{ "type": "FeatureCollection", "features": [ {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.0691250297447329,
51.5462448874379
],
[
-0.0691510961928943,
51.5459630404703
],
[
-0.0692056531364391,
51.5456827414947
],
[
-0.0692883661627076,
51.5454050640766
],
[
-0.0693070134960316,
51.545356361588
],.....
脚本看起来像
var width = 960;
var height = 600;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("/GeoJsonFiles/samplePolygon1.json", function (error, json) {
if (error) console.error(error);
var projection = d3.geoMercator()
.fitSize([width, height], json);
var path = d3.geoPath().projection(projection);
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", "gray")
.attr("stroke", "black");});
据我所知,没有错误,但 svg 没有显示任何内容。我也尝试过scale()center()和offset()之类的方法。到目前为止没有任何效果。这是我的第一个 D3 脚本 - 请帮忙。
【问题讨论】:
-
你的坐标是什么样的?在您的示例 geojson 中分享一些将是有益的。你也验证过geojson吗?
-
你好安德鲁!谢谢你的评论。请参阅样本坐标的更新。我使用了 [链接] (npm.runkit.com/geojson-validation),它使用
geojson-validation来验证坐标。他们是正确的。请问有什么想法吗? -
可以在 [link] (api.myjson.com/bins/vfga0) 上找到多边形的完整坐标
-
@Max 你用的是什么版本的 D3?
-
@altocumulus 它是 v4。我喜欢 d3.js 在顺时针方向使用右手规则/坐标,因此检查以确保多边形是顺时针方向的。希望有帮助
标签: javascript d3.js polygon geojson