0

I have a raster layer which consists of unique values 0-4 and I want to color them through ArcPy.

I have tried this:

import arcpy
import os
arcpy.env.overwriteOutput = True
from arcpy.sa import *

#set the pathway to the maps
aprx = arcpy.mp.ArcGISProject(rel_path)

#get the map of interest
one = aprx.listMaps()[0]

#add new layer to map
one.addDataFromPath(new_layer_location)

#get all layers
one_layers = one.listLayers()

#get the first layer
l = one_layers[0]

#build attribute table so I can colorize it
arcpy.BuildRasterAttributeTable_management(l, "Overwrite")

#get symbology
sym = l.symbology

#make a dictionary for changing colors
legend_dict = {'0': {'RGB': [255, 85, 0, 0]}, '1': {'RGB': [0, 92, 230, 0]}, '2':{'RGB': [132, 0, 168, 0]}, '3':{'RGB': [56, 168, 0, 0]}, '4':{'RGB': [255, 255, 255, 0]}}

#set the colors to use based on value
if hasattr(sym, 'colorizer'):
    
    if sym.colorizer.type == 'RasterUniqueValueColorizer': 
       
        sym.colorizer.field = 'Value'
        
        for grp in sym.colorizer.groups:
            
             for itm in grp.items:
                                    
                #change the color
                itm.color = legend_dict[itm.label]
              
               
#update symbology
l.symbology = sym       


#save output
aprx.saveACopy(relpath + r'\\SavedOutput.aprx')

but when I look at this output all of the colors in the legend are transparent are have the information of:

{'HSV': [0, 0, 100, 0]}

I used this information to get this far:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/rasteruniquevaluecolorizer-class.htm

but I am not sure where it is different than what I am doing.

1 Answer 1

1

I believe the only thing you need to change is your legend dictionary to:

legend_dict = {'0': {'RGB': [255, 85, 0, 100]}, '1': {'RGB': [0, 92, 230, 100]}, '2':{'RGB': [132, 0, 168, 100]}, '3':{'RGB': [56, 168, 0, 100]}, '4':{'RGB': [255, 255, 255, 100]}}

Note I have changed all the alpha settings from 0 to 100

4
  • No, I don't think so, that would be fully transparent I believe. 0 should be no transparency. Verified with my script and the doc I listed. Apr 20, 2021 at 21:15
  • Thats not what happened for me. The raster was symbolized. Have you actually tried it? I did run your code within the notebook thats the only difference and the suggested change.
    – Hornbydd
    Apr 20, 2021 at 22:05
  • Yea, I did try it. Let me try again though with a fresh notebook. Thank you. Apr 20, 2021 at 22:38
  • Yea weird, I just needed to save a new copy, thanks again. Apr 22, 2021 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.