ArcEngine简单教程——栅格、矢量图层的颜色渲染

ArcEngine简单教程汇总

目录

ArcEngine简单教程汇总

1 目的

2 概要

色带:

栅格渲染:

矢量渲染:

3代码

4小结


 

1 目的

实现简单的按照arcmap自带色带,进行图层渲染功能,包括

对栅格图层,按栅格值进行拉伸渲染、分级渲染、唯一值渲染

对矢量图层,按某一字段值进行分级渲染、唯一值渲染

 

2 概要

本文只介绍主要功能方法,具体的调用关系、窗口实现等完整完整demo可见:

https://download.csdn.net/download/nominior/12454058

色带:

1)获取

使用SymbologyControl控件获取ArcMap色带,色带位置:ArcMap安装路径/Styles/ESRI.Style

ArcEngine简单教程——栅格、矢量图层的颜色渲染_第1张图片   ArcEngine简单教程——栅格、矢量图层的颜色渲染_第2张图片

2)显示

使用C#中的combobox对色带进行显示(比SymbologyControl效果略好),分两个过程:

  • 修改combobox的DrawItem方法,使得combobox可以绘制色带
  • 遍历SymbologyControl中的色带,绘制到combobox中

 

栅格渲染:

  • 包括拉伸渲染、分级渲染、唯一值渲染等渲染方式
  • 基本渲染流程为:创建并设置对应渲染器,渲染颜色与渲染值映射,栅格图层渲

具体见 代码及注释

 

矢量渲染:

  • 包括拉伸渲染、分级渲染、唯一值渲染等渲染方式
  • 基本渲染流程为:创建并设置对应渲染器,数值处理,渲染颜色与渲染值映射,栅格图层渲染

具体见 代码及注释

 

 

3代码

涉及的ArcGIS引用、界面示意如图:

ArcEngine简单教程——栅格、矢量图层的颜色渲染_第3张图片     ArcEngine简单教程——栅格、矢量图层的颜色渲染_第4张图片     ArcEngine简单教程——栅格、矢量图层的颜色渲染_第5张图片

combobox色带显示代码

        // combobox重绘
        private void cb_colormap_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawImage(cb_colormap.Items[e.Index] as Image, e.Bounds);
        }

        // 初始化符号库
        private void InitSymbologyControl()
        {
            //色带定位
            string sInstall = "D:\\ArcGIS\\Desktop10.2\\";
            string defaultStyle = System.IO.Path.Combine(sInstall, "Styles\\ESRI.ServerStyle");
            //色带读取
            this.axSymbologyControl1.LoadStyleFile(defaultStyle);
            this.axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;
            //色带另存
            this.pSymbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
        }

        // 初始化色带下拉框
        private void InitColorRampCombobox()
        {
            this.cb_colormap.DrawMode = DrawMode.OwnerDrawFixed;
            this.cb_colormap.DropDownStyle = ComboBoxStyle.DropDownList;
            //绘制色带
            for (int i = 0; i < pSymbologyStyleClass.ItemCount; i++)
            {
                //从另存中读取色带
                IStyleGalleryItem pStyleGalleryItem = pSymbologyStyleClass.GetItem(i);
                IPictureDisp pPictureDisp = pSymbologyStyleClass.PreviewItem(pStyleGalleryItem, cb_colormap.Width, cb_colormap.Height);
                Image image = Image.FromHbitmap(new IntPtr(pPictureDisp.Handle));
                cb_colormap.Items.Add(image);
            }
            cb_colormap.SelectedIndex = 25;
        }

