简体   繁体   中英

Fix invalid geometry in Geopandas

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM