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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With