Changing Data Source on MXD

4563
7
Jump to solution
07-31-2013 11:01 AM
TonyAlmeida
Occasional Contributor II
I am needing to change my multiple layers Data Source in about 24 MXD's from one sde geodabase to a different. We have changed servers. My SDE instance is different now. Note: there is no passwords and there is some .shp files in this mxd

I am getting the following error. so any help would be gratefully appreciated.

Proposed_ZoningMapBook_Page_1.mxd

Traceback (most recent call last):
  File "C:/GIS/Python Scripts/Change Data Source MXD.py", line 29, in <module>
    MXD.replaceWorkspaces(source, "SDE_WORKSPACE", datapath, "SDE_WORKSPACE")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 915, in replaceWorkspaces
    return convertArcObjectToPythonObject(self._arc_object.replaceWorkspaces(*gp_fixargs((old_workspace_path, old_workspace_type, new_workspace_path, new_workspace_type, validate), True)))
ValueError: MapDocObject: Unexpected error
# Import system modules import arcpy, os, glob  #This is the folder containing your MXD's arcpy.env.workspace = "C:\GIS\MAPBOOK\Proposed Zoning Book"   wrksp3 = arcpy.env.workspace arcpy.env.workspace = wrksp3   #Lets grab a listing of all MXD's in our workspace; called wrksp3 for filename in os.listdir(wrksp3):     fullpath = os.path.join(wrksp3, filename)     if os.path.isfile(fullpath):         basename, extension = os.path.splitext(fullpath)         if extension.lower() == ".mxd":             print filename                         MXD = arcpy.mapping.MapDocument(fullpath)             #list all the layers in the first MXD from above             for lyr in arcpy.mapping.ListLayers(MXD):                 #print lyr                 #Here we grab the source from the first layer                 source = lyr.workspacePath                 #print lyr, source                 #This is the NEW SOURCE that your point to                 datapath = r"C:\Users\**\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DBO.**.**.sde"                 #Here the actual source replacement is done                 MXD.replaceWorkspaces(source, "SDE_WORKSPACE", datapath, "SDE_WORKSPACE")                 #dont forget to save your changes; this option saves the MXD to v10, no saving as pre-10 versions of ArcGIS                 MXD.save() 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor
Just at a glance, I would think that your mxd is not getting set correctly.  maybe the fullpath variable isn't set correctly?  what does a print fullpath give you?

what does >>>MXD.filePath give you?

