Digital Geography

6. August 2014

Creating models in QGIS

You’ve probably heard of model builder in ESRI ArcGIS (even used it?), but did you know the same functionality is available in QGIS?

Image showing the “Processing Toolbox” in the background right and the “Processing modeler” in the front.

In my example I’ll be building what should be a very simple tool that extracts points at a maximum distance from some lines, but it has a complication due to a bug in QGIS. However there’s a fix explained on the QGIS hub (link). You should basically edit a script file (python/plugins/processing/algs/qgis/ftools/ExtractByLocation.py) moving and changing the code according to this:

Remove text in red, replace with text in green, save your file and restart QGIS (backup the file before you edit).

(This may be fixed when you read this article)

Back to business

You start the “Graphical modeler” under the “Processing” menu (the window title is however “Processing modeler” as in the picture below).

Adding “Inputs” to your model.

Start by adding “Inputs” to your model, and in my case I’ll be using two vector parameters (one point and one line) and a number parameter for the distance. If you click on the “Edit model help” button you can add more details and help for the tool, but it’s not necessary.

By the way, QGIS tends to crash extra often when you use the modeler so name your model and save often.

Models are created by connecting inputs with algorithms resulting in outputs.

I’m using the “Fixed distance buffer” and “Extract by location” tools to first create a buffer around my lines at a given distance and then extract the points that are within that distance.

There’s more to this than what’s visible in the picture above or in the text, but it should be pretty self explanatory.

Use the search field in the modeler to find the tool you’re after. Some functions can be found in many places depending on which extras you have installed in QGIS, and they work in slightly different ways.

You can run the tool from the modeler directly, but normally you will run it from the “Processing Toolbox” where it’s been saved under “Models”.

The MyDistanceTool dialog and the result in the background highlighting all points within a distance of 200 meters from the lines.

You can also share your model file with others and there’s a special tool in the toolbox for adding models from a file.

Reflections

QGIS is a bit shaky with the toolbox and modeler, mainly because the underlying tool scripts are a bit “iffy” sometimes. It should clear up eventually, as long as we keep reporting the bugs we find.

Since QGIS does not use a “unit” when entering a distance you need to use the same unit as the layer CRS. The easiest way is to use only projected data in projected projects for this to work (I’m using a Swedish national projection in my example). If you use un-projected data the given distance will be interpreted as degrees (200 degrees is a lot more than 200 meters).

You can run the model without loaded data, which also limits which tools can be used in the model. In my case the “Select by location” tool might be useful, but since selections doesn’t work with data that isn’t loaded it’s excluded from the modeler.

You can even use your own models inside other models and thereby create more complex models from simpler ones.

A nice feature is to define a QGIS layer style file (*.qml) for the resulting layer. You do this by right clicking on the tool in the toolbox and selecting “Edit rendering styles for outputs”.

Good luck, and don’t forget to report your bugs!

  • As long as I am using QGIS it was always “hard” for me to report bugs. In the github repo you cannot open up issues and despite providing a “report a bug” button in the interface of QGIS you can find commercial support… The “old” http://hub.qgis.org/projects/quantum-gis/issues/new is to detailed in my case.

  • Allan Lopez

    Superb !!. I was looking for such a tool !. Thank you

  • Phillip Davis

    Hope you will visit our new QGIS Academy: http://foss4geo.wordpress.com

  • Works really great! Thanks for describing this model builder.

  • Frank Hanssen

    I am looking into QGIS curious on the work processing service. Have you any experience with that? I would like to design models using QGIS Processing Modeler and make them externally available as a work processing service. All data will be stored internally in a PostgreSQL Database using PostGIS. Any tip on how to proceed?