11

I am having a lot of fun playing with topojson, but it looks like topojson.object is undefined in V1 of topojson, where it was supported in V0. Can someone explain how I might work around this issue? I am trying to draw distinct path elements for each polygon in an input file formatted as topojson. the code is:

d3.json("maTopo.json", function(error, ma) {
    svg.selectAll(".subunit")
      .data(topojson.object(ma, ma.objects.ma).geometries)
    .enter().append("path")
      .attr("class", function(d) { return "subunit " + d.id; })
      .attr("d", path);
});
Mark Roper
  • 1,389
  • 1
  • 16
  • 27

2 Answers2

17

You can use topojson.feature instead.

d3.json("maTopo.json", function(error, ma) {
  svg.selectAll(".subunit")
      .data(topojson.feature(ma, ma.objects.ma).features)
    .enter().append("path")
      .attr("class", function(d) { return "subunit " + d.id; })
      .attr("d", path);
});

A detailed example can be found here: http://bost.ocks.org/mike/map/

mbostock
  • 51,423
  • 13
  • 175
  • 129
Pablo Navarro
  • 8,244
  • 2
  • 43
  • 52
4

The v1 release replaced topojson.object with topojson.feature; the behavior is similar, but the new topojson.feature method returns a Feature or FeatureCollection (rather than a Geometry or GeometryCollection) for better compatibility with GeoJSON.

@mbostock's words from this thread. So change just one string in your code to this:.data(topojson.feature(ma, ma.objects.ma).features). And I guess you also should regenerate your TopoJSON file with v1 from GeoJSON.

Community
  • 1
  • 1
KoGor
  • 237
  • 5
  • 12