1

I have CSV file which I read with geopandas. I use "read_file",define the crs , but seems like it doesn't know to choose the correct geometry column. I have tried to use "set_geometry" but then I get the typeerror:

TypeError: Input must be valid geometry objects:...

When I save teh csv as shapefile and then read it it does identify the geoemtry column. This is sample of my dataframe:

>>>name       GEOMETRY                                                                        
0   Greens  MultiPolygon (((-53.75361862037225791 -16.81549027035698884, -53.7434949680008458 -16.80934187415235925, -53.73225824390273431 -16.81517224986364667, -53.74868930272544532 -16.82322876902832931, -53.75160449058108725 -16.81893549236820107, -53.75361862037225791 -16.81549027035698884)))     
1  Tomas    MultiPolygon (((-53.76220054670636728 -16.80317298894741285, -53.75584013683950957 -16.81220477095834909, -53.76697085410650345 -16.8175475152465097, -53.77237720249333108 -16.81086908488630982, -53.77364928446670689 -16.80069242909933891, -53.76220054670636728 -16.80317298894741285)))
2  Lilians  MultiPolygon (((-53.76402937655674918 -16.78765299523402277, -53.75130855682304087 -16.78781200548069208, -53.7503544953430108 -16.80424306430340664, -53.76662654391905249 -16.79772364418987962, -53.76402937655674918 -16.78765299523402277)))

When I read it from csv:


shapes = gpd.read_file('./shapes_csv/polygons.csv')
shapes.crs = 'epsg:4326'

#checking the dtypes of columns: 
#we get new additional geometry column which is null:
shapes.dtypes
>>>
name          object
GEOMETRY      object
id            object
geometry    geometry


#trying to use set geoemtry in otder to get geoemtry values:

shapes=shapes.set_geometry('GEOMETRY')

>>>TypeError: Input must be valid geometry objects: MultiPolygon (((-53.75361862037225791 -16.81549027035698884, -53.7434949680008458 -16.80934187415235925, -53.73225824390273431 -16.81517224986364667, -53.74868930272544532 -16.82322876902832931, -53.75160449058108725 -16.81893549236820107, -53.75361862037225791 -16.81549027035698884)))

If I save the csv as shapefile in qgis and try to read it, then the geoemtry column is identified no problem:

shapes = gpd.read_file('./shapes_csv/polygons.shp')
shapes.dtypes
>>>
id            object
geometry    geometry
dtype: object

as you can see ,as shape no additionl column is added and I get the correct geometry column to be identified as geometry. My question is why is this hapenning? and how can I set the correct geometry column from geodataframe that is read from csv?

3

When you open a csv file, with GeoPandas it automatically adds a geometry field, empty here

gdf = gpd.read_file('myFile.csv')
print(gdf)       
          GEOMETRY                                     name     geometry  
0  MULTIPOLYGON (((-53.753618620372258 -16.815490...   Greens     None
1  MULTIPOLYGON (((-53.762200546706367 -16.803172...    Tomas     None
2  MULTIPOLYGON (((-53.764029376556749 -16.787652...  Lilians     None

The format of yours MultiPolygons is WKT (in GEOMETRY), so you have to convert it to shapely MultiPolygons (Converting a column of Polygons from string to GeoPandas geometry, How do I import a .csv file with a column of coordinates for polygons to a geodataframe?,Creating a GeoDataFrame from a DataFrame with coordinates,...)

from shapely.wkt import loads
gdf.geometry =  gdf['GEOMETRY'].apply(loads)
2
  • Where does the r come from in this example?
    – Jon Mitten
    Aug 13 at 2:48
  • 1
    Corrected thanks
    – gene
    Aug 14 at 15:18

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.