Hello,
I'm trying to automate a process for my collegues with ArcPy. A part of this process is to change the datasource of a single layer in a map to a new File Geodatabase. After searching the documentation i found this would be possible by using the updateConnectionProperties. I've been trying to get the following script to work, but no success so far:
import arcpy
# parameters
path = r"C:\MyProjects\ProjectX"
project = r"Kadastraal.aprx"
aprx = arcpy.mp.ArcGISProject(path+"\\"+project)
mapx = aprx.listMaps("Vastgoedobject1")[0]
layer = mapx.listLayers("Vastgoedobject")
#Change datasource of specified layer
old_file = r"C:\Data\OldData.gdb"
new_file = r"C:\NewData\newData.gdb"
for lyr in layer:
print (lyr.connectionProperties)
print ("Start updating datasource: " + lyr.name)
lyr.updateConnectionProperties({'dataset': 'Percelen_poly',
'workspace_factory': 'File Geodatabase', 'connection_info': {'database':
old_file}}, {'dataset': 'Percelen_poly', 'workspace_factory': 'File
Geodatabase', 'connection_info': {'database': nieuw_bestand}})
print("Datasource of " + lyr.name + " is updated")
print (lyr.connectionProperties)
This gives no errors for me and if i look at the print statements it seems to update the source. But if i look at the map it still shows the old connection.
The only way i have found it works is if i add aprx.saveACopy (r"C:\Temp"). Then the connection is updated in the new aprx file. I tried using aprx.save() instead. but that just gives met the below error. Is this a known issue? Or am i doing something wrong?
OSError Traceback (most recent call last)
In [19]:
Line 22: aprx.save()
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in save:
Line 333: return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))
OSError: C:\MyProjects\ProjectX\Kadastraal.aprx
An issue for some as noted.
Updating and fixing data sources—ArcGIS Pro | Documentation
says
If no matches are found when you replace the current_connection_info parameter with the new_connection_info parameter in the updateConnectionProperties function, your script may complete, but nothing will be updated.
The code examples seem to prefer/expect that saveACopy. unless you break the connections first (topic 5)
The other related threads don't provide and simple direct solutions either.
@Jeroen_S I solved it. I deserve a medal and a cumulative ~16 hours of my life back from multiple thwarted attempts to figure this issue out over the past two years. On that note, I hope @DanPatterson already has one, because he's the constant anchor of reason, answering questions on these forums. Anyways, it must be a bug. But your clue to aprx.saveACopy() to a different location worked(!) and subsequently got me tinkering. The solution to this bug is to load the aprx object as such:
aprx = arcpy.mp.ArcGISProject('current')
# Then do all the exact same dataConnection updates and save with the path
aprx.saveACopy(fp_aprx)
So, basically just load the aprx object using "current" not "path/to/project.aprx". I think this is specific to various changes we are making to the dataConnection, because I've passed path/to/aprx under different circumstances, i.e. same gdb, but remapping to updated names (i.e. <filename>_v2). Anyways, below is a post I made today prior to determining the issue. Nice work.