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

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():

  1. Open c:\ArcpyBook\Ch3\Crime_BrokenDataLinks.mxd in ArcMap.
  2. Right-click on any of the layers and select Properties.
  3. 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 the C:\ArcpyBook\data folder.
  4. Open IDLE and create a new script window.
  5. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  6. Reference the Crime_BrokenDataLinks.mxd map document file:
    mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\Crime_BrokenDataLinks.mxd")
  7. Use 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:\ArcpyBook\Ch3\Data\OldData\CityOfSanAntonio.gdb", r" C:\ArcpyBook\Data\CityOfSanAntonio.gdb")
  8. Save the results to a new .mxd file:
    mxd.saveACopy(r"C:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd")
  9. Save the script as C:\ArcpyBook\Ch3\MapDocumentFindReplaceWorkspacePath.py.
  10. You can check your work by examining the c:\ArcpyBook\code\Ch3\MapDocumentFindReplaceWorkspacePath.py solution file.
  11. Run the script.
  12. 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.

主站蜘蛛池模板: 中江县| 崇义县| 定边县| 会泽县| 驻马店市| 合山市| 彭山县| 和田市| 锡林郭勒盟| 西贡区| 天水市| 郎溪县| 保靖县| 凌云县| 肥乡县| 玛纳斯县| 兴海县| 庄浪县| 股票| 达尔| 通城县| 龙海市| 青龙| 三亚市| 搜索| 象州县| 惠水县| 临夏市| 揭东县| 三原县| 扬州市| 抚顺县| 慈利县| 德庆县| 东海县| 逊克县| 湟中县| 丹凤县| 廉江市| 田阳县| 温州市|