22.5. グラフィカルモデラー

グラフィカルモデラー ではシンプルで使いやすいインターフェースを利用して複雑なモデルを作ることができます. GISで作業をするときにほとんどの解析作業は単独で存在するのではなく一連の操作の一部として存在します. グラフィカルモデラーを使うと一連の操作を単一のプロセスにまとめることができ,後で異なるデータセットを入力として実行するときに便利です. 多くのステップや異なるアルゴリズムを含んでいてもモデルは単一のアルゴリズムとして実行されるので時間と労力を節約できます.

グラフィカルモデラーはプロセッシングメニュー (プロセッシング ‣ グラフィカルモデラー) から開けます.

The modeler has a working canvas where the structure of the model and the workflow it represents are shown. The left part of the window is a panel with two tabs that can be used to add new elements to the model.


図 22.18 モデラー


  1. 必要な入力項目の定義 。これらの入力はパラメーターウィンドウに追加されますので、ユーザはモデルの実行時にそれらの値を入力できます。モデルそれ自体はアルゴリズムですので、プロセッシングフレームワークで利用可能なすべてのアルゴリズムで、入力が必要になった時にパラメーターウィンドウが自動的に生成されます。

  2. Definition of the workflow. Using the input data of the model, the workflow is defined by adding algorithms and selecting how they use the defined inputs or the outputs generated by other algorithms in the model.

22.5.1. 入力の定義

The first step is to define the inputs for the model. The following elements are found in the Inputs tab on the left side of the modeler window:

  • 認証の設定

  • 真偽値

  • CRS

  • Color

  • 距離

  • 列挙

  • 領域

  • フィールドマッパー

  • ファイル/フォルダ

  • 地図レイヤ

  • 行列

  • 複数入力

  • 数値

  • 印刷レイアウト

  • Print Layout Item

  • 範囲

  • ラスタバンド

  • ラスタレイヤ

  • Scale

  • 文字列

  • ベクタ地物

  • ベクタフィールド

  • ベクタレイヤ

When double-clicking on an element, a dialog is shown that lets you define its characteristics. Depending on the parameter, the dialog will contain at least one basic element (the description, which is what the user will see when executing the model). When adding a numerical value, as can be seen in the next figure, in addition to the description of the parameter, you have to set a default value and the range of valid values.


図 22.19 モデルパラメーター定義



図 22.20 モデルパラメータ

You can also add inputs by dragging the input type from the list and dropping it in the position where you want it in the modeler canvas.

22.5.2. ワークフローの定義

Once the inputs have been defined, it is time to define the algorithms of the model. Algorithms can be found in the Algorithms tab, grouped much in the same way as they are in the Processing toolbox.


図 22.21 モデル入力

To add an algorithm to a model, double-click on its name or drag and drop it, just like for inputs. An execution dialog will appear, with a content similar to the one found in the execution panel that is shown when executing the algorithm from the toolbox. The ones shown next correspond to the QGIS 'Drape (set Z value from raster)' algorithm and the QGIS 'Climb along line' algorithm.


図 22.22 モデルアルゴリズムパラメーター



  • レイヤー(ラスターおよびベクター)とテーブル。これらは、リストから選択されるが、この場合には、可能な値は、現在QGISにロードレイヤーまたはテーブルではなく、モデルに対応するタイプの入力、またはアルゴリズムによって生成された他のレイヤーまたはテーブルのリストが既にモデルに追加されます。

  • Numerical values. Literal values can be introduced directly in the text box. Clicking on the button beside the text box, expressions can be entered. Available variables for expressions include numerical inputs of the model, outputs from model algorithms and also statistical values from available layers within the model.

  • String. Literal strings can be typed in the corresponding text box. Clicking on the button beside the text box, expressions can be entered, as for numerical values.

  • Vector Field. The fields of a vector layer cannot be known at design time, since they depend on the selection of the user each time the model is executed. To set the value for this parameter, type the name of a field directly in the text box, or use the list to select a table field. The validity of the selected field will be checked at run time.

すべての場合において、ツールボックスからアルゴリズムを呼び出すときには使用できない 親アルゴリズム という名前の追加のパラメーターがあることに気づくでしょう。このパラメーターでは、あるアルゴリズムを現在のアルゴリズムの親として明示的に定義することによってアルゴリズムが実行される順序を定義し、親アルゴリズムを現在のアルゴリズムの前に強制的に実行させることができます。

When you use the output of a previous algorithm as the input of your algorithm, that implicitly sets the previous algorithm as parent of the current one (and places the corresponding arrow in the modeler canvas). However, in some cases an algorithm might depend on another one even if it does not use any output object from it (for instance, an algorithm that executes a SQL sentence on a PostGIS database and another one that imports a layer into that same database). In that case, just select the previous algorithm in the Parent algorithms parameter and they will be executed in the correct order.

