0

I have a pandas DataFrame with geometry column with Polygons. I am trying to convert it to a geopandas dataframe and need to change the column type of geometry from string.

I get this error when attempting to apply Polygon from shapely.

import geopandas as gpd 
from shapely.geometry import Polygon

df_geo['geometry'][0]

POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))
                     

# convert geometry to Polygon

df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

df_geo



Traceback (most recent call last):

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_23632/494222449.py", line 3, in <module>
    df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/series.py", line 4357, in apply
    return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/apply.py", line 1043, in apply
    return self.apply_standard()

  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/apply.py", line 1098, in apply_standard
    mapped = lib.map_infer(

  File "pandas/_libs/lib.pyx", line 2859, in pandas._libs.lib.map_infer

  File "/var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/ipykernel_23632/494222449.py", line 3, in <lambda>
    df_geo['geometry'] = df_geo['geometry'].apply(lambda x: Polygon(eval(x)))

  File "<string>", line 1
    POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))
                         ^
SyntaxError: invalid syntax
1

1 Answer 1

1

Your polygon string is in wkt format. You can use shapely.wkt.loads:

import pandas as pd
import geopandas as gpd
import shapely

data = [['A', 'POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))']]
df = pd.DataFrame.from_records(data=data, columns=['name','g'])

df['geometry'] = df['g'].apply(lambda x: shapely.wkt.loads(x))
gdf = gpd.GeoDataFrame(data=df, geometry=df['geometry'], crs=4329)

If you have invalid wkt's you can find and drop them like this, prior to the code above:

import pandas as pd
import geopandas as gpd
import shapely
from shapely.errors import WKTReadingError

data = [['A', 'POLYGON ((-83.ZXCZXZCXZCZX30504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))'],
        ['B', 'POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))']]
df = pd.DataFrame.from_records(data=data, columns=['name','g'])

invalid = []

for ix in df.index:
    try:
        shapely.wkt.loads(df.iloc[ix].g) 
    except WKTReadingError: 
        invalid.append(ix) #If it is invalid, append its index to invalid list.
        
df = df.drop(invalid) #And delete all invalid rows
3
  • /Applications/Anaconda/anaconda3/lib/python3.9/site-packages/shapely/geos.py in read(self, text) 287 geom = self._lgeos.GEOSWKTReader_read(self._reader, c_string) 288 if not geom: --> 289 raise WKTReadingError( 290 "Could not create geometry because of errors " 291 "while reading input.") WKTReadingError: Could not create geometry because of errors while reading input.
    – kms
    Feb 26 at 13:27
  • Some of your row(s) contain invalid WKT strings
    – BERA
    Feb 26 at 13:30
  • can we either fix or ignore the invalid WKT string?
    – kms
    Feb 26 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.