QgsProcessingException exécutant JoinAttributesByLocation

Sep 18 2020

J'essaye d'exécuter le code suivant

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

Mon intention est d'obtenir le script pour exécuter les attributs de jointure par emplacement pour tous les fichiers dans le test de dossier. Cependant, il renvoie l'erreur suivante:

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

Cela signifie que la couche n'a pas pu être trouvée. Je ne peux pas vraiment comprendre pourquoi je reçois ce message d'erreur car il fonctionne lors du remplacement de path_to_csv par un copier-coller des chaînes qu'il est censé combiner.

Je suppose donc que je spécifie le path_to_csv de manière erronée. Aucun conseil?

Edit 1: le cas échéant, c'est le code que j'ai exécuté avec succès:

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

Réponses

2 timpan Sep 18 2020 at 20:41

Je n'ai pas précisé qu'il s'agissait d'un 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'}

Maintenant, il fonctionne bien!