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
19.4k4 gold badges25 silver badges64 bronze badges
asked Feb 24, 2020 at 10:21

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.

answered Feb 26, 2020 at 10:34
6
  • I've an uncomplete script to try to solve the issue at gist.github.com/ThomasG77/71def7a758bfa75425d5bc87dde68906 If you have any clue, do not hesitate Commented Feb 26, 2020 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. Commented Feb 26, 2020 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)... Commented Feb 26, 2020 at 23:03
  • 1
    Either wait for QGIS 3.14, or have a look to this pull request to check how it is done github.com/qgis/QGIS/pull/34617 Commented Feb 27, 2020 at 17:43
  • 1
    You can have a look to this code. It's working well to load a custom plugin processing provider : github.com/3liz/qgis-lizsync-plugin/blob/master/processing/… Commented Mar 3, 2020 at 17:19
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")
answered Jun 30, 2021 at 9:10
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
19.4k4 gold badges25 silver badges64 bronze badges
answered Feb 14, 2022 at 12:16

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.