Fixing broken data sources with MapDocument.findAndReplaceWorkspacePaths()

The MapDocument.findAndReplaceWorkspacePaths() method is used to perform a global find and replace of workspace paths for all the layers and tables in a map document. You can also replace the paths to multiple workspace types at once.

Getting ready

We need to cover some definitions before examining the methods used to fix datasets. You'll see these terms used frequently when discussing the methods used to fix broken data sources, so you'll need to understand what they mean in this context. A workspace is simply a container for data. This can be a folder (in the case of shapefiles), personal geodatabase, file geodatabase, or ArcSDE connection. A workspace path is the system path to the workspace. In the case of file geodatabases, this would include the name of the geodatabase. A dataset is simply a feature class or table within a workspace, and finally, a data source is the combination of the workspace and dataset. Don't confuse a dataset with a feature dataset. The former is just a generic term for data, while the latter is an object within a geodatabase that serves as a container for feature classes and other datasets.

There are three acpy.mapping classes involved in fixing broken data sources. They are MapDocument, Layer, and TableView. Each class contains methods that can be used to fix data sources. In this recipe, we'll examine how you can use the findAndReplaceWorkspacePaths() method in the MapDocument class to perform a global find and replace operation on the layers and tables in a map document.

How to do it…

Follow these steps to learn how to fix layers and tables in a map document using findAndReplaceWorkspacePaths():

  1. Open c:ArcpyBookCh4Crime_BrokenDataLinks.mxd in ArcMap.
  2. Right-click on any of the layers and select Properties.
  3. Go to the Source tab and you will notice that the location for the layer refers to C:ArcpyBookCh4DataOldDataCityOfSanAntonio.gdb. This is a file geodatabase but the location no longer exists; it has moved to the C:ArcpyBookdata folder.
  4. Open IDLE and create a new script window.
  5. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  6. Reference the Crime_BrokenDataLinks.mxd map document file:
    mxd = mapping.MapDocument(r"c:ArcpyBookCh4Crime_BrokenDataLinks.mxd")
  7. Use MapDocument.findAndReplaceWorkspacePaths() to fix the source path for each data source in the map document:
    mxd.findAndReplaceWorkspacePaths(r"C:ArcpyBookCh4DataOldDataCityOfSanAntonio.gdb", r"C:ArcpyBookDataCityOfSanAntonio.gdb")
  8. Save the results to a new .mxd file:
    mxd.saveACopy(r"C:ArcpyBookCh4Crime_DataLinksFixed.mxd")
  9. Save the script as C:ArcpyBookCh4MapDocumentFindReplace.py.
  10. Run the script.
  11. In ArcMap, open the C:ArcpyBookCh4Crime_DataLinksFixed.mxd file. You will notice that all the data sources get fixed, as shown in the following screenshot:
    How to do it…

How it works…

The MapDocument.findAndReplaceWorkspacePaths() method is used to perform a global find and replace of workspace paths for all layers and tables in a map document. You can replace the paths to multiple workspace types at once.

There's more…

The Layer and TableView objects also have a findAndReplaceWorkspacePaths() method that performs the same type of operation. The difference is that this method, on the Layer and TableView objects, is used to fix an individual broken data source rather than a global find and replace of all broken data sources in a map document.

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

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