0

How can i fix invalid geometries in a Geopandas dataframe. From:

gpdf_gml_geom_invalid = gpdf_gml_files[gpdf_gml_files.is_valid ==False]
gpdf_gml_geom_invalid.info()

I can see have invalid geoms. I have attempted using Shapely to understand what might be the source of invalid geom using

gpdf_gml_geom_invalid = gpdf_gml_geom_invalid.set_geometry('geometry')
explain_validity(gpdf_gml_geom_invalid.geometry)

Which throws the error

AttributeError: 'GeoDataFrame' object has no attribute '_geom'

Edit, adding data

4     MULTIPOLYGON (((526079.599 251118.907, 526080....
13    MULTIPOLYGON (((541228.102 252251.403, 541203....
16    MULTIPOLYGON (((546165.813 277723.432, 546164....
30    MULTIPOLYGON (((510680.266 267340.564, 510680....
37    MULTIPOLYGON (((520711.924 279690.049, 520721....
Name: geometry, dtype: geometry
mapping dom
  • 1,737
  • 4
  • 27
  • 50
  • really need some sample data to comment. `gpdf_gml_geom_invalid.head(5)` – Rob Raymond Dec 31 '21 at 14:16
  • Thanks, I'd looked at adapting this as well https://stackoverflow.com/questions/20833344/fix-invalid-polygon-in-shapely – mapping dom Dec 31 '21 at 14:22
  • 1
    @mappingdom - that's not sample data at all... that's `print(gdf)`. Note the `...` please try to post a [mre]. The issue is that you can't call shapely commands on a whole geopandas GeoArray/GeoSeries. Take your list of invalid geoms and use shapely to explain the validity of each one individually. – Michael Delgado Dec 31 '21 at 17:33

1 Answers1

0

This worked for me

def geometry_reviewer(object):
    """
    Simple function that returns output of shapely validation check
    ----------

    Returns
    -------
    <string>
      shapely output
    """  
    return explain_validity(object)

gpdf_gml_files['geometry'].apply(geometry_reviewer)

That will return something quite verbose which you can make easier to read using

gpdf_gml_files['geom_review_simple'] = gpdf_gml_files['geom_review'].str.split('[').str[0]
gpdf_gml_files.groupby(['geom_review_simple']).size()

The output of that summarises polygon validation issues you could then programmatically address

geom_review_simple
Interior is disconnected      3
Ring Self-intersection       32
Self-intersection            37
Valid Geometry              359
mapping dom
  • 1,737
  • 4
  • 27
  • 50