3

I have a dataframe where one of the columns contains a list of x and y points and I would like to convert it to a spatial data frame but I am having trouble and not sure what to try next. My data frame looks something like this:

df['Location']
0     [(-80.58954, 44.60728), (-80.58942, 44.6073), ...
1        [(-81.09047, 44.74311), (-81.09004, 44.73771)]
2     [(-81.09004, 44.73771), (-81.09102, 44.75006),...
3     [(-81.09061, 44.7503), (-81.09036, 44.75045), ...
4        [(-81.08738, 44.75144), (-81.08759, 44.75657)]
                            ...
60    [(-80.48456, 44.35474), (-80.48454, 44.355), (...
61    [(-80.53522, 44.20374), (-80.53284, 44.20422),...
62    [(-80.52965, 44.20488), (-80.52937, 44.20494),...
63    [(-80.51611, 44.20489), (-80.51624, 44.2049), ...
64    [(-80.60715, 44.21462), (-80.60723, 44.21471),...

I have no problem when I take a single cell and convert it to LineString:

LineString(df['Location'][0]) 
<shapely.geometry.linestring.LineString object at 0x000001DBD35D0128>

However when I try and try and use the apply function I get some errors that I cant seem to figure out.

df['Location'].apply(lambda x: LineString(df['Location']))

or

df['Location'].apply(lambda x: LineString(x), axis=1)

Here is the error:

Traceback (most recent call last):
  File "shapely/speedups/_speedups.pyx", line 86, in shapely.speedups._speedups.geos_linestring_from_py
    array = ob.__array_interface__
  File "D:\Python36\Python36\lib\site-packages\pandas\core\generic.py", line 5175, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute '__array_interface__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python36\Python36\lib\site-packages\pandas\core\series.py", line 4042, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas\_libs\lib.pyx", line 2228, in pandas._libs.lib.map_infer
  File "<stdin>", line 1, in <lambda>
  File "D:\Python36\Python36\lib\site-packages\shapely\geometry\linestring.py", line 48, in __init__
    self._set_coords(coordinates)
  File "D:\Python36\Python36\lib\site-packages\shapely\geometry\linestring.py", line 97, in _set_coords
    ret = geos_linestring_from_py(coordinates)
  File "shapely/speedups/_speedups.pyx", line 166, in shapely.speedups._speedups.geos_linestring_from_py
    assert n == 2 or n == 3
AssertionError

3 Answers 3

5

You need to add x to lambda function instead of df["Location"].

import geopandas as gpd
...


geometry = df['Location'].apply(lambda x: LineString(x))
crs = {'init': 'epsg:4326'} # For example

gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
3

What if you try like this :

for index, row in df.iterrows():
   LineString(index[0])
3

I actually found my answer, it seems there is a bunch of coordinates that are points and they are not all LineStrings. I don't know if there is a way to input into a GeoDataFrame without first determining what type of geometry it is but I think that's a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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