0

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.

  • I've looked at it numerous times and I simply don't have the experience to extrapolate. Any sample script pointing me in the right direction would be greatly appreciated. – GIS_GOD Mar 10 '17 at 20:23
  • 2
    There will be a number of scripts on this site already that illustrate this. – PolyGeo Mar 10 '17 at 20:25
  • I've deleted two of my comments above because I had forgotten that dataSource is a read-only property of a Layer object. – PolyGeo Mar 11 '17 at 2:38
3

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()
  • Extremely helpful Midavalo, I was initially getting an error but figured out the problem was a missing "\" for the geodatabase the contained the feature class. – GIS_GOD Mar 13 '17 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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