I'm creating a python toolbox geoprocessing tool that will take a raster map layer as input (USA NLCD Land Cover), do some analysis on it, and generate a report - basically tabulating the acreage of the different land cover types (eg. open water, barren land, mixed forest...) for a given area. In the report I'd like to show the classification labels, but I can only see how to access the actual numeric pixel values (eg. 1,2,3, etc). I assumed I could get this throught the layer's symbology, but I'm not seeing it. How can I use arcpy to find the mapping from numeric classification to the text label, starting from a Layer object?
Solved! Go to Solution.
You can retrieve this by using the CIM definition properties of the raster layer. More specifically, using the colorizer property. I assumed you are using the Unique Values symbology type.
import arcpy
# Access the current ArcGIS Pro project and the specific map and raster layer
project = arcpy.mp.ArcGISProject("CURRENT")
map_obj = project.listMaps("YourMapName")[0] # Replace "YourMapName" with your map name
raster_layer = map_obj.listLayers("YourRasterLayerName")[0] # Replace with your raster layer's name
# Get the CIM definition of the raster layer
cim_def = raster_layer.getDefinition('V3')
# Check if the colorizer is of type CIMRasterUniqueValueColorizer
if isinstance(cim_def.colorizer, arcpy.cim.CIMRasterUniqueValueColorizer):
# Access the Unique Value Colorizer properties
unique_value_colorizer = cim_def.colorizer
# Iterate through each unique value class in the first group
for unique_value_class in unique_value_colorizer.groups[0].classes:
label = unique_value_class.label
values = unique_value_class.values # List of values for this unique class
color = unique_value_class.color # RGBA color as a CIMColor object
print(f"Label: {label}")
print(f"Values: {values}")
print(f"Color: RGBA({color.values[0]}, {color.values[1]}, {color.values[2]}, {color.values[3]})")
print("----")
else:
print("The raster layer does not have a CIMRasterUniqueValueColorizer applied.")
You can retrieve this by using the CIM definition properties of the raster layer. More specifically, using the colorizer property. I assumed you are using the Unique Values symbology type.
import arcpy
# Access the current ArcGIS Pro project and the specific map and raster layer
project = arcpy.mp.ArcGISProject("CURRENT")
map_obj = project.listMaps("YourMapName")[0] # Replace "YourMapName" with your map name
raster_layer = map_obj.listLayers("YourRasterLayerName")[0] # Replace with your raster layer's name
# Get the CIM definition of the raster layer
cim_def = raster_layer.getDefinition('V3')
# Check if the colorizer is of type CIMRasterUniqueValueColorizer
if isinstance(cim_def.colorizer, arcpy.cim.CIMRasterUniqueValueColorizer):
# Access the Unique Value Colorizer properties
unique_value_colorizer = cim_def.colorizer
# Iterate through each unique value class in the first group
for unique_value_class in unique_value_colorizer.groups[0].classes:
label = unique_value_class.label
values = unique_value_class.values # List of values for this unique class
color = unique_value_class.color # RGBA color as a CIMColor object
print(f"Label: {label}")
print(f"Values: {values}")
print(f"Color: RGBA({color.values[0]}, {color.values[1]}, {color.values[2]}, {color.values[3]})")
print("----")
else:
print("The raster layer does not have a CIMRasterUniqueValueColorizer applied.")
Thanks @Marshal!