栅格渲染代码

        #region 不同方式的栅格渲染
        private void SetRasterSymbol(IRasterLayer pRasterLayer)
        {
            //获取cb选择的色带序号,根据序号选择色带
            int symbol_index = cb_colormap.SelectedIndex;
            ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
            IStyleGalleryItem mStyleGalleryItem = symbologyStyleClass.GetItem(symbol_index);
            IColorRamp colorramp_select = (IColorRamp)mStyleGalleryItem.Item;

            if (cb_method.Text == "Stretch")
            {
                RasterStretchRender(colorramp_select, pRasterLayer);
            }
            else if (cb_method.Text == "Classify")
            {
                if (textBox1.Text == "不分")
                {

                }
                else
                {
                    int cls_num = Convert.ToInt32(textBox1.Text);
                    RasterClassifyRender(colorramp_select, pRasterLayer, cls_num);
                }          
            }
            else if (cb_method.Text == "Unique")
            {
                RasterUniqueRender(colorramp_select, pRasterLayer);
            }

            //视图刷新
            map.ActiveView.ContentsChanged();
            map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
        }

        private void RasterStretchRender(IColorRamp pColorRamp, IRasterLayer pRasterLayer)
        {
            //渲染器定义
            IRasterStretchColorRampRenderer pSRRender = new RasterStretchColorRampRendererClass();
            IRasterRenderer pRRender = pSRRender as IRasterRenderer;

            //渲染器设置
            pRRender.Raster = pRasterLayer.Raster;
            pRRender.Update();
            pSRRender.ColorRamp = pColorRamp;

            //栅格渲染
            pRasterLayer.Renderer = pRRender;
        }

        private void RasterClassifyRender(IColorRamp pColorRamp, IRasterLayer pRasterLayer, int cls_num)
        {
            //渲染器定义
            IRasterClassifyColorRampRenderer pRCRender = new RasterClassifyColorRampRendererClass();
            IRasterRenderer pRRend = pRCRender as IRasterRenderer;

            //渲染器设置
            pRRend.Raster = pRasterLayer.Raster;
            pRRend.Update();
            pRCRender.ClassCount = cls_num;
            //渲染映射
            int gap = pColorRamp.Size / (cls_num-1);
            IFillSymbol fillSymbol = new SimpleFillSymbol() as IFillSymbol;
            for (int i = 0; i < pRCRender.ClassCount; i++)
            {
                int index;
                if (i < pRCRender.ClassCount - 1)
                {
                    index = i * gap;
                }
                else
                {
                    index = pColorRamp.Size-1;
                }
                fillSymbol.Color = pColorRamp.get_Color(index);
                pRCRender.set_Symbol(i, fillSymbol as ISymbol);
                pRCRender.set_Label(i, pRCRender.get_Break(i).ToString("0.00") +"-"+ pRCRender.get_Break(i+1).ToString("0.00"));
            }
            //栅格渲染
            pRasterLayer.Renderer = pRRend;
        }

        private void RasterUniqueRender(IColorRamp pColorRamp, IRasterLayer pRasterLayer)
        {
            //渲染器定义
            IRasterUniqueValueRenderer pRURender = new RasterUniqueValueRendererClass();
            IRasterRenderer pRRend = pRURender as IRasterRenderer;

            //渲染器设置
            pRRend.Raster = pRasterLayer.Raster;
            pRRend.Update();
            
            IUniqueValues uniqueValues = new UniqueValuesClass();
            IRasterCalcUniqueValues calcUniqueValues = new RasterCalcUniqueValuesClass();
            calcUniqueValues.AddFromRaster(pRasterLayer.Raster, 0, uniqueValues);//计算第0通道的唯一值
            //渲染映射
            int gap = pColorRamp.Size / (uniqueValues.Count - 1);
            IFillSymbol fillSymbol = new SimpleFillSymbol() as IFillSymbol;
            for (int i = 0; i < uniqueValues.Count; i++)
            {
                int index;
                if (i < uniqueValues.Count - 1)
                {
                    index = i * gap;
                }
                else
                {
                    index = pColorRamp.Size - 1;
                }
                fillSymbol.Color = pColorRamp.get_Color(index);
                pRURender.AddValue(0, i, uniqueValues.get_UniqueValue(i));
                pRURender.set_Label(0, i, uniqueValues.get_UniqueValue(i).ToString());
                pRURender.set_Symbol(0, i, fillSymbol as ISymbol);
            }
            //栅格渲染
            pRasterLayer.Renderer = pRRend;
        }
        #endregion

