ArcGIS Engine开发之空间查询

ArcGIS Engine开发之空间查询

空间查询功能是通过用户选择的空间几何体以及该几何体与当前地图中要素之间的几何关系进行空间查找,从而得到查询结果的操作。

相关类与接口
空间查询相关的类主要是SpatialFilter类,其实现的接口主要为ISpatialFilter接口。SpatialFilter类是空间关系过滤类,ISpatialFilter接口的成员主要用于返回和修改数据过滤器所使用的空间关系。ISpatialFilter接口同时包含了空间和属性两种查询约束,它继承了IQueryFilter接口。

1.Geometry属性:

设置或获取用来筛选数据的几何体,其值为IGeometry接口类型。

2.GeometryField属性:

获取或设置应用于查询过滤器中几何字段的名称。

3.SpatialRel属性

获取或设置过滤器所要使用的控件关系,其值为esriSpatialRelEnum枚举类型,包括相交esriSpatialRelInersects、覆盖esriSpatialRelOverlaps、跨越esriSpatialRelCrosses等多种空间关系。

实现思路:

通过ISpatialFilter接口定义空间查询条件,其Geometry属性确定用来查询的空间几何体,SpatialRel属性定义查询所使用的空间关系,为esriSpatialRelEnum枚举类型的变量。包括:esriSpatialRelIntersects(空间相交)、esriSpatialRelTouches(空间相接,共享空间边界),esriSpatialRelOverlaps(空间覆盖)、esriSpatialRelCrosses(空间跨越)、esriSpatialRelWithin(空间被包含)、esriSpatialRelContains(空间包含)等。因为ISpatialFilter接口继承与IQueryFilter接口,因此在定义好空间查询条件后,可以使用IQueryFilter接口的查询方法进行空间查询操作。另外,在合并源图层的几何体时,使用ITopologicalOperator接口的Union方法来进行几何体合并操作,该接口是点、线、面等几何体对象所共同实现的接口。

代码:

