Mapping Replace Workspaces issue

973
5
08-09-2012 06:59 AM
BenSloboda
New Contributor II
I have the following code:

import arcpy
fileFolder = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Maps"
oldSpace = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Data\Burlington GIS\ANG_Burlington\ANG_Burlington_CIP.mdb\cadastre\installation_area"
newSpace = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Data\Burlington GIS\VTANG_Boundary.shp"

for element in fileFolder:
    fileType = element[-4:]
    if fileType == ".mxd":
        mxd = arcpy.mapping.MapDocument(element)
        mxd.replaceWorkspaces(oldSpace,"ACCESS_WORKSPACE",newSpace,"SHAPEFILE_WORKSPACE")


The premise is simple.  Iterate through a number of mxd files in the fileFolder location and change the source for one of the layers.  There are other files, so to prevent the silly "Invalid MXD filename" error, I added an if clause which checks for the fileType variable.  This variable is set to the last four characters of the file, so it would know if the file is an mxd.  If the fileType is an mxd, it replaces the old .mdb source with the new .shp source.  The problem is this: the script runs with no errors, but does not actually replace the source file in the corresponding mxds.

Help!

-Ben S.
Tags (2)
5 Replies
ThomasEmge
Esri Contributor
Ben,

try

mxd.save()


as the last line in your inner loop.

- Thomas
0 Kudos
BruceBacia
Occasional Contributor
Have you used print statements to verify that your logic for identifying mxd files is passing the correct values to arcpy.mapping.MapDocument()?.
0 Kudos
BruceBacia
Occasional Contributor
You could try built in python methods to get mxds  instead of your own logic
import arcpy, os
fileFolder = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Maps\\"
dirs = os.listdir(fileFolder)
maps = [(fileFolder + dir) for dir in dirs if dir.endswith(".mxd")]
oldSpace = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Data\Burlington GIS\ANG_Burlington\ANG_Burlington_CIP.mdb\cadastre\installation_area"
newSpace = r"G:\ProjectF\GIS\Projects\INRMP\Burlington\GIS_Data\Burlington GIS\VTANG_Boundary.shp"

for map in maps:
    mxd = arcpy.mapping.MapDocument(map)
    mxd.replaceWorkspaces(oldSpace,"ACCESS_WORKSPACE",newSpace,"SHAPEFILE_WORKSPACE")
    mxd.save()
    del mxd
0 Kudos
BruceBacia
Occasional Contributor
I think your previous code was not doing anything because the for loop was iterating through every letter (element) in the fileFolder string.  Since they were single characters, no value was returned for the [-4:] slice, thus nothing happened.
0 Kudos
BruceBacia
Occasional Contributor
Btw, it looks like the replace Workspace method will replace the workspaces for all layers in the mxd.  If you only want to replace the source for one layer you will need to use layer.replaceDataSource.  Pseudocode below:
For each map doc in fileFolder:
    For each layer in map doc:
        if layer.dataSource = oldSpace:
            layer.replaceDataSource with newSpace

If you need any help writing this let me know.
0 Kudos