Escribir nuevos complementos de procesamiento

En función del tipo de complemento que vayas a desarrollar, puede ser más recomendable añadir la funcionalidad en cuestión como un algoritmo de Procesamiento (o un conjunto de algoritmos). Esta opción permite una mejor integración en QGIS, añadiendo la funcionalidad (ya que así podrá ser ejecutada dentro de cualquiera los componentes de menú Procesos, como el modelador gráfico o la interfaz de procesos por lotes), y optimizando el tiempo de desarrollo (ya que el complemento de Procesos hará gran parte del trabajo).

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.

To create a plugin from scratch which contains an algorithm provider, you can follow these steps using the Plugin Builder:

  • Instala el complemento Plugin Builder.

  • Crea un nuevo complemento, usando el Plugin Builder. En el cuadro de diálogo del Plugin Builder, selecciona «Processing provider».

  • The created plugin contains a provider with a single algorithm. Both the provider file and the algorithm file are fully commented and contain information about how to modify the provider and add additional algorithms. Refer to them for more information.

If you want to add your existing plugin to Processing, you need to add some code.

In your metadata.txt, 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:

from qgis.core import QgsApplication
from .processing_provider.provider import Provider


class YourPluginName():

    def __init__(self):
        self.provider = None

    def initProcessing(self):
        self.provider = Provider()
        QgsApplication.processingRegistry().addProvider(self.provider)

    def initGui(self):
        self.initProcessing()

    def unload(self):
        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.

    from qgis.core import QgsProcessingProvider
    
    from .example_processing_algorithm import ExampleProcessingAlgorithm
    
    
    class Provider(QgsProcessingProvider):
    
        def loadAlgorithms(self, *args, **kwargs):
            self.addAlgorithm(ExampleProcessingAlgorithm())
            # add additional algorithms here
            # self.addAlgorithm(MyOtherAlgorithm())
    
        def id(self, *args, **kwargs):
            """The ID of your plugin, used for identifying the provider.
    
            This string should be a unique, short, character only string,
            eg "qgis" or "gdal". This string should not be localised.
            """
            return 'yourplugin'
    
        def name(self, *args, **kwargs):
            """The human friendly name of your plugin in Processing.
    
            This string should be as short as possible (e.g. "Lastools", not
            "Lastools version 1.0.1 64-bit") and localised.
            """
            return self.tr('Your plugin')
    
        def icon(self):
            """Should return a QIcon which is used for your provider inside
            the Processing toolbox.
            """
            return QgsProcessingProvider.icon(self)
    
  • example_processing_algorithm.py which contains the example algorithm file. Copy/paste the content of the script template: https://github.com/qgis/QGIS/blob/release-3_4/python/plugins/processing/script/ScriptTemplate.py

Now you can reload your plugin in QGIS and you should see your example script in the Processing toolbox and modeler.