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").
1 Answer
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()
variable
, or astring
.