Hello Everyone,
I am working on a script that multiplies rasters by a given constant using the Times tool. Both the rasters and constants are parameters in the script. However, I am receiving an error that I don't know how to fix:
# -*- utf-8 -*- # --------------------------------------------------------------------------- # Times.py # Created on: 2012-09-20 14:34:40.00000 # (generated by ArcGIS/ModelBuilder) # Usage: Times <Risk_Layer> <Weight> <Risk_Layer__2_> <Weight__2_> <Risk_Layer__3_> <Weight__3_> <Risk_Layer__5_> <Weight__5_> <Risk_Layer__4_> <Weight__4_> <Risk_Layer__6_> <Weight__6_> <Risk_Layer__7_> <Weight__7_> <Risk_Layer__8_> <Weight__8_> <Risk_Layer__10_> <Weight__10_> <Risk_Layer__9_> <Weight__9_> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Check out any necessary licenses arcpy.CheckOutExtension("spatial") #Set geoprocessing environment inWorkspace = arcpy.GetParameterAsText(0) arcpy.env.workspace = inWorkspace arcpy.env.overwriteOutput = True # Script arguments Risk_Layer = arcpy.GetParameterAsText(1) Weight = arcpy.GetParameterAsText(2) Risk_Layer__2_ = arcpy.GetParameterAsText(3) Weight__2_ = arcpy.GetParameterAsText(4) Risk_Layer__3_ = arcpy.GetParameterAsText(5) Weight__3_ = arcpy.GetParameterAsText(6) Risk_Layer__5_ = arcpy.GetParameterAsText(7) Weight__5_ = arcpy.GetParameterAsText(8) Risk_Layer__4_ = arcpy.GetParameterAsText(9) Weight__4_ = arcpy.GetParameterAsText(10) Risk_Layer__6_ = arcpy.GetParameterAsText(11) Weight__6_ = arcpy.GetParameterAsText(12) Risk_Layer__7_ = arcpy.GetParameterAsText(13) Weight__7_ = arcpy.GetParameterAsText(14) Risk_Layer__8_ = arcpy.GetParameterAsText(15) Weight__8_ = arcpy.GetParameterAsText(16) Risk_Layer__10_ = arcpy.GetParameterAsText(17) Weight__10_ = arcpy.GetParameterAsText(18) Risk_Layer__9_ = arcpy.GetParameterAsText(19) Weight__9_ = arcpy.GetParameterAsText(20) # Local variables: Weight__Risk_Layer_ = Risk_Layer Weight__Risk_Layer__2__ = Risk_Layer__2_ Weight__Risk_Layer__3__ = Risk_Layer__3_ Weight__Risk_Layer__4__ = Risk_Layer__4_ Weight__Risk_Layer__5__ = Risk_Layer__5_ Weight__Risk_Layer__6__ = Risk_Layer__6_ Weight__Risk_Layer__7__ = Risk_Layer__7_ Weight__Risk_Layer__8__ = Risk_Layer__8_ Weight__Risk_Layer__9__ = Risk_Layer__9_ Weight__Risk_Layer__10__ = Risk_Layer__10_ # Process: Times a rcpy.gp.Times_sa(Risk_Layer, Weight, Weight__Risk_Layer_) # Process: Times (2) arcpy.gp.Times_sa(Risk_Layer__2_, Weight__2_, Weight__Risk_Layer__2__) # Process: Times (3) arcpy.gp.Times_sa(Risk_Layer__3_, Weight__3_, Weight__Risk_Layer__3__) # Process: Times (4) arcpy.gp.Times_sa(Risk_Layer__4_, Weight__4_, Weight__Risk_Layer__4__) # Process: Times (5) arcpy.gp.Times_sa(Risk_Layer__5_, Weight__5_, Weight__Risk_Layer__5__) # Process: Times (6) arcpy.gp.Times_sa(Risk_Layer__6_, Weight__6_, Weight__Risk_Layer__6__) # Process: Times (7) arcpy.gp.Times_sa(Risk_Layer__7_, Weight__7_, Weight__Risk_Layer__7__) # Process: Times (8) arcpy.gp.Times_sa(Risk_Layer__8_, Weight__8_, Weight__Risk_Layer__8__) # Process: Times (9) arcpy.gp.Times_sa(Risk_Layer__9_, Weight__9_, Weight__Risk_Layer__9__) # Process: Times (10) arcpy.gp.Times_sa(Risk_Layer__10_, Weight__10_, Weight__Risk_Layer__1)
And receiving the error:
Traceback (most recent call last): File "\\HDQ_FP2\ISD\racheskin\Desktop\Times.py", line 75, in <module> arcpy.gp.Times_sa(Risk_Layer, Weight, Weight__Risk_Layer_) File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 484, in <lambda> return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: Failed to execute. Parameters are not valid. ERROR 000670: output P:\Crime_Analysis\Testing_Training\RiskTerrainModeling\RobinAdrienne\Assault_Factors.gdb\Bin_Rec_AA_JAS_10 is same as input P:\Crime_Analysis\Testing_Training\RiskTerrainModeling\RobinAdrienne\Assault_Factors.gdb\Bin_Rec_AA_JAS_10 Failed to execute (Times).
I am confused as to why the output ends up the same as the input since I specified the outRasters be named differently in the script.
Help please!
Thanks,
Robin
This is a great example of a case where building a process in modelbuilder and exporting to Python is not the optimal approach. The code you get is just too convoluted and does not take advantage of Python map algebra. The translation may have gone (a little) easier if you had used the Raster Calculator tool instead of many Times tools, but not much.
Rather than fix the code, I recommend a rewrite. I'll do it here with just two rasters input and you can expand it from there.
arcpy.CheckOutExtension("spatial") from arcpy.sa import * wks = arcpy.GetParameterAsText(0) # input workspace Risk1 = arcpy.GetParameterAsText(1) # input raster layer Weight1 = arcpy.GetParameterAsText(2) # input raster layer outRaster1 = arcpy.GetParameterAsText(3) # output raster arcpy.env.workspace = wks out1 = Raster(Risk1) * Raster(Weight1) out1.save(outRaster1)
I would prefer not to have the outRaster name as a parameter, but rather would like all the rasters saved as Weight_%Name% and have the output location as a parameter.
Does setting the input workspace take care of the location
How can I save all the rasters like this automatically?
import os arcpy.CheckOutExtension("spatial") from arcpy.sa import * wks = arcpy.GetParameterAsText(0) # input workspace Risk1 = arcpy.GetParameterAsText(1) # input raster layer Weight1 = arcpy.GetParameterAsText(2) # input raster layer arcpy.env.workspace = wks out1 = Raster(Risk1) * Raster(Weight1) outRaster1 = "Wt_" + os.path.basename(Risk1) out1.save(outRaster1). arcpy.SetParameterAsText(3,outRaster1)
I need Weight1 to be a number (I need to multiply a raster layer by a constant where the constant is a parameter), so I set the parameter data type to double. However, I am now getting an error.
import os arcpy.CheckOutExtension("spatial") from arcpy.sa import * wks = arcpy.GetParameterAsText(0) # input workspace Risk1 = arcpy.GetParameterAsText(1) # input raster layer Weight1 = arcpy.GetParameterAsText(2) # input weight value arcpy.env.workspace = wks out1 = Raster(Risk1) * Weight1 outRaster1 = "Wt_" + os.path.basename(Risk1) out1.save(outRaster1) arcpy.SetParameterAsText(3,outRaster1)
Error: Traceback (most recent call last): File "\\HDQ_FP2\ISD\racheskin\Desktop\MapAlgebra.py", line 9, in out1 = Raster(Risk1) * Weight1 File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\sa\Functions.py", line 4049, in Times in_raster_or_constant2) File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\sa\Utils.py", line 47, in swapper result = wrapper(*args, **kwargs) File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\sa\Functions.py", line 4046, in wrapper return _wrapLocalFunctionRaster(u"Times_sa", ["Times", in_raster_or_constant1, in_raster_or_constant2]) RuntimeError: ERROR 000732: Input Raster: Dataset 2.75 does not exist or is not supported Failed to execute (WeightRisk2).
import os import arcpy from arcpy import env from arcpy.sa import * constantValue = arcpy.GetParameterAsText(0) # input weight value cellSize = 100 outExtent = Extent(871599.89, 982834.44, 912502.09, 1070920.9) arcpy.CheckOutExtension("Spatial") outConstRaster = CreateConstantRaster(constantValue, "FLOAT", cellSize, outExtent) #Set local variables wks = arcpy.GetParameterAsText(1) # input workspace inRaster1 = arcpy.GetParameterAsText(2) # input raster layer env.workspace = wks outRaster = "Ct_" + os.path.basename(inRaster1) outConstRaster.save(outRaster) outTimes = Raster(inRaster1)* Raster(outRaster) outRaster1 = "Wt_" + os.path.basename(inRaster1) outTimes.save(outRaster1) arcpy.SetParameterAsText(3,outRaster1) arcpy.Delete_management(outRaster)
I know this is three years later... too bad no one else picked up this thread...
Your issue with WeightValue is to directly use it in arcpy map algebra you need to convert it back to a number with float() so you can use it as a multiplier. (GetParameterAsText always gives you a string [hence its name!]).
If you just leave the constant raster outRaster as temporary, this will probably delay calculation until you run outTimes.save(). The reason it looked like it was running really fast is that nothing was really happening until you executed the .save() method which runs the calculation.
Local functions get packed up until they are needed which can really speed things up as intermediate grids in some cases do not have to be saved to disk.
import os
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
constantValue = float(arcpy.GetParameterAsText(0)) # input weight value
cellSize = 100
outExtent = arcpy.Extent(871599.89, 982834.44, 912502.09, 1070920.9)
# Set local variables
wks = arcpy.GetParameterAsText(1) # input workspace
inRaster1 = arcpy.GetParameterAsText(2) # input raster layer
arcpy.CheckOutExtension("Spatial")
# run Times
outTimes = Raster(inRaster1) * constantValue
outTimes.save("Wt_" + os.path.basename(inRaster1) )
# return raster (outTimes is a raster object so path is returned "as text"
arcpy.SetParameterAsText(3, outTimes)