Is "findAndReplaceWorkspacePath" unstable or is it me?

344
3
10-29-2010 09:38 AM
EricAubert
New Contributor II
Ok, after hours trying to figure out what's going on with the "findAndReplaceWorkspacePath" I'm just giving up and reaching out for help. So far, I am thinking that this method provides completely unexpected/unstable/unreliable results. But before I get to far with my frustrations, I need somebody to review this code and let me know what I am doing wrong. The problem is that the workspace path of the layer files don't get re-pointed to the new SDE connection file, whether I use validate = True or False. When set to True the script crashes miserably, even though the connection file/parameters is valid, so it this example I set it to False, which you would expect to work, but it does not! This scripts loops through a root folder, recursively finds all the ".lyr" file under the root and re-points the workspacePath location. It can be executed in PythonWin/Idle or the ArcMap Python window, the results are the same i.e. unexpected/unstable/unreliable i.e. the layers don't get re-pointed.

import arcpy, sys, os, traceback, time, sys, random, string
root = r"D:\eaub490\repath_layers\DNR Map Layers\Forest Management"

for path, folders, fileList in os.walk(root):
 print "*************\nFolder: " + path + "\n*************"
 for f in fileList:
  print f
  if f.endswith(".lyr"):
   lyr = arcpy.mapping.Layer(path + "\\" + f)
   if lyr.isGroupLayer == False:
    if lyr.supports("WORKSPACEPATH"):  
     if ".sde" in lyr.workspacePath:
      workPath = lyr.workspacePath
      print " * Old:", workPath
      if lyr.isFeatureLayer:
       workPathNew = r"D:\eaub490\repath_layers\sde_connections_read\ropa_v10.sde"  # this is a valid connection file
       lyr.findAndReplaceWorkspacePath("", workPathNew, False)  # when validate = True then it becomes extremely unstable and unpredictable
      else:
       workPathNew = r"D:\eaub490\repath_layers\sde_connections_read\raster_v10.sde"
       lyr.findAndReplaceWorkspacePath("", workPathNew, False)
      lyr.visible = False  # turn the visibility off
      lyr.save()  # save to v.10
      if lyr.workspacePath <> workPathNew:  # check because the findAndReplaceWorkspacePath does not always works
       print " * Dang! the findAndReplaceWorkspacePath does not work:", lyr.workspacePath
      else:
       print " * New:", lyr.workspacePath
     else:
      print " * Not a SDE layer"
    else:
     print " * Workspacepath not supported"
   else:
    print " * Group layer, cannot be re-pathed"  # as far as I know
  else:
   print " * Not a layer file"
 
print "\nEnd of program\n"
0 Kudos
3 Replies
KristinGoff
New Contributor
I was having the same problem executing this function as well.  I was also trying to set my new path to an SDE database.  It appeared to run, but after showing the hour glass for a while there would be no change.

I just executed a test using this function from one file geodatabase to another (same database name, but in a new location) and it worked like a charm.  Maybe it is only good for the latter scenario.
0 Kudos
JeffBarrette
Esri Regular Contributor
We have found that in some SDE scenarios that findAndReplaceWorkspacePath is not working, for example, going from a development database to a production database on the same server.

Try using lyr.replaceDataSource instead.

We will update the Updating And Fixing Data Sources topic once we've completed our analysis.

Jeff
0 Kudos
WaiChan1
New Contributor II
I could never get findAndReplaceWorkspacePath to work either. But I did find more success with replaceDataSource when working with SDE data. Here is the syntax I used:

lyr.replaceDataSource(r"Database Connections\<sde connection>.sde", "SDE_WORKSPACE", True)

I used this to repoint the layers in map documents in ArcMap and in folders in ArcCatalog.
0 Kudos