Run raster calculation on multiple lists of rasters

2972
6
Jump to solution
08-30-2012 12:24 PM
by Anonymous User
Not applicable
So I'm having a little trouble here...This is part of a larger script and I can get everything to work up until this point.

I have an equation I need to throw in the Raster Calculator:

((DEM - MIN) / (MAX - MIN) * 100)


I need to perform this equation 6 times (I have 6 DEMs, 6 Mins, and 6 Maxes numbered in ascending order in the gdb).  What I was hoping to do was set up an equation where I can do something like this:

# List 3 raster types for input for raster calculator demlist = arcpy.ListRasters('DEM_ext*') minlist = arcpy.ListRasters('ZonalMin*') maxlist = arcpy.ListRasters('ZonalMax*')


From this list I would then like to use this equation in the raster calculator:

# Starting from the first raster of each type and iterate through to the sixth [0] - [5]  # Start at index [0]    expression = ((demlist[0] - minlist[0]) / (maxlist[0] - minlist[0])* 100)  # Then go to [1] and so on... outrast = outpath + os.sep + 'Relative_elv' + str(index)  # index here is a variable to represent a raster in the list RasterCalculator(expression, outrast)  # index += 1 


I would like this to iterate through and perform this same calculation on all 6 rasters (DEM, MIN, MAX).  I have been trying to figure out a way but I am at a loss as to how to do this? What would be a good way to initiate a loop to go through and perform this calculation?  Thanks.


This is what each list of rasters looks like:
[ATTACH=CONFIG]17386[/ATTACH]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
The Raster Calculator is designed for use as a tool or in ModelBuilder.
You'd do well to do this task in Python map algebra instead.

For example, use Raster() to cast them to type Raster so the map algebra will work.

from arcpy.sa import *  arcpy.workspace = path  # direct where the input and output is  # list inputs demlist = arcpy.ListRasters('DEM_ext*') minlist = arcpy.ListRasters('ZonalMin*') maxlist = arcpy.ListRasters('ZonalMax*')  # Starting from the first raster of each type and iterate through to the sixth [0] - [5] # Start at index [0]    for k in range(6): # 0,1,2... 5     outrast = 100.0 * ((Raster(demlist) - Raster(minlist)) / \                     (Raster(maxlist) - Raster(minlist))        outrastName = 'Rel_elv%s' % k # grid names should be < 13 chars     outrast.save(outrastName)

View solution in original post

0 Kudos
6 Replies
curtvprice
MVP Esteemed Contributor
The Raster Calculator is designed for use as a tool or in ModelBuilder.
You'd do well to do this task in Python map algebra instead.

For example, use Raster() to cast them to type Raster so the map algebra will work.

from arcpy.sa import *  arcpy.workspace = path  # direct where the input and output is  # list inputs demlist = arcpy.ListRasters('DEM_ext*') minlist = arcpy.ListRasters('ZonalMin*') maxlist = arcpy.ListRasters('ZonalMax*')  # Starting from the first raster of each type and iterate through to the sixth [0] - [5] # Start at index [0]    for k in range(6): # 0,1,2... 5     outrast = 100.0 * ((Raster(demlist) - Raster(minlist)) / \                     (Raster(maxlist) - Raster(minlist))        outrastName = 'Rel_elv%s' % k # grid names should be < 13 chars     outrast.save(outrastName)
0 Kudos
by Anonymous User
Not applicable
The Raster Calculator is designed for use as a tool or in ModelBuilder.
You'd do well to do this task in Python map algebra instead.

For example, use Raster() to cast them to type Raster so the map algebra will work.

Code:

from arcpy.sa import *
arcpy.workspace = path  # direct where the input and output is

# list inputs
demlist = arcpy.ListRasters('DEM_ext*')
minlist = arcpy.ListRasters('ZonalMin*')
maxlist = arcpy.ListRasters('ZonalMax*')

# Starting from the first raster of each type and iterate through to the sixth [0] - [5]
# Start at index [0]  
for k in range(6): # 0,1,2... 5
    outrast = 100.0 * ((Raster(demlist) - Raster(minlist)) / \
                    (Raster(maxlist) - Raster(minlist))  
    outrastName = 'Rel_elv%s' % k # grid names should be < 13 chars
    outrast.save(outrastName)



Curtis,

Thank you very much for the reply!  One question though, I see that you are using the parameter substitution on the Rel_elv output.  I am still pretty to new to python so I am just wondering what that does?  


This worked perfectly! Thanks again!

Caleb
0 Kudos
Leo_KrisPalao
New Contributor II

Hi Caleb and Curtis,

I am following the expression/syntax of this thread. I have successfully ran the script. I just have some questions. One is, how can I save my output file that the filename looks like file_001 (PET_001....to PET_037) and not file_00. My filename always starts with file_00. Is there a way to concatenate the file naming to: file_037 and not file_0037.

Another, how can I save it as .tif. Is this as simple as this: 'Re'_elv%s' %k + ".tif"

Hope my query is clear.

Thanks very much,

-Leo

0 Kudos
curtvprice
MVP Esteemed Contributor

Yes, you can use python string formatting to have full control over leading zeros, AND the way to save a tif is to give a folder path (or have the current workspace be a folder, and use a .tif extension on the filename. The there are some variations on TIFF format (compression, tiling etc) that can be controlled by settings in the geoprocessing environment (10.0 and later).

You can use the modern ".format()" string formatting instead of the old (but still supported C-like "%" formatting I used above. Which is best? I think the % syntax was going to be deprecated but many programmers just like it so it unlikely to go away - it's still there in Python 3.4. However, I am liking the new form the more I use it, it's a little more powerful and I think it's easier to read (and therefore debug).

>>> "%03d" % 3

'003'

>>> "{0:03d}".format(3)

'003'

Here it is in my code sample.

outrastName = 'Rel_elv{03d}.tif".format(k) # Python 2.6+

outrastName = 'Rel_elv%03d.tif" % k # deprecated - but still works

outrast.save(outrastName)

Leo_KrisPalao
New Contributor II

Hi Curtis,

Thanks for your suggestion. It worked okay. I am reading some of the linked in your reply about string operation.

-Leo

0 Kudos
NeilAyres
MVP Alum

Or even ...

>>> n = 3

>>> str(n).zfill(3)

'003'

>>>

Cheers,

Neil