Replace data source comes back with empty mxd

357
4
02-02-2022 04:09 AM
ThomasPiggott
New Contributor

Hi

I am having issues with the arcpy.replacedatasource function. I am currently updating a script we use at work. The script is intended to search through the servers for certain datasets, export them using a buffer to a geodatabase and then after creating a setup mxd replace the data sources for each of the layers if a new dataset in the geodatabase exists. The idea behind this is so that the symbology that has been set up for each layer located in the tender setup mxd remains in the new mxd. 

Ive provided a code snippet below of how it is intended to work for one dataset. At the moment, the mxd that is created is completely empty, no errors are shown. If I remove the replacedatasource functions  then the created mxd has all the layers and broken links shown and the created geodatabases exist within the specified geodatabase. 

import arcpy, os, shutil

# --- Check out Spatial Analyst --------------------------------------------------------------------------
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

# studyAreaSize = Study area buffer size (metres) (String)
studyAreaSize = 1000
# siteName = Site name (String)
siteName = "TheStreet"
# projectNumber = Project number (String)
projectNumber = "1234"
# GISFolder = Location of GIS folder (Folder)
GISFolder = r"D:\Projects\1234\GIS"
#geodatabase = location of geodatabase
geodatabase = r"D:\Projects\1234\GIS\Databases\Test.gdb"
# dataFrom = Where should the data come from? Salisbury, Sheffield, Kent, Edinburgh - Value List
dataFrom = "Salisbury"

#create Study Area

arcpy.Buffer_analysis(r"D:\Projects\1234\GIS\SHP\Site\Site.shp", "D:/Projects/1234/GIS/Databases/Test.gdb/StudyArea", studyAreaSize,"","","ALL")

#locate previous project data
if dataFrom == "Salisbury":
     mappingFolder = r"X:/GIS/Data/WA Mapping/SHP"
else:
    mappingFolder = "R:/GIS/WA Mapping/SHP"     

studyAreas = os.path.join(mappingFolder, "WA_Project_Study_Areas.shp")
arcpy.MakeFeatureLayer_management(studyAreas, "WA_studyAreas")

# --- Extract previous projects data
arcpy.SelectLayerByLocation_management("WA_studyAreas", "INTERSECT", "D:/Projects/1234/GIS/Databases/Test.gdb/StudyArea", "1000")
featuresCount = arcpy.GetCount_management("WA_studyAreas")
if str(featuresCount) == "0":
    arcpy.AddMessage("No WA study areas in the vicinity")
else:
    arcpy.conversion.FeatureClassToGeodatabase("WA_studyAreas", geodatabase)

# --- Copy the template MXD 
arcpy.SetProgressorLabel("Copying the template MXD")
# --- Template source ---------
if dataFrom == "Salisbury":
    srcMXD = "X:/GIS/_Templates/Heritage.mxd"        

# Create mxd destination path
dstMXDName = projectNumber + "_" + siteName + "_Setup.mxd"
dstMXD = os.path.join(GISFolder, "MXD", dstMXDName)
shutil.copy(srcMXD, dstMXD)

mxd = arcpy.mapping.MapDocument(dstMXD)

# Get data frame for removing and updating layers
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# Loop through layers - remove or fix sources
for lyr in arcpy.mapping.ListLayers(mxd):

# Check WA Project Data
    if lyr.name == "Study areas":
        lyr.replaceDataSource(geodatabase, "FILEGDB_WORKSPACE", "WA_StudyAreas")
    else:
        arcpy.mapping.RemoveLayer(df, lyr)

    if lyr.name == "HERs":
        if dataFrom == "Salisbury":
            lyr.replaceDataSource("X:/GIS/Data/HERs/SHP", "SHAPEFILE_WORKSPACE", "HERs")
# Save the MXD
mxd.save()

 

 

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

to format your code and to provide line numbers for reference... as it stands it looks like you have a load of format errors


... sort of retired...
0 Kudos
ThomasPiggott
New Contributor

thanks @DanPatterson  I have reformated the code 

0 Kudos
DanPatterson
MVP Esteemed Contributor

are your last 3 lines indented properly? they are outside the loop starting on line 58


... sort of retired...
0 Kudos
ThomasPiggott
New Contributor

Missed that, lines indented now 

0 Kudos