Select to view content in your preferred language

use map tool to get the label of the selected feature

872
1
06-17-2021 03:42 AM
D_CGIS
by
Emerging Contributor

D_CGIS_0-1623926276623.png

Hello everyone, I have encountered some problems on gis pro. I want to use maptools to get the label name of the currently selected feature, but I can only get the collection of the feature layer label class.

thanks

0 Kudos
1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

In case you use the 'default' label option and not an expression the following maptool should work:

internal class SelectLabel : MapTool
{
	public SelectLabel()
	{
		IsSketchTool = true;
		SketchType = SketchGeometryType.Rectangle;
		SketchOutputMode = SketchOutputMode.Map;
	}

	protected override Task OnToolActivateAsync(bool active)
	{
		return base.OnToolActivateAsync(active);
	}

	protected override async Task<bool> OnSketchCompleteAsync(Geometry geometry)
	{
        var labels = await QueuedTask.Run<string>(() => 
        {
            // use the geometry to make a new selection
            var selectionSet = MapView.Active.SelectFeatures(geometry, SelectionCombinationMethod.New, true, true);
            if (selectionSet.Count == 0) return "nothing selected";
            // Layer of interest - use only the first layer in the collection
            // for this example. You can search for specific layer using Linq
            var selectedFeatureLayer = selectionSet.Keys.FirstOrDefault() as FeatureLayer;
            // Get the selected Oids
            var oids = selectionSet[selectedFeatureLayer];
            // get the CIM definition from the layer
            var cimFeatureDefinition = selectedFeatureLayer.GetDefinition() as ArcGIS.Core.CIM.CIMBasicFeatureLayer;
            // get the view of the source table underlying the layer
            var cimDisplayTable = cimFeatureDefinition.FeatureTable;
            // this field is used as the 'label' to represent the row
            var displayField = cimDisplayTable.DisplayField;

            // collect all 
            var sb = new StringBuilder();
            // search for all selected object ids
            using (RowCursor rowCursor = selectedFeatureLayer.Search(new QueryFilter() { ObjectIDs = oids }))
            {
                // Iterate through the selected features
                var labelFieldIndex = rowCursor.FindField(displayField);
                while (rowCursor.MoveNext())
                {
                    using (var row = rowCursor.Current) {
                        // Get the label field value
                        var labelForFeature = row[labelFieldIndex].ToString();
                        sb.AppendLine($@"oid: {row.GetObjectID()} label: {labelForFeature}");
                    }
                }
            }
            return sb.ToString();
        });
        MessageBox.Show(labels);
        return true;
	}
}

If the layer's label is defined by an expression (i.e. Arcade) there is no solution at this point. However, the Pro Dev team is working on providing script evaluation to the API in one of the upcoming releases.