Select to view content in your preferred language

Save layer file to data source path

89
4
Jump to solution
yesterday
FabioTuccini
New Contributor II

Hello,

I'm trying to write a code to save the layer file (.lyrx) for each layer present in my project in the same folder of the data source.

At the moment I can only one by one, with the following code:

import arcpy
arcpy.mp.ArcGISProject('CURRENT')
arcpy.management.SaveToLayerFile('layer','output_path', 'RELATIVE')

But I wold like to create a iteration with all  layers in my project. It's important that each .lyrx will be saved in the data source path.

Someone can help please

 

0 Kudos
1 Solution

Accepted Solutions
FabioTuccini
New Contributor II

Finally i found the solution. Maybe to someone can help, here the final code:

import arcpy
import os
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('Map') [0]
arcpy.env.overwriteOutput = True
for lyr in m.listLayers():
if lyr.isFeatureLayer:
arcpy.management.SaveToLayerFile(lyr,os.path.dirname(lyr.dataSource)+"\\"+lyr.name, 'RELATIVE')

View solution in original post

0 Kudos
4 Replies
simoxu
by MVP Regular Contributor
MVP Regular Contributor

You are almost there, just put your code in the layer iteration:

 

m = aprx.listMaps("map name")[0]
for lyr in m.listLayers():
    if !lyr.isGroupLayer:
        arcpy.management.SaveToLayerFile(lyr,'output_path', 'RELATIVE')

 Something like above, but I haven't tested code myself.

0 Kudos
FabioTuccini
New Contributor II

Thanks a lot, yes we are almost there but still missing somenthing.

I have modified the code as follow:

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('Map') [0]
for lyr in m.listLayers():
if lyr.isGroupLayer:
arcpy.management.SaveToLayerFile(lyr,'output_path', 'RELATIVE')

The  layer iteration is working but is replacing the outputfile with the same name.

I would like to get the output file name with the layer name and in the same folder of the source data. Is it possible?

0 Kudos
FabioTuccini
New Contributor II

OK, I'm very close!

I've modified the code as follow:

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('Map') [0]
for lyr in m.listLayers():
if lyr.isFeatureLayer:
arcpy.management.SaveToLayerFile(lyr,lyr.dataSource + lyr.name, 'RELATIVE')

But I get the following error message:

Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 11180, in SaveToLayerFile
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 11177, in SaveToLayerFile
retval = convertArcObjectToPythonObject(gp.SaveToLayerFile_management(*gp_fixargs((in_layer, out_layer, is_relative_path, version), True)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Layer: Dataset C:\Users\Fabio\Documents\ArcGIS\Projects\PRY002_WTL\03_Shape\Routes\Rerouting\VAB15_A.shpVAB15_A already exists.
Failed to execute (SaveToLayerFile).

0 Kudos
FabioTuccini
New Contributor II

Finally i found the solution. Maybe to someone can help, here the final code:

import arcpy
import os
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('Map') [0]
arcpy.env.overwriteOutput = True
for lyr in m.listLayers():
if lyr.isFeatureLayer:
arcpy.management.SaveToLayerFile(lyr,os.path.dirname(lyr.dataSource)+"\\"+lyr.name, 'RELATIVE')

0 Kudos