I'm having trouble with a simple script in Python. I do not know where the error is. I use GRASS 8.0.
import os
import math
import grass.script as grass
elevation = 'dem_10_fill'
elev_new = 'elevation_mod'
grass.run_command('g.region', raster=elevation)
grass.mapcalc('elev_new = if(elevation < 280, elevation)')
The message is:
Niewłaściwa mapa elevation / Incorrect elevation map
Parse error
ERROR: parse error
ERROR: An error occurred while running r.mapcalc with expression: elev_new = if(elevation < 280, elevation)
Vince
20.5k16 gold badges49 silver badges65 bronze badges
1 Answer 1
The variable names that you defined are not parsed inside your expression string. You need to build the mapcalc expression using python string formatting. i.e.:
expr = '{0} = if({1} < 280, {1})'.format(elev_new, elevation)
grass.mapcalc(expr)
lang-py