官术网_书友最值得收藏!

Finding broken data sources in all map documents in a folder

A common scenario in many organizations involves the movement of data from one workspace to another or from one workspace type to another. When this happens, any map documents or layers that reference these data sources become broken. Finding each of these data sources can be a huge task if undertaken manually. Fortunately, you can create a geoprocessing script that will find all broken data sources in a folder or list of folders.

Getting ready

In this recipe, you will learn how to recursively search directories for map document files, find any broken data sources within these map documents, and write the names of the broken data layers to a file.

How to do it...

Follow these steps to learn how to find all broken data sources in all map documents in a folder:

  1. Open IDLE and create a new script window.
  2. Import the arcpy and os packages:
    import arcpy.mapping as mapping, os
  3. Open a file that you will use to write the broken layer names:
    f = open('BrokenDataList.txt', 'w')
  4. Pass a path to the c:\ArcpyBook folder to use in the os.walk() method along with a for loop to walk the directory tree:
    for root,dirs,files in os.walk("C:\ArcpyBook"):
  5. Inside the for loop, create a second for loop that loops through all the files returned and create a new filename variable. Remember to indent the for loop inside the first for loop:
    for name in files:
        filename = os.path.join(root, name)
  6. Following the last line of code that you added, test the file extension to see if it is a map document file. If so, create a new map document object instance using the path, write the map document name, get a list of broken data sources, loop through each of the broken data sources, and write to the file:
    if ".mxd" in filename:
         mxd = mapping.MapDocument(filename)
         f.write("MXD: " + filename + "\n")
         brknList = mapping.ListBrokenDataSources(mxd)
         for brknItem in brknList:
              print "Broken data item: " + brknItem.name + " in " + filename
              f.write("\t" + brknItem.name + "\n")
  7. Add a print statement to indicate that you are done and close the file:
    print("All done")
    f.close()
  8. The entire script should appear as follows:
  9. You can check your work by examining the c:\ArcpyBook\code\Ch3\ListBrokenDataSources.py solution file.
  10. Run the script to generate the file.
  11. Open the file to see the results. Your output will vary depending upon the path you've defined. The following screenshot shows my output file:

How it works...

This script uses a combination of methods from the Python os package and the arcpy.mapping package. The os.walk() method walks a directory tree and returns the path, a list of directories, and a list of files for each directory starting with a root directory that you have defined as the c:\ArcpyBook directory. This root directory could have been any directory. The os.walk() method returns a three item tuple consisting of the root directory, a list of directories immediately contained within that root, as well as a list of files immediately contained within the root. We then loop through this list of files and test each one to see if it contains the .mxd string, which indicates a map document file. Files identified as map documents have their filenames written to a text file, and a new MapDocument object instance is created. The ListBrokenDataSources() method is then used with a reference to the map document to generate a list of broken data sources within the file, and these broken data sources are written to the file as well.

主站蜘蛛池模板: 汶上县| 余江县| 凌云县| 郓城县| 秦安县| 黄冈市| 嘉义县| 承德市| 绥德县| 常州市| 通河县| 宿迁市| 奉新县| 林州市| 奉新县| 菏泽市| 聂拉木县| 北票市| 忻城县| 浦江县| 马公市| 澜沧| 宿州市| 福鼎市| 凭祥市| 汶上县| 宁乡县| 大理市| 广元市| 南皮县| 汝阳县| 富顺县| 邵东县| 翁牛特旗| 长汀县| 渝北区| 辽宁省| 波密县| 普格县| 远安县| 色达县|