Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have asked this question a while back but within arcgis environment without using addin. Now i am trying to do the same task to get pixel value when user click anywhere on screen, to use in later steps. Following pythonaddin button code snippet returns nothing. Any suggestions where is the catch?

class Val(object):
"""Implementation for Pythonaddin_addin.genW (Button)"""
def __init__(self):
    self.enabled = True
    self.checked = False
def onClick(self):
    geog = arcpy.GetParameterAsText(0)
    ras = arcpy.PointToRaster_conversion(geog, "ID", "in_memory/ras", "MAXIMUM", "",30)
    pass
share|improve this question
    
What exactly are you trying to do? The code you have written makes no sense in what little context you have provided. –  blah238 Nov 21 '12 at 21:54
    
I want to get a single pixel value when user click anywhere on map and use that value to create a single pixel raster. –  Ibe Nov 21 '12 at 23:56
1  
I learned that i should use tool instead of button for interaction with screen. –  Ibe Nov 22 '12 at 0:27
add comment

1 Answer

up vote 2 down vote accepted

As you mentioned, you must implement a Tool add-in class instead of a button. Then, write your logic in the class's onMouseDownMap method. The arguments passed in to this method include the X and Y coordinates of the clicked location, in the map's coordinate system.

From there it's basically up to you to figure out what to do in response to that event, but here is a simple function to report the coordinates and cell value at the clicked location for the first raster layer in the first map in the current map document.

def onMouseDownMap(self, x, y, button, shift):
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    layers = arcpy.mapping.ListLayers(mxd, None, df)
    rasterlayer = None
    for layer in layers:
        if layer.isRasterLayer:
            rasterlayer = layer
            break
    if rasterlayer:
        cellvalue = arcpy.management.GetCellValue(rasterlayer, "{} {}".format(x, y)).getOutput(0)
        pythonaddins.MessageBox("XY: {}, {}\nCell Value: {}".format(x, y, cellvalue), "Results")

Example output:

Output

share|improve this answer
    
thanks. Its helpful. –  Ibe Nov 29 '12 at 4:54
    
"arcpy.management.GetCellValue" should be "arcpy.GetCellValue_management" in the above code. –  PolyGeo Mar 26 '13 at 1:38
    
Either works, try it out. –  blah238 Mar 26 '13 at 2:17
add comment

Your Answer

 
discard

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

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