I have two processes which should run after each other, first dissolve, then multipart-singlepart. Until now I have worked with file-based layers but I would like to have the output from the dissolve process as an in-memory layer which should be used in the multipart-to-singlepart process.
Currently I have each process in a single script, but would like to have them integrated into one.
This is my code so far:
Script 'Dissolve'
from qgis.core import *
import os
import processing
pathInput = "path/to/input.shp"
pathOutput = "path/to/dissolved.shp"
# Dissolve features based on values in attribute table field
processing.run("native:dissolve",{'INPUT':pathInput,
'FIELD':['highway', 'maxspeed', 'surface'],
'OUTPUT':pathOutput}
Script 'Multipart to singlepart'
pathInput = "path/to/dissolved.shp"
pathOutput = "path/to/singlepart.shp"
processing.run("qgis:multiparttosingleparts",{'INPUT':pathInput,
'OUTPUT':pathOutput}
)
How can I run this without creating an actual dissolve.shp-file, but instead create and use an in-memory layer, so that I can integrate both processes into one script? I'm very sorry if this seems to be an easy task, however, I couldn't find the proper information on how to 'chain' those processes with temporary files, especially, because I don't know how to refer to the temporary dissolve file to use it in the next process.
System Windows 10
QGIS 3.28 Firenze
1 Answer 1
The solution to chain two processes and using in-memory layer is by assigning a name to the process and defining the output of the process as 'memory:'. In my case, the code for chaining the two process is:
from qgis.core import *
import os
import processing
pathInput = "path/to/input/file.shp"
pathOutput= "path/to/output/file.shp"
# Dissolve features based on values in an attribute table field
dissolve = processing.run("native:dissolve",{'INPUT':pathInput,
'FIELD':['highway', 'maxspeed', 'surface'],
'OUTPUT':'memory:'})
dissolve_layer = dissolve['OUTPUT']
# Apply multipart-to-singleparts on memory-layer and save result as file
processing.run("qgis:multiparttosingleparts",{'INPUT':dissolve_layer,
'OUTPUT':pathOutput}
)
dissolve_result = processing.run("native:dissolve",{'INPUT':pathInput, 'FIELD':['highway', 'maxspeed', 'surface'], 'OUTPUT':'memory:'}
and then I'll be able to refer to the output bydissolve_layer = dissolve_result['OUTPUT']