Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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").

share|improve this question
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 Jul 10 '12 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. –  Get Spatial Jul 10 '12 at 7:37
    
I would also expect a Python 'Invalid Syntax' error from line 2. You shouldn't have the colon after the r. –  MappaGnosis Jul 10 '12 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. –  Arabella Jul 10 '12 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. –  Arabella Jul 11 '12 at 11:05
add comment

1 Answer

up vote 1 down vote accepted

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()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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