官术网_书友最值得收藏!

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:

  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:\ArcpyBook\Ch3\ 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:\ArcpyBook\data","SHAPEFILE_WORKSPACE","Burglaries_2009")
  7. Save the results to a new map document file:
    mxd.saveACopy(r"c:\ArcpyBook\Ch3\Crime_DataLinksNewLayer.mxd")
  8. Save the script as c:\ArcpyBook\Ch3\LayerReplaceDataSource.py.
  9. You can check your work by examining the c:\ArcpyBook\code\Ch3\LayerReplaceDataSource.py solution file.
  10. Run the script.
  11. Open C:\ArcpyBook\Ch3\Crime_DataLinksNewLayer.mxd in ArcMap. You should see that the Burglary layer now references a new workspace:
  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 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.

主站蜘蛛池模板: 宜川县| 哈巴河县| 隆化县| 民权县| 曲松县| 石家庄市| 田阳县| 纳雍县| 岢岚县| 灌云县| 甘南县| 仙居县| 哈尔滨市| 德保县| 青川县| 绿春县| 宜良县| 旬邑县| 贵南县| 合江县| 汝州市| 武夷山市| 十堰市| 上饶县| 巴林左旗| 高清| 荃湾区| 区。| 白银市| 河西区| 盈江县| 乐陵市| 横山县| 涿鹿县| 含山县| 若羌县| 黄山市| 孝义市| 监利县| 吉隆县| 武宁县|