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.
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.
Follow these steps to learn how to fix layers and tables in a map document using findAndReplaceWorkspacePaths()
:
c:ArcpyBookCh3Crime_BrokenDataLinks.mxd
in ArcMap.ArcpyBookCh3DataOldDataCityOfSanAntonio.gdb
. This is a file geodatabase but the location no longer exists. It has moved to the C:ArcpyBookdata
folder.arcpy.mapping
module:import arcpy.mapping as mapping
Crime_BrokenDataLinks.mxd
map document file:mxd = mapping.MapDocument(r"c:ArcpyBookCh3Crime_BrokenDataLinks.mxd")
MapDocument.findAndReplaceWorkspacePaths()
to fix the source path for each data source in the map document. The findAndReplaceWorksapcePaths()
method accepts the old path as the first parameter and the new path as the second parameter:mxd.findAndReplaceWorkspacePaths(r" C:ArcpyBookCh3DataOldDataCityOfSanAntonio.gdb", r" C:ArcpyBookDataCityOfSanAntonio.gdb")
.mxd
file:mxd.saveACopy(r"C:ArcpyBookCh3Crime_DataLinksFixed.mxd")
C:ArcpyBookCh3MapDocumentFindReplaceWorkspacePath.py
.c:ArcpyBookcodeCh3MapDocumentFindReplaceWorkspacePath.py
solution file.C:ArcpyBookCh3Crime_DataLinksFixed.mxd
file. You will notice that all the data sources get fixed, as shown in the following screenshot: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.
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 individual broken data source rather than a global find, along with the replacement of all broken data sources in a map document.