-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraduatedColorSymbols.cs
314 lines (280 loc) · 12 KB
/
GraduatedColorSymbols.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
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.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
namespace WindowsFormsApplication1
{
public partial class GraduatedColorSymbols : Form
{
IHookHelper m_hookHelper = null;
IActiveView m_activeView = null;
IMap m_map = null;
IFeatureLayer layer2Symbolize = null;
string strRendererField = string.Empty;
string strNormalizeField = string.Empty;
int gClassCount = 5;
double[] gClassbreaks = null;
string strClassifyMethod = "自然裂点分类";
IClassBreaksRenderer m_classBreaksRenderer = null;
IStyleGallery pStyleGlry = new ServerStyleGalleryClass();
IColorRamp colorRamp = null;
public GraduatedColorSymbols(IHookHelper hookHelper)
{
InitializeComponent();
m_hookHelper = hookHelper;
m_activeView = m_hookHelper.ActiveView;
m_map = m_hookHelper.FocusMap;
}
private void GraduatedColorSymbols_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 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;
}
m_classBreaksRenderer = CreateClassBreaksRenderer(featureClass);
if (m_classBreaksRenderer == null) return;
pGeoFeatureL.Renderer = (IFeatureRenderer)m_classBreaksRenderer;
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
}
private IClassBreaksRenderer CreateClassBreaksRenderer(IFeatureClass featureClass)
{
if (colorRamp == null)
{
MessageBox.Show("请先选择色带!!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
}
classify();
int ClassesCount = gClassbreaks.GetUpperBound(0);
if (ClassesCount == 0) return null;
nudClassCount.Value = ClassesCount;
IClassBreaksRenderer pClassBreaksRenderer = new ClassBreaksRendererClass();
pClassBreaksRenderer.Field = strRendererField;
if (strNormalizeField.ToLower() != "none")
pClassBreaksRenderer.NormField = strNormalizeField;
//设置着色对象的分级数目
pClassBreaksRenderer.BreakCount = ClassesCount;
pClassBreaksRenderer.SortClassesAscending = true;
//通过色带设置各级分类符号的颜色
colorRamp.Size = ClassesCount;
bool createRamp;
colorRamp.CreateRamp(out createRamp);
IEnumColors enumColors = colorRamp.Colors;
enumColors.Reset();
IColor pColor = null;
ISymbol symbol = null;
//需要注意的是分级着色对象中的symbol和break的下标都是从0开始
for (int i = 0; i < ClassesCount; i++)
{
pColor = enumColors.Next();
switch (featureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
simpleMarkerSymbol.Color = pColor;
symbol = simpleMarkerSymbol as ISymbol;
break;
case esriGeometryType.esriGeometryPolyline:
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
simpleLineSymbol.Color = pColor;
symbol = simpleLineSymbol as ISymbol;
break;
case esriGeometryType.esriGeometryPolygon:
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Color = pColor;
symbol = simpleFillSymbol as ISymbol;
break;
default:
break;
}
pClassBreaksRenderer.set_Symbol(i, symbol);
pClassBreaksRenderer.set_Break(i, gClassbreaks[i + 1]);
}
return pClassBreaksRenderer;
}
private void btnSelectColorRamp_Click(object sender, EventArgs e)
{
//GetSymbolByControl colorRampForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassColorRamps);
//colorRampForm.ShowDialog(); //08.04.09
//if (colorRampForm.m_styleGalleryItem != null)
// colorRamp = colorRampForm.m_styleGalleryItem.Item as IColorRamp;
//colorRampForm.Dispose();
ColorRampForm colorRampform = new ColorRampForm();
colorRampform.ShowDialog();
colorRamp = colorRampform.m_styleGalleryItem.Item as IColorRamp;
colorRampform.Dispose();
}
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 nudClassCount_ValueChanged(object sender, EventArgs e)
{
gClassCount = Convert.ToInt32(nudClassCount.Value);
}
private void classifyCBX_SelectedIndexChanged(object sender, EventArgs e)
{
if (classifyCBX.SelectedItem != null)
{
strClassifyMethod = classifyCBX.SelectedItem.ToString();
}
}
private void classify()
{
if (layer2Symbolize == null) return;
IFeatureClass featureClass = layer2Symbolize.FeatureClass;
ITable pTable = (ITable)featureClass;
ITableHistogram pTableHistogram = new BasicTableHistogramClass();
IBasicHistogram pHistogram = (IBasicHistogram)pTableHistogram;
pTableHistogram.Field = strRendererField;
if (strNormalizeField.ToLower() != "none")
pTableHistogram.NormField = strNormalizeField;
pTableHistogram.Table = pTable;
object dataFrequency;
object dataValues;
pHistogram.GetHistogram(out dataValues, out dataFrequency);
//下面是分级方法,用于根据获得的值计算得出符合要求的数据
//根据条件计算出Classes和ClassesCount,numDesiredClasses为预定的分级数目
IClassifyGEN pClassify = new NaturalBreaksClass();
switch (strClassifyMethod)
{
case "等间隔分类":
pClassify = new EqualIntervalClass();
break;
//case "预定义间隔分类":
// pClassify = new DefinedIntervalClass();
// break;
case "分位数分类":
pClassify = new QuantileClass();
break;
case "自然裂点分类":
pClassify = new NaturalBreaksClass();
break;
//case "标准差分类":
// pClassify = new StandardDeviationClass();
// break;
case "几何间隔分类":
pClassify = new GeometricalIntervalClass();
break;
default:
break;
}
int numDesiredClasses = gClassCount;
pClassify.Classify(dataValues, dataFrequency, ref numDesiredClasses);
gClassbreaks = (double[])pClassify.ClassBreaks;
}
}
}