-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerAttributes.cs
469 lines (421 loc) · 17.8 KB
/
LayerAttributes.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
namespace SpatialDataManagement
{
public partial class AttributesContextMenu : Form
{
IFeatureLayer currentLayer = null;
IMap m_map = null;
IActiveView m_activeView = null;
const string m_dataSetName = "m_layerDataSet";
const string m_dataSourceName = "GeoDataSource";
DataSet m_layerDataSet = new DataSet(m_dataSetName);
int currentRow = 0;//鼠标点击位置所在的行
int x, y; //鼠标点击的位置
string strOBJECTID = null;
public AttributesContextMenu(IMap map, IFeatureLayer featureLayer)
{
InitializeComponent();
m_map = map;
m_activeView = map as IActiveView;
currentLayer = featureLayer;
}
private void AttributesContextMenu_Load(object sender, EventArgs e)
{
this.Text = currentLayer.Name + " 属性表";
FindOIDField();
if (strOBJECTID == null) return;
int rowCount = ConstructDataSet(currentLayer);
dataGridView1.DataSource = m_layerDataSet;
dataGridView1.DataMember = currentLayer.Name;
toolStripStatusLabel1.Text = "选择记录数:0";
toolStripStatusLabel2.Text = "总记录数:" + currentLayer.FeatureClass.FeatureCount(null).ToString();
}
private void FindOIDField()
{
if (currentLayer == null) return;
IFields fields = currentLayer.FeatureClass.Fields;
IField field;
for (int i = 0; i < fields.FieldCount; i++)
{
field = fields.get_Field(i);
if (field.Type == esriFieldType.esriFieldTypeOID)
{
strOBJECTID = field.Name;
break;
}
}
}
private void OnActiveViewEventsAfterDraw(IDisplay Display, esriViewDrawPhase phase)
{
if (m_activeView == null) return;
if (phase != esriViewDrawPhase.esriViewGeoSelection) return;
ISelectionSet selectedFeatures = GetSelectedFeature();
UpdateDataGridView(selectedFeatures);
}
private void OnFeatureLayerSelectionChanged()
{
ISelectionSet selectedFeatures = GetSelectedFeature();
UpdateDataGridView(selectedFeatures);
}
private void UpdateDataGridView(ISelectionSet selectedFeatures)
{
//if (selectedFeatures.Count == 0 && dataGridView1.SelectedRows.Count != 0)
// dataGridView1.ClearSelection();
IEnumIDs enumIDs = selectedFeatures.IDs;
int iD = enumIDs.Next();
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == iD.ToString())
dataGridView1.Rows[i].Selected = true;
}
iD = enumIDs.Next();
}
}
private ISelectionSet GetSelectedFeature()
{
if (currentLayer == null) return null;
IFeatureSelection featureSelection = currentLayer as IFeatureSelection;
ISelectionSet selectionSet = featureSelection.SelectionSet;
return selectionSet;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
m_map.ClearSelection();
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, m_activeView.Extent);
DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
if (selectedRows == null) return;
string strOID = string.Empty;
List<string> OIDList = new List<string>();
for (int i = 0; i < selectedRows.Count; i++)
{
DataGridViewRow row = selectedRows[i];
strOID = row.Cells[strOBJECTID].Value.ToString();
//strOID = row.Cells[0].Value.ToString();
OIDList.Add(strOID);
}
SelectFeatures(OIDList);
toolStripStatusLabel1.Text = "选择记录数:" + OIDList.Count.ToString();
toolStripStatusLabel2.Text = "总记录数:" + currentLayer.FeatureClass.FeatureCount(null).ToString();
}
private void SelectFeatures(List<string> oidList)
{
IFeatureClass featureClass = currentLayer.FeatureClass;
string strID = string.Empty;
string[] IDs = oidList.ToArray();
for (int i = 0; i < IDs.Length; i++)
{
strID = IDs[i];
IFeature selectedFeature = featureClass.GetFeature(Convert.ToInt32(strID));
m_map.SelectFeature(currentLayer, selectedFeature);
}
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, m_activeView.Extent);
}
#region "ConstructDataSet"
private int ConstructDataSet(IFeatureLayer pFeatLyr)
{
ILayerFields pFeatlyrFields = pFeatLyr as ILayerFields;
IFeatureClass pFeatCls = pFeatLyr.FeatureClass;
int rows = 0;
if (m_layerDataSet.Tables[pFeatLyr.Name] == null)
{
DataTable pTable = new DataTable(pFeatLyr.Name);
DataColumn pTableCol;
for (int i = 0; i <= pFeatlyrFields.FieldCount - 1; i++)
{
pTableCol = new DataColumn(pFeatlyrFields.get_Field(i).AliasName);
pTable.Columns.Add(pTableCol);
pTableCol = null;
}
IFeatureCursor features = pFeatLyr.Search(null, false);
IFeature feature = features.NextFeature();
while (feature != null)
{
DataRow pTableRow = pTable.NewRow();
for (int i = 0; i <= pFeatlyrFields.FieldCount - 1; i++)
{
//pTableRow[i] = feature.get_Value(i);
if (pFeatlyrFields.FindField(pFeatCls.ShapeFieldName) == i)
{
pTableRow[i] = pFeatCls.ShapeType;
}
else
{
pTableRow[i] = feature.get_Value(i);
}
}
pTable.Rows.Add(pTableRow);
feature = features.NextFeature();
}
rows = pTable.Rows.Count;
m_layerDataSet.Tables.Add(pTable);
System.Runtime.InteropServices.Marshal.ReleaseComObject(features);
}
return rows;
}
#endregion
private void 闪烁该要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
PanToFlash();
}
private void 缩放到该要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
IEnvelope pEnvelope = new EnvelopeClass();
IFeature selectedFeature = GetCurrentFeature();
m_map.SelectFeature(currentLayer, selectedFeature);
if (currentLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
pEnvelope = m_activeView.Extent;
pEnvelope.Height = pEnvelope.Height / 2;
pEnvelope.Width = pEnvelope.Width / 2;
pEnvelope.CenterAt(selectedFeature.ShapeCopy as IPoint);
}
else
{
pEnvelope = selectedFeature.Extent;
}
m_activeView.Extent = pEnvelope;
m_activeView.PartialRefresh((esriViewDrawPhase)6, null, m_activeView.Extent);
}
catch (Exception)
{
return;
}
}
private void 平移到该要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
PanToFlash();
}
private void 缩放到当前图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
m_activeView.Extent = ((IGeoDataset)currentLayer).Extent;
m_activeView.ScreenDisplay.UpdateWindow();
m_activeView.PartialRefresh((esriViewDrawPhase)6, null, m_activeView.Extent);
}
private void 缩放到所有选择要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
IEnvelope wholeEnvelope = GetLayerSelectedFeaturesEnvelope(currentLayer);
if (wholeEnvelope == null) return;
m_activeView.Extent = wholeEnvelope;
//m_activeView.ScreenDisplay.UpdateWindow();
m_activeView.PartialRefresh((esriViewDrawPhase)6, null, m_activeView.Extent);
}
private void 清楚所有选择要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
m_map.ClearSelection();
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
dataGridView1.ClearSelection();
}
private void 删除所有选择要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteSelectedFeatures();
}
private IFeature GetCurrentFeature()
{
try
{
string strID = dataGridView1.Rows[currentRow].Cells[strOBJECTID].Value.ToString();
//string strID = dataGridView1.Rows[currentRow].Cells[0].Value.ToString();
IFeatureClass featureClass = currentLayer.FeatureClass;
IFeature selectedFeature = featureClass.GetFeature(Convert.ToInt32(strID));
return selectedFeature;
}
catch (Exception)
{
return null;
}
}
private void PanToFlash()
{
try
{
IFeature selectedFeature = GetCurrentFeature();
IGeometry geometry = selectedFeature.Shape;
IPoint pCenterPoint = new PointClass();
double x = (geometry.Envelope.LowerLeft.X + geometry.Envelope.UpperRight.X) / 2;
double y = (geometry.Envelope.LowerLeft.Y + geometry.Envelope.UpperRight.Y) / 2;
pCenterPoint.PutCoords(x, y);
IDisplayTransformation pDisplayTransform = m_activeView.ScreenDisplay.DisplayTransformation;
IEnvelope pEnvelope = pDisplayTransform.VisibleBounds;
pEnvelope.CenterAt(pCenterPoint);
pDisplayTransform.VisibleBounds = pEnvelope;
//m_map.SelectFeature(currentLayer, selectedFeature);
m_activeView.PartialRefresh((esriViewDrawPhase)6, null, m_activeView.Extent);
m_activeView.ScreenDisplay.UpdateWindow();
ISymbol symbol = CreateSimpleSsymbol(geometry.GeometryType);
if (symbol == null) return;
DrawSymbol(symbol, geometry);
}
catch (Exception)
{
return;
}
}
private ISymbol CreateSimpleSsymbol(esriGeometryType geometryType)
{
ISymbol symbol = null;
switch (geometryType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
markerSymbol.Color = getRGB(255, 0, 0);
markerSymbol.Size = 8;
symbol = markerSymbol as ISymbol;
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
lineSymbol.Color = getRGB(255, 0, 0);
lineSymbol.Width = 4;
symbol = lineSymbol as ISymbol;
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = getRGB(255, 0, 0);
symbol = fillSymbol as ISymbol;
break;
default:
break;
}
symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
return symbol;
}
private IColor getRGB(int yourRed, int yourGreen, int yourBlue)
{
IRgbColor pRGB;
pRGB = new RgbColorClass();
pRGB.Red = yourRed;
pRGB.Green = yourGreen;
pRGB.Blue = yourBlue;
pRGB.UseWindowsDithering = true;
return pRGB;
}
private void DrawSymbol(ISymbol symbol, IGeometry geometry)
{
IScreenDisplay pDisplay = m_activeView.ScreenDisplay;
pDisplay.StartDrawing(0, (short)esriScreenCache.esriNoScreenCache);
pDisplay.SetSymbol(symbol);
for (int i = 0; i < 10; i++)
{
switch (geometry.GeometryType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
pDisplay.DrawPoint(geometry);
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint:
pDisplay.DrawMultipoint(geometry);
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
pDisplay.DrawPolyline(geometry);
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
pDisplay.DrawPolygon(geometry);
break;
default:
break;
}
System.Threading.Thread.Sleep(100);
}
//m_mapControl.FlashShape(geometry, 5, 300, symbol);
pDisplay.FinishDrawing();
}
private IEnvelope GetSelectedFeaturesExtent()
{
IEnvelope mapEnvelope = new EnvelopeClass();
UID uid = new UIDClass();
uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
//{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer
//{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
//{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
//{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
//{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
//{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
//{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer
IEnumLayer layers = m_map.get_Layers(uid, false);
layers.Reset();
IGeoFeatureLayer geoFeatureLayer = layers.Next() as IGeoFeatureLayer;
int i = 1;
while (geoFeatureLayer != null)
{
IEnvelope layerEnvelope = GetLayerSelectedFeaturesEnvelope(geoFeatureLayer);
if (layerEnvelope == null) goto gotoLabel;
if (i == 1)
mapEnvelope = layerEnvelope;
else
{
mapEnvelope.Union(layerEnvelope);
}
i++;
gotoLabel: geoFeatureLayer = layers.Next() as IGeoFeatureLayer;
}
return mapEnvelope;
}
private IEnvelope GetLayerSelectedFeaturesEnvelope(IFeatureLayer pFeatLyr)
{
IEnvelope layerEnvelope = null;
IFeatureClass pFeatCls = pFeatLyr.FeatureClass;
IFeatureSelection selectLayer = pFeatLyr as IFeatureSelection;
ISelectionSet selectionSet = selectLayer.SelectionSet;
IEnumIDs enumIDs = selectionSet.IDs;
IFeature feature;
int i = 1;
int iD = enumIDs.Next();
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
feature = pFeatCls.GetFeature(iD);
IEnvelope envelope = feature.ShapeCopy.Envelope;
if (i == 1)
layerEnvelope = envelope;
else
{
layerEnvelope.Union(envelope);
}
i++;
iD = enumIDs.Next();
}
return layerEnvelope;
}
private void DeleteSelectedFeatures()
{
IFeatureClass pFeatCls = currentLayer.FeatureClass;
IFeatureSelection selectLayer = currentLayer as IFeatureSelection;
ISelectionSet selectionSet = selectLayer.SelectionSet;
IEnumIDs enumIDs = selectionSet.IDs;
IFeature feature;
int iD = enumIDs.Next();
while (iD != -1) //-1 is reutned after the last valid ID has been reached
{
feature = pFeatCls.GetFeature(iD);
feature.Delete();
iD = enumIDs.Next();
}
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
currentRow = e.RowIndex;
x = e.X;
y = e.Y;
}
}
}
}