1

Im looking for a way to get the name and path of a layer file which is used in an MXD. I know once a layer file is added to a MXD, the path info is not stored, which is problematic.

Essentially the end goal is to use arcpy.mapping module to update a bunch of layers within any MXD. using the following code

arcpy.mapping.UpdateLayer(maindataframe, updateLayer, sourceLayer, True)

We have a folder structure where all our layer files are connected to a mix of SDE and GDB's. And to find a way to make a table of all the layers with the paths would be a godsend.

The question is, is there a way to programmaticly run though each folder and sub folders (maybe using os.walk?) and get the layer name used in ArcMap (in this case, the updatelayer), and the path (sourcelayer) to get this format -> [River System,r'C:\Catchments\Area\River_Catchments.lyr']

Edit: I should point out that this is not to update the datasource, but the path of where it is found. The reason I need this is that there are old MXD's that contain old/dated symbology and need to be updated. By updating the layer with the one in the folder structure I can update multiple MXD's at once.

  • 1
    There is no requirement that a layer have a layer file. The source information is retained in the MXD, and some of it may be available (password to enterprise geodatabase connections, for instance, is not). It sounds like you just need to use arcpy.mapping.ListLayers() – Vince Feb 7 '17 at 1:54
  • 1
    Full paths to datasources are only lost if you check 'Relative Paths' resources.arcgis.com/en/help/main/10.2/index.html#//… in ArcMap and save the MXD in a root folder of your data. If your layers are broken the value of the datasource may be inaccessible to python but it's still stored, check out Change Layer Paths resources.arcgis.com/en/help/main/10.2/index.html#//… tool... it might do what you want without needing a script. – Michael Stimson Feb 7 '17 at 3:05
  • I've just added to the original post to give some clarification. This is to update the layer information (new colours, symbol classes, etc) as opposed to the datasouces, my concern is that if I have a layer within an MXD which has the same name as layer within a folder structure, I effectively want to update the MXD layer with the one in the folder, if that makes sense. – Slevy Feb 7 '17 at 4:10
1

Assuming that you "have a folder structure where all [your] layer files are connected to a mix of SDE and GDB's", and that you are talking about *.lyr files and not talking about layers stored in map documents (*.mxd files), the code you need to write just needs to:

  1. Use arcpy.da.Walk to walk through the folder structure looking for *.lyr files.
  2. For each layer file found create a layer object using arcpy.mapping.Layer().
  3. View the layer objects name using the name property and its data source using the dataSource property.
  4. When you write a list of the above as tuples you could also store the name of the layer file as a third item in each tuple.
  • Testing this now, seems promising – Slevy Feb 7 '17 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.