replaceDataSource error

232
0
04-01-2013 08:29 AM
BetsySchenck-Gardner
Occasional Contributor
I have a python script that replaces layers in an mxd file. It checks for the existence of the SDE layer and if it exists, writes it into the mxd file. If it doesn't exist, it writes a different SDE layer into the mxd file. Everything works until I use the arcpy.Exists statement.  If I remove this statement, the replaceDataSource command works. If I keep the statement, it errors out with the extremely helpful error message "Value Error: Layer: Unexpected error". I'm at my wit's end with this one. Here's my code:
import sys, string, os, datetime, arcpy
from time import *

today = datetime.date.today()
wrkspc = r"Database Connections\Connection to HABSOS fluke pg.sde"

#  update sea surface temperature project file
print "Updating Sea Surface Temperature Project"
mxd = arcpy.mapping.MapDocument(r"d:\ArcServer_Maps\ModelRuns_AMSEAS\AMSEAS_WaterTemp_Hindcasts.mxd")
new_mxd = r"d:\ArcServer_Maps\ModelRuns_AMSEAS\updates\AMSEAS_WaterTemp_Hindcasts.mxd"
if os.path.isfile(new_mxd):
    os.remove(new_mxd)

i = 1
while i < 3:
    previous = today - datetime.timedelta(i)
    newDate = previous.isoformat()
    dateString = newDate.replace('-','')
    AMSEAS_SST = wrkspc + "\AMSEAS_SST_" + dateString
    sdeLayer = "AMSEAS_SST_" + dateString
    templateLayer = "AMSEAS_SST_Template"
    nameString = "Day " + str(i) + " - " + dateString
    lyrID = i - 1
    if arcpy.Exists(AMSEAS_SST):
        print "Found SST layer %s .. Will update project" % AMSEAS_SST
        lyr = arcpy.mapping.ListLayers(mxd)[lyrID]
        lyr.replaceDataSource(wrkspc, "SDE_WORKSPACE", sdeLayer)
        lyr.name = nameString
        del lyr
    else:
        print "Didn't find SST layer %s ... Will update project with SST template" % AMSEAS_SST
        lyr = arcpy.mapping.ListLayers(mxd)[lyrID]
        lyr.replaceDataSource(wrkspc, "SDE_WORKSPACE", templateLayer)
        lyr.name = nameString
        del lyr
    i += 1

mxd.saveACopy(new_mxd)
del mxd, new_mxd


This is the error I get when I leave the arcpy.Exists call:

Traceback (most recent call last):
  File "D:\ArcServer_Maps\ModelRuns_AMSEAS\test.py", line 28, in <module>
    lyr.replaceDataSource(wrkspc, "SDE_WORKSPACE", sdeLayer)
  File "D:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "D:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 585, in replaceDataSource
    return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error

Please help!!!!
0 Kudos
0 Replies