26.8. 使用来自命令行的处理

QGIS附带了一个名为 QGIS Processing Executor 它允许您直接从命令行运行处理算法和模型(内置或由插件提供),而无需启动QGIS Desktop本身。

从命令行工具运行 qgis_process 您应该获得:

QGIS Processing Executor - 3.27.0-Master 'Master' (3.27.0-Master)
Usage: C:\OSGeo4W\apps\qgis-dev\bin\qgis_process.exe [--help] [--version] [--json] [--verbose] [--no-python] [command] [algorithm id, path to model file, or path to Python script] [parameters]

Options:

  --help or -h      Output the help
  --version or -v   Output all versions related to QGIS Process
  --json            Output results as JSON objects
  --verbose         Output verbose logs
  --no-python       Disable Python support (results in faster startup)

Available commands:

  plugins          list available and active plugins
  plugins enable   enables an installed plugin. The plugin name must be specified, e.g. "plugins enable cartography_tools"
  plugins disable  disables an installed plugin. The plugin name must be specified, e.g. "plugins disable cartography_tools"
  list             list all available processing algorithms
  help             show help for an algorithm. The algorithm id or a path to a model file must be specified.
  run              runs an algorithm. The algorithm id or a path to a model file and parameter values must be specified.
                   Parameter values are specified after -- with PARAMETER=VALUE syntax.
                   Ordered list values for a parameter can be created by specifying the parameter multiple times,
                   e.g. --LAYERS=layer1.shp --LAYERS=layer2.shp
                   Alternatively, a '-' character in place of the parameters argument indicates that the parameters should be read from STDIN as a JSON object.
                   The JSON should be structured as a map containing at least the "inputs" key specifying a map of input parameter values.
                   This implies the --json option for output as a JSON object.
                   If required, the ellipsoid to use for distance and area calculations can be specified via the "--ELLIPSOID=name" argument.
                   If required, an existing QGIS project to use during the algorithm execution can be specified via the "--PROJECT_PATH=path" argument.

备注

仅已安装的广告插件 hasProcessingProvider=yes 在他们的 metadata.txt 文件被识别,并可通过QGIS_Process工具激活或加载。

该命令 list 可用于获取所有可用提供程序和算法的列表。

qgis_process list

该命令 help 可用于获取有关命令或算法的进一步信息。

qgis_process help qgis:regularpoints

该命令 run 可用于运行算法或模型。将算法名称或模型路径指定为第一个参数。

qgis_process run native:buffer -- INPUT=source.shp DISTANCE=2 OUTPUT=buffered.shp

如果参数接受一系列值,请多次设置同一变量。

qgis_process run native:mergevectorlayers -- LAYERS=input1.shp LAYERS=input2.shp OUTPUT=merged.shp

在运行算法时,会显示一个基于文本的反馈栏,可以通过以下方式取消操作 CTRL+C

这个 run 命令还支持更多参数。

  • --json 将以JSON结构化方式格式化标准输出。

  • --ellipsoid 会将椭球体设置为指定的椭球体。

  • --distance_units 将使用指定的距离单位。

  • --area_units 将使用指定的面积单位。

  • --project_path 将加载用于运行算法的指定项目。

Qgis_process支持复杂输入参数,即本身被指定为算法的字典类型对象的参数类型。要指示将通过标准输入指定参数,QGIS_PROCESS命令必须遵循格式(尾随 - 代替通常的参数列表)。

qgis_process run algorithmId -

JSON对象必须包含一个“ins”键,它是输入参数值的映射。例如。

echo "{'inputs': {'INPUT': 'my_shape.shp', 'DISTANCE': 5}}" | qgis_process run native:buffer -

此外,此JSON对象中还可以包括额外的设置,如距离单位、面积单位、椭球体和投影路径:

{
 'ellipsoid': 'EPSG:7019',
 'distance_units': 'feet',
 'area_units': 'ha',
 'project_path': 'C:/temp/my_project.qgs'
 'inputs': {'DISTANCE': 5, 'SEGMENTS': 8 ... }
}

通过标准输入指定输入参数会自动隐含 JSON 结果的输出格式。