How can I convert a variable to another data type using Google Earth Engine? For example I've created a NDVI image in double number (interval value -1.0 to +1.0). I want to save the image in byte (Unsigned8Bit or Byte) after running a formula like this: NDVI8bit = 128 + 125*NDVI (interval value 3 to 253) in order to save disk storage capacity without losing information.


Probably you would like something like this:

// Calculate NDVI for a Landsat 5 image
var NDVI = image.normalizedDifference(['B4', 'B3']);
var scaledNDVI = NDVI.multiply(125).add(128);

var toBytes = scaledNDVI.toByte();
Map.addLayer(toBytes, {min: 0, max: 255})

Link to script with a random location and LS 5 image

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.