Select to view content in your preferred language

replaceDataSource working on single mxd, but not multiple.

1255
3
Jump to solution
07-25-2013 03:52 PM
davidarnold2
Deactivated User
Im very new to Python, and Im trying to write a script to trawl through a folder of mxds and replace an incorrectly named dataset.  I have a script that works on a single map document (below)
current_mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (current_mxd) for dataframes in df:     print "DATAFRAME = " + dataframes.name + "\n" + "LAYERS: "     lyrs = arcpy.mapping.ListLayers (dataframes)     for layers in lyrs:         print "  " + layers.name     for broken in arcpy.mapping.ListBrokenDataSources (dataframes):         print broken.name + " source is missing"         if broken.dataSource == r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb\East_Timort":                broken.replaceDataSource (r"C:\Student\MapScripting10_0\PlainsView.gdb","FILEGDB_WORKSPACE","East_Timor", False) del broken del layers del dataframes del mxd arcpy.RefreshTOC()


But when I try and create this to run over multiple map documents in a folder (below), I get no change. 
import arcpy, os start = r"C:\Student\MapScripting10_0\Maps" for root, dirs, files in os.walk(start):     for mapDoc in files:         if mapDoc.endswith(".mxd"):             path = os.path.abspath(os.path.join(root,mapDoc))             mxd = arcpy.mapping.MapDocument(path)             print "\n" + "Map Document = " + path             df = arcpy.mapping.ListDataFrames (mxd)             for dataframes in df:                 print "  DATAFRAME = " + dataframes.name + "\n" + "LAYERS: "                 lyrs = arcpy.mapping.ListLayers (dataframes)                 for layers in lyrs:                     print "    " + layers.name                 del layers                 for broken in arcpy.mapping.ListBrokenDataSources (dataframes):                     print broken.name + " source is missing"                     print "    Original source = " + broken.dataSource                     if broken.dataSource == r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb\East_Timort":                         print "    Found. Attempting to fix."                         osource = r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb"                         nsource = "Timor"                         broken.replaceDataSource (osource,"FILEGDB_WORKSPACE",nsource, False)                 del broken                 del layers             del dataframes             del df             del mxd #    mxd.saveACopy(os.getcwd() + "\\" + mapDoc[:-4] + "_new.mxd")       del mapDoc del root del dirs del files arcpy.RefreshTOC() arcpy.RefreshActiveView()


Where am I going wrong? If someone could steer me in the right direction it would be greatly appreceated.

Regards,
David
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor
Well, got to run, so no time to try to explain what I did, but this code is working for me. Goes through every mxd in my folder, if it finds a broken datasource that matches the one I specified, it replaces with the new one in all mxd's in that folder.

you should be able to see what is different. I hard coded some of the paths for ease, but you could put back to variables.

import arcpy, os
from arcpy import da

ws = r'D:\test'
workspace = arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True 


start = "D:\\test"
for root, dirs, files in os.walk(start):
    for mapDoc in files:
        if mapDoc.endswith(".mxd"):
            path = os.path.abspath(os.path.join(root,mapDoc))
            mxd = arcpy.mapping.MapDocument(path)
            print "\n" + "Map Document = " + path
            df = arcpy.mapping.ListDataFrames(mxd)
            for dataframes in df:
                print "  DATAFRAME = " + dataframes.name + "\n" + "LAYERS: "
#                lyrs = arcpy.mapping.ListLayers(dataframes)
#                for layers in lyrs:
#                    print "    " + layers.name
                for broken in arcpy.mapping.ListBrokenDataSources(dataframes):
                   if broken.supports("DATASOURCE"):
                      print broken.name + " source is missing"
                      print "    Original source = " + broken.dataSource
                      if broken.dataSource == r"D:\esri_data.gdb\WasteSites\WasteSitesLine":
                          print "    Found. Attempting to fix."
                          broken.replaceDataSource(r'\\mcflight01\MCFlightData\HGIS\Data\WCH.gdb',"FILEGDB_WORKSPACE","WasteSitesLine", False)
            mxd.save()
            del dataframes
            del df
            del mxd