Once all the parameters have been assigned valid values, click on OK and the algorithm will be added to the canvas. It will be linked to the elements in the canvas (algorithms or inputs) that provide objects that are used as inputs for the algorithm.

Elements can be dragged to a different position on the canvas. This is useful to make the structure of the model more clear and intuitive. Links between elements are updated automatically. You can zoom in and out by using the mouse wheel.


図 22.23 A complete model

You can run your algorithm any time by clicking on the Run button. In order to use the algorithm from the toolbox, it has to be saved and the modeler dialog closed, to allow the toolbox to refresh its contents.

22.5.3. モデルを保存したりロードする

Use the Save button to save the current model and the Open button to open any previously saved model. Models are saved with the .model3 extension. If the model has been already been saved from the modeler window, you will not be prompted for a filename. Since there is already a file associated with the model, that file will be used for subsequent saves.

Before saving a model, you have to enter a name and a group for it in the text boxes in the upper part of the window.

Models saved in the models folder (the default folder when you are prompted for a filename to save the model) will appear in the toolbox in the corresponding branch. When the toolbox is invoked, it searches the models folder for files with the .model3 extension and loads the models they contain. Since a model is itself an algorithm, it can be added to the toolbox just like any other algorithm.

Models can also be saved within the project file using the addToProjectSave model in project button. Models saved using this method won't be written as .model3 files on the disk but will be embedded in the project file.

Project models are available in the qgsProjectFileProject models menu of the toolbox.

モデルフォルダは モデラー グループ下、処理設定ダイアログから設定できます。

Models loaded from the models folder appear not only in the toolbox, but also in the algorithms tree in the Algorithms tab of the modeler window. That means that you can incorporate a model as a part of a bigger model, just like other algorithms.

Models will show up in the Browser panel , and can be run from there. Exporting a model as an image, PDF or SVG

A model can also be exported as an image, SVG or PDF (for illustration purposes).

22.5.4. モデルを編集する

You can edit the model you are currently creating, redefining the workflow and the relationships between the algorithms and inputs that define the model.

If you right-click on an algorithm in the canvas, you will see a context menu like the one shown next:


図 22.24 モデラー右ボタンクリック

削除 オプションを選択すると選択されているアルゴリズムを削除できます。アルゴリズムは他のアルゴリズムからの依存がなければ削除できます。つまり、アルゴリズムからの出力が別のアルゴリズムの入力として使用されていない場合。他のアルゴリズムからの依存があるアルゴリズムを削除しようとすると、以下のような警告メッセージが表示されます:


図 22.25 アルゴリズムを削除できない

Selecting the Edit option will show the parameter dialog of the algorithm, so you can change the inputs and parameter values. Not all input elements available in the model will appear as available inputs. Layers or values generated at a more advanced step in the workflow defined by the model will not be available if they cause circular dependencies.

Select the new values and click on the OK button as usual. The connections between the model elements will change in the modeler canvas accordingly.

モデルは、そのアルゴリズムの一部を無効化することで、部分的に実行できます。それを行うには、アルゴリズムの要素を右クリックし、表示されるコンテキストメニューで 無効化 オプションを選択します。選択されたアルゴリズム、およびそれに依存しているモデル内のすべてのものは灰色で表示され、モデルの一部として実行されなくなるでしょう。


図 22.26 無効化されたアルゴリズムを使用したモデル

When right-clicking on an algorithm that is not active, you will see a Activate menu option that you can use to reactivate it.

22.5.5. モデルヘルプファイルとメタ情報を編集する

You can document your models from the modeler itself. Just click on the Edit Model Help button, and a dialog like the one shown next will appear.


図 22.27 Editing Help



22.5.6. Pythonスクリプトとしてモデルを出力する

As we will see in a later chapter, Processing algorithms can be called from the QGIS Python console, and new Processing algorithms can be created using Python. A quick way of creating such a Python script is to create a model and then to export is as a Python file.

To do so, right click on the name of the model in the Processing Toolbox and choose Export Model as Python Algorithm....

22.5.7. 利用可能なアルゴリズムについて

You might notice that some algorithms that can be executed from the toolbox do not appear in the list of available algorithms when you are designing a model. To be included in a model, an algorithm must have the correct semantic. If an algorithm does not have such a well-defined semantic (for instance, if the number of output layers cannot be known in advance), then it is not possible to use it within a model, and it will not appear in the list of algorithms that you can find in the modeler dialog.