2

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.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Erich Purpur
  • 1,337
  • 3
  • 14
  • 34

2 Answers2

4

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
martinfleis
  • 7,124
  • 2
  • 22
  • 30
0

Try this simple code to plot the valid geometries only:

my_df[ my_df.geometry.is_valid ].plot(ec='black', alpha=0.3)

To get only valid geometries in a new geodataframe:

valid_gdf = my_df[ my_df.geometry.is_valid ]

Edit

My answer does not answer the question directly. But it is clearly that the errors involve bad geometries. Thus I provide the code that helps check the validity of the geometry.

swatchai
  • 17,400
  • 3
  • 39
  • 58