I am about to break a lot of data sources in our team's .aprx files because we are overhauling the places we are storing our data. Our data is stored in shapefile and file geodatabase formats.
Is there a way to programmatically do this? I have found snippets online that answer slightly different questions, like changing a gdb or SDE connection. But in my case it might be
- changing shapefile/feature class name,
- moving a GDB or shapefile to a different folder, or
- changing to a different GDB, different feature class,
- or some combo of the above.
the examples I found online only go down to the gdb level, as in changing to a new GDB and all the other features inside will magically get their links fixed. Trying that method with the full path name to the feature class level didn't work for me.
I have so far got a script that
- goes thru folders to look for .aprx files
- uses the aprx object to return broken links eg. my_broke_list = my_aprx.listBrokenDataSources()
- and I have stored my data changes in a spreadsheet. I use pandas to read the columns for old and new. and was hoping to use that info to change the data source.
The missing piece is updating the data source. I've tried it different ways but each time the result is a new aprx file with the same old broken links. I've tried using the aprx property and the layer properties.
So far my only conclusion is that lyr.updateConnectionProperties(old,new) doesn't do what it is supposed to be doing...
I'm just testing on a code snippet trying to figure out how to do the layer connection fix and I've tried a few things. Latest version is:
import arcpy
arcpy.env.overwriteOutput = True
aprx = r"Q:\blah\testBrokenLinks\small_test.aprx"
aprxNew = r"Q:\blah\testBrokenLinks\small_test4.aprx"
arpxObj = arcpy.mp.ArcGISProject(aprx)
old = r'Q:\blah\testBrokenLinks\lfsa000b21a_e.shp'
new = r'Q:\blah\testBrokenLinks\postal_codes.shp'
for m in arpxObj.listMaps():
for lyr in m.listLayers():
if lyr.isBroken:
print('broke')
if lyr.supports("DATASOURCE"):
if lyr.dataSource == old:
print(old)
lyr.updateConnectionProperties(old,new)
print(new)
arpxObj.saveACopy(aprxNew)
print ('finito')any help or examples on how to do this?