I develop a python script tool to adapt the symbology of a set of shapefiles based on a given symbology template (layer file).
These are the properties that should be changed:
Number of classes and colors: using the ApplySymbologyFromLayer()
function.
The symbology template has 4 classes set to natural breaks and the colors are set manually to specific RGB values (green, brown, red, blue), there is no color scheme used.
Class break values and labels: calculated based on statistical values of each shapefile (in the code below entered arbitrary values)
After using the ApplySymbologyFromLayer()
, the shapefile is set to 4 classes, natural breaks, and the specified colors.
But when I then set the class break values and labels, the colors are reset to the default color scheme. I cannot use the ApplySymbologyFromLayer()
afterwards, as then the classification scheme is reset.
EDIT: Obviously the problem are the manually set colors, as the colors remain consistent when a color scheme is used. But I need to apply the specific color values.
Why are the manually applied colors reset and is there a Workaround for this?
Here is my code:
import arcpy
inptShapefiles = arcpy.GetParameterAsText(0)
symbologyTemplate = arcpy.GetParameterAsText(1)
shapefiles = inptShapefiles.split(';')
for shp in shapefiles:
name = os.path.splitext(os.path.basename(shp))[0]
lyr = arcpy.MakeFeatureLayer_management(shp, name)
arcpy.ApplySymbologyFromLayer_management(lyr, symbologyTemplate)
lyr.getOutput(0).symbology.classBreakValues = [1, 100, 200, 300, 400]
lyr.getOutput(0).symbology.classBreakLabels = ["Class A", "Class B", "Class C", "Class D"]
arcpy.SaveToLayerFile_management(lyr, "E:/temp/" + lyr.getOutput(0).name, "RELATIVE")