2

I have troubles using PyQgis processing module with QGIS 3.8. For example, I can reproject a layer with QGIS graphic interface, but when using PyQgis processing module through the following script :

 import sys
 from qgis.core import *
 QgsApplication.setPrefixPath("/usr", True)
 qgs = QgsApplication([], True)
 qgs.initQgis()
 print(QgsApplication.showSettings())
 ##Processing initialization
 sys.path.extend(['/usr/share/qgis/python/plugins', '/usr/share/qgis/python/plugins/processing', 
 '/usr/share/qgis/python', '/usr/lib/qgis/plugins']) # Folder for Processing
 import processing
 from processing.core.Processing import Processing
 Processing.initialize() # needed to be able to use the functions afterwards
 ##Get and load the project instance
 project = QgsProject.instance()
 project.setCrs(QgsCoordinateReferenceSystem(2154))
 ## Chargement d'une couche
 vlayer = QgsVectorLayer('/my/place/to/save/zone_etude.shp','zone_etude','ogr')
 project.addMapLayer(vlayer)
 param = {'INPUT':'/my/place/to/save/zone_etude.shp',
 'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),
 'OUTPUT':'TEMPORARY_OUTPUT'}
 processing.run("native:reprojectlayer", param)
 project.write('/my/place/to/save/tutoproject.qgs')
 qgs.exitQgis()

This code returns :

Application state:
QGIS_PREFIX_PATH env var: 
Prefix: /usr
Plugin Path: /usr/lib/qgis/plugins
Package Data Path: /usr/share/qgis
Active Theme Name: 
Active Theme Path: /usr/share/qgis/resources/themes//icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths: /usr/share/qgis/svg/
 /home/edelb/.local/share/profiles/default/svg/
User DB Path: /usr/share/qgis/resources/qgis.db
Auth DB Path: /home/edelb/.local/share/profiles/default/qgis-auth.db
Backend TkAgg is interactive backend. Turning interactive mode on.
/usr/lib/python3/dist-packages/osgeo/gdal.py:112: DeprecationWarning:
gdal.py was placed in a namespace, it is now available as osgeo.gdal
Traceback (most recent call last):
 File "/my/place/to/save/repro_bug.py", line 26, in <module>
 processing.run("native:reprojectlayer", param)
 File "/usr/share/qgis/python/plugins/processing/tools/general.py", line 106, in run
 return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
 File "/usr/share/qgis/python/plugins/processing/core/Processing.py", line 125, in runAlgorithm
 raise QgsProcessingException(msg)
_core.QgsProcessingException: Error: Algorithm native:reprojectlayer not found

I am on Ubuntu 18.04, using Qgis 3.8, and I don't know how I can access several of these processing algorithms like "native:reprojectlayer" or "native:saveselectedfeatures"...
Still, some processing algorithms are reachable, as this command shows:

for alg in QgsApplication.processingRegistry().algorithms():
 print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName()))
GDAL:aspect --> Aspect
GDAL:assignprojection --> Assign projection 
... 
GRASS:r.spreadpath --> r.spreadpath 
...
QGIS:vectorlayerscatterplot --> Vector layer scatterplot
QGIS:voronoipolygons --> Voronoi polygons
QGIS:zonalstatistics --> Zonal statistics

And for example processing.algorithmHelp("qgis:voronoipolygons") returns what it is supposed to return.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 23, 2019 at 14:59
2
  • 1
    you need add this line QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) after Processing.initialize() for use native algorithms Commented Jul 24, 2019 at 6:47
  • I add my comment as an answer then Commented Jul 24, 2019 at 7:28

1 Answer 1

6

To use native algorithms in a standalone application, you need to add the provider using:

QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

It would be like this:

from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
answered Jul 24, 2019 at 7:31
0

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.