changing sde datasources and saving mxd

2413
6
Jump to solution
11-09-2012 07:03 AM
PaulFrank
New Contributor II
Hello - we are migrating to new SDE datasources, and I am trying to make the changes to our mxd's.  This is complicated by new names for the geodatabases.  My issue now seems to be when saving the mxd's.  I have a number of loops, and perhaps the problem is I do not have my indentations correct.  In the end, I am not seeing the changes in the mxds.  Any suggestions are appreciated!

Paul Frank

# Author:  Paul Frank, City of Austin # Date:    November 7, 2012 # Version: ArcGIS 10.0 # Purpose:  This script will iterate through each MXD in a folder and change the SDE connection and dataset name. #           Then it will save the mxd that it has finished changing.  The script is intended to run from a script #           tool that requires a parameter with the folder containing MXDs.  #setting variables import arcpy, os arcpy.env.overwriteOutput = True print arcpy.GetMessages()  #setting variables for new data source and table that lists the old and new dataset names newdatasource = r"C:\Documents and Settings\frankp\Application Data\ESRI\Desktop10.0\ArcCatalog\DataMart.sde" sdechangetable = r"G:\NEIGHBOR PLAN\ArcView\Themes\GIS_Administration\sde_change.dbf"  #Read input parameters from GP dialog #folderPath = arcpy.GetParameterAsText(0) folderPath = r"C:\arcinfo\New Folder"  try:     #Loop through each MXD file     for filename in os.listdir(folderPath):         fullpath = os.path.join(folderPath, filename)         if os.path.isfile(fullpath):             if filename.lower().endswith(".mxd"):                 #Reference MXD                 mxd = arcpy.mapping.MapDocument(fullpath)                 arcpy.AddMessage("Changing " + mxd.filePath)                 print "changing " + mxd.filePath                 #Reference each data frame and report data                 DFList = arcpy.mapping.ListDataFrames(mxd)                 for df in DFList:                     #Reference each layer in a data frame                     lyrs = arcpy.mapping.ListLayers(mxd, "", df)                     for lyr in lyrs:                         if lyr.isFeatureLayer:                             olddataset = lyr.datasetName                             arcpy.AddMessage("Setting " + olddataset)                             print "Setting " + olddataset                             rows, row = None,None                             rows = arcpy.SearchCursor(sdechangetable, "\"OLDSDE\" = " + "'" + olddataset + "'")                             row = rows.next()                             #proceed if a match is found in sde layers list                             if row <> None:                                 newdataset = row.getValue("NEWSDE")                                 arcpy.AddMessage("Changing " + olddataset + " to " + newdatasource + " " + newdataset)                                 print "Changing " + olddataset + " to " + newdatasource + " " + newdataset                                 #attempt to change the datasource.  Datasource may not actually exist even if found in list                                 try:                                     lyr.replaceDataSource(newdatasource, "SDE_WORKSPACE", newdataset)                                     lyr.name = newdataset                                     arcpy.AddMessage(olddataset + " changed!")                                     print olddataset + " changed!"                                 except:                                     arcpy.AddError(arcpy.GetMessages(2))                             #do not proceed because no match was found in sde layers list                             else:                                 arcpy.AddMessage("Can not replace " + lyr.name)                                 print "Can not replace " + lyr.name                         else:                             arcpy.AddMessage("Trying next layer")                             print "trying next layer"     arcpy.AddMessage("Finished replacing " + filename)     mxd.save()     arcpy.AddMessage("Saved " + filename)     print "saved copy of mxd"     del mxd     del row     del rows     del folderPath, fullpath except Exception, e:   import traceback   map(arcpy.AddError, traceback.format_exc().split("\n"))   arcpy.AddError(str(e))
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
It definitely looks like you have your mxd.save() outside of your loop. I would have it as the last line under your if statement to verify your file is an mxd.

if filename.lower().endswith(".mxd"):     ...     arcpy.AddMessage("Finished replacing " + filename)     mxd.save()

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
It definitely looks like you have your mxd.save() outside of your loop. I would have it as the last line under your if statement to verify your file is an mxd.

if filename.lower().endswith(".mxd"):     ...     arcpy.AddMessage("Finished replacing " + filename)     mxd.save()
0 Kudos
PaulFrank
New Contributor II
It definitely looks like you have your mxd.save() outside of your loop. I would have it as the last line under your if statement to verify your file is an mxd.

if filename.lower().endswith(".mxd"):
    ...
    arcpy.AddMessage("Finished replacing " + filename)
    mxd.save()


Mathew,

Thanks.  That seems to be the trick.  Another issue I was having was the script would get stuck on event layers.  So I added a second qualifier to my if statement:
for lyr in lyrs:
                        if lyr.isFeatureLayer and 'sde' in lyr.dataSource: #added the 'sde' code to proceed on sde sources

In the end, I was hoping this script would do a good portion of the work required to make our transition to new SDE sources.  But there are also a lot of definition queries and symbology that will require additional changes in the mxd's themselves.  Anyways, I'm attaching the final working .py file.

Cheers,
Paul
0 Kudos
MichaelVolz
Esteemed Contributor
Paul:

In your last post you stated:

"But there are also a lot of definition queries and symbology that will require additional changes in the mxd's themselves."

Can you please explain what additional work would need to be done with definition queries and symbology when moving to a new geodatabase?
0 Kudos
PaulFrank
New Contributor II
Paul:

In your last post you stated:

"But there are also a lot of definition queries and symbology that will require additional changes in the mxd's themselves."

Can you please explain what additional work would need to be done with definition queries and symbology when moving to a new geodatabase?


Any definition queries and symbology based on old field names will need to be changed.  That will be a challenge for some of our less experienced users.  I wrote this script for my department because nothing was provided by our IT department.  The script repairs the datasource, but if a user wants the color blue to represent something, they might get green.  Arcmap 10 python does not have modules for symbols.  My understanding is we will eventually have a layer library so this sort of transition is smoother in the future.
0 Kudos
jamesdiaz
New Contributor
can you post a sample of the .dbf file used in your script?
0 Kudos
CharlesBanks1
New Contributor II
Hi All,

What would be the parameters for this tool?
Thanks
0 Kudos