updateconnectionproperties only works if name of dataset in both geodatabases is the same

904
2
09-27-2022 06:00 PM
Labels (1)
Ams_Purdue
New Contributor II

I have >30 maps to make with ~40 layers in them.  The data for the different maps is stored in geodatabases with the same file structure BUT each layer has a unique 12 digit ID at the end of their names.  I am trying to update the geodatabase and feature class where the data sit to speed up my map making. 

The following code (adapted from https://community.esri.com/t5/python-questions/arcpy-layer-updateconnectionproperties-not-working/td...) works fine as long as the dataset from the old geodatabase has the same name as the one in the new geodatabase, but as soon as I change the dataset name (with or without a number in it), the script doesn't work. I don't get an error but it just keeps pointing to both the old gdb and the old feature class within it.

How do I fix this?

Any help would be greatly appreciated.

##

import arcpy, os
import pprint

# .aprx we are using as template
aprx = arcpy.mp.ArcGISProject(r'C:\\temp\\ESRI_aprx_change\\proj1.aprx')

# Look at this especially for how to update (joins)
# https://pro.arcgis.com/en/pro-app/2.8/arcpy/mapping/updatingandfixingdatasources.htm

# make sure I can access the gdb
# https://pro.arcgis.com/en/pro-app/2.8/arcpy/functions/describe-object-properties.htm 
##desc = arcpy.Describe(r"C:\\Users\\davisay2\\Desktop\\GEO495_USAFA_F21\\Mouse\\RestorationAreas.gdb")
##print("CatalogPath: " + desc.catalogPath)
##for child in desc.children:
##    print("\t%s = %s" % (child.name, child.dataType))

###https://community.esri.com/t5/python-questions/arcpy-layer-updateconnectionproperties-not-working/td-p/519982



# Input parameters are  the chosen map within the current project, name of new geodatabase, and name of new feature class
#mapName = arcpy.GetParameterAsText(0)
#newGDB = arcpy.GetParameterAsText(1)
#newfc = arcpy.GetParameter(2)
#===================================

## code to automate later
#map = aprx.listMaps(mapName)[0]

# Looking in Map1 in that .aprx
map = aprx.listMaps("Map1")[0]

# Looking for the feature class named RestorationAreas
layer = map.listLayers("RestorationAreas")[0]

## https://support.esri.com/en/technical-article/000021613
# Call the connectionProperties dictionary.
pprint.pprint(layer.connectionProperties)

#Specify the current data source (geodatabase) dictionary.
new_conn_prop = layer.connectionProperties
# Point to the new geodatabase base
new_conn_prop['connection_info']['database'] = r'C:\\temp\\ESRI_aprx_change\\RestorationAreasCopy.gdb'
# Point to the name of the new feature class
new_conn_prop['connection_info']['dataset'] = 'RestorationAreasCopy'
##new_conn_prp['connection_info']['workspace_factory'] = 'File Geodatabase'

## code to automate later
#new_conn_prop['connection_info']['database'] = newGDB
#new_conn_prop['connection_info']['dataset'] = newfc

layer.updateConnectionProperties(layer.connectionProperties,new_conn_prop)


# output new aprx
aprx.saveACopy(r"C:\\temp\\ESRI_aprx_change\\proj8New.aprx")

if line 47 is changed to this the code works:

# Point to the name of the new feature class
new_conn_prop['connection_info']['dataset'] = 'RestorationAreas'
0 Kudos
2 Replies
Ams_Purdue
New Contributor II

I'm going to try renaming the files in both .gdb to the same thing in the code and then name them back to what I need them. Stay tuned.

0 Kudos
Ams_Purdue
New Contributor II

This works but the fc in the new .aprx isn't named what I want it to be named...

 

import arcpy, os
import pprint

## Changing the fc class names because of ESRI bug
dest_gdb = r'C:\\temp\\ESRI_aprx_change\\RestorationAreasCopy.gdb'

# Set workspace
arcpy.env.workspace = dest_gdb

# Set local variables
in_data =  "RestorationAreasCopy" # name of the feature class I want to point to in the new aprx/map but
# it has a different name than in the original .gdb (that I'm currently point to in the map project below)
# in the original .gdb it's callled RestorationAreas so I will change it to that in the destination gdb
out_data = "RestorationAreas"
data_type = "FeatureClass"

# Run Rename
arcpy.management.Rename(in_data, out_data, data_type)


# .aprx we are using as template
aprx = arcpy.mp.ArcGISProject(r'C:\\temp\\ESRI_aprx_change\\proj1.aprx')



## Need to recode 

# Input parameters are  the chosen map within the current project, name of new geodatabase, and name of new feature class
#mapName = arcpy.GetParameterAsText(0)
#newGDB = arcpy.GetParameterAsText(1)
#newfc = arcpy.GetParameter(2)
#===================================

## code to automate later
#map = aprx.listMaps(mapName)[0]

# Looking in Map1 in that .aprx
map = aprx.listMaps("Map1")[0]

# Looking for the feature class named RestorationAreas
layer = map.listLayers("RestorationAreas")[0]

## https://support.esri.com/en/technical-article/000021613
# Call the connectionProperties dictionary.
pprint.pprint(layer.connectionProperties)

#Specify the current data source (geodatabase) dictionary.
new_conn_prop = layer.connectionProperties
# Point to the new geodatabase base
new_conn_prop['connection_info']['database'] = r'C:\\temp\\ESRI_aprx_change\\RestorationAreasCopy.gdb'
# Point to the name of the new feature class
new_conn_prop['connection_info']['dataset'] = 'RestorationAreas' # No need to use this since the renaming up top should have taken care of it.
##new_conn_prp['connection_info']['workspace_factory'] = 'File Geodatabase'

## code to automate later
#new_conn_prop['connection_info']['database'] = newGDB
#new_conn_prop['connection_info']['dataset'] = newfc

layer.updateConnectionProperties(layer.connectionProperties,new_conn_prop)


## Changing the feature class name back to what it needs to be (because of ESRI bug)
# Run Rename again
#arcpy.management.Rename(out_data, in_data, data_type)

# output new aprx
aprx.saveACopy(r"C:\\temp\\ESRI_aprx_change\\proj5New.aprx")

 

0 Kudos