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

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:

How to do it…

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

  1. Open C:\ArcpyBook\Ch3\Crime_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:

  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:\ArcpyBook\Ch3\Crime_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:\ArcpyBook\Ch3 folder.
  9. You can check your work by examining the c:\ArcpyBook\code\Ch3\FindFixBrokenData.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.

主站蜘蛛池模板: 乐昌市| 山阳县| 凤凰县| 新兴县| 乌海市| 东平县| 青海省| 河源市| 吐鲁番市| 武穴市| 成安县| 桑植县| 清苑县| 深水埗区| 读书| 广西| 专栏| 钦州市| 通山县| 新民市| 顺义区| 西城区| 德钦县| 上饶市| 龙海市| 将乐县| 聂拉木县| 太保市| 甘谷县| 东兴市| 布拖县| 宜良县| 余干县| 宝鸡市| 泰顺县| 基隆市| 蕉岭县| 谢通门县| 青浦区| 名山县| 鄯善县|