TransWikia.com

Distinguishing between Layers and TableView using ArcPy?

Geographic Information Systems Asked by lida on June 10, 2021

I am working with ArcMap and am using ArcPy.

My task is to loop through mxds and find broken data sources.

My code is:

mxd = arcpy.mapping.MapDocument(fullPath)
brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknMXD:  
    if brknItem.supports("workspacePath"):
        try:
            source = brknItem.workspacePath
            print  str(brknItem) + ": " +  source
        except:
            print "unexpected error"
            continue
    else:
        print "Layer does not support source"

Some mxds have TableViews and that is where the code breaks.

Traceback (most recent call last): File
“P:ScriptsFind_MXDs_Broken_Sources.py”, line 5, in
if brknItem.supports(“workspacePath”): AttributeError: ‘TableView’ object has no attribute ‘supports’

I checked within an mxd python interpreter to see what type of objects are return by arcpy.mapping.ListBrokenDataSources(mxd) and it indicated:

>>> for each in brknMXD:
...     print type(each)
...     
<class 'arcpy._mapping.Layer'>
<class 'arcpy._mapping.Layer'>
<class 'arcpy._mapping.Layer'>
<class 'arcpy._mapping.TableView'>

Clearly I can just set a test to distinguish between these two types, but I don’t know how exactly to write it.

Any ideas?

One Answer

As commented by @MichaelStimson:

You could use the describe statement http://resources.arcgis.com/en/help/main/10.2/index.html#//03q30000001w000000 and look at the dataType, it should be Layer or TableView for the two indicated types, though I'm not sure how well it works with broken objects... give it a go and see what you get: if arcpy.Describe(brknItem).dataType == 'Layer': before .supports(.. line.

and as commented by @BenSNadler:

You can use listLayers and listTables and check if each one is broken Or as noted above, check the type before .supports

Answered by PolyGeo on June 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP