using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
namespace Studioat.ArcEngine
{
public sealed class Classify : BaseCommand
{
private IHookHelper m_hookHelper = null;
private ITOCControl2 m_tocControl = null;
public Classify(ITOCControl2 tocControl)
{
base.m_caption = "Classify LINQ";
m_tocControl = tocControl;
try
{
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overriden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
try
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
if (m_hookHelper.ActiveView == null)
m_hookHelper = null;
}
catch
{
m_hookHelper = null;
}
if (m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
//nedd to get the layer from the custom-property of the map
if (null == m_hookHelper)
return;
//get the mapControl hook
object hook = null;
if (m_hookHelper.Hook is IToolbarControl2)
{
hook = ((IToolbarControl2)m_hookHelper.Hook).Buddy;
}
else
{
hook = m_hookHelper.Hook;
}
//get the custom property from which is supposed to be the layer to be saved
object customProperty = null;
IMapControl3 mapControl = null;
if (hook is IMapControl3)
{
mapControl = (IMapControl3)hook;
customProperty = mapControl.CustomProperty;
}
else
return;
if ((null == customProperty) || !(customProperty is ILayer) || !(customProperty is IFeatureLayer))
return;
IFeatureLayer featureLayer = customProperty as IFeatureLayer;
IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
IFeatureClass featureClass = geoFeatureLayer.DisplayFeatureClass;
string sFieldName = "Popolazione";
int numClasses = 5;
IBasicHistogram basicHistogram = new BasicTableHistogram();
ITableHistogram tableHistogram = basicHistogram as ITableHistogram;
tableHistogram.Field = sFieldName;
tableHistogram.Table = featureClass as ITable;
object oVals =new object();
object oFreqs =new object();
basicHistogram.GetHistogram(out oVals, out oFreqs);
IClassifyGEN classifyGEN = new NaturalBreaks();
classifyGEN.Classify(oVals, oFreqs, ref numClasses);
IClassBreaksRenderer render = new ClassBreaksRenderer();
double[] cb = (double[])classifyGEN.ClassBreaks;
double[] dVals = (double[])oVals;
int[] iFreqs = (int[])oFreqs;
var dValsL = dVals.Select((num, index) => new { Num = num, Index = index });
var iFreqsL = iFreqs.Select((num, index) => new { Num = num, Index = index });
var pairs = from a in dValsL join b in iFreqsL on a.Index equals b.Index select new { Values = a.Num, Frequency = b.Num };
List<int> listCount = new List<int>();
for (int i = 0; i < numClasses; i++)
{
var a = from p in pairs where ((i == 0) ? (p.Values >= cb[i]) : (p.Values > cb[i])) && p.Values <= cb[i + 1] select new { p.Frequency };
int j = a.Sum(p => p.Frequency);
listCount.Add(j);
}
render.Field = sFieldName;
render.BreakCount = numClasses;
render.MinimumBreak = cb[0];
IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRamp();
colorRamp.Algorithm = esriColorRampAlgorithm.esriCIELabAlgorithm;
IRgbColor color1 = new RgbColor();
IRgbColor color2 = new RgbColor();
color1.Red = 255 ;
color1.Green = 210;
color1.Blue = 210;
color2.Red = 190;
color2.Green = 0;
color2.Blue = 170;
colorRamp.FromColor = color1;
colorRamp.ToColor = color2;
colorRamp.Size = 5 ;
bool ok = true;
colorRamp.CreateRamp (out ok);
IClassBreaksUIProperties classBreaksUIProperties = render as IClassBreaksUIProperties;
classBreaksUIProperties.ColorRamp = "Custom";
IEnumColors enumColors = colorRamp.Colors;
enumColors.Reset();
for (int i = 0;i<numClasses;i++)
{
render.set_Break(i,cb[i + 1]);
render.set_Label(i, string.Format("{0} - {1} - Count {2}", cb[i], cb[i + 1], listCount[i]));
classBreaksUIProperties.set_LowBreak(i, cb[i]);
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
simpleFillSymbol.Color = enumColors.Next();
render.set_Symbol(i,simpleFillSymbol as ISymbol);
}
geoFeatureLayer.Renderer = render as IFeatureRenderer;
mapControl.ActiveView.Refresh();
m_tocControl.Update();
}
#endregion
}
}
Qui puoi scaricare la soluzione in VS2008 ClassBreakLinq