| 1 | == Introduction |
| 2 | |
| 3 | QGIS' [https://docs.qgis.org/testing/en/docs/user_manual/processing/index.html# Processing framework] is a geoprocessing environment that can be used to call native and third-party algorithms from QGIS. GRASS GIS is supported as an algorithm provider among a variety of external (to QGIS) applications. More details can be found at [https://docs.qgis.org/testing/en/docs/user_manual/processing/3rdParty.html#grass QGIS processing framework > Configuring external applications > GRASS]. |
| 4 | |
| 5 | == How does it work? |
| 6 | |
| 7 | The GRASS provider for QGIS’ Processing is a graphical interface towards GRASS. In overview: |
| 8 | |
| 9 | * A GRASS GIS data base is created on-the-fly in a temporary directory |
| 10 | |
| 11 | * Input layers/rasters are imported (or referenced) into this GRASS GIS data base |
| 12 | |
| 13 | * Launching a GRASS algorithm from the [https://docs.qgis.org/testing/en/docs/user_manual/processing/toolbox.html Processing Toolbox] is essentially a GRASS GIS command line with the requested parameters, executed in the shell or as a system call onto the temporary GRASS GIS database. This command line is recorded in a log. |
| 14 | |
| 15 | * Results returned by the algorithm, are extracted from the GRASS GIS temporary database and converted into layers/rasters (and directly opened in QGIS if the respective option has been enabled). |
| 16 | |
| 17 | * Finally, the temporary GRASS GIS data base is deleted |
| 18 | |
| 19 | == Integrating an add-on under the GRASS provider |
| 20 | |
| 21 | To integrate an add-on in the GRASS provider, it is required to build a ''description'' file. A ''description'' file will suffice for ''simple algorithms'', i.e. import input data, execute a single GRASS GIS command and extract data. However, if an algorithm consists of multiple actions (i.e. a preliminary check of the imported data, executing more than one GRASS GIS commands or transforming the output data before the extraction), then the ''ext'' mechanism has to be used. The ''ext'' mechanism is a way to add additional logic to an algorithm. More on this mechanism in the following sections. |
| 22 | |
| 23 | === Description file |
| 24 | |
| 25 | * A '''description file''' describes the different parameters of an add-on |
| 26 | |
| 27 | * The name of the description file must be identical to the add-on name |
| 28 | |
| 29 | * Description files are placed under `python/plugins/processing/algs/grass7/description/`. Reading some of those files helps to understand the different options. |
| 30 | |
| 31 | === QGIS' Processing interface to GRASS |
| 32 | |
| 33 | In the code of the GRASS provider for QGIS' Processing framework (`python/plugins/processing/algs/grass7/Grass7Algorithm.py`) there are 4 pre-defined methods to interact with GRASS GIS. These are: `processCommand`, `processInputs`, `processOutputs`, `checkParameterValuesBeforeExecuting`. |
| 34 | |
| 35 | These methods reflect 4 levels of algorithmic building blocks: |
| 36 | |
| 37 | 1. '''Checking the input parameters'''. For example, in the case of two exclusive |
| 38 | optins, to verify that both have been enabled. |
| 39 | |
| 40 | 2. '''Processing inputs import'''. In the case of processing input layers after |
| 41 | importing. |
| 42 | |
| 43 | 3. '''Processing the command by itself'''. For example, chaining several GRASS commands together. |
| 44 | |
| 45 | 4. '''Processing the outputs export'''. For example to perform special things before exporting layers or if using special export methods. |
| 46 | |
| 47 | === Building complex algorithms |
| 48 | |
| 49 | To add some logic on one or more levels, the pre-defined methods will be overriden by custom ones defined in a Python file, named after the add-on and placed under the `ext` directory. In this way, the custom methods will be imported and run instead of the pre-defined ones. This enables to build an interface for complex GRASS GIS algorithms in QGIS' Processing Toolbox. |
| 50 | |
| 51 | Specifically, a `.py` file, named after the add-on ('''note:''' substitute `.` with `_`), has to be created under `python/plugins/processing/algs/grass7/ext`. In this Python file, custom methods need to be defined with the following names: |
| 52 | |
| 53 | - `checkParameterValuesBeforeExecuting` for Input parameters |
| 54 | - `processInputs` for Inputs import |
| 55 | - `processCommand` for Inputs import |
| 56 | - `processOutputs` for Outputs export |