使用来自独立PyQGIS脚本的QGIS3处理算法(GUI外部)


23

我正在编写必须在QGIS GUI外部运行的脚本。我从qgis.core调用了一些API函数,但我想使用处理插件。

我可以使用sys.path.append()导入处理,但无法运行任何进程。此外,QgsApplication.processingRegistry()。algorithms()中缺少所有“本机”算法

那么有可能以这种方式运行处理吗?我想念什么?

import os, sys
from qgis.core import *
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()

sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
Processing.initialize()
import processing

layer1 = QgsVectorLayer('data/ROUTE_PRIMAIRE.SHP')
layer2 = QgsVectorLayer('data/ROUTE_SECONDAIRE.SHP')

processing.run('qgis:union', layer1, layer2, 'test.shp') # returns nothing

我正在使用QGIS 3.0.1-Debian 9

Answers:


28

您可以通过以下方式在独立(无GUI)模式下运行QGIS处理算法:

import sys

from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)

# See /gis//a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()

# Append the path where processing plugin can be found
sys.path.append('/docs/dev/qgis/build/output/python/plugins')

import processing
from processing.core.Processing import Processing
Processing.initialize()

layer1 = QgsVectorLayer('/path/to/geodata/lines_1.shp', 'layer 1', 'ogr')
layer2 = QgsVectorLayer('/path/to/geodata/lines_2.shp', 'layer 2', 'ogr')

# You can see what parameters are needed by the algorithm  
# using: processing.algorithmHelp("qgis:union")
params = { 
    'INPUT' : layer1,
    'OVERLAY' : layer2, 
    'OUTPUT' : '/path/to/output_layer.gpkg|layername=output'
}
feedback = QgsProcessingFeedback()

res = processing.run('qgis:union', params, feedback=feedback)
res['OUTPUT'] # Access your output layer

本机算法

现在,如果要使用本机算法(即,来自本机提供程序的算法,其算法用C ++编写),则需要在初始化Processing之后添加提供程序:

import sys

from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms

# See /gis//a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('/usr', True)
qgs = QgsApplication([], False)
qgs.initQgis()

# Append the path where processing plugin can be found
sys.path.append('/docs/dev/qgis/build/output/python/plugins')

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

layer = QgsVectorLayer('/path/to/geodata/lines.shp', 'my layer', 'ogr')

# You can see what parameters are needed by the algorithm  
# using: processing.algorithmHelp("native:extractvertices")
params = {
    'INPUT': layer,
    'OUTPUT': 'memory:'
}
feedback = QgsProcessingFeedback()

res = processing.run("native:extractvertices", params, feedback=feedback)
res['OUTPUT'] # Access your output layer

工作正常,谢谢!如果提供了路径,可以同时将输出写入磁盘吗?
vidlb '18

当然,这也是一个非常有用的选项。
赫尔曼·卡里略

1
这真是不可思议-感谢您分享详细的解决方案!
root676

@GermánCarrillo某种程度上非常相似的代码无法正常工作。查看更多:gis.stackexchange.com/questions/286281/...
车先生

我尝试在QGIS插件中运行QGIS算法,是否以相同的方式工作?因为我无法运行它
gHupf

1

当我尝试在包含QGIS 3.4.4的OSGeo4W安装中尝试如上所述使用本机算法时,出现错误“ NameError:名称'QgsNativeAlgorithms'未定义”。该解决方案原来是缺少的导入:

from qgis.analysis import QgsNativeAlgorithms

(来自QGIS 3.4 / 3.6独立脚本

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.