Search code examples
pythongeopandasshapelywkt

How to filter WKT strings with invalid geometries in pandas or GeoPandas


I'm trying to convert an Excel file into a GeoPandas dataframe. I'm trying to use Shapely and WKT to do so.

After reading the file into pandas, I have a geometry column. I'm trying to do the following to convert the geometry column to an 'object' data type:

from shapely import wkt

my_df['geometry'] = my_df['geometry'].apply(wkt.loads)

and I receive the following error:

WKTReadingError: Could not create geometry because of errors while reading input.

To try and diagnose what the problem is, I also tried to convert this Excel file to a geodataframe, using the following code:

import geopandas

my_df = gpd.GeoDataFrame(my_df, geometry='geometry')

and I get the following error:

TypeError: Input must be valid geometry objects: MULTIPOLYGON (((1314112.145833299 1027703.927083313, 1314091.947916642 1027761.937499985, 1314232.583333299 1027811.447916642, 1314240.99999997 1027814.395833313, 1314246.739583299 1027794.468749985, 1314292.71874997 1027692.947916642, 1314282.18749997 1027689.010416642, 1314136.364583299 1027634.374999985, 1314112.145833299 1027703.927083313)))

It appears I only have one bad geometry? (Or it could just be the first bad geometry).

Can I just skip this bad one in the wkt.loads step? I could not find documentation or other examples of how to do this.


Solution

  • You can do the loop instead of apply with try/except to catch the erroneous geometry.

    from shapely import wkt
    
    geom = []
    
    for g in my_df['geometry']:
        try:
            geom.append(wkt.loads(g))
        except:
            geom.append(None)
    
    my_df['geometry'] = geom