Select to view content in your preferred language

Update Data Connection with CIM Changes Dictionary but not Feature Source in TOC

76
1
Jump to solution
9 hours ago
ZacharyUhlmann1
Occasional Contributor III

Hi,

Updating data connections through the CIM seems to have different recipes with different use cases.  In this current case, I am trying change data sources for multiple layers in multiple maps in multiple projects wherein the path/to/gdb and the feature dataset change (feature class remains the same in this case).

So change source to a new geodatabase in a new folder, with a new feature dataset.

For the below example, the lyrCIM object updates the dictionary and retains it, yet the layer in the TOC does not change.

i.e. lyrCIM.featureTable.dataConnection will reflect my changes when I inspect (print) the properties featureDatasetworskpaceConnectionString,etc.

 

# Specify the new geodatabase properties
newGDB =r'path\to\new\whatever.gdb'
origGDB = r'path\to\original\whatever.gdb'
newFeatureClass = r'land_ownership'
# original Feature Dataset was something else
newFeatureDataSet = r'mapping'

# Reference project, map and layer 
fp_aprx = r"path/to/pro_project.aprx"
p = arcpy.mp.ArcGISProject(fp_aprx)
m = p.listMaps('layer_name')[0]
l = m.listLayers('map_name')[0]

# Get the layer's CIM definition
lyrCIM = l.getDefinition('V3')         

dc = lyrCIM.featureTable.dataConnection
dc.workspaceConnectionString = f'DATABASE={newGDB}'
# Note that the name is the same, but this will vary by case
dc.dataset = newFeatureClass

# If the data is in a Feature Dataset, then update it 
if hasattr(dc, "featureDataset"):
    dc.featureDataset = newFeatureDataSet

l.setDefinition(lyrCIM)
p.saveACopy(fp_aprx)

 

This code was basically verbatim from here Using the CIM to change a layer's dataset  (#3, under subtitle displayed in link; towards bottom of page).  I also tried #4 to no avail.

Note that I have had success in the past My previous post with updating source within same gdb 

Is there no programmatic solution to my situation?

v.3.0.2

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
ZacharyUhlmann1
Occasional Contributor III

I Found a solution thanks to the bread crumbs from this post Using updateConnectionProperties to update a single layer with ArcPy .  It appears to be a bug.  To circumvent it, do this:

aprx = arcpy.mp.ArcGISProject('current')
# Then do all the exact same dataConnection updates and save with the path
fp_aprx = 'path/to/project.aprx'
aprx.saveACopy(fp_aprx)

So, basically just load the aprx object using the argument, "current" - not "path/to/project.aprx".  Unsure what's going on, but I can confirm that this solved the problem.

View solution in original post

0 Kudos
1 Reply
ZacharyUhlmann1
Occasional Contributor III

I Found a solution thanks to the bread crumbs from this post Using updateConnectionProperties to update a single layer with ArcPy .  It appears to be a bug.  To circumvent it, do this:

aprx = arcpy.mp.ArcGISProject('current')
# Then do all the exact same dataConnection updates and save with the path
fp_aprx = 'path/to/project.aprx'
aprx.saveACopy(fp_aprx)

So, basically just load the aprx object using the argument, "current" - not "path/to/project.aprx".  Unsure what's going on, but I can confirm that this solved the problem.

0 Kudos