-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSelectByAttrFrm.cs
339 lines (297 loc) · 13 KB
/
SelectByAttrFrm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
namespace EngineApplication
{
public partial class SelectByAttrFrm : DevComponents.DotNetBar.Office2007Form
{
public SelectByAttrFrm(MainForm mainFrm)
{
InitializeComponent();
tempMainFrm = mainFrm;
}
MainForm tempMainFrm;
IMap pMap;
IFeatureLayer pFeatureLayer;
ILayer pLayer;
ILayerFields pLayerFields;
IEnumLayer pEnumLayer;
#region 鼠标单击/双击生成Whereclause的部分
private void listBoxValues_DoubleClick(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " " + listBoxValues.SelectedItem.ToString();
}
private void buttonEqual_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " = ";
}
private void buttonNotEqual_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " <> ";
}
private void buttonBig_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " > ";
}
private void buttonBigEqual_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " >= ";
}
private void buttonSmall_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " < ";
}
private void buttonSmallEqual_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " <= ";
}
private void buttonChars_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = "%";
}
private void buttonChar_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = "_";
}
private void buttonLike_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " Like ";
}
private void buttonAnd_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " And ";
}
private void buttonOr_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " Or ";
}
private void buttonNot_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " Not ";
}
private void buttonIs_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = " Is ";
}
private void buttonBrace_Click(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = "( )";
//让输入的位置恰好处在()里面,就同arcmap的效果一样
textBoxWhereClause.SelectionStart = textBoxWhereClause.Text.Length - 2;
}
#endregion
/// <summary>
/// 窗体初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectByAttrFrm_Load(object sender, EventArgs e)
{
this.pMap = this.tempMainFrm.axMapControl1.Map;
if (this.pMap.LayerCount == 0) return;
this.pEnumLayer = this.pMap.get_Layers(null, true);
if (pEnumLayer == null)
{
return;
}
pEnumLayer.Reset();
for (this.pLayer = pEnumLayer.Next(); this.pLayer != null; this.pLayer = pEnumLayer.Next())
{
if (this.pLayer is IFeatureLayer)
{
this.comboBoxLayers.Items.Add(this.pLayer.Name);
}
}
if (this.comboBoxLayers.Items.Count == 0)
{
MessageBox.Show("没有可供选择的图层!");
this.Close();
return;
}
this.comboBoxLayers.SelectedIndex = 0;
this.comboBoxMethod.SelectedIndex = 0;
}
/// <summary>
/// 选择了别的图层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBoxLayers_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.pEnumLayer == null) return;
IField pField;
int currentFieldType;
this.pEnumLayer.Reset();
this.listBoxFields.Items.Clear();
for (this.pLayer = this.pEnumLayer.Next(); this.pLayer != null; this.pLayer = this.pEnumLayer.Next())
{
if (this.pLayer.Name != this.comboBoxLayers.Text) continue;
this.pLayerFields = this.pLayer as ILayerFields;//强制转化,似乎有什么规则。Java中的东西很像。
for (int i = 0; i < this.pLayerFields.FieldCount; i++)
{
pField = this.pLayerFields.get_Field(i);
currentFieldType = (int)pField.Type;
if (currentFieldType > 5) continue;//不是可以查询的字段类型
this.listBoxFields.Items.Add(pField.Name);
}
break;
}
this.pFeatureLayer = this.pLayer as IFeatureLayer;
this.labelLayer.Text = this.comboBoxLayers.Text;
}
/// <summary>
/// 双击字段名称
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxFields_DoubleClick(object sender, EventArgs e)
{
textBoxWhereClause.SelectedText = listBoxFields.SelectedItem.ToString() + " ";
}
/// <summary>
/// 获得当前字段的唯一值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonGetValue_Click(object sender, EventArgs e)
{
try
{
if (this.listBoxFields.SelectedIndex == -1) return;
string currentFieldName = this.listBoxFields.Text;//当前字段名
string currentLayerName=this.comboBoxLayers.Text;
this.pEnumLayer.Reset();
for (this.pLayer = this.pEnumLayer.Next(); this.pLayer != null; this.pLayer = this.pEnumLayer.Next())
{
if (this.pLayer.Name == currentLayerName) break;
}
this.pLayerFields = this.pLayer as ILayerFields;
IField pField = this.pLayerFields.get_Field(this.pLayerFields.FindField(currentFieldName));
esriFieldType pFieldType = pField.Type;
//对Table中当前字段进行排序,把结果赋给Cursor
ITable pTable = this.pLayer as ITable;
ITableSort pTableSort = new TableSortClass();
pTableSort.Table = pTable;
pTableSort.Fields = currentFieldName;
pTableSort.set_Ascending(currentFieldName, true);
pTableSort.set_CaseSensitive(currentFieldName, true);//这两句的意思我还不懂,照猫画虎写的。
pTableSort.Sort(null);//排序
ICursor pCursor = pTableSort.Rows;
//字段统计
IDataStatistics pDataStatistics = new DataStatisticsClass();
pDataStatistics.Cursor = pCursor;
pDataStatistics.Field = currentFieldName;
System.Collections.IEnumerator pEnumeratorUniqueValues = pDataStatistics.UniqueValues;//唯一值枚举
int uniqueValueCount = pDataStatistics.UniqueValueCount;//唯一值的个数
this.listBoxValues.Items.Clear();
string currentValue = null;
pEnumeratorUniqueValues.Reset();
//别人的答案
if (pFieldType == esriFieldType.esriFieldTypeString)
{
while (pEnumeratorUniqueValues.MoveNext())
{
currentValue = pEnumeratorUniqueValues.Current.ToString();
this.listBoxValues.Items.Add("'" + currentValue + "'");
}
}
else
{
while (pEnumeratorUniqueValues.MoveNext())
{
currentValue = pEnumeratorUniqueValues.Current.ToString();
this.listBoxValues.Items.Add(currentValue);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClear_Click(object sender, EventArgs e)
{
textBoxWhereClause.Clear();
}
private void buttonApply_Click(object sender, EventArgs e)
{
if (textBoxWhereClause.Text == "")
{
MessageBox.Show("请生成查询语句!");
return;
}
try
{
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = textBoxWhereClause.Text;
IFeatureSelection pFeatureSelection = this.pFeatureLayer as IFeatureSelection;
// int iSelectedFeaturesCount = pFeatureSelection.SelectionSet.Count;
esriSelectionResultEnum selectMethod;
switch (comboBoxMethod.SelectedIndex)
{
case 0: selectMethod = esriSelectionResultEnum.esriSelectionResultNew; break;
case 1: selectMethod = esriSelectionResultEnum.esriSelectionResultAdd; break;
case 2: selectMethod = esriSelectionResultEnum.esriSelectionResultSubtract; break;
case 3: selectMethod = esriSelectionResultEnum.esriSelectionResultAnd; break;
default: selectMethod = esriSelectionResultEnum.esriSelectionResultNew; break;
}
pFeatureSelection.SelectFeatures(pQueryFilter, selectMethod, false);//执行查询
//如果本次查询后,查询的结果数目没有改变,则认为本次查询没有产生新的结果
if (pFeatureSelection.SelectionSet.Count == 0 )//|| pFeatureSelection.SelectionSet.Count == 0)
{
MessageBox.Show("没有符合本次查询条件的结果!");
return;
}
////如果复选框被选中,则定位到选择结果
//if (checkBoxZoomtoSelected.Checked == true)
//{
// IEnumFeature pEnumFeature = MainAxMapControl.Map.FeatureSelection as IEnumFeature;
// IFeature pFeature = pEnumFeature.Next();
// IEnvelope pEnvelope = new EnvelopeClass();
// while (pFeature != null)
// {
// pEnvelope.Union(pFeature.Extent);
// pFeature = pEnumFeature.Next();
// }
// MainAxMapControl.ActiveView.Extent = pEnvelope;
// MainAxMapControl.ActiveView.Refresh();//如果不这样刷新,只要查询前地图已经被放大所效果的话,定位后
// //底图没有刷新,选择集倒是定位和刷新了
//}
//else MainAxMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
this.tempMainFrm.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
//double i = MainAxMapControl.Map.SelectionCount;
//i = Math.Round(i, 0);//小数点后指定为0位数字
//pMainform.toolStripStatusLabel1.Text = "当前共有" + i.ToString() + "个查询结果";
}
catch (Exception ex)
{
MessageBox.Show("您的查询语句可能有误,请检查 | " + ex.Message);
return;
}
}
private void buttonOk_Click(object sender, EventArgs e)
{
this.buttonAnd_Click(sender, e);
this.Dispose();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void listBoxFields_SelectedIndexChanged(object sender, EventArgs e)
{
this.listBoxValues.Items.Clear();
}
}
}