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

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.

主站蜘蛛池模板: 南陵县| 鄂托克旗| 延边| 泽州县| 舞钢市| 宁南县| 河曲县| 彭州市| 比如县| 榆中县| 广宗县| 旌德县| 漳州市| 农安县| 兴化市| 沁阳市| 临高县| 耒阳市| 桓台县| 金寨县| 赣州市| 上饶县| 金寨县| 视频| 白银市| 怀安县| 乌拉特中旗| 新乡县| 天峻县| 都江堰市| 义马市| 额敏县| 镇巴县| 宁城县| 璧山县| 西贡区| 莒南县| 宁夏| 当雄县| 电白县| 离岛区|