Select to view content in your preferred language

Use custom "Text Symbol" style to format feature class labels

635
2
Jump to solution
09-21-2022 11:14 AM
JeromeHaaland
Occasional Contributor

The goal is to format feature class labels using a Text Symbol style item from a custom Style

static async Task SetToTextStyle(FeatureLayer featureLayer, string zValueField, string layerName)
{
    string style = "Style1";                
    string labelStyleName = "Label1";

    await QueuedTask.Run(() =>
    {  
        IList<SymbolStyleItem> labelStyleItem = GetLabelStyleFromStyle(Project.Current, style, labelStyleName);
        var labelStyle = labelStyleItem[0];

        // Need to go from a SymbolStyleItem above to a CIMTextSymbol below.

        CIMTextSymbol textSymbol = SymbolFactory.Instance.ConstructTextSymbol();

        CIMSymbolReference symbolReferance = textSymbol.MakeSymbolReference();

        CIMLabelClass labelClass = new CIMLabelClass()
        {
            ExpressionEngine = LabelExpressionEngine.Arcade,
            Expression = $"\"pH \" + textformatting.newline +  $feature.pH", //probably not correct format
            TextSymbol = symbolReferance,
            Visibility = true
        };
        CIMFeatureLayer layerDefinition = featureLayer.GetDefinition() as CIMFeatureLayer;

        // Code below taken from community sample
        // This code appears to do the same thing as line 17
        List<CIMLabelClass> listLabelClasses = layerDefinition.LabelClasses.ToList();
        CIMLabelClass theLabelClass = listLabelClasses.FirstOrDefault();

        featureLayer.SetDefinition(layerDefinition);
        featureLayer.SetLabelVisibility(true);                   
    });
}
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Your code snippet doesn't change the FeatureLayer definition, instead it only reads it.  

I found this snippet in our community samples that updates the labeling.  You can see how to update the layer definition (Esri/arcgis-pro-sdk-community-samples: ArcGIS Pro SDK for Microsoft .NET Framework Community Samples... ).:

private async Task<CIMLabelClass> CreateAndApplyLabelClassAsync(FeatureLayer featureLayer, string oidField, List<long> oids)
{

    var labelClass = await QueuedTask.Run(() =>
    {
        var labelSelectedFeaturesWithLength = new CIMLabelClass
        {
            Name = "LabelSelectedFeaturesWithLength",
            ExpressionEngine = LabelExpressionEngine.Arcade,
            Expression = "$feature.MILES",
            WhereClause = $"{oidField} IN ({String.Join(", ", oids.ToArray())})",
            TextSymbol = SymbolFactory.Instance.ConstructTextSymbol().MakeSymbolReference(),
            Visibility = true
        };
        var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
        var listLabelClasses = lyrDefn.LabelClasses.ToList();
        listLabelClasses.Add(labelSelectedFeaturesWithLength);
        lyrDefn.LabelClasses = listLabelClasses.ToArray();
        featureLayer.SetDefinition(lyrDefn);
        return labelSelectedFeaturesWithLength;
    });
    return labelClass;
}

 

View solution in original post

0 Kudos
2 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Your code snippet doesn't change the FeatureLayer definition, instead it only reads it.  

I found this snippet in our community samples that updates the labeling.  You can see how to update the layer definition (Esri/arcgis-pro-sdk-community-samples: ArcGIS Pro SDK for Microsoft .NET Framework Community Samples... ).:

private async Task<CIMLabelClass> CreateAndApplyLabelClassAsync(FeatureLayer featureLayer, string oidField, List<long> oids)
{

    var labelClass = await QueuedTask.Run(() =>
    {
        var labelSelectedFeaturesWithLength = new CIMLabelClass
        {
            Name = "LabelSelectedFeaturesWithLength",
            ExpressionEngine = LabelExpressionEngine.Arcade,
            Expression = "$feature.MILES",
            WhereClause = $"{oidField} IN ({String.Join(", ", oids.ToArray())})",
            TextSymbol = SymbolFactory.Instance.ConstructTextSymbol().MakeSymbolReference(),
            Visibility = true
        };
        var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
        var listLabelClasses = lyrDefn.LabelClasses.ToList();
        listLabelClasses.Add(labelSelectedFeaturesWithLength);
        lyrDefn.LabelClasses = listLabelClasses.ToArray();
        featureLayer.SetDefinition(lyrDefn);
        return labelSelectedFeaturesWithLength;
    });
    return labelClass;
}

 

0 Kudos
JeromeHaaland
Occasional Contributor

Wolf... Thank You very much.

I was missing one line.   Sometimes the obvious is right in front of us and we don't see it.

listLabelClasses.Add(labelSelectedFeaturesWithLength);