Select to view content in your preferred language

How to display a custom label class for a feature layer

618
2
05-19-2022 11:04 AM
DaveLewis73
Occasional Contributor

I have an issue with labeling.  How do you set up a new label class for a feature layer and then set it to that layer to be viewed on the map? I have code that should work, but I can only create new label classes.  I am unable to properly associate them to my feature layer so that they can be seen.  As an fyi, I can check the label classes in the GUI and my new custom classes do show up.  The only way that I can see any labels though is to use the default label class.  I have deleted the default label class in the hopes that it will display my custom classes, but that doesn't work either.  Any help would be greatly appreciated.

The following is my current code:

private void CreateLabelProperties(string className, System.Drawing.Color color, string whereClause = "")
{
    CIMColor annoColor = CIMColor.CreateRGBColor(color.R, color.G, color.B, 100);
    CIMTextSymbol textSymbol = SymbolFactory.Instance.ConstructTextSymbol(annoColor, 12, "Arial", "Bold");
    CIMSymbolReference textSymRef = textSymbol.MakeSymbolReference();

    CIMFeatureLayer layerDef = _gridLayer.GetDefinition() as CIMFeatureLayer;
    List<CIMLabelClass> listLabelClasses = new List<CIMLabelClass>();
    if (layerDef.LabelClasses?.Length > 0)
    {
        listLabelClasses = layerDef.LabelClasses.ToList();
    }
    CIMLabelClass labelClass;
    if (className == "Default")
    {
        labelClass = layerDef.LabelClasses.ToList().FirstOrDefault();
    }
    else
    {
        labelClass = new CIMLabelClass();
        labelClass.Name = className;
        labelClass.ExpressionEngine = LabelExpressionEngine.Arcade;
        labelClass.Expression = string.Format("$feature.{0}", CellNameField);
        labelClass.WhereClause = whereClause;
        labelClass.Visibility = true;
        listLabelClasses.Add(labelClass);
    }
    labelClass.TextSymbol = textSymRef;
    CIMStandardLabelPlacementProperties standardLabelPlacementProps = new      CIMStandardLabelPlacementProperties
    {
        FeatureType = LabelFeatureType.Polygon
    };
    labelClass.StandardLabelPlacementProperties = standardLabelPlacementProps;
    layerDef.LabelClasses = listLabelClasses.ToArray();
    _gridLayer.AddLabelClass(className);
    _gridLayer.SetDefinition(layerDef);
    _gridLayer.SetLabelVisibility(true);
}

0 Kudos
2 Replies
DaveLewis73
Occasional Contributor

As an fyi, I am able to get my labels to appear if I remove the layer from the map and then undo the operation to bring it back in the map.  Obviously this is not a solution, but just worth noting.

0 Kudos
DaveLewis73
Occasional Contributor

I figured out the issue with the labels.  For anyone who is interested, the problem deals with instantiating Maplex label placement properties.  The following code needed to be added for the labels to appear:

CIMMaplexLabelPlacementProperties maplexLabelPlacementProps = new CIMMaplexLabelPlacementProperties
{
    FeatureType = LabelFeatureType.Polygon
};
labelClass.MaplexLabelPlacementProperties = maplexLabelPlacementProps;

 

0 Kudos