-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProportionalSymbols.cs
302 lines (271 loc) · 11.4 KB
/
ProportionalSymbols.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
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 ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DisplayUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace WindowsFormsApplication1
{
public partial class ProportionalSymbols : Form
{
IHookHelper m_hookHelper = null;
IActiveView m_activeView = null;
IMap m_map = null;
IFeatureLayer layer2Symbolize = null;
string strRendererField = string.Empty;
string strNormalizeField = string.Empty;
// IStyleGallery pStyleGlry = new StyleGallery();
IMarkerSymbol markerSymbol = null;
IFillSymbol fillSymbol = null;
// IColor gColor = null;
double minSize = 12;
int legendCount = 5;
public ProportionalSymbols(IHookHelper hookHelper)
{
InitializeComponent();
m_hookHelper = hookHelper;
m_activeView = m_hookHelper.ActiveView;
m_map = m_hookHelper.FocusMap;
}
private void ProportionalSymbols_Load(object sender, EventArgs e)
{
CbxLayersAddItems();
}
private void cbxLayers2Symbolize_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxLayers2Symbolize.SelectedItem != null)
{
string strLayer2Symbolize = cbxLayers2Symbolize.SelectedItem.ToString();
layer2Symbolize = GetFeatureLayer(strLayer2Symbolize);
CbxFieldAdditems(layer2Symbolize);
strRendererField = cbxFields.Items[0].ToString();
}
}
private void CbxFieldAdditems(IFeatureLayer featureLayer)
{
IFields fields = featureLayer.FeatureClass.Fields;
cbxFields.Items.Clear();
cbxNormalization.Items.Clear();
cbxNormalization.Items.Add("None");
for (int i = 0; i < fields.FieldCount; i++)
{
if ((fields.get_Field(i).Type == esriFieldType.esriFieldTypeDouble) ||
(fields.get_Field(i).Type == esriFieldType.esriFieldTypeInteger) ||
(fields.get_Field(i).Type == esriFieldType.esriFieldTypeSingle) ||
(fields.get_Field(i).Type == esriFieldType.esriFieldTypeSmallInteger))
{
cbxFields.Items.Add(fields.get_Field(i).Name);
cbxNormalization.Items.Add(fields.get_Field(i).Name);
}
}
cbxFields.SelectedIndex = 0;
cbxNormalization.SelectedIndex = 0;
}
private void CbxLayersAddItems()
{
if (GetLayers() == null) return;
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer = layers.Next();
while (layer != null)
{
if (layer is IFeatureLayer)
{
cbxLayers2Symbolize.Items.Add(layer.Name);
}
layer = layers.Next();
}
}
private void btnSymbolize_Click(object sender, EventArgs e)
{
if (layer2Symbolize == null) return;
Renderer();
}
private IProportionalSymbolRenderer CreateRenderer()
{
IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)layer2Symbolize;
ITable pTable = (ITable)pGeoFeatureLayer;
ICursor pCursor = pTable.Search(null, false);
//Use the statistics objects to calculate the max value and the min value
IDataStatistics pDataStatistics = new DataStatisticsClass();
pDataStatistics.Cursor = pCursor;
//Set statistical field
pDataStatistics.Field = strRendererField;
//Get the result of statistics
IStatisticsResults pStatisticsResult = pDataStatistics.Statistics;
if (pStatisticsResult == null) return null;
if (markerSymbol == null)
{
MessageBox.Show("请先选择点符号...", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
}
if (fillSymbol == null)
{
MessageBox.Show("请先选择背景...", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
}
markerSymbol.Size = minSize;
// Create a new proportional symbol renderer to draw pop1990
IProportionalSymbolRenderer pProportionalSymbolR = new ProportionalSymbolRendererClass();
pProportionalSymbolR.Field = strRendererField;
if (strNormalizeField.ToLower() != "none")
pProportionalSymbolR.NormField = strNormalizeField;
pProportionalSymbolR.MinDataValue = pStatisticsResult.Minimum;
pProportionalSymbolR.MaxDataValue = pStatisticsResult.Maximum;
pProportionalSymbolR.BackgroundSymbol = fillSymbol;
pProportionalSymbolR.MinSymbol = (ISymbol)markerSymbol;
pProportionalSymbolR.LegendSymbolCount = legendCount;
pProportionalSymbolR.CreateLegendSymbols();
return pProportionalSymbolR;
}
private void Renderer()
{
IGeoFeatureLayer pGeoFeatureL = (IGeoFeatureLayer)layer2Symbolize;
IFeatureClass featureClass = pGeoFeatureL.FeatureClass;
//找出rendererField在字段中的编号
int lfieldNumber = featureClass.FindField(strRendererField);
if (lfieldNumber == -1)
{
MessageBox.Show("Can't find field called " + strRendererField);
return;
}
IProportionalSymbolRenderer renderer = CreateRenderer();
if (renderer == null) return;
pGeoFeatureL.Renderer = (IFeatureRenderer)renderer;
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
#region "GetLayers"
private IEnumLayer GetLayers()
{
UID uid = new UIDClass();
uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";// IFeatureLayer
//uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; // IGeoFeatureLayer
//uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}"; // IDataLayer
if (m_map.LayerCount != 0)
{
IEnumLayer layers = m_map.get_Layers(uid, true);
return layers;
}
return null;
}
#endregion
#region "GetFeatureLayer"
private IFeatureLayer GetFeatureLayer(string layerName)
{
//get the layers from the maps
if (GetLayers() == null) return null;
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer = null;
while ((layer = layers.Next()) != null)
{
if (layer.Name == layerName)
return layer as IFeatureLayer;
}
return null;
}
#endregion
private void cbxFields_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxFields.SelectedItem != null)
{
strRendererField = cbxFields.SelectedItem.ToString();
}
}
private void cbxNormalization_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxNormalization.SelectedItem != null)
{
strNormalizeField = cbxNormalization.SelectedItem.ToString();
}
}
private void btnSelectBackColor_Click(object sender, EventArgs e)
{
GetSymbolByControl fillSymbolForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassFillSymbols);
fillSymbolForm.ShowDialog();
if (fillSymbolForm.m_styleGalleryItem == null) return;
fillSymbol = fillSymbolForm.m_styleGalleryItem.Item as IFillSymbol;
fillSymbolForm.Dispose();
}
private void btnSelectSymbol_Click(object sender, EventArgs e)
{
GetSymbolByControl markerSymbolForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassMarkerSymbols);
markerSymbolForm.ShowDialog();
if (markerSymbolForm.m_styleGalleryItem == null) return;
markerSymbol = markerSymbolForm.m_styleGalleryItem.Item as IMarkerSymbol;
markerSymbolForm.Dispose();
}
private ISymbol GetSymbolBySymbolSelector(esriGeometryType geometryType)
{
try
{
ISymbolSelector pSymbolSelector = new SymbolSelectorClass();
ISymbol symbol = null;
switch (geometryType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
symbol = new SimpleMarkerSymbolClass();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
symbol = new SimpleLineSymbolClass();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
symbol = new SimpleFillSymbolClass();
break;
default:
break;
}
pSymbolSelector.AddSymbol(symbol);
bool response = pSymbolSelector.SelectSymbol(0);
if (response)
{
symbol = pSymbolSelector.GetSymbolAt(0);
return symbol;
}
return null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ISymbol symbol = null;
switch (geometryType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
symbol = new SimpleMarkerSymbolClass();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
symbol = new SimpleLineSymbolClass();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
symbol = new SimpleFillSymbolClass();
break;
default:
break;
}
return symbol;
}
}
private void nudLegendCount_ValueChanged(object sender, EventArgs e)
{
legendCount = Convert.ToInt32(nudLegendCount.Value);
}
private void nudMinsize_ValueChanged(object sender, EventArgs e)
{
minSize = Convert.ToDouble(nudMinsize.Value);
}
}
}