3

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)
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 15, 2023 at 9:24

1 Answer 1

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)
answered Nov 15, 2023 at 15:30
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.