0

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)

1 Answer 1

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)

Your Answer

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

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