5

I am trying to call the QuickOSM QGIS plugin from a standalone Python application and I´m not quite sure how to or if this is even possible.

My QGIS version is 3.10 (using OSGeo4W64) with the QuickOSM plugin installed. Running on Windows.

I tried to achieve this by using QGIS Processing, but I failed.

import sys
import os
import qgis
from qgis.gui import *
from qgis.core import *

from PyQt5.QtCore import *

QgsApplication.setPrefixPath("C:\OSGeo4W64\apps\qgis\", True) qgs = QgsApplication([], False) qgs.initQgis()

qg_project = QgsProject.instance()

from qgis.analysis import QgsNativeAlgorithms import processing from processing.core.Processing import Processing

Processing.initialize()

qgs.processingRegistry().addProvider(QgsNativeAlgorithms())

The extent is calculated somewhere else

alg_params = {'EXTENT': my_extent, 'KEY': '', 'SERVER': 'http://www.overpass-api.de/api/interpreter', 'TIMEOUT': 25, 'VALUE': '' }

feedback = QgsProcessingFeedback()

outputs['BuildQueryInsideAnExtent'] = processing.run('quickosm:buildqueryextent', alg_params, feedback)

qgs.exitQgis()

This fails, stating algorithm quickosm:buildqueryextent not found. When I try to call QGIS native, GDAL or GRASS algorithms, processing works.

I haven't found a solution to this. Maybe I can tell the script somehow where to find the plugin scripts? Is such a call even possible? Or do I have to do it another way all together?

Matt
  • 16,843
  • 3
  • 21
  • 52
GeoV_JM
  • 51
  • 5

3 Answers3

3

Your error is around here:

qgs.processingRegistry().addProvider(QgsNativeAlgorithms())

Your are adding only QGIS native algorithms in your Processing. QuickOSM is a plugin and is not in the QgsNativeAlgorithms.

The way to do that might be differente since version 3.8. You should be able to do the initProcessing() function somehow, but I haven't checked.

from qgis.utils import plugins
plugins['QuickOSM'].initProcessing()

But I don't think it will work. (but it would be the best and cleaned way).

As a workaround, you can still do

from QuickOSM.quick_osm_processing.provider import Provider
QgsApplication.processingRegistry().addProvider(Provider())

Note that you are in standalone executable. You need first to enable QuickOSM in your application. This is possible.

etrimaille
  • 7,240
  • 34
  • 47
  • I've an uncomplete script to try to solve the issue at https://gist.github.com/ThomasG77/71def7a758bfa75425d5bc87dde68906 If you have any clue, do not hesitate – ThomasG77 Feb 26 '20 at 12:35
  • I will have a look to your script @ThomasG77. I think the plugin is not known by QGIS and needs to be loaded by QGIS. I'm not sure. – etrimaille Feb 26 '20 at 17:30
  • 1
    In standalone context, from qgis.utils import plugins followed by a print(plugins) returns empty dict (because no iface, so not automatic plugin loading I think)... – ThomasG77 Feb 26 '20 at 23:03
  • @etrimaille Thanks for your answer. I tried both your suggestions but both were not working right away. I´m looking more into this. How would I enable QuickOSM for my application? – GeoV_JM Feb 27 '20 at 10:17
  • 1
    Either wait for QGIS 3.14, or have a look to this pull request to check how it is done https://github.com/qgis/QGIS/pull/34617 – etrimaille Feb 27 '20 at 17:43
  • 1
    You can have a look to this code. It's working well to load a custom plugin processing provider : https://github.com/3liz/qgis-lizsync-plugin/blob/master/processing/standalone_processing_runner.py – etrimaille Mar 03 '20 at 17:19
2

I was going through the quick_osm code and thought doing this might work, which actually did! Please correct me if there is a better way.

from QuickOSM.quick_osm import QuickOSMPlugin
QuickOSMPlugin.initProcessing(QuickOSMPlugin)

You can verify the QuickOSM processing algorithms by:

for alg in QgsApplication.processingRegistry().algorithms():
    print(alg.id(), "->", alg.displayName())

-->

.
.
.
qgis:vectorlayerhistogram -> Vector layer histogram
qgis:vectorlayerscatterplot -> Vector layer scatterplot
qgis:voronoipolygons -> Voronoi polygons
quickosm:buildqueryaroundarea -> Build query around an area
quickosm:buildquerybyattributeonly -> Build query by attribute only
quickosm:buildqueryextent -> Build query inside an extent
quickosm:buildqueryinsidearea -> Build query inside an area
quickosm:buildrawquery -> Build raw query
quickosm:openosmfile -> Open sublayers from an OSM file

or,

processing.algorithmHelp("quickosm:buildqueryextent")
nospec
  • 161
  • 6
2

The answer here did what I needed: Access the plugin (QNEAT3 in my case) and run its algos in standalone. May not work with every plugin, but is not the big hack I was worried about.

Matt
  • 16,843
  • 3
  • 21
  • 52
thmensing
  • 21
  • 1