# Single_Output_Map_Algebra_sample.py # Description: # Runs an expression built with the Map Algebra language to # output a new raster dataset. # Requirements: None # Author: ESRI # Date: Sept 6, 2005 # Import system modules import traceback import arcgisscripting # Create the Geoprocessor object gp = arcgisscripting.create() try: # Set local variables InRaster = gp.GetParameterAsText(0) StudyAreaRaster = gp.GetParameterAsText(1) #Example from Raster Calculator # rclssb2_md = CON([studyarea250],CON(IsNull([rclssb2]),-99,[rclssb2])) InExpression = "CON(%s,CON(IsNull(%s),-99,%s))"%(StudyAreaRaster,InRaster,InRaster) OutRaster = gp.GetParameterAsText(2) # Check out Spatial Analyst extension license gp.CheckOutExtension("Spatial") # Process: Map Algebra Statement gp.SingleOutputMapAlgebra_sa(InExpression, OutRaster) gp.SetParameterAsText(2, OutRaster) except: # get the traceback object tb = sys.exc_info()[2] # tbinfo contains the line number that the code failed on and the code from that line tbinfo = traceback.format_tb(tb)[0] # concatenate information together concerning the error into a message string pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \ str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n" # generate a message string for any geoprocessing tool errors if len(gp.GetMessages(2)) > 0: msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n" gp.AddError(msgs) # return gp messages for use with a script tool gp.AddError(pymsg) # print messages for use in Python/PythonWin print msgs print pymsg