4

I have a large number of rasters and I want to add 1 to each of them (e.g. each raster's values currently range from 0 to 1, and I want to make them range from 1 to 2). I believe ModelBuilder, using the "Iterate Rasters" tool and the "Raster Calculator" tool, should do this.

In "Raster Calculator" outside of modelbuilder, selecting the raster name and typing + 1 works to do this on a single raster. However, in modelbuilder, using ("%Raster%") + 1 does not work (see attached photo).

I'm new to ModelBuilder and Python.

Model_to_be_fixed

2
  • Would you be interested in doing this in python, I don't like the model builder iterators myself... I think though you might need to change your calculation to %Raster% + 1. Commented Sep 15, 2015 at 4:29
  • Removing the " around %Raster% gives a syntax error and won't let the model run. The quotations appear automatically when I click on the iterated Raster option in the Raster Calculator dialogue.. When I run it with them in (as in my question), I get: ERROR 000539: Error running expression: rcexec() Traceback (most recent call last): File "<expression>", line 1, in <module> File "<string>", line 5, in rcexec TypeError: cannot concatenate 'str' and 'int' objects. I'd like to figure it out in modelbuilder as I'm most of the way there, but if Python is easier I'm open to trying it.
    – Becky
    Commented Sep 15, 2015 at 16:21

1 Answer 1

12

The easiest solution is to replace the raster calculator with the "Plus" operator in the iterator.

EDIT: note that I wasn't able to reproduce the problem in ArcGIS 10.6:

("%Raster%") + 1

worked fine in my iterator ( with output =, for example, %Raster%_out )

Anyway, the best solution is to use a Python script

import arcpy 
from arcpy.sa import *

arcpy.env.workspace = r'your_path_to_directory'
for r in arcpy.ListRasters("*"): # list all rasters in workspace
    outRaster = Raster(r)+1 #perfoms addition
    outRaster.save(r[:-4] + "out.tif") #save output in tif format
4
  • Thanks, the python script worked well for me. I tried to upvote your answer but don't have the ability yet!
    – Becky
    Commented Sep 15, 2015 at 19:02
  • That's how I would do it radouxju, I don't like the model builder iterators so anything involving iteration I would do in python. There's a few good gems of information here Becky, enough to get you started, like r[:-4] is python string manipulation for everything except the last 4 characters. I hope this wets your appetite for scripting... Commented Sep 15, 2015 at 21:54
  • Could you do a similar Python script to get the maximum of each raster?
    – Bowen Liu
    Commented Nov 8, 2018 at 15:59
  • yes, simply "print(outRaster.maximum)" or store outRaster.maximum in a dictionnary with " r " as a key. See gis.stackexchange.com/questions/175352/… By the way, please do not use comments to ask questions.
    – radouxju
    Commented Nov 9, 2018 at 11:11

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.