Select to view content in your preferred language

Changing LYR from absolute to relative path through ArcPy

5150
5
08-31-2010 08:45 AM
TaraMacey
New Contributor
I need to change thousands of LYR files to use relative paths instead of absolute.  Following the example in help I got this:

import arcpy, os
folderPath = r"C:\Testing\ArcGIS10_release\paths"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".lyr":
            lyr = arcpy.mapping.Layer(fullpath)
            lyr.findAndReplaceWorkspacePath (r"C:\Testing\ArcGIS10_release\paths", r"\\ Testing\ArcGIS10_release\paths ")
            lyr.save()
del lyr

The paths are coming out as broken when I move them to the same path but a different directory (D:\Testing\ArcGIS10_release\paths).  I think I need to append the file name on the end of the path for this to work - but am not sure how.

Any help?
0 Kudos
5 Replies
JeffBarrette
Esri Regular Contributor
Hello Tara,

I would change the following line:

lyr.findAndReplaceWorkspacePath (r"C:\Testing\ArcGIS10_release\paths", r"\\ Testing\ArcGIS10_release\paths ")

to:

lyr.findAndReplaceWorkspacePath (r"C:\Testing\ArcGIS10_release\paths", r"D:Testing\ArcGIS10_release\paths")

There were some double slashes and extra spaces.  I looked like you were trying to set UNC paths which is supported.

I doesn't appear that you can replace layer file data source paths with relative paths (e.g., r"..\ArcGIS10_release\paths")

Feel free to follow-up with me at jbarrette@esri.com

Thanks,
Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
Luke,

The only way I know to make data sources relative is:

1) set the Map Document properties to store relative paths.  Each layer in the map document will be relative to the MXD.
2) Use the same setting in the map document and then save layers out to layer files.  These can then be added relative to the MXD.

When viewing a data source, it will always be the full path.

Jeff
0 Kudos
jayshukla
Regular Contributor
Hi Jeffrey ,
I am having a similar issue and and I am able to set the path to UNC path. The issue that I am getting is with Layer files with nested data sources.

Would you have any input on how to make this happen.

Thanks
Jay 

I need to change thousands of LYR files to use relative paths instead of absolute.  Following the example in help I got this:

import arcpy, os
folderPath = r"C:\Testing\ArcGIS10_release\paths"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".lyr":
            lyr = arcpy.mapping.Layer(fullpath)
            lyr.findAndReplaceWorkspacePath (r"C:\Testing\ArcGIS10_release\paths", r"\\ Testing\ArcGIS10_release\paths ")
            lyr.save()
del lyr

The paths are coming out as broken when I move them to the same path but a different directory (D:\Testing\ArcGIS10_release\paths).  I think I need to append the file name on the end of the path for this to work - but am not sure how.

Any help?
0 Kudos
JeffBarrette
Esri Regular Contributor
Please provide examples of what you have and what you are trying to go to.

Thanks,
Jeff
0 Kudos
jayshukla
Regular Contributor
Hi Jeffery,
Thanks for your reply. This is the script that I am using. I am having mixed success. Script fails sometimes, giving some Utility.py error.

I would appreciate any suggestions.

Thanks
Jay  

import arcpy, datetime, os, io, sys

try:

    #Read input parameters from GP dialog
 
    # Prod Setting...
    folderPath = arcpy.GetParameterAsText(0)
    output = arcpy.GetParameterAsText(1)
    sourceString = r"P:\Local" # arcpy.GetParameterAsText(1)
    replacmentString = r"\\hq\share" # arcpy.GetParameterAsText(2)
   
    #Create an output file
    outFile = open(output, "w")

    #Loop through each "LYR" file
    count = 0
    for path, dires, files in os.walk(folderPath):
        for filename in files:
            fullpath = os.path.join(path, filename)

            if os.path.isfile(fullpath):
                if filename.lower().endswith(".lyr"):
                    groupLayerName = ""
                    layerFile = arcpy.mapping.Layer(fullpath)

                    #Reference each layer in a layer file
                    count = 1
                    for lyr in arcpy.mapping.ListLayers(layerFile):
                        if lyr.isGroupLayer:
                            groupLayerName = lyr.name

                        if lyr.supports("DATASOURCE"):
                           
                            oldLoc = sourceString
                            newLoc = replacmentString
                           
                            if lyr.dataSource.find(oldLoc) != -1:
                               
                                ds = lyr.dataSource.replace(oldLoc, newLoc)

                                index = ds.find(lyr.datasetName)
                                newds = ds[0:index-1]

                                if lyr.dataSource.find(".shp") >= 0:
                                    lyr.replaceDataSource(newds, 'SHAPEFILE_WORKSPACE', lyr.datasetName)
                                    outFile.write(fullpath + "\t" + "SHP" + "\t" + lyr.longName + "\t" + groupLayerName + "\t" +lyr.name + "\t"  + lyr.datasetName + "\t" + str(newds) + "\t" + "SHP" "\n")
                                elif lyr.dataSource.find(".mdb") >= 0:
                                      lyr.replaceDataSource(newds, 'ACCESS_WORKSPACE', lyr.datasetName)
                                    outFile.write(fullpath + "\t" + "PGDB" + "\t" + lyr.longName + "\t" + groupLayerName + "\t" +lyr.name + "\t"  + lyr.datasetName + "\t" + str(newds) + "\t" + "PGDB" "\n")
                                elif lyr.dataSource.find(".gdb") >= 0:
                                    lyr.replaceDataSource(newds, 'FILEGDB_WORKSPACE', lyr.datasetName)
                                    outFile.write(fullpath + "\t" + "FGDB" + "\t" + lyr.longName + "\t" + groupLayerName + "\t" +lyr.name + "\t"  + lyr.datasetName + "\t" + str(newds) + "\t" + "FGDB" "\n")
                                               
                    layerFile.save()
                    del layerFile

    outFile.close()

    #Open the resulting text file
    os.startfile(output)

    #Delete variables that reference data on disk
    del outFile
   
except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))
0 Kudos