I have created two scripts for my QGIS project. I need to do a manual step after the first script for the second script to work. The second step involves a QGIS tool called "join by nearest". I would like to build this step into my script, but I have trouble running the tool from the Python console. It runs but does not give me an output layer. I am not sure if it doesn't do the job or just cannot provide the layer in the end.
I also want to accecss the vector layer from the current project and not put in the path of the file. I am not sure if I have done this correctly.
from qgis.core import *
import processing
# Replace 'input_layer_name' with the actual name of your vector layer in the QGIS project
input_layer_name = "layer name in project"
input_layer_name_2 = 'Abschnitte LAP Stufe 4'
output = "C:/Users/QGis LAP/Skripte/output.shp"
processing.run("native:joinbynearest", {
'INPUT': input_layer_name,
'INPUT_2': input_layer_name_2,
'FIELDS_TO_COPY': ['fid'],
'DISCARD_NONMATCHING': False,
'PREFIX': '',
'NEIGHBORS': 1,
'MAX_DISTANCE': None,
'OUTPUT': 'output'
})
output_layer = result['OUTPUT']
# Add the output layer to the map canvas
QgsProject.instance().addMapLayer(output_layer)
1 Answer 1
In the params you have the output 'output'
, which is just a string. But you need to use the variable output
Then replace processing.run(algo, params)
with the following:
processing.runAndLoadResults(algo, params)
Explore related questions
See similar questions with these tags.