ESRI Code Snippet: Find Layer by Name

A handy code snippet from Neil Clemmons who is a prolific trouble shooter on the ESRI forums.  Sometimes it’s handy to have a bit of VBA code to find a layer by name and this code does the job very well.  Of course you can apply the same logic in any language and application context you see fit.

Public Function GetLayerByFeatureClass(vMap As Variant, sFeatureClassName As String, Optional bMessages As Boolean = True) As ILayer
     Dim pApplication As IApplication
     Dim pMxDocument As IMxDocument
     Dim pCompositeLayer As ICompositeLayer
     Dim pMap As IMap
     Dim l As Long
     Dim m As Long
     
     On Error GoTo ErrorHandler
     
     ' Get map reference from vMap parameter.
     If TypeOf vMap Is IMxApplication Then
          Set pApplication = vMap
          Set pMxDocument = pApplication.Document
          Set pMap = pMxDocument.FocusMap
     ElseIf TypeOf vMap Is IMxDocument Then
          Set pMxDocument = vMap
          Set pMap = pMxDocument.FocusMap
     ElseIf TypeOf vMap Is IMap Then
          Set pMap = vMap
     Else
          If bMessages Then MsgBox "vMap parameter does not implement an interface supported by this function.", vbOKOnly, "GetLayerByFeatureClass()"
          
          Exit Function
     End If
     
     ' Process each layer in the map.
     For l = 0 To pMap.LayerCount - 1
          Set GetLayerByFeatureClass = LayerByFeatureClass(pMap.Layer(l), sFeatureClassName, bMessages)
          If Not GetLayerByFeatureClass Is Nothing Then Exit Function
     Next l
     
     ' Give layer not found message.
     If bMessages Then MsgBox "The layer '" & sFeatureClassName & "' could not be found in the map.", vbOKOnly, "GetLayerByFeatureClass()"
Exit Function
ErrorHandler:
     If bMessages Then ErrorMessage "modAOUtilities:GetLayerByFeatureClass()"
End Function

' Written by: Neil Clemmons
' Date Created: 12/26/2002
' Date Modified:
' Description:  Helper function for GetLayerByFeatureClass().  Uses recursion to find layers that may be inside nested
'               group layers.  If this function returns a value other than Nothing, the layer has been found and
'               the search will stop.  Otherwise, the search will continue until all layers have been processed.
Private Function LayerByFeatureClass(pLayer As ILayer, sName As String, bMessages As Boolean) As ILayer
     Dim pDataset As IDataset
     Dim pReturnLayer As ILayer
     Dim pFeatureLayer As IFeatureLayer
     Dim pCompositeLayer As ICompositeLayer
     Dim l As Long
     
     On Error GoTo ErrorHandler
     
     ' If layer is a feature layer, get it's underlying dataset.
     If TypeOf pLayer Is IFeatureLayer Then
          Set pFeatureLayer = pLayer
          Set pDataset = pFeatureLayer.FeatureClass
          
          ' Set return layer and exit if this layer is the layer being sought.
          If UCase$(pDataset.Name) = UCase$(sName) Then
               Set LayerByFeatureClass = pLayer
               Exit Function
          End If
     End If
     
     ' Exit if layer is not a group layer.  Not setting return value will cause search to continue.
     If Not TypeOf pLayer Is IGroupLayer Then Exit Function
     
     ' Process each layer in the group layer.
     Set pCompositeLayer = pLayer
     For l = 0 To pCompositeLayer.Count - 1
          ' Use recursion to traverse nested group layers.
          Set pReturnLayer = LayerByFeatureClass(pCompositeLayer.Layer(l), sName, bMessages)
          
          ' If layer was found, set return value and exit.
          If Not pReturnLayer Is Nothing Then
               Set LayerByFeatureClass = pReturnLayer
               Exit Function
          End If
     Next l
Exit Function
ErrorHandler:
     If bMessages Then ErrorMessage "modAOUtilitiesLayerByName()"
End Function
Advertisements
ESRI Code Snippet: Find Layer by Name

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s