Here is the creation of an example GeoDataFrame, gdf
:
import shapely.wkt
import geopandas as gpd
s0 = shapely.wkt.loads("POINT(0 0)")
s1 = shapely.wkt.loads("POINT(1 1)")
gdf = gpd.GeoDataFrame({'geometry':[s0, s1],'data':['a','b']})
I would like to write its contents to a GeoJSON. I would like to have feature id for each feature, but not bounding box. Ideal output:
{
"type": "FeatureCollection",
"features": [
{ "id": "1", "type": "Feature", "properties": { "data": "a" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "id": "2", "type": "Feature", "properties": { "data": "b" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}
My attempt:
gdf.to_file("example.geojson")
example.geojson
becomes:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "data": "a" }, "geometry": { "type": "Point", "coordinates": [ 0.0, 0.0 ] } },
{ "type": "Feature", "properties": { "data": "b" }, "geometry": { "type": "Point", "coordinates": [ 1.0, 1.0 ] } }
]
}
No id
field. The thread Adding a feature id when writing to file provides a way:
gdf.__geo_interface__
is:
{'type': 'FeatureCollection',
'features': [{'id': '0',
'type': 'Feature',
'properties': {'data': 'a'},
'geometry': {'type': 'Point', 'coordinates': (0.0, 0.0)},
'bbox': (0.0, 0.0, 0.0, 0.0)},
{'id': '1',
'type': 'Feature',
'properties': {'data': 'b'},
'geometry': {'type': 'Point', 'coordinates': (1.0, 1.0)},
'bbox': (1.0, 1.0, 1.0, 1.0)}],
'bbox': (0.0, 0.0, 1.0, 1.0)}
I could write this into a text file, and I would have the id
s I want (from 0
, not from 1
, but I can live with that). This method however also adds bbox
fields, which I'd like to avoid.
How can I create a GeoJSON from a GeoDataFrame with Feature id
s, but without bbox
es using GeoPandas?
For those who just want to do it, no matter with what, here is thread doing the above with ogr2ogr
.