It looks like you are wanting to replace the data source on all layers within the mxd(s).  No need to itereate if that is the case, the
findAndReplaceWorkspacePaths function does that automatically, and is simpler to code (don't need to specify type) and will replace in all layers in the mxd.

Also, you might find it easier to get the mxd's using os.walk rather than listDirs, the entities are already parsed, and you only need to join the path/filename.

Would suggest the arcpy.da.walk, but there is a bug in that where it won't report map documents (though the docs say it will).

R_

View solution in original post

0 Kudos
7 Replies
RhettZufelt
MVP Frequent Contributor
Just at a glance, I would think that your mxd is not getting set correctly.  maybe the fullpath variable isn't set correctly?  what does a print fullpath give you?

what does >>>MXD.filePath give you?

It looks like you are wanting to replace the data source on all layers within the mxd(s).  No need to itereate if that is the case, the
findAndReplaceWorkspacePaths function does that automatically, and is simpler to code (don't need to specify type) and will replace in all layers in the mxd.

Also, you might find it easier to get the mxd's using os.walk rather than listDirs, the entities are already parsed, and you only need to join the path/filename.

Would suggest the arcpy.da.walk, but there is a bug in that where it won't report map documents (though the docs say it will).

R_
0 Kudos
TonyAlmeida
Occasional Contributor II
rzufelt, thank you for the reply and tips. All my mxd's layers data sources are being updated.

Here is my working script.

import arcpy
from arcpy import env
from arcpy import mapping
arcpy.env.workspace = workspace = "C:\GIS\MAPBOOK\Proposed Zoning Book"

arcpy.env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd = workspace + "\\" + mxd
    print mxd + " is being processed"
    mxd = arcpy.mapping.MapDocument(mxd)
    for df in arcpy.mapping.ListDataFrames(mxd, "*"):
        for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
            mxd.findAndReplaceWorkspacePaths(r"C:\Documents and Settings\***\Application Data\ESRI\ArcCatalog\***.sde",r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DBO.***.VECTOR.sde")
            print "Successfully updated data sources"
    mxd.save()
0 Kudos
TonyAlmeida
Occasional Contributor II
I spoke to soon...

My script ran successfully and and it indicated that the datat sources was updated successfully, but when I open up my mxd's the data sources are not updated they are still on the old ones.  did i miss something?

C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_11.mxd is being processed
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
Successfully updated data sources
C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_12.mxd is being processed
Successfully updated data sources
0 Kudos
RhettZufelt
MVP Frequent Contributor
import arcpy
from arcpy import env
from arcpy import mapping

workspace = arcpy.env.workspace =  "C:\GIS\MAPBOOK\Proposed Zoning Book"

arcpy.env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd = workspace + "\\" + mxd
    print mxd + " is being processed"
    mxd = arcpy.mapping.MapDocument(mxd)
    mxd.findAndReplaceWorkspacePaths(r"C:\Documents and Settings\***\Application Data\ESRI\ArcCatalog\***.sde",r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DBO.***.VECTOR.sde")
    print "Successfully updated data sources"
    mxd.save(r'new_mxd_filename.mxd'))


Not sure, looks like it should be saving it.  Wonder if the findAndReplaceWorkspacePaths is confusing it in a loop since it is a global command.  Try the above code, see what happens.

If that doesn't work, I'd try mxd.saveACopy() and see if the new file is written, if so, if the paths are updated appropriatly.

R_

Your listing of the mxds is much cleaner now..
0 Kudos
TonyAlmeida
Occasional Contributor II
I am getting the following error with mxd.save...

C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd is being processed
Successfully updated data sources

Traceback (most recent call last):
  File "C:/GIS/Python Scripts/Change Data Source MXD_2_A.py", line 24, in <module>
    mxd.save(r'new_mxd_filename.mxd')
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
TypeError: save() takes exactly 1 argument (2 given)
>>>

I have deleted the r'new_mxd_filename.mxd')

I would also like to update the data source for a shapefile.
I have tried the following code

mxd.replaceWorkspaces(r"O:\GIS\COUNTY MAPS\Proposed Zoning\ProposedZonningAreas_Erase_E1.shp", "SHAPEFILE_WORKSPACE", r"O:\GIS\COUNTY MAPS\Proposed Zoning\FutureLandUseZoning_8_4_2011.shp", "SHAPEFILE_WORKSPACE")   



but i get an error...

C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd is being processed

Traceback (most recent call last):
  File "C:/GIS/Python Scripts/Change Data Source MXD_2_A.py", line 22, in <module>
    mxd.replaceWorkspaces("O:\GIS\COUNTY MAPS\Proposed Zoning\ProposedZonningAreas_Erase_E1.shp", "SHAPEFILE_WORKSPACE", "O:\GIS\COUNTY MAPS\Proposed Zoning\FutureLandUseZoning_8_4_2011.shp", "SHAPEFILE_WORKSPACE")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 915, in replaceWorkspaces
    return convertArcObjectToPythonObject(self._arc_object.replaceWorkspaces(*gp_fixargs((old_workspace_path, old_workspace_type, new_workspace_path, new_workspace_type, validate), True)))
ValueError: MapDocObject: Unexpected error
>>>
0 Kudos
TonyAlmeida
Occasional Contributor II
I have figured out to replace the data source for shapefile.

Now i trying to replace the data source for a ArcSDE Personal Server, but i am receiving the following error.
any help would be gratefully appreciated.

my current code,

import arcpy
from arcpy import env
from arcpy import mapping

arcpy.env.workspace = workspace = "C:\GIS\MAPBOOK\Proposed Zoning Book"

arcpy.env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd = workspace + "\\" + mxd
    print mxd + " is being processed"
    mxd = arcpy.mapping.MapDocument(mxd)
    for lyr in arcpy.mapping.ListLayers(mxd): 
        if lyr.name == "CITY LIMITS":
                lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits")
    print "Successfully updated data sources"
    mxd.save()



I get the following error:

C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd is being processed

Traceback (most recent call last):
  File "C:/GIS/Python Scripts/Change Data Source MXD4.py", line 18, in <module>
    lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay", "FILEGDB_WORKSPACE", "City_Limits")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource
    return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error
0 Kudos
PaulLohr
Occasional Contributor III

This is marked as solved but the last post indicates that it may not be.

Is it the case that the original map document cannot be saved? I noticed that ArcCatalog's Set Data Source does not save the original but instead saves a copy. Maybe writing a new map document would be a workaround (less than ideal).

0 Kudos