ArcGIS二次开发基础教程(07):简单符号及图层渲染

ArcGIS二次开发基础教程(07):简单符号及图层渲染

简单渲染

0. 点渲染

IGeoFeatureLayer GetLayerByName(string name)
{
    ILayer layer = null;
    for(int i=0;i

1. 线渲染

//线渲染和点渲染类似
IGeoFeatueLayer geoFeatureLayer = GetLayerByName("图层名称");
ISimpleRenderer renderer = new SimpleRendererClass();
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
IRGBColor color = new RGBColorClass();
color.RGB = 251;
simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot;
simpleLineSymbol.Color = color;
simpleLineSymbol.Width = 2;
renderer.Symbol = simpleLineSymbol as ISymbol;
geoFeatureLayer.Renderer = renderer;
axMapControl1.Refresh();
axTOCControl1.Update();

2. 面渲染

IGeoFeatureLayer geoFeatureLayer = GeLayerByName("图层名称");
ISimpleRenderer renderer = new SimpleRendererClass();
//面相当于外轮廓线框加内部填充
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
//外轮廓线
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
IRGBColor color1 = new RGBColorClass();
color.RGB = 251;
//外轮廓线样式
simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot;
simpleLineSymbol.Color = color;
simpleLineSymbol.Width = 2;

simpleFillSymbol.Color = color;//此处线颜色和内部填充颜色一致,也可不一致
//内部填充样式
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSVertical;
simpleFillSymbol.Outline = simpleLineSymbol;
renderer.Symbol = simpleFillSymbol as ISymbol;
geoFeatureLayer.Renderer = renderer;
axMapControl1.Refresh();
axTOControl.Update();

唯一值渲染

//为图层要素类的某个字段所有不同要素属性值一个特定的颜色,区分不同。
//随机获取颜色值   参数i是随机数种子  防止颜色相同
IColor GetRandomColor(int i)
{
	Random ran = new Random(i);
    IRGBColor color = new RGBColorClass();
    color.Red = ran.Next(256);
    color.Green = ran.Next(256);
    color.Blue = ran.Next(256);
    return color;
}

void 唯一值渲染()
{
    //创建一个唯一值渲染器
    IUniqueValueRendere uniqueValueRenderer = new UniqueValueRendererClass();
    //用来渲染的字段数 最多三个  这里仅设置一个
    uniqueValueRenderer.FieldCount = 1;
    uniqueValueRenderer.set_Field(0,"用来渲染的字段名");
    //获取图层
    IFeaturelayer featureLayer = GetLayerByName("图层名") as IFeatureLayer;
    //无条件查询获得所有要素的起始光标
    IFeatureCursor featureCursor = featurelayer.FeatureClass.Search(null,true);
    IFeature feature = featureCursor.Next();
    ISimpleFillSymbol simpleFillSymbol = null;
    //遍历所有要素为唯一值渲染器添加值
    while(feature!=null)
    {
        int i = feature.get_value(0);
        string value = feature.get_Value(feature.Fields.FindField("用来渲染的字段名"));
        //渲染符号
        simpleFillSymbol = new SimpleFillSymbolClass();
        simpleFilleSymbol.Style = esriSimpleFillStyle.esriSFSSoild;
        simpleFillSymbol.Color = GetRandomColor(i);
        uniqueValueRenderer.AddValue(value,"用来渲染的字段名",simpleFillSymbol as iSymbol);
    }
    //设置图层的渲染器为唯一值渲染器
  	IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
    geoFeatureLayer.Renderer = uniqueValueRenderer as IFeatureRenderer;
    axMapControl1.Refresh();
    axTOCControl1.Update();
}

分等级渲染

//将图层要素类的某个字段的属性值按给定数量的等级划分,用渐变颜色表示不同等级。
//获得渐变颜色带
IAlgorithmicColorRamp CreateAlgorithmicRamp(int count)
{
    //创建渐变颜色带
    IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRampClass();
    colorRamp.size = count;//颜色数目
    //起始颜色对象
    IRGBColor fromColor = new RGBColorClass();
    fromColor.Red = 196;
    fromColor.Green = 10;
    fromColor.Blue = 10;
    //终止颜色对象
    IRGBColor toColor = new RGBColorClass();
    toColor.Red = 255;
    toColor.Green = 235;
    toColor.Blue = 214;
    colorRamp.ToColor = toColor;
    colorRamp.FromColor = fromColor;
    //梯度类型
    colorRamp.Algorithmic = esriColorRampAlgorithmic.esriCIELabAlgorithmic;
    bool ptrue = true;
    colorRamp.Create(out pture);
    return colorRamp;
}
void 分等级渲染()
{
	int count = 10;//分级数量
    //该变量用于控制从表格中生成的直方图类型
	ITableHistogram tableHistogram = new BasicTableHistogramClass()
	IGeoFearureLayer geoFeatureLayer = GetLayerByName("欲渲染的图层名称");
    //将图层属性转换为表格
	ITable table = ((ILayer)geoFeatureLayer) as ITable;
	tableHistogram.Table = table;
	tableHistogram.Field = "用来渲染的字段名";
    //该变量用于从不同数据源中生成的直方图
	IBasicHistogram basicHitogram = tableHistogram as IBasicHistogram;
    //先统计每个值出现的次数,结果赋予valus,frequences
	object values;
	object frequences;
	basicHistogram.GetHistogram(out values, out frequences);
    //IClassifyGEN接口实现了很多分类接口,这里使用分类数分类方法
	IClassifyGEN classifyGEN = new QuantileClass();
	classifyGEN.Classify(values,frequens,ref count);
    //获取分类节点数据
	double[] classes = classifyGEN.Breaks as double[];
    //定义分级渲染器并设置相关属性
	IClassBreaksRenderer classBreaksRenderer = new ClassBreaksRendererClass();
    classBreaksRenderer.BreakCount = classes.Count;
    classBreaksRenderer.Field = "用来渲染的字段名";
    //升序显示
    classBreaksRenderer.SortClassesAscending = true;
    //提供渐变色带
    IAlgorithmicColorRamp rampColor = CreateAlgorithmicRamp(count);
    IEnumColor enumColor = rampCOlor.COlors;
    for(int i=0;i

唯一值和分等级是最常用的图层渲染法,当然还有很多不同的渲染法,如符号大小渲染法,单一值渲染法,大同小异不一一介绍。

历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中

你可能感兴趣的:(ArcGIS二次开发,AE+C#)