evtguy

Trouble with file based IRaster vs SDE IRaster source

Discussion created by evtguy on Apr 22, 2014
One of my converted VBA tools allows the user to quickly create a custom color ramp for elevation data and bypass the annoyingly long statistics building step when you do the same thing through the layer's property dialog. In my tool, a simple dialog appears in which you select the raster, enter a starting elevation, the number of classes to render, and finally the interval of each class. I use this tool to explore the subtle features found in LIDAR data.

Anyways, VBA and dot.net versions of my code USED to work fine whether the data was coming from SDE or a file based raster format (IMG, TIFF, etc). Now the code only works for rasters coming from SDE and I can't seem to understand why this is. Can anyone see what I'm missing in my attached code? This is the code that runs once the user clicks OK:
   Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Dim I As Integer
        Dim minElev As Long
        Dim numClasses As Integer
        Dim theInterval As Double
        Dim theRaster As String
        Dim curElev As Double
        Dim application As ESRI.ArcGIS.Framework.IApplication
        Dim pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
        Dim pMap As ESRI.ArcGIS.Carto.IMap
        Dim pID As New ESRI.ArcGIS.esriSystem.UID
        Dim pEnumLayer As ESRI.ArcGIS.Carto.IEnumLayer
        Dim pLayer As ESRI.ArcGIS.Carto.ILayer

        Dim pRLayer As ESRI.ArcGIS.Carto.IRasterLayer
        Dim pRaster As ESRI.ArcGIS.Geodatabase.IRaster

        Dim PMouseCursor As ESRI.ArcGIS.Framework.IMouseCursor
        PMouseCursor = New ESRI.ArcGIS.Framework.MouseCursor
        PMouseCursor.SetCursor(0)

        Try
            Application = My.ArcMap.Application
            Dim document As ESRI.ArcGIS.Framework.IDocument = Application.Document

            pMxDoc = application.Document
            pMap = pMxDoc.FocusMap
            pID.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}"


            If String.IsNullOrEmpty(Me.txtStartElev.Text) Or Me.txtStartElev.Text = "" Then
                MsgBox("You did not specify a starting elevation.", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            End If

            If String.IsNullOrEmpty(Me.txtNumLevels.Text) Or Me.txtNumLevels.Text = "" Then
                MsgBox("You did not specify the number of classes.", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            End If

            If String.IsNullOrEmpty(Me.cboInterval.Text) Or Me.cboInterval.Text = "" Then
                MsgBox("You did not specify the interval.", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            End If

            minElev = CInt(Me.txtStartElev.Text)
            numClasses = CInt(Me.txtNumLevels.Text)
            theInterval = CDbl(Me.cboInterval.Text)
            theRaster = Me.cboRasterLyr.Text
            curElev = minElev

            pEnumLayer = pMap.Layers(pID, True)
            pEnumLayer.Reset()
            pRLayer = Nothing

            pLayer = pEnumLayer.Next

            Do While Not pLayer Is Nothing
                If TypeOf pLayer Is ESRI.ArcGIS.Carto.IRasterLayer Then
                    If StrComp(pLayer.Name, theRaster, vbTextCompare) = 0 Then
                        pRLayer = pLayer
                    End If
                End If
                pLayer = pEnumLayer.Next
            Loop

            If TypeOf pRLayer.Renderer Is ESRI.ArcGIS.Carto.IRasterStretchColorRampRenderer Then
                Dim question, answer

                question = "It may take a long time in order to change this raster's" & vbNewLine
                question = question & "symbology. Are you sure you would like to continue?" & vbNewLine

                answer = MsgBox(question, MsgBoxStyle.YesNo, "WARNING!")

                If answer = vbNo Then
                    Exit Sub
                End If
            End If

            'Create classfy renderer and QI RasterRenderer interface
            pRaster = pRLayer.Raster
            Dim pClassRen As ESRI.ArcGIS.Carto.IRasterClassifyColorRampRenderer
            pClassRen = New ESRI.ArcGIS.Carto.RasterClassifyColorRampRenderer
            Dim pRasRen As ESRI.ArcGIS.Carto.IRasterRenderer
            pRasRen = pClassRen

            'These simple three lines is all you need in order to change the color ramp
            'that is used. They do need to exist shortly after pClassRen is initialized
            'otherwise the specification is ignored..
            Dim pClassProp As ESRI.ArcGIS.Carto.IRasterClassifyUIProperties
            pClassProp = pClassRen
            pClassProp.ColorRamp = "Temperature" 'Name of Ramp from Style Manager

            'Set raster for the render and update
            pRasRen.Raster = pRaster
            pClassRen.ClassCount = numClasses
            pRasRen.ResamplingType = ESRI.ArcGIS.Geodatabase.rstResamplingTypes.RSP_BilinearInterpolation 'Set to Bilinear Interpretation
            pRasRen.Update() 'THIS IS WHERE THE CODE CRASHES


            'loop through the classes and update the label
            For I = 0 To pClassRen.ClassCount - 1

                pClassRen.Break(I) = curElev

                Select Case I
                    Case 0
                        pClassRen.Label(I) = "< " & CStr(curElev) & " Feet"
                    Case pClassRen.ClassCount - 1
                        pClassRen.Label(I) = "> " & CStr(curElev) & " Feet"
                    Case Else
                        pClassRen.Label(I) = CStr(curElev) & " - " & CStr(curElev + theInterval)
                End Select

                curElev = curElev + theInterval
            Next I


            'Update the renderer and plug into layer
            pRasRen.Update()
            pRLayer.Renderer = pClassRen
            pMxDoc.ActiveView.Refresh()
            pMxDoc.UpdateContents()


            'Release memeory
            pMxDoc = Nothing
            pMap = Nothing
            pRLayer = Nothing
            pRaster = Nothing
            pRasRen = Nothing
            pClassRen = Nothing
            pClassProp = Nothing

            Me.Close()
        Catch ex As Exception
            globalErrorHandler(ex)
        End Try
    End Sub


The error handler returns the following when it crashes:
ERROR DESCRIPTION:  Error HRESULT E_FAIL has been returned from a call to a COM component.
TYPE OF ERROR:   System.Runtime.InteropServices.COMException
STACK TRACE:   at ESRI.ArcGIS.Carto.RasterClassifyColorRampRendererClass.Update()
   at pwTools2.frmAdjustColorramp.OK_Button_Click(Object sender, EventArgs e) in C:\Visual Studio 2008\Projects\pwTools2\pwTools2\frmAdjustColorramp.vb:line 105


Any ideas? THANKS!

Steve

Outcomes