""" Prints total area in sq km of each raster value of each input raster and the mask raster. """ import sys, os, traceback from SDM_Utilities import rowgen def testDataAreas(gp): try: rasters = gp.getParameterAsText(0) ras_name = {} ras_count = {} ras_cellsize = {} if gp.mask: rasters += ';' + gp.mask for nras, raster in enumerate(rasters.split(';')): ras_name[nras] = raster ras_cellsize[nras] = gp.describe(raster).MeanCellHeight for raster in rowgen(gp.searchcursor(raster)): if nras in ras_count: ras_count[nras] += raster.count else: ras_count[nras] = raster.count for nras in sorted(ras_name.keys()): if nras in ras_count: count = ras_count[nras] cellsize = ras_cellsize[nras] area = count * cellsize * cellsize / 1000000.0 else: count, cellsize, area = 0, 0, 0 gp.addwarning("%-20s counts: %10d cell size: %6.1f area: %12.4f" \ %(ras_name[nras], count, cellsize, area)) except Exception, msg: # 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 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 pymsg print msgs raise if __name__ == '__main__': import arcgisscripting gp = arcgisscripting.create() testDataAreas(gp)