1

I'm trying to create a linestring out of a dataframe of latitudes and longitudes.

geometry = [Point(xy) for xy in zip(linkdf.lon, linkdf.lat)]
linkgdf = GeoDataFrame(linkdf, geometry=geometry)
linkdf_line = linkgdf.groupby(['linkId'])['geometry'].apply(lambda x: x.tolist())
linkdf_line = linkdf_line.apply(lambda x: LineString(x))
linkgdf_line = GeoDataFrame(linkdf_line, geometry='geometry')

Where linkdf is just a dataframe of latitudes and longitudes: linkdf

Unfortunately, I keep getting this error:

ValueError: Input [<shapely.geometry.point.Point object at 0x000002AB98131AC8>, <shapely.geometry.point.Point object at 0x000002AB98131198>] is the wrong shape for a LineString

What is really confusing me is that this seems to work in a jupyter notebook, but not running my python script in terminal.

  • Is there a reason you are converting each coordinate to a shapely Point() first rather than just going straight to a LineString? – Jon Jun 17 at 17:27
  • no - I assumed LineString would need shapely Points instead of just passing it strings of points... let me see what works – Geoff Perrin Jun 17 at 17:29
  • Well, LineString should take either Points or coordinate tuples (or even a mix of both), but given your error I might try to pass in tuples directly--may fix or shed more light on the issue. – Jon Jun 17 at 17:32
  • Ok, that seemed to work! Thank you! – Geoff Perrin Jun 17 at 17:34
  • Cool. You should add the answer (whatever you did) to your question and accept it. – Jon Jun 17 at 17:36
0

passing lat / lon directly as a string tuple fixed the problem - didn't even need to convert to shapely point to turn into LineString

geometry = [xy for xy in zip(linkdf.lon, linkdf.lat)]
linkdf['geometry'] = geometry
linkdf_line = linkdf.groupby(['linkId'])['geometry'].apply(lambda x: x.tolist())
linkdf_line = linkdf_line.apply(lambda x: LineString(x))
linkgdf_line = GeoDataFrame(linkdf_line, geometry='geometry')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.