Save/Export Custom Merged Multipolygons to JSON

37 views
Skip to first unread message

Zac Rinehart

unread,
Dec 7, 2017, 11:13:19 AM12/7/17
to d3-js
I have used an approach similar to: https://bl.ocks.org/mbostock/5416405 for creating custom merged regions. The only difference being mine are counties instead of the stats as in that example block. Here is an exact code excerpt from my project:

var set1 = d3.set([
  48111, 48421, 48195, 48357, 48295, 48205, 48341, 48233, 48393, 48211,
  48359, 48375, 48065, 48179, 48483, 48485, 48077, 48337, 48237, 48009,
  48503, 48023, 48269, 48125, 48107, 48303, 48219, 48079, 48501, 48445,
  48305, 48169,
  48117, 48381, 48011, 48129, 48087, 48369, 48069, 48437, 48045, 48191,
  48075, 48017, 48279, 48189, 48153, 48345, 48101, 48155, 48197, 48487
]);

var region1 = {type: "FeatureCollection", features: counties.filter(function(d) {return set1.has(d.id); })};

countiesG.append("path")
    .datum(region1)
    .attr("class", "region")
    .attr("d", path);

countiesG.append("path")
    .datum(topojson.merge(us, us.objects.counties.geometries.filter(function(d) {return set1.has(d.id); })))
    .attr("class", "region-boundary")
    .attr("d", path);

I will just provide one region's code above for reference. In reality, I repeated this process many times to create all of my desired merged conglomerate regions. Hard-coding all of this data was arduous and makes my code less readable. My hope is that the hard work is behind me, and I can somehow use these new merged multi-polygon regions in the future.

Now that I have hard-coded every region, can topojson somehow save the coordinates of these new merged multipolygons into the original us.json file or even a new json file? That way when I parse the json's coordinates it will have all of my custom merged regions -- which would allow me to delete all my hard-coded regions. I'm open to other approaches as well, as long as I can achieve my goal of exporting the merged multipolygon boundary coordinates.

Mike Tahani

unread,
Dec 12, 2017, 10:36:31 AM12/12/17
to d3...@googlegroups.com

You can wrap your merged geometries into a GeoJSON object and save that however you want, e.g.

let feature = topojson.merge(us, us.objects.counties.geometries.filter(function(d) {return set1.has(d.id); }))

let features = {
  type: 'FeatureCollection', 
  features: [
    {type: 'Feature', geometry: feature, properties: {}}
  ]
}

copy(JSON.stringify(feature, null, 2)) // only tested/working in Chrome dev tools

Result for just the feature above (from the Bostock example you linked): https://gist.github.com/miketahani/c31291432e1d958b27a16e7b92944852

You may want to look into node + D3 to automate this process so you’re not copy/pasting JSON for every merged geometry.


--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zac Rinehart

unread,
Dec 16, 2017, 1:37:23 AM12/16/17
to d3-js
Thanks for looking into this! I was wondering about the line: 
copy(JSON.stringify(feature, null, 2)) // only tested/working in Chrome dev tools

Should it be feature or features ? The latter seems to be more like a JSON format, but I'm kind of new at this, so I could be wrong.

And a follow-up question, if I did want to create a JSON that contained geometries for all of my custom regions, you were saying I would need node.js or some other way to automate that. I'll try that out, but I haven't used node before, and I've heard the learning curve is on the steep side. Could you perhaps show me how two custom geometries would be saved to a JSON file manually? That way I have a something to fall back to. Suppose I had:

var region1 = {type: "FeatureCollection", features: counties.filter(function(d) {return set1.has(d.id); })};
var region2 = {type: "FeatureCollection", features: counties.filter(function(d) {return set2.has(d.id); })};
Reply all
Reply to author
Forward
0 new messages