ArcEngine C# GIS开发入门作业 (三)Ex04——实现地图的简单渲染和唯一值渲染

作业说明:

ArcEngine C# GIS开发入门作业 (三)Ex04——实现地图的简单渲染和唯一值渲染
此次实现主要落在了渲染上,所以没有采用IWorkSpace的方式打开要素,直接加载了MXD文件进行渲染。另外控件名称按照同学我的个人习惯作了修改,还是那句话,理解过程和看懂代码为主。

下面为我​​的窗体样式(图借的是舍友的):
ArcEngine C# GIS开发入门作业 (三)Ex04——实现地图的简单渲染和唯一值渲染

不说废话了直接上代码,下面的注释应该还够用,有的因为在前面的几个作业中有过描述就没有再重复了,新朋友可以看我前几篇文章。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;


namespace EX04副件
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                ILayer pLayer = axMapControl1.get_Layer(i);
                FeatureLayercomboBox.Items.Add(pLayer.Name);

            }
        }

        private void FeatureLayercomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedcomboBox.Items.Clear();
            GreencomboBox.Items.Clear();
            BluecomboBox.Items.Clear();
            UniqueValuecomboBox.Items.Clear();
            int index = FeatureLayercomboBox.SelectedIndex;//将FeatureLayercomboBox下拉框中选中的图层的引索赋予index
            ILayer pLayer = axMapControl1.get_Layer(index);//将刚设置的index设置为axMapControl要get的图层引索号
            pLayer.Visible = true;//将选中的图层设置为显示
            axMapControl1.Refresh();//这里因为不刷新图层的变化将显示不出来,所以要刷新一下

            IFeatureLayer pFeatureLayer = (FeatureLayer)pLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            int num = pFeatureClass.Fields.FieldCount;
            for (int i = 0; i < num; i++)//将图层的各字段名字预设到UniqueValuecomboBox中以供选择
            {
                UniqueValuecomboBox.Items.Add(pFeatureClass.Fields.get_Field(i).Name);

            }
            for (int j = 1; j < 256; j++)//在RGB下拉框内预设0-255的值以供选择
            {
                RedcomboBox.Items.Add(j);
                GreencomboBox.Items.Add(j);
                BluecomboBox.Items.Add(j);

            }
        }

        private void RenderLayer_Click(object sender, EventArgs e)
        {
            
            

            //单值渲染
            ILayer pLayer = null;
            int i = FeatureLayercomboBox.SelectedIndex;
            if (SimpleradioButton.Checked == true /*&& i == 0*/)
            {
                //将各颜色combobox中的text类型值转化为int类型,方便后面将其赋给新创建pRgbColor。
                if (RedcomboBox.Text == "") return;
                int RedValue = Convert.ToInt32(RedcomboBox.Text);
                int GreenValue = Convert.ToInt32(GreencomboBox.Text);
                int BlueValue = Convert.ToInt32(BluecomboBox.Text);
                pLayer = axMapControl1.get_Layer(i);
                IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
                //设定RGB三种填充颜色给pRgbColor,并再赋给pSimpleFillSymbol
                ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
                IRgbColor pRgbColor = new RgbColor();
                pRgbColor.Red = RedValue;
                pRgbColor.Green = GreenValue;
                pRgbColor.Blue = BlueValue;
                pSimpleFillSymbol.Color = pRgbColor;
                //将pSimpleFillSymbol赋给新创建的pRenderer,再将pRenderer赋给之前的pGeoFeatureLayer
                ISimpleRenderer pRenderer = new SimpleRendererClass();
                pRenderer.Symbol = pSimpleFillSymbol as ISymbol;
                pGeoFeatureLayer.Renderer = pRenderer as IFeatureRenderer;
                axMapControl1.Refresh();
                axTOCControl1.Update();
            }

            //唯一值渲染:下面根据随机色给各个要素,按给定要素的属性字段值的不同赋予不同颜色
            if(UniqueValueradioButton.Checked==true)
            {
                pLayer = axMapControl1.get_Layer(i);
                IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
                IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();//构造一个UniqueValueRenderer

                pUniqueValueRenderer.FieldCount = 1;
                string FieldName = UniqueValuecomboBox.SelectedItem.ToString();//将UniqueValuecomboBox中所选的项目转换为字符类型,并赋给FieldName
                pUniqueValueRenderer.set_Field(0,FieldName);//将唯一值渲染器pUniqueValueRenderer要渲染的字段设为FieldName所代表的UniquecomboBox中的Item

                //要做到随机色渲染,需要先创建一个随机渲染的色谱范围,方便后面对uniqueValueRenderer赋予的颜色pRgbColor在所设定的色谱范围内随机读取
                //(色谱的范围大致决定了所渲染出来的整体效果)
                IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
                pRandomColorRamp.StartHue = 0;//设置随机色带的起始色度
                pRandomColorRamp.EndHue = 360;//设置随机色带的末尾色度
                pRandomColorRamp.MinSaturation = 10;//设置随机色带的最小饱和度
                pRandomColorRamp.MaxSaturation = 30;//设置随机色带的最大饱和度
                pRandomColorRamp.MinValue = 100;//设置随机色带的最小纯度
                pRandomColorRamp.MaxValue = 100;//设置随机色带的最小纯度

                IQueryFilter pQueryFilter = new QueryFilterClass();
                pQueryFilter.AddField(FieldName);
                //根据渲染字段的唯一值的个数,设施一组随机颜色
                pRandomColorRamp.Size = pGeoFeatureLayer.FeatureClass.FeatureCount(pQueryFilter);


                bool out_outparameter = false;
                pRandomColorRamp.CreateRamp(out out_outparameter);//创建随机颜色→需要输出参数,参数类型为bool类型
                IEnumColors pEnumRamp = pRandomColorRamp.Colors;//创建颜色枚举
                IColor pNextUniqueColor = null;//视为用来移动的指示颜色的指针
                pQueryFilter = new QueryFilterClass();//刷新pQueryFilter的值
                pQueryFilter.AddField(FieldName);//添加待查询字段

                ITable pTable = pLayer as Table;
                int FieldNumbero = pTable.FindField(FieldName);//将字段在表中的索引(即第几列)存入FieldNumbero

                ICursor pCursor = pTable.Search(pQueryFilter,true);//true循环使用内存空间,false开辟新的内存空间(UpdateCursor用),
                IRow pNextRow = pCursor.NextRow();
                IRowBuffer pNextRowBuffer = null;
                object codeValue = null;//创建移动指针,用来指向要渲染字段的每一个值

                while (pNextRow != null)
                {
                    pNextRowBuffer = pNextRow as RowBuffer;
                    codeValue = pNextRowBuffer.get_Value(FieldNumbero);//获取要渲染字段的每一个值
                    pNextUniqueColor = pEnumRamp.Next();//将pNextUniqueColor指向之前创建好的色带pEnumRamp的下一个颜色
                    //若为空值则返回色带的第一个颜色进行赋值
                    if (pNextUniqueColor == null)
                    {
                        pEnumRamp.Reset();//跳回pEnumRamp顶部
                        pNextUniqueColor = pEnumRamp.Next();//将pEnumRamp的第一个颜色赋予pNextUniqueColor 
                    }
                    ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();//
                    pSimpleFillSymbol.Color = pNextUniqueColor;
                    pUniqueValueRenderer.AddValue(codeValue.ToString(),"",pSimpleFillSymbol as ISymbol);
                    pNextRow = pCursor.NextRow();
 
                }
                pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;
                axMapControl1.Refresh();
                axTOCControl1.Refresh();


    
            }
        }
    }
}

最后运行成果是这样的
ArcEngine C# GIS开发入门作业 (三)Ex04——实现地图的简单渲染和唯一值渲染

相关文章: