11
votes

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);
});
2

2 Answers

17
votes

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/

4
votes

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.