Replacedatasource not working with a shapefile

693
5
08-14-2012 04:06 PM
RobertFriedel
New Contributor II
And I can't for the life of me figure out why this isn't working.  It worked fine in a previous script when I had to resource multiple grid files in a single data frame but I can't get this one, tiny, simple, shapefile to re-source to a new workspace and name.  The workspace and name would be managed by user input but at this time I've got it hard coded.  Does anyone have any idea why this isn't working?  Script and the error I get pasted below. 

Rob

*********** THE SCRIPT ************************************************
#Return a list of all the mxds in the input folder
Inputmxds = os.listdir(INPUT_DIR)
for a in Inputmxds:
        if "Figure1" in a:
                # Open the mxd
                Themxd = arcpy.mapping.MapDocument(INPUT_DIR + "\\" + a)
               
                # Set variables to the project, turbines               
                MainDF = arcpy.mapping.ListDataFrames(Themxd, "Main View")[0]
                for feat_lay in arcpy.mapping.ListLayers(Themxd, "", MainDF):
                        #print feat_lay
                        if feat_lay.name.upper() == "PROJECT_AREA":
                                TheWRA = feat_lay
                                print TheWRA
                                #Resource the project boundary to the input wra
                                TheWRA.replaceDataSource("C:\Python_Avian_Reports", "SHAPEFILE_WORKSPACE", "Project_Boundary.shp")


*********** THE ERROR MESSAGE ************************************************
Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
    exec codeObject in __main__.__dict__
  File "P:\GIS_MANAGEMENT\ESRI_Related\Library_Python\Arc10\Avian_Report_Figures\AvianReportFigures_Hardcoded.py", line 39, in <module>
    TheWRA.replaceDataSource("C:\Python_Avian_Reports", "SHAPEFILE_WORKSPACE", "Project_Boundary.shp")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\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
Tags (2)
0 Kudos
5 Replies
GraemeBrowning
Occasional Contributor III
On this line:

TheWRA.replaceDataSource("C:\Python_Avian_Reports", "SHAPEFILE_WORKSPACE", "Project_Boundary.shp")

try replacing:

"C:\Python_Avian_Reports"

with:

r"C:\Python_Avian_Reports"
or
"C:\\Python_Avian_Reports"
or
"C:/Python_Avian_Reports"

- Graeme
0 Kudos
curtvprice
MVP Esteemed Contributor
Here's the help reference for paths that goes into detail why and some other good tips about paths:

Arc 10.0 help: Paths explained: Absolute, relative, UNC, and URL
0 Kudos
RobertFriedel
New Contributor II
On this line:


try replacing:

"C:\Python_Avian_Reports"

with:

r"C:\Python_Avian_Reports"
or
"C:\\Python_Avian_Reports"
or
"C:/Python_Avian_Reports"

- Graeme



Thanks for the feedback but none of those choices worked.  The absolute path isn't even supposed to be there, I was just using it as a place holder thinking one of my variables was incorrect but I don't think that's the case.  I'm going to try a different approach and see what's up.  Thanks again you guys for the feedback.
0 Kudos
GraemeBrowning
Occasional Contributor III
Good luck with the alternative approach but if you need to post more code it would be good to see what the/a value of your INPUT_DIR variable is, and to see the code indented (use the # button to preserve it).

- Graeme
0 Kudos
curtvprice
MVP Esteemed Contributor
  if you need to post more code it would be good to see what the/a value of your INPUT_DIR variable is, and to see the code indented (use the # button to preserve it).


Yes!
[post=224499]Please read: How to post Python code[/post]
0 Kudos