GISer

导航

Layer相关的一些函数总结(VB.Net源码)

本文是一些和layer相关的函数,如获得某一类型的所有layer,根据图层名获得layer,获得某layer所属的grouplayer等。 

  

''' <summary>
  ''' 得到pMap中所有的FeatureLayer图层
  ''' </summary>
  ''' <param name="pMap">IMap对象</param>
  ''' <returns>查询图层列表</returns>
  ''' <remarks></remarks>
  Public Shared Function GetAllFeatureLayer(ByVal pMap As IMap) As IEnumLayer
    If pMap Is Nothing OrElse pMap.LayerCount = 0 Then Return Nothing
    Dim pEnumLayer As IEnumLayer
    Dim pId As New UID
    Try
      pId.Value = "{" & GetType(IFeatureLayer).GUID.ToString & "}" ''查找出所有的IFeatureLayer对象(包括IAnnotationLayer)
      pEnumLayer = pMap.Layers(pId, True)
      Return pEnumLayer
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  
  ''' <summary>
  ''' 获得当前地图中所有某一类型的图层
  ''' </summary>
  ''' <param name="pMap">IMap对象</param>
  ''' <param name="pType">图层类型,形式如:GetType(ILayer)</param>
  ''' <returns>该类型图层的list</returns>
  ''' <remarks></remarks>
  Public Shared Function GetAllLayers(ByVal pMap As IMap, ByVal pType As Type) As List(Of ILayer)
    If pMap Is Nothing OrElse pMap.LayerCount = 0 Then Return Nothing
    Dim pLayers As New List(Of ILayer)
    Dim pEnumLayer As IEnumLayer
    Dim pId As New UID
    Try
      pId.Value = "{" & pType.GUID.ToString & "}" ''查找出所有的IFeatureLayer对象(包括IAnnotationLayer)
      pEnumLayer = pMap.Layers(pId, True)

      Dim pLayer As ILayer = pEnumLayer.Next()
      While Not pLayer Is Nothing
        pLayers.Add(pLayer)
        pLayer = pEnumLayer.Next()
      End While
      Return pLayers
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  ''' <summary>
  ''' 在地图中选择出对应为pFClass的第一个图层
  ''' </summary>
  ''' <param name="pMap">IMap对象</param>
  ''' <param name="pFClass">IFeatureClass对象</param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Shared Function GetLayerByFeatureClass(ByVal pMap As IMap, ByVal pFClass As IFeatureClass) As IFeatureLayer
    If pMap Is Nothing OrElse pFClass Is Nothing Then Return Nothing
    Dim pEnumLayer As IEnumLayer
    Dim pId As New UID
    Try
      pId.Value = "{" & GetType(IFeatureLayer).GUID.ToString & "}" ''查找出所有的IFeatureLayer对象(包括IAnnotationLayer)
      pEnumLayer = pMap.Layers(pId, True)
      Dim pLayer As IFeatureLayer = pEnumLayer.Next()
      While Not pLayer Is Nothing
        If pLayer.FeatureClass Is pFClass Then
          Return pLayer
        End If
        pLayer = pEnumLayer.Next()
      End While
      Return Nothing
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  ''' <summary>
  ''' 查询pLayer所在的IGroupLayer
  ''' </summary>
  ''' <param name="pMap">IMap对象</param>
  ''' <param name="pLayer">ILayer对象</param>
  ''' <returns>IGroupLayer对象</returns>
  ''' <remarks>
  ''' 无或异常返回Nothing
  ''' </remarks>
  Public Shared Function GetGroupLayerByLayer(ByVal pMap As IMap, ByVal pLayer As ILayer) As IGroupLayer
    If pMap Is Nothing OrElse pLayer Is Nothing Then Return Nothing
    Dim pEnumLayer As IEnumLayer
    Dim pId As New UID
    Try
      pId.Value = "{" & GetType(IGroupLayer).GUID.ToString & "}" ''查找出所有的IFeatureLayer对象(包括IAnnotationLayer)
      pEnumLayer = pMap.Layers(pId, True)
      Dim pGLayer As IGroupLayer = pEnumLayer.Next()
      While Not pGLayer Is Nothing
        Dim i As Integer
        For i = 0 To CType(pGLayer, ICompositeLayer).Count - 1
          If CType(pGLayer, ICompositeLayer).Layer(i) Is pLayer Then
            Return pGLayer
          End If
        Next
        pGLayer = pEnumLayer.Next()
      End While
      Return Nothing
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  ''' <summary>
  ''' 查找出pFeature所在的图层
  ''' </summary>
  ''' <param name="pFeature">IFeature对象</param>
  ''' <param name="pMap">IMap对象</param>
  ''' <returns>查询图层</returns>
  ''' <remarks>
  ''' 通过比对IMap中图层的FeatureClass.AliasName和IFeature.Class.AliasName进行判断
  ''' </remarks>
  Public Shared Function SearchLayer(ByVal pFeature As IFeature, ByVal pMap As IMap) As IFeatureLayer
    Try
      If pFeature Is Nothing Or pMap Is Nothing OrElse pMap.LayerCount = 0 Then Return Nothing

      Dim pEnumLayer As IEnumLayer
      Dim pLayer As IFeatureLayer
      Dim pId As New UID

      pId.Value = "{" & GetType(IFeatureLayer).GUID.ToString & "}"
      pEnumLayer = pMap.Layers(pId, True)
      pEnumLayer.Reset()
      pLayer = pEnumLayer.Next
      While pLayer IsNot Nothing
        If pLayer.FeatureClass IsNot Nothing Then
          If pLayer.FeatureClass.AliasName = pFeature.Class.AliasName Then  '当pLayer中没有对象时,pLayer.FeatureClass为空
            Return (pLayer)
          End If
        End If
        pLayer = pEnumLayer.Next
      End While
      Return Nothing
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  ''' <summary>
  ''' 查询Map中的图层名为sLayerName的图层
  ''' </summary>
  ''' <param name="pMap">IMap对象</param>
  ''' <param name="sLayerName">图层名称</param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Shared Function GetLayerByName(ByVal pMap As IMap, ByVal sLayerName As String) As ILayer
    If pMap Is Nothing OrElse pMap.LayerCount = 0 Then Return Nothing

    Try
      Dim pId As New UID
      pId.Value = "{" & GetType(ILayer).GUID.ToString & "}"

      Dim pEnumLayer As IEnumLayer = pMap.Layers(pId, True)
      pEnumLayer.Reset()
      Dim pLayer As ILayer
      pLayer = pEnumLayer.Next
      While pLayer IsNot Nothing
        If pLayer.Name = sLayerName Then
          Exit While
        End If
        pLayer = pEnumLayer.Next
      End While
      Return pLayer
    Catch ex As Exception
      Return Nothing
    End Try
  End Function
  ''' <summary>
  ''' 根据图层名称取得其图层所在编号,如果没有此图层,则返回-1
  ''' </summary>
  ''' <param name="LayerName"></param>
  ''' <param name="pMap"></param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Public Shared Function GetFeatureLayerIndex(ByVal LayerName As String, ByVal pMap As IMap) As Integer
    If pMap Is Nothing OrElse pMap.LayerCount = 0 Then Return -1

    Dim Counter As Long
    Try
      For Counter = 0 To pMap.LayerCount - 1
        If TypeOf pMap.Layer(Counter) Is IFeatureLayer Then
          If pMap.Layer(Counter).Name = LayerName Then
            Return Counter
          End If
        End If
      Next Counter
      Return -1
    Catch ex As Exception
      Return -1
    End Try
  End Function
  
  ''' <summary>
  ''' 根据文件路径创建layer
  ''' </summary>
  ''' <param name="pFilePath">文件全路径</param>
  ''' <returns>返回创建的layer</returns>
  ''' <remarks></remarks>
  Shared Function CreateLayerFromPath(ByVal pFilePath As String) As ILayer
    Try
      'Get the ILayerFactoryHelper interface
      Dim pLayerFactoryHelper As ILayerFactoryHelper
      pLayerFactoryHelper = New LayerFactoryHelper
      'Get the IEnumLayer interface through the ILayerFatcoryHelper interface
      Dim pFileName As IFileName = New FileName
      pFileName.Path = pFilePath
      Dim pEnumLayer As IEnumLayer
      pEnumLayer = pLayerFactoryHelper.CreateLayersFromName(pFileName)
      pEnumLayer.Reset()
      'Get the ILayer interface
      Dim pLayer As ILayer
      pLayer = pEnumLayer.Next
      'Loop through layers
      Dim pReLayer As ILayer = Nothing
      Do While Not pLayer Is Nothing
        'Add the layer to the map
        pReLayer = pLayer
        pLayer = pEnumLayer.Next
      Loop
      Return pReLayer
    Catch ex As Exception
      Return Nothing
    End Try
  End Function

posted on 2010-07-15 13:18  于小栋  阅读(497)  评论(0编辑  收藏  举报