English

How To: Get the accurate X,Y,Z from a Raster layer from a mouse click programmatically

Summary

This document will demonstrate how to determine the accurate X,Y,Z values from a Raster layer using a mouse click.

Procedure

  • The following code illustrates how you can perform this task. Use it in the MouseDown event of a tool:

    Code:
    Private Sub RasterIdentifyTool_MouseDown(ByVal button As Long, ByVal shift As
    Long, ByVal x As Long, ByVal y As Long)
    Dim pMxDoc As IMxDocument
    Dim pRasLyr As IRasterLayer
    Dim pRasRen As IRasterRenderer
    Dim pIdentify As IIdentify
    Dim pArray As IArray
    Dim pPoint As IPoint
    Dim pGeom As IGeometry
    Dim pRasID As IRasterIdentifyObj

    Set pMxDoc = ThisDocument
    Set pPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
    Set pGeom = pPoint

    Set pRasLyr = pMxDoc.FocusMap.Layer(0)
    Set pRasRen = pRasLyr.Renderer
    Set pIdentify = pRasLyr 'Test RasterLayer
    'Set pIdentify = pRasRen 'Test RasterRenderer

    Set pArray = pIdentify.Identify(pGeom)
    If TypeOf pArray.Element(0) Is IRasterIdentifyObj Then
    Set pRasID = pArray.Element(0)
    Else
    MsgBox "failed"
    Exit Sub
    End If

    MsgBox pRasID.MapTip, , pRasID.Location.x & "," & pRasID.Location.y

    End Sub

  • The returned value changes with the renderers for different image types:
    Code:
    stretched renderer returns - pixel value
    rgb renderer returns - r, g, b values
    classified renderer returns - pixel value
    unique value renderer returns - OID


    NOTE: the unique value renderer returns the OID value from the attribute table instead of the pixel value. The OID value can be converted to report the correct value, but this has not been addressed in the supplied code.
  • The interpolation of the display will affect the results of the identify. Set the interpolation to Nearest Neighbor to get the actual cell value.