Tuesday, 8 September 2015

arcgis 10.0 - Replace data source of layer in ModelBuilder


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



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()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...