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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 23, 2017 at 4:21
3
  • 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. Commented Oct 24, 2017 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. Commented Oct 26, 2017 at 2:01
  • It does not work from the command line either. Commented Nov 1, 2017 at 20:12

1 Answer 1

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

answered Nov 3, 2017 at 11:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.