Google Earth Engine – Resolving Image.subtract Error: Invalid Image Type

google-earth-enginegoogle-earth-engine-javascript-apiimage

I have some problem with my script, I'm trying to calculate MNDISI, and I've got some issues with errors:

Number (Error)
Image.subtract, argument 'image1': Invalid type.
Expected type: Image.
Actual type: ImageCollection.

var Jawa = ee.FeatureCollection("users/mohfaisal181099/KP/shp_jawa");
Map.centerObject(Jawa,7);
    
//Define Research Time
  var start = ee.Date('2020-01-01');
  var end = ee.Date('2020-12-31');

//Time Variable
var ndays = ee.Number(end.difference(start,'day')).round();
var nmonths = ee.Number(end.difference(start,'month')).round();
var nyears = ee.Number(end.difference(start,'year')).round();
print(ndays, 'ndays');
print(nmonths, 'nmonths');
print(nyears, 'nyears');

//Convert Kelvin to Celcius
  function celcius(img){
    return img.multiply(0.02).subtract(273.15)
              .copyProperties(img,['system:time_start']);
  }
//LST Terra MODIS 
  var terra_kelvin = ee.ImageCollection('MODIS/006/MOD11A1')
                       .filterBounds(Jawa)
                       .select('LST_Day_1km')
                       .filterDate(start,end);
  var terra_LST = terra_kelvin.map(celcius);
  print(terra_LST);

  var modisProjection = terra_LST.first().projection();
  print('MODIS Projection:', modisProjection.nominalScale());
  
//Downsetting Function
function reduceres(image){
  return image.reduceResolution({
    reducer : ee.Reducer.mean(),
    maxPixels : 1024
  })
  .reproject({
    crs : modisProjection
  });
}

//Define Dataset
  //Terra MODIS Surface Reflectance
    var terra_SR = ee.ImageCollection('MODIS/006/MOD09GA')
                     .filterBounds(Jawa)
                     .filterDate(start,end)
                     .map(reduceres);

//Function Indices Terra MODIS
  var indices_terra_1 = function(a){
  //MNDWI
    var mndwi = a.expression(
                  '(Green - SWIR)/(Green + SWIR)',
                   {'Green': a.select('sur_refl_b04'),
                    'SWIR': a.select('sur_refl_b02')
                   }).rename('MNDWI');
    return a.addBands(mndwi)
            .copyProperties(a,['system:time_start']);
    };
  var terra_mndwi = terra_SR.map(indices_terra_1);
  var tst = terra_mndwi.first().projection();
  print('MNDWI Projection:', tst.nominalScale());
  //MNDISI
  var indices_terra_2 = function(aa){
    var mndisi = aa.expression(
                  '(LST - ((MNDWI + NIR + SWIR)/3))/(LST + ((MNDWI + NIR + SWIR)/3))',
                   {'LST': terra_LST.select('LST_Day_1km'),
                    'MNDWI': terra_mndwi,
                    'NIR' : aa.select('sur_refl_b02'),
                    'SWIR': aa.select('sur_refl_b06')
                   }).rename('MNDISI');
    return aa.addBands(mndisi)
             .copyProperties(aa,['system:time_start']);
    };
  var terra_mndisi = terra_SR.map(indices_terra_2);
  var tst2 = terra_mndisi.first().projection();
  print('MNDISI Projection:', tst2.nominalScale());

I have checked my script in detail and found that I made a mistake in the MNDISI calculation part, but I haven't found any related reference to help solve my problem.

Best Answer

This error appears because you are mixing in indices_terra_2 function, images with ImageCollections in mndisi calculation. Following code fixes such error by using list of images, instead ImageCollection, for retrieving adequate index in LST images.

var Jawa = ee.FeatureCollection("users/mohfaisal181099/KP/shp_jawa");
Map.centerObject(Jawa,7);

Map.addLayer(Jawa);

//Define Research Time
  var start = ee.Date('2020-01-01');
  var end = ee.Date('2020-12-31');

//Time Variable
var ndays = ee.Number(end.difference(start,'day')).round();
var nmonths = ee.Number(end.difference(start,'month')).round();
var nyears = ee.Number(end.difference(start,'year')).round();
print('ndays', ndays);
print('nmonths', nmonths);
print('nyears', nyears);

//Convert Kelvin to Celcius
function celcius(img){
    return img.multiply(0.02).subtract(273.15)
              .copyProperties(img,['system:time_start']);
}
  
//LST Terra MODIS 
var terra_kelvin = ee.ImageCollection('MODIS/006/MOD11A1')
                       .filterBounds(Jawa)
                       .select('LST_Day_1km')
                       .filterDate(start,end);

var terra_LST = terra_kelvin.map(celcius);
print("terra_LST", terra_LST);

var terra_LST_lst = terra_LST.toList(terra_LST.size());

var modisProjection = terra_LST.first().projection();
print('MODIS Projection:', modisProjection.nominalScale());


//Downsetting Function
function reduceres(image){
  return image.reduceResolution({
    reducer : ee.Reducer.mean(),
    maxPixels : 1024
  })
  .reproject({
    crs : modisProjection
  });
}

//Define Dataset
//Terra MODIS Surface Reflectance
var terra_SR = ee.ImageCollection('MODIS/006/MOD09GA')
                     .filterBounds(Jawa)
                     .filterDate(start,end)
                     .map(reduceres);

print("terra_SR", terra_SR);

//Function Indices Terra MODIS
var indices_terra_1 = function(a){
  //MNDWI
  var mndwi = a.expression(
                  '(Green - SWIR)/(Green + SWIR)',
                   {'Green': a.select('sur_refl_b04'),
                    'SWIR': a.select('sur_refl_b02')
                   }).rename('MNDWI');
    return a.addBands(mndwi)
            .copyProperties(a,['system:time_start']);
    };
  
var terra_mndwi = terra_SR.map(indices_terra_1);
var tst = terra_mndwi.first().projection();

print("terra_mndwi", terra_mndwi);

print('MNDWI Projection:', tst.nominalScale());

//MNDISI
var terra_mndwi_lst = terra_mndwi.toList(terra_mndwi.size());

function indices_terra_2(aa){
  
  var idx = terra_mndwi_lst.indexOf(aa);
  
  var mndisi = ee.Image(aa).expression(
                  '(LST - ((MNDWI + NIR + SWIR)/3))/(LST + ((MNDWI + NIR + SWIR)/3))',
                   {'LST': ee.Image(ee.List(terra_LST_lst).get(idx)).select('LST_Day_1km'),
                    'MNDWI': ee.Image(aa).select('MNDWI'),
                    'NIR' : ee.Image(aa).select('sur_refl_b02'),
                    'SWIR': ee.Image(aa).select('sur_refl_b06')
                   }).rename('MNDISI');
    
    return ee.Image(aa).addBands(mndisi)
             .copyProperties(aa,['system:time_start']);

    }
    
var terra_mndisi = terra_mndwi_lst.map(indices_terra_2);

print("terra_mndisi", terra_mndisi);

var tst2 = ee.Image(terra_mndisi.get(0)).projection();
print('MNDISI Projection:', tst2.nominalScale());

After running above code in GEE code editor, it can be observed in following image that MNDWI and MNDISI bands were added to Image Collection list and nominalScale of first image was printed as expected.

enter image description here