问题描述
最近基于QGIS做一些实验,有写库还是蛮好用的,但是毕竟开源的,虽然官方提供的基本功能都有了,但是相较于arcgis pro还是少了些。所以很多功能需要自己开发或者找一找轮子。
这时候就会出现问题了,在我们使用python进行QGIS的二次开发时,调用QGIS内部插件方法时会使用processing.run的方法,但是对于第三方插件使用这种方法就会有两种情况。
1.在QGIS的console中使用是没有问题的;
2.在python中单独使用就会出现方法不存在的情况:
_core.QgsProcessingException: Error: Algorithm slyr:lyrtoqml(这里是方法name) not found
虽然这个问题比较小众,一般人很少有这种需求,但是我翻遍了百度、谷歌、csdn、stackoverflow好像都没有提到过这个问题。
解决问题
大概看了一下QGIS的python代码,发现console的加载过程并没有涉及很多深层的逻辑问题,大多是界面的初始化,但是打开Processing.py的代码会发现这一段:
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.AlgorithmExecutor import execute
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.RenderingStyles import RenderingStyles
from processing.tools import dataobjects
with QgsRuntimeProfiler.profile('Import QGIS Provider'):
from processing.algs.qgis.QgisAlgorithmProvider import QgisAlgorithmProvider # NOQA
with QgsRuntimeProfiler.profile('Import GRASS Provider'):
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider
with QgsRuntimeProfiler.profile('Import GDAL Provider'):
from processing.algs.gdal.GdalAlgorithmProvider import GdalAlgorithmProvider # NOQA
with QgsRuntimeProfiler.profile('Import OTB Provider'):
from processing.algs.otb.OtbAlgorithmProvider import OtbAlgorithmProvider # NOQA
with QgsRuntimeProfiler.profile('Import SAGA Provider'):
from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider # NOQA
with QgsRuntimeProfiler.profile('Import Script Provider'):
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider # NOQA
这不正好是初始化的部分嘛,修改一下源码,直接在后面加上:
with QgsRuntimeProfiler.profile('Import slyr Provider'):
from processing.algs.slyr_community.qgis_plugin.provider import SlyrProvider # NOQA
以slyr这个插件举例,将provider类注册进来,并添加至下面这里:
# Add the basic providers
for c in [
QgisAlgorithmProvider,
Grass7AlgorithmProvider,
GdalAlgorithmProvider,
OtbAlgorithmProvider,
SagaAlgorithmProvider,
ScriptAlgorithmProvider,
SlyrProvider, # 加在这里了
ModelerAlgorithmProvider,
ProjectProvider
]
试一下:
from qgis.core import QgsApplication
print(QgsApplication.processingRegistry().providers())
[<qgis._core.QgsProcessingProvider object at 0x000001D3195985E8>, <processing.algs.gdal.GdalAlgorithmProvider.GdalAlgorithmProvider object at 0x000001D3152310D8>, <processing.algs.grass7.Grass7AlgorithmProvider.Grass7AlgorithmProvider object at 0x000001D315231048>, <processing.modeler.ModelerAlgorithmProvider.ModelerAlgorithmProvider object at 0x000001D319489168>, <qgis._core.QgsProcessingProvider object at 0x000001D319598678>, <processing.algs.otb.OtbAlgorithmProvider.OtbAlgorithmProvider object at 0x000001D31824D558>, <processing.modeler.ProjectProvider.ProjectProvider object at 0x000001D3195983A8>, <processing.algs.qgis.QgisAlgorithmProvider.QgisAlgorithmProvider object at 0x000001D3152224C8>, <processing.algs.saga.SagaAlgorithmProvider.SagaAlgorithmProvider object at 0x000001D31944E318>, <processing.script.ScriptAlgorithmProvider.ScriptAlgorithmProvider object at 0x000001D319480288>, <processing.algs.slyr_community.qgis_plugin.provider.SlyrProvider object at 0x000001D3194890D8>]
看到SlyrProvider 已经在里面了,之后就可以用processing.run去调用了。