CIMUniqueValueRenderer是ArcGIS Pro SDK中的一个类,用于创建唯一值渲染器(Unique Value Renderer)。
在ArcGIS Pro中长这样:
通过对CIMUniqueValueRenderer的操作,可以对符号系统进行更改,实现很多功能。
1、获取CIMUniqueValueRenderer
cs
// 获取CIMUniqueValueRenderer
CIMUniqueValueRenderer uvr = featureLayer.GetRenderer() as CIMUniqueValueRenderer;
2、获取符号项
cs
// 获取符号项
CIMUniqueValueClass[] uvClasses = uvr.Groups[0].Classes;
3、逐行对符号项进行操作
cs
// 逐行对符号项进行操作
foreach (CIMUniqueValueClass uvClass in uvClasses)
{
// TODO
}
4、获取符号项的值
cs
// 获取符号项的值
string va = uvClass.Values[0].FieldValues[0].ToString();
5、获取、赋值符号项的标注
cs
// 获取符号项的标注
string lab = uvClass.Label;
// 给符号项的标注赋值
uvClass.Label = "飘游";
6、对符号项进行筛选
cs
// 对符号项进行筛选
uvr.Groups[0].Classes = uvClasses.Where(x =>dic01.ContainsKey(x.Values[0].FieldValues[0].ToString())).ToArray();
7、通过lyrx文件获取到CIMUniqueValueRenderer
cs
// 通过lyrx文件获取到CIMUniqueValueRenderer
LayerDocument lyrFile = new LayerDocument(def_path + @"\三调用地fin.lyrx");
CIMLayerDocument cimLyrDoc = lyrFile.GetCIMLayerDocument();
CIMUniqueValueRenderer uvr = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMUniqueValueRenderer;
8、设置CIMUniqueValueRenderer的值字段
cs
// 设置CIMUniqueValueRenderer的值字段
uvr.Fields = new string[] { "DLBM" };
9、给图层应用渲染器
cs
// 给图层应用渲染器
ly.SetRenderer(uvr);
10、通过字典修改符号项的标注
cs
// 获取当前地图
var map = MapView.Active.Map;
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取CIMUniqueValueRenderer
CIMUniqueValueRenderer uvr = featureLayer.GetRenderer() as CIMUniqueValueRenderer;
// 获取映射字段
string mapFieldName = uvr.Fields.FirstOrDefault();
// 获取字段值映射表
Dictionary<string, string> dic01 = new Dictionary<string, string>();
Dictionary<string, string> dic02 = new Dictionary<string, string>();
if (field01 != "")
{
dic01 = featureLayer.Get2FieldValueDic(mapFieldName, field01);
}
if (field02 != "")
{
dic02 = featureLayer.Get2FieldValueDic(mapFieldName, field02);
}
CIMUniqueValueClass[] uvClasses = uvr.Groups[0].Classes;
// 修改每个标注类别的表达式
foreach (CIMUniqueValueClass uvClass in uvClasses)
{
// 映射字段值
string va = uvClass.Values[0].FieldValues[0].ToString();
// 获取标注字段值
string fieldValue01 = "";
string fieldValue02 = "";
if (dic01.ContainsKey(va))
{
fieldValue01 = dic01[va];
}
if (dic02.ContainsKey(va))
{
fieldValue02 = dic02[va];
}
// 标注
uvClass.Label = txtFront + fieldValue01 + fieldValue02 + txtBack;
}
pw.AddProcessMessage(40, time_base, "删除计数值为0的行");
// 删除计数值为0的行
uvr.Groups[0].Classes = uvClasses.Where(x => dic01.ContainsKey(x.Values[0].FieldValues[0].ToString())).ToArray();
// 应用渲染器
featureLayer.SetRenderer(uvr);
11、通过lyrx文件应用符号系统给图层
cs
// 应用符号系统
LayerDocument lyrFile = new LayerDocument(def_path + @"\三调用地fin.lyrx");
CIMLayerDocument cimLyrDoc = lyrFile.GetCIMLayerDocument();
CIMUniqueValueRenderer uvr = ((CIMFeatureLayer)cimLyrDoc.LayerDefinitions[0]).Renderer as CIMUniqueValueRenderer;
uvr.Fields = new string[] { "DLBM" };
// 修改每个标注类别的表达式
foreach (CIMUniqueValueClass uvClass in uvr.Groups[0].Classes)
{
var va = uvClass.Values[0].FieldValues[0].ToString();
uvClass.Label = va + DataLib.dic_sdAll[va];
}
// 应用渲染器
ly.SetRenderer(uvr);
12、创建一个CIMUniqueValueRenderer并应用给图层
cs
var flyr = MapView.Active.GetSelectedLayers()[0] as FeatureLayer;
// 设置唯一值的字段
var fields = new List<string> { "Type" };
// 构造一个点符号作为模板符号
CIMPointSymbol pointSym = SymbolFactory.Instance.ConstructPointSymbol(
ColorFactory.Instance.GreenRGB, 16.0, SimpleMarkerStyle.Pushpin);
CIMSymbolReference symbolPointTemplate = pointSym.MakeSymbolReference();
// 构造一个UniqueValueRendererDefinition
UniqueValueRendererDefinition uniqueValueRendererDef =
new UniqueValueRendererDefinition(fields, symbolPointTemplate);
// 创建一个唯一值渲染器
CIMUniqueValueRenderer uniqueValueRenderer = flyr.CreateRenderer(uniqueValueRendererDef) as CIMUniqueValueRenderer;
// 应用到图层
flyr.SetRenderer(uniqueValueRenderer);