
Fixing inpidual layer and table objects with replaceDataSource()
The previous recipes in this chapter have used various methods on the MapDocument
object to fix broken data links. The Layer
and TableView
objects also have methods that can be used to fix broken data links at the inpidual object level rather than working on all datasets in a map document file. This recipe discusses the repairing of Layer
and TableView
objects.
Getting ready
Both the Layer
and TableView
classes have a replaceDataSource()
method. This method changes the workspace path, workspace type, and/or dataset name for a single layer or table. In this recipe, you'll write a script that changes the workspace path and workspace type for a single layer. The replaceDataSource()
method is available for the Layer
and TableView
classes. In the case of a layer, it can either be in a map document or layer file. For a table, it can refer to the map document only, since TableView
objects can't be contained inside a layer file.
How to do it…
Follow these steps to learn how to fix inpidual Layer
and TableView
objects in a map document using replaceDataSource()
:
Open c:\ArcpyBook\Ch3\Crime_DataLinksLayer.mxd
in ArcMap. The Crime data frame contains a layer called Burglary, which is a feature class in the CityOfSanAntonio
file geodatabase. You're going to replace this feature class with a shapefile layer containing the same data:

- Open IDLE and create a new script window.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the
Crime_DataLinksLayer.mxd
map document file:mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\ Crime_DataLinksLayer.mxd")
- Get a reference to the
Crime
data frame:df = mapping.ListDataFrames(mxd,"Crime")[0]
- Find the Burglary layer and store it in a variable:
lyr = mapping.ListLayers(mxd,"Burglary",df)[0]
- Call the
replaceDataSource()
method on theLayer
object and pass the path to the shapefile. A keyword will indicate that this will be a shapefile workspace, and it also indicates the name of the shapefile:lyr.replaceDataSource(r"c:\ArcpyBook\data","SHAPEFILE_WORKSPACE","Burglaries_2009")
- Save the results to a new map document file:
mxd.saveACopy(r"c:\ArcpyBook\Ch3\Crime_DataLinksNewLayer.mxd")
- Save the script as
c:\ArcpyBook\Ch3\LayerReplaceDataSource.py
. - You can check your work by examining the
c:\ArcpyBook\code\Ch3\LayerReplaceDataSource.py
solution file. - Run the script.
- Open
C:\ArcpyBook\Ch3\Crime_DataLinksNewLayer.mxd
in ArcMap. You should see that the Burglary layer now references a new workspace: - Right-click on the Burglary layer and select Properties.
- Click on the Source tab and note the new workspace, workspace type, and dataset name:
How it works…
The replaceDataSource()
method accepts two required parameters and two optional parameters. The first two parameters define the workspace path and workspace type for the layer that will be used as the replacement. The third parameter, dataset_name
, is an optional parameter that defines the name of the dataset that will be used as the replacement layer. This name must be an exact match. For example, in this recipe, we passed in a dataset_name
attribute as Burglaries_2009
, which is the name of the shapefile that will now be used as the replacement layer in the data frame. If a name is not provided, the method will attempt to replace the dataset by finding a table with the same name as the current layer's dataset property. The final optional parameter is validate
. By default, this value is set to True
. When set to True
, a workspace will only be updated if the workspace_path
value is a valid workspace. If it is not a valid workspace, then the workspace will not be replaced. If it's set to False
, the method will set the source to match workspace_path
, regardless of whether it is a valid match or not. This can result in a broken data source, but can be useful if you are creating or modifying a map document in preparation for data that does not yet exist.
There's more…
The Layer
and TableView
classes also contain a findAndReplaceWorkspacePath()
method. This method is very similar to the MapDocument.findAndReplaceWorkspacePaths()
method. The only difference is that it works against a single Layer
or TableView
class instead of iterating the entire map document or layer file.