带有WKT列的DataFrame到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...
现在我试图根据他们的文档将其写入带有 GeoPandas 的 shapefile :
我们使用 shapely.wkt 子模块来解析 wkt 格式:
from shapely import wkt
df['Coordinates'] = geopandas.GeoSeries.from_wkt(df['Coordinates'])
但是当我尝试做同样的事情时,我得到了:
AttributeError: type object 'GeoSeries' has no attribute 'from_wkt'
我的地理熊猫:
geopandas 0.8.1 py_0 conda-forge
回答
使用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')