0

I'm very new to GEE. I'm trying to calculate TSAVI using Sentinel-2A images for one year. But I have an error to run this code. The error is "Image.bitwiseXor: Bitwise operands must be integer only.".

Map.centerObject(AOI);
Map.addLayer(AOI,{},'Abshirin');

//Selecting Sentinel-2 Images
var S2_RAW = ee.ImageCollection("COPERNICUS/S2")
.filterBounds(AOI)
.filterDate('2019-09-20','2020-09-20')
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10)
.filterMetadata('SPACECRAFT_NAME', 'equals', 'Sentinel-2A')
print('S2_RAW', S2_RAW)


//Masking Clouds
function maskS2clouds(image) {
  var qa = image.select('QA60');
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
             qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000).clip(AOI)
      .copyProperties(image, 
      ['system:time_start', 'system:time_end']);
 }

 
// Mosaic Sentinel-2 data which are on the same dates
function mosaicByDate_S2(imcol){
  var imlist = imcol.toList(imcol.size());
  var unique_dates = imlist.map(function(im){
    return ee.Image(im).date().format("YYYY-MM-dd")
  }).distinct();
  var mosaic_imlist = unique_dates.map(function(d){
    d = ee.Date(d);
    var im = imcol
      .filterDate(d, d.advance(1, "day"))
      .mosaic();
    return im.set(
        "system:time_start", d.millis());
  });
  return ee.ImageCollection(mosaic_imlist)
}

var S2_cloudless = S2_RAW.map(maskS2clouds)
var S2 = mosaicByDate_S2(S2_cloudless);
print('S2', S2)


//Calculate TSAVI Index
var TSAVI=S2.map(function(img){
var bands=img.multiply(0.0001).clip(AOI);
var index = bands.expression(
'(1.22*(NIR-1.22*RED-0.03)/(1.22*NIR+RED-1.22*0.03+0.08*(1+1.22^2)))',{
'RED' : bands.select('B4'),
'NIR' : bands.select('B8')});
return index.rename('TSAVI')
.copyProperties(img,['system:time_start','system:time_end']);
});

print(TSAVI);

How to solve this problem? Thank You!

The code is : https://code.earthengine.google.com/9eb375c6c6c99cffb0dbc769928d363c

Gandom
  • 1
  • 2

2 Answers2

0

The error is in this piece:

var bands=img.multiply(0.0001).clip(AOI);
var index = bands.expression(
'(1.22*(NIR-1.22*RED-0.03)/(1.22*NIR+RED-1.22*0.03+0.08*(1+1.22^2)))',{
'RED' : bands.select('B4'),
'NIR' : bands.select('B8')});
return index.rename('TSAVI')
.copyProperties(img,['system:time_start','system:time_end']);
});

^ is the bitwise xor operator, not the exponential operator. The exponential operator is **. The code should actually read

var TSAVI=S2.map(function(img){
var bands=img.multiply(0.0001).clip(AOI);
var index = bands.expression(
'(1.22*(NIR-1.22*RED-0.03)/(1.22*NIR+RED-1.22*0.03+0.08*(1+1.22**2)))',{
'RED' : bands.select('B4'),
'NIR' : bands.select('B8')});
return index.rename('TSAVI')
.copyProperties(img,['system:time_start','system:time_end']);
});
Jesse Anderson
  • 4,507
  • 26
  • 36
-3

As highlighted by @blindjesse, for exponential operator use (**) instead of (^). More information is available at (https://developers.google.com/earth-engine/guides/image_math)

I have removed the issue from your code. The new code works fine now: https://code.earthengine.google.com/72c760a2250d63a09dcaab0342476224