1

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.

  • you need add this line QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) after Processing.initialize() for use native algorithms – Fran Raga Jul 24 at 6:47
  • Thanks a lot !! – Edel Jul 24 at 7:27
  • I add my comment as an answer then – Fran Raga Jul 24 at 7:28
1

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())
  • Can you please add the from qgis._analysis import QgsNativeAlgorithms ? – Edel Jul 24 at 7:34
  • of course! added :) – Fran Raga Jul 24 at 10:05
0

please can you tell me how do you do that a script without qgis? I tried many times! but always i have the same problem: qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "" This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

My path is: C:\Program Files\QGIS 3.6\apps\Python37\Scripts;C:\Program Files\QGIS 3.6\apps\Python37;C:\Program Files\QGIS 3.6\bin;C:\Program Files\QGIS 3.6\apps\qgis;C:\Program Files\QGIS 3.6\apps\Qt5;C:\Program Files\QGIS 3.6\apps\qgis\python;C:\Program Files\QGIS 3.6\;C:\Program Files\QGIS 3.6\apps\qgis\bin;C:\Program Files\QGIS 3.6\apps\qt5\bin;C:\Program Files\QGIS 3.6\apps\qt5\plugins;C:\Program Files\QGIS 3.6\apps\qgis\qtplugins;C:\Program Files\QGIS 3.6\apps\Python27;C:\Program Files\QGIS 3.6\apps\Python27\Scripts;C:\Program Files\QGIS 3.6\apps\Qt5\bin;C:\Program Files\QGIS 3.6\apps\Qt5\plugins;C:\Program Files\QGIS 3.6\apps\Qt5\lib;C:\Program Files\QGIS 3.6\apps\Qt5\translations;C:\Program Files\QGIS 3.6\apps\Qt5\include;C:\Program Files\QGIS 3.6\apps\Qt5\doc;

  • Hi, sorry I am not an expert on Windows setup for QGIS standalone scripts. If you want an answer, you would be better ask a new question/topic. – Edel Aug 19 at 8:40

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.