带有 WKT 列的 DataFrame 到 GeoPandas 几何
DataFrame with WKT Column to GeoPandas Geometry
我写了一个脚本来查询 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...
现在我正尝试根据他们的 documentation:
将其写入带有 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'
我的地理Pandas:
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')
geopandas.GeoSeries.from_wkt
已添加到 GeoPandas 0.9.0 中。它在旧版本中不存在,这就是它在 0.8.1 中不起作用的原因。
我写了一个脚本来查询 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...
现在我正尝试根据他们的 documentation:
将其写入带有 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'
我的地理Pandas:
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')
geopandas.GeoSeries.from_wkt
已添加到 GeoPandas 0.9.0 中。它在旧版本中不存在,这就是它在 0.8.1 中不起作用的原因。