基于C#的ArcEngine二次开发教程(11):矢量数据属性查询接口介绍及实现源码
目錄
1 使用ArcMap進行矢量數據屬性查詢
示例1:根據Name字段進行查詢
示例2:根據KIND字段進行多條記錄查詢
2 基于AE的矢量數據屬性查詢的實現
2.1 IFeatureLayer
2.2?IFeatureClass接口
2.3?IFeatureCursor接口
2.4?IQueryFilter
2.5?IFeatureSelection
3 源碼實現
3.1 獲取矢量屬性表中的記錄數
3.2 依據指定條件查詢記錄
3.3 將查詢結果在地圖上高亮顯示
4 結果展示
1 使用ArcMap進行矢量數據屬性查詢
【打開屬性表】--> 【按屬性選擇】,即可根據指定的屬性選擇需要的項目
示例1:根據Name字段進行查詢
示例2:根據KIND字段進行多條記錄查詢
2 基于AE的矢量數據屬性查詢的實現
2.1 IFeatureLayer
包含Search方法:
參數為IQueryFilter,返回值為IFeatureCursor:
Remarks
If there is a definition query set on the layer, the Search method will automatically work on the subset of features in the layer that meet the definition criteria. You specify an additional query that will be applied after the layer's definition query by passing valid IQueryFilter object for the QueryFilter parameter.
This Search method will not work on joined fields. If the FeatureLayer has any joins, you should use the IGeoFeatureLayer::SearchDisplayFeatures method instead.
You cannot use the cursor returned by IFeatureLayer::Search to update features, instead use IFeatureClass::Update.
Recycling specifies whether the resulting feature cursor will be recycling or non-recycling. Use True for recycling cursor and False for a non-recycling cursor. See the topic for IFeatureClass::Search for more information.
第二個參數如果為true,只能查詢要素;如果為false,可對返回的要素進行修改
如果設置為true時,可以優化讀取速度;因此在不涉及對數據進行修改的情況下,我們應該將第二個參數設置為true
2.2?IFeatureClass接口
也包含Search方法:
Remarks
Search will return an IFeatureCursor with all of the features that satisfy?the attribute and/or spatial?constraints as specified by?an IQueryFilter reference. If a null value is passed to the filter parameter,?the feature cursor will return?all of the features from the feature class.
The recycling parameter controls row object allocation behavior. Recycling cursors rehydrate a single feature object on each fetch and can be used to optimize read-only access, for example, when drawing. It is illegal to maintain a reference on a feature object returned by a recycling cursor across multiple calls to NextFeature on the cursor.?Features returned by a recycling cursor should not be modified. Non-recycling cursors return a separate feature object on each fetch. The features returned by a non-recycling cursor may be modified and stored with polymorphic behavior.
The Geodatabase guarantees "unique instance semantics" on non-recycling feature objects fetched during an edit session. In other words, if the feature retrieved by a search cursor has already been instantiated and is being referenced by the calling application, then a reference to the existing feature object is returned.
Non-recycling feature cursors returned from the Search method?*MUST* be used when copying features from the cursor into an insert cursor of another class.? This is because a recycling cursor reuses the same geometry and under some circumstances all of the features inserted into the insert cursor may have the same geometry.? Using a non-recycling cursor ensures that each geometry is unique.
Existing rows can be edited?with either a search cursor or an update cursor.?When using a search cursor,?NextRow is?called, returning a reference to a row.?The?row is edited, and?IRow.Store?is called. When using an update cursor, NextRow is called, the?row is edited, and?ICursor.UpdateRow is called with the row as a parameter. It is important to remember, when using an update cursor, do not call the Store method.
In the same way, rows can be deleted using a search cursor or an update cursor, with IRow.Delete used instead of IRow.Store, and ICursor.DeleteRow?used instead of ICursor.UpdateRow.
The recommended approach depends on whether or not the edits are being made in an edit session, if the cursor is being used by ArcMap or by an Engine application, and if the?features being edited are?simple or complex.?The table below shows which approach to use in different situations:
| ? | ArcMap | Engine - Simple | Engine - Complex |
| Inside edit sessions | Search Cursor | Search Cursor | Search Cursor |
| Outside?edit sessions | Search Cursor | Update Cursor | Search Cursor |
A search cursor is always recommended in ArcMap, because the query may be satisfied by the contents of the map cache, making a DBMS query unnecessary.
When working with simple features and edit sessions?in an Engine application, it's recommended that a search cursor be used to take advantage of batched updates in edit operations.?With complex features, on the other hand,?update calls?are overridden by the features' custom behavior, meaning that the feature's store method will be called?even if an update cursor is used.
When using cursors within an edit session,?they should always be scoped to edit operations. In other words, a cursor should be created after an edit operation has begun and should not be used once that edit operation has been stopped or aborted.
IFeatureLayer.Search方法獲取的是更新后的要素,IFeatureClass.Search為未進行更新的要素
2.3?IFeatureCursor接口
對應ArcMap屬性表中的:
IFeatureCursor的幫助文檔
2.4?IQueryFilter
如果不做設置,返回所有要素
官方文檔:
2.5?IFeatureSelection
IFeatureSelection.SelectFeatures方法:
[C#]public void SelectFeatures (IQueryFilterFilter,esriSelectionResultEnumMethod,booljustOne);?
3 源碼實現
3.1 獲取矢量屬性表中的記錄數
private void 記錄查詢ToolStripMenuItem_Click(object sender, EventArgs e){IFeatureLayer myFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;IFeatureClass myFeatureClass = myFeatureLayer.FeatureClass;IFeatureCursor myCursor = myFeatureClass.Search(null, true);IFeature fea = myCursor.NextFeature();int cnt = 0;while (fea != null){cnt++;fea = myCursor.NextFeature();}MessageBox.Show("查詢到" + cnt.ToString() + "條記錄", "查詢結果");}3.2 依據指定條件查詢記錄
private void 條件查詢ToolStripMenuItem_Click(object sender, EventArgs e){IFeatureLayer myFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;IFeatureClass myFeatureClass = myFeatureLayer.FeatureClass;IQueryFilter myQuery = new QueryFilterClass();myQuery.WhereClause = "KIND = '1086'";IFeatureCursor myCursor = myFeatureClass.Search(myQuery, true);IFeature fea = myCursor.NextFeature();int cnt = 0;while (fea != null){cnt++;fea = myCursor.NextFeature();}MessageBox.Show("查詢到" + cnt.ToString() + "條記錄", "查詢結果");}3.3 將查詢結果在地圖上高亮顯示
private void 高亮顯示ToolStripMenuItem_Click(object sender, EventArgs e){IFeatureLayer myFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;IFeatureSelection myFeaSelect = myFeatureLayer as IFeatureSelection;//接口跳轉IQueryFilter myQuery = new QueryFilterClass();myQuery.WhereClause = "KIND = '1380'";myFeaSelect.SelectFeatures(myQuery, esriSelectionResultEnum.esriSelectionResultAdd, true);ISelectionSet set = myFeaSelect.SelectionSet;MessageBox.Show(set.Count.ToString());axMapControl1.Refresh();}4 結果展示
?
更多精彩,歡迎關注個人微信公眾賬號“學而立行”
?
總結
以上是生活随笔為你收集整理的基于C#的ArcEngine二次开发教程(11):矢量数据属性查询接口介绍及实现源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sping注解
- 下一篇: C#_串口调试助手-保存TextBox数