Search code examples
geojsontopojson

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?


Solution

  • 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.