Select to view content in your preferred language

replaceDataSource error

4037
2
04-01-2013 08:48 AM
BetsySchenck-Gardner
Deactivated User
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
2 Replies
MichaelVolz
Esteemed Contributor
Can you print out the variable AMSEAS_SST?  I believe this should be a feature class.

I'm wondering if you would need to make a connection to the SDE database with this connection string to really check for its existence.  The ESRI Help sample at:

http://resources.arcgis.com/en/help/main/10.1/index.html#//018v0000004p000000

Does not show how to use python to check an SDE feature class, so I'm thinking it might not be as straightforward as checking the existence of a feature class in a personal or file geodatabase.
0 Kudos
DanJoyce1
Occasional Contributor
I'm getting the exact same error when trying to use replaceDataSource on SDE feature classes...you are not alone.  I haven't figured out a work around for this yet so any help would be appreciated!
0 Kudos