mattijn / topojson

Encode spatial data as topology in Python! 🌍 https://mattijn.github.io/topojson
BSD 3-Clause "New" or "Revised" License
177 stars 27 forks source link

Merge multiple layers in a single topojson #147

Closed kvalev closed 2 years ago

kvalev commented 2 years ago

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?

mattijn commented 2 years ago

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!

kvalev commented 2 years ago

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.

natsuapo commented 2 years ago

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.

mattijn commented 2 years ago

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

mattijn commented 2 years ago

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: