2

I have difficulties with the processing package. I try to run a simple QGIS dissolve in PyQGIS 3. When I run the line :

from qgis.core import QgsVectorLayer
import processing
from processing.core.Processing import Processing
Processing.initialize()
layer = QgsVectorLayer(layerPath, 'layer 1', 'ogr')
parameters = {'INPUT': layer, 'OUTPUT': 'memory:'}
dissolved = processing.run('gdal:dissolve', parameters)

In QGIS Python Console, it works fine but when I try it in my script, it sends me the error message in the title.

I run my scripts with a batch that imports all necessary libraries.

The full message is following :

File "C:\Program Files\QGIS 3.2\apps\qgis\python\plugins\processing\tools\general.py", line 96, in run
 return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
File "C:\Program Files\QGIS 3.2\apps\qgis\python\plugins\processing\core\Processing.py", line 125, in runAlgorithm
 raise QgsProcessingException(msg)
_core.QgsProcessingException: Error: Algorithm gdal:dissolve not found
Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Aug 17, 2018 at 14:27

2 Answers 2

1

To check whether PyQGIS has a certain algorithm one can use the following code:

from qgis import processing
for alg in QgsApplication.processingRegistry().algorithms():
 if 'dissolve' in alg.id(): #here change the 'dissolve' into what you are looking for
 print(alg.id(), " : " , alg.displayName())

The statement above should provide such an output:

gdal:dissolve : Dissolve
grass7:v.dissolve : v.dissolve
native:dissolve : Dissolve

But your problem is not about the algorithm, it is about the deployment of the PyQGIS standalone scripts.

So, you may try this instead:

import sys
from qgis.core import (
 QgsApplication, 
 QgsProcessingFeedback, 
 QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms
# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Append the path where processing plugin can be found
sys.path.append('/docs/dev/qgis/build/output/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
layer = QgsVectorLayer(layerPath, 'layer 1', 'ogr')
parameters = {'INPUT': layer, 'OUTPUT': 'memory:'}
dissolved = processing.run('gdal:dissolve', parameters)

Another thing is that you are lacking parameters for "gdal:dissolve", because there should be : INPUT, FIELD, GEOMETRY, EXPLODE_COLLECTIONS, KEEP_ATTRIBUTES, COUNT_FEATURES, COMPUTE_AREA, COMPUTE_STATISTICS, STATISTICS_ATTRIBUTE, OPTIONS, OUTPUT.

Check them with this statement processing.algorithmHelp("gdal:dissolve"):

Dissolve (gdal:dissolve)
----------------
Input parameters
----------------
INPUT: Input layer
 Parameter type: QgsProcessingParameterFeatureSource
 Accepted data types:
 - str: layer ID
 - str: layer name
 - str: layer source
 - QgsProcessingFeatureSourceDefinition
 - QgsProperty
 - QgsVectorLayer
FIELD: Dissolve field
 Parameter type: QgsProcessingParameterField
 Accepted data types:
 - str
 - QgsProperty
GEOMETRY: Geometry column name
 Parameter type: QgsProcessingParameterString
 Accepted data types:
 - str
 - QgsProperty
EXPLODE_COLLECTIONS: Produce one feature for each geometry in any kind of geometry collection in the source file
 Parameter type: QgsProcessingParameterBoolean
 Accepted data types:
 - bool
 - int
 - str
 - QgsProperty
KEEP_ATTRIBUTES: Keep input attributes
 Parameter type: QgsProcessingParameterBoolean
 Accepted data types:
 - bool
 - int
 - str
 - QgsProperty
COUNT_FEATURES: Count dissolved features
 Parameter type: QgsProcessingParameterBoolean
 Accepted data types:
 - bool
 - int
 - str
 - QgsProperty
COMPUTE_AREA: Compute area and perimeter of dissolved features
 Parameter type: QgsProcessingParameterBoolean
 Accepted data types:
 - bool
 - int
 - str
 - QgsProperty
COMPUTE_STATISTICS: Compute min/max/sum/mean for attribute
 Parameter type: QgsProcessingParameterBoolean
 Accepted data types:
 - bool
 - int
 - str
 - QgsProperty
STATISTICS_ATTRIBUTE: Numeric attribute to calculate statistics on
 Parameter type: QgsProcessingParameterField
 Accepted data types:
 - str
 - QgsProperty
OPTIONS: Additional creation options
 Parameter type: QgsProcessingParameterString
 Accepted data types:
 - str
 - QgsProperty
OUTPUT: Dissolved
 Parameter type: QgsProcessingParameterVectorDestination
 Accepted data types:
 - str
 - QgsProperty
 - QgsProcessingOutputLayerDefinition
----------------
Outputs
----------------
OUTPUT: <QgsProcessingOutputVectorLayer>
 Dissolved

References:

answered Apr 30, 2022 at 8:41
0

Maybe try 'qgis:dissolve' instead of 'gdal:dissolve.

Or check that Processing Toolbox / Settings / Providers / GDAL is activated?

answered Jul 22, 2019 at 19:57

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.