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.
1 Answer 1
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
Explore related questions
See similar questions with these tags.
your_script.py c=<cohesion_raster>
. Then in the scriptc = options['c']
will work.