I have produced NDVI plots in GEE. I plot them with the simple Map.addLayer function, but I am for now only interested in all the pixels with values larger than 0. I would like to remove all the pixels less than 0 when I am mapping.

How do I do that?

// Define a point of interest. Use the UI Drawing Tools to import a point
// geometry and name it "point" or set the point coordinates with the
// ee.Geometry.Point() function as demonstrated here.
var point = ee.Geometry.Point([-122.292, 37.9018]);

// Import the Landsat 8 TOA image collection.
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');

// Get the least cloudy image in 2015.
var image = ee.Image(
    .filterDate('2015-01-01', '2015-12-31')

// Compute the Normalized Difference Vegetation Index (NDVI).
var nir = image.select('B5');
var red = image.select('B4');
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');

// Display the result.
Map.centerObject(image, 9);
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, ndviParams, 'NDVI image');

I thought I could do something like:

var value = 0
var mask = ndvi.lt(0)

// create new image
var new_image = mask.multiply(value).add(ndvi.multiply(mask.not()))

but that just sets it to be 0 and still plots it.

1 Answer 1


It ended up being simple:

var maskNDVI = ndvi.gt(0); //set the threshold you need. Here I keep all pixels greater than 0.
var maskedNDVI = ndvi.updateMask(maskNDVI); //Apply this in a mask

Map.addLayer(maskedNDVI,{min: 0, max: 1, palette: palette}, 'NDVI');

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.