Building GIS Models II
Reading time
Content
The possibilities with Model Designer go far beyond simple automation. You can create custom GIS tools by designing a model that combines multiple algorithms into a single, reusable process. These models can include branching logic, allowing for different processing paths depending on user-defined parameters. They can also integrate both raster and vector processing in the same workflow, making them useful for tasks such as suitability analysis, environmental monitoring, urban planning, and terrain analysis. Once built, a model can be saved and run like any other tool in the Processing Toolbox, or even shared with colleagues as a file. This means that Model Designer is not just a time-saver, but also a bridge between routine GIS operations and advanced, repeatable workflows that can be integrated into larger projects, ensuring accuracy and efficiency in spatial analysis.
Python Scripting in QGIS
In QGIS, the Model Designer and Python programming language work hand in hand through the Processing Framework, which means a workflow you create visually can be transformed into a Python script — and a Python script can be turned back into a model — with very little extra work.
When you finish building a model in the Model Designer, you can save it as a .model3 file (the native QGIS model format) and then export it as a Python script directly from the Model Designer interface using the “Export as Python script” option. This generates a .py file containing a self-contained Python function that calls the same algorithms and uses the same parameters as your model. This is useful when you want to extend the model’s capabilities, integrate it into larger Python-based workflows, run it headlessly (e.g., in batch mode without opening QGIS), or deploy it as part of a plugin.
The reverse is also possible, though slightly less direct. If you have a Python script that uses the processing.run() or processing.runAndLoadResults() commands to execute a series of algorithms, you can recreate the same logic in the Model Designer by adding the equivalent tools and connecting them in the same sequence. While QGIS does not have an “import Python as model” button, you can follow the script step by step, adding each tool, input, and output into the Model Designer. This approach is particularly helpful when converting scripts created by others into an easy-to-use visual workflow for non-programmers.
By moving between models and Python scripts, you can take advantage of the strengths of both worlds: the clarity and accessibility of the visual Model Designer, and the flexibility and automation power of Python. This makes it possible to adapt workflows for different audiences — from technical GIS analysts to casual users who just want to click “Run” on a well-prepared tool.
In QGIS, you can integrate your own Python scripts directly into a model built with the Model Designer by treating the script as a custom processing algorithm. The process works like this: first, you need to create your Python script in the Processing Script Editor (found in Processing Toolbox → Create New Script). Your script must follow the QGIS Processing Framework syntax, meaning it should define a class derived from QgsProcessingAlgorithm and implement methods such as initAlgorithm(), processAlgorithm(), and name(). In the script, you can define input parameters (e.g., layers, numbers, strings) and outputs just like any built-in QGIS tool. Once the script is saved in your QGIS profile’s processing/scripts folder, it will automatically appear in the Processing Toolbox under the “Scripts” group.
When building your model in the Model Designer, you can now drag your Python script from the Scripts section of the Processing Toolbox into the model canvas, just like any other algorithm. You can connect it to preceding steps by linking its inputs to the outputs of earlier tools in the model, and you can pass its outputs to later tools in the chain. This way, the Python code becomes just one step in a larger automated workflow, allowing you to handle special calculations, custom logic, or integrations with external systems that standard QGIS tools can’t easily provide.
This approach is particularly powerful when you have specialised code — for example, a custom statistical calculation or data transformation — and you want it to be reusable, parametrised, and accessible within a no-code model interface. It bridges the flexibility of Python with the visual logic of the Model Designer, making your workflows both programmable and user-friendly.