Following the update to QGIS 3.0, it has become very difficult to find any information concerning the writing of processing scripts in QGIS 3.0.
@Underdark (see here ) has provided a basis for the skeleton. This code also seems to have been added in QGIS, when writing a new script from template (QGIS 3.0.2).
However, I couldn't find any way to help Python newbies like me to understand how to change that code, especially for the input and output layers.
My goal is to write a script taking 2 raster layers and a double as input, outputting two layers.
What would be the changes required to the example code to allow that?
For QGIS 2.x I would have used the following syntax :
##Layer1=raster
##Layer2=raster
##myDouble=Double
##OutLayer1=output raster
##OutLayer2=output raster
From what I understand, the changes have to be made in the following procedure, but I'm not sure what to put in place.
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr("Input layer"),
[QgsProcessing.TypeVectorAnyGeometry]))
self.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr("Output layer"),
QgsProcessing.TypeVectorAnyGeometry))
On May 16th, the QGIS Python API documentation was released. However it is still unclear for me how to use it here. (Which might very well be a lack of Python knowledge)
Best Answer
With the transition from QGIS2.x to QGIS3.x the whole processing framework has been reworked and large parts of it run now as C++ classes that you can interact with using Python. Unfortunately the simple parameter syntax for data/dataset IO are no longer valid. The new parameter structure is much more orientated after the builtin (Python-) Processing algorithms that you find preinstalled in the toolbox.
As I see, you already followed the description of the new algorithm structure by @underdark. But in order to adjust this structure for your requirements (raster layers, double input, etc.) you have to change the code at multiple locations in the script. I have coded a rough example with a short explanation for you (just an algorithm skeleton based on @underdarks example):
Which steps are done?
QgsProcessingAlgorithm
.INPUT_RASTER_A = 'INPUT_RASTER_A'
) in order to reference your algorithm with the parameters provided by the processing framework.QgsProcessingParameterType
- in the case of your algorithm:QgsProcessingParameterRasterLayer
,QgsProcessingParameterNumber
, and so on. You can consult the API entries (ie.QgsProcessingParameterRasterLayer
) in order to pass the right arguments and construct the parameter objects.context
andfeedback
objects to theprocessAlgorithm()
method where you obtain the input datasets from the parameters at runtime (in this case QgsRasterLayer objects by using theparameterAsRasterLayer()
method, etc.).processAlgorithm()
.I hope I could give you some insights on how to design your python algorithms in QGIS3. Whenever you are stuck, it is always helpful to look at how existing algorithms of the processing framework handle the parameters. You can have a look at them here.