0

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

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 20, 2023 at 9:43
3
  • I think I may found an answer, referring to this source : gis.stackexchange.com/questions/284064/…. So I may need to add 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 by dissolve_layer = dissolve_result['OUTPUT'] Commented Apr 20, 2023 at 9:52
  • Yes that's how it is done Commented Apr 20, 2023 at 10:02
  • 1
    anitagraser.com/… Commented Apr 20, 2023 at 10:17

1 Answer 1

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}
 )
answered Apr 20, 2023 at 11:30

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.