gpd.to_file could support GeoDataFrames with multiple geometry types
See original GitHub issueContext
Since Fiona 446 resolution (see PR 539), fiona.open allows to provide a schema with a list of geometry types, or even any types (using Unknown keyword)
Current behaviour
Currently, gpd.to_file fails for the following cases (using GeoJSON OGR driver):
- a gdf containing a shape type and the corresponding Multi shape type (e.g.
PolygonandMultiPolygon): the inferred schema contains only the simple shape type (e.g.Polygon), but then fiona raises the error below:File "fiona/ogrext.pyx", line 1182, in fiona.ogrext.WritingSession.writerecs fiona.errors.GeometryTypeValidationError: Record's geometry type does not match collection schema's geometry type: 'MultiPolygon' != 'Polygon' - a gdf containing containing heterogeneous geometries (e.g.
Points andPolygons) directly fails ingpd.to_filewith the error below:ValueError: Geometry column cannot contain mutiple geometry types when writing to file.
Suggested improvement
I suggest to support writing GeoDataFrames with multiple geometry types (without having to provide a schema): the inferred schema would just contain a list of geometry types (following Fiona new behaviour);
OGR drivers supporting multiple geometry types would succeed; others would probably fail anyways…
Let me know if there is a case I did not think about.
Ready to contribute
I would be glad to provide a PR if this functionality is desired (I already have some additional unit tests showing the current behaviour)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:10 (9 by maintainers)
Top Results From Across the Web
python - How do I write out a mixed geometry geodataframe to ...
Each layer of GeoPackage can contain a single geometry type not mixed. You have to save different geom types to separate layers.
Read more >geopandas.GeoDataFrame.to_file
Write the GeoDataFrame to a file. By default, an ESRI shapefile is written, but any OGR data source supported by Fiona can be...
Read more >How to save a geosdataframe with many geomertry columns ...
1 Answer 1 · as per comments, both geopandas and geojson only support one geometry per feature · hence your data frame is...
Read more >GeoPandas – Astraea Support Center
The primary difference is that a GeoDataFrame always contains a designated "geometry" column that is of type GeoSeries. When a spatial method is ......
Read more >Geometries (shapely) — Spatial Data Programming with Python
Spatial data can be divided into two categories: Vector layers—points, lines, and polygons, associated with attributes.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@josemarfdc Indeed it is… I tried it and it works. Thank you very much.
@awa5114 I believe this functionality is implemented with
gdf.explode()