Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

replaceDataSource working on single mxd, but not multiple.

$
0
0
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)
Code:

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.
Code:

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

Viewing all articles
Browse latest Browse all 2485

Latest Images

Trending Articles



Latest Images