Select to view content in your preferred language

updateConnectionProperties doesn't work in group layers?

187
3
Jump to solution
a month ago
AlexanderGray
Regular Contributor II

ArcGIS pro 3.3.1 here I have a map with some group layers.  Inside the group layers there are layers pointing to two different filegeodatabase.  I need to update one on the fgdbs (the old database) for the entire map and leave the other (otherdatabase) one intact.  

The problem I am trying to use updateConnectionProperties with the old and the new fgdb but the problem is as soon as a layer where the source is the fgdb I am NOT updating, it stops updating the layers below.  So it will do the first few layers then stop and leave the ones at the bottom with the original database.  If I take the layers out of group or if I place the ones that I am not changing below, it works.  Changing the order is not practical.   The schema of the old and the new database is identical.  The other database is different.  Everything is on the local workstation.

aprx = arcpy.mp.ArcGISProject('CURRENT')
mymap = aprx.activeMap
mymap.updateConnectionProperties(current_connection_info=r'C:\oldpath\database.gdb', new_connection_info=r'C:\newpath\database.gdb', ignore_case=True)

AlexanderGray_0-1720547567670.png

 

0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
Occasional Contributor III

Maybe use the group layers name.

 

import arcpy

# Paths to the old and new geodatabases
old_geodatabase_path = r"C:\path\to\old\geodatabase.gdb"
new_geodatabase_path = r"C:\path\to\new\geodatabase.gdb"

# Define the path to your map document
mxd_path = r"C:\path\to\your\Text.mxd"

# Group layer names that need their layers' connections updated
target_group_layer_names = ["GroupLayer1", "GroupLayer2"]

mxd = arcpy.mapping.MapDocument(mxd_path)

# update connection properties for layers within a group layer
def update_layer_connection_properties(layer, old_path, new_path):
    if layer.isGroupLayer:
        for sublayer in layer:
            if sublayer.supports("dataSource"):
                sublayer.replaceDataSource(new_path, "FILEGDB_WORKSPACE")

# Iterate over each data frame in the map document
for df in arcpy.mapping.ListDataFrames(mxd):
    for layer in arcpy.mapping.ListLayers(mxd, "", df):
        if layer.isGroupLayer and layer.name in target_group_layer_names:
            update_layer_connection_properties(layer, old_geodatabase_path, new_geodatabase_path)
    
    # update the data frame source if needed
    df.updateConnectionProperties(old_geodatabase_path, new_geodatabase_path)


mxd.save()

# Clean up
del mxd

 

View solution in original post

0 Kudos
3 Replies
TonyAlmeida
Occasional Contributor III

Maybe use the group layers name.

 

import arcpy

# Paths to the old and new geodatabases
old_geodatabase_path = r"C:\path\to\old\geodatabase.gdb"
new_geodatabase_path = r"C:\path\to\new\geodatabase.gdb"

# Define the path to your map document
mxd_path = r"C:\path\to\your\Text.mxd"

# Group layer names that need their layers' connections updated
target_group_layer_names = ["GroupLayer1", "GroupLayer2"]

mxd = arcpy.mapping.MapDocument(mxd_path)

# update connection properties for layers within a group layer
def update_layer_connection_properties(layer, old_path, new_path):
    if layer.isGroupLayer:
        for sublayer in layer:
            if sublayer.supports("dataSource"):
                sublayer.replaceDataSource(new_path, "FILEGDB_WORKSPACE")

# Iterate over each data frame in the map document
for df in arcpy.mapping.ListDataFrames(mxd):
    for layer in arcpy.mapping.ListLayers(mxd, "", df):
        if layer.isGroupLayer and layer.name in target_group_layer_names:
            update_layer_connection_properties(layer, old_geodatabase_path, new_geodatabase_path)
    
    # update the data frame source if needed
    df.updateConnectionProperties(old_geodatabase_path, new_geodatabase_path)


mxd.save()

# Clean up
del mxd

 

0 Kudos
AlexanderGray
Regular Contributor II

this sort of works but some layers are not in group layers at all.   I can just get all the layers from the map and then iterate through each one and check if supports('datasource') and attempt to change regardless of source database.  I still don't know why it doesn't work on the map though.  I used updateConnectionProperties on the layer.  That way if the source is the other fgdb it won't do anything.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Maybe this is a bug in how updateConnectionProperties() works on workspaces. I have had luck iterating over each layer rather than applying to the whole map.

 

for layer in mymap.listLayers():
    layer.updateConnectionProperties(
        current_connection_info=r'C:\oldpath\database.gdb',
        new_connection_info=r'C:\newpath\database.gdb',
        ignore_case=True
    )