Updating Data Source in ArcGIS PRO project with arcpy

13090
34
03-22-2019 10:55 AM
Arne_Gelfert
Occasional Contributor III

Trying to update data source for layers in ArcGIS Pro maps programmatically. So, I must be misunderstanding the syntax for doing that explained here. Because the following does not work:

import arcpy

aprx = arcpy.mp.ArcGISProject(r'path to my project file')

#This gets me list of all the maps
maplist = aprx.listMaps()

#If I only want those belonging to MyGroup
mygroup = aprx.listMaps('MyGroup')[0]

#To get the layers in that group
grouplayers = mygroup.listLayers()

new_sdeConn = r'path to my SDE connection file'

for lyr in grouplayers:
    old = lyr.connectionProperties	#also tried lyr.connectionProperties['connection_info']
    lyr.updateConnectionProperties(old, new_sdeConn)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This doesn't throw an error. it just doesn't do update the data sources as desired. The ESRI example goes like:

aprx.updateConnectionProperties(r'C:\Projects\YosemiteNP\Vector_Data\Yosemite.gdb',
                                r'C:\Projects\YosemiteNP\DBConnections\Server.sde')‍‍‍‍

Does this maybe only work for the project as a whole?

0 Kudos
34 Replies
Rita_Matildes_dev
New Contributor

I have just concluded the same for 2.7.

Updating and fixing data sources funtion does not update the datasources nor the instructions in Update the data source between Enterprise geodatabases in ArcGIS Pro using ArcPy solve the problem. Connections don't update.

 

0 Kudos
diaconori
New Contributor III

Funny. My mentor has just asked me to test it in 2.7 for ArcMap... Interested to see if it'll work for us. I'll keep this thread updated on my progress.

0 Kudos
diaconori
New Contributor III

Works in 2.7 for ArcMap using: 

lyr.findAndReplaceWorkspacePath(old_path, new_path, True)

 Don't fix what's already working, Esri...

0 Kudos
mody_buchbinder
Occasional Contributor III

I found that if the layer have Join to some table then the syntax of the connectionProperties change and you cannot just replace it.

You must remove the join and then you  can update.

Then you can renew the join if the table exists

 

0 Kudos
by Anonymous User
Not applicable

Old thread, but seems to get a lot of traffic and thought I'd add a solution that worked for me.  Needing to update paths from shapefiles to a GDB, I think that the ESRI documenation for replacing parts of the path string is assuming that it is still a GDB to GDB update, like all of their other examples.  Their provided example syntax failed to update the paths so after some digging I came up with creating the connection property, which worked:

lyrcon = {'dataset': lyr.name, 'workspace_factory': 'File Geodatabase', 'connection_info': {'database': newDataPath}}

lyr.updateConnectionProperties('', lyrcon)
0 Kudos
TI
by
Occasional Contributor II

I went for the full-on sledge-hammer approach.  Here's what worked for me:

  • Add a NEW TEMPORARY layer to the map from the equivalent feature class in the new data source (using some matching of names logic from the feature class of the old layer)
  • Get the connection properties of this temporary layer (into a Python variable)
  • Remove the new temporary layer from the map
  • Replace the connection properties of the original layer with the connection properties of the (removed) temporary layer

yay!  it works at last!

0 Kudos
diaconori
New Contributor III

Could you please share your implementation? It does sound like a good, hacky approach but it'd be nice to see it with some actual code.

Thanks.

0 Kudos
TI
by
Occasional Contributor II

The code snippet below is part of my Python tool which makes this easy.

In my code, this is run within a loop for multiple layers and it very slow, as it has to wait for ArcGIS Pro to add each layer to the map and then remove it again.  ArcGIS Pro is the most unresponsive application I've used on any OS anywhere anytime ever (can you tell it annoys me?  A lot.).

Before this snippet, the logic gets a list of all featutre classes in the new Data Source and for each of the old feature classes to be changed, it checks the list to make sure that there is one with a matching name in the new Data Source to use as 'fcNew'.  If there is not, then my particular tool will output a warning, and depending on another parameter in the tool, may also remove the layer if its data source could not be updated (due to no matching FC in the new DS).

 

 

        fcNew = os.path.join(newDS, fcName)
        tmpLayer = map.addDataFromPath(fcNew)
        newProperties = tmpLayer.connectionProperties
        map.removeLayer(tmpLayer)
##        arcpy.AddMessage("OLD connection properties:\n{}".format(layer.connectionProperties))
        layer.updateConnectionProperties(layer.connectionProperties, newProperties)
##        arcpy.AddMessage("NEW connection properties:\n{}".format(layer.connectionProperties))

 

 

 

 

 

 

ZacharyUhlmann1
Occasional Contributor III

I'm just responding here to be updated if this is ever resolved.  Perhaps in another 4 years. My code and problem were pretty identical to original post - but moving from one gdb to another gdb with Feature Classes all of the same, exact name.  Tried all the solutions offered here in the comments, to no avail. 

Honestly not a bad, temporary solution if your situation allows is as follows: 1) Disconnected drive or server containing path needing change 2) Open project 3) Click the red exclamation point from all the broken paths and fix the broken paths.  In my case, much of the original data was on a gdb on an external hard drive.  If within a Pro project on server 1a) copy it to external hard drive 2a) disconnect from server 3a) fix paths as above 4a) copy back to server / disconnect external hard drive 5a) fix paths again, setting them to desired new gdb.  Screw it.

0 Kudos
JohnGaiot
Occasional Contributor

HOLY COW TI ! Your solution, although a roundabout one, worked pretty slick! I can't believe it's been so hard to find a SIMPLE solution.. Cheers. 

0 Kudos