I'm trying to source a layer in my TOC with a new feature class. I've read countless forums, ArcGIS Resource Center, etc. With the MXD and python window open this is what I've surmised so far.
import arcpy
mxd = arcpy.mapping.MapDocumnet("CURRENT")
Using the following link, which scenario matches what I want to do most accurately?
MXD Source = r'\\na\data\telecom\projects\GIS\Proposal_Kentucky_Nextgen\02GISData\18DDP_ENG\MXDS\ENCHROACHMENT MXD\0180.SA - C001-C010 - 1.0 - PE.mxd
MXD Name = 0180.SA - C001-C010 - 1.0 - PE.mxd
Layer Name in TOC = STRUCTURE_SITEID_180
Layer in TOC's path = \\na\data\telecom\projects\GIS\Proposal_Kentucky_Nextgen\02GISData\18DDP_ENG\Design_Data.gdb\STRUCTURE_SITEID_180
Feature Class I Want to Source to Name = STRUCTURE_SITEID_211
Feature Class I Want to Source to File Path (same as the layer I'm trying to resource) = \\na\data\telecom\projects\GIS\Proposal_Kentucky_Nextgen\02GISData\18DDP_ENG\Design_Data.gdb\STRUCTURE_SITEID_211
How do I finish the Python script to accomplish the task? If I can get one to work I can extrapolate. I'm using ArcMap 10.3.1.
Answer
The documentation at Updating and fixing data sources with arcpy.mapping says
- findAndReplaceWorkspacePath or findAndReplaceWorkspacePaths allows you to substitute an entire or partial string for a layer or table's workspace path. It can't be used if the workspace type or dataset name has changed. It is ideal for scenarios where drive letters change, switch to UNC paths, update SDE connection file information, and so on. See the Common Scenarios section below.
- replaceWorkspaces or replaceDataSource allows you to change the workspace path, workspace type, and / or change the dataset name.
(emphasis mine)
So in order to change the layer dataset source you will need to use .replaceDataSource()
.
You will need to loop through your layers and use layer.replaceDataSource()
to update the datasource and layer.name
to update the layer name.
Something along the lines of the following:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)
oldLayerName = 'STRUCTURE_SITEID_180' # Current name of the layer you wish to change
myGDB = r'\na\data\telecom\projects\GIS\Proposal_Kentucky_Nextgen\02GISData\18DDP_ENG\Design_Data.gdb' # Geodatabase that contains the new feature class
newFCName = 'STRUCTURE_SITEID_211' # Name of the feature class in the GDB
newLayerName = 'STRUCTURE_SITEID_211' # Name of the layer in ArcMap TOC - doesn't need to be the same as the feature class name
for layer in layers:
if layer.name == oldLayerName:
layer.replaceDataSource(myGDB, "FILEGDB_WORKSPACE", newFCName)
layer.name = newLayerName
arcpy.RefreshTOC()
No comments:
Post a Comment