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?

3 Answers 3

4

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.

6
3

I was going through the quick_osm code and thought doing this might work, which actually did!

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")
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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