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:ArcpyBookCh3Crime_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:
    How to do it…
  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:ArcpyBookCh3Crime_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 dataCityOfSanAntonio.gdb", "FILEGDB_WORKSPACE",r"c:ArcpyBook 
    ew_dataCityOfSanAntonio_Personal.mdb","ACCESS_WORKSPACE")
  7. Save a copy of the map document file:
    mxd.saveACopy(r"c:ArcpyBookCh3Crime_DataLinksUpdated.mxd")
  8. Save the script as c:ArcpyBookCh3MapDocumentReplaceWorkspaces.py.
  9. You can check your work by examining the c:ArcpyBookcodeCh3MapDocumentReplaceWorkspaces.py solution file.
  10. Run the script.
  11. In ArcMap, open the c:ArcpyBookCh3Crime_DataLinksUpdated.mxd file. As shown in the following screenshot, all data sources now reference a personal geodatabase (note the .mdb extension):

    How to do it…

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.

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

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