2

After reading through the documentation about MapDocument and Layer classes, I have been trying to use the replaceDataSource method in ModelBuilder to replace the data sources of certain layers within a map document I specify. However, it gives a runtime error even though I state the workspace (a file geodatabase), the type ("FILEGDB_WORKSPACE") and the dataset name ("fc1").

5
  • 1
    Are the variable name mismatches just typos? I would expect a NameError if so... please post your exact code. Little differences in case and spelling make huge differences in actual execution.
    – blah238
    Commented Jul 10, 2012 at 6:49
  • I agree with @blah238, it is likely that your script will work if you clean up the code, make sure that the variable names are correct, and that you are clear in certain functions, whether you are referencing a variable, or a string. Commented Jul 10, 2012 at 7:37
  • I would also expect a Python 'Invalid Syntax' error from line 2. You shouldn't have the colon after the r. Commented Jul 10, 2012 at 7:54
  • Before I completely lost the plot, I edited the question and added more detail. Those were typos earlier, but they weren't in my original code. Commented Jul 10, 2012 at 12:52
  • After looking at my updated question today, I realised that I lost the plot again, so I've edited it with details relevant only to the question title, and then I found the answer! Posted below. Commented Jul 11, 2012 at 11:05

1 Answer 1

3

Answer: Upon further reading of the documentation, I discovered that it is not necessary to put the feature dataset name in the workspace path (as described in the general usage section). As long as fc1 is within that file geodatabase, it will be used as the new data source.

I have the following model parameters:

  • Mxd_in: The map document containing the layers
  • Input GDB: The file geodatabase containing the feature class
  • Input FC: The name of the feature class inside a feature dataset within the file geodatabase

I used the following code:

setDataSource("%mxd_in%", "%Input GDB%", "%Input FC%")

def setDataSource(md, gdb, fc);
  mxd = arcpy.mapping.MapDocument(md)
  lyr = arcpy.mapping.ListLayers(mxd)[0]  #the layer I want to update is the top one
  lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", fc)
  mxd.save()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.