Select to view content in your preferred language

SaveToLayerFile() generates invalid .lyrx from stand-alone Python script

156
1
Jump to solution
a week ago
GISDataTasPorts
Occasional Contributor

My stand-alone arcpy script below produces .lyrx files in the right location, with the right name.

If I drag any of the .lyrx files onto a map in an ArcGIS Pro project, it fails with the error:  "The selected layer file is not valid".  The contencts of the .lyrx file (viewed in a text editor) is:

{
  "type" : "CIMLayerDocument",
  "version" : "3.5.0",
  "build" : 57366
}

This is clearly not a valid .lyrx file!

This issue occurs if I run the script against a wide variety of ArcGIS Pro projects, using ArcGIS Pro version 3.5.2.

If I right-click on the same layer(s) in the same ArcGIS Pro project and save to a layer file that way, then the .lyrx can be used without any problems.

Similarly, this same code works fine if running from an ArcGIS Pro python window.  It only produces the invalid .lyrx files when running from a stand-alone Python script.

What is going wrong here?

Here is the very simple script:

for lyr in mp.listLayers():
    lyrx_path = os.path.join(layers_dir, lyr.name) + ".lyrx"
    arcpy.SaveToLayerFile_management(lyr, lyrx_path)

UPDATE:

ESRI have been able to reproduce this issue and they have suggested that I could try using arcpy.management.MakeFeatureLayer() to create a feature layer from the Layer object and then saving that to a layer file instead.  However, this suggestion is unsuitable for my situation, so I haven't even tried it.  I need this to work for raster layers, online tile layers, and most especially, I need this to work for group layers (which include multiple other layers).  These cannot be converted to feature layers!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
GISDataTasPorts
Occasional Contributor

I have found a work-around for this issue…

Instead of doing this:

arcpy.SaveToLayerFile_management(group_layer, lyrx_path)

I can do this:

group_layer.saveACopy(lyrx_path)

The first one fails (produces invalid .lyrx file) when run in a stand-alone Python script (although curiously it works when run in an ArcGIS Python window).  NB:  This failure does NOT produce any error messages.   It fails silently, and therefore I have asked ESRI to consider logging this as a bug.  (I believe that it should, at the very least, produce an error message, or otherwise succeed.  It should not fail silently!)

The second one succeeds in a stand-alone Python script and produces a valid .lyrx file.

UPDATE:

This has now been logged as a bug by ESRI:  BUG-000179880

View solution in original post

0 Kudos
1 Reply
GISDataTasPorts
Occasional Contributor

I have found a work-around for this issue…

Instead of doing this:

arcpy.SaveToLayerFile_management(group_layer, lyrx_path)

I can do this:

group_layer.saveACopy(lyrx_path)

The first one fails (produces invalid .lyrx file) when run in a stand-alone Python script (although curiously it works when run in an ArcGIS Python window).  NB:  This failure does NOT produce any error messages.   It fails silently, and therefore I have asked ESRI to consider logging this as a bug.  (I believe that it should, at the very least, produce an error message, or otherwise succeed.  It should not fail silently!)

The second one succeeds in a stand-alone Python script and produces a valid .lyrx file.

UPDATE:

This has now been logged as a bug by ESRI:  BUG-000179880

0 Kudos