将一列多边形从字符串转换为GeoPandas几何 [英] Converting a column of Polygons from string to GeoPandas geometry

查看:547
本文介绍了将一列多边形从字符串转换为GeoPandas几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我的数据的样子

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.

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

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

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.

推荐答案

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

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

这篇关于将一列多边形从字符串转换为GeoPandas几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