Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a column of Polygons from string to GeoPandas geometry

Tags:

I have a dataframe stored as csv file, one column of which is Polygon object. However, this column is stored as strings instead of GeoPandas geometry object. How can I convert this column to Geopandas geometry object so that I can perform geo analysis?

This is how my data looks like

my_df['geometry'].head()
0    POLYGON ((-122.419942 37.809021, -122.419938 3...
1    POLYGON ((-122.419942 37.809021, -122.419938 3...
2    POLYGON ((-122.419942 37.809021, -122.419938 3...
3    POLYGON ((-122.419942 37.809021, -122.419938 3...
4    POLYGON ((-122.405659 37.806674, -122.405974 3...
Name: geometry, dtype: object

I want to convert this Pandas DataFrame to Geopandas GeoDataFrame, using the column 'geometry' as the Geopandas geometry column.

my_geo_df = gpd.GeoDataFrame(my_df, geometry=my_df['geometry'])

However, as the column is stored as strings, Geopandas.DataFrame() does not recognize it and therefore cannot actually create a GeoDataFrame.

TypeError: Input geometry column must contain valid geometry objects.
like image 209
xinyuanliu Avatar asked Jun 03 '19 19:06

xinyuanliu


1 Answers

The format of your polygon is WKT, so you have to convert it to shapely Polygon. Following Geopandas docs (https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html) do following

Using GeoPandas 0.9+:

df['geometry'] = gpd.GeoSeries.from_wkt(df['geometry'])
my_geo_df = gpd.GeoDataFrame(my_df, geometry='geometry')

Using older versions:

from shapely import wkt

df['geometry'] = df['geometry'].apply(wkt.loads)
my_geo_df = gpd.GeoDataFrame(my_df, geometry='geometry')
like image 142
martinfleis Avatar answered Sep 27 '22 21:09

martinfleis