2

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?

2

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)
  • I did it too but returns polygon instead of multipolygon, is that right? – Rodrigo Vargas Jun 28 at 10:10
  • No, when I type df.geom[0].type I get back MultiPolygon. – pan Jun 28 at 10:26
  • 1
    You're right. It's the same for me calling type function. I told that because when I called for df.head() it returns POLYGON(((421405.897134601 4585295.726121... – Rodrigo Vargas Jun 28 at 10:43

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.