使用d3.js打印topojson文件(纽约市行政区和普查区)

使用d3.js打印topojson文件(纽约市行政区和普查区),d3.js,shapefile,map-projections,D3.js,Shapefile,Map Projections,这是第一个问题。我在绘制纽约市行政区地图时遇到问题,无法找出原因。下面的代码只是带有不同topojson文件的一个副本。我已经上传了文件。下面是我如何创建该文件的详细信息。现在,我只是得到混乱的线条。可能是因为topojson文件,但我不知道出了什么问题 ps:我无法将其标记为topojson,因为以前从未使用过该标记 TopoJSON文件 1从下载shapefile 在“Borough&Community Districts”下,文件“Boroughs”左侧为ArcView形状文件 2使用QG

这是第一个问题。我在绘制纽约市行政区地图时遇到问题,无法找出原因。下面的代码只是带有不同topojson文件的一个副本。我已经上传了文件。下面是我如何创建该文件的详细信息。现在,我只是得到混乱的线条。可能是因为topojson文件,但我不知道出了什么问题

ps:我无法将其标记为topojson,因为以前从未使用过该标记

TopoJSON文件

1从下载shapefile

在“Borough&Community Districts”下,文件“Boroughs”左侧为ArcView形状文件

2使用QGis简化形状文件

3使用

ogr2ogr -f geoJSON nybb-geo.json nybb.shp
topojson -o nybb.json nybb-geo.json
HTML/JS代码


正如用户10579的评论所建议的,我能够通过将shapefile重新投影到NAD83 EPSG 4269来解决这个问题。从重新投影的shapefile创建topojson文件后,d3.js显示带有

var projection = d3.geo.albers();
var path = d3.geo.path().projection(projection);
我遇到的第二个问题与正确的中心、比例和平移值有关。有了上面的代码,纽约将只是一个有大量空白的小点。找到正确的中心、比例和平移值可能有点乏味。最后,我添加了下面的代码,允许您拖放地图并滚动以更改比例参数。每次更改后都会显示这些值,这样就可以轻松地将地图放置在正确的位置,并只采用控制台输出中的最后一个参数

  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())
  }

只是猜测而已。尝试将shapefile重新投影到wgs84,然后重试。map变量的定义是什么?能否提供指向JS Fiddle的链接?
  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())
  }