2

I have a raster layer with 3 unique values (1,2,3) and I want to apply a unique color to each using RGB values or hex codes, without the use of a symbology layer (layer file).

Is it possible to create a color map or symbology layer (layer file) entirely in ArcPy?

I want my code to add the raster layer to the map with the symbology I want (as opposed to random), but I don't want to use a pre-created symbology layer (layer file) or colormap - I would like the colors to be selected entirely in ArcPy, if possible.

arcpy.MakeRasterLayer_management(cat, "cat")
arcpy.SaveToLayerFile_management("cat", "cat.lyr", "ABSOLUTE") 
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
arcpy.mapping.AddLayer(df, "cat.lyr", "TOP")
# this is where I want to apply unique colors to each pixel value (pixel values are: 1,2,3)
# but I don't want to use add colormap or apply symbology from layer, unless the symbology
# layer or colormap can be created entirely in arcpy. I would like the color decisions
# to be made entirely in arcpy, without having to make a symbology layer or colormap 
# in the GUI
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df

something like this?

colormap = open("colormap.clr", "w")
colormap.write("1 70 137 102\n")
colormap.write("2 255 176 59\n")
colormap.close()
  • Are you wanting to change the pixel values? Or just make it display differently? – Midavalo Jul 14 '16 at 21:09
  • Pixel values can remain, I just want to change the colors – cartoscience Jul 14 '16 at 21:10
  • Change the colours of what? If it's not in a layer, how are you seeing it? – Midavalo Jul 14 '16 at 21:12
  • 1
    Look at the tool Apply Colormap resources.arcgis.com/en/help/main/10.2/index.html#//… and resources.arcgis.com/en/help/main/10.2/index.html#//… about creating the colormap file, that sounds like what you're after. Note there is also a Delete Colormap tool to reset the raster back once you're done. – Michael Stimson Jul 14 '16 at 21:29
  • 1
    Yes, read the links (carefully), the color file is just a text file that can be written 'on the fly' by python then applied. All you need to know is what RGB values to assign to each unique value. Note: raster must be an integer type (int8, int16 etc..) you cannot apply a colormap to a float without Int-ing it first. It would be best to apply the colormap before adding it to the map or the modifications may not be recognized. Can you give examples of RGB values you want to use? – Michael Stimson Jul 14 '16 at 22:53
3

You can do it like this:

# I'm assuming cat is the full path to a raster
# or a raster object

TempDir = os.environ.get("TEMP") # Your temp folder
ColFile = os.path.join(TempDir,"TEMP_CLR_FILE.clr")

with open(ColFile,'w') as ColWrite:
    ColWrite.write("1 255 0 0\n") # 1 = red
    ColWrite.write("2 0 0 255\n") # 2 = blue

arcpy.AddColormap_management(cat,input_CLR_file = ColFile)

arcpy.MakeRasterLayer_management(cat, "cat")
arcpy.SaveToLayerFile_management("cat", "cat.lyr", "ABSOLUTE") 
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
arcpy.mapping.AddLayer(df, "cat.lyr", "TOP")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df
os.remove(ColFile) # clean up your mess

Write the colorfile on the fly with python file writing.. it's only a text file. Then remove the temp file after it has been applied; the file is not linked to the raster.

AddColormap has some specific requirements that need to be addressed.. please read the docs carefully as there's a few traps that will prevent this method from working.

  • worked perfectly, ty! – cartoscience Jul 14 '16 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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