Plotting topojson file with d3.js (NYC boroughs and census tracts)

僤鯓⒐⒋嵵緔 提交于 2019-12-05 22:44:31

As also suggested by user 10579's comment, I was able to solve the problem by re-projecting the shapefile to NAD83 (EPSG 4269). After creating the topojson file from the reprojected shapefile, d3.js shows the map with

var projection = d3.geo.albers();
var path = d3.geo.path().projection(projection);

The second problem I encountered was related to the correct center, scale, and translate values. With the code above nyc will just be a small dot with a lot of white space. Finding the correct center, scale, and translate values can be a little tedious. In the end, I added the code below, which allows you to drag and drop the map around and scroll to change the scale parameter. The values are displayed after each change so that it's easy to put the map in the right location and just adopt the last parameters from the console output.

  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())
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!