Select to view content in your preferred language

Label by attribute

573
3
12-21-2022 05:40 AM
Daniel4
Emerging Contributor

Hi Everbody

I am trying to label a FeatureLayer with values from an attribute, but somehow it fails. I have calculated the z-value and want to label that, but for some reason it label another attribute. 

In the CIMLabelClass I define the Expression to be the 'zValue' attribute:

var labelFeatureLayer = new CIMLabelClass
                        {
                            Name = "labelFeatureLayer",
                            ExpressionEngine = LabelExpressionEngine.Arcade,
                            Expression = "$feature.zValue",
                            TextSymbol = SymbolFactory.Instance.ConstructTextSymbol().MakeSymbolReference(),
                            Visibility = true
                        };


                        var lyrDefn = fcLayer.GetDefinition() as CIMFeatureLayer;
                        var listLabelClasses = lyrDefn.LabelClasses.ToList();
                        listLabelClasses.Add(labelFeatureLayer);
                        lyrDefn.LabelClasses = listLabelClasses.ToArray();
                        fcLayer.SetDefinition(lyrDefn);

                        fcLayer.SetLabelVisibility(true);

 

But its just showing a different attribute (in this case its the objectid)

Daniel4_0-1671629918077.png

 

 

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

I think you need to switch off visibility for other existing label classes. Look at Esri community sample LabelLineFeatures 

                //Turn off all the label classes except this one
                foreach (var lc in listLabelClasses)
                {
                    lc.Visibility = lc.Name == "labelFeatureLayer" ? true : false;
                }
0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

The following snippet worked for me:

protected override async void OnClick()
{
  var fcLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name == "TestPoints").FirstOrDefault();
  await QueuedTask.Run(() =>
  {
    List<CIMLabelClass> newLabels = new List<CIMLabelClass>()
    { new CIMLabelClass
      {
        Name = "labelFeatureLayer",
        ExpressionEngine = LabelExpressionEngine.Arcade,
        Expression = "Geometry($feature).z",
        TextSymbol = SymbolFactory.Instance.ConstructTextSymbol().MakeSymbolReference(),
        Visibility = true
      }
    };
    var lyrDefn = fcLayer.GetDefinition() as CIMFeatureLayer;
    lyrDefn.LabelClasses = newLabels.ToArray();
    fcLayer.SetDefinition(lyrDefn);
    fcLayer.SetLabelVisibility(true);
  });
}

@GKmieliauskas  is correct as there can be multiple labels with visibility.   The snippet above removes all but the new label from the array of labels.

0 Kudos
Daniel4
Emerging Contributor

Hi, thanks. 

I tried with your code @Wolf but ArcGIS Pro doesn't  show the label. 

The label tab dont show the new label class:

Daniel4_0-1672660750002.png

But if I manual in ArcGis Pro create a label class (eg. 'test'), suddenly the new label class appires:

Daniel4_1-1672660974167.png

 

Do I need to delete some Label Classes before? Or do I need do to something with 'Class 1'?

 

 

0 Kudos