IFeatureLayer count for a complex group layer? Similar to IMap.Layers property

825
4
Jump to solution
10-08-2013 01:09 PM
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Hello,

This might be a lost cause, but worth a shot...

I'm curious about a potential functionality exactly like IMap.Layers, but instead of IMap, I want ICompositeLayer (or any reference to a group layer really).

I'm looking for the most elegant solution to getting a count of all IFeatureLayer layers, but only within a relatively complex group layer, not the entire map.

In my head, I would call ICompositeLayer.Layers(<insert IFeatureLayer UID here>, True), if it existed. But it doesn't, and I'm not sure if anything like it does.

Currently, the code I'm working with calls a function "GetNumLayers" which has a series of nested loops checking if a particular ILayer, is of type IFeatureLayer or not and adding +1 to the counter, and then returning the total. I'm convinced something faster exists as an alternative.

I think it would be a good learning experience, and to be honest, I'm really not worried about the time it takes, rather the "best" way to achieve this.

Any assistance would be much appreciated!
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
You can write a function that counts the feature layers in a given group layer by looping through the layers in that group layer.  For each layer, check the type.  If it's a feature layer, then add to the count.  If it's a group layer then call the function recursively and add it's return value to the layer count.  The function returns the count.

View solution in original post

0 Kudos
4 Replies
JohnStephens
Occasional Contributor
I am not sure if I 100% understand, but you are trying to get at the layers in an ICompositeLayer?

Can't you just get the Count and then loop through the layers using the Layer[index]?
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Sorry, hopefully this will help.

Unfortunately ICompositeLayer.Count would return a count similar to pMap.LayerCount wouldn't it?

The main group layer itself has multiple levels: sub-group layers and feature layers all kind of mixed in.

I'm looking for specifically: a count of feature layers (regardless of how deep they are). I don't want to include sub-group layers (that may contain feature layers), in that count. Only layers that are TypeOf IFeatureLayer.

Definitely an odd problem I'll admit that haha. Anyways, hopefully that clarifies things a little bit.

Thanks for the suggestion!
0 Kudos
NeilClemmons
Regular Contributor III
You can write a function that counts the feature layers in a given group layer by looping through the layers in that group layer.  For each layer, check the type.  If it's a feature layer, then add to the count.  If it's a group layer then call the function recursively and add it's return value to the layer count.  The function returns the count.
0 Kudos
deleted-user-VeC5jUIlNXtq
Occasional Contributor III
Hmmm, was hoping there would be a hidden property I wasn't aware of, oh well.

Currently, that's how the count is retrieved, though I didn't think about recursion, that's likely the faster method than a series of loops (also looks nicer).

Thanks Neil.
0 Kudos