5

I want to dissolve my layer by using this script and it is successful, however I want my dissolved layer to be temporary, and my input as any active layer. Can someone help me?

from qgis import processing

inlayer = r'C:\Users\CMCA\OneDrive - Boskalis\Documents\Trial PyQGIS\Tobe_Dissolved01.shp' outlayer = r'C:\Users\CMCA\OneDrive - Boskalis\Documents\Trial PyQGIS\dissolve.shp'

processing.run("native:dissolve", {'INPUT':inlayer, 'FIELD':[], 'OUTPUT':outlayer}) iface.addVectorLayer(outlayer, '', 'ogr')

Taras
  • 32,823
  • 4
  • 66
  • 137
PyCher
  • 171
  • 4

2 Answers2

9

In more recent versions of QGIS, do not forget to include the 'SEPARATE_DISJOINT' argument, see the Changelog for QGIS 3.26 for more details.

So, your code may look like this:

from qgis import processing
from qgis.core import QgsProject

layer = QgsProject.instance().mapLayersByName('Tobe_Dissolved01')[0]

params = { 'FIELD' : [], 'INPUT' : layer, 'OUTPUT' : 'memory:dissolve', 'SEPARATE_DISJOINT' : False }

dissolved = processing.run("native:dissolve", params)['OUTPUT']

QgsProject.instance().addMapLayer(dissolved)

Taras
  • 32,823
  • 4
  • 66
  • 137
7

Use 'TEMPORARY_OUTPUT' value for 'OUTPUT' parameter. Use the following lines:

processing.runAndLoadResults("native:dissolve",
                             {'INPUT':inlayer, 
                              'FIELD':[],
                              'OUTPUT': 'TEMPORARY_OUTPUT'})

If you use runAndLoadResults, you won't need to use iface.addVectorLayer.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • Traceback (most recent call last): File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 6, in TypeError: QgisInterface.addVectorLayer(): argument 1 has unexpected type 'QgsVectorLayer'

    I got an error like this ?

    – PyCher Jan 09 '23 at 09:48
  • Ok. I've edited the answer. Please check. – Kadir Şahbaz Jan 09 '23 at 09:55
  • oh .. Thank you sooo much it works! , but if it is not soo much trouble I'd like to understand what is the difference between runAndLoadResults and iface.addVectorLayer.? – PyCher Jan 09 '23 at 11:06
  • how about my input layer ? how can I make it select any layer? – PyCher Jan 09 '23 at 11:10
  • Can i make the dissolve output layer into a variable again? – PyCher Jan 11 '23 at 07:21
  • so last time, I run the script successfully , but when i Tried to run it again after closing my qgis , it gave me an error.

    from qgis import processing

    inlayDir = r'C:\Users\CMCA\OneDrive - Boskalis\Documents\Trial PyQGIS\cdc_1stfill.shp'

    processing.runAndLoadResults("native:dissolve", {'INPUT': 'inlayDir', 'FIELD':[], 'OUTPUT': 'TEMPORARY_OUTPUT'})

    #iface.addVectorLayer(output['OUTPUT'], 'Dissolved', 'ogr')

    – PyCher Jan 14 '23 at 06:47
  • File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 5, in File – PyCher Jan 14 '23 at 06:49
  • "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\tools\general.py", line 151, in runAndLoadResults return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback, File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins\processing\core\Processing.py", line 181, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: Unable to execute algorithm Could not load source layer for INPUT: inlayDir not found – PyCher Jan 14 '23 at 06:49