使用 GeoPandas 将点几何图形转换为线串

win*_*ity 5 geometry point line latitude-longitude geopandas

我有一个gdf如下所示的地理数据框:

        longitude   latitude    geometry
8628    4.890683    52.372383   POINT (4.89068 52.37238)
8629    4.890500    52.371433   POINT (4.89050 52.37143)
8630    4.889217    52.369469   POINT (4.88922 52.36947)
8631    4.889300    52.369415   POINT (4.88930 52.36942)
8632    4.889100    52.368683   POINT (4.88910 52.36868)
8633    4.889567    52.367416   POINT (4.88957 52.36742)
8634    4.889333    52.367134   POINT (4.88933 52.36713)
Run Code Online (Sandbox Code Playgroud)

我试图将这些点几何图形转换成一条线。然而,下面的代码给出了一个错误:AttributeError: 'Point' object has no attribute 'values'

line_gdf = gdf['geometry'].apply(lambda x: LineString(x.values.tolist()))
line_gdf = gpd.GeoDataFrame(line_gdf, geometry='geometry')
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

swa*_*hai 6

LineString当您从地理数据框中的所有点创建 时,您仅获得 1 条线。以下是您可以运行来创建 LineString 的代码:

from shapely.geometry import LineString

# only relevant code here
# use your gdf that has Point geometry
lineStringObj = LineString( [[a.x, a.y] for a in gdf.geometry.values] )
Run Code Online (Sandbox Code Playgroud)

如果您需要 1 行的地理数据框并以此线串作为其几何形状,请继续执行以下操作:

import pandas as pd
import geopandas as gpd

line_df = pd.DataFrame()
line_df['Attrib'] = [1,]
line_gdf = gpd.GeoDataFrame(line_df, geometry=[lineStringObj,])
Run Code Online (Sandbox Code Playgroud)

编辑1

Pandas具有强大的聚合函数,可用于收集所有坐标(经度、纬度)以用于LineString()创建所需的几何图形。

为了读者的利益,我提供了这个可运行的代码来演示这种方法。

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
from shapely import wkt
from io import StringIO
import numpy as np

# Create a dataframe from CSV data
df5 = pd.read_csv(StringIO(
"""id longitude latitude
8628  4.890683  52.372383
8629  4.890500  52.371433
8630  4.889217  52.369469
8631  4.889300  52.369415
8632  4.889100  52.368683
8633  4.889567  52.367416
8634  4.889333  52.367134"""), sep="\s+")

# Using pandas' aggregate function
# Aggregate longitude and latitude
stack_lonlat = df5.agg({'longitude': np.stack, 'latitude':  np.stack})
# Create the LineString using aggregate values
lineStringObj = LineString(list(zip(*stack_lonlat)))

# (Previously use) Create a lineString from dataframe values
#lineStringObj = LineString( list(zip(df5.longitude.tolist(), df5.latitude.tolist())) )
# Another approach by @Phisan Santitamnont may be the best.

# Create a geodataframe `line_gdf` for the lineStringObj
# This has single row, containing the linestring created from aggregation of (long,lat) data
df6 = pd.DataFrame()
df6['LineID'] = [101,]
line_gdf = gpd.GeoDataFrame(df6, crs='epsg:4326', geometry=[lineStringObj,])

# Plot the lineString in red
ax1 = line_gdf.plot(color="red", figsize=[4,10]);
# Plot the original data: "longitude", "latitude" as kind="scatter"
df5.plot("longitude", "latitude", kind="scatter", ax=ax1);
Run Code Online (Sandbox Code Playgroud)

输出