kevinbg07

Add Layer By Selected Features

Discussion created by kevinbg07 on Aug 17, 2010
Latest reply on Aug 18, 2010 by kevinbg07
I was wondering how you would add a layer (preferably a raster layer) based off of a selection in a map layer (in this case a shapefile). I ask because we have a grid that that lists the names of the individual .tif aerial images and I would like to add the photos based off of a selection in the grid. I have something that worked in VBA, but I haven't had much trying to bend it to work in VB.NET. I'm assuming that it would work as an ArcMap command and the code is supposed to be entered in the OnClick() Subroutine, but, I have fairly limited experience with VB.NET, so I might be wrong here.
Anyway, I've posted what I have below. Thanks











    Public Overrides Sub OnClick()
        MyBase.OnClick()

        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        Dim pFeatureLayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pFeatureCursor As IFeatureCursor
        Dim pSelectedFeature As IFeature
        Dim pSelectionSet As ISelectionSet

        pMxDoc = CType(m_application.Document, IMxDocument)
        pMap = pMxDoc.FocusMap
        pFeatureLayer = pMap.Layer(0) ' Layer index of the shape file
        pFeatureSelection = pFeatureLayer
        pSelectionSet = pFeatureSelection.SelectionSet

        pFeatureCursor = pFeatureLayer.Search(pSelectionSet, 0)
        pSelectionSet.Search(Nothing, False, pFeatureCursor)
        pSelectedFeature = pFeatureCursor.NextFeature

        'I assume that you have single feature selected
        If Not pSelectedFeature Is Nothing Then
            Dim strFullPath As String
            strFullPath = pSelectedFeature.Value(pSelectedFeature.Fields.FindField("TILE_NAME"))  ' You can specify the field name from where you accessing the path

            Dim strPath As String
            Dim strFilename As String
            Dim strArray() As String
            Dim intI As Integer

            strArray = Split(strFullPath, "\")
            strFilename = strArray(UBound(strArray))
            strPath = Mid(strFullPath, 1, Len(strFullPath) - Len(strFilename))

            Dim pWorkspaceFactory As IWorkspaceFactory
            pWorkspaceFactory = New RasterWorkspaceFactory

            Dim pRasterWorkspace As IRasterWorkspace
            pRasterWorkspace = pWorkspaceFactory.OpenFromFile(strPath, 0)

            Dim pRasterDataset As IRasterDataset
            pRasterDataset = pRasterWorkspace.OpenRasterDataset(strFilename)

            Dim pRasterLayer As IRasterLayer
            pRasterLayer = New RasterLayer
            pRasterLayer.CreateFromDataset(pRasterDataset)

            pMap.AddLayer(pRasterLayer)

            pMxDoc.UpdateContents()
            pMxDoc.ActiveView.Refresh()
        End If

    End Sub
End Class

Outcomes