简体   繁体   中英

Shapely loop not creating Linestring

I'm attempting to convert tuples from a dataframe into a linestring. This is part of my dataframe imported from a csv file.

   Unnamed: 0      name     route                                              decode
0           0    Funshine!  ofosF|mqaShJ@?rLh@d@veCIVd@LbEJfJ^f@lE?Rp@^L~g...  '[(-105.28, 39.999), (-105.282, 39.998), (-105.282, 39.99), (-105.28, 39.995), (-105.282, 39.99), (etc)]'

If I manually copy and paste the contents of the decode column into the LineString() condition, it converts it. The error I receive is posted below.

line = LineString(df.decode[0])
print(line)
Traceback (most recent call last):
  File "shapely\speedups\_speedups.pyx", line 86, in shapely.speedups._speedups.geos_linestring_from_py
AttributeError: 'str' object has no attribute '__array_interface__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/taylo/PycharmProjects/PermitProj/Polyline Decode.py", line 20, in <module>
    line = LineString(df.decode[1])
  File "C:\Users\taylo\Miniconda3\envs\ExcelPermitExe\lib\site-packages\shapely\geometry\linestring.py", line 48, in __init__
    self._set_coords(coordinates)
  File "C:\Users\taylo\Miniconda3\envs\ExcelPermitExe\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
AssertionError


I eventually would like to loop it, so I set it to the dataframe column decode. This is the loop that I've created to eventually write the linestring to the column.

def linestringdecode(name, decode):
    try:
        return LineString(decode)
    except:
        print(name)
        return np.nan

df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[3]), axis=1)

How do I write this so that I avoid this error and can convert the tuples to a column in my dataframe?

Edit final solution

After some clearing up it turns out that the column decode is saved as a string "[(1,1),(2,3),(4,4),(1,3)]" which first needs to be converted to a list of tuples. After converting with a dense list comprehension the LineString conversion works as supposed to

df['decode'] = [eval(ele) for ele in df.decode.str.strip()[:]]
df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[4]), axis=1)

Alternative Another option to go about this, is to already fix the import. By converting the string directly to list of tuples directly with the help of the ast.literal_eval , as suggested in this SO Question

import ast
df = pd.read_csv("Test_Csv_With_List.csv", quotechar='"', sep=",",converters={4:ast.literal_eval})

Before Edit: I tried to reproduce your error with the code below. However it runs through perfectly fine without any error.

from shapely.geometry import LineString
import pandas as pd

def linestringdecode(name, decode):
    try:
        return LineString(decode)
    except:
        print(name)
        return np.nan

data = {'Unamed 0': [0,1],
        'name': ['test','test2'],
        'rote': ['Gibberish','moreGib'],
        'decode': [[(-105.27983, 40.06008), (-105.27984, 40.05827)],[(-23, 23), (-22, 24)]]}

df = pd.DataFrame(data)

# print(df)
df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[3]), axis=1)

From your error message AttributeError: 'str' I think I can deduct that something is wrong with the import of your data. My Assumption is, that decode has the dtype Object rather than list.

Please validate that the passed argument decode to the function linestringdecode() is of type list and not string.

The answer was found in this section.

https://gis.stackexchange.com/questions/358068/converting-to-linestring-using-dataframe-column/

df['decode'] = df.decode.apply(lambda row: LineString(eval(row)))

Edit: eval() is dangerous to use. Make sure you're using trusted data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM