将一列多边形从字符串转换为GeoPandas几何

时间:2019-06-03 19:00:55

标签: python polygon geopandas

我有一个存储为csv文件的数据框,其中一列是Polygon对象。但是,此列存储为字符串而不是GeoPandas几何对象。如何将该列转换为Geopandas几何对象,以便执行地理分析?

这就是我的数据的样子

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

我想使用“ geometry”列作为Geopandas geometry列将此Pandas DataFrame转换为Geopandas GeoDataFrame。

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

但是,由于该列存储为字符串,因此Geopandas.DataFrame()无法识别它,因此实际上无法创建GeoDataFrame。

TypeError: Input geometry column must contain valid geometry objects.

1 个答案:

答案 0 :(得分:0)

多边形的格式为WKT,因此必须将其转换为匀称的多边形。关注Geopandas文档(https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html)遵循

from shapely import wkt

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