# List 3 raster types for input for raster calculator 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] 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
Solved! Go to Solution.
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)
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)
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)
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
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)
Hi Curtis,
Thanks for your suggestion. It worked okay. I am reading some of the linked in your reply about string operation.
-Leo
Or even ...
>>> n = 3
>>> str(n).zfill(3)
'003'
>>>
Cheers,
Neil