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

Fixing broken data sources with MapDocument.replaceWorkspaces()

During the course of normal GIS operations, it is a fairly common practice to migrate data from one file type to another. For example, many organizations migrate data from older personal geodatabase formats to the new file geodatabase types, or perhaps even enterprise ArcSDE geodatabases. You can automate the process of updating your datasets to a different format with MapDocument.replaceWorkspaces().

Getting ready

MapDocument.replaceWorkspaces() is similar to MapDocument.findAndReplaceWorkspacePaths(), but it also allows you to switch from one workspace type to another. For example, you can switch from a file geodatabase to a personal geodatabase. However, it only works in one workspace at a time. In this recipe, we'll use MapDocument.replaceWorkspaces() to switch our data source from a file geodatabase to a personal geodatabase.

How to do it…

Follow these steps to learn how to fix broken data sources using MapDocument.replaceWorkspaces():

  1. Open c:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd in ArcMap.
  2. Notice that all of the layers and tables are loaded from a file geodatabase called CityOfSanAntonio.gdb, as shown in the following screenshot:
  3. Open IDLE and create a new script window.
  4. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  5. Reference the Crime_DataLinksFixed.mxd map document file:
    mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd")
  6. Call the replaceWorkspaces() method, passing a reference to the old geodatabase type as well as the new geodatabase type:
    mxd.replaceWorkspaces(r"c:\ArcpyBook \data\CityOfSanAntonio.gdb", "FILEGDB_WORKSPACE",r"c:\ArcpyBook \new_data\CityOfSanAntonio_Personal.mdb","ACCESS_WORKSPACE")
  7. Save a copy of the map document file:
    mxd.saveACopy(r"c:\ArcpyBook\Ch3\Crime_DataLinksUpdated.mxd")
  8. Save the script as c:\ArcpyBook\Ch3\MapDocumentReplaceWorkspaces.py.
  9. You can check your work by examining the c:\ArcpyBook\code\Ch3\MapDocumentReplaceWorkspaces.py solution file.
  10. Run the script.
  11. In ArcMap, open the c:\ArcpyBook\Ch3\Crime_DataLinksUpdated.mxd file. As shown in the following screenshot, all data sources now reference a personal geodatabase (note the .mdb extension):

How it works…

The MapDocument.replaceWorkspaces() method accepts several parameters including old and new workspace paths along with the old and new workspace types. Paths to the workspaces are self-explanatory, but some discussion of the workspace types is helpful. The workspace types are passed into the method as string keywords. In this case, the old workspace type was a file geodatabase so its keyword is FILEGDB_WORKSPACE. The new workspace type is ACCESS_WORKSPACE, which indicates a personal geodatabase. Personal geodatabases are stored in Microsoft Access files. There are a number of different workspace types that can store GIS data. Make sure you provide the workspace type that is appropriate for your dataset. The following is a list of valid workspace types (many people still work with shapefiles so, in this case, the workspace type would be SHAPEFILE_WORKSPACE):

  • ACCESS_WORKSPACE: This is a personal geodatabase or Access workspace
  • ARCINFO_WORKSPACE: This is an ArcInfo coverage workspace
  • CAD_WORKSPACE: This is a CAD file workspace
  • EXCEL_WORKSPACE: This is an Excel file workspace
  • FILEGDB_WORKSPACE: This is a file geodatabase workspace
  • NONE: This is used to skip a parameter
  • OLEDB_WORKSPACE: This is an OLE database workspace
  • PCCOVERAGE_WORKSPACE: This is a PC ARC/INFO Coverage workspace
  • RASTER_WORKSPACE: This is a raster workspace
  • SDE_WORKSPACE: This is an SDE geodatabase workspace
  • SHAPEFILE_WORKSPACE: This is a shapefile workspace
  • TEXT_WORKSPACE: This is a text file workspace
  • TIN_WORKSPACE: This is a TIN workspace
  • VPF_WORKSPACE: This is a VPF workspace
Tip

When switching workspaces via the replaceWorkspaces() method, the dataset names must be identical. For example, a shapefile called Highways.shp can be redirected to a file geodatabase workspace only if the dataset name in the file geodatabase is also called Highways. Use the replaceDataSource() method on the layer or TableView objects if the dataset name is different.

主站蜘蛛池模板: 邢台市| 泸水县| 佛山市| 榕江县| 通州市| 吉木乃县| 喜德县| 政和县| 彭阳县| 恩平市| 沙河市| 江陵县| 廊坊市| 习水县| 大田县| 什邡市| 玉门市| 庆安县| 龙陵县| 土默特右旗| 扶风县| 天水市| 兴国县| 岢岚县| 渝中区| 彭阳县| 秦安县| 页游| 图们市| 宁国市| 红安县| 合川市| 石阡县| 武宣县| 达孜县| 曲水县| 牟定县| 儋州市| 黑山县| 寿阳县| 墨玉县|