IMap.FeatureSelection方法獲取Feature的屬性信息

近日在通過 IMap.FeatureSelection 獲取了地圖的選擇集要素之後,發現獲取的 IFeature 對象屬性信息都是空的。感到很奇怪,仔細看了一下文檔,發現如下的描述:


only the shape field is guaranteed with the selection. This is the default and exists for performance reasons. The IMap::FeatureSelection property is typically used to draw the map selection, not access feature attributes. This is particularly noticeable with shapefiles and coverage but also in geodatabases if the selection is large enough. Use IEnumFeatureSetup::AllFields to set a flag indicating all fields be returned with the selection. If you want to loop through the map selection to perform an operation, it is typically best to access each layer's selection rather than the entire map's selection. See the example for a sample of each.

 

原來出於性能方面的考慮,該接口默認只返回 shape 信息, FeatureSelection 方法主要是用來獲取選中要素進行繪製,不需要訪問屬性信息。可以通過 IEnumFeatureSetup::AllFields 方法來設置允許返回屬性。如果要遍歷地圖中的選擇集,最好是通過 Layer 來獲取選擇集,比這種方法好。


IMap 獲取選擇集屬性信息的方法如下:


// 獲取選擇集

ISelection pSelection = axMapControl1 .Map .FeatureSelection ;

 

// 打開屬性標籤

IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup ;

pEnumFeatureSetup .AllFields = true ;

 

// 讀取屬性

IEnumFeature pEnumFeature = pSelection as IEnumFeature ;

IFeature pFeature = pEnumFeature .Next ();

 

while (pFeature != null )

{

Console .WriteLine (pFeature .get_Value (2));

pFeature = pEnumFeature .Next ();

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章