How can I make a built-in palette in EE apply to all bands of an Image(), not just a specific one?

Context: I am using Google EarthEngine to display data from the Cropland Data Layer (CDL), which is a cropmap. Typical CDL images come with a band cropland, which has a built-in colour palette (showing the same colour scheme as the original CDL).

The problem: Now when I do a conversion from "Image Collection with many images, each including cropland" to "Image with multiple cropland bands, where the name of the band represents the year", I loose the colour attributes! Ironically, if I were to rename a specific band to cropland when visualising, the palette will come back.

This seems to suggest that the palette is associated to a specific band name (indeed, it shows up in properties as: cropland_class_names).

How can I make the palette apply to any band in the Image, with arbitrary band name?


var CDL_12 = ee.Image('USDA/NASS/CDL/2012')
var CDL_13 = ee.Image('USDA/NASS/CDL/2013')
var CDL_12_13 = CDL_12.select("cropland").rename("CDL_2012").

print(CDL_12_13, "CDL_12_13")

First one (untouched) will show the right colours. Second (band renamed), won;t, while third (renamed band name dback to cropland) will show right again:

Map.addLayer(CDL_12.select("cropland"), {}, "CDL 2012 raw")
Map.addLayer(CDL_12_13.select("CDL_2012"), {}, "CDL 2012 from Image")
Map.addLayer(CDL_12_13.select("CDL_2012").rename("cropland"), {}, "CDL 2012 from Image: works")

You can apply the palette manually using image.visualize(), however if the values that the colors match aren't contiguous, you'll need to remap them to be contiguous.

var CDL_12 = ee.Image('USDA/NASS/CDL/2012')

var palette = ee.List(CDL_12.get("cropland_class_palette"))
var values = ee.List(CDL_12.get("cropland_class_values"))
var sequence = ee.List.sequence(0, values.size().subtract(1))

var colorized = CDL_12.remap(values, sequence).visualize({palette: palette})
  • thanks! I still don't understand fully why it works directly on the original CDL, but not the transformed/renamed? Is there not a simple way to say that a palette corresponds to multiple bands, without the need to remap each band individually? – Matifou Dec 5 '18 at 17:06

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.