I have an ArcGIS Pro Project containing layers that have shapefiles as sources. I wrote a script using ArcPy to convert this data to File Geodatabases. Everything works fine here without any issues.
The next step I want to take is replace the data sources of the same layers that I exported, so that they will be referencing the new file location in the File GDB. The best (and only?) way to replace the data source and dataset name is to use Layer.replaceDataSource . But when I use this Layer method, I get this error:
AttributeError: 'Layer' object has no attribute 'replaceDataSource'
This is because replaceDataSource is a method found only in arcpy.mapping's Layer class. In other words, I'd need to use an ArcMap map document to utilize this method, instead of Pro. Is there a way to do this using arcpy.mp library? Are there any other ways to achieve what I'm trying to do in ArcGIS Pro with ArcPy?
Thank you for the help!
Solved! Go to Solution.
As Dan mentioned this function should work.
def remap_arcgis_pro(source_aprx, target_aprx, source_sde_conn, target_sde_conn):
arcpy.AddMessage("Remapping ArcGIS Pro...")
aprx = arcpy.mp.ArcGISProject(source_aprx)
aprx.updateConnectionProperties(current_connection_info=source_sde_conn,
new_connection_info=target_sde_conn)
aprx.saveACopy(target_aprx)
return target_aprx
sounds like Updating and fixing data sources—ArcGIS Pro | Documentation
eg updateConnectionProperties
As Dan mentioned this function should work.
def remap_arcgis_pro(source_aprx, target_aprx, source_sde_conn, target_sde_conn):
arcpy.AddMessage("Remapping ArcGIS Pro...")
aprx = arcpy.mp.ArcGISProject(source_aprx)
aprx.updateConnectionProperties(current_connection_info=source_sde_conn,
new_connection_info=target_sde_conn)
aprx.saveACopy(target_aprx)
return target_aprx
I'm saving the Pro Project, but not using saveACopy(), but updateConnectionProperties doesn't seem to work. I'm updating the connectionProperties dict and replacing the original with the new dict. Any ideas on how or what to fix?
aprx = arcpy.mp.ArcGISProject(project_path)
for mp in aprx.listMaps("Map Name 1"):
for lyr in mp.listLayers():
# export out to file geodatabase with new name
cp = lyr.connectionProperties
cp['connection_info']['database'] = output_db #output db path+name
cp['dataset'] = lyr_longName
lyr.updateConnectionProperties(lyr.connectionProperties, cp)
aprx.save()
You have to use saveAs
Layer—ArcGIS Pro | Documentation and updateConnectionProperties is a method of layers in both incarnations.
I used save() instead of saveACopy() so that I can work with the same aprx file. Seems to work fine with save() after some troubleshooting now. If any issues, pop back up, I'll have to try the saveACopy() method instead, which I believe is what you're suggesting. Thanks!
saveACopy prevents disasterous overwrites... disk space is cheap
You're absolutely right. A million things that could go wrong, and I don't want to mess up my original Project file.
why updateConnectionProperties on aprx file (not lyr) didn't work?
in your code print(lyr) in the for loop to see which lyr is making problem. I am guessing it doesn't like Esri basemap layers.
I had a break statement in the look to stop the loop after the first layer, and had already removed the base layers...
I think the issue was that the data source on Pro's layers weren't getting updated in the Pro instance I had open. Once I closed it, ran the script, opened it back up again, it works as it should. Must have been just a lock issue.