I have a Geopandas data frame with multi-polygons geometries. Now, I want to convert the data frame into geojson. So, I have converted the dataframe into dict
and then used json.dump(dict)
to convert the data frame into json. This works well when I have single polygon but throws error TypeError: Object of type MultiPolygon is not JSON serializable
when the geometry column has multi-polygon. What is the best to convert geopandas dataframe into json
seraliazble
whether the geometry is multi-polygon or polygon.
df=
location geometry
1 MULTIPOLYGON (((-0.304766 51.425882, -0.304904...
2 MULTIPOLYGON (((-0.305968 51.427425, -0.30608 ...
3 MULTIPOLYGON (((-0.358358 51.423471, -0.3581 5...
4 MULTIPOLYGON (((-0.357654 51.413925, -0.357604...
list_data = df.to_dict(orient='records')
print(json.dumps(list_data))
Error:-
TypeError: Object of type MultiPolygon is not JSON serializable
You could use geopandas.GeoDataFrame.to_json.
Something like this:
Result: