0

I tried to do calculations for 2 dataset of EVI on different year. I got an error message evi2020.subtract is not a function. The code is below

    var countries = Indonesia

var Nama_Provinsi = ['Kalimantan Selatan'] 
var geometry = countries.filter(ee.Filter.inList('NAME_1', Nama_Provinsi));
var area1 = ee.FeatureCollection(geometry);

var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
                  .filter(ee.Filter.date('2020-01-01', '2020-12-31'));
var evi2020 = dataset.select('EVI');
var eviVis = {
  min: 0.0,
  max: 8000.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.setCenter(116, -3, 8);


Map.addLayer(evi2020, eviVis, 'EVI20');

var dataset = ee.ImageCollection('MODIS/006/MOD13Q1')
                  .filter(ee.Filter.date('2010-01-01', '2010-12-31'));
var evi2010 = dataset.select('EVI');
var eviVis = {
  min: 0.0,
  max: 8000.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.setCenter(116, -3, 8);


Map.addLayer(evi2010, eviVis, 'EVI10');

var selisihevi = evi2020.subtract(evi2010);
Map.addLayer(selisihevi.clip(area1), {min: -0.5, max: 0.5, palette:'blue, green, yellow, red'}, "Selisih EVI");

1 Answer 1

1

You are trying to apply subtract() to an ee.ImageCollection, however subtract() only works for ee.Image.

The way you can go from an Image Collection to a single image is by reducing, using for example first() or mean():

var dataset = ee.ImageCollection('MODIS/006/MOD13Q1').select('EVI')

var evi2020 = dataset.filterDate('2020-01-01', '2020-12-31')
var evi2010 = dataset.filterDate('2010-01-01', '2010-12-31');

// Reduce the image collections to a single image

var evi2020_mean = evi_2020.mean()
var evi2010_mean = evi_2010.mean()

var difference = evi2020_mean.subtract(evi2010_mean);

On the other hand, if you want to subtract every Image from one day of year in 2020 from the same day in 2010 it is a bit more complicated. To do this you would have to map over the image collection (Note that this only works if the acquisitions are on exactly the same date every year).

var dataset = ee.ImageCollection('MODIS/006/MOD13Q1').select('EVI')

var evi2020 = dataset.filterDate('2020-01-01', '2020-12-31')
var evi2010 = dataset.filterDate('2010-01-01', '2010-12-31');

var doy_difference = evi2020.map(function(image){
  var date = image.date()
  var doy = ee.Number.parse(date.format('D'))
  // Get the corresponding image from 2010
  // you also need to reduce the image collection in this case to get an Image
  var doy_2010 = evi2010.filter(ee.Filter.dayOfYear(doy, doy)).first()
  var difference = image.subtract(doy_2010)
  return difference.set('system:time_start', date)
})

print(doy_difference)

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.