SaveToLayerFile_management function and symbology

682
7
09-19-2018 04:58 AM
ALAINmartineau
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.

Ws=arcpy.env.workspace
Mxd = arcpy.mp.ArcGISProject("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
 else:
 Labels.append(BorneInf + " à " + Seuil)
 BorneInf = Seuil

Symbologie = in_layer.symbology
Symbologie.updateRenderer('GraduatedColorsRenderer')
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
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Alain, could you format your code

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

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 

ALAINmartineau
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? 

DanPatterson_Retired
MVP Esteemed Contributor

strange... but nice work around

0 Kudos
ALAINmartineau
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
DanPatterson_Retired
MVP Esteemed Contributor

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
ChrisWiebke
New Contributor III

https://www.exprodat.com/blogs/creating-layer-files-with-python-4/ 

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

0 Kudos