1

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.

2 Answers 2

2

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

1
  • Thank you, I've tried but got stuck when trying to visualize, in command said "Cannot add an object of type <List> to the map" Dec 27, 2021 at 8:33
2

There're two mistakes in your expression, you're defining a variable that is an ImageCollection, not an image; for 'LST' and 'MNDWI'.

The first fix is an easy one: you're mapping your second function to the original IC and not the output from the first function where you calculate 'MNDWI'. So you simply change terra_SR.map(indices_terra_2) to terra_mndwi.map(indices_terra_2).

The second is a bit trickier as you need to add each image of terra_LST to terra_SR. You can use the 'system:index' property for this; these are unique for each day/image.

This code should work:

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)
                     .map(function(img){
                        return img.addBands(terra_LST.filter(ee.Filter.eq('system:index',img.get('system:index'))).first())
                     })
                     
//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': aa.select('LST_Day_1km'),
                    'MNDWI': aa.select('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_mndwi.map(indices_terra_2);
  var tst2 = terra_mndisi.first().projection();
  print('MNDISI Projection:', tst2.nominalScale());
2
  • Thank you, I've tried but got stuck when trying to visualize, in command said "Layer error: Image.visualize: Cannot provide a palette when visualizing more than one band" Dec 27, 2021 at 8:31
  • Before adding a layer to the Map, try selecting the band you want to visualise with .select('bandName')
    – hooge048
    Jan 3, 2022 at 13:16

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.