1

I'm currently working on a GRASS script that takes maps from user input and computes them into a mapcalc expression. The issue i'm stucked with that when i ask for the map i can't set a variable name on it to use it on the mapcalc command. The code looks like this:

Asking for the map:

#%option G_OPT_R_INPUT
#% key: c
#% key_desc: name
#% gisprompt: old,raster,raster
#% required: yes
#% description: Effective cohesion (kPa)
#%end

and trying to set a name on it:

c = options['c']

I have previously imported parser from grass.script.

  • The option parser typically reads input values from the command line arguments. So you should be running: your_script.py c=<cohesion_raster>. Then in the script c = options['c'] will work. – Micha Oct 24 '17 at 8:54
  • @Micha but i need to add them from the gui that shows up, e.g., the windows that pops up from a Grass module. – Federico Gómez Oct 26 '17 at 2:01
  • It does not work from the command line either. – Federico Gómez Nov 1 '17 at 20:12
0

Here's a very simple test script you can try.

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Dummy test script
Created on Fri Nov  3 13:32:55 2017
@author: micha
"""

#%module
#% description: Add together two rasters
#% keyword: raster
#%end

#%option G_OPT_R_INPUT
#% key: input1
#% description: First input raster map
#% required: yes
#%end
#%option G_OPT_R_INPUT
#% key: input2
#% description: Second input raster map
#% required: yes
#%end
#%option G_OPT_R_OUTPUT
#% key: output
#% description: Output raster map
#% required: yes
#%end

import grass.script as gscript
import sys

def main():
    expr = "%s = %s + %s" % (output, input1, input2)
    gscript.message("Running mapcalc expression: %s" % expr)
    gscript.mapcalc(expr)

if __name__ == "__main__":
    options, flags = gscript.parser()
    input1 = options['input1']
    input2 = options['input2']
    output = options['output']
    overwrite = gscript.overwrite()

    sys.exit(main())

If I run this with the --h (help) argument I get:

python  ~/GIS/scripts/test_script.py  --h
Description:
 Add together two rasters

Keywords:
 raster

Usage:
 test_script.py input1=name input2=name output=name [--overwrite]
   [--help] [--verbose] [--quiet] [--ui]

Flags:
 --o   Allow output files to overwrite existing files
 --h   Print usage summary
 --v   Verbose module output
 --q   Quiet module output
 --qq  Super quiet module output
 --ui  Force launching GUI dialog

Parameters:
  input1   First input raster map
  input2   Second input raster map
  output   Output raster map

And if I give the full set of parameters, input1, input2 and output, it performs the mapcalc expression, adding the two input rasters together. If that works then just replace the expression, and your input variable names, and you should be OK.

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.