Reputation: 363
I have a GeoPandas df:
import geopandas as gpd
from shapely.geometry import LineString
geo_df = gpd.GeoDataFrame({'name': ['foo', 'bar', 'oof'], 'geometry': [[(5.239672304278279, 43.449400744605434), (5.291017601291771, 43.40657292095388)], [(5.27346289130589, 43.418074031107516), (4.935465352479518, 43.44997495014662)], [(4.935465352479518, 43.44997495014662), (5.64570049516335, 43.367506660731095)]]})
which looks like:
name geometry
0 foo [(5.239672304278279, 43.449400744605434), (5.2...
1 bar [(5.27346289130589, 43.418074031107516), (4.93...
2 oof [(4.935465352479518, 43.44997495014662), (5.64...
How do I transform the coordinate column into LineString (regardless of how many points are in the list of tuples)? E.g.:
name geometry
0 foo LINESTRING (5.239672304278279 43.449400744605434, 5.2...
1 bar LINESTRING (5.27346289130589 43.418074031107516, 4.93...
2 oof LINESTRING (4.935465352479518 43.44997495014662, 5.64...
Edit: I've tried (see Prateek's answer):
geo_df['geometry']=geo_df['geometry'].apply(lambda x: LineString(x))
as well as:
geo_list = [LineString(x) for i in geo_df['geometry'].tolist()]
geo_df['geometry'] = geo_list
Both return the following error:
AttributeError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.8/site-packages/shapely/speedups/_speedups.pyx in shapely.speedups._speedups.geos_linestring_from_py()
AttributeError: 'list' object has no attribute 'array_interface'
During handling of the above exception, another exception occurred:
AssertionError Traceback (most recent call last) in 17 geo_df['geometry'] = tuple_list 18 ---> 19 geo_df['geometry'] = geo_df['geometry'].apply(lambda x: LineString(x)) 20 21
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 4198 else: 4199 values = self.astype(object)._values -> 4200 mapped = lib.map_infer(values, f, convert=convert_dtype) 4201 4202 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
in (x) 17 geo_df['geometry'] = tuple_list 18 ---> 19 geo_df['geometry'] = geo_df['geometry'].apply(lambda x: LineString(x)) 20 21
~/opt/anaconda3/lib/python3.8/site-packages/shapely/geometry/linestring.py in init(self, coordinates) 46 BaseGeometry.init(self) 47 if coordinates is not None: ---> 48 self._set_coords(coordinates) 49 50 @property
~/opt/anaconda3/lib/python3.8/site-packages/shapely/geometry/linestring.py in _set_coords(self, coordinates) 95 def _set_coords(self, coordinates): 96 self.empty() ---> 97 ret = geos_linestring_from_py(coordinates) 98 if ret is not None: 99 self._geom, self._ndim = ret
~/opt/anaconda3/lib/python3.8/site-packages/shapely/speedups/_speedups.pyx in shapely.speedups._speedups.geos_linestring_from_py()
AssertionError:
Upvotes: 1
Views: 1137
Reputation: 369
That should be simple.
import geopandas as gpd
from shapely.geometry import LineString
geo_df = gpd.GeoDataFrame({'name': ['foo', 'bar', 'oof'], 'geometry': [[(5.239672304278279, 43.449400744605434), (5.291017601291771, 43.40657292095388)], [(5.27346289130589, 43.418074031107516), (4.935465352479518, 43.44997495014662)], [(4.935465352479518, 43.44997495014662), (5.64570049516335, 43.367506660731095)]]})
geo_df['geometry']=geo_df['geometry'].apply(lambda x: LineString(x))
Upvotes: 4