2

I'm building a python add in button that calculates elevation change. I want the user to be able to click the button, click one cell on a DEM, click another cell, and then display a message that has the calculated elevation change between the two cells. Below is my code. When I install it and click on the newly created button in ArcMap nothing happens. My cursor doesn't even change to the one I selected.

Can anyone tell me what my code is missing?

import arcpy
import pythonaddins

class CollectElevations(object):
    """Implementation for ElevationDifference_addin.CollectElevations 
(Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = True
        self.shape="None"
        global clickCount
        self.cursor=3
        clickCount=0

    def onMouseDownMap(self,x,y,button,shift):
        global clickCount
        rasters = arcpy.ListRasters()

        if clickCount==0:
            for raster in rasters:
                self.result=arcpy.GetCellValue_management(raster, "{} {}".format(x, y), "1").getOutput(0)
                clickCount=1

        elif clickCount==1:
            for raster in rasters:
                self.result2=arcpy.GetCellValue_management(raster, "{} {}".format(x, y), "1").getOutput(0)
                message = "Elevation difference is"+str(" ")+str(self.result-self.result2)
                pythonaddins.MessageBox(message, "Results:")
                clickCount=0
  • Have you created this as a button or as a tool? Buttons do something when they're clicked on and then stop when they reach the end of the code, tools stay active while still the current tool. Buttons, by nature, don't have onMouse... events because they hold focus until they end. BTW you haven't set the workspace for ListRasters but I think you would want to use arcpy.mapping.ListLayers instead and filter where the layer type is a raster layer (layer.isRasterLayer). – Michael Stimson Apr 27 '17 at 3:39
2

Got it to work. Here's the solution for anyone curious.

import arcpy
import pythonaddins

class ElevationDifference(object):
    """Implementation for ElevationDifferenceFinder_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "NONE"
        self.cursor=3
        global clickCount
        global result
        clickCount=0

    def onMouseDownMap(self, x, y, button, shift):

        global clickCount
        global result

        raster = pythonaddins.GetSelectedTOCLayerOrDataFrame()

        if not raster:
            mess="You must first select the DEM in the Table of Contents"
            pythonaddins.MessageBox(mess,"Warning:")

            return

        if clickCount==0:

            value=arcpy.GetCellValue_management(raster, "{} {}".format(x, y), "1")
            result=value.getOutput(0)
            print "Result 1 is {}".format(result)

            clickCount=1

        elif clickCount==1:

            result2=arcpy.GetCellValue_management(raster, "{} {}".format(x, y), "1").getOutput(0)
            print "Result 2 is {}".format(result2)


            finalresult=float(result)-float(result2)

            message = "Elevation difference is "+str(finalresult)
            pythonaddins.MessageBox(message, "Results:")

            clickCount=0
            result=0
  • raster should be a layer or data frame, I would suggest testing this object in three steps if type(raster) is arcpy.mapping.Layer: if not raster.isBroken: if raster.isRasterLayer: before trying to GetCellValue with it. – Michael Stimson Apr 27 '17 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.