- Programming ArcGIS with Python Cookbook(Second Edition)
- Eric Pimpler
- 563字
- 2021-07-16 13:32:25
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()
:
- Open
c:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd
in ArcMap. - Notice that all of the layers and tables are loaded from a file geodatabase called
CityOfSanAntonio.gdb
, as shown in the following screenshot: - Open IDLE and create a new script window.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the
Crime_DataLinksFixed.mxd
map document file:mxd = mapping.MapDocument(r"c:\ArcpyBook\Ch3\Crime_DataLinksFixed.mxd")
- 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")
- Save a copy of the map document file:
mxd.saveACopy(r"c:\ArcpyBook\Ch3\Crime_DataLinksUpdated.mxd")
- Save the script as
c:\ArcpyBook\Ch3\MapDocumentReplaceWorkspaces.py
. - You can check your work by examining the
c:\ArcpyBook\code\Ch3\MapDocumentReplaceWorkspaces.py
solution file. - Run the script.
- 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 workspaceARCINFO_WORKSPACE
: This is an ArcInfo coverage workspaceCAD_WORKSPACE
: This is a CAD file workspaceEXCEL_WORKSPACE
: This is an Excel file workspaceFILEGDB_WORKSPACE
: This is a file geodatabase workspaceNONE
: This is used to skip a parameterOLEDB_WORKSPACE
: This is an OLE database workspacePCCOVERAGE_WORKSPACE
: This is a PC ARC/INFO Coverage workspaceRASTER_WORKSPACE
: This is a raster workspaceSDE_WORKSPACE
: This is an SDE geodatabase workspaceSHAPEFILE_WORKSPACE
: This is a shapefile workspaceTEXT_WORKSPACE
: This is a text file workspaceTIN_WORKSPACE
: This is a TIN workspaceVPF_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.