QGis 2.8 graphic modeller: Python script Iteration, nesting and export

The execution of model algorithms by iteration

In the previous chapter we have executed our model that selects and cuts those plots located within 500 meters of our oyster facilities. The result is a single layer of plots. But we could want to recover the plots of each facility in a different layer. We have nine facilities; therefore we would like to have s nine layers.
In the first case, if a plot is located within 500 m of two or more facilities it appears just once.  In the second case we would have this plot in as many layers as facilities are located within 500 m from it.

With the QGis graphic modeller, this task is extremely simple.

Remember the structure of our model:

A first algorithm calculates the 500m buffer zone around   installations and a second algorithm cuts the plots layer with the buffer zones produced by the first.

Therefore we would like this model to run as many times as facilities are present in the Installations layer .

We must only launch the model and indicate that we wish the iteration in the Installations layer.

The symbol located next to the input layers enables to run this task. Just click to activate Iteration on the layer concerned.
Be careful not to get the wrong entry layer. In our example we have 9 Facilities and 863 plots.  If you make a mistake and ask iteration on the plots you will get 863 layers in the output!

As with any model, you can leave the name of the output layer empty to create temporary layers, or enter a path and file name to create permanent layers. The only thing that changes with iteration is that temporary or permanent names will be completed with a digital index indicating the correspondent iteration number iteration:

In this example we have entered C: \ QGis \ data articles \ results \ result as name of the exit layer. We have got nine layers named result_0.shp to result_8.shp .

Nesting models

As we have stated above, when a model is checked in the models folder, it appears in the treatment tools category Panel Models Box. What has not been mentioned above is that it appears also in the algorithms tab of the graphic modeller window. This means that one model previously written can be used as an algorithm in another model.

But you have to be careful since a model may not appear in the algorithms list if some of their components are not available. It may occur if an algorithm provider is disabled. For example, if you have in use a SAGA tool in a model but you have subsequently disabled the SAGA tools, this model will not be available. As long as all the algorithms of a model are present in the panel of the treatment toolbox, the model will be available whenever an algorithm in the graphic modeller.

To illustrate this feature, we will rely on the model we have used in the previous section. Once the parcels are created and the land areas located within 500 m we will merge the Maritime District results. This information is included in the attribute table of the parcels and therefore in the attribute table of the outcome layer.

We start by naming the new model and assigning the same group

We add the input data: plots and facilities

We add the existing model

1- We select the algorithms tab
2- Click-and-move the riparian model plots
3- We complete the input fields

Then we click OK

Now we will add the algorithm for merging polygons following the content of a field (dissolved)

For the input layer select riparian plots created by the algorithm ‘riparian plots
In Dissolve everything select NO since we want to use the contents of a field like as merger criterion
In Unique identification Field we enter the field name containing the criterion (NEIGHBORHOOD)
We give a name to the outcome layer (riparianzone)
We do not need to define the parent algorithm since we use the output of the previous algorithm.
We click OK

Now the full model is as follows :

Once registered, the new model is ready to be used. It appears in the section Models of treatment tools Box

Double-click to execute it.
We inform the dialogue window

The result appears. If we open the attribute table of the outcome layer, we see that there is only one single polygon; all plots concerned corresponding to the same maritime district.

Using batch processing with the models

The models can also be launched in batch mode just like the other processing algorithms. To do this , simply find the model in the panel of the treatment toolbox, right-click it and select perform as batch processing, as shown in the following screenshot:

The tool works as other geoalgorithme in QGIS.

You can click on Add lines, delete lines and finally RUN when you are ready. With this method, the model can be used on data sets from different geographical areas. This technique is also useful in those cases where you will repeat the geoprocessing steps many times on a collection of files.

How to convert a model in a Python script

In the QGIS version 2.4 it was possible to transform a processing model to a Python script. For that, it was enough to right click on the model in the processing Toolbox and select the option Record as a Python script. During the development of QGIS 2.6, the underlying code graphic modeller has been completely rewritten. Therefore, there are many changes and improvements in the files modeller new version. Model files are henceforth stored in JSON format instead of XML; there is no depth limit for nesting the models; we can drag and paste the algorithms and inputs to the graphic window of the modeler ; …
Unfortunately , the possibility to export a model like a Python script has not been implemented in the modeller updated.
This feature should reappear in a future version of QGIS. For the moment, given the fact that the models are algorithms, they can always be executed from the Python Console.

 

Leave a Reply

Your email address will not be published. Required fields are marked *