分级渲染
/// <summary>/// 分级渲染/// </summary>/// <param name="pMapControl">数据视图</param>/// <param name="pFtLayer">图层</param>/// <param name="ClassCount">分为几类</param>/// <param name="pFieldName">字段名称</param>public ClassRender(AxMapControl pMapControl, IFeatureLayer pFtLayer, int ClassCount, string pFieldName){IGeoFeatureLayer pGeolayer = pFtLayer as IGeoFeatureLayer;IActiveView pActiveView = pMapControl.ActiveView;//以下是为了统计和分类所需要的对象ITable pTable = pGeolayer as ITable;IClassifyGEN pClassify = new EqualIntervalClass();//为一只分类IBasicHistogram pBasicHist = new BasicTableHistogramClass();ITableHistogram pTableHist = pBasicHist as ITableHistogram;//相当于一个统计表pTableHist.Table = pTable;//属性表信息pTableHist.Field = pFieldName;//字段名称IHsvColor pFromColor;IHsvColor pToColor;//用于构建另外一个颜色带对象。IMap pMap = pMapControl.Map;pMap.ReferenceScale = 0;object datavalus;object Frenquen;pBasicHist.GetHistogram(out datavalus, out Frenquen);//获得数据和相应的频数。//datavalues与Frenquen对应,前者获取到数字,后者获取到该输出自出现的频率try{pClassify.Classify(datavalus, Frenquen, ref ClassCount);}catch (Exception e){MessageBox.Show(e.Message);}// 分类完成double[] ClassNum = (double[])pClassify.ClassBreaks;//得到分段值int ClassCountResult = ClassNum.GetUpperBound(0);//返回分级个数,GetUpperBound获取最高下标//地图渲染IClassBreaksRenderer pClassBreak = new ClassBreaksRendererClass();pClassBreak.Field = pFieldName;//设置分级字段名称pClassBreak.BreakCount = ClassCountResult;//分级数pClassBreak.SortClassesAscending = true;IAlgorithmicColorRamp pAlgo = new AlgorithmicColorRampClass();//设色算法pAlgo.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;pFromColor = Hsv(60, 100, 96);pToColor = Hsv(0, 100, 96);pAlgo.FromColor = pFromColor;pAlgo.ToColor = pToColor;pAlgo.Size = ClassCountResult;bool ok;pAlgo.CreateRamp(out ok);IEnumColors pEnumColor;pEnumColor = pAlgo.Colors;pEnumColor.Reset();IColor pColor;ISimpleFillSymbol pSimFill;for (int indexColor = 0; indexColor <= ClassCountResult - 1; indexColor++){pColor = pEnumColor.Next();pSimFill = new SimpleFillSymbolClass();pSimFill.Color = pColor; pSimFill.Style = esriSimpleFillStyle.esriSFSSolid;//染色pClassBreak.set_Symbol(indexColor, pSimFill as ISymbol);pClassBreak.set_Break(indexColor, ClassNum[indexColor + 1]);}pGeolayer.Renderer = pClassBreak as IFeatureRenderer;pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);}/// <summary>/// HSV颜色设置/// </summary>/// <param name="hue"></param>/// <param name="saturation"></param>/// <param name="val"></param>/// <returns></returns>public IHsvColor Hsv(int hue, int saturation, int val){IHsvColor pHsvC;pHsvC = new HsvColorClass();pHsvC.Hue = hue;pHsvC.Saturation = saturation;pHsvC.Value = val;return pHsvC;}
有次可以看出分类主要使用 IClassifyGEN 接口,其接口描述如下:
Classify objects apply one of several methods to statistically subdivide a set of numeric values into classes.
The IClassifyGEN interface is implemented by all the data classification objects (DefinedInterval, EqualInterval, NaturalBreaks, Quantile,StandardDeviation); this is the interface used to pass in histogram data and then classify it into breaks. The ClassID and MethodName properties are used by user interface dialog boxes to identify the classification object and establish what the classification is called.
In general, to classify a dataset, use this interface together with IClassBreaksRenderer. Use the Classify method to add data to the classification; set the number of classes; and generate breaks (typically use the same number of classes specified here for IClassBreaksRenderer.BreakCount). Then apply the classification to the renderer by cycling through ClassBreaks and setting each IClassBreaksRenderer.Break.
遍历图层所有字段
/// <summary>/// 添加字段/// </summary>private void AddField(){if (m_layer == null){ return; }IFeatureLayer feaLayer = m_layer as IFeatureLayer;IFields fields = feaLayer.FeatureClass.Fields;cmbFields.Items.Clear();if (fields != null){for (int i = 0; i < fields.FieldCount; ++i){string name = fields.get_Field(i).Name;if (name != "Shape" && name != "FID"){cmbFields.Items.Add(name);}}}}
将symbol转换成图片
//Preview an image of the symbol/// <summary>/// 将符号转换为图片/// </summary>/// <param name="pSymbol">符号</param>/// <param name="iwidth">宽度</param>/// <param name="iheight">高度</param>/// <param name="lGap">边距</param>/// <returns></returns>private System.Drawing.Bitmap SymbolToBitmp(ESRI.ArcGIS.Display.ISymbol pSymbol, int iwidth, int iheight, int lGap){///根据高宽创建图象Bitmap bmp = new Bitmap(iwidth, iheight);Graphics gImage = Graphics.FromImage(bmp);//转化成GraphicsgImage.Clear(Color.White); //清除底色,设置为白色double dpi = gImage.DpiX; //IEnvelope pEnvelope = new EnvelopeClass();//创建矩形范围pEnvelope.PutCoords(0, 0, (double)bmp.Width, (double)bmp.Height);//设置边界值tagRECT deviceRect;deviceRect.left = lGap;deviceRect.right = bmp.Width - lGap;deviceRect.top = lGap;deviceRect.bottom = bmp.Height - lGap;IDisplayTransformation pDisplayTransformation = new DisplayTransformationClass();pDisplayTransformation.VisibleBounds = pEnvelope;pDisplayTransformation.Bounds = pEnvelope;pDisplayTransformation.set_DeviceFrame(ref deviceRect);pDisplayTransformation.Resolution = dpi; IGeometry pGeo = CreateGeometryFromSymbol(pSymbol, pEnvelope);//符号转为图片System.IntPtr hdc = new IntPtr();hdc = gImage.GetHdc();pSymbol.SetupDC((int)hdc, pDisplayTransformation);pSymbol.Draw(pGeo);pSymbol.ResetDC();gImage.ReleaseHdc(hdc);gImage.Dispose();return bmp;}/// <summary>/// 通过符号转换为几何图形/// </summary>/// <param name="sym">符号</param>/// <param name="env">几何边界</param>/// <returns></returns>private ESRI.ArcGIS.Geometry.IGeometry CreateGeometryFromSymbol(ISymbol sym, IEnvelope env){if (sym is IMarkerSymbol){IArea area = env as IArea;return area.Centroid as IGeometry;}else if (sym is ILineSymbol || sym is ITextSymbol){IPolyline line = new PolylineClass();IPoint pt = new PointClass();pt.PutCoords(env.LowerLeft.X, (env.LowerLeft.Y + env.UpperRight.Y) / 2);line.FromPoint = pt;pt = new PointClass();pt.PutCoords(env.UpperRight.X, (env.LowerLeft.Y + env.UpperRight.Y) / 2);line.ToPoint = pt;return line as IGeometry;}else if (sym is IFillSymbol){IPolygon polygon = new PolygonClass();IPointCollection ptCol = (IPointCollection)polygon;IPoint pt = new PointClass();pt.PutCoords(env.LowerLeft.X, env.LowerLeft.Y);ptCol.AddPoints(1, ref pt);pt.PutCoords(env.UpperLeft.X, env.UpperLeft.Y);ptCol.AddPoints(1, ref pt);pt.PutCoords(env.UpperRight.X, env.UpperRight.Y);ptCol.AddPoints(1, ref pt);pt.PutCoords(env.LowerRight.X, env.LowerRight.Y);ptCol.AddPoints(1, ref pt);pt.PutCoords(env.LowerLeft.X, env.LowerLeft.Y);ptCol.AddPoints(1, ref pt);return polygon as IGeometry;}else{System.Windows.Forms.MessageBox.Show("找到一种特殊的符号!");return null;}}/// <summary>/// 设置栅格数据渲染/// </summary>/// <param name="mRasterLayer">栅格图层</param>private void SetRasterSymbol(IRasterLayer mRasterLayer){try{int symbol_index = cmb_symbol.SelectedIndex;//获取选择的序号ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);IStyleGalleryItem mStyleGalleryItem = symbologyStyleClass.GetItem(symbol_index);IColorRamp colorramp_select = (IColorRamp)mStyleGalleryItem.Item;//获取选择的符号//IColorRampSymbol mColorRampSymbol = new ColorRampSymbolClass();//mColorRampSymbol = (IColorRampSymbol)symbol_select;IRasterStretchColorRampRenderer pStretchRen = (IRasterStretchColorRampRenderer)mRasterLayer.Renderer;pStretchRen.ColorRamp = colorramp_select;}catch (Exception ex){System.Diagnostics.Trace.WriteLine(ex.Message);}}
重写ComboBox控件,用于显示颜色
/// <summary>/// 重写ComboBox控件/// </summary>public partial class ComboboxSymbol : ComboBox{public ComboboxSymbol(){//以下两句是关键的;DrawMode = DrawMode.OwnerDrawFixed;DropDownStyle = ComboBoxStyle.DropDownList;}//重写函数protected override void OnDrawItem(DrawItemEventArgs e){e.DrawBackground();e.DrawFocusRectangle();try{ //显示图片Image image = (Image)Items[e.Index];System.Drawing.Rectangle rect = e.Bounds;e.Graphics.DrawImage(image, rect);}catch{}finally{base.OnDrawItem(e);}}}