Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm testing layers in the current document using the Describe function. My code is:

for lyr in arcpy.mapping.ListLayers(df):
     if(lyr.isGroupLayer == False and lyr.isRasterLayer == False):
         x = arcpy.Describe(str(lyr))

It's throwing an error on the annotation layer:

"LSDSectionBoundaryAnno\Default" does not exist

I'm thinking that the Describe function doesn't like annotation layers or the subclasses? Can I use Describe on annotation layers and if not, how would I filter out annotation layers? There doesn't seem to be a 'isAnnotationLayer' property.

Thanks, Mike

share|improve this question
add comment

2 Answers 2

Try something like this (untested):

for lyr in arcpy.mapping.ListLayers(df):
    if not (lyr.isGroupLayer or lyr.isRasterLayer):
        desc = arcpy.Describe(lyr)
        if desc.dataElement.dataType == "FeatureClass":
            if not (desc.featureType in ["Annotation", "CoverageAnnotation"]):
                # Not an annotation layer
share|improve this answer
add comment

Update for others based off of blah238's initial design:

If you want to use the "arcpy.Describe" property on an annotation layer; to get the Annotation layer file address.

This method uses a roundabout method to access some of the annotation properties.

In testing this worked, although the run time was very slow, future revisions/refinement welcome.

#
mxd = arcpy.mapping.MapDocument("current")
for lyr in arcpy.mapping.ListLayers(mxd):
    if (lyr.isGroupLayer): 
    #annotation layers behave as a group layer type

        try:
            desc = arcpy.Describe(lyr)
            if desc.dataElement.dataType == "FeatureClass":
                if (desc.featureType in ["Annotation", "CoverageAnnotation"]):
                    print "is an annotation layer"
                    print lyr.name
                    print lyr.dataSource
                else: 
                    print "nothing goes here"

        except:
            #additional error checking goes here
            pass
#

The below code has been moderately tested, and shows how to access the annotation layer file path from within a more realistic script. The design is little convoluted, and I welcome changes or edits to increase its' elegance.

This script prints datasources for layers, grouplayers and annotation layers. Depending upon the desired describe property the script may be modifiable to preform other annotation tasks.

mxd = arcpy.mapping.MapDocument("current")
group_array = []
for lyr in arcpy.mapping.ListLayers(mxd):
    if (lyr.isGroupLayer): 
    #annotation layers behave as a group layer type

        try:
            desc = arcpy.Describe(lyr)
            if desc.dataElement.dataType == "FeatureClass":
                if (desc.featureType in ["Annotation", "CoverageAnnotation"]):
                    print "is an annotation layer"
                    print lyr.name
                    print lyr.dataSource
                    group_array.append(lyr)
                else:
                    raise #forces entry into except statement
            else: 
                raise #forces entry into except statement


        except:
            try: #handles normal group layers
               group_1 = arcpy.mapping.ListLayers(lyr) 
               #lists all layers within the grouplayer (annotation layers are an exception)
               for g1 in group_1: 
                   print "in group layer"
                   print g1.dataSource
                   group_array.append(g1)   #appends all layers within the normal grouplayer

            except: #in this except the first time the annotation layer is encountered
                pass  

    else: #is a 'normal' layer        
           try:
               #print lyr.name #prints all layer names, including the label classes within the annotation layer
               print lyr.dataSource #errors out when it reaches an annotation layer label class
               group_array.append(lyr)
           except:
               pass
print "##############################################################################\n\n\n"               
for item in group_array: #prints filepaths of all 'normal' layers, group layers, and annotation layers
    print item.dataSource
#

The above code examples are a workaround for, the 'normal' design seen below.

Note: the below code does NOT work for annotation layers

mxd = arcpy.mapping.MapDocument("current")
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.dataSource #fails when encounters Annotation layer
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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