Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 import *
import pythonaddins


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, '*')[0]
        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)
            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")
                    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 " + + ' 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")
                    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)
share|improve this question
I tried to recreate your problem but was never able to get a different value for that of the entire value of my raster whether I set the env.mask and got the raster properties using Arcpy or by manually setting the mask environment and using the the Get Raster Properties GUI. One solution might be to extract by mask your raster using the selected polygon and then get the raster values you are looking for. – F_Kellner Dec 31 '15 at 20:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.