17. Scrierea unui plugin Processing
Depending on the kind of plugin that you are going to develop, it might be a better option to add its functionality as a Processing algorithm (or a set of them). That would provide a better integration within QGIS, additional functionality (since it can be run in the components of Processing, such as the modeler or the batch processing interface), and a quicker development time (since Processing will take of a large part of the work).
To distribute those algorithms, you should create a new plugin that adds them to the Processing Toolbox. The plugin should contain an algorithm provider, which has to be registered when the plugin is instantiated.
17.1. Creating from scratch
To create a plugin from scratch which contains an algorithm provider, you can follow these steps using the Plugin Builder:
Install the Plugin Builder plugin
Creați un plugin nou, utilizând Plugin Builder. În cazul în care Plugin Builder vă cere șablonul de utilizat, selectați „Furnizor Processing”.
Plugin-ul creat conține un furnizor cu un singur algoritm. Atât fișierul furnizorului cât și cel al algoritmului sunt complet comentate și conțin informații cu privire la modul de modificare a furnizorului și de adăugare a algoritmilor suplimentari.
17.2. Updating a plugin
If you want to add your existing plugin to Processing, you need to add some code.
In your
metadata.txt
file, you need to add a variable:hasProcessingProvider=yes
In the Python file where your plugin is setup with the
initGui
method, you need to adapt some lines like this:1from qgis.core import QgsApplication 2from processing_provider.provider import Provider 3 4class YourPluginName(): 5 6 def __init__(self): 7 self.provider = None 8 9 def initProcessing(self): 10 self.provider = Provider() 11 QgsApplication.processingRegistry().addProvider(self.provider) 12 13 def initGui(self): 14 self.initProcessing() 15 16 def unload(self): 17 QgsApplication.processingRegistry().removeProvider(self.provider)
You can create a folder
processing_provider
with three files in it:__init__.py
with nothing in it. This is necessary to make a valid Python package.provider.py
which will create the Processing provider and expose your algorithms.1from qgis.core import QgsProcessingProvider 2 3from processing_provider.example_processing_algorithm import ExampleProcessingAlgorithm 4 5 6class Provider(QgsProcessingProvider): 7 8 def loadAlgorithms(self, *args, **kwargs): 9 self.addAlgorithm(ExampleProcessingAlgorithm()) 10 # add additional algorithms here 11 # self.addAlgorithm(MyOtherAlgorithm()) 12 13 def id(self, *args, **kwargs): 14 """The ID of your plugin, used for identifying the provider. 15 16 This string should be a unique, short, character only string, 17 eg "qgis" or "gdal". This string should not be localised. 18 """ 19 return 'yourplugin' 20 21 def name(self, *args, **kwargs): 22 """The human friendly name of your plugin in Processing. 23 24 This string should be as short as possible (e.g. "Lastools", not 25 "Lastools version 1.0.1 64-bit") and localised. 26 """ 27 return self.tr('Your plugin') 28 29 def icon(self): 30 """Should return a QIcon which is used for your provider inside 31 the Processing toolbox. 32 """ 33 return QgsProcessingProvider.icon(self)
example_processing_algorithm.py
which contains the example algorithm file. Copy/paste the content of the script template file and update it according to your needs.
Now you can reload your plugin in QGIS and you should see your example script in the Processing toolbox and modeler.