-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBufferAnalysisForm.cs
More file actions
344 lines (254 loc) · 9.91 KB
/
Copy pathBufferAnalysisForm.cs
File metadata and controls
344 lines (254 loc) · 9.91 KB
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
340
341
342
343
344
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
namespace ArcEngine
{
public delegate void AddLayer(string s);
public partial class BufferAnalysisForm : Form
{
private IHookHelper _hookHelper;
private AddLayer _addLayer;
private const uint WmVscroll = 0x0115;
private const uint SbBottom = 7;
[DllImport("user32.dll")]
private static extern int PostMessage(
IntPtr wnd,
uint msg,
IntPtr wParam,
IntPtr lParam
);
public BufferAnalysisForm(IHookHelper hookHelper, AddLayer addLayer)
{
InitializeComponent();
_hookHelper = hookHelper;
_addLayer = addLayer;
}
private void BufferAnalysisForm_Load(object sender, EventArgs e)
{
if (null == _hookHelper || null == _hookHelper.Hook || 0 == _hookHelper.FocusMap.LayerCount)
return;
//load all the feature layers in the map to the layers combo
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer;
while ((layer = layers.Next()) != null)
{
inputComBox.Items.Add(layer.Name);
}
//select the first layer
if (inputComBox.Items.Count > 0)
inputComBox.SelectedIndex = 0;
string tempDir = System.IO.Path.GetTempPath();
outputPath.Text = System.IO.Path.Combine(tempDir, ((string) inputComBox.SelectedItem + "_buffer.shp"));
//set the default units of the buffer
esriUnits mapUnit = _hookHelper.FocusMap.MapUnits;
switch (mapUnit)
{
case esriUnits.esriMeters:
unitsComBox.SelectedIndex = 0;
break;
case esriUnits.esriKilometers:
unitsComBox.SelectedIndex = 1;
break;
case esriUnits.esriMiles:
unitsComBox.SelectedIndex = 2;
break;
default:
Show(String.Format("未知单位 : {0} ", mapUnit));
break;
}
}
// 执行缓冲区
private void 确定_Click(object sender, EventArgs e)
{
// 线性单位缓冲区
String unit;
if (linearUnitButton.Checked)
{
// 确定选择单位
unit = unitsComBox.Text;
//make sure that all parameters are okay
double bufferDistance;
double.TryParse(bufferDistanceBox.Text, out bufferDistance);
if (bufferDistance < 0)
{
MessageBox.Show("无效的缓冲距离!");
return;
}
switch (unit)
{
case "米":
unit = "Meters";
break;
case "千米":
unit = "Kilometers";
break;
case "英里":
unit = "Miles";
break;
}
Buffer(Convert.ToString(bufferDistance, CultureInfo.InvariantCulture) + " " + unit);
}
// 字段缓存区
else if (propertyButton.Checked)
{
unit = propertyBox.Text;
Buffer(unit);
}
DialogResult dialogResult =
MessageBox.Show("是否添加图层?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dialogResult == DialogResult.OK)
{
_addLayer(outputPath.Text);
}
}
private void Buffer(string unit)
{
//修改当前指针样式
Cursor = Cursors.WaitCursor;
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(outputPath.Text)) ||
".shp" != System.IO.Path.GetExtension(outputPath.Text))
{
MessageBox.Show("无效的文件名!");
return;
}
if (_hookHelper.FocusMap.LayerCount == 0)
return;
//get the layer from the map
IFeatureLayer layer = GetFeatureLayer((string) inputComBox.SelectedItem);
if (null == layer)
{
txtMessages.Text += "图层 " + (string) inputComBox.SelectedItem + "未被找到!\r\n";
return;
}
//scroll the textbox to the bottom
ScrollToBottom();
//add message to the messages box
txtMessages.Text += "进行缓冲区的图层: " + layer.Name + "\r\n";
txtMessages.Text += "\r\n正在获取空间数据。这可能需要几秒钟时间...\r\n";
txtMessages.Update();
//get an instance of the geoprocessor
Geoprocessor gp = new Geoprocessor {OverwriteOutput = true};
txtMessages.Text += "正在进行缓冲区分析...\r\n";
txtMessages.Update();
//create a new instance of a buffer tool
ESRI.ArcGIS.AnalysisTools.Buffer buffer = new ESRI.ArcGIS.AnalysisTools.Buffer(
layer, outputPath.Text, unit
);
//execute the buffer tool (very easy :-))
IGeoProcessorResult results = (IGeoProcessorResult) gp.Execute(buffer, null);
if (results.Status != esriJobStatus.esriJobSucceeded)
{
txtMessages.Text += "缓冲区失败的图层: " + layer.Name + "\r\n";
}
txtMessages.Text += ReturnMessages(gp);
//scroll the textbox to the bottom
ScrollToBottom();
txtMessages.Text += "\r\n完成!\r\n";
txtMessages.Text += "------------------------------------------------------\r\n";
//scroll the textbox to the bottom
ScrollToBottom();
//修改当前指针样式
this.Cursor = Cursors.Default;
}
// 取消
private void 取消_Click(object sender, EventArgs e)
{
Close();
}
private IFeatureLayer GetFeatureLayer(string layerName)
{
//get the layers from the maps
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer;
while ((layer = layers.Next()) != null)
{
if (layer.Name == layerName)
return layer as IFeatureLayer;
}
return null;
}
private IEnumLayer GetLayers()
{
UID uid = new UIDClass();
uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
IEnumLayer layers = _hookHelper.FocusMap.get_Layers(uid);
return layers;
}
private string ReturnMessages(Geoprocessor gp)
{
StringBuilder sb = new StringBuilder();
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
System.Diagnostics.Trace.WriteLine(gp.GetMessage(Count));
sb.AppendFormat("{0}\n", gp.GetMessage(Count));
}
}
return sb.ToString();
}
private void ScrollToBottom()
{
PostMessage(txtMessages.Handle, WmVscroll, (IntPtr) SbBottom, IntPtr.Zero);
}
private void Show(string s)
{
MessageBox.Show(s);
}
// 选择储存路径
private void btnOutputLayer_Click(object sender, EventArgs e)
{
//set the output layer
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.CheckPathExists = true;
saveDlg.Filter = "Shapefile (*.shp)|*.shp";
saveDlg.OverwritePrompt = true;
saveDlg.Title = "Output Layer";
saveDlg.RestoreDirectory = true;
saveDlg.FileName = (string) inputComBox.SelectedItem + "_buffer.shp";
DialogResult dr = saveDlg.ShowDialog();
if (dr == DialogResult.OK)
outputPath.Text = saveDlg.FileName;
}
/// <summary>
/// 添加选择图层字段
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void propertyButton_CheckedChanged(object sender, EventArgs e)
{
//get the layer from the map
IFeatureLayer layer = GetFeatureLayer((string) inputComBox.SelectedItem);
IFeatureClass featureClass = layer.FeatureClass;
int count = featureClass.Fields.FieldCount;
for (int i = 0; i < count; i++)
{
propertyBox.Items.Add(featureClass.Fields.Field[i].Name);
}
}
/// <summary>
/// 修改储存路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void inputComBox_SelectedIndexChanged(object sender, EventArgs e)
{
string tempDir = System.IO.Path.GetTempPath();
outputPath.Text = System.IO.Path.Combine(tempDir, ((string) inputComBox.SelectedItem + "_buffer.shp"));
}
private void txtMessages_TextChanged(object sender, EventArgs e)
{
}
}
}