带有 WKT 列的 DataFrame 到 GeoPandas 几何

TOu*_*ang 1 python pandas geopandas

我写了一个脚本来查询 PostGIS 数据库,返回一个 Pandas 数据框,如下所示:

   ID  ...                          WKT
0   1  ...  LINESTRING(1.5047434 42.6319022,1.5053385 42.6...
1   2  ...  LINESTRING(1.5206333 42.5291144,1.5206306 42.5...
Run Code Online (Sandbox Code Playgroud)

现在我试图根据他们的文档将其写入带有 GeoPandas 的 shapefile :

我们使用 shapely.wkt 子模块来解析 wkt 格式

from shapely import wkt

df['Coordinates'] = geopandas.GeoSeries.from_wkt(df['Coordinates'])
Run Code Online (Sandbox Code Playgroud)

但是当我尝试做同样的事情时,我得到了:

AttributeError: type object 'GeoSeries' has no attribute 'from_wkt'
Run Code Online (Sandbox Code Playgroud)

我的地理熊猫:

geopandas                 0.8.1                      py_0    conda-forge
Run Code Online (Sandbox Code Playgroud)

mar*_*eis 14

geopandas.GeoSeries.from_wktGeoPandas 0.9.0 中添加了API 。它在旧版本中不存在,这就是它在 0.8.1 中不起作用的原因。


Pea*_*eka 5

使用shapely.wkt.loads创建几何列。

import geopandas as gpd
from shapely import wkt


df['geometry'] = df.WKT.apply(wkt.loads)
df.drop('WKT', axis=1, inplace=True) #Drop WKT column

# Geopandas GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry='geometry')

#Export to shapefile
gdf.to_file('myshapefile.shp')
Run Code Online (Sandbox Code Playgroud)