Hi
I have a library of many hundred .lyr files that are distributed to my user base along with a data package.
Users will save the data and layer files on various drive letters and directory locations. This works fine as the layer files are generally saved with relative file paths (creating using tool "Save To Layer File" and selecting the 'Store Relative Path' option.
I would now like to develop an audit process for the layer file library, and to do so I will walk through the directory structure and iterate through all of the .lyr files. For each file I will check if the data source is current (which I've worked out how to do) and then check that the .lyr file path is relative (which I haven't worked out how to do).
Is there an arcpy method that I can check this component of the .lyr file properties? I would imagine it sits somewhere with the layer methods, but I can't see any reference to this property in the documentation.
Thanks
There are methods inside the layer class
Thanks Dan.I have combed through the methods and haven't come up with anything that I think will help me, but it is entirely possible that I've missed or misinterpreted something.
My working solution for the moment is to compare the layer source (eg J: drive) with the layer location (eg C:) and if there is a difference, flag it for further investigation. I was hoping for a more definitive test if one exists though.
The paths can be changed... I doubt they are kept as relative since if you add a layer to a map document and separate it from its data, the link is broken... to fix, arcpy has the functionality
Changing a layer's data source is a common requirement. There are two methods on the Layer object that help with this. The findAndReplaceWorkspacePath method is intended for replacing part or all of a layer's workspace path. The replaceDataSource method allows you to change a layer's workspace and source dataset. For a more detailed discussion, parameter information, scenarios, and code samples, please refer to the Updating and fixing data sources with arcpy.mapping help topic.
and an addendum, suggesting that it is way the project was saved that affects whether absolute or relative paths are saved... you will have to experiment with a project with and with out relative paths linked
Once you've saved the layer file, you can't change the data source options from absolute to relative or vice versa. The layer will always maintain the data source option that was set for the map document at the time you saved the layer.
From Referencing data in the map—Help | ArcGIS Desktop
at the bottom
The only way I have found to identify if a layer file is relative - is to open it in Notepad (not Notepad++. Sublime or anything else, just Notepad) and search for ". . \" - spaces and all.
The only way I have found to identify if a layer file is relative - is to open it in Notepad (not Notepad++. Sublime or anything else, just Notepad) and search for ". . \" - spaces and all.
If this works consistently, you could easily read the layer file with Python and look for this string. It's a kluge, but why not if it works?
def lyr_relpath(lyrfile):
with open(lyrfile, 'rb') as f:
data = f.read()
gotRelative = (data.find(". . \\") > -1 or
data.find(". . /") > -1)
return gotRelative
Hi curtvprice ,
Does this code work for you? I just applied it to the two layerfiles I have and both returned false.
No it doesn't. I looked at my two test files in notepad (one save as layer file with mxd relative paths on, one off) and can't find the magic cookie that Grant mentioned. So I'm kind of confused.
Do you see a drive path when you open them in Notepad?