Run QGIS processing algorithms from the command line

17 Jul 2020 ᛫ 3 min read

SHARE:

QGIS 3.14 (Pi) introduced the qgis_process tool – a command-line executable that “allows users to run processing algorithms (both built-in, and those provided by plugins) directly from the console” developed by Nyall Dawson and sponsored by the Swedish User Group. You can find the Pull Request of the feature here.

Being run straight from the console and outside the QGIS GUI application, qgis_process provides users the ability to harness the full power of the QGIS processing algorithms through batch files, shell scripts, and other operating system tools. This is also useful if you want to use QGIS processing algorithms externally without needing to write Python (or PyQGIS) scripts.

Currently, the tool has several commands:

  • qgis_process list: outputs a complete list of all available algorithms, grouped by provider.
  • qgis_process plugins: lists available and activated plugins which advertise the hasProcessingProvider metadata option (only these plugins are loaded by the tool)
  • qgis_process help algid: outputs the help and input descriptions for the specified algorithm, e.g. qgis_process help native:centroids
  • qgis_process run: runs an algorithm. Parameters are specified by a --param=value syntax. For example:
qgis_process run native:centroids --INPUT="my_shapefile.shp" --OUTPUT="centroids.kml"
qgis_process run native:buffer --INPUT=/home/me/my.shp --DISTANCE=20 --OUTPUT=/home/me/buffered.shp

Running the qgis_process tool

If you are on Linux, the tool can be accessed directly from the terminal. For Windows users, you can access the tool from the OSGeo4W shell (this usually comes installed with QGIS).

In my examples, I run this tool on a machine with a Pop! OS 20.04 (based on Ubuntu 20.04) operating system.

qgis_process list

If you want to find what algorithms you can run, enter qgis_process list on the terminal.

qgis_process list
Running the qgis_process list terminal command
Running qgis_process list

qgis_process help algid

If you want help for a specific algorithm (e.g. you want to know its parameters/arguments), enter qgis_process help algid on the terminal. algid refers to the id used by QGIS to identify the algorithm. Veterans and power users of the QGIS processing framework are probably already familiar with this. For beginners, these are the first items per line of the qgis_process list output and follows the format : (e.g. native:dissolve which refers to the dissolve algorithm provided by QGIS natively).

qgis_process help native:dissolve
Running the qgis_process help terminal command
Running qgis_process help

Take note of the Arguments listed by qgis_process help. These are the arguments/parameters that can be provided when running the algorithm. Some arguments/parameters are optional. To learn more about QGIS processing algorithms and their parameters, you can check the QGIS documentation or this list made by Thomas Gratier.

----------------
Arguments
----------------

INPUT: Input layer
	Argument type:	source
	Acceptable values:
		- Path to a vector layer
FIELD: Dissolve field(s)
	Argument type:	field
	Acceptable values:
		- The name of an existing field
		- ; delimited list of existing field names
OUTPUT: Dissolved
	Argument type:	sink
	Acceptable values:
		- Path for new vector layer

For native: dissolve, we have INPUT, FIELD, and OUTPUT. Adding these arguments/parameters to the command is done via a --param=value syntax.

qgis_process run

Let’s run a simple dissolve algorithm. First, add/export the QT_QPA_PLATFORM variable (for Linux).

export QT_QPA_PLATFORM="offscreen"

Afterwards, run the algorithm.

qgis_process run native:dissolve --FIELD="[]" --INPUT="/path/to/input.vector" --OUTPUT="/path/to/output.vector"

In my example, I used a geojson containing features of municipalities of a province and dissolved them to have a geojson with just the feature of the province.

Running a QGIS processing algorithm in the terminal using qgis_process run
Running qgis_process run

You can check your output by opening it in QGIS.

Like and follow BNHR on Facebook and Twitter for more #FOSS4G and #QGIS stuff. :)


Hat-tip to Totò Fiandaca for the original post here.

SHARE:
comments powered by Disqus

You may also like:

Ukol sa mga bersyon at release cycle ng QGIS

26 Oct 2020 ᛫ 6 min read

#PluginFridays: SRTM Downloader

22 Oct 2020 ᛫ 2 min read

Add Orfeo ToolBox (OTB) processing provider in QGIS

11 Oct 2020 ᛫ 3 min read

Run QGIS processing algorithms in R

04 Oct 2020 ᛫ 3 min read

Custom splash screens in QGIS

05 Sep 2020 ᛫ 2 min read

BNHR

[email protected]

Creative Commons License
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)