使用topojson映射美国邮政编码时出错

时间:2013-08-18 04:13:01

标签: d3.js topojson

加载美国邮政编码topojson文件后,我在d3.js中收到错误groupdata在此行中未定义:

  function bind(group, groupData) {
     var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData;

错误:

Uncaught TypeError: Cannot read property 'length' of undefined 

我调用和创建路径的代码是:

  d3.json("data/us-atlas/us-zipcodes.json", function(error, topology) {
  svg.selectAll("path")
      .data(topojson.feature(topology, topology.objects.zipcodes).features)
      .enter()
      .append("path")
      .attr("d", path)
  });

我从此repo生成了zipcode topojson文件:https://github.com/mbostock/us-atlas。当我在加载时检查拓扑对象时,我在topology.objects.zipcodes下看到32893个弧。

我已经使用chloropleth示例http://bl.ocks.org/mbostock/4060606成功加载了县,并使用了类似的模式。

我正在使用d3.js版本3.2.8和topojson.js版本1.2.3。

有什么想法吗?它是一个糟糕的zipcode文件还是我称错了?

1 个答案:

答案 0 :(得分:6)

@Hugolpz - 对不起,我没有,我没有回应。我没有得到你的评论通知。

我终于明白了。我在这里录制它也许它会帮助别人。

我最初从美国人口普查网站上获得了我的邮政编码shapefile(目前由于政府关闭而下降)。它被称为tl_2012_us_zcta510.zip,是836MB。我尝试使用topojson使用参数@mbostock在这里建议转换它:http://bl.ocks.org/mbostock/4965422

转换花费了12个小时,给node.js 6GB内存,将shapefile转换为topojson文件。它仍然无法在d3.js中工作(请参阅原始问题中的错误)。另外,调试大量的json文件很难调试。原始shapefile也不会显示在QGIS中。

我最终放弃并搜索了不同的数据集。 Geocommons有一个5MB的zipcode shapefile,其中包含zipcode,state,name,population和area:http://geocommons.com/overlays/54893等属性。我把它交给了topojson,它在一分钟内转换了shapefile:

topojson \
-p name=PO_NAME \
-p zip=ZIP \
-p state=STATE \
-o zips_us_topo.json \
zip_codes_for_the_usa.shp

为了检查json文件以理解它,我使用https://github.com/einars/js-beautify使用此命令:

js-beautify zips_us_topo.json -o zips_us_topo_pretty.json

我使用非美化版本加载到浏览器中,因为它更小。

为了映射它,我基本上使用了与@ mbostock的县chloropleth地图相同的代码。如果你需要它或d3代码或清理topojson文件,你可以在这里得到它们:

https://gist.github.com/jefffriesen/6892860

http://bl.ocks.org/jefffriesen/6892860