arcpy code for iteration and Raster Calculator

6948
6
Jump to solution
11-11-2015 11:35 AM
johnpolo
New Contributor III

I have several rasters to iterate through. I want to reclass the cell values to 0-255 with the following equation:

rescaled grid = [(grid - min value from grid) * (max scale value - min scale value) / (max value from

grid - min value from grid)] + min scale value

Searching on the web led to comments that one should use arcpy instead of model builder for iterating with Raster Calculator, instead of using Model Builder. I have never used python/arcpy and have no idea what to write. I have looked around and found results to lead me to something like this:

# Import arcpy module

import arcpy

# Check out any necessary licenses

arcpy.CheckOutExtension("spatial")

# Load required toolboxes

arcpy.ImportToolbox("Model Functions")

# Local variables:

clim20s30s = "C:\\Users\\JPolo\\Documents\\climateTemps\\PRISM_ppt\\clim20s30s"

v_name_sc_tif = "C:\\Users\\JPolo\\Documents\\climateTemps\\PRISM_ppt\\cacl\\%name%sc.tif"

# Process: Iterate Rasters

arcpy.IterateRasters_mb(clim20s30s, "", "", "NOT_RECURSIVE")

# Process: Raster Calculator

arcpy.gp.RasterCalculator_sa("outRaster = ((\"%ppt_192001_bil.tif%\" - min(\"%ppt_192001_bil.tif%\")) * (255 - 0) / (max(\"%ppt_192001_bil.tif%\") - min(\"%ppt_192001_bil.tif%\"))) - 0", v_name_sc_tif)

However, this doesn't work and I don't know what needs to be changed to make it work.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

It seems that you copied the code exporting a model to Python. When using Python you can do nice things using raster objects, like reading the minimum and maximum values of a raster. The code below is not tested, but shows how this would work:

# Import arcpy module
import arcpy
import os

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Local variables:
ws = r"C:\Users\JPolo\Documents\climateTemps\PRISM_ppt\clim20s30s"
v_name_sc_tif = r"C:\Users\JPolo\Documents\climateTemps\PRISM_ppt\cacl\{0}sc.tif"

# Process: Iterate Rasters
arcpy.env.workspace = ws
ras_names = arcpy.ListRasters()
for ras_name in ras_names:
    name, ext = os.path.splitext(ras_name)
    ras = arcpy.Raster(os.path.join(ws, ras_name))

    out_ras = arcpy.sa.Int((ras - ras.minimum) * 255 / (ras.maximum - ras.minimum))
    out_ras.save(v_name_sc_tif.format(name))

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

well your best bet would be to do the process manually, then get the output from the Results window, From there you can modify that to put it through for ... statements to cycle through a folder/geodatabase for other rasters.  The results that you show seem to be the export of a model to python which is totally different that exporting the results of a manual process to python.   Give that a shot and report back with that 

johnpolo
New Contributor III

Dan, thanks for the help. I tried to running a single process and checking the results, but it was slow going with trying to figure out how to make it iterative. It's definitely a good exercise to understand the process and how to tinker with it. When I can find some time to mess around with it more, maybe I can figure it out, but for now, I need to get this process wrapped up.

0 Kudos
curtvprice
MVP Esteemed Contributor

The Raster Calculator tool should only be used in ModelBuilder. You also cannot use the Model Builder iterator tool in Python at all, you'd need to use something like arcpy.ListRasters to build a list and a for loop to iterate instead.

Do you have a reason you must use Python for this task? I'd do it in Model Builder if you don't have the Python skills.  My suggestion is build a model that does it for one raster and then add the iterator after that. Check out these geonet forums:

Spatial Analyst

Model Builder

You also have some logic errors in here, min() and max() will not work on rasters. The Cell Statistics tool can be used to get min and max, but just cel by cell, not for the whole raster.  You want to use the Get Raster Properties tools to get values for the min and max values in grids, and then you can use those variables in the Raster Calculator expression, which would then look something like this, where everything in % signs is variable from the model. You can build a valid expression in the Raster Calculator tool dialog.

 (((%ingrid% - %ingrid_min%) * (%max scale% - %min scale%)) 
  / (%ingrid max% - %ingrid min%)) + %min scale% )

Or, you  may want to check out the Slice tool, which I think is easier. You can use it either outside or inside Raster Calculator.

Slice(InputGrid, 256, "EQUAL_INTERVAL", 0)
johnpolo
New Contributor III

Curtis, I tried Slice and it looks like it will solve my problem. Thanks for suggesting this.

XanderBakker
Esri Esteemed Contributor

It seems that you copied the code exporting a model to Python. When using Python you can do nice things using raster objects, like reading the minimum and maximum values of a raster. The code below is not tested, but shows how this would work:

# Import arcpy module
import arcpy
import os

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Local variables:
ws = r"C:\Users\JPolo\Documents\climateTemps\PRISM_ppt\clim20s30s"
v_name_sc_tif = r"C:\Users\JPolo\Documents\climateTemps\PRISM_ppt\cacl\{0}sc.tif"

# Process: Iterate Rasters
arcpy.env.workspace = ws
ras_names = arcpy.ListRasters()
for ras_name in ras_names:
    name, ext = os.path.splitext(ras_name)
    ras = arcpy.Raster(os.path.join(ws, ras_name))

    out_ras = arcpy.sa.Int((ras - ras.minimum) * 255 / (ras.maximum - ras.minimum))
    out_ras.save(v_name_sc_tif.format(name))
johnpolo
New Contributor III

Xander, the code worked. The maths were creating minimum values like "-0.00002", which is kind of annoying. I am sure there is a way to fix that, but otherwise, everything else seemed good. Thanks very much for the code!

0 Kudos