当前位置: 移动技术网 > IT编程>开发语言>c# > c#唯一值渲染实例代码

c#唯一值渲染实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

该着色方法一句着色图层中要素类的某个数值字段的属性值,按这个属性值为每种不同值得要素单独分配一种显示符号样式。关键在于获取该字段所有要素的唯一值(即将所有在该字段中属性值相同的要素归为一种),利用uniquevaluerenderer的addvalue方法即可进行渲染

需要添加的类库

复制代码 代码如下:

using system.collections;
using system.windows.forms;
using esri.arcgis.display;
using esri.arcgis.carto;
using esri.arcgis.esrisystem;
using esri.arcgis.geodatabase;

具体代码(本例为单值渲染)

复制代码 代码如下:

private isymbol getsymbol(icolor pcolor)
        {
            isymbol psymbol;
            isimplefillsymbol psymbolfillsymbol = new simplefillsymbolclass();
            psymbolfillsymbol.color = pcolor;
            psymbolfillsymbol.outline.width = 0.4;
            psymbol = psymbolfillsymbol as isymbol;
            return psymbol;
        }
 private void uniquevaluerenderertoolstripmenuitem_click(object sender, eventargs e)
        {
            imap pmap = this.axmapcontrol1.activeview.focusmap;
            if (pmap.layercount== 0)
            {
                messagebox.show("地图为空,请加载地图!", "提示", messageboxbuttons.ok, messageboxicon.warning);
                return;
            }
            igeofeaturelayer pgeolayer = this.axmapcontrol1.get_layer(0) as igeofeaturelayer ;
            itable ptable = pgeolayer.featureclass as itable;
            icursor pcursor;
            iqueryfilter pqueryfilter = new queryfilter();
            pqueryfilter.addfield("perimeter");
            pcursor = ptable.search(pqueryfilter, true);//获取字段
            ienumerator penumreator;

            //获取字段中各要素属性唯一值
            idatastatistics pdatastatistics = new datastatisticsclass();
            pdatastatistics.field = "perimeter";//获取统计字段
            pdatastatistics.cursor = pcursor;
            penumreator = pdatastatistics.uniquevalues;
            int fieldcount = pdatastatistics.uniquevaluecount;//唯一值个数,以此确定颜色带范围

            iuniquevaluerenderer puniquevaluer = new uniquevaluerendererclass();
            puniquevaluer.fieldcount = 1;//单值渲染
            puniquevaluer.set_field(0, "perimeter");//渲染字段
            ienumcolors penumcolor = getcolorramp(fieldcount).colors;
            penumcolor.reset();

            while (penumreator.movenext())
            {
                string value = penumreator.current.tostring();
                if (value != null)
                {
                    icolor pcolor = penumcolor.next();
                    isymbol psymbol = getsymbol(pcolor);
                    puniquevaluer.addvalue(value, "perimeter", psymbol);
                    //puniquevaluer.set_symbol(value, psymbol);

                }

            }
            pgeolayer.renderer = puniquevaluer as ifeaturerenderer;
            this.axmapcontrol1.activeview.partialrefresh(esriviewdrawphase.esriviewgeography, null, null);
            this.axtoccontrol1.update();

        }
        private irandomcolorramp getcolorramp(int size)
        {
            irandomcolorramp prandomcolorramp = new randomcolorrampclass();
            prandomcolorramp.starthue = 10;
            prandomcolorramp.endhue = 300;
            prandomcolorramp.maxsaturation =100;
            prandomcolorramp.minsaturation = 0;
            prandomcolorramp.maxvalue = 100;
            prandomcolorramp.minvalue = 0;
            prandomcolorramp.size = size;
            bool ok = true;
            prandomcolorramp.createramp(out ok);
            return prandomcolorramp;
        }

其中datastatistic和uniquevaluerenderer的field必须要明确,且为相同值

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网