ArcGIS Engine9.3的分级着色法制作专题图(转)
ArcGIS Engine9.3的分级着色法制作专题图 (转)
原文地址:http://blog.csdn.net/suinon/article/details/5700136
分级着色法制作专题地图,一般首先从两个极值颜色生成一个颜色带,并按数值高低,赋予图元相应的颜色。ArcEngine9.3中无法使用IColorPallete、IColorSelector、IColorBrower等颜色板接口,一个可行的办法是使用.net的颜色对话框选择颜色,然后将选中颜色转换为ESRI的颜色。专题图显示后,需要带颜色带的图例才能观察,方法是,用panel控件的背景色进行设置。
程序如下:
- 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.Geodatabase;
- using ESRI.ArcGIS.esriSystem;
- namespace WorldMap
- {
- public partial class ThematicMapForm : Form
- {
- public ThematicMapForm()
- {
- InitializeComponent();
- }
- public ThematicMapForm(IMapControl3 pMapC)
- {
- InitializeComponent();
- this.pMapControl = pMapC;//获得MapControl的引用
- }
- private IMapControl3 pMapControl;
- private IEnumColors pEnumColors;//颜色带
- private double[] classes;//断点值,两端分别是渲染字段的最小值和最大值
- private Color startColor;//低值颜色
- private Color endColor;//高值颜色
- //在地图中添加label,参数为字段
- private void AddLabel2Map(string strField)
- {
- IGeoFeatureLayer pGeoFeatureLayer;
- ILineLabelPosition pLineLabelPosition;
- ILabelEngineLayerProperties pLabelEngineLayerProperties;
- IAnnotateLayerProperties pAnnotateLayerProperties;
- pGeoFeatureLayer = (IGeoFeatureLayer)this.pMapControl.get_Layer(4);
- pGeoFeatureLayer.AnnotationProperties.Clear();
- pLineLabelPosition = new LineLabelPositionClass();
- pLineLabelPosition.Above = false;
- pLineLabelPosition.AtEnd = false;
- pLineLabelPosition.Below = false;
- pLineLabelPosition.Horizontal = false;
- pLineLabelPosition.InLine = true;
- pLineLabelPosition.OnTop = true;
- pLineLabelPosition.Parallel = true;
- pLineLabelPosition.ProduceCurvedLabels = true;
- pLabelEngineLayerProperties = new LabelEngineLayerPropertiesClass();
- pLabelEngineLayerProperties.Symbol = new TextSymbolClass();
- pLabelEngineLayerProperties.IsExpressionSimple = true;
- pLabelEngineLayerProperties.Expression = "[" + strField + "]";//需要中括号
- pLabelEngineLayerProperties.BasicOverposterLayerProperties.LineLabelPosition = pLineLabelPosition;
- pAnnotateLayerProperties = (IAnnotateLayerProperties)pLabelEngineLayerProperties;
- pAnnotateLayerProperties.DisplayAnnotation = true;
- pAnnotateLayerProperties.FeatureLayer = pGeoFeatureLayer;
- pAnnotateLayerProperties.LabelWhichFeatures = esriLabelWhichFeatures.esriVisibleFeatures;
- pAnnotateLayerProperties.WhereClause = "";
- pGeoFeatureLayer.AnnotationProperties.Add(pAnnotateLayerProperties);
- pGeoFeatureLayer.DisplayAnnotation = true;
- this.pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
- }
- //产生图例(色带+数值区间)
- private void ProduceLegend(IEnumColors pEnumColors,double[] classes)
- {
- this.panel1.Controls.Clear();
- pEnumColors.Reset();//必须重设
- IColor pC = pEnumColors.Next();//获取ESRI色带的下一个颜色
- int j = 0;
- while (pC != null)
- {
- Panel p = new Panel();//色带用p的背景颜色表示
- Label l = new Label();//数值区间用l的tex表示
- p.BackColor = ColorTranslator.FromOle(pC.RGB);//ESRI颜色转换为.net颜色
- p.BorderStyle = BorderStyle.FixedSingle;
- p.Size = new System.Drawing.Size(88, 25);
- p.Location = new System.Drawing.Point(5, 5 + j * 25);
- l.Size = new System.Drawing.Size(120, 25);
- l.Location = new System.Drawing.Point(102, 13 + j * 25);
- l.Text = this.classes[j].ToString("#0.00") + "—" + this.classes[j + 1].ToString("#0.00");//保留两位小数
- this.panel1.Controls.Add(p);//添加到一个panel中
- this.panel1.Controls.Add(l);//添加到一个panel中
- j++;
- pC = pEnumColors.Next();
- }
- }
- //将.net颜色转变为ESRI的颜色
- public IColor ConvertColorToIColor(Color color)
- {
- IColor pColor = new RgbColorClass();
- pColor.RGB = color.B * 65536 + color.G * 256 + color.R;
- return pColor;
- }
- //专题图渲染(多级颜色)。参数为需要渲染的字段(数值类型),分级数目
- private void ClassBreaksMap(string strField, int numDesiredClasses)
- {
- IGeoFeatureLayer pGeoFeatureLayer;
- ITable pTable;
- IClassifyGEN pClassify;
- ITableHistogram pTableHistogram;
- IBasicHistogram pBasicHistogram;
- object dataFrequency;
- object dataValues;
- int classesCount;
- IClassBreaksRenderer pClassBreaksRenderer;
- IColor pColor;
- ISimpleFillSymbol pSimpleFillSymbol;
- int breakIndex;
- pGeoFeatureLayer = (IGeoFeatureLayer)this.pMapControl.get_Layer(4);
- pTable = (ITable)pGeoFeatureLayer.FeatureClass;
- pTableHistogram = new BasicTableHistogramClass();
- pBasicHistogram = (IBasicHistogram)pTableHistogram;
- pTableHistogram.Field = strField;
- pTableHistogram.Table = pTable;
- pBasicHistogram.GetHistogram(out dataValues, out dataFrequency);
- pClassify = new EqualIntervalClass();
- try
- {
- pClassify.Classify(dataValues, dataFrequency, ref numDesiredClasses);
- }
- catch (Exception ee)
- {
- MessageBox.Show(ee.Message);
- }
- classes = (double[])pClassify.ClassBreaks;
- classesCount = classes.GetUpperBound(0);
- pClassBreaksRenderer = new ClassBreaksRendererClass();
- pClassBreaksRenderer.Field = strField;
- pClassBreaksRenderer.BreakCount = classesCount;
- pClassBreaksRenderer.SortClassesAscending = true;
- pEnumColors = this.ProduceEnumColors(this.startColor, this.endColor, classesCount);//产生色带
- for (breakIndex = 0; breakIndex < classesCount; breakIndex++)
- {
- pColor = pEnumColors.Next();
- pSimpleFillSymbol = new SimpleFillSymbolClass();
- pSimpleFillSymbol.Color = pColor;
- pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
- pClassBreaksRenderer.set_Symbol(breakIndex, pSimpleFillSymbol as ISymbol);
- pClassBreaksRenderer.set_Break(breakIndex, classes[breakIndex + 1]);
- }
- pGeoFeatureLayer.Renderer = (IFeatureRenderer)pClassBreaksRenderer;
- this.pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
- }
- //根据起点颜色、终点颜色和级别数目,产生色带
- private IEnumColors ProduceEnumColors(Color start, Color end, int gradecount)
- {
- //创建一个新AlgorithmicColorRampClass对象
- IAlgorithmicColorRamp algColorRamp = new AlgorithmicColorRampClass();
- algColorRamp.ToColor = this.ConvertColorToIColor(end);//从.net的颜色转换
- algColorRamp.FromColor = this.ConvertColorToIColor(start);
- //设置梯度类型
- algColorRamp.Algorithm = esriColorRampAlgorithm.esriCIELabAlgorithm;
- //设置颜色带颜色数量
- algColorRamp.Size = gradecount;
- //创建颜色带
- bool bture = true;
- algColorRamp.CreateRamp(out bture);
- //使用IEnumColors获取颜色带
- return algColorRamp.Colors;
- }
- private void button3_Click(object sender, EventArgs e)
- {
- try
- {
- this.ClassBreaksMap("数据", 7);//按值分级渲染地图
- this.ProduceLegend(this.pEnumColors, this.classes);//绘制图例
- }
- catch(Exception thematicE)
- {
- MessageBox.Show(thematicE.Message+Environment.NewLine+"渲染地图错误,请重新设置!");
- }
- try
- {
- this.AddLabel2Map("数据");
- }
- catch (Exception addLabelE)
- {
- MessageBox.Show(addLabelE.Message + Environment.NewLine + "添加地图Label错误,请重新设置!");
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- DialogResult dr = this.colorDialog1.ShowDialog();
- if (dr == DialogResult.OK)
- {
- this.startColor = this.colorDialog1.Color;
- this.button4.BackColor = this.startColor;
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- DialogResult dr = this.colorDialog1.ShowDialog();
- if (dr == DialogResult.OK)
- {
- this.endColor = this.colorDialog1.Color;
- this.button5.BackColor = this.endColor;
- }
- }
- }
- }
效果图:

由于该图只有6个地区,效果不是很明显。当地区较多时,效果会比较好。
另外,分级着色法,必须保证级别数目大于等于2,当小于2时,无法正确绘制。
浙公网安备 33010602011771号