查询分为属性查询和空间查询。我现在做的是属性查询,用IQueryFilter
属性查询的基本步骤是:
1)获取要查询的图层
2)获取要素要素图层
3)获取要素集
4)用IQueryFilter进行过滤筛选
Dim pFeatureSelection As IFeatureSelection = CType(pFeatureLayer, IFeatureSelection) Dim pQueryFilter As IQueryFilter = New QueryFilterClass() pQueryFilter.WhereClause = (TypeComboBox.Text + TextBox1.Text) pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
5)要将筛选出的要素集显示在数据表中,用ICursor和IFeatureCursor对每个要素进行遍历
在这里谈一下个人对ICursor和IFeatureCursor的理解。ICursor主要是针对所有的类型的行进行的操作,而IFeatureCursor则主要是针对地图的要素进行的操作。ICursor是IFeatureCursor的父类。
Dim pCursor As ICursor = Nothing pSelectionSet.Search(Nothing, True, pCursor) '找所有满足查询过滤器的行,返回一个ICursor。这是Nothing,所有返回了所有行。 Dim pFeatureCursor As IFeatureCursor = CType(pCursor, IFeatureCursor) Dim pFeature As IFeature pFeature = pFeatureCursor.NextFeature
6)将各元素依次赋值给datatable,以datatable为数据源传给datagridview
If pSelectionSet IsNot Nothing Then Dim dt As New DataTable Dim dc As DataColumn = Nothing For i = 1 To pFeatureLayer.FeatureClass.Fields.FieldCount dc = New DataColumn(pFeatureLayer.FeatureClass.Fields.Field(i - 1).Name) dt.Columns.Add(dc) Next Dim dr As DataRow While (pFeature IsNot Nothing) Dim pGeometry As IGeometry pGeometry = pFeature.Shape Dim obj As Object = Nothing My.Forms.MainForm.AxMapControl1.FlashShape(pGeometry, 3, 400, obj) dr = dt.NewRow Dim j As Integer For j = 0 To pFeature.Fields.FieldCount - 1 Step 1 If pFeatureLayer.FeatureClass.FindField(pFeatureLayer.FeatureClass.ShapeFieldName) = j Then dr(j) = pFeatureLayer.FeatureClass.ShapeType.ToString() Else dr(j) = pFeature.Value(j).ToString() End If Next dt.Rows.Add(dr) pFeature = pFeatureCursor.NextFeature() End While DataGridView1.DataSource = dt End If