JoinAttributesByLocation을 실행하는 QgsProcessingException

Sep 18 2020

다음 코드를 실행하려고합니다.

import os
directory_in_str='/Users/name/Desktop/test/'
directory = os.fsencode(directory_in_str)
tre='?type=csv&quote=&escape=&trimFields=Yes&skipEmptyFields=Yes&maxFields=10000&detectTypes=yes&xyDms=yes&xField=lon&yField=lat&crs=EPSG:4326&spatialIndex=yes&subsetIndex=no&watchFile=no'
start='file:///Users/name/Desktop/test/'
for file in os.listdir(directory):
     filename = os.fsdecode(file)
     if filename.endswith(".csv"): 
        path_to_csv=start+filename+tre
        processing.run("qgis:joinattributesbylocation", {'INPUT':path_to_csv,'JOIN':'/Users/name/Desktop/gis/NUTS_uppdelade/NUTS_ID_SE11.gpkg|layername=NUTS_ID_SE11','PREDICATE':[0,5],'JOIN_FIELDS':[],'METHOD':0,'DISCARD_NONMATCHING':True,'PREFIX':'','OUTPUT':'/Users/name/Desktop/gis/regn/vikter/NUTS_ID_SE111900Q1.gpkg'})

내 의도는 폴더 테스트의 모든 파일에 대해 위치별로 결합 속성을 실행하는 스크립트를 얻는 것입니다. 그러나 다음 오류를 반환합니다.

exec(open('/var/folders/ks/1x_p2hz95jldm5xvpjfg0gmm0000gn/T/tmpdq2z8ia3.py'.encode('utf-8')).read())
Traceback (most recent call last):
  File "/Applications/QGIS3.10.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.7/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 10, in <module>
  File "/Applications/QGIS3.10.app/Contents/MacOS/../Resources/python/plugins/processing/tools/general.py", line 106, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "/Applications/QGIS3.10.app/Contents/MacOS/../Resources/python/plugins/processing/core/Processing.py", line 137, in runAlgorithm
    raise QgsProcessingException(msg)
_core.QgsProcessingException: Kan inte köra algoritm
Kunde inte läsa in källager för INPUT: file:///Users/name/Desktop/test/1900Q1.csv?type=csv&quote=&escape=&trimFields=Yes&skipEmptyFields=Yes&maxFields=10000&detectTypes=yes&xyDms=yes&xField=lon&yField=lat&crs=EPSG:4326&spatialIndex=yes&subsetIndex=no&watchFile=no ej hittad

레이어를 찾을 수 없음을 의미합니다. path_to_csv를 결합 할 문자열의 복사 붙여 넣기로 대체 할 때 작동하기 때문에이 오류 메시지가 표시되는 이유를 이해할 수 없습니다.

그래서 나는 어떤 종류의 path_to_csv를 잘못 지정했다고 가정합니다. 어떤 충고?

편집 1 : 관련이 있다면 이것은 성공적으로 실행 한 코드입니다.

processing.run("qgis:joinattributesbylocation", {'INPUT':'file:///Users/name/Desktop/test/1900Q1.csv?type=csv&quote=&escape=&trimFields=Yes&skipEmptyFields=Yes&maxFields=10000&detectTypes=yes&xyDms=yes&xField=lon&yField=lat&crs=EPSG:4326&spatialIndex=yes&subsetIndex=no&watchFile=no','JOIN':'/Users/name/Desktop/gis/NUTS_uppdelade/NUTS_ID_SE11.gpkg|layername=NUTS_ID_SE11','PREDICATE':[0,5],'JOIN_FIELDS':[],'METHOD':0,'DISCARD_NONMATCHING':True,'PREFIX':'','OUTPUT':'/Users/name/Desktop/gis/regn/vikter/NUTS_ID_SE111900Q1.gpkg'})

답변

2 timpan Sep 18 2020 at 20:41

QgsVectorLayer임을 지정하지 않았습니다.

vlayer = QgsVectorLayer(path_to_csv, "temp", "delimitedtext")
processing.run("qgis:joinattributesbylocation", {'INPUT':vlayer,'JOIN':'/Users/name/Desktop/gis/NUTS_uppdelade/NUTS_ID_SE11.gpkg|layername=NUTS_ID_SE11','PREDICATE':[0,5],'JOIN_FIELDS':[],'METHOD':0,'DISCARD_NONMATCHING':True,'PREFIX':'','OUTPUT':'/Users/name/Desktop/gis/regn/vikter/NUTS_ID_SE111900Q1.gpkg'}

이제 원활하게 실행됩니다!