Search code examples
maskgoogle-earth-engine

In Google Earth Engine, How do I select pixels from one image collection which correspond to a selected pixel value from another image collection?


I want to plot the count of burn pixels for modis burned area product within my geometry regions called "table" for only agricultural pixels (obtained from 'lc' image collection). I couldn't find anything in the docs to indicate you can do such a query between 2 image collections. Anyone have any suggestions?

I have tried using a mask, but it seems that this might only work on individual ee.Image not between different image collections. The code is shown below:

var modba = ee.ImageCollection('MODIS/006/MCD64A1').filterDate('2017-01- 
01', '2017-12-31').select('BurnDate')

var modbaN = ee.ImageCollection('MODIS/006/MCD64A1').filterDate('2017-01- 
01', '2017-12-31').select('Uncertainty')

var lc = ee.ImageCollection('MODIS/006/MCD12Q1').filterDate('2017-01-01', 
'2017-12-31').select('LC_Type1')

var AgOnly = lc.map(function(img) {
  var ag = img.select('LC_Type1');
  return ag.eq(12); 
//Would also like to maybe have 2 or 3 LC types to select here
});

var mask_ba = modba.map(function(img){
  return img.updateMask(AgOnly);
});

var bats =
    //ui.Chart.image.seriesByRegion(modba, table, ee.Reducer.count());
    ui.Chart.image.seriesByRegion(mask_ba, table, ee.Reducer.count());

print(bats);
var unts =
    ui.Chart.image.seriesByRegion(modbaN, table, ee.Reducer.mean());

print(unts);

Solution

  • It's still doable with a wider date range and several land cover types.

    In that case, just keep your old code that calculates AgOnly, and modify the code that calculates mask_ba as below:

    var mask_ba = modba.map(function(img){
      var img_year = img.date().format('YYYY');
      var start_date = ee.Date(img_year.cat('-01-01'));
      var end_date = start_day.advance(1, 'year');
    
      var Agri_this_year = AgOnly.filterDate(start_date, end_date).max();
      return img.updateMask(Agri_this_year);
    });
    

    Basically, the above code just extracts the year of the current img, then use filterDate method to select the land type cover of that year from AgOnly image collection, and finally apply updateMask.

    The same idea could be applied to other land cover types.

    Hope this helps.