#    mxd.saveACopy(os.getcwd() + "\\" + mapDoc[:-4] + "_new.mxd")  
    del mapDoc


I had to make several changes to my data to test this. However, it might be as simple as saving the mxd so that the changes take effect.
Could just make that change and see if it works for you first.

R_

just noticed this also in your original code:

                    if broken.dataSource == r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb\East_Timort":
                        print "    Found. Attempting to fix."
                        osource = r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb"
                        nsource = "Timor"


Does FC "Timor" actually exist or should it be "Timort"? Or, should it be "East_Timor"

View solution in original post

0 Kudos
3 Replies
RhettZufelt
MVP Notable Contributor
Well, got to run, so no time to try to explain what I did, but this code is working for me. Goes through every mxd in my folder, if it finds a broken datasource that matches the one I specified, it replaces with the new one in all mxd's in that folder.

you should be able to see what is different. I hard coded some of the paths for ease, but you could put back to variables.

import arcpy, os
from arcpy import da

ws = r'D:\test'
workspace = arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True 


start = "D:\\test"
for root, dirs, files in os.walk(start):
    for mapDoc in files:
        if mapDoc.endswith(".mxd"):
            path = os.path.abspath(os.path.join(root,mapDoc))
            mxd = arcpy.mapping.MapDocument(path)
            print "\n" + "Map Document = " + path
            df = arcpy.mapping.ListDataFrames(mxd)
            for dataframes in df:
                print "  DATAFRAME = " + dataframes.name + "\n" + "LAYERS: "
#                lyrs = arcpy.mapping.ListLayers(dataframes)
#                for layers in lyrs:
#                    print "    " + layers.name
                for broken in arcpy.mapping.ListBrokenDataSources(dataframes):
                   if broken.supports("DATASOURCE"):
                      print broken.name + " source is missing"
                      print "    Original source = " + broken.dataSource
                      if broken.dataSource == r"D:\esri_data.gdb\WasteSites\WasteSitesLine":
                          print "    Found. Attempting to fix."
                          broken.replaceDataSource(r'\\mcflight01\MCFlightData\HGIS\Data\WCH.gdb',"FILEGDB_WORKSPACE","WasteSitesLine", False)
            mxd.save()
            del dataframes
            del df
            del mxd
#    mxd.saveACopy(os.getcwd() + "\\" + mapDoc[:-4] + "_new.mxd")  
    del mapDoc


I had to make several changes to my data to test this. However, it might be as simple as saving the mxd so that the changes take effect.
Could just make that change and see if it works for you first.

R_

just noticed this also in your original code:

                    if broken.dataSource == r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb\East_Timort":
                        print "    Found. Attempting to fix."
                        osource = r"C:\Student\MapScripting10_0\Maps\PlainsView.gdb"
                        nsource = "Timor"


Does FC "Timor" actually exist or should it be "Timort"? Or, should it be "East_Timor"
0 Kudos
davidarnold2
Deactivated User
Thanks for the quick response!

What is the line
"from arcpy import da"
this is returning an error when I attempt to run. When I removed it was fine. Thanks once again!



To answer your question "Timor" does exist, but it could as easily be "East_Timor" - essentially I want to replace what is a typo in the case of "East_Timort", but have the script adaptable for future uses.

I suppose that brings me to my next question. How to optimise this for use as a tool.
0 Kudos
RhettZufelt
MVP Notable Contributor
da is the set of data access arcpy functions introduced in 10.1.  Didn't realize I left that in my import as it is not needed for this script.  Normally needed for the new Cursors.

As far as a tool, I'm not the one to help with that.  I do everything stand alone, but would start here:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001500000006000000.htm

R_

Figured there was a typo there.  Funny, it is the typo you are coding to fix :rolleyes:
0 Kudos