Puzzlemaster
Puzzlemaster

Reputation: 164

Converting DataFrame XYZ to Geopandas LineString

I have DataFrame as shown below

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)

I am converting points to Linestring

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

It successfully converts to LineString. But when i try to plot it, it says data is not numeric.

Any ideas?

Upvotes: 0

Views: 2213

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

  • your sample data does not contain lomdrivename so have used station
  • almost same technique as your code, using geometry of 3D points to generate LINESTRING
  • output shows this has worked by showing both lines and individual line
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"]

enter image description here

Upvotes: 1

Related Questions