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)
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jun 10, 2022 at 10:18

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)
answered Jun 11, 2022 at 6:08

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.