Esri has introduced in ArcGIS 10 a new geoprocessing package ArcPy, which is based on Python – a popular open scripting language. This module is supposed to replace the previously used in ArcGIS 9.3 arcgisscripting module and contains a plenty of new functions that could help you to be more efficient when managing your GIS workflows.
Today, I would like to focus on using the arcpy.mapping module. This module is included in the ArcPy package and its primary use is to manage map documents (.mxd) and layer files (.lyr). This module can be used very effectively when data sources in mxd map documents need to be updated or redirected to some other locations.
To learn more about the arcpy.mapping module, please refer to the Esri help page: Updating and fixing data sources with arcpy.mapping
Of course, you can change the source paths using ArcCatalog, yet it does not provide the same level of access to underlying objects as the ArcPy package. Please refer to the Esri Knowledge Base article and Help pages to learn more how to change data sources in your .mxd map documents:
Below is the script which can be used for changing multiple SDE data sources in one .mxd map document.
import arcpy # Define the path to the .mxd map document you want to update mxd = arcpy.mapping.MapDocument(r"C:\Temp\Esri_sde_service.mxd") # Define which connection strings will be changed. Here there is just one connection string being changed. mxd.findAndReplaceWorkspacePaths(r"C:\Users\username\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\Production@sde.sde", r"C:\Users\alte\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\Development@sde.sde") # It is possible to add more connection strings that have to be changed in the same map document mxd.findAndReplaceWorkspacePaths(r"C:\Users\username\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\RasterData.sde", r"C:\Users\username\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\RasterData_load.sde") # The path where the new .mxd map document will be saved to. mxd.saveACopy(r"C:\GIS\Output\Sde_load.mxd") del mxd
Here is the script which can be used for changing multiple SDE data sources in multiple.mxd map documents.
import arcpy, os # Define the path to the folder where multiple .mxd map documents are stored folderPath = r"C:\GIS\Maps" for filename in os.listdir(folderPath): fullpath = os.path.join(folderPath, filename) if os.path.isfile(fullpath): basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) # Define which connection strings will be changed. Here there are two connection strings being changed. One for vector data mxd.findAndReplaceWorkspacePaths(r"C:\Users\username\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\Production@sde.sde", r"C:\Users\username\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\Development@sde.sde") # and second one for raster data which may have used another connection string. mxd.findAndReplaceWorkspacePaths(r"C:\Documents and Settings\username\Application Data\ESRI\ArcCatalog\raster_data.sde", r"C:\Documents and Settings\username\Application Data\ESRI\Desktop10.0\ArcCatalog\raster_dataV10.sde") # You can use even more defitions of the connection strings that will be changed. Note that here is the update of the path from 9.3 to 10 is going on which you can see since the path to the .sde file is different (update from 9.3 to 10) mxd.findAndReplaceWorkspacePaths(r"C:\Documents and Settings\username\Application Data\ESRI\ArcCatalog\TopologyData.sde", r"C:\Documents and Settings\username\Application Data\ESRI\Desktop10.0\ArcCatalog\TopologyData_manager.sde") mxd.save() del mxd