Updating and fixing data sources with arcpy mapping

Add Layer(df, newlayer,"BOTTOM") # Refresh things arcpy. Refresh TOC() del mxd, df, newlayer Recently I struggled with a similar task, and initially used the method of identifying the map document, identifying the data frame, creating a layer and adding the layer to the map document.

Interestingly enough, this can all be accomplished using the following provided it is called from within the current map document.

During a recent customer upgrade from ESRI 9.3 to 10SP3, we uncovered a last minute “gotcha” regarding over 1,000 map files floating around several share drives.

Walk(out_loc,datatype='Raster Dataset'): for rstr in rasters: RList.append(rstr) #walk through mxds in mxd path and generate unique list of layers sourced in all #mxd documents for dirpath, dirnames, filenames in os.walk(mxd_path): for filename in filenames: full Path = join(dirpath, filename) basename, extension = splitext(filename) if extension == ".mxd": mxd = arcpy.mapping. List Layers(mxd) for item in Lyr List: if item.supports("DATASOURCE"): lyr_lst.append(Source) lyr_lst_unique = list(set(lyr_lst)) #retrieve file names without extension from unique mxd layers lists, to compare #with contents of output gdb for lyr in lyr_lst_unique: path, file = split(lyr) fl_nm = splitext(file)[0] fl_nm_lst.append(fl_nm) #compare file names in lyr_list_unique to lists of feature classes and rasters that #already exist in output gdb.Also, you will need to make sure that there is a new data source file on the disk.Just setting a new source path does not work and will throw an exception. # Import system modules import arcpy, os #find all the MXD’s in the directory tree for root, sub Folders, files in os.walk(r”C:\Temp\Maps”): for filename in files: fullpath = join(root, filename) basename, extension = splitext(fullpath) if extension.lower() == “.mxd”: print “——————————” print filename #open the map document MXD = arcpy.mapping.In reality, this new “.sde” file would live on a shared directory, so all of the map files could reference it. Map Document(fullpath) #get all the layers for lyr in arcpy.mapping.List Layers(MXD): #get the source from the layer if lyr.supports(“workspace Path”): source = lyr.workspace Path print “%s - %s” % (lyr, source) basename, extension = splitext(source) if extension.lower() == “.sde”: #This is the NEW SOURCE that you want to point to datapath = r”C:\temp\[email protected]@new Sql Server.sde” #replace the old path wih the new And Replace Workspace Path(source, datapath) #save your changes MXD.save() del MXD The final thing to do is have some good exception handling.

Leave a Reply