Unit 12 - Script User Interface

Standard input

In the script prepared in Unit 11 - PyGRASS scripting some tools (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")

Content of input files 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 will be changed to use rules="-" syntax.

Task

Define a cleanup routine to remove intermediate data by g.remove tool.

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

User interface (UI)

Let’s improve UI generated by Graphical Modeler in Unit 10 - Python intro.

#%module
#% description: NDVI model version 3
#%end
#%option
#% key: voverlay1_ainput
#% description: Name of input vector map (A)
#% required: yes
#% type: string
#% key_desc: name
#% answer: jena_boundary@PERMANENT
#%end
# %option
# % key: vclean8_threshold
# % description: Threshold in map units, one value for each tool
# % required: yes
# % type: double
# % answer: 1600
# %end
../_images/ndvi-dialog.png

Fig. 78 Generated GUI dialog with voverlay1_ainput option.

Let’s change UI by defining 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 4
#%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. 79 GUI dialog with input options.

In the script input parameters are still hardcoded, eg.

    Module("v.overlay",
           overwrite = True,
           ainput=options["voverlay1_ainput"],
           alayer = "1",
           atype = "auto",
           binput = "MaskFeature@PERMANENT",

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

    options, flags = parser()

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

    Module("v.overlay",
           overwrite = True,
           ainput = options["region"],
           alayer = "1",
           atype = "auto",
           binput = options["clouds"],

Task

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. 80 Improved NDVI script in action.

Sample script to download: ndvi-v4.py