1

I was trying to access processing algorithm using PyQGIS 3 on a standalone script. I found this solution and tried it but when I list the processing algorithm using print(QgsApplication.processingRegistry().algorithms()) it only lists the built-in plugins. The contour plugin that I wanted to import is not listed although it works just fine QGIS.

I have tried this code in QGIS Console:

import processing
QgsApplication.processingRegistry().algorithms()

and the contour plugin is listed there as <contour.ContourGeneratorAlgorithm.ContourGeneratorAlgorithm object at 0x7fe052e6aca8>, but when I tried in standalone script using this script:

sys.path.append('/usr/share/qgis/python/plugins/processing')

import processing
from processing.core.Processing import Processing
Processing.initialize()

print(QgsApplication.processingRegistry().algorithms())

it only lists the built-in plugins. Where is my mistake?

Btw, Contour plugin is an external QGIS plugin that make a contour from data points as an input. This plugin still in experimental state.

1

There's no standard way to handle importing algorithms from 3rd party plugins for standalone scripts. Depending on the plugin, there's an infinite number of ways this plugin may need to be initialized.

The general steps you'd need to take are:

  1. Find the original plugin source code (e.g. via online repository or just from the local plugins folder).

  2. Check what steps that plugin does to initialise and setups its algorithms. In the case of the contour plugin, you can see those here:

https://github.com/ccrook/QGIS-Contour-Plugin/blob/master/contour/ContourPlugin.py

So, specifically, you'd need:

contour_provider = ContourGeneratorProvider()
QgsApplication.processingRegistry().addProvider(contour_provider)
  1. Import the required classes. For the code above you'd need to import the ContourGeneratorProvider class. This may take a bit of sys path wrangling experiments to find the correct path combination to get this to work.
  • thank you for your answer, I've been successfully importing and running it. I append the sys path: /home/user/.local/share/QGIS/QGIS3/profiles/default/python/plugins, then i have to import it first using from contour.ContourGeneratorProvider import ContourGeneratorProvider then add it to the processing registry using your code. And then I have to import the processing module. This last step will imports all the processing algorithms, which i think a bit too excessive. But I have to do that though in order to run the contour plugin algorithm. – Ayato Jun 27 '18 at 8:25
  • is there a possibility to run the contour processing algorithm without importing all the built-in processing algorithm? – Ayato Jun 27 '18 at 8:26
  • Possibly - you have to check to see if the contour plugin requires these internally. – ndawson Jun 27 '18 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.