Select to view content in your preferred language

arcpy.management.GetCellValue returning NoData

2972
4
02-25-2013 08:01 PM
JonPedder
Deactivated User
Been struggling with this for a couple of days now and could use a fresh set of eyes on the problem. I'm trying to get an elevation spot value for a DEM file. When I inspect any point with the Information Tool I get a pixel value of elevation. However when I try tp pull the value using arcpy.management.GetCellValue() I always get a NoData.

This is pretty generic code that was pulled form Google searches, it reports the X and Y correctly just not the pixel value.

Any help would be appreciated

Thanks

class ToolClass10(object):
    """Implementation for MapSARElevation_addin.tool_1 (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDown(self, x, y, button, shift):
        pass
    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")
Tags (2)
0 Kudos
4 Replies
JonPedder
Deactivated User
Working this problem some more this morning, I thought I MUST be passing bad coordinates and pointing to a location where there is indeed have no data. I've tried several ways to manually enter the location points, still no luck though, cellvalue still returns �??noData�??

From a python IDE session
rasterLayer = <map layer u'grdn35w121_13'>
>>> rasterLayer.isRasterLayer
True
x = '213860'
y = '3835693'
>>> place = '%s %s' % (x,y)
>>> place
'213860 3835693'
cellvalue = arcpy.management.GetCellValue(rasterLayer, place)
>>> cellvalue
<Result 'NoData'>


From the screen shot there is data at these x, y points. Not sure where I'm going wrong here, the arcmap �??help�?? doesn't exactly provide much help either.

[ATTACH=CONFIG]22174[/ATTACH]
0 Kudos
JimCousins
MVP Regular Contributor
Jon,
I had success with your code in the active window Note: 2 raster layers returned, and values 0 and 89 are correct:
[HTML]>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> df = mxd.activeDataFrame
>>> rasLayers = arcpy.mapping.ListLayers(mxd,'Chromium*', df)
>>> for lyr in rasLayers:
...     x='570715'
...     y='133340'
...     place = '%s %s' % (x,y)
...     cellvalue = arcpy.management.GetCellValue(lyr, place)
...     print cellvalue
...    
0
89
[/HTML]
0 Kudos
curtvprice
MVP Esteemed Contributor
The tool ref doesn't say it is honoring raster environment settings - but maybe you should check the extent and mask, just to be sure.
0 Kudos
SimonElfert
New Contributor
Hi everyone,

I am perfectly aware that the post is almost one year now, but I was struggeling with the same problem as Jon and searched the internet for two days without explanation.

If I am not the only one left, this posting - perhaps - helps others, having this issue, too.

In my case, I overlooked the coordinate system of the input raster. I was thinking to handle a projected raster, when indeed it turned out that only my dataframe (due to the rest of the layers - which were projected) was in a Swiss projection. Thus, I had to either use geographic coordinates (with me: WGS84) in arcpy.management.GetCellValue or project my raster to Swiss grid before inquiring a raster cell value (then with projection coordinates). Both ways worked out for me.

Hopefully, someone can take advantage of this.

Cheers,
Simon
0 Kudos