Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to iteratively add each raster map of viewshed analysis obtained from a series of single observer locations. For this i used grass.mapcalc with Python (GRASS 7 for Windows). However, my problem is that in grass.mapcalc you cannot allow functions like the following that we do apply in normal programming.

grass.mapcalc("viewshedN = viewshed + ViewshedN", overwrite = True, quiet = True)

Therefore, i have a trouble in adding each raster iteratively. Is it possible to add an incremental number to the output each time a new raster created? I just tried this in a loop with an integer variable but failed (parse error) in the following code. Is there any other work around/ method or alternative to add each raster result iteratively in GRASS with Python?

formula = 'viewshed' + str(i)          
grass.mapcalc(formula, overwrite = True, quiet = True)
share|improve this question
up vote 3 down vote accepted

Here the correct syntax:

#!/usr/bin/env python
# r.mapcalc example in Python

import sys
import os
import atexit

import grass.script as grass

def cleanup():

def main():

    return 0

if __name__ == "__main__":
    options, flags = grass.parser()

    inmap = "slope"
    outmap = "slope_div10"
    # some junk example calculation
    grass.mapcalc("$outmap = float($inmap / $value)", inmap = inmap, outmap = outmap, value = 10.0)


And the result:

GRASS 7.0.svn (nc_spm_08):~ > g.region rast=slope
GRASS 7.0.svn (nc_spm_08):~ > python 

GRASS 7.0.svn (nc_spm_08):~ > -r slope

GRASS 7.0.svn (nc_spm_08):~ > -r slope_div10

See also the Wiki page:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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