该着色方法一句着色图层中要素类的某个数值字段的属性值,按这个属性值为每种不同值得要素单独分配一种显示符号样式。关键在于获取该字段所有要素的唯一值(即将所有在该字段中属性值相同的要素归为一种),利用UniqueValueRenderer的AddValue方法即可进行渲染
需要添加的类库
1 using System.Collections; 2 using System.Windows.Forms; 3 using ESRI.ArcGIS.Display; 4 using ESRI.ArcGIS.Carto; 5 using ESRI.ArcGIS.esriSystem; 6 using ESRI.ArcGIS.Geodatabase;
具体代码(本例为单值渲染)
1 private ISymbol GetSymbol(IColor pColor) 2 { 3 ISymbol pSymbol; 4 ISimpleFillSymbol pSymbolFillSymbol = new SimpleFillSymbolClass(); 5 pSymbolFillSymbol.Color = pColor; 6 pSymbolFillSymbol.Outline.Width = 0.4; 7 pSymbol = pSymbolFillSymbol as ISymbol; 8 return pSymbol; 9 } 10 private void uniqueValueRendererToolStripMenuItem_Click(object sender, EventArgs e) 11 { 12 IMap pMap = this.axMapControl1.ActiveView.FocusMap; 13 if (pMap.LayerCount== 0) 14 { 15 MessageBox.Show("地图为空,请加载地图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); 16 return; 17 } 18 IGeoFeatureLayer pGeoLayer = this.axMapControl1.get_Layer(0) as IGeoFeatureLayer ; 19 ITable pTable = pGeoLayer.FeatureClass as ITable; 20 ICursor pCursor; 21 IQueryFilter pQueryFilter = new QueryFilter(); 22 pQueryFilter.AddField("PERIMETER"); 23 pCursor = pTable.Search(pQueryFilter, true);//获取字段 24 IEnumerator pEnumreator; 25 26 //获取字段中各要素属性唯一值 27 IDataStatistics pDataStatistics = new DataStatisticsClass(); 28 pDataStatistics.Field = "PERIMETER";//获取统计字段 29 pDataStatistics.Cursor = pCursor; 30 pEnumreator = pDataStatistics.UniqueValues; 31 int fieldcount = pDataStatistics.UniqueValueCount;//唯一值个数,以此确定颜色带范围 32 33 IUniqueValueRenderer pUniqueValueR = new UniqueValueRendererClass(); 34 pUniqueValueR.FieldCount = 1;//单值渲染 35 pUniqueValueR.set_Field(0, "PERIMETER");//渲染字段 36 IEnumColors pEnumColor = GetColorRamp(fieldcount).Colors; 37 pEnumColor.Reset(); 38 39 while (pEnumreator.MoveNext()) 40 { 41 string value = pEnumreator.Current.ToString(); 42 if (value != null) 43 { 44 IColor pColor = pEnumColor.Next(); 45 ISymbol pSymbol = GetSymbol(pColor); 46 pUniqueValueR.AddValue(value, "PERIMETER", pSymbol); 47 //pUniqueValueR.set_Symbol(value, pSymbol); 48 49 } 50 51 } 52 pGeoLayer.Renderer = pUniqueValueR as IFeatureRenderer; 53 this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); 54 this.axTOCControl1.Update(); 55 56 } 57 private IRandomColorRamp GetColorRamp(int size) 58 { 59 IRandomColorRamp pRandomColorRamp = new RandomColorRampClass(); 60 pRandomColorRamp.StartHue = 10; 61 pRandomColorRamp.EndHue = 300; 62 pRandomColorRamp.MaxSaturation =100; 63 pRandomColorRamp.MinSaturation = 0; 64 pRandomColorRamp.MaxValue = 100; 65 pRandomColorRamp.MinValue = 0; 66 pRandomColorRamp.Size = size; 67 bool ok = true; 68 pRandomColorRamp.CreateRamp(out ok); 69 return pRandomColorRamp; 70 }
其中datastatistic和uniqueValueRenderer的field必须要明确,且为相同值