GeoPandas Error – Fixing ‘Input Geometry Column Must Contain Valid Geometry Objects’ TypeError

geometrygeopandaspandas

I'm working in a urban biodiversity project and I have a csv file for a birds inventory with a column index called 'geom' composed by multypolygons as this:

        name    geom                                                id  
0   Abubilla    MULTIPOLYGON(((421405.897134601 4585295.726121...   83  
1   Abubilla    MULTIPOLYGON(((424405.897979997 4584795.702582...   83  
2   Abubilla    MULTIPOLYGON(((424905.87219322 4581295.6934019...   83  
3   Abubilla    MULTIPOLYGON(((424905.883573821 4582795.695714...   83  
4   Abubilla    MULTIPOLYGON(((424905.906345894 4585795.700331...   83  

the column 'geom' is not recognized as geometry.

when I run

birds_geo = birds.set_geometry('geom')

it returns the following error:

TypeError: Input geometry column must contain valid geometry objects.

I've tried this script which I found for a similar question:

from shapely.wkt import loads
birds['geom'] = birds['geom'].apply(lambda x: loads(x))
for index, row in birds.iterrows():
    birds.set_value(index, 'geom', loads(row['geom']))

but it seems to work for polygons but not for multipolygons, returning the following error:

AttributeError: 'MultiPolygon' object has no attribute 'encode'

Is there a way to make to turn multipolgons into a valid geometry index?

Best Answer

I tested the following and works fine:

from shapely import wkt
import pandas as pd

df = pd.DataFrame(
    {'name': ['testname', 'testname'],
     'geom': ['MULTIPOLYGON (((21.78 40.36, ... 21.77 40.35)))',
              'MULTIPOLYGON (((21.78 40.36, ... 21.77 40.35)))']})

df['geom'] = df['geom'].apply(wkt.loads)