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!
Alain, could you format your code
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
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?
strange... but nice work around
This worked for me... have fun!! new reply old thread..
p2 = arcpy.mp.ArcGISProject(aprfilenew)
maps = p2.listMaps()
for map in maps:
print ('--------------------------------------------------------------')
print (map.name)
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)
print(layer.longName)
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")
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)..
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
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.