
Fixing broken data sources with MapDocument.findAndReplaceWorkspacePaths()
The MapDocument.findAndReplaceWorkspacePaths()
method is used to perform global find and replace workspace paths for all the layers and tables in a map document. You can also replace the paths to multiple workspace types at once. For example, you might pass personal and file geodatabase workspace types at the same time.
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 an ArcSDE connection. A workspace provides 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 names. 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 arcpy.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 global find and replace operations in the layers and tables of a map document.
How to do it…
Follow these steps to learn how to fix layers and tables in a map document using findAndReplaceWorkspacePaths()
:
- Open
c:\ArcpyBook\Ch3\Crime_BrokenDataLinks.mxd
in ArcMap. - Right-click on any of the layers and select Properties.
- Go to the Source tab and you will notice that the location for the layer is
ArcpyBook\Ch3\Data\OldData\CityOfSanAntonio.gdb
. This is a file geodatabase but the location no longer exists. It has moved to theC:\ArcpyBook\data
folder. - Open IDLE and create a new script window.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the
Crime_BrokenDataLinks.mxd
map document file:mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\Crime_BrokenDataLinks.mxd")
- Use
MapDocument.findAndReplaceWorkspacePaths()
to fix the source path for each data source in the map document. ThefindAndReplaceWorksapcePaths()
method accepts the old path as the first parameter and the new path as the second parameter:mxd.findAndReplaceWorkspacePaths(r" C:\ArcpyBook\Ch3\Data\OldData\CityOfSanAntonio.gdb", r" C:\ArcpyBook\Data\CityOfSanAntonio.gdb")
- Save the results to a new
.mxd
file:mxd.saveACopy(r"C:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd")
- Save the script as
C:\ArcpyBook\Ch3\MapDocumentFindReplaceWorkspacePath.py
. - You can check your work by examining the
c:\ArcpyBook\code\Ch3\MapDocumentFindReplaceWorkspacePath.py
solution file. - Run the script.
- In ArcMap, open the
C:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd
file. You will notice that all the data sources get fixed, as shown in the following screenshot:
How it works…
The MapDocument.findAndReplaceWorkspacePaths()
method performs global find and replace workspace paths for all layers and tables in a map document. You can replace the paths for 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, in the Layer
and TableView
objects, is used to fix an inpidual broken data source rather than a global find, along with the replacement of all broken data sources in a map document.