Unit 12 - Script User Interface

Standard input

There is a clear issue related to the script from Unit 11 - PyGRASS scripting. Some modules (r.recode, r.colors) use hardcoded paths to the input files. See code below (rules option).

    Module("r.recode",
           overwrite = True,
           input = "ndvi",
           output = "ndvi_class",
           rules = "/home/user/geodata/models/reclass.txt")

    Module("r.colors",
           map = "ndvi_class",
           rules = "/home/user/geodata/models/colors.txt")

By PyGRASS a content of input file can be defined as a string object and transfered to the command via standard input (stdin_). See a sample code below.

    Module("r.recode",
           overwrite = True,
           input = "ndvi",
           output = "ndvi_class",
           rules = "-",
           stdin_ = "-1:0.1:1\n0.1:0.5:2\n0.5:1:3")

    Module("r.colors",
           map = "ndvi_class",
           rules = "-",
           stdin_ = "1 grey\n2 255 255 0\n3 green")

Note that many of GRASS modules allows sending data via standard input by option value - (dash). In our case the both commands with rules=- option.

Tip

Cleanup routine which removes intermediate data by g.remove can be also defined.

def cleanup():
    Module('g.remove', flags='f', name='region_mask', type='vector')

User interface (UI)

The script lacks user input, all input paramaters are hardcoded. Remember a first modification of the script generated by Graphical Modeler in Unit 11 - PyGRASS scripting.

At first add previously removed lines back to the script.

#%module
#% description: NDVI model version 2
#%end

With these lines included a magic will happen, a standardized GUI dialog appears. Since only module description was defined and no parameters, the dialog offers only global flags like –verbose or –quiet.

../_images/ndvi-dialog.png

Fig. 81 Automatically generated GUI dialog.

Let’s define parameters below:

  • region: vector map defining a computation region (required)
  • clouds: vector map with cloud mask features (required)
  • red: input red channel (required)
  • nir: input nir channel (required)
  • threshold: threshold for removing small areas (optional)
  • output: output vector map (required)

UI definition below.

#%module
#% description: NDVI model version 3
#%end
#%option G_OPT_V_INPUT
#% key: region
#% description: Name of input vector region map 
#%end
#%option G_OPT_V_INPUT
#% key: clouds
#% description: Name of input vector clouds map 
#%end
#%option G_OPT_R_INPUT
#% key: red
#% description: Name of input red channel
#%end
#%option G_OPT_R_INPUT
#% key: nir
#% description: Name of input NIR channel
#%end
#%option
#% key: threshold
#% description: Threshold for removing small areas
#% answer: 1600
#%end
#%option G_OPT_V_OUTPUT
#%end
../_images/ndvi-dialog-params.png

Fig. 82 GUI dialog with input options.

In the script input parameters are still hardcoded, eg.

    Module("v.overlay",
           overwrite = True,
           ainput = "jena_boundary@PERMANENT",

The input parameters are accesible by options and flags objects which are generated by parse() function.

    options, flags = parser()

Options and flags are dictionaries, where parameters are accessible by option keys, see example below.

    Module("v.overlay",
           overwrite = True,
           ainput = options["region"],

Note

All generated (intermediate) maps can be removed when computation finished.

    Module('g.remove', flags='f', name='region_mask', type='vector')
    Module('g.remove', flags='f', name='ndvi', type='raster')
    Module('g.remove', flags='f', name='ndvi_class', type='raster')
    Module('g.remove', flags='f', name='ndvi_class', type='vector')
../_images/call-ndvi-script.png

Fig. 83 Improved NDVI script in action.

Sample script to download: ndvi-v3.py