Select to view content in your preferred language

SaveToLayerFile_management function and symbology

09-19-2018 04:58 AM
New Contributor II

Hi all. I'm in trouble with the SaveToLayerFile_management function in a python script for arcgis pro 2.1.2. The script is setting a symbology that works but happens to not beeing saved on disk. I mean the lyrx file is done but doesn't contain the symbology. I've tried many changes in vain. The code below works properly on the python console however. For info, I changed here somes values initially gotten with GetParameterAsText.

Mxd ="Current")
MapPath = Mxd.filePath
ActMap = Mxd.activeMap
LyrName = 'Benzene'
SeuilsNb = 2
Seuils = "10;580" #string vers list
fileName = os.path.basename(MapPath)
fileNameLen = len(fileName)+1
Ws = MapPath[0:-fileNameLen]
in_layer = ActMap.listLayers(LyrName)[0]
LayerSource = in_layer.dataSource
TargetFolder = "J:\\00-Template-job_Oct2017\\900_CAD_GIS\\Lyr"
Labels = []

for Seuil in Seuils:
 if Seuil == Seuils[0]:
 Labels.append("Inférieur à " + Seuil)
 BorneInf = Seuil
 Labels.append(BorneInf + " à " + Seuil)
 BorneInf = Seuil

Symbologie = in_layer.symbology
Symbologie.renderer.classificationField = 'result_value_atof'
Symbologie.renderer.breakCount = SeuilsNb
i = 0
for bc in Symbologie.renderer.classBreaks:
 bc.upperBound = float(Seuils[i])
 bc.label = Labels[i]
 i =+1
in_layer.symbology = Symbologie

arcpy.SaveToLayerFile_management(LyrName,os.path.join(TargetFolder, LyrName + ".lyrx"))

Thanks in advance for the help!

0 Kudos
8 Replies
MVP Emeritus

Alain, could you format your code


0 Kudos
MVP Emeritus

Without dissecting your paths, are they in the same location? if not, you might want to explore the absolute/relative option to see if that is an issue or has an effect

Save To Layer File—Data Management toolbox | ArcGIS Desktop 

New Contributor II

Hello Dan. Thanks for helping. Unfortunatly, adding an "absolute" or "relative" parameter doesn't help. However I can say one thing more I can't explain eather, If I run the code twice, the symbology disappear on arcgis but the lyrx file is this time complete :-). Does that make sens for you? 

MVP Emeritus

strange... but nice work around

0 Kudos
New Contributor III

This worked for me...  have fun!!  new reply   old thread..

p2 =
maps = p2.listMaps()
for map in maps:
print ('--------------------------------------------------------------')
print (
print ('--------------------------------------------------------------')
layers = map.listLayers()
for layer in layers:
if layer.supports('NAME') and layer.supports('LONGNAME') \
and layer.supports('DATASOURCE'):
print (layer.longName + ' ---> ' + layer.dataSource)

newfeaturelayer = layer.longName
arcpy.MakeFeatureLayer_management(layer, newfeaturelayer)
newLayerout = newfeaturelayer + "-az" # ## rename this as needed....

arcpy.ApplySymbologyFromLayer_management(newfeaturelayer, layer)

arcpy.SaveToLayerFile_management(newfeaturelayer, path + newLayerout + '.lyrx', "ABSOLUTE")
New Contributor II

I kept trying to solve the issue in many ways. Result still consists on a file without my symbology. If running the program twice helps writing a complete lyrx file , may be the layer cache could be responsible. Is there a direct way to programaticly clear a layer cache? (since the updateConnectionProperties function is not exactly a success here)..

0 Kudos
MVP Emeritus

you would have to search help using 'cache' for what can have a cache, and how to set or get rid of them.

This is the help topic for layer properties, where they talk about caching ...

Set layer properties—ArcGIS Pro | ArcGIS Desktop 

you might try the approach, but whether there is arcpy access I haven't looked

Set layer properties—ArcGIS Pro | ArcGIS Desktop 

0 Kudos
Occasional Contributor 

this worked for my in pycharm.  i used the module and lyrx layer files.  I also referenced a path to my aprx project.

0 Kudos