【发布时间】:2020-11-16 10:08:26
【问题描述】:
我在 D3.js 中绘制地图时遇到问题
以下代码适用于某些 json 文件,但在我需要使用的数据上,它会返回类似于以下的错误字符串:
错误:属性 d:预期数字,“….21478248741596,NaNL547.70469610…”。
并显示这张图片(虽然漂亮不是我希望的英格兰地图)
这是代码:
const width = 800
const height = 800
const svg = d3.select('svg')
.append('g')
.attr('class', 'map');
const projection = d3.geoMercator()
Promise.all([
d3.json('https://raw.githubusercontent.com/joewretham/d3_map_project/main/Clinical_Commissioning_Groups__April_2019__Boundaries_EN_BUC.json')
]).then(
d => ready(null, d[0], d[1])
);
function ready(error, data) {
var fixed = data.features.map(function(feature) {
return turf.rewind(feature, {
reverse: true
});
});
const path = d3.geoPath().projection(projection);
projection.fitSize([width, height], {
"type": "FeatureCollection",
"features": fixed
})
svg.append('g')
.attr('class', 'countries')
.selectAll('path')
.data(fixed)
.enter().append('path')
.attr('d', path)
.style('fill', "teal")
.style('stroke', 'white')
.style('opacity', 0.8)
.style('stroke-width', 0.3);
};
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<svg id="partitionSVG" width="800" height="800">"</svg>
有人知道这里发生了什么吗?因为该代码适用于其他数据集,所以我知道这与坐标有关,我猜它们需要以某种方式进行转换,但我对地理数据了解得不够多,不知道该怎么做。
感谢您的任何建议
乔
【问题讨论】:
-
请使用堆栈 sn-ps 或 JSFiddle/Codepen 将您的代码转换为可运行的 minimal reproducible example。它将帮助我们理解问题并带来更好的答案
-
您的 JSON 文件有一些不常见的投影。通常,坐标以纬度/经度给出,但有许多不同的投影 - 最常见的是 WGS84(也称为 4326,由它的 ID)。当前的投影似乎距离某个零点以米或英尺为单位。我不知道当前的投影,如果你找到了,你可以将JSON转换为WGS84。
-
嗨鲁本 - 感谢您的回复。你带我找到解决方案 - 我回到从 (geoportal.statistics.gov.uk/datasets/…) 获取数据的地方,我发现他们可用的 API 具有 WGS84 中的坐标。更新了小提琴 - jsfiddle.net/joe_wretham/fm1ud063/13。感谢您的帮助
-
太棒了!乐于助人!
标签: javascript json d3.js maps geojson