topojson icon indicating copy to clipboard operation
topojson copied to clipboard

Merge multiple layers in a single topojson

Open kvalev opened this issue 2 years ago • 4 comments

Hey, I am trying to merge multiple geojson feature collections to a single topojson, but I cant seem to get it running.

I tried both:

objects = {
   "regions": regions_geojson,
   "municipalities": municipalities_geojson,
}

tp.Topology(objects)

and

tp.Topology([regions_geojson, municipalities_geojson])

but neither of them worked. Is this use case supported?

kvalev avatar Dec 19 '21 00:12 kvalev

No, that is not supported. But it would be cool. I know topojson supports multiple layers with single declared arcs, but I don't know what a good approach would be to handle this usecase. Any suggestions or approaches are much welcome!

mattijn avatar Dec 19 '21 12:12 mattijn

Thanks for the quick response! Unfortunately, I wont be of much help, as I am very much a newbie wrt to topojson. I just stumbled upon it, because one data visualization tool requires it.

kvalev avatar Dec 20 '21 12:12 kvalev

One simple solution: you can first merge the geojsons / geo-dataframes together and distinguish them by adding one field. Then in the generated topojson dict, replace the single object by creating a dict based on the field you created.

natsuapo avatar Mar 30 '22 11:03 natsuapo

@natsuapo, simple but effective. Sounds like an idea worth trying!

mattijn avatar Apr 07 '22 20:04 mattijn

This is now supported by means of multiple geo-dataframes per https://github.com/mattijn/topojson/pull/169 and is documented here: https://mattijn.github.io/topojson/example/input-types.html#list-of-geodataframes.

Usage for above mentioned example can be declared as such:

from topojson import Topology
topo = Topology(
    data=[regions, municipalities],  # can be either a GeoDataFrame or a GeoJSON FeatureCollection  
    object_name=['regions', 'municipalities']
)

Documentation is here:

  • list of gdf: https://mattijn.github.io/topojson/example/input-types.html#list-of-geodataframes
  • list of geojson: https://mattijn.github.io/topojson/example/input-types.html#list-of-geojson-objects

mattijn avatar Aug 26 '22 20:08 mattijn