目录
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 结果展示
【打开属性表】--> 【按属性选择】,即可根据指定的属性选择需要的项目
包含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
也包含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为未进行更新的要素
对应ArcMap属性表中的:
IFeatureCursor的帮助文档
如果不做设置,返回所有要素
官方文档:
IFeatureSelection.SelectFeatures方法:
[C#]public void SelectFeatures ( IQueryFilterFilter, esriSelectionResultEnumMethod, booljustOne);
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() + "条记录", "查询结果");
}
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() + "条记录", "查询结果");
}
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();
}
更多精彩,欢迎关注个人微信公众账号“学而立行”