1

I have this nice code that goes through each dataframe in the current MXD, and through each layer in the dataframe, and generates a list containing the name of the dataframe, the name of the layer, and the name of the coordinate system of that layer, divided by commas. Something like this:

Focus Area,SubArea,South_Pole_Lambert_Azimuthal_Equal_Area Focus Area,SubArea_2,South_Pole_Lambert_Azimuthal_Equal_Area and so on.

The code is this:

import arcpy
mxd=arcpy.mapping.MapDocument("CURRENT")
dataframes=arcpy.mapping.ListDataFrames(mxd)
for dataframe in dataframes:
    for lyr in arcpy.mapping.ListLayers(mxd, "", dataframe):
        print(dataframe.name+","+lyr.name+","+arcpy.Describe(lyr).spatialReference.name)

The code works well if there are no Group Layers in the dataframes. If there is a group layer containing layers, the code will stop and throw this message:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\__init__.py", line 1234, in Describe
    return gp.describe(value)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe
    self._gp.Describe(*gp_fixargs(args, True)))
IOError: "Climatology and environment (L)" does not exist

So, "Climatology and environment (L)" is a group layer. In my understanding, the code is trying to find the "layer" and get its coordinate system, but there is no actual layer, so it just breaks. Do you think of any workaround to this? A way to ignore the group layers maybe?

1 Answer 1

6

You may check to see if the layer is a group layer or not using the isGroupLayer property:

Returns True if a layer is a group layer.

Layer (arcpy.mapping)

Code e.g.

for lyr in arcpy.mapping.ListLayers(mxd, "", dataframe):
    if not lyr.isGroupLayer:
        # continue

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.