ArcEngine实现要素图层唯一值符号化(渲染)(20190909)

1、需求

选择地图中某一要素图层的属性,对其进行唯一值符号化(相同的属性用同样的颜色渲染)

2、解决方案

(1)IUniqueValueRenderer接口实现要素唯一值的渲染;
(2)IRandomColorRamp接口创建随机颜色带;

3、界面设计

主要有两个界面,分别是主窗体界面和唯一值符号化界面
(1)主界面:
ArcEngine实现要素图层唯一值符号化(渲染)(20190909)_第1张图片
(2)唯一值符号化界面
ArcEngine实现要素图层唯一值符号化(渲染)(20190909)_第2张图片

4、代码实现

实现要素图层属性唯一值设色的方法,这里一定要注意COM对象的手动释放(包括其他任何情况下IQueryFiter、ICursour都必须手动释放,否则容易造成内存溢出)

/// 
        /// 实现要素图层属性唯一值设色
        /// 
        /// 需要设色的要素图层
        /// 唯一值的属性字段
        private void UniqueValueRenderer(IFeatureLayer pFeatureLayer, string fieldName)
        {
            IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
            ITable pTable = pFeatureLayer as ITable;
            int fieldNumber = pTable.FindField(fieldName);
            IUniqueValueRenderer pUniqueRenderer = new UniqueValueRendererClass();
            pUniqueRenderer.FieldCount = 1;
            pUniqueRenderer.set_Field(0, fieldName);

            //设置显示颜色的范围
            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(pFeatureLayer.FeatureClass, fieldName).Count;
            bool bSucess = false;
            pRandColorRamp.CreateRamp(out bSucess);

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

            //属性唯一值
            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.AddField(fieldName);
            ICursor pCursor = pFeatureLayer.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 (pGeoFeatureLayer.FeatureClass.ShapeType)
                {
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                        pMarkerSymbol = new SimpleMarkerSymbolClass();
                        pMarkerSymbol.Color = pNextUniqueColor;
                        pUniqueRenderer.AddValue(codeValue.ToString(), "", pMarkerSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                        pLineSymbol = new SimpleLineSymbolClass();
                        pLineSymbol.Color = pNextUniqueColor;
                        pUniqueRenderer.AddValue(codeValue.ToString(), "", pLineSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        break;
                    case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                        pFillSymbol = new SimpleFillSymbolClass();
                        pFillSymbol.Color = pNextUniqueColor;
                        pUniqueRenderer.AddValue(codeValue.ToString(), "", pFillSymbol as ISymbol);
                        pNextRow = pCursor.NextRow();
                        int k = pFillSymbol.Color.CMYK;
                        break;
                    default:
                        break;
                }
            }
            pGeoFeatureLayer.Renderer = pUniqueRenderer as IFeatureRenderer;

            //必须手动清除COM对象,否则会造成内存溢出(尤其是IQueryFilter,ICursor)
            Marshal.ReleaseComObject(pQueryFilter);
            Marshal.ReleaseComObject(pCursor);
            Marshal.ReleaseComObject(pEnumColors);
        }

5、代码下载

github: ArcEngine实现要素图层唯一值符号化

你可能感兴趣的:(ArcEngine,ArcEngine,唯一值渲染,符号化)