by David Crowther
Question:
Can you automate commons tasks within QGIS?
Answer:
Yes. QGIS has a Modeller Tool which allows you to create models to undertake and automate multiple common tasks.
I am going to write a BLOG on the QGIS Modeller soon, but for the moment, here is a great YouTube video which shows you one Use Case for applying the Modeller - https://www.youtube.com/watch?v=eZb5VLTc9-o
…below, I will follow these steps to create and then run my very first QGIS Model!
The QGIS Modeller is accessible from the Processing menu using the > Graphical Modeller option.
This opens the Model Designer into a new window.
In this Use Case, we are going to use the QGIS Modeller to run a Buffer against my River Layer and then use that Buffer to Clip a Raster Layer. Here are my two datasets – Shrewsbury River Lines (Shapefile) and Shrewsbury Site (Image).
Firstly, in the Model Properties pane give the Model a Name and add it to a Group.
Then Save the Model so that you can return to it at any time.
There are two main tabs – Algorithms and Inputs:
- Inputs – the Layers that will be input or used within the Model e.g. the Vector and Raster layers
- Algorithms – these include common Vector and Raster tasks that are available in QGIS - such as Clip, Buffer, Intersect, etc… - which are then performed on the Input Layers
Step 1 – Choose Input Vector Layer to be Buffered
Here we will drag across the Input > Vector layer > give it a name and set the Geometry as Any Type and make it mandatory.
Step 2 – Add a Number for the Buffer
Use the Inputs to allow the user to choose a dynamic number for the buffer distance. Drag across the Input > Number > give it a name e.g. Buffer Distance
Step 3 – Add a Raster Layer
Drag across the Input > Raster > give it a name e.g. Raster to Clip.
We now have the 3 Inputs that the user needs to choose. Next we will add the algorithms to be performed against each Input.
Step 4 – Choose the Buffer option and Dissolve the Buffer
In the Algorithms panel search for a drag over the Vector Buffer option.
- Input – choose the Model Input > Input Vector Layer
- Distance – change this to use the Buffer Distance (Map Units) Model Input option
- Dissolved – leave this as No for the moment, but we will add this as a Boolean Option for the User to choose on the fly.
In the Input panel search for a drag over the Boolean option > name it Distance Buffer Output and make it checked (by default this is true) and mandatory.
Click in the THREE DOTS for the Buffer to edit the Buffer Algorithm and change the Dissolve option to use Model Input > Using the new Distance Buffer Output Boolean option.
This now connects the three INPUT options to the Buffer Algorithm.
Step 5 – Choose Clip to Raster
In the Algorithms panel search for clip and choose the GDAL > Clip Raster by Mask layer
- Input – change to Model Input and choose > Raster to Clip
- Mask Layer – change to Model Input and choose > Input Vector layer
- Set the Source and Target CRS – to be BNG 27700
- Create an Output Alpha Band
- Resolution – choose set Output file resolution – Yes
- Output – name the output raster as – Clipped Raster Output
- Dependencies – ensure you tick the dependency for the Buffer to be created before the Raster Layer is Clipped to Mask.
NOTE - The Model has an issue in it, where the Input Vector Layer is also going into the Clipped Raster Output, but we only need the Buffer and the Raster layer as inputs.
To fix this, edit (three dots) the Clip Raster by Mask Layer and change the Mask Layer to use an Algorithm > using the Buffered from Algorithm Buffer.
Click OK and now we only have the Buffer and Raster (to clip) Layer going into the Clip Raster by Mask Command.
Save the Model ….
and then RUN the Model.
Simply enter the buffer distance – as we set this an Input option – and choose the Raster to Clip as your Raster layer e.g. Shrewsbury Site.
Once the Model runs, the Clip Raster to Mask is performed on the Raster layer using the buffered Shrewsbury River layer.
If you wish to run this multiple times, simply choose Run and edit the Buffer Distance value …. 100 metres.
And again this time to 200 metres.
The QGIS Modeller is a very powerful tool, allowing you to build complex models to automate common Vector and Raster geoprocessing tasks. These can be run very quickly time and time again just by choosing different input options.
Comments (0 comments)