簡體   English   中英

勻稱的循環不創建線串

[英]Shapely loop not creating Linestring

我正在嘗試將元組從 dataframe 轉換為線串。 這是我從 csv 文件導入的 dataframe 的一部分。

   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)

如何編寫此代碼以避免此錯誤並將元組轉換為 dataframe 中的列?

編輯最終解決方案

經過一番清理后發現,列decode保存為字符串"[(1,1),(2,3),(4,4),(1,3)]" ,首先需要將其轉換為元組列表。 在使用密集列表理解進行轉換后,LineString 轉換按預期工作

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

替代go 關於此的另一個選項是已經修復導入。 通過在ast.literal_eval的幫助下直接將字符串轉換為元組列表,如本SO Question中所建議的那樣

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

編輯前:我嘗試使用以下代碼重現您的錯誤。 但是,它運行得很好,沒有任何錯誤。

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 Object 而不是列表。

請驗證傳遞給 function linestringdecode linestringdecode()的參數decode是 list 類型而不是 string。

在本節中找到了答案。

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

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

編輯: eval() 使用起來很危險。 確保您使用的是受信任的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM