1. 功能简介
分级渲染是矢量的一种数据表达方式。通过选取一个字段,并根据实际需要对字段的数据进行分级,并对每一级设置不同的符号,已达到区分显示的效果。
2. 功能实现说明
2.1 实现思路及原理说明
第一步 |
实例化矢量分级渲染对象 |
第二步 |
设置分级渲染的字段 |
第三步 |
设置分级数 |
第四步 |
设置不同分级的值及对应的符号 |
第五步 |
矢量渲染器接口转换 |
第六步 |
矢量图层渲染赋值 |
2.2 核心接口与方法
接口/类 |
方法 |
说明 |
Carto. IFeatureClassBreaksRender |
Field |
分级字段 |
ClassCount |
分级数 |
|
SetBreak () |
设置分级对应数值 |
|
SetSymbol() |
设置不同级别对应的符号 |
2.3 示例代码
项目路径 |
百度云盘地址下/PIE示例程序/07图层渲染/02.矢量分级渲染 |
数据路径 |
百度云盘地址下/PIE示例数据/矢量数据/Shape |
视频路径 |
百度云盘地址下/PIE视频教程/07图层渲染/02.矢量分级渲染.avi |
示例代码 |
|
1 方法(一) 2 if (mapControlMain.ActiveView.CurrentLayer == null) return; 3 IFeatureLayer featureLayer = mapControlMain.ActiveView.CurrentLayer as IFeatureLayer; 4 if (featureLayer == null) return; 5 //分级字段,根据实际情况进行修改 6 string classifyField = "ADCODE99"; 7 //获取图层对应要素类 8 IFeatureClass featureClass = featureLayer.FeatureClass; 9 //获取分级字段对应的索引 10 IFields fields = featureClass.GetFields(); 11 int index = fields.GetFieldIndex(classifyField); 12 13 //遍历要素准备,初始化游标 14 //IQueryFilter filter = new QueryFilter(); 15 IFeatureCursor cursor = featureClass.Search(null); 16 IFeature feature = cursor.NextFeature(); 17 18 //遍历要素获取最大值 19 double max = 0; 20 while (feature != null) 21 { 22 double value = Convert.ToDouble(feature.GetValue(index)); 23 if (value >= max) 24 { 25 max = value; 26 } 27 //遍历所有 28 feature = cursor.NextFeature(); 29 } 30 (cursor as IDisposable).Dispose(); 31 //创建FeatureClassBreaksRender,设置参数 32 IFeatureClassBreaksRender featureClassBreaksRender = new FeatureClassBreaksRender(); 33 featureClassBreaksRender.Field = classifyField; //设置渲染字段 34 featureClassBreaksRender.ClassCount = 3;//设置分级数 35 featureClassBreaksRender.SortClassesAscending = true; 36 37 //分级数、分级的值,根据实际情况自定义修改即可 38 //设置分级级别 39 featureClassBreaksRender.SetBreak(0, max / 3); 40 featureClassBreaksRender.SetBreak(1, (2 * max) / 3); 41 featureClassBreaksRender.SetBreak(2, max); 42 43 // 定义简单填充符号 44 IFillSymbol fillSymbol0 = new SimpleFillSymbol(); 45 fillSymbol0.Color = Color.FromArgb(255, 0, 0); 46 IFillSymbol fillSymbol1 = new SimpleFillSymbol(); 47 fillSymbol1.Color = Color.FromArgb(0, 255, 0); 48 IFillSymbol fillSymbol2 = new SimpleFillSymbol(); 49 fillSymbol2.Color = Color.FromArgb(0, 0, 255); 50 51 //设置分级符号 52 featureClassBreaksRender.SetSymbol(0, fillSymbol0); 53 featureClassBreaksRender.SetSymbol(1, fillSymbol1); 54 featureClassBreaksRender.SetSymbol(2, fillSymbol2); 55 56 featureLayer.Render = featureClassBreaksRender as IFeatureRender; 57 mapControlMain.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); |
2.4 示例截图