在D3中加载TopoJSON文件时出现随机行 [英] Random lines when loading a TopoJSON file in D3
问题描述
我正在尝试显示意大利制图的形状文件,例如这个,使用D3.js
I'm trying to display the Italian cartographic shapefiles, for example this one, using D3.js
我可以在 MapShaper 上加载Zip文件,查看地图,对其进行简化并导出到TopoJSON.我可以在MapShaper中加载TopoJSON,但看起来仍然没问题.
I can load the Zip file on MapShaper, look at the map, simplify it and export to TopoJSON. I can load back the TopoJSON in MapShaper and it still looks ok.
但是当我尝试使用D3.js显示它时,我得到了一堆意大利面:
But when I try to display it using D3.js, I get a bunch of spaghetti:
美女,不是吗?
代码直接来自示例.投影中心,旋转和平行线应该是意大利的标准格式,但这并不重要:地图仍然像意大利面条一样,可以选择任何投影.
The code is taken straight from the examples. The projection center, rotation, and parallels are supposed to be the canonical ones for Italy, but it doesn't really matter: the map remains spaghetti-like with any choice of projection.
width = 600
height = 1200
projection = d3.geo.albers()
.center [0, 41]
.rotate [347, 0]
.parallels [35, 45]
.scale 2000
.translate [width / 2, height / 2]
path = d3.geo.path()
.projection projection
svg = d3.select "body"
.append "svg"
.attr "width", width
.attr "height", height
d3.json "Reg2011_ED50.json", (json) ->
svg.append "path"
.datum topojson.feature json, json.objects.Reg2011_ED50
.attr "d", path
我在做什么错了?
推荐答案
我发现了一个旧的邮件列表帖子,其中详细介绍了如何导入这些确切的文件.
I found an old mailing list post detailing how to import those exact files.
基本上,我需要使用以下命令将投影转换为标准投影:
Basically I needed to convert the projection to a standard one with this command:
ogr2ogr -t_srs EPSG:4326 converted.shp original.shp
我不明白为什么需要它:我认为.prj
文件解决了投影差异.显然不是.
I don't understand why it's needed: I thought the .prj
file took care of projection differences. Apparently not.
这篇关于在D3中加载TopoJSON文件时出现随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!