c#唯一值渲染实例代码
该着色方法一句着色图层中要素类的某个数值字段的属性值,按这个属性值为每种不同值得要素单独分配一种显示符号样式。关键在于获取该字段所有要素的唯一值(即将所有在该字段中属性值相同的要素归为一种),利用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必须要明确,且为相同值