0

I have a geodataframe gdf that looks like this:

        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)

I was trying to convert these point geometries into a line. However, the following code below gives an error: 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')

Any ideas ?

winecity
  • 195
  • 7

1 Answers1

0

When you create a LineString from all Points in a geodataframe, you get only 1 line. Here is the code you can run to create the 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] )

If you need a geodataframe of 1 row with this linestring as its geometry, proceed with this:

import pandas as pd
import geopandas as gpd

line_df = pd.DataFrame()
line_df['Attrib'] = [1,]
line_gdf = gpd.GeoDataFrame(line_df, geometry=[lineStringObj,])
swatchai
  • 14,086
  • 3
  • 34
  • 51