使用ArcEngine9.3开发的栅格和矢量的渲染。
开发环境:ArcEngine9.3,VS2008。
功能:栅格(拉伸和分级)和矢量(简单、唯一值、分级、比例)渲染。
开发界面如图所示。
图1 主界面
图2 栅格渲染界面
图3 矢量渲染界面
部分源码:
按 Ctrl+C 复制代码
public void SimpleRenderer(IFeatureLayer featLayer, string fieldName, IColorRamp colorRamp)
{
IGeoFeatureLayer pGeoFeatureLayer = featLayer as IGeoFeatureLayer;
IFeatureClass pFeatureClass = featLayer.FeatureClass; //获取图层上的featureClass
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass(); //唯一值渲染器
//设置渲染字段对象
pUniqueValueRenderer.FieldCount = 1;
pUniqueValueRenderer.set_Field(0, fieldName);
ISimpleFillSymbol pSimFillSymbol = new SimpleFillSymbolClass(); //创建填充符号
pUniqueValueRenderer.DefaultSymbol = (ISymbol)pSimFillSymbol;
pUniqueValueRenderer.UseDefaultSymbol = false;
int n = pFeatureClass.FeatureCount(null);
for (int i = 0; i < n; i++)
{
IFeature pFeature = pFeatureCursor.NextFeature();
IClone pSourceClone = pSimFillSymbol as IClone;
ISimpleFillSymbol pSimpleFillSymbol = pSourceClone.Clone() as ISimpleFillSymbol;
string pFeatureValue = pFeature.get_Value(pFeature.Fields.FindField(fieldName)).ToString();
pUniqueValueRenderer.AddValue(pFeatureValue, "", (ISymbol)pSimpleFillSymbol);
}
//为每个符号设置颜色
for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++)
{
string xv = pUniqueValueRenderer.get_Value(i);
if (xv != "")
{
ISimpleFillSymbol pNextSymbol = (ISimpleFillSymbol)pUniqueValueRenderer.get_Symbol(xv);
pNextSymbol.Color = colorRamp.get_Color(127);
pUniqueValueRenderer.set_Symbol(xv, (ISymbol)pNextSymbol);
}
}
pGeoFeatureLayer.Renderer = (IFeatureRenderer)pUniqueValueRenderer;
}
public void UniqueRenderer(IFeatureLayer featLayer, string fieldName, IColorRamp colorRamp)
{
IGeoFeatureLayer pGeoFeatureLayer = featLayer as IGeoFeatureLayer;
IFeatureClass pFeatureClass = featLayer.FeatureClass; //获取图层上的featureClass
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass(); //唯一值渲染器
//设置渲染字段对象
pUniqueValueRenderer.FieldCount = 1;
pUniqueValueRenderer.set_Field(0, fieldName);
ISimpleFillSymbol pSimFillSymbol = new SimpleFillSymbolClass(); //创建填充符号
pUniqueValueRenderer.DefaultSymbol = (ISymbol)pSimFillSymbol;
pUniqueValueRenderer.UseDefaultSymbol = false;
int n = pFeatureClass.FeatureCount(null);
for (int i = 0; i < n; i++)
{
IFeature pFeature = pFeatureCursor.NextFeature();
IClone pSourceClone = pSimFillSymbol as IClone;
ISimpleFillSymbol pSimpleFillSymbol = pSourceClone.Clone() as ISimpleFillSymbol;
string pFeatureValue = pFeature.get_Value(pFeature.Fields.FindField(fieldName)).ToString();
pUniqueValueRenderer.AddValue(pFeatureValue, "", (ISymbol)pSimpleFillSymbol);
}
//为每个符号设置颜色
for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++)
{
string xv = pUniqueValueRenderer.get_Value(i);
if (xv != "")
{
ISimpleFillSymbol pNextSymbol = (ISimpleFillSymbol)pUniqueValueRenderer.get_Symbol(xv);
//pColor = ramp.get_Color(j * (ramp.Size - 1) / (count - 1));
pNextSymbol.Color = colorRamp.get_Color(i * (colorRamp.Size-1) / (pUniqueValueRenderer.ValueCount-1));
pUniqueValueRenderer.set_Symbol(xv, (ISymbol)pNextSymbol);
}
}
pGeoFeatureLayer.Renderer = (IFeatureRenderer)pUniqueValueRenderer;
}
public void ClassRenderer(IFeatureLayer featLayer, string fieldName, IColorRamp colorRamp, int count)
{
//值分级
IBasicHistogram pBasicHis = new BasicTableHistogramClass();
ITableHistogram pTabHis = (ITableHistogram)pBasicHis;
//贫困发生率
IClassifyGEN pClassify = new EqualIntervalClass();
pTabHis.Field = fieldName;
//IGeoFeatureLayer geolayer = (IGeoFeatureLayer)Layer;
ILayer Layer = (ILayer)featLayer;
ITable pTab = (ITable)Layer;
pTabHis.Table = pTab;
object doubleArrVal, longArrFreq;
pBasicHis.GetHistogram(out doubleArrVal, out longArrFreq);
int nDes = count;
pClassify.Classify(doubleArrVal, longArrFreq, ref nDes);
object classes = pClassify.ClassBreaks;
double[] ClassNum;
ClassNum = (double[])pClassify.ClassBreaks;
int ClassCountResult = ClassNum.GetUpperBound(0);
IClassBreaksRenderer pRender = new ClassBreaksRendererClass();
pRender.BreakCount = ClassCountResult;
pRender.Field = fieldName;
ISimpleFillSymbol pSym;
IColor pColor;
for (int j = 0; j < ClassCountResult; j++)
{
pColor = colorRamp.get_Color(j * (colorRamp.Size-1) / (ClassCountResult-1));
pSym = new SimpleFillSymbolClass();
pSym.Color = pColor;
pRender.set_Symbol(j, (ISymbol)pSym);
pRender.set_Break(j, ClassNum[j + 1]);
pRender.set_Label(j, ClassNum[j].ToString("0.00") + " - " + ClassNum[j + 1].ToString("0.00"));
}
IGeoFeatureLayer pGeoLyr = (IGeoFeatureLayer)Layer;
pGeoLyr.Renderer = (IFeatureRenderer)pRender;
}
public void ProportionalRenderer(IFeatureLayer featLayer, string fieldName, IColorRamp colorRamp, double count)
{
IProportionalSymbolRenderer psrender = new ProportionalSymbolRendererClass();
psrender.Field = fieldName;
psrender.ValueUnit = esriUnits.esriUnknownUnits;
psrender.ValueRepresentation = esriValueRepresentations.esriValueRepUnknown;
//选择渲染的样式,与颜色 minsymbol为比填内容,否则没有效果
ISimpleMarkerSymbol markersym = new SimpleMarkerSymbol();
markersym.Size = count;
markersym.Style = esriSimpleMarkerStyle.esriSMSCircle;
markersym.Color = colorRamp.get_Color(127);
psrender.MinSymbol = markersym as ISymbol;
//IFeatureLayer featLayer = featLayer;
IGeoFeatureLayer geofeat = featLayer as IGeoFeatureLayer;
ICursor cursor = ((ITable)featLayer).Search(null, true);
IDataStatistics datastat = new DataStatisticsClass();
datastat.Cursor = cursor;
datastat.Field = fieldName;//千万不能忽视
IStatisticsResults statisticsResult;
try
{
statisticsResult = datastat.Statistics;
psrender.MinDataValue = statisticsResult.Minimum + 0.1;
psrender.MaxDataValue = statisticsResult.Maximum;
}
catch
{
MessageBox.Show("错误,选择的属性不是数值型!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
////设置background的样式
IFillSymbol fillsym = new SimpleFillSymbolClass();
fillsym.Color = getcolor(201, 201, 251);
ILineSymbol linesym = new SimpleLineSymbolClass();
linesym.Width = 1;
fillsym.Outline = linesym;
psrender.BackgroundSymbol = fillsym;
psrender.LegendSymbolCount = 6;//legend的数量
psrender.CreateLegendSymbols();//创建TOC的legend
geofeat.Renderer = (IFeatureRenderer)psrender;
}
按 Ctrl+C 复制代码
完整源码下载:FeatureAndRasterRenderer.rar