I've created a button on a python addin toolbar which uses the selected features from the selected layer in the TOC, creates a feature layer from that selection and uses that feature layer to set the environment mask. This is so I can get the raster properties (max, mean) for just my area of interest from a larger raster.
I had originally coded it as a tool/script in ArcToolbox and that tool works fine. Now after converting it to be used as addin toolbar button, the tool works but does not recognise the environment mask so gives me the max and mean for the whole slope raster.
Can anyone see why this would be the case or offer any suggestions? (suggestions are welcome for more efficient code too :)
import os, arcpy from arcpy import env from arcpy.sa import * import pythonaddins arcpy.CheckOutExtension("Spatial") class Terrain_Statistics(object): """Implementation for FISToolBar_addin.Terrain_Statistics (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): mxd = arcpy.mapping.MapDocument('current') #df = arcpy.mapping.ListDataFrames(mxd, '*') TocLayer = pythonaddins.GetSelectedTOCLayerOrDataFrame()# Use the selected layer in the Table of Contents outLocation = 'O:\\GeomasterPublicData\\General\\Terrain.mdb' # hardcoded for now if not isinstance(TocLayer, arcpy.mapping.Layer): # if a layer in TOC hasn't been selected, give error pythonaddins.MessageBox('Please select polygons AND the polygon layer in the TOC', 'Error', 0) else: count = 0 # count the selection count = arcpy.GetCount_management(TocLayer) if count >= 1: cursor = arcpy.UpdateCursor(TocLayer) for row in cursor: forest = row.getValue("Forest") #forest = '903' pythonaddins.MessageBox('Selected ' + str(count) + ' features in ' + forest,'INFO', 0) arcpy.MakeFeatureLayer_management(TocLayer, "Selected") try: arcpy.env.mask = "Selected" slopeResult = arcpy.GetRasterProperties_management(outLocation +'\\Slope_' + forest, "MEAN") maxResult = arcpy.GetRasterProperties_management(outLocation +'\\Slope_' + forest, "MAXIMUM") slope = slopeResult.getOutput(0) maximum = maxResult.getOutput(0) pythonaddins.MessageBox("Selected " + TocLayer.name + ' polygons' + '\n' + "Slope Model: Slope_" + forest + '\n' + "------------------------------" + '\n' + "Mean Slope: " + str(slope) + '\n' + "Max Slope: " + str(maximum)+ '\n', 'Sale Area Terrain Information', 0) #arcpy.mapping.RemoveLayer(df, "Selected") except: pythonaddins.MessageBox("Something went wrong", 'Error',0) #arcpy.mapping.RemoveLayer(df, "Selected") elif count < 1: pythonaddins.MessageBox("No features selected. Please select polygons and try again", 'INFO', 0)