CIMLabelClass(Cartographic Information Model Label Class)是ArcGIS Pro SDK中的一个类。
它主要用于定义标签的样式和属性,如字体、大小、颜色、对齐方式等,以及标签的排列和布局规则。
1、获取当前地图的标签引擎
cs
// 获取当前地图的定义【CIMMap】
CIMMap cimMap = MapView.Active.Map.GetDefinition();
// 从【CIMMap】中获取标签引擎
CIMGeneralPlacementProperties labelEngine = cimMap.GeneralPlacementProperties;
2、修改当前地图的标签引擎
cs
// 获取当前地图的定义【CIMMap】
CIMMap cimMap = MapView.Active.Map.GetDefinition();
// 从【CIMMap】中获取标签引擎
CIMGeneralPlacementProperties labelEngine = cimMap.GeneralPlacementProperties;
// 创建一个新的Maplex标签引擎
CIMMaplexGeneralPlacementProperties mgp = new CIMMaplexGeneralPlacementProperties();
// 设置标签引擎
cimMap.GeneralPlacementProperties = mgp;
// 应用到地图设置中
MapView.Active.Map.SetDefinition(cimMap);
3、将文字符号TextSymbol应用到要素图层
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 创建一个CIMTextSymbol
CIMTextSymbol textSymbol = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlueRGB, 8, "Corbel", "Regular");
// 设置文字样式TextSymbol
theLabelClass.TextSymbol.Symbol = textSymbol;
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
featureLayer.SetLabelVisibility(true);
4、更改标签位置------点
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
theLabelClass.StandardLabelPlacementProperties.PointPlacementMethod = StandardPointPlacementMethod.OnTopPoint;
}
else
{
theLabelClass.MaplexLabelPlacementProperties.PointPlacementMethod = MaplexPointPlacementMethod.CenteredOnPoint;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
featureLayer.SetLabelVisibility(true);
5、更改标签位置------线
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
var lineLablePosition = new CIMStandardLineLabelPosition
{
Perpendicular = false, // 垂直
Parallel = false, // 平行
ProduceCurvedLabels = true, // 曲线
Horizontal = false, // 水平
OnTop = false // 位于上方
};
theLabelClass.StandardLabelPlacementProperties.LineLabelPosition = lineLablePosition;
}
else
{
theLabelClass.MaplexLabelPlacementProperties.LinePlacementMethod = MaplexLinePlacementMethod.CenteredPerpendicularOnLine;
theLabelClass.MaplexLabelPlacementProperties.LineFeatureType = MaplexLineFeatureType.General;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }
6、更改标签位置------面
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注位置
// 根据标签引擎的不同,需要分别处理
CIMGeneralPlacementProperties labelEngine = MapView.Active.Map.GetDefinition().GeneralPlacementProperties;
if (labelEngine is CIMStandardGeneralPlacementProperties)
{
theLabelClass.StandardLabelPlacementProperties.PolygonPlacementMethod =
StandardPolygonPlacementMethod.AlwaysHorizontal;
theLabelClass.StandardLabelPlacementProperties.PlaceOnlyInsidePolygon = true;
}
else
{
theLabelClass.MaplexLabelPlacementProperties.PolygonFeatureType =
MaplexPolygonFeatureType.LandParcel;
theLabelClass.MaplexLabelPlacementProperties.AvoidPolygonHoles = true;
theLabelClass.MaplexLabelPlacementProperties.PolygonPlacementMethod =
MaplexPolygonPlacementMethod.HorizontalInPolygon;
theLabelClass.MaplexLabelPlacementProperties.CanPlaceLabelOutsidePolygon = true;
}
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }
7、更改对齐方式【Maplex】
cs
// 点、面
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignment = true;
theLabelClass.MaplexLabelPlacementProperties.GraticuleAlignmentType = MaplexGraticuleAlignmentType.Curved;
// 线
theLabelClass.MaplexLabelPlacementProperties.AlignLabelToLineDirection = true;
8、更改标注旋转方式【Maplex】
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注,转为List
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 更新标注旋转
CIMMaplexRotationProperties rotationProperties = new CIMMaplexRotationProperties
{
Enable = true,
RotationField = "ELEVATION", // 按字段
AdditionalAngle = 15, // 角度
RotationType = MaplexLabelRotationType.Arithmetic,
AlignmentType = MaplexRotationAlignmentType.Perpendicular,
AlignLabelToAngle = true
};
// 应用属性设置
theLabelClass.MaplexLabelPlacementProperties.RotationProperties = rotationProperties;
// 将listLabelClasses转为Array
lyrDefn.LabelClasses = listLabelClasses.ToArray();
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }
9、更改标注表达式
cs
// 获取图层
FeatureLayer featureLayer = MapView.Active.GetSelectedLayers().FirstOrDefault() as FeatureLayer;
// 获取图层定义
CIMFeatureLayer lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
// 获取标注
var listLabelClasses = lyrDefn.LabelClasses.ToList();
CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();
// 设置标注语言为python
theLabelClass.ExpressionEngine = LabelExpressionEngine.Python;
// 设置标注内容
string code = $"import...... ";
theLabelClass.Expression = code;
// 应用标注设置
lyrDefn.LabelClasses[0] = theLabelClass; // 假设只有一个标注类别
// 应用标注
featureLayer.SetDefinition(lyrDefn);
// 打开标注
if (!featureLayer.IsLabelVisible) { featureLayer.SetLabelVisibility(true); }