当您创建一个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] )
如果您需要 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,])
Edit1
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);