空间查询包括:属性查询,空间关系查询(附:要素选择集),空间分析包括:缓冲区分析和叠置分析。
图1 属性查询
核心代码:
1 private esriSelectionResultEnum selectmethod = esriSelectionResultEnum.esriSelectionResultNew;/*用来记录处理结果的方法*/
2 private IFeatureSelection pFeatureSelection = null;
3
4 public SelectByAttribute(IMapControl3 mapcontrol) //修改其构造函数,构造函数中添加一个参数MapControl,用于获取MapControl中的数据 5 {
6 InitializeComponent();
7 this.m_mapcontrol = mapcontrol;
8 m_map = mapcontrol.Map;
9 m_activeview = mapcontrol.ActiveView;
10 }
11
12 private void SelectByAttribute_Load(object sender, EventArgs e) 13 {
14 IEnumLayer Layers = GetLayers();
15 Layers.Reset();
16 ILayer Layer = Layers.Next();
17 while (Layer != null)
18 {
19 comboBoxLayers.Items.Add(Layer.Name.ToString());
20 Layer = Layers.Next();
21 }
22 // SetupEvents();
23 }
24
25 private void comboBoxLayers_SelectedIndexChanged(object sender, EventArgs e) 26 {
27 listBoxFields.Items.Clear();
28 listBoxValues.Items.Clear();
29 string strSelectedLayerName = comboBoxLayers.Text;
30 IFeatureLayer pFeatureLayer;
31
32 try
33 {
34 for (int i = 0; i <= m_map.LayerCount - 1; i++)
35 {
36 if (m_map.get_Layer(i).Name == strSelectedLayerName)
37 {
38 if (m_map.get_Layer(i) is IFeatureLayer)
39 {
40 pFeatureLayer = m_map.get_Layer(i) as IFeatureLayer;
41
42 for (int j = 0; j <= pFeatureLayer.FeatureClass.Fields.FieldCount - 1; j++)
43 {
44 listBoxFields.Items.Add(pFeatureLayer.FeatureClass.Fields.get_Field(j).Name);
45 }
46
47 labelDescription2.Text = strSelectedLayerName;
48 }
49 else
50 {
51 MessageBox.Show("该图层不能够进行属性查询!请重新选择");
52 break;
53 }
54 }
55 }
56 }
57 catch (Exception ex)
58 {
59 MessageBox.Show(ex.Message);
60 }
61 }
62
63 // 确定选择方法
64 private void comboBoxMethod_SelectedIndexChanged(object sender, EventArgs e) 65 {
66 switch (comboBoxMethod.SelectedIndex)
67 {
68 case 0: selectmethod = esriSelectionResultEnum.esriSelectionResultNew; break;
69 case 1: selectmethod = esriSelectionResultEnum.esriSelectionResultAdd; break;
70 case 2: selectmethod = esriSelectionResultEnum.esriSelectionResultSubtract; break;
71 case 3: selectmethod = esriSelectionResultEnum.esriSelectionResultAnd; break;
72 }
73 }
74
75 private void buttonOk_Click(object sender, EventArgs e) 76 {
77 if (textBoxWhereClause.Text == string.Empty)
78 {
79 MessageBox.Show("请构造SQL查询语句!");
80 return;
81 }
82 int result = ExceuteAttributeSelect();
83 if (result == -1)
84 {
85 labelResult.Text = "查询出现错误!";
86 return;
87 }
88 labelResult.Text = string.Format("查到{0}个对象", result);
89 }
90
91 private void buttonClear_Click(object sender, EventArgs e) 92 {
93 //清除语句
94 textBoxWhereClause.Clear();
95 //清除已选的要素
96 m_map.ClearSelection();
97 m_activeview.Refresh();
98 labelResult.Text = "";
99 }
100
101 private void SelectByAttribute_FormClosing(object sender, FormClosingEventArgs e)102 {
103 if (pFeatureSelection != null)
104 {
105 System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureSelection);
106 }
107 }
图2 空间关系查询
核心代码:
1 //窗体加载时触发事件,执行本函数
2 private void FormQueryBySpatial_Load(object sender, EventArgs e) 3 {
4 try
5 {
6 //清空目标图层列表
7 checkedListBoxTargetLayers.Items.Clear();
8
9 string layerName; //设置临时变量存储图层名称
10
11 //对Map中的每个图层进行判断并添加图层名称
12 for (int i = 0; i 13 {
14 //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
15 if (currentMap.get_Layer(i) is GroupLayer)
16 {
17 //使用ICompositeLayer接口进行遍历操作
18 ICompositeLayer compositeLayer = currentMap.get_Layer(i) as ICompositeLayer;
19 for (int j = 0; j 20 {
21 //将图层的名称添加到checkedListBoxTargetLayers控件和comboBoxMethods控件中
22 layerName = compositeLayer.get_Layer(j).Name;
23 checkedListBoxTargetLayers.Items.Add(layerName);
24 comboBoxSourceLayer.Items.Add(layerName);
25 }
26 }
27 //如果图层不是图层组类型,则直接添加名称
28 else
29 {
30 layerName = currentMap.get_Layer(i).Name;
31 checkedListBoxTargetLayers.Items.Add(layerName);
32 comboBoxSourceLayer.Items.Add(layerName);
33 }
34 }
35
36 //将comboBoxSourceLayer控件的默认选项设置为第一个图层的名称
37 comboBoxSourceLayer.SelectedIndex = 0;
38 //将comboBoxMethods控件的默认选项设置为第一种空间选择方法
39 comboBoxMethods.SelectedIndex = 0;
40 }
41 catch { }
42 }
43
44 private void buttonClose_Click(object sender, EventArgs e) 45 {
46 this.Close();
47 }
48
49 private void buttonApply_Click(object sender, EventArgs e) 50 {
51 try
52 {
53 SelectFeaturesBySpatial();
54 }
55 catch
56 { }
57 }
58
59 private void buttonOK_Click(object sender, EventArgs e) 60 {
61 try
62 {
63 SelectFeaturesBySpatial();
64 this.Close();
65 }
66 catch
67 { }
68 }
69
70 ///
71 /// 在地图中根据图层名称获得矢量图层。
72 ///
73 /// 当前地图
74