使用ArcEngine的Select Features工具,如何获取选中的要素
(2012-02-22 11:43:59)
标签:
杂谈 |
分类: GIS |
(转载)
使用ArcEngine的Select Features工具,如何获取选中的要素
好久没写博客了,最近转型ArcEngine开发了。刚开始做项目就遇到一个难缠的问题。ArcEngine的ToolbarBean提供了很多好用的工具,其中最常用的莫过于查询工具类中的ControlsSelectFeaturesTool了,但是如何获取到他所选中的要素呢?到网上查了两天,发现论坛上问这个问题的人很多,但是有用的回答确很少。于是开始认真的看官网的帮助发现了这样一段代码:
[java] view plaincopyprint?
-
public
void getSelectedFeature() - {
-
-
{ -
IMap map = mapBean.getMap(); -
ISelection selection = map.getFeatureSelection(); -
IEnumFeature enumFeature = (IEnumFeature)selection; -
enumFeature.reset(); -
IFeature feature = enumFeature.next(); -
(feature null)!= -
{ -
System.out.println( ID: " + feature.getOID()); -
feature = enumFeature.next(); -
} -
} -
(Exception e) -
{ -
e.printStackTrace(); -
} - }
很兴奋呀!但是用了之后发现我得到的IFeature对象中确只能拿到ID,其他的字段读取出来全部为空。而且IEnumFeature enumFeature = (IEnumFeature)selection会报类型转换错误,只能用IEnumFeature enumFeature = new IEnumFeatureProxy(selection);于是沉下心认真的读了API中的REMARK,他是这样写的:When this interface is used to access features from a map's feature selection (IMap.FeatureSelection), the IEnumFeatureSetup.AllFields property should be used to return fully hydrated features.也就是说:ArcGIS中FeatureSelection默认的时候只存入Feature 的Shape,而不是整个Feature的字段数据。如果要查看其他数据,必须要进行以下转换才可以:
[c-sharp] view plaincopyprint?
-
IEnumFeatureSetup
iEnumFeatureSetup new= IEnumFeatureSetupProxy(selection); true);iEnumFeatureSetup.setAllFields(
于是才有了现在这几行珍贵的代码,最终解决了问题。
[c-sharp] view plaincopyprint?
-
IMap
map = mapBean.getMap(); -
ISelection selection = map.getFeatureSelection(); -
IEnumFeatureSetup iEnumFeatureSetup = IEnumFeatureSetupProxy(selection); -
iEnumFeatureSetup.setAllFields( -
IEnumFeature enumFeature = IEnumFeatureProxy(iEnumFeatureSetup); -
enumFeature.reset(); -
IFeature feature = enumFeature.next(); -
(feature null)!= -
{ -
System. ID: " + feature.getOID()); -
System. -
feature = enumFeature.next(); -
}
希望以上的信息会对大家有所帮助,也希望各位ArcGIS的大鸟们能够抽出一些时间多回答一下网上的提问,让我们这些菜鸟能够少走一些弯路,通过技术交流,共同进步。
看了您的文章总算把难题解决了,不过不知道是不是版本问题,感觉有些代码语法的冲突,我用的是arcengine
10和vs2010,现在我把我根据您的代码改编语法格式的代码发上,希望后面看到的人有用!
再次的感谢您!
-
public
void getSelectedFeature() -
{ -
-
{ -
IMap map = axMapControl1.Map; -
ISelection selection = map.FeatureSelection; -
IEnumFeatureSetup iEnumFeatureSetup = (IEnumFeatureSetup)selection; -
iEnumFeatureSetup.AllFields = -
IEnumFeature enumFeature = (IEnumFeature)iEnumFeatureSetup; -
enumFeature.Reset(); -
IFeature feature = enumFeature.Next(); -
(feature null)!= -
{ -
hehe = feature.get_Value(5).ToString(); -
MessageBox.Show(hehe); -
feature = enumFeature.Next(); -
} -
} -
(Exception e) -
{ -
} -
}
public void getSelectedFeature() { try { IMap map = axMapControl1.Map; ISelection selection = map.FeatureSelection; IEnumFeatureSetup iEnumFeatureSetup = (IEnumFeatureSetup)selection; iEnumFeatureSetup.AllFields = true; IEnumFeature enumFeature = (IEnumFeature)iEnumFeatureSetup; enumFeature.Reset(); IFeature feature = enumFeature.Next(); while (feature != null) { string hehe = feature.get_Value(5).ToString(); MessageBox.Show(hehe); feature = enumFeature.Next(); } } catch (Exception e) { } }