Converting a column of Polygons from string to GeoPandas geometry

xinyuanliu picture xinyuanliu · Jun 3, 2019 · Viewed 7.5k times · Source

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.

Answer

martinfleis picture martinfleis · Jun 3, 2019

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

from shapely import wkt

df['geometry'] = df['geometry'].apply(wkt.loads)
my_geo_df = gpd.GeoDataFrame(my_df, geometry='geometry')