1

I have a MXD with many group which has many layers. I have tried to get coordinate system data from all layers with the code from Iterating through layers in mxd and describing coordinate system. So far no luck.

I have tried with this code. I also tried with comments on the given answer but no luck. This is my code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd, '')[0] 
layerlist = arcpy.mapping.ListLayers
for layer in layerlist(mxd, '', df): # Loop through layers
    sr = arcpy.Describe(layer).spatialReference
    if spatialReference.type == "Unknown":
        print layer + " has an unknown spatial reference\n"
    else:
        print layer.name + sr.name + "\n"
del mxd

And this is error I get.

Runtime error 
Traceback (most recent call last):
  "<string>", line 6, in <module>
 AttributeError: DescribeData: Method spatialReference does not exist

And if I try this code, from official ESRI site, nothing happens:

import arcpy
arcpy.env.workspace = "C:/PATH_TO_FOLDER/"
feature_classes = arcpy.ListFeatureClasses()
for fc in feature_classes:
    spatial_ref = arcpy.Describe(fc).spatialReference
    if spatial_ref.name == "Unknown":
        print("{0} has an unknown spatial reference".format(fc))
    else:
        print("{0} : {1}".format(fc, spatial_ref.name))
del mxd
  • 2
    The first code block should have an undefined variable error. You're ignoring the fact that not all layers have data sources (GroupLayers in particular). Please ask only one question per Question. – Vince Nov 29 '19 at 11:28
  • To compliment the comment made by @Vince - you might have broken data sources? You don't trap for that scenario either. – Hornbydd Nov 29 '19 at 12:43
  • I dont think the dataset is broken, since I am working with test data. – Dean7 Nov 29 '19 at 12:45
1

You have a number of issues with your script which will either cause issues now, or will cause issues when you reach Python 3.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd, '')[0] 
for layer in arcpy.mapping.ListLayers(mxd, None, df): # function variable elimination

    # Ignore layers without local data
    if (layer.isGroupLayer or layer.isServiceLayer):
        continue

    # Skip layers with invalid source
    if (layer.isBroken):
        print("Warning: Layer '{:s}' source is broken!".format(layer.name))
        continue

    sr = arcpy.Describe(layer).spatialReference
    # Avoid undefined variable
    if (sr.type == "Unknown"):
        # print is function at Python 3; use string.format()
        print("Warning: Layer '{:s}' has an unknown spatial reference\n".format(layer.name))
    else:
        # print is function at Python 3; use string.format()
        print("{:30s}: {:s}\n".format(layer.name,sr.name))

del mxd
  • it works! i also learned how to modify the print area. Cool. Thank you very much. – Dean7 Dec 2 '19 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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