Select to view content in your preferred language

Change datasource in .lyr files in specific folder

10035
20
10-12-2017 12:48 PM
JoseSanchez
Frequent Contributor

Good afternoon everyone,

We have a folder  with 175 .lyr files. All .lyr files are pointing to an SDE instance and we need to change each lyr data source to point to another SDE instance.

I am looking for a way to do that in python where I can loop through all files in the folder and change their datasource.

Thanks

0 Kudos
20 Replies
MicahBabinski
Frequent Contributor

I think Dan Patterson‌ nailed it - you gotta save the layer file.

lyr.save()
0 Kudos
DanPatterson_Retired
MVP Emeritus

you seem to be missing the except portion of your last try... what happens if it fails?

see my previous comment about saving mxd's...   and for sde stuff, does it need to be updated or something if things are shuffling around in there?

0 Kudos
JoseSanchez
Frequent Contributor

Sorry I did not copy paste the exception part of my code:

except Exception as e:
    print "Python Error occurred"
    #print exception message to the Interactive window
    print e
    #place error message in geoprocessing messages
    arcpy.AddError(e)
    logging.error(": %s" % str(e))
 
    sys.exit(1)

except arcpy.ExecuteError:
    print "Geoprocessing error occurred"
    print arcpy.GetMessages(2)
    logging.error(": %s" % arcpy.GetMessages(0))
  
    sys.exit(1)

except:
    print "Error occurred"
    #print any geoprocessing messages to the Interactive window
    #print arcpy.GetMessages(0)


 

0 Kudos
DanPatterson_Retired
MVP Emeritus

save....

JoseSanchez
Frequent Contributor

Hi Dan,

This script works fine for the .lyr files that are pointing to a SDE instance that is active. But there are a few that are pointing to an old instance has been retired 2 or 3 years ago. These .lyr file made the program stop

Error Message:

Replace Datasource for xxx.yyy.ProjectLine
Python Error occurred
Layer: Unexpected error
ERROR: Layer: Unexpected error
ERROR:root:: Layer: Unexpected error

0 Kudos
MichaelVolz
Esteemed Contributor

I believe you can check for a broken datasource and skip trying to process it and just put that lyr information into a log file so you can process those lyr files manually.

If you try to access these old SDE instances in ArcCatalog, does it take a long time for ArcCatalog to say that the datasource cannot be found?  From past experiences broken SDE datasources can take some time for ArcMap or ArcCatalog to resolve (between 3-40 minutes).

0 Kudos
JoseSanchez
Frequent Contributor

Hi Michael,

Yes it takes very long to right clock on the .lyr and goo to properties to fix the connection for the ones that are broken.

0 Kudos
MichaelVolz
Esteemed Contributor

I believe there is an Idea on the ESRI GeoNet to modify ArcMap and/or ArcPro to not attempt to find broken connections for longer than a smaller amount of time (something like 1-2 minutes instead of up to 40 minutes which some endusers experienced).

0 Kudos
JoseSanchez
Frequent Contributor

Hello everyone,

In some cases when I run the line to change the workspace, it does not work. Do variables find_workspace_path and replace_workspace_path need to include the feature dataset or only the connection path?

lyr.findAndReplaceWorkspacePath(find_workspace_path=lyr.workspacePath, replace_workspace_path=r"Database Connections\Esde8_WASDREAD.sde")

 lyr.save():

It fails:

Python Error occurred
Layer: Unexpected error
ERROR: Layer: Unexpected error
ERROR:root:: Layer: Unexpected error

These are the values for find_workspace_path and replace_workspace_path:

SValve.lyr

feature class whole path: Database Connections\SQLInstance.sde\Sewer.SewerCollection\Sewer.SValve

find_workspace_path= C:\Users\mydesktop\AppData\Roaming\Esri\Desktop10.2\ArcCatalog\OracleInstance.sde

replace_workspace_path= Database Connections\SQLInstance.sde

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

You may want to check my python addin /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...‌ to see if that will help.  Use ass is, if it helps, or take a look at the code if you want something simpler.

0 Kudos