Hi. I am organizing a project so that all feature layers used in map and layouts are from the project geodatabase. Initially I had 10 feature classes in the project gdb (easements.gdb) and 10 more split across three other gdbs. They are file Geodatabases. I had no problem copying the feature classes into the project geodatabase, so now all 20 are there - with the same name. Now I am struggling to change the paths of the 10 added FCs to the project geodatabase within the maps. Here is my code:
import copy
current_gdb = r'path/to/current/projet/easements.gdb'
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('easement_map')[0]
lyrs = [f for f in m.listLayers() if f.isFeatureLayer]
for lyr in lyrs:
cp = lyr.connectionProperties
cp['connection_info']['database']= copy.copy(current_gdb)
# note that dictionary updates properly when printed
print(cp['connection_info']['database'])
lyr.updateConnectionProperties(lyr.connectionProperties, cp)
However, lyr does not update
# still prints the old database
print(lyr.connectionProperties)
PRINTS: {'dataset': 'feat_name', 'workspace_factory': 'File Geodatabase', 'connection_info': {'database': 'path/to/original/whatever.gdb'}}
So the layer property dictionary is not updating. I'm baffled - this should work...
Thanks,
Zach
I'll reply to myself here. Found this post: Updating Data Source in Arcgis Pro project with Arcpy from 2019, with current posts (27 total!!). They're all having the same issue. A few people apparently hackishly solved the problem, but none worked for me. Version 3.<whatever> crazy this is still an issue.
There is no solution to updating the layer's source using Arcpy unless the source link is already broken in the layer you want to update.
Here is the other method I tried, which does not work, and outlined below:
1. Copy the feature from the layer with the symbology you want to a new geodatabase (Arcpy... EASY!)
2. Add the copied feature with the new geodatabase as a new layer to the current map. (Arcpy... EASY!)
3. The new layer with the copied feature has random symbology, this is ok... let's keep using Arcpy to fix it!
4. Use "ApplySymbologyFromLayer" to apply the same symbology to our new layer from the layer of the feature we just copied to a new geodatabase. (Should be easy... it's just Arcpy)
5. Arcpy says no... over and over again.
I can use "ApplySymbologyFromLayer" via the symbology docker. I can even copy the python code in the history. But it never works when running an Acrpy script as a tool
I am sad.