矢量渲染代码

        #region 不同方式的矢量渲染
        private void SetFeatureSymbol(IFeatureLayer pFeatureLayer)
        {
            //获取cb选择的色带序号,根据序号选择色带
            int symbol_index = cb_colormap.SelectedIndex;
            ISymbologyStyleClass symbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
            IStyleGalleryItem mStyleGalleryItem = symbologyStyleClass.GetItem(symbol_index);
            IColorRamp colorramp_select = (IColorRamp)mStyleGalleryItem.Item;

            if (cb_method.Text == "Classify")
            {
                if (textBox1.Text == "不分")
                {

                }
                else
                {
                    int cls_num = Convert.ToInt32(textBox1.Text);
                    FeatureClassifyRender(colorramp_select, pFeatureLayer, cls_num);
                }
            }
            else if (cb_method.Text == "Unique")
            {
                FeatureUniqueRender(colorramp_select, pFeatureLayer);
            }

            //视图刷新
            map.ActiveView.ContentsChanged();
            map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
        }

        private void FeatureClassifyRender(IColorRamp pColorRamp, IFeatureLayer pFeatureLayer, int cls_num)
        {
            if (pField.Type == esriFieldType.esriFieldTypeDouble || pField.Type == esriFieldType.esriFieldTypeInteger || pField.Type == esriFieldType.esriFieldTypeOID)
            {
                //渲染器定义
                IClassBreaksRenderer pFCRender = new ClassBreaksRendererClass();
                IFeatureRenderer pFRender = pFCRender as IFeatureRenderer;

                //渲染器设置
                pFCRender.Field = pField.Name;
                pFCRender.BreakCount = cls_num+1;

                //数值处理
                ITableHistogram histo = new BasicTableHistogramClass();//获取直方图
                histo.Field = pField.Name;
                histo.Table = pFeatureLayer.FeatureClass as ITable;
                object dbArray, nArray;//定义分级数组
                (histo as IBasicHistogram).GetHistogram(out dbArray, out nArray);
                double[] dataArray = dbArray as double[];
                int[] fredArray = nArray as int[];
                IClassifyGEN classify = new EqualIntervalClass();//平均分级法
                classify.Classify(dataArray, fredArray, cls_num);
                double[] Breaks = classify.ClassBreaks as double[];//分级数组确定


                //渲染映射
                pFCRender.MinimumBreak = Breaks[0];
                int gap = pColorRamp.Size / (pFCRender.BreakCount - 1);
                for (int i = 0; i < cls_num; i++)
                {
                    int index;
                    if (i < cls_num - 1)
                    {
                        index = i * gap;
                    }
                    else
                    {
                        index = pColorRamp.Size - 1;
                    }
                    ISymbol pSymbol = null;
                    if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        pSymbol = new SimpleFillSymbolClass();
                        (pSymbol as IFillSymbol).Outline.Width = 1.0;
                        (pSymbol as IFillSymbol).Color = pColorRamp.get_Color(index);
                    }
                    else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                    {
                        pSymbol = new SimpleLineSymbolClass();
                        (pSymbol as ILineSymbol).Width = 1.0;
                        (pSymbol as ILineSymbol).Color = pColorRamp.get_Color(index);
                    }
                    else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                    {
                        pSymbol = new SimpleMarkerSymbolClass();
                        (pSymbol as IMarkerSymbol).Size = 1.0;
                        (pSymbol as IMarkerSymbol).Color = pColorRamp.get_Color(index);      
                    }
                    pFCRender.set_Break(i, Breaks[i + 1]);
                    pFCRender.set_Symbol(i, pSymbol);
                    pFCRender.set_Label(i, Breaks[i].ToString() + "-" + Breaks[i + 1].ToString());
                }

                //矢量图层渲染
                (pFeatureLayer as IGeoFeatureLayer).Renderer = pFRender;
            }
        }

        private void FeatureUniqueRender(IColorRamp pColorRamp, IFeatureLayer pFeatureLayer)
        {
            //渲染器定义
            IUniqueValueRenderer pFURender = new UniqueValueRendererClass();
            IFeatureRenderer pFRender = pFURender as IFeatureRenderer;

            //渲染器设置
            pFURender.FieldCount = 1;
            pFURender.set_Field(0, pField.Name);
            if (pField.Type == esriFieldType.esriFieldTypeString)
            {
                pFURender.set_FieldType(0, true);
            }
            else
            {
                pFURender.set_FieldType(0, false);
            }

            //数值处理
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeatureCursor lFeatureCursor = pFeatureClass.Search(null, false);
            IDataStatistics pData = new DataStatisticsClass();
            pData.Field = pField.Name;
            pData.Cursor = lFeatureCursor as ICursor;
            IEnumerator pEnumVar = pData.UniqueValues;
            string[] uniqueValues = new string[pData.UniqueValueCount];
            pEnumVar.Reset();
            int n = 0;
            while (pEnumVar.MoveNext())
            {
                uniqueValues[n++] = pEnumVar.Current.ToString();
            }

            //渲染映射
            int gap = pColorRamp.Size / (uniqueValues.Length - 1);
            for (int i = 0; i < uniqueValues.Length; i++)
            {
                int index;
                if (i < uniqueValues.Length - 1)
                {
                    index = i * gap;
                }
                else
                {
                    index = pColorRamp.Size - 1;
                }
                ISymbol pSymbol = null;
                if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    pSymbol = new SimpleFillSymbolClass();
                    (pSymbol as IFillSymbol).Outline.Width = 1.0;
                    (pSymbol as IFillSymbol).Color = pColorRamp.get_Color(index);
                }
                else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    pSymbol = new SimpleLineSymbolClass();
                    (pSymbol as ILineSymbol).Width = 1.0;
                    (pSymbol as ILineSymbol).Color = pColorRamp.get_Color(index);
                }
                else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    pSymbol = new SimpleMarkerSymbolClass();
                    (pSymbol as IMarkerSymbol).Size = 2.0;
                    (pSymbol as IMarkerSymbol).Color = pColorRamp.get_Color(index);
                }
                pFURender.AddValue(uniqueValues[i], "", pSymbol);
            }

            //矢量图层渲染
            (pFeatureLayer as IGeoFeatureLayer).Renderer = pFRender;


        }
        #endregion

4小结

渲染过程2个关键对象:色带、渲染器,1个重要过程渲染器与对应渲染值、渲染颜色的关系映射

你可能感兴趣的:(arcgis,ArcEngine,ArcGIS,颜色渲染)