QGIS – Can I automate (Model) tasks?

Marketing
Marketing
  • Updated

by David Crowther

DC_QGIS_Model_01.png

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.

DC_QGIS_Model_02.jpg

This opens the Model Designer into a new window.

DC_QGIS_Model_03.jpg

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).

DC_QGIS_Model_04.jpg

Firstly, in the Model Properties pane give the Model a Name and add it to a Group.

DC_QGIS_Model_05.jpg

Then Save the Model so that you can return to it at any time.

DC_QGIS_Model_06.jpg

DC_QGIS_Model_07.jpg

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.

DC_QGIS_Model_08.jpg

DC_QGIS_Model_09.jpg

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

DC_QGIS_Model_10.jpg

DC_QGIS_Model_11.jpg

Step 3 – Add a Raster Layer

Drag across the Input > Raster > give it a name e.g. Raster to Clip.

DC_QGIS_Model_12.jpg

DC_QGIS_Model_13.jpg

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.

DC_QGIS_Model_14.jpg

  • 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.

DC_QGIS_Model_15.jpg

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.

DC_QGIS_Model_16.jpg

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.

DC_QGIS_Model_17.jpg

This now connects the three INPUT options to the Buffer Algorithm.

DC_QGIS_Model_18.jpg

Step 5 – Choose Clip to Raster

In the Algorithms panel search for clip and choose the GDAL > Clip Raster by Mask layer

DC_QGIS_Model_19.jpg

  • 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

DC_QGIS_Model_20.jpg

  • Dependencies – ensure you tick the dependency for the Buffer to be created before the Raster Layer is Clipped to Mask.

DC_QGIS_Model_21.jpg

DC_QGIS_Model_22.jpg

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.

DC_QGIS_Model_23.jpg

Click OK and now we only have the Buffer and Raster (to clip) Layer going into the Clip Raster by Mask Command.

DC_QGIS_Model_24.jpg

Save the Model ….

DC_QGIS_Model_25.jpg

and then RUN the Model.

DC_QGIS_Model_26.jpg

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.

DC_QGIS_Model_27.jpg

Once the Model runs, the Clip Raster to Mask is performed on the Raster layer using the buffered Shrewsbury River layer.

DC_QGIS_Model_28.jpg

If you wish to run this multiple times, simply choose Run and edit the Buffer Distance value …. 100 metres.

DC_QGIS_Model_29.jpg

DC_QGIS_Model_30.jpg

And again this time to 200 metres.

DC_QGIS_Model_31.jpg

DC_QGIS_Model_32.jpg

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.

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.