Shapely循环未创建线串

问题描述 投票:0回答:1

我正在尝试将元组从数据帧转换为线串。这是从csv文件导入的数据框的一部分。

   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)]'

如果我手动将解码列的内容复制并粘贴到LineString()条件中,它将对其进行转换。我收到的错误发布在下面。

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


我最终想循环播放,因此我将其设置为dataframe列解码。这是我创建的最终将线串写入列的循环。

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)

如何编写此代码,以免发生此错误,并且可以将元组转换为数据框中的列?

python-3.x pandas geopandas
1个回答
0
投票

我尝试使用以下代码重现您的错误。但是,它运行得很好,没有任何错误。

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)

从您的错误消息AttributeError: 'str'中,我想可以推断出您的数据导入有问题。我的假设是,该解码具有dtype对象而不是列表。

请验证传递给函数decode的参数linestringdecode()是list类型,而不是字符串。

如果decode参数的类型以字符串形式返回,您可以尝试按照ast中的建议,使用SO question库将列解码形式解析为字符串。参见下面的代码

df = pd.read_csv("Test_Csv_With_List.csv", quotechar='"', converters={1:ast.literal_eval})
© www.soinside.com 2019 - 2024. All rights reserved.