Fixing individual 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 individual 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 individual Layer and TableView objects in a map document using replaceDataSource():

Open c:ArcpyBookCh3Crime_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:

How to do it…
  1. Open IDLE and create a new script window.
  2. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  3. Reference the Crime_DataLinksLayer.mxd map document file:
    mxd = mapping.MapDocument(r"c:ArcpyBookCh3 Crime_DataLinksLayer.mxd")
  4. Get a reference to the Crime data frame:
    df = mapping.ListDataFrames(mxd,"Crime")[0]
  5. Find the Burglary layer and store it in a variable:
    lyr = mapping.ListLayers(mxd,"Burglary",df)[0]
  6. Call the replaceDataSource() method on the Layer 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:ArcpyBookdata","SHAPEFILE_WORKSPACE","Burglaries_2009")
  7. Save the results to a new map document file:
    mxd.saveACopy(r"c:ArcpyBookCh3Crime_DataLinksNewLayer.mxd")
  8. Save the script as c:ArcpyBookCh3LayerReplaceDataSource.py.
  9. You can check your work by examining the c:ArcpyBookcodeCh3LayerReplaceDataSource.py solution file.
  10. Run the script.
  11. Open C:ArcpyBookCh3Crime_DataLinksNewLayer.mxd in ArcMap. You should see that the Burglary layer now references a new workspace:
    How to do it…
  12. Right-click on the Burglary layer and select Properties.
  13. Click on the Source tab and note the new workspace, workspace type, and dataset name:
    How to do it…

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.

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

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