I am quite new to both Google Earth Engine and JavaScript. My goal is to obtain the NDVI just for some region (some polygon) only in the selected cover type. I managed to compute areas and number of pixels for every cover type. I have the NDVI for all the region. Now I would like to select the NDVI just in some land cover type (e.g. LC_Type=10 'grassland').

Here is my code so far.

var modis_landcover = ee.ImageCollection("MODIS/006/MCD12Q1"),
modis_reflectance = ee.ImageCollection("MODIS/006/MOD09GA"),
roi = /* color: #d63000 */ee.Geometry.Polygon(
    [[[37.83249743231477, 11.586900001191903],
      [40.64499743231477, 11.500787248991408],
      [40.16159899481477, 13.943967698396829],
      [38.05222399481477, 13.85865131156253]]]);


// select dates for land cover
var lcSeries=modis_landcover.filterDate('2000-01-01', '2017-12-31');
print('MODIS lc series:', lcSeries);

//select dates for reflectance/ndvi
var reflSeries=modis_reflectance.filterDate('2001-01-01', '2001-12-31');
print('MODIS refl series:', reflSeries);

var lc=modis_landcover.filterBounds(roi)
print('lc filtered per year and type: ',lc);
var refl=modis_reflectance.filterBounds(roi)

// Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
var ndvi=refl.normalizedDifference(['sur_refl_b02','sur_refl_b01']);
// Make a palette: a list of hex strings.
var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
               '74A901', '66A000', '529400', '3E8601', '207401', '056201',
               '004C00', '023B01', '012E01', '011D01', '011301'];
var palette2=['05450a', '086a10', '54a708', '78d203', '009900', 'c6b044', 'dcd159',
    'dade48', 'fbff13', 'b6ff05', '27ff87', 'c24f44', 'a5a5a5', 'ff6d4c',
    '69fff8', 'f9ffa4', '1c0dff'];

var ndvi_roi=ndvi.clip(roi);
var lc_roi=lc.map(function(img) {return img.clip(roi)});
Map.addLayer(ndvi_roi, {min: 0, max: 1, palette: palette}, 'NDVI');
Map.addLayer(lc_roi,{min: 1, max: 17, palette: palette2},'land cover');

//area of the region in Km^2
print('Polygon area in km^2: ', roi.area().divide(1000 * 1000));

// Get the scale of the data from the first band's projection:
var modisScale = refl.select('sur_refl_b01')
print('MODIS scale in meters:', modisScale);

var lc_image=lc.first();
var c = lc_image.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: roi

var frequency = lc_image.reduceRegion({
    print('landcover frequency', frequency.get('LC_Type1'));

1 Answer 1


I suppose what you would like to do is mask the modis NDVI image everywhere the landcover type is not the value 10 (land cover type 'grassland').

You can do that as follows:

var NDVIforGrassland = ndvi_roi.updateMask(lc_image.eq(10));

You can than continue to work with the updated mask image to calculate statistics just for land cover type 'grassland', as all masked pixels are not used anymore in reduceRegion().

Link to script

  • Thank you very much, that's just what I wanted! It works perfectly.
    – Marta
    Jan 23, 2019 at 14:10

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.