获取图层字段的唯一值集合(ArcEngine)
有时我们需要读取图层中某个字段的所有值的唯一值集合,或做统计用途,或作为其他功能的输入。
一般通过IDataStatistics接口来获取唯一值集合,代码如下:
''' <summary>
''' 通过IDataStatistic接口获取图层指定字段的唯一值
''' </summary>
''' <param name='pFeatureLayer'>目标图层</param>
''' <param name='strField'>目标字段名</param>
''' <returns>目标字段的所有值的唯一值集合</returns>
''' <remarks></remarks>
Public Function GetUVByDataStatisticsFunction GetUVByDataStatisticsByVal pFeatureLayer As IFeatureLayer, ByVal strField As String As IListOf String
Dim uvList As IListOf String = New ListOf String
Dim pQueryFilter As IQueryFilter = New QueryFilter
Dim pFeatureCur As IFeatureCursor
pQueryFilter.SubFields = strField
pFeatureCur = pFeatureLayer.FeatureClass.SearchpQueryFilter, True
Dim pDataStatic As IDataStatistics = New DataStatistics
pDataStatic.Field = strField
pDataStatic.Cursor = pFeatureCur
Dim pEnumvar As System.Collections.IEnumerator = pDataStatic.UniqueValues
pEnumvar.Reset
While pEnumvar.MoveNext
Dim pObj As Object = pEnumvar.Current
uvList.AddpObj.ToString
End While
Return uvList
End Function
但当图层的数据量很大时,以上这种方法执行起来速度较慢,性能影响比较明显,得考虑使用另外一种方法。通过IQueryDef接口使用类似SQL的查询语句来获取图层字段的唯一值集合,执行速度等同于执行SQL查询语句。
Public Function GetUVByQueryDefFunction GetUVByQueryDefByVal pFeatureLayer As IFeatureLayer, ByVal strField As String As IListOf String
Dim uvList As IListOf String = New ListOf String
Dim pQueryDef As IQueryDef
Dim pRow As IRow
Dim pCursor As ICursor
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pDataset As IDataset
pDataset = pFeatureLayer.FeatureClass
pFeatureWorkspace = pDataset.Workspace
pQueryDef = pFeatureWorkspace.CreateQueryDef
With pQueryDef
.Tables = pDataset.Name ' Fully qualified table name
.SubFields = 'DISTINCT' & strField & ''
pCursor = .Evaluate
End With
pRow = pCursor.NextRow
While Not pRow Is Nothing
Dim pObj As Object = pRow.Value0
uvList.AddpObj.ToString
pRow = pCursor.NextRow
End While
Return uvList
End Function