Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Automating GIS Tasks in QGIS

Automating GIS Tasks in QGIS

If you find yourself having to repeat the same process several times throughout your workday, then you should consider automation. It might take some time to accomplish this, but once done, it will save you a lot of time in the long run.

In this article, we will cover how to automate repetitive GIS tasks in QGIS using different approaches. Some of the approaches we will cover are

  • Model Builder
  • Python Scripts (PyQGIS)
  • Scheduled tasks

QGIS Graphical Modeler

QGIS Graphical Modeler provides a graphical user interface (GUI) through which the user can program spatial data processing pipelines and workflow design. In order to be able to use the Graphical Modeler effectively, one needs to understand the processes provided under the QGIS Processing toolbox, as the Graphical Modeler comes with most of the processes available under the Processing toolbox, with additional processes specific to the model builder.

The Graphical Modeler can be launched from the Processing menu or using Ctrl+Alt+G shortcut. This will bring up the Graphical Modeler window.

In the model interface, there are two tabs in the left panel, the input and algorithm panel. In the input tab, you define the inputs your model will accept from the user with the associated data integrity checks. On the algorithm tab, you define the algorithm to be performed on the input.

From this window give the name and group for your new model. The group is essentially a tag to enable searching of the model from the processing tool search bar.

Define Your Input for the Model

If you restrict the kind of data input that the user can pass into the program, the algorithm will only accept the input that meets the predefined parameters. 

For instance, you can set it to only accept point data.

Define the parameters to be used by the model 

If you want the model used to provide only some of the parameters to be used by the model, you can define them. For example, if your model will run a buffer on the input data you can let the user pass the buffer radius and set predefined limits for the input parameters. For this, you would click on Numbers under the input tab and from there you can specify the data type, and maximum and minimum accepted values among others. If you want to create a tool that always runs a 5 mile buffer on point inputs, you would only allow the user to define the point input, with no option to change the buffer distance.

Define the Process to be Performed

The processes to be performed on the inputted data are defined under the algorithm tab. These processes can be anything that is available in the processing toolbox such as buffer, clip, etc.. Under algorithms, search for the process you need to run, then select it from the filtered results.

Upon clicking on your process, it gives you a window from which you can define its input, output, and other related parameters. Remember to set the input source to model input by selecting the “Using model” input option under the input layer fields.

After defining the input to the algorithm, you should see the clipping algorithm with arrows connecting to its input on the modeler canvas. You can then chain several processes together where the output of one tool serves as input to the next tool. When adding subsequent processes you should set the input to ‘Using algorithm output’.

Documenting Your Model

Documentation is crucial for any tool you build for yourself or others, and models are no exceptions. In order to properly document your model, in your model window go to the edit model window. A Help editor window will pop up where you can document your model using text and HTML formats. You can document each element inside the Select element to edit panel

.

Running the Model and Exporting the Model Script

After you have finished building your model you can run the model or save it for future use.  Models are saved inside the .qgis2 directory. Upon running the model, you will see each step of the process being run, and intermediate results will be generated.

Batch Processing

Each process in the model can be run in a batch mode with multiple inputs. This results in each model being run in a set order. In order to enable the batch process, click on the “Run model” button and select “Run as batch process” at the bottom of the Run model window.

All saved models can be accessed through the Processing toolbox under Project Models.

Additionally, you can export it as a python script and run it from the python console. 

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
import processing
class Spatial_analysis(QgsProcessingAlgorithm):
    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer(‘inputlayer’, ‘Input Layer’,        types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer(‘overlaylayer’, ‘Overlay Layer’, types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
        results = {}
        outputs = {}
        # Clip
        alg_params = {
            ‘INPUT’: parameters[‘inputlayer’],
            ‘OVERLAY’: parameters[‘overlaylayer’],
            ‘OUTPUT’: QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs[‘Clip’] = processing.run(‘native:clip’, alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}
        # Buffer
        alg_params = {
            ‘DISSOLVE’: False,
            ‘DISTANCE’: 10,
            ‘END_CAP_STYLE’: 0,  # Round
            ‘INPUT’: outputs[‘Clip’][‘OUTPUT’],
            ‘JOIN_STYLE’: 0,  # Round
            ‘MITER_LIMIT’: 2,
            ‘SEGMENTS’: 5,
            ‘OUTPUT’: QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs[‘Buffer’] = processing.run(‘native:buffer’, alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        return results
    def name(self):
        return ‘spatial_analysis’
    def displayName(self):
        return ‘spatial_analysis’
    def group(self):
        return ‘analysis’
    def groupId(self):
        return ‘analysis’
    def createInstance(self):
        return Spatial_analysis()

The script will have the model as a class with all the algorithms defined as methods, and inputs as class properties. This step starts us off on how to automate tasks using Python script in the next section.

Python Scripts (PyQGIS) 

QGIS comes with a built-in console where you can write your python code, and execute it. This will require you to have a basic understanding of the Python programming language. Here are two resources to learn setting up python environments and places you can learn python respectively:

Writing Python Scripts on the QGIS Python Console

To open the python console, go to Plugins ‣ Python Console on the toolbar. A new panel will open at the bottom of the QGIS canvas.

On the >>> prompt you can type your python commands. Note that to interact with the QGIS interface, you need to use the iface variable which fetches the currently loaded layer, and then you can store it in a variable.

The official documentation covers the above steps and more so go and check out here PyQGIS developer cookbook

Importing a Script to the QGIS Python Console

In order to open the script we exported from our graphical modeler, click on the “Show editor” button, and this will open the editor tab. From the editor tab, click on the “Open script” button, and then navigate to the location of your script and select it. You can then run the script using the “Run” button.

For more details on how to work with scripts on QGIS, check the official documentation here QGIS official documentation

Scheduling QGIS Processes

What we have looked at so far requires one to run the script or the model manually while QGIS is open. However, we may want to run them several times at regular intervals from the command line without launching QGIS. This approach is suitable where your input data changes regularly and you want to automate the whole process. Python scripts can use QGIS libraries and run from the command line. To do this, we must set some environment configuration options in a .bat file which will also launch the python file. Additionally, you need to have a separate .py file to write your python logic. It is worth noting that the bat file configurations will vary depending on the operating system your machine is running. Windows Task Scheduler may also satisfy your needs, and provides a GUI.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.

Leave a Reply