Changing data source of layer in ArcGIS Pro Project using ArcPy

554
10
Jump to solution
12-21-2021 02:54 PM
KevinCheriyan
Occasional Contributor

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!


--------------------------------------------------
Application Developer, GeoMarvel
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
AzinSharaf
Occasional Contributor II

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

 

View solution in original post

10 Replies
DanPatterson
MVP Esteemed Contributor

sounds like Updating and fixing data sources—ArcGIS Pro | Documentation 

eg updateConnectionProperties


... sort of retired...
AzinSharaf
Occasional Contributor II

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

 

KevinCheriyan
Occasional Contributor

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()

 


--------------------------------------------------
Application Developer, GeoMarvel
0 Kudos
DanPatterson
MVP Esteemed Contributor

You have to use saveAs

Layer—ArcGIS Pro | Documentation  and updateConnectionProperties is a method of layers in both incarnations.

 


... sort of retired...
0 Kudos
KevinCheriyan
Occasional Contributor

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!


--------------------------------------------------
Application Developer, GeoMarvel
0 Kudos
DanPatterson
MVP Esteemed Contributor

saveACopy prevents disasterous overwrites... disk space is cheap


... sort of retired...
KevinCheriyan
Occasional Contributor

You're absolutely right. A million things that could go wrong, and I don't want to mess up my original Project file.


--------------------------------------------------
Application Developer, GeoMarvel
0 Kudos
AzinSharaf
Occasional Contributor II

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. 

0 Kudos
KevinCheriyan
Occasional Contributor

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. 


--------------------------------------------------
Application Developer, GeoMarvel