Getting names of layers with selected features

222
8
03-19-2019 03:31 PM
AlanDodson
New Contributor III

I'd like to get the names of any layer that has at least 1 selected feature.  A function is building a bunch of selections, after which I want to find the names of those layers that have selections within to do more stuff.

I've tried:

for layer in layers:

  if len(layer.getSelectionSet()) > 0:

    print layer.name

which returns all the layers, even those with no selection.

I've also tried something very similar to:

for layer in layers:

  if layer.getFIDSet != '':

    print layer.name  

which gives me an error message.

I am a bit stumped.  I'll be using the output from this to create layer-specific messages based on if / elif statements, if that context helps.

Thank you!

Tags (1)
0 Kudos
8 Replies
RandyBurton
MVP Regular Contributor

You might try:

for layer in layers:
if layer.getSelectionSet() is not None:
print layer.name, len(layer.getSelectionSet())
0 Kudos
AlanDodson
New Contributor III

Unfortunately I'm getting an error on line 2 of that code:  'str' object has no attribute 'getSelectionSet'

Any ideas?  Is it finding strings when it shouldn't be?

0 Kudos
MatthewDriscoll
MVP Regular Contributor

Try this:

for layer in layers:
if layer.getSelectionSet() > 0:
print layer
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Given that error, how are you creating the layers list because my guess is that the list you are processing contains strings of layer names and not layer objects/references themselves.

AlanDodson
New Contributor III

The list where I got that error was strings of layer names, you are correct.  Sorry for not specifying that more clearly in the OP.  What would I need to change in this scenario to get the expected result, or is it too far gone now...

Later on in my script I need to run that again - this time it is running on layer objects returned from listing the layers in my dataframe and even though there are selected features within the layers, no result is given.  There is no error, but there is also no result.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To call Layer properties or methods, you will need to have references to Layer objects.  Since you don't show any code on how your layers list is created, I can't offer any suggestions on what to change.  Since you are creating a list of layer references/objects later in the script, and that is working, just use the same approach earlier in your script.

RandyBurton
MVP Regular Contributor

If  you are using ListLayers, you might want to take another look at the documentation.  From your question in a related thread, it appeared that you might only be referencing the active data frame.  ListLayers requires a map document reference and an optional reference to the data frame.  Hope this helps.

ListLayers (map_document_or_layer, {wildcard}, {data_frame})
AlanDodson
New Contributor III

Thanks, I will look at that.  I'm using a mix of hardcoded lists and ListLayers so I'm guessing that's where I'm running into trouble.  I just posted a long-winded reply in that other thread in case you'd like to know more about my warped little script 

0 Kudos