大家好,又见面了,我是你们的朋友全栈君。
ArcGIS二次开发基础教程(07):简单符号及图层渲染
简单渲染
0. 点渲染
IGeoFeatureLayer GetLayerByName(string name)
{
ILayer layer = null;
for(int i=0;i<axMapConTrol1.LayerCount;i++)
{
layer = axMapControl1.get_Layer(i);
if(layer.Name.Equals(name))
return layer as IGeoFeatureLayer;
//这里转换为IGeoFeatureLayer是为了方便添加渲染器
}
return null;
}
//获取图层
IGeoFeatureLayer geoFeatureLayer = GetLayerByName("图层名称");
//创建简单渲染器
ISimpleRenderer renderer = new SimpleRendererClass();
//创建并设置形状的样式、颜色和大小
ISimleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
IRGBColor color = new RGBClass();
color.RGB = 251;
simpleMarkerSymbol.Color = color;
simpleMarkerSymbol.Style = esriSimleMarkerStyle.esriSMSX;
simpleMarkerSymbol.Size = 10;
renderer.Symbol = simpleMarkerSymbol as ISymbol;
//设置图层渲染器
geoFeatureLayer.Renderer = renderer;
axMapControl1.Refresh();
axTOCControl1.Update();
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<classes.Count;i++)
{
//设置渐变符号为填充,颜色由创建好的颜色带提供
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.COlor = enumColor.Next();
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
//设置节点和符号
classBreaksRenderer.set_Break(i,classes[i]);
classBreaksRenderer.set_Symbol(i,simpleFillSymbol as ISymbol);
}
if(geoFeatureLayer!=null)
{
//设置渲染器
geoFeatureLayer.Renderer = classBreaksRenderer as IFeatrueRenderer;
}
axMapControl1.Refresh();
axTOCControl1.Update();
}
唯一值和分等级是最常用的图层渲染法,当然还有很多不同的渲染法,如符号大小渲染法,单一值渲染法,大同小异不一一介绍。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163434.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...