0

I thought the arcpy.mapping.ListLayers function in ArcGIS 10.1 was supposed to return group layers AND all of their sub-layers, but my script is only outputting the first sub-layer in a group layer. Can anyone see why by looking at my code? Thanks! (p.s. the purpose of my code is to output a text file - code omitted - that lists all of the layers in a specified directory and the layers' properties).

In ArcGIS 10.1 Desktop Help it says:

Group layers are treated just like layers. The index values are simply generated from top to bottom as they appear in the table of contents or the way they would appear in a layer file.

Code:

layerList = arcpy.ListFiles("*.lyr")
for layer in layerList:
    path = os.path.join(directory, layer)
    lyrFile = arcpy.mapping.Layer(path)

    for lyr in arcpy.mapping.ListLayers(lyrFile):

        try:
            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and lyr.supports("DEFINITIONQUERY") and lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, str(lyr.transparency), str(lyr.visible), lyr.definitionQuery, str(lyr.showLabels), str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, str(lyr.transparency), str(lyr.visible), lyr.definitionQuery, " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and not lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, " ", " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if not lyr.supports("DATASETNAME") and not lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, " ", " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

        except KeyError:
            pass

1 Answer 1

5

Your loop is apparently terminating after the first pass. What if you remove the 'break's and replace the 'if's with 'elif's?

2
  • Works! Thanks a lot. I started out with an IF/ELIF statement. I'm not sure why I switched to IFs with breaks. Still learning :)
    – Kristen G.
    Commented Jan 10, 2014 at 16:10
  • 1
    If you remove the 'break's, it should work with 'if's only as your conditions are exclusive, but your code will be neater with 'elif's...
    – GISGe
    Commented Jan 11, 2014 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.