racheski

Python efficiency with Rasters

Discussion created by racheski on Sep 21, 2012
Latest reply on Sep 24, 2012 by arekmatoszka
The script below multiplies two rasters, where the cell value of one raster is a parameter.
#Import modules
import os
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

#Set local variables
wks = arcpy.GetParameterAsText(0) # input workspace
inRaster1 = arcpy.GetParameterAsText(1) # input raster layer
env.workspace = wks

#Perform Constant Raster Function
constantValue = arcpy.GetParameterAsText(2) # input weight value
cellSize = 100
outExtent = Extent(871599.89, 982834.44, 912502.09, 1070920.9)
outConstRaster = CreateConstantRaster(constantValue, "FLOAT", cellSize, outExtent)
outConstRaster.save("temp1")

#Perform Times Function
outTimes = Raster(inRaster1)* Raster("temp1")
outRaster1 = "Wt_" + os.path.basename(inRaster1)
outTimes.save(outRaster1)

#Create script output and delete constant raster
arcpy.SetParameterAsText(3,outRaster1)
arcpy.Delete_management("temp1")


This works, but is not optimal and much slower than I would like.  I tried this code without saving outConstRaster and just calling it as a raster object, but I kept getting the same error: "TypeError: expected a raster or layer name."  This will also need to work for up to 10 different raster layers.

Any thoughts on making this more efficient?

Outcomes