Converting a column of Polygons from string to GeoPandas geometry

左心房为你撑大大i 提交于 2019-12-06 12:14:52

问题


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.

回答1:


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')


来源:https://stackoverflow.com/questions/56433138/converting-a-column-of-polygons-from-string-to-geopandas-geometry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!