1

I want to add several spectral indexes in one go over a Sentinel2 TOA collection. If I do only with NDVI the script works. If I add 2 types of NDWI I get the following "Syntax Error: Unespected token (61:4)" Please see below the script I am using

// First define an area of interest
var lat = 65.64; 
var lng = 34.35;
var point = ee.Geometry.Point(lat, lng); 
//var aoi = point.buffer(100000); // Create an area (1km buffer around point)
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
.filter(ee.Filter.eq('country_co', 'AF'));
var aoi = country;

Map.setCenter(lat, lng, 5); // Center the map on this location, zoom level 10

var start = '2019-03-29'; // initial date of the image collection
var end = '2019-05-05'; //final date of the image collection

/**
 * Function to mask clouds using the Sentinel-2 QA band
 * @param {ee.Image} image Sentinel-2 image
 * @return {ee.Image} cloud masked Sentinel-2 image
 */
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}


// reduce NDVI over an image collection
// Function to calculate and add an NDVI band
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

// reduce NDWI (1-2) over an image collection
// Function to calculate and add an NDWI (1-2) bands

var addNDWI1 = function(image) {
  var ndwi1 = image.normalizedDifference(['B8A', 'B11']).rename('NDWI1');
  return image.addBands(ndwi1);
};

var addNDWI2 = function(image) {
  var ndwi2 = image.normalizedDifference(['B8A', 'B12']).rename('NDWI2');
  return image.addBands(ndwi2);
};
// Download the Sentinel-2 imagery collection
var imgs = ee.ImageCollection('COPERNICUS/S2')
    .filterDate(start, end)
    .filterBounds(aoi)
    .map(maskS2clouds)
    .map(addNDVI);
    .map(addNDWI1);
    .map(addNDWI2);
    ```

1 Answer 1

2

You only need one semicolon at the end when you are applying map several times. Just remove the others:

// Download the Sentinel-2 imagery collection
var imgs = ee.ImageCollection('COPERNICUS/S2')
    .filterDate(start, end)
    .filterBounds(aoi)
    .map(maskS2clouds)
    .map(addNDVI)
    .map(addNDWI1)
    .map(addNDWI2);

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.