复制代码
1 public partial class FormQueryBySpatial : DevExpress.XtraEditors.XtraForm
2 {
3 //变量定义
4 private IMap currentMap;//当前MapControl控件的Map对象
5 private IFeatureLayer currentFeatureLayer;//设置临时类变量来使用IFeatureLayer接口的当前图层对象
6 private string currentFileName;//设置临时类变量来存储字段名称
7 ///
8 /// 获得当前MapControl控件中的对象
9 ///
10 public IMap CurrentMap
11 {
12 set
13 {
14 currentMap = value;
15 }
16 }
17 public FormQueryBySpatial()
18 {
19 InitializeComponent();
20 }
21
22 private void FormQueryBySpatial_Load(object sender, EventArgs e)
23 {
24 //清空目标图层列表
25 checkListTargetLayer.Items.Clear();
26 string layerName;//设置临时变量存储图层名称
27 //对Map中的每一个图层进行判断并添加图层名称
28 for (int i = 0; i < currentMap.LayerCount; i++)
29 {
30 //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
31 if (currentMap.get_Layer(i) is GroupLayer)
32 {
33 //使用ICompositeLayer接口进行遍历操作
34 ICompositeLayer compositeLayer = currentMap.get_Layer(i) as ICompositeLayer;
35 for (int j = 0; j < compositeLayer.Count; j++)
36 {
37 //将图层的名称添加到checkListTargetLayer控件中
38 layerName = compositeLayer.get_Layer(j).Name;
39 checkListTargetLayer.Items.Add(layerName);
40 comBoxSourceLayer.Items.Add(layerName);
41 }
42 }
43 //如果图层不是图层组类型,则直接添加名称
44 else
45 {
46 layerName = currentMap.get_Layer(i).Name;
47 checkListTargetLayer.Items.Add(layerName);
48 comBoxSourceLayer.Items.Add(layerName);
49 }
50 }
51 //将comboxSourceLayer控件的默认选项设置为第一个图层的名称
52 comBoxSourceLayer.SelectedIndex = 0;
53 //将ComBoxMethod控件的默认选项设置为第一种控件选择方法
54 comBoxMethod.SelectedIndex = 0;
55 }
56
57
58 //定义方法获取要素图层
59 private IFeatureLayer GetFeatureLayerByName(IMap map, string layerName)
60 {
61 //对地图图层进行遍历
62 for (int i = 0; i < map.LayerCount; i++)
63 {
64 //如果该图层为图层组类型,则分别对包含的每个图层进行操作
65 if (map.get_Layer(i) is GroupLayer)
66 {
67 //使用ICompositeLayer接口进行遍历操作
68 ICompositeLayer compositeLayer = map.get_Layer(i) as ICompositeLayer;
69 for (int j = 0; j < compositeLayer.Count; j++)
70 {
71 //如果图层名称为所要查询的图层名称,则返回IFeaturelayer接口的矢量图层对象
72 if (compositeLayer.get_Layer(j).Name == layerName)
73 {
74 return (IFeatureLayer)compositeLayer.get_Layer(j);
75 }
76 }
77 }
78 //如果图层不是图层组类型,则直接进行判断
79 else
80 {
81 if (map.get_Layer(i).Name == layerName)
82 {
83 return (IFeatureLayer)map.get_Layer(i);
84 }
85 }
86 }
87 return null;
88 }
89 //定义方法获取几何对象
90 private IGeometry GetFeatureLayerGeometryUnion(IFeatureLayer featureLayer)
91 {
92 //定义IGeometry接口对象,存储每一步拓扑操作后得到的几何体
93 IGeometry geometry = null;
94 //使用ITopologicalOperator接口进行几何体的拓扑操作
95 ITopologicalOperator topologicalOperator;
96 //使用null作为查询过滤器得到图层中所有要素的游标
97 IFeatureCursor featureCursor = featureLayer.Search(null, false);
98 //获取IFeature接口游标中的第一个元素
99 IFeature feature = featureCursor.NextFeature();
100 //当游标不为空时
101 while (feature != null)
102 {
103 //如果几何体不为空
104 if (geometry != null)
105 {
106 //进行接口转换,使当前接口进行拓扑操作
107 topologicalOperator = geometry as ITopologicalOperator;
108 //执行拓扑合并操作,将当前要素的几何体与已有的几何体进行Union,返回新的合并后的几何体
109 geometry = topologicalOperator.Union(feature.Shape);
110 }
111 else
112 geometry = feature.Shape;
113 feature = featureCursor.NextFeature();//移动游标到下一个要素
114 }
115 //返回最新合并的几何体
116 return geometry;
117 }
118 //定义指定查询的方法
119 private void SelectFeaturesBySpatial()
120 {
121 //定义和创建用于查询的ISpatialFilter接口对象
122 ISpatialFilter spatialFilter = new SpatialFilterClass();
123 //默认设定用于查询的空间几何体为当前地图源图层中所有要素几何体的集合
124 spatialFilter.Geometry = GetFeatureLayerGeometryUnion(GetFeatureLayerByName(currentMap, comBoxSourceLayer.SelectedItem.ToString()));
125 //根据对空间选择方法的选择采用相应的空间选择方法
126 switch (comBoxMethod.SelectedIndex)
127 {
128 case 0:
129 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
130 break;
131 case 1:
132 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin;
133 break;
134 case 2:
135 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
136 break;
137 case 3:
138 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelTouches;
139 break;
140 case 4:
141 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
142 break;
143 case 5:
144 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects;
145 break;
146 case 6:
147 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelOverlaps;
148 break;
149 default:
150 spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
151 break;
152 }
153 //对选择的目标图层进行遍历,并对每一个图层进行空间查询,查询结果将放在选择集中
154 IFeatureLayer featureLayer;
155 //对所有选择的目标图层进行遍历
156 for (int i = 0; i < checkListTargetLayer.CheckedItems.Count; i++)
157 {
158 //根据选择的目标图层名称获得对应的矢量图层
159 featureLayer = GetFeatureLayerByName(currentMap, (string)checkListTargetLayer.CheckedItems[i]);
160 //进行接口转换,使用IFeatureSelection接口选择要素
161 IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
162 //使用IFeatureSelection接口的SelectFeature方法,根据空间查询过滤器选择要素,将其放在新的选择集中
163 featureSelection.SelectFeatures((IQueryFilter)spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
164 }
165 //进行接口转换,使用IActiveView接口进行视图操作
166 IActiveView activeView = currentMap as IActiveView;
167 //进行部分刷新操作,只刷新选择集的内容
168 activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
169 }
170
171 private void btnOk_Click(object sender, EventArgs e)
172 {
173 try
174 {
175 SelectFeaturesBySpatial();
176 this.Close();
177 }
178 catch { }
179 }
180
181 private void btnApply_Click(object sender, EventArgs e)
182 {
183 try
184 {
185 SelectFeaturesBySpatial();
186 }
187 catch { }
188 }
189
190 private void btnClose_Click(object sender, EventArgs e)
191 {
192 this.Close();
193 }

原文:https://www.cnblogs.com/dongteng/p/5931525.html

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值