Finding broken data sources in your map document and layer files

Broken data sources are a very common problem with map document files. You can use arcpy.mapping to identify data sources that have moved, been deleted, or changed in their format.

Getting ready

In ArcMap, a broken data connection is signified by a red exclamation point just before the layer name. This is illustrated in the following screenshot. The ListBrokenDataSources() function in arcpy.mapping returns a list of layer objects from a map document or layer file that have a broken data connection:

Getting ready

How to do it…

Follow these steps to learn how to find broken data sources in a map document file.

  1. Open C:ArcpyBookCh3Crime_BrokenDataLinks.mxd in ArcMap.

    You will see that each of the data sources have been broken. In this case, the data has been moved to another folder, but you'd see the same indicator if the data had been deleted or migrated to a different format. For example, it is not uncommon to convert data from a personal geodatabase to a file geodatabase:

    How to do it…
  2. Close ArcMap.
  3. Open IDLE and create a new script window.
  4. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  5. Reference the Crime_BrokenDataLinks.mxd map document file:
    mxd = mapping.MapDocument(r"c:ArcpyBookCh3Crime_BrokenDataLinks.mxd")
  6. Get a list of the broken data sources:
    listBrokenDS = mapping.ListBrokenDataSources(mxd)
  7. Iterate the list and print out the layer names:
    for layer in listBrokenDS:
         print(layer.name)

    The output will be printed as follows:

    District_Crime_Join
    Bexar_County_Boundary
    District_Crime_Join
    Bexar_County_Boundary
    Bexar_County_Boundary
    Texas_Counties_LowRes
    School_Districts
    Crime_surf
    Bexar_County_Boundary
    Crime2009Table
  8. Save your script as FindFixBrokenData.py in the c:ArcpyBookCh3 folder.
  9. You can check your work by examining the c:ArcpyBookcodeCh3FindFixBrokenData.py solution file.

How it works…

The ListBrokenDataSources() function returns a Python list of Layer objects that have a broken data source. We then use a for loop to iterate this list and perform some sort of action for each layer. In this case, we printed out the layer names simply to illustrate the data returned by this function. In a later recipe, we'll build on this code by fixing these broken data sources.

There's more…

In addition to returning a list of broken data sources from a map document file, the ListBrokenDataSources() function can also find broken data sources in a (.lyr) layer file. Simply pass the path to the layer file to have the function examine the file for broken data sources. Keep in mind that these functions are not needed with Map or Layer packages, since the data is bundled with these files unlike a layer file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset