Hi All,
I am trying to automate the addition of a polygon layer to a Map in ArcGIS Pro (2.9, 3.1) and apply a consistent symbology to the layer based an attribute (TotalCanop), which is a floating point field. I don't want to apply symbology from a layer file as I want this code to be part of an ".atbx" with the code embedded an no other files required.
Below is the code (which is a sub part of larger script). When it is applied to the attached shapefile only one class ("<=5.0% Canopy cover") is displayed for the layer in the table of contents. The class that example polygon should have is "<=15.0% Canopy cover" . In fact my preference would be to have the layer symbology show all classes in the script no matter what the range of values in the "TotalCanop" field.
Any help in fixing this would be greatly appreciated. The code I am sharing is based on help supplied by @Anonymous User to solve this post:https://community.esri.com/t5/python-questions/arcpy-update-raster-layer-symbology-using/m-p/1306854#M68119
import arcpy
def total_canopy_symbology(layer_filter, map_filter):
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps(map_filter)[0]
l = m.listLayers(layer_filter)[0]
breaks = {0: {'upper': 5.0, 'color': [225, 225, 225, 100]},
1: {'upper': 10.0, 'color': [204, 204, 204, 100]},
2: {'upper': 15.0, 'color': [156, 156, 156, 100]},
3: {'upper': 20.0, 'color': [130, 130, 130, 100]},
4: {'upper': 30.0, 'color': [112, 168, 0, 100]},
5: {'upper': 100.0, 'color': [92, 137, 68, 100]}
}
arcpy.AddMessage("Adding CEAs symbolised by % canopy to the Map")
sym = l.symbology
sym.updateRenderer('GraduatedColorsRenderer')
sym.renderer.classificationField = "TotalCanop"
sym.renderer.breakCount = len(breaks)
strSta = f"Rendered applied to {l.name}\n"
for i, brk in enumerate(sym.renderer.classBreaks):
brk.upperBound = breaks[i]['upper']
brk.label = "<=" + str(breaks[i]['upper']) + "% Canopy cover"
brk.symbol.color = {'RGB': breaks[i]['color']}
strCur = f"Adding break at {breaks[i]['upper']} with colour {breaks[i]['color']}\n"
strSta += strCur
l.symbology = sym
print(strSta)
return True
total_canopy_symbology("*CEAs", "Map")