Select to view content in your preferred language

GetFeatures result hierarchy

173
1
Jump to solution
05-29-2024 09:12 AM
SteveCole
Frequent Contributor

Is there anyway to enforce a hierarchy on the results returned when using GetFeatures? For example, lets say I have a location with 3 features at a location- a road (line), a stream (line), and a bridge (point). Ideally, I'd like to have the results returned in the same order as my Table of Contents (bridge->road->stream) but, in reality, it's returning otherwise (road->bridge->stream).

Thanks!

Steve

0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor

To partially answer my own question, you can implement a SortedDictionary on the results of the mapTool click to at least create a sorted list based on your Table of Contents.

Dictionary<MapMember, List<long>> features = new Dictionary<MapMember, List<long>>();
features = mapView.GetFeatures(geometry).ToDictionary();

Dictionary<int, string> theHitList = new Dictionary<int, string>();

var counter = 0;
foreach (var feature in features)
{
       theHitList.Add(counter, feature.Key.Name);
       counter++;
}

var theLayerName = getTopLayer(theHitList, features.First().Key.Map);

 

The getTopLayer function with the SortedDictionary is in this function:

 

        private static string getTopLayer(Dictionary<int,string> clickList, Map theMap)
        {
            var layers = MapView.Active.Map.GetLayersAsFlattenedList(); // MapView.Active.Map.Layers.Where(layer => layer is FeatureLayer);
            SortedDictionary<int, string> sortedLayerList = new SortedDictionary<int, string>();

            foreach (var layer in clickList)
            {                
                var curLayerName = layer.Value;
                for (int i = 0; i < layers.Count; i++)
                {
                    if (layers[i].Name == curLayerName)
                    {
                        sortedLayerList.Add(i, layers[i].Name); 
                        break;
                    }
                }
            }
            return sortedLayerList.First().Value;
        }

 

I'm still learning C# so I'm sure there are more elegant and efficient ways to do this. In my use case, I'm labelling so I was really only concerned with the topmost layer that was clicked on using the mapTool. Nonetheless, the sortedDictionary is the mechanism to sort the information. all you do is just walk through the layers in the TOC, and add the clicked features' layer and TOC position number. So far, this has worked in my testing so I'm happy enough with this approach.

Steve

View solution in original post

0 Kudos
1 Reply
SteveCole
Frequent Contributor

To partially answer my own question, you can implement a SortedDictionary on the results of the mapTool click to at least create a sorted list based on your Table of Contents.

Dictionary<MapMember, List<long>> features = new Dictionary<MapMember, List<long>>();
features = mapView.GetFeatures(geometry).ToDictionary();

Dictionary<int, string> theHitList = new Dictionary<int, string>();

var counter = 0;
foreach (var feature in features)
{
       theHitList.Add(counter, feature.Key.Name);
       counter++;
}

var theLayerName = getTopLayer(theHitList, features.First().Key.Map);

 

The getTopLayer function with the SortedDictionary is in this function:

 

        private static string getTopLayer(Dictionary<int,string> clickList, Map theMap)
        {
            var layers = MapView.Active.Map.GetLayersAsFlattenedList(); // MapView.Active.Map.Layers.Where(layer => layer is FeatureLayer);
            SortedDictionary<int, string> sortedLayerList = new SortedDictionary<int, string>();

            foreach (var layer in clickList)
            {                
                var curLayerName = layer.Value;
                for (int i = 0; i < layers.Count; i++)
                {
                    if (layers[i].Name == curLayerName)
                    {
                        sortedLayerList.Add(i, layers[i].Name); 
                        break;
                    }
                }
            }
            return sortedLayerList.First().Value;
        }

 

I'm still learning C# so I'm sure there are more elegant and efficient ways to do this. In my use case, I'm labelling so I was really only concerned with the topmost layer that was clicked on using the mapTool. Nonetheless, the sortedDictionary is the mechanism to sort the information. all you do is just walk through the layers in the TOC, and add the clicked features' layer and TOC position number. So far, this has worked in my testing so I'm happy enough with this approach.

Steve

0 Kudos