Raster Calculator within loop

2492
2
Jump to solution
11-26-2010 01:07 AM
Yaseen_TahaMustafa
New Contributor III
Hi guys,
I want to use raster calculator with loop to consider many raters. however i got the following error:
[HTML]
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)
Failed to execute (RasterCalculator).
[/HTML]

any help would appreciate

Python script is:

import sys, string, os, arcgisscripting, math, arcpy
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

gp.workspace = "D:/Rc_FPAR_test/FPAR1000"

out_workspace = "D:/Rc_FPAR_test/FPAR250"
# Get a list of grids in the workspace.
rasters = gp.ListRasters("","TIF")

raster = rasters.next()
while raster:
    print raster
    raster = rasters.next()
rasters.reset()
raster = rasters.next()

print gp.GetMessages()

while raster:
    # Set local variables
    InRaster = raster
    # Set the outputname for each output to be the same as the input.
    OutRaster = out_workspace + "/" + raster
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
    arcpy.gp.RasterCalculator_sa("0.5913 *" + InRaster + " + 0.3896", OutRaster)
    
    print raster
    raster = rasters.next()
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Yaseen_TahaMustafa
New Contributor III
Hi guys,
I have solved the problem by helping of one of my friend.
we recognize that [HTML]arcpy.gp.RasterCalculator_sa[/HTML] does not exist and does not work in python. we used the addition and the multiplication operations of Raster Calculator, and we did in two separate lines as follows:
import sys, string, os, arcgisscripting, math, arcpy
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

gp.workspace = "D:/Rc_FPAR_test/FPAR1000"

out_workspace = "D:/Rc_FPAR_test/FPAR250"
# Get a list of grids in the workspace.
rasters = gp.ListRasters("","TIF")

raster = rasters.next()
while raster:
    print raster
    raster = rasters.next()
rasters.reset()
raster = rasters.next()

print gp.GetMessages()

while raster:
    # Set local variables
    InRaster = raster
    # Set the outputname for each output to be the same as the input.
    OutRaster = out_workspace + "/" + raster
    # Check out Spatial Analyst extension license
    
    gp.CheckOutExtension("Spatial")
    gp.Times_sa(InRaster, 0.5913 , OutRaster)
    outTimes = Raster(InRaster) * 0.5913
    outPlus = outTimes + 0.3896
    outPlus.save(OutRaster)
    print raster
    raster = rasters.next()
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()
]

Cheers,

abotaha

View solution in original post

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus
perhaps
    print gp.GetMessages()

should be unindented such as
print gp.GetMessages()
0 Kudos
Yaseen_TahaMustafa
New Contributor III
Hi guys,
I have solved the problem by helping of one of my friend.
we recognize that [HTML]arcpy.gp.RasterCalculator_sa[/HTML] does not exist and does not work in python. we used the addition and the multiplication operations of Raster Calculator, and we did in two separate lines as follows:
import sys, string, os, arcgisscripting, math, arcpy
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

gp.workspace = "D:/Rc_FPAR_test/FPAR1000"

out_workspace = "D:/Rc_FPAR_test/FPAR250"
# Get a list of grids in the workspace.
rasters = gp.ListRasters("","TIF")

raster = rasters.next()
while raster:
    print raster
    raster = rasters.next()
rasters.reset()
raster = rasters.next()

print gp.GetMessages()

while raster:
    # Set local variables
    InRaster = raster
    # Set the outputname for each output to be the same as the input.
    OutRaster = out_workspace + "/" + raster
    # Check out Spatial Analyst extension license
    
    gp.CheckOutExtension("Spatial")
    gp.Times_sa(InRaster, 0.5913 , OutRaster)
    outTimes = Raster(InRaster) * 0.5913
    outPlus = outTimes + 0.3896
    outPlus.save(OutRaster)
    print raster
    raster = rasters.next()
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()
]

Cheers,

abotaha
0 Kudos