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
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.
-
I've an uncomplete script to try to solve the issue at gist.github.com/ThomasG77/71def7a758bfa75425d5bc87dde68906 If you have any clue, do not hesitateThomasG77– ThomasG772020年02月26日 12:35:03 +00:00Commented 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.etrimaille– etrimaille2020年02月26日 17:30:36 +00:00Commented Feb 26, 2020 at 17:30
-
1In standalone context,
from qgis.utils import plugins
followed by aprint(plugins)
returns empty dict (because no iface, so not automatic plugin loading I think)...ThomasG77– ThomasG772020年02月26日 23:03:30 +00:00Commented Feb 26, 2020 at 23:03 -
1Either wait for QGIS 3.14, or have a look to this pull request to check how it is done github.com/qgis/QGIS/pull/34617etrimaille– etrimaille2020年02月27日 17:43:07 +00:00Commented Feb 27, 2020 at 17:43
-
1You 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/…etrimaille– etrimaille2020年03月03日 17:19:38 +00:00Commented Mar 3, 2020 at 17:19
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")
Explore related questions
See similar questions with these tags.