TransWikia.com

Joining attributes by location for multiple layers using PyQGIS

Geographic Information Systems Asked on August 27, 2021

Using the script below, I’ve created a list of catchment area corresponding to a list of outlets. It works perfectly but I would like to keep the attributes of my outlets in my catchment area layers (output_3 in my script).

In order to do so I’ve added a new output (output4) to join each outlet layer to its corresponding catchment area.

import processing
import os # This is is needed in the pyqgis console also
from qgis.core import (QgsVectorLayer)
#provide path to outflow points where x, y are stored in fields named 'x', 'y' (or rename geom.asPoint() to match your coordinates fields)
path_outlet = 'F:/outlet.shp'

#provide path to filled and clipped DEM
path_filled_DEM = 'F:/mnt_surcreuse.sdat'

#provide path where to store temporary outputs from processing algorythims 
path_to_temp_output1 = 'F:/upslope_raster{}.sdat'
path_to_temp_output2 = 'F:/polygonize{}.shp'
path_to_temp_output3 = 'F:/extract{}.shp'
path_to_temp_output4 = 'F:/final_output{}.shp'

#adds outflow point vector file to processing
outflow_points = QgsVectorLayer(path_outlet, "Outflow points layer", "ogr")


#iterates over all points in the outflow file. i is going to serve as polygone identifier
for i,f in enumerate(outflow_points.getFeatures()):

    #specifies geometry
    geom = f.geometry()

    #creates a raster file with upslope area
    output_1 = processing.run("saga:upslopearea", {'TARGET_PT_X': geom.asPoint().x(), 'TARGET_PT_Y': geom.asPoint().y(), 'ELEVATION': path_filled_DEM, 'METHOD': 0, 'AREA': path_to_temp_output1.format(str(i))})

    #converts raster to polygon
    output_2 = processing.run("gdal:polygonize", {'INPUT': output_1['AREA'], 'BAND': '1', 'OUTPUT': path_to_temp_output2.format(str(i))})

    #extracts only polygons with value 100
    output_3 = processing.run("qgis:extractbyattribute", {'INPUT': output_2['OUTPUT'], 'FIELD': 'DN', 'OPERATOR': '0', 'VALUE': '100', 'OUTPUT': path_to_temp_output3.format(str(i))})

    #add fields of each outlet layer to each corresponding upslope area
    output_4 = processing.run("qgis:joinattributesbylocation", {
        "TARGET": path_outlet,
        "JOIN": output_3,
        "PREDICATE": 'intersects',
        "SUMMARY": 0,
        "KEEP": 1,
        "OUTPUT": path_to_temp_output4.format(str(i))})

Unfortunately the script doesn’t work and I get this error message instead:

Traceback (most recent call last):
  File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/pluginsprocessingscriptScriptEditorDialog.py", line 224, in runAlgorithm
    exec(self.editor.text(), _locals)
  File "<string>", line 50, in <module>
  File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/pluginsprocessingtoolsgeneral.py", line 106, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File "C:/PROGRA~1/QGIS3~1.10/apps/qgis/./python/pluginsprocessingcoreProcessing.py", line 137, in runAlgorithm
    raise QgsProcessingException(msg)
_core.QgsProcessingException: There were errors executing the algorithm.
Could not load source layer for INPUT: no value specified for parameter

(line 50 corresponds to the last line of the script)

Could someone help me solving this issue?

I am using QGIS 3.10.

One Answer

I could be wrong but you don't seem to set an INPUT in the output4 declaration (hence the error message below your script returns).

Could not load source layer for INPUT: no value specified for parameter

I would replace JOIN with INPUT like below

output_4 = processing.run("qgis:joinattributesbylocation", {
        "TARGET": path_exutoires,
        "INPUT": output_3["OUTPUT"], # I've changed the JOIN to INPUT
        "PREDICATE": 'intersects',
        "SUMMARY": 0,
        "KEEP": 1,
        "OUTPUT": path_to_temp_output4.format(str(i))})

Correct answer by ThomasG77 on August 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP