zinfandel
zinfandel

Reputation: 458

Merge TopoJSON and only keep new objects

I'm trying to use TopoJSON's CLI to merge the shapes of US congressional districts by state, which works fine:

topomerge states=districts -k 'd.id.slice(0, 2)' < districts_topo.json > states_topo.json

(the merge is based on an ID field that starts with the state code)

However, the real reason I'm doing this merge is that I want a smaller output file with all unnecessary geometries removed. Is there a way to do this in TopoJSON? By default it copies over the existing districts object as well, creating a file that is bigger than the original input. Even if I manually remove the districts object, the file remains too large, presumably because the unused arcs are retained.

What's the best way to get a smaller file with only the merged geometry?

Upvotes: 0

Views: 321

Answers (2)

DGentry
DGentry

Reputation: 16248

I think there is a way to do it using the command line topojson tools:

topomerge -f 'false' districts=districts | toposimplify -f

The topomerge outputs an empty districts geometry, and then toposimplify will remove the unreferenced arcs.

Upvotes: 0

zinfandel
zinfandel

Reputation: 458

To answer my own question, I believe the only way to do this is to convert the object in question into GeoJSON and back ...

const statesGeo = topojson.feature(statesTopo, statesTopo.objects.states)
const onlyStatesTopo = topojson.topology({ states : statesGeo })

... which seems quite weird but works for my use case.

Upvotes: 1

Related Questions