这是第一个topojson
如此提问。我在渲染地图(纽约市行政区)时遇到问题,并且不明白为什么。下面的代码只是一个副本这个例子 http://bl.ocks.org/4108203使用不同的 topojson 文件。我已上传文件here http://temp-share.com/show/FHKdqfs16。下面还详细介绍了我如何创建该文件。现在,我的线路很混乱。原因可能是 topojson 文件,但我不知道出了什么问题。
ps:我无法将其标记为topojson
因为该标签之前没有被使用过
TopoJSON 文件
1)从下载形状文件here http://www.nyc.gov/html/dcp/html/bytes/dwndistricts.shtml
(在“自治市和社区区”下的文件“自治市”(左),ArcView Shapefile)
2) 使用 QGis 简化 shapefile
3) 转换为 TopoJSON
ogr2ogr -f geoJSON nybb-geo.json nybb.shp
topojson -o nybb.json nybb-geo.json
HTML/JS 代码
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.boundary {
fill: none;
stroke: #000;
stroke-width: .5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 500;
var path = d3.geo.path();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("/geo/nybb.json", function(error, topology) {
svg.append("path")
.datum(topojson.object(topology, topology.objects['nybb-geo'].geometries[0]))
.attr("d", path)
.attr("class", "boundary");
});
</script>
正如用户 10579 的评论所建议的,我能够通过将 shapefile 重新投影到 NAD83 (EPSG 4269) 来解决该问题。从重新投影的 shapefile 创建 topojson 文件后,d3.js 显示地图
var projection = d3.geo.albers();
var path = d3.geo.path().projection(projection);
我遇到的第二个问题与正确的中心、比例和平移值有关。使用上面的代码 nyc 将只是一个带有大量空白的小点。找到正确的中心、比例和平移值可能有点乏味。最后,我添加了下面的代码,它允许您拖放地图并滚动以更改比例参数。每次更改后都会显示这些值,以便轻松将地图放置在正确的位置,并且只需采用控制台输出中的最后一个参数即可。
svg.call(d3.behavior.zoom()
.translate(projection.translate())
.scale(projection.scale())
.on("zoom", redraw));
function redraw() {
if (d3.event) {
projection
.translate(d3.event.translate)
.scale(d3.event.scale);
}
map.datum(topojson.object(topology, topology.objects.nyct2010))
.attr("d", path)
.attr("class", "boundary");
console.log("Current scale:" + projection.scale())
console.log("Current translate:" + projection.translate())
console.log("Current rotate:" + projection.rotate())
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)