I need to clip over a Landsat ImageCollection using another ImageCollection which contains yearly crop masks extracted from MODIS Landcover Type (2001 to 2016).

I need code that:

  • clips the Landsat image from 2001 with the Crop Mask in 2001
  • clips the Landsat image from 2002 with the Crop Mask in 2002

... and so on.

Here is the code for the yearly crop area and Landsat8 collection:

function cropmask (img){
  return img.updateMask(img.eq(12));//crop lands are pixels with value=12
var yearlyCrop = ee.ImageCollection('MODIS/006/MCD12Q1').select('LC_Type1').map(cropmask);
var l8images = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA').filterBounds(roi);

1 Answer 1


Interesting question. I think 'clipping' as you want it is better done inside the earth engine using masking. Clipping is done on a geometry, while you can also mask all the pixels which are crop in the MODIS images.

As you didn't define your Area of Interest (roi) in your question, I draw a polygon myself. Also, you wanted it done from 2001 till 2016, so I first merged the image collections of landsat 5, 7 and 8 together:

// Landsat images
var l8images = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA').filterBounds(roi);
var l7images = ee.ImageCollection('LANDSAT/LE07/C01/T1_TOA').filterBounds(roi);
var l5images = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA').filterBounds(roi);
var merged = l8images.merge(l7images).merge(l5images);

You then want to mask all images of a certain year based on the MODIS image where cropland is classified. I think it is usefull to filter each image collection on a year and then mask all the landsat images from that year on the MODIS image of that year. I would do that as follows:

// mask images for years between: 
var startyear = 2001;
var endyear   = 2016;

// make image composites
var loopsteps = ee.List.sequence(startyear,endyear);
var masking = loopsteps.map(function(x){
  var sdate = ee.Date.fromYMD(x,1,1);                // startdate of current loop
  var edate = ee.Date.fromYMD(x,12,31).advance(1, 'day'); // enddate of current loop (exclusive, so add 1 day)
  var filtLs = merged.filterDate(sdate, edate);
  var filtCrop = yearlyCrop.filterDate(sdate, edate).first().rename("LC_Type1Crop");
  // map over the ls images of 1 year and mask everything which is not Crop (=12)
  var maskedLandsat = filtLs.map(function(image){
    return image.updateMask(filtCrop).addBands(filtCrop).clip(roi);
  return maskedLandsat;

Note that I clipped every image based on the area of interest (roi), but that is not necesarry. Also you should know that if your studyarea is large or on the border of an Landsat scene, you will have to make image composites so your whole studyarea is covered by images.

Now you will end up with a list with on each position a imagecollection of that year. You possibly want to merge all the imageCollections of every year back together:

// rearrange the list of collections into a single image collection
var sizeList = masking.length();
var emptyCol = ee.ImageCollection(ee.Image());
function rearrange(current, previous){
  previous = ee.ImageCollection(previous);
  current = ee.ImageCollection(current);
  return current.merge(previous);
// all the images masked on the yearly crop MODIS image
var output = ee.ImageCollection(masking.iterate(rearrange, emptyCol))
                    .filter(ee.Filter.listContains('system:band_names', 'LC_Type1Crop'));
print('images valid for MODIS crop', output);

Link to the script

  • That is an excellent answer! I checked and it works exactly as I want!! Thank you @Kuik Feb 11, 2019 at 23:02
  • Just curious, when you merged the image collections, does it affect the bands as well? For example Band 3 in L5 is not the same as Band 3 in L8. Does that make sense? Feb 12, 2019 at 18:29
  • 1
    Using merge, all images will be put together without any change of the bands, image properties or projections. To remain consistency over the image collections, it is therefore recommended to first change the bands to their 'natural' names, such as 'red', 'swir1' etc before merging.
    – Kuik
    Feb 13, 2019 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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