Pro and Enumerating Layers: IEnumLayer

662
3
12-13-2021 06:03 AM
LaurenPeckman
New Contributor III

I'm combing through ArcPro snippets in GitHub, and I'm still having a very hard time transitioning to the Pro syntax. 

It was very simple, in ArcGIS for Desktop, to: 
-create a UID class
-assign a value
-enumerate layers, filtering by said UIDClass value

UIDClass uIDClass = new UIDClass();
uIDClass.Value = "{setValueHere}"; 
IEnumLayer layers = Document.FocusMap.get_Layers(uIDClass, true);

In Pro, I'm having the hardest time even finding how to enum layers! I see a LOT about GetLayersAsFlattenedList(), but I can't figure out HOW to use it for my purpose

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

 It's very  simple in ArcGIS Pro too.

For example you need only feature layers:

 

 

var featLayers = map.GetLayersAsFlattenedList().OfType<FeatureLayer>();

 

 

More useful things you can do with linq (do not forget add using System.Linq;)

For example you need to get all feature layers which are named "MyLayer":

 

var myLayers = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(x => string.Compare(x.Name, "MyLayer", true) == 0);

 

Next one all polygon type layers:

 

                    var polygonLayers = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(
                        l => l.ShapeType == esriGeometryType.esriGeometryPolygon).ToList();

 

 

0 Kudos
LaurenPeckman
New Contributor III

Thanks for the speedy reply! I have follow-up questions:
But doesn't your example, var featLayers, only return 1 layer?  The first in the flattened list? As it's just assigned as variable var, and not anything explicitly ESRI like IEnumLayer, I suppose I'm still a little unclear on that.

When I try your example, I see that I can somehow select AsEnumerable, and maybe that will do ... something. Whereas, with IEnumLayer, I merely needed ".Next()" 

And I have absolutely no idea where to find syntax for creating a UIDClass. I'm searching & searching through the API Reference Guide and the Pro Snippets in GitHub, and I'm having a hard time not just flailing about!

Some of the places I've been looking, to find any examples at all of people enumerating layers, or using UIDClass (or whatever the equivalent might be):
https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/index.html#topic1.html

https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets#mapexploration-snippets


0 Kudos
GKmieliauskas
Esri Regular Contributor

To get one layer you need to use List methods FirstOrDefault or First  and you will get one layer:

                var myLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(x => string.Compare(x.Name, "MyLayer", true) == 0).FirstOrDefault();

Each enumerable you can convert to list using ToList() method. Then you can use all List class methods . Using foreach syntax you can go through list members:

var allFeatLayers = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList(); 
foreach (var layer in allFeatLayers)
{
    // Do something with layer
}

 

0 Kudos