GIS-ArcGIS应用与开发技术(34)

唯一值符号化

1.制作子窗体

2.编写代码,在确定按钮的click事件中


        private void button1_Click(object sender, EventArgs e)
        {
           
            IFeatureLayer pfealayer = currentFeatureLayer;
            string sFieldName = comboBox2.SelectedItem.ToString();
           
            IGeoFeatureLayer pGeoFeaLayer = pfealayer as IGeoFeatureLayer;
            ITable ptable = pfealayer as ITable;
            IUniqueValueRenderer puniRender = new UniqueValueRendererClass();
            int fieldNumber = ptable.FindField(sFieldName);
            puniRender.FieldCount = 1;
            puniRender.set_Field(0, sFieldName);
            //设置显示颜色的范围
            IRandomColorRamp pRandColorRamp = new RandomColorRampClass();
            pRandColorRamp.StartHue = 0;
            pRandColorRamp.MinValue = 85;
            pRandColorRamp.MinSaturation = 15;
            pRandColorRamp.EndHue = 360;
            pRandColorRamp.MaxValue = 100;
            pRandColorRamp.MaxSaturation = 30;

            //创建随机颜色带
            pRandColorRamp.Size = getUniqueValue(pfealayer.FeatureClass, sFieldName)+1;
            bool bSucess = false;
            pRandColorRamp.CreateRamp(out bSucess);

            IEnumColors pEnumColors = pRandColorRamp.Colors;
            IColor pNextUniqueColor = null;


            //属性唯一值
            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.AddField(sFieldName);
            ICursor pCursor = pfealayer.FeatureClass.Search(pQueryFilter, false) as ICursor;
            IRow pNextRow = pCursor.NextRow();
            object codeValue = null;
            IRowBuffer pNextRowBuffer = null;

            IFillSymbol pFillSymbol = null;
            ILineSymbol pLineSymbol = null;
            IMarkerSymbol pMarkerSymbol = null;
            while (pNextRow != null)
            {
                pNextRowBuffer = pNextRow as IRowBuffer;
                codeValue = pNextRowBuffer.get_Value(fieldNumber);
                pNextUniqueColor = pEnumColors.Next();
                if (pNextUniqueColor == null)
                {
                    pEnumColors.Reset();
                    pNextUniqueColor = pEnumColors.Next();
                }
                switch (pGeoFeaLayer.FeatureClass.ShapeType)
                {
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                        pMarkerSymbol = new SimpleMarkerSymbolClass();
                        pMarkerSymbol.Color = pNextUniqueColor;
                        puniRender.AddValue(codeValue.ToString(), "", pMarkerSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                        pLineSymbol = new SimpleLineSymbolClass();
                        pLineSymbol.Color = pNextUniqueColor;
                        puniRender.AddValue(codeValue.ToString(), "", pLineSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                        pFillSymbol = new SimpleFillSymbolClass();
                        pFillSymbol.Color = pNextUniqueColor;
                        puniRender.AddValue(codeValue.ToString(), "", pFillSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        int k = pFillSymbol.Color.CMYK;
                        break;
                    default:
                        break;
                }
            }
            pGeoFeaLayer.Renderer = puniRender as IFeatureRenderer;
            mapcontrol1.Refresh();
            toccontrol1.Update();
            this.Close();
            必须手动清除COM对象,否则会造成内存溢出(尤其是IQueryFilter,ICursor)
            //Marshal.ReleaseComObject(pQueryFilter);
            //Marshal.ReleaseComObject(pCursor);
            //Marshal.ReleaseComObject(pEnumColors);


        }

 

3.定义函数,用来获取唯一值的个数

  public static int getUniqueValue(IFeatureClass pFeatureClass, string strFld)
       {
             //得到IFeatureCursor游标
            IFeatureCursor pCursor = pFeatureClass.Search(null, false);

            //coClass对象实例生成
           IDataStatistics pData = new DataStatisticsClass();
           pData.Field=strFld;
            pData.Cursor=pCursor as ICursor;

            //枚举唯一值
            IEnumerator pEnumVar = pData.UniqueValues;

            //记录总数
           int RecordCount = pData.UniqueValueCount;

            //字符数组
           string[] strValue = new string[RecordCount];   
           pEnumVar.Reset();
        
           int i = 0;

            while(pEnumVar.MoveNext())
            {
              
               strValue[i++]=pEnumVar.Current.ToString();
               
            }            
  
           return i;
       }

4.添加引用

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using System.Collections;
using ESRI.ArcGIS.Controls;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值