question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gpd.to_file could support GeoDataFrames with multiple geometry types

See original GitHub issue

Context

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. Polygon and MultiPolygon): 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 and Polygons) directly fails in gpd.to_file with 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:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
amine-aboufirasscommented, May 19, 2019

@josemarfdc Indeed it is… I tried it and it works. Thank you very much.

0reactions
josemarfdccommented, Apr 22, 2019

I just wrote this function and tested it for a simple use case. Probably not the best but might be useful to others who have similar problems:

def flatten_gdf_geometry(gdf, geom_type):
    geometry = gdf.geometry
    flattened_geometry = []

    flattened_gdf = geopandas.GeoDataFrame()

    for geom in geometry:
        if geom.type in ['GeometryCollection', 'MultiPoint', 'MultiLineString', 'MultiPolygon']:
            for subgeom in geom:
                if subgeom.type==geom_type:
                    flattened_geometry.append(subgeom)
        else:
            if geom.type==geom_type:
                flattened_geometry.append(geom)

    flattened_gdf.geometry=flattened_geometry

    return flattened_gdf

@awa5114 I believe this functionality is implemented with gdf.explode()

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found