将 DataFrame XYZ 转换为 Geopandas LineString

Converting DataFrame XYZ to Geopandas LineString

我有如下所示的 DataFrame

gdf = gpd.GeoDataFrame(geo_df, geometry=gpd.points_from_xy(geo_df.x.astype(float),geo_df.y.astype(float)))


    x          y              z        station          geometry
651669.5725 4767831.32  -74.46883723    Loc1    POINT (651669.5725 4767831.32)
651529.8717 4767843.542 -77.35837037    Loc1    POINT (651529.8717 4767843.542)
651528.5995 4767846.217 -77.39481647    Loc2    POINT (651528.5995 4767846.217)
651455.8623 4767730.411 -73.44656122    Loc2    POINT (651455.8623 4767730.411)
651406.0155 4767551.434 -72.57809472    Loc2    POINT (651406.0155 4767551.434)
651403.4501 4767537.248 -72.721502      Loc2    POINT (651403.4501 4767537.248)

我正在将点转换为线串

geo_df = gdf.groupby('station')['geometry'].apply(lambda x: LineString(x.tolist()))

成功转换为LineString。但是当我尝试绘制它时,它说数据不是数字。

有什么想法吗?

  • 您的示例数据不包含 lomdrivename 所以使用了 station
  • 与您的代码几乎相同的技术,使用 3D 点的 几何 生成 LINESTRING
  • 输出显示这通过显示行和单独的行
import io
import shapely.geometry
import geopandas as gpd
import pandas as pd

df = pd.read_csv(io.StringIO("""    x          y              z        station          geometry
651669.5725  4767831.32   -74.46883723    Loc1    POINT (651669.5725 4767831.32)
651529.8717  4767843.542  -77.35837037    Loc1    POINT (651529.8717 4767843.542)
651528.5995  4767846.217  -77.39481647    Loc2    POINT (651528.5995 4767846.217)
651455.8623  4767730.411  -73.44656122    Loc2    POINT (651455.8623 4767730.411)
651406.0155  4767551.434  -72.57809472    Loc2    POINT (651406.0155 4767551.434)
651403.4501  4767537.248  -72.721502      Loc2    POINT (651403.4501 4767537.248)"""), sep="\s\s+", engine="python")

# convert back into a GeoDataFrame
df = gpd.GeoDataFrame(df, geometry=df.apply(lambda r: shapely.geometry.Point(r.x,r.y,r.z), axis=1))

# generate Linestrings grouping by station
gls = gpd.GeoSeries(df.groupby("station").apply(lambda d: shapely.geometry.LineString(d["geometry"].values)))
gls.plot()
gls.loc["Loc2"]