mattijn / topojson

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

Converting TopoJson to a GeoJSon object_name not changing #221

Open j-alves-c opened 1 week ago

j-alves-c commented 1 week ago

mapa_plot py - SearchWeb - Visual Studio Code 26_06_2024 10_26_02 (1)

I can't do the conversion to GeoJson file using the .to_geojson() with a different object_name parameter other than default (data).

I have objects with different names (e.g. example) { "type": "Topology", "transform": { "scale": [0.0005000500050005, 0.00010001000100010001], "translate": [100, 0] }, "objects": { "example": { "type": "GeometryCollection", "geometries": [ { "type": "Point", "properties": { "prop0": "value0" }, "coordinates": [4000, 5000] }, { "type": "LineString", "properties": { "prop0": "value0", "prop1": 0 }, "arcs": [0] }, { "type": "Polygon", "properties": { "prop0": "value0", "prop1": { "this": "that" } }, "arcs": [[1]] } ] } }, "arcs": [ [[4000, 0], [1999, 9999], [2000, -9999], [2000, 9999]], [[0, 0], [0, 9999], [2000, 0], [0, -9999], [-2000, 0]] ] }

if a pass as object_name='example',

topo = Topology(topojson_data) geojson_data = topo.to_geojson(object_name="example")

it still does not read the file and send the error:

'data' is not an object name in your topojson file

I could only read my file by editing the topojson and changing "example" to "data"

mattijn commented 1 week ago

Thanks for rasing @j-alves-c.

If you are using a topojson file as input, then you have to explicitly provide the object_name of your object when loading it into the Topology class, as such:

import json
import topojson as tp

filepath = ''
with open(filepath) as f:
    topojson_data = json.load(f)

topo = tp.Topology(topojson_data, object_name='example')
geojson_data = topo.to_geojson()

Hope this helps!