0

I am trying to create an add-in button which opens a dialog box filtering only rasterdata sets. After that once I select a Raster dataset, the Hill shade operation should be applied to that Raster dataset. I have written the following code but some where I am getting an error called "Error during conversion process". Please look at the below code and let me know if any changes need to done.

Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.DataSourcesRaster
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.GeoAnalyst
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Catalog

Public Class AddHillshade
Inherits ESRI.ArcGIS.Desktop.AddIns.Button



Public Sub New()

End Sub

Protected Overrides Sub OnClick()
    '
    '  TODO: Sample code showing how to access button host
    '
    My.ArcMap.Application.CurrentTool = Nothing
    Dim pMxDoc As IMxDocument = My.ArcMap.Document
    Dim pMap As IMap = pMxDoc.FocusMap
    Dim bSelectedObject As Boolean
    Dim pGxDialog As ESRI.ArcGIS.CatalogUI.IGxDialog = New ESRI.ArcGIS.CatalogUI.GxDialog
    'only show raster datasets
    Dim pRasterFilter As ESRI.ArcGIS.Catalog.IGxObjectFilter = New ESRI.ArcGIS.Catalog.GxFilterRasterDatasets
    'Layers and document declaration
    Dim pGXSelect As IEnumGxObject = Nothing
    Dim pGxObject As IGxObject = Nothing
    Dim pGxDataset As IGxRasterDataset
    Dim pRasterLayer As IRasterLayer
    Dim pDataset As IDataset

    Try
        With pGxDialog
            .AllowMultiSelect = False
            .StartingLocation = "C:\Users\rxm160030\Downloads\Lab12\Data"
            .Title = "Select Raster datasets to Add to Map"
            .ObjectFilter = pRasterFilter

        End With
        bSelectedObject = pGxDialog.DoModalOpen(My.ArcMap.Application.hWnd, pGXSelect)
        'If no file selected, exit
        If bSelectedObject = Nothing Then Exit Sub
        pGXSelect.Reset()
        'Get first dataset
        pGxDataset = pGXSelect.Next
        'no dataset selected
        If pGxDataset Is Nothing Then Exit Sub
        'iterate through remaining datasets
        Do Until pGxDataset Is Nothing
            pRasterLayer = New RasterLayer
            Dim pInputRaster As IRaster = pRasterLayer.Raster

            ' Do something with the featureLayer
            Dim choice1 As Double = InputBox("Enter Azimuth")
            Dim choice2 As Double = InputBox("Enter Altitude")

            Dim pSurfaceOp As ISurfaceOp = New RasterSurfaceOp
            Dim pEnv As IRasterAnalysisEnvironment = pSurfaceOp
            Dim pWSF As IWorkspaceFactory = New RasterWorkspaceFactory
            Dim pWS As IWorkspace = pWSF.OpenFromFile("C:\Users\rxm160030\Downloads\Lab12\Data", 0)
            pEnv.OutWorkspace = pWS
            Dim pOutput As IGeoDataset = pSurfaceOp.HillShade(pInputRaster, choice1, choice2, True, 1)

            Dim pOutLayer As IRasterLayer = New RasterLayer
            pOutLayer.CreateFromRaster(pOutput)
            pOutLayer.Name = "HillShade"
            pMap.AddLayer(pOutLayer)
            ' Go to next dataset
            pGxDataset = pGXSelect.Next
        Loop
    Catch ex As Exception
        MsgBox("BtnAdd_Click Exception: " & ex.Message)
    Finally
        ' Release ArcObjects objects
        pGXSelect = Nothing
        pRasterFilter = Nothing
        pGxDialog = Nothing
        pGxDataset = Nothing
        pRasterLayer = Nothing
        ' Call garbage collection
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Sub
End Class

After clicking the add in button I am getting an option to select the Raster Dataset and input the Azimuth and Elevation values. But Later on I am getting an error.enter image description here

  • Change ex.Message in your error handler to ex.ToString(), it will give you the line of code that caused the error and maybe some additional info. – Rich Wawrzonek Apr 18 '17 at 18:27
1

It looks like you are passing an empty raster to the pSurfaceOp.HillShade method. You iterate through the IEnumGxObject, but don't do anything with the pGxDataset. Cast the pGxDataset to the pInputRaster before calling the hillshade operation.

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.