0

I'm trying to find NDVI of cropland that I have masked from ESA and mosaiced it with Sentinel but I'm facing error of mosaic.map is not a function when I try to find NDVI map and NDVI chart for cropland. I don't know why it is giving me error when my first code is running fine.

Map.centerObject(aoi)
var s2 = ee.ImageCollection('COPERNICUS/S2');
var s2c = ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY');

var start = ee.Date('2017-06-15');
var end = ee.Date('2017-10-15');

function filterBoundsDate(imgCol, aoi, start, end) {
  return imgCol.filterBounds(aoi).filterDate(start, end)}
s2 = filterBoundsDate(s2, aoi, start, end);
s2c = filterBoundsDate(s2c, aoi, start, end);

function indexJoin(colA, colB, propName) {
  var joined = ee.ImageCollection(ee.Join.saveFirst(propName).apply({
    primary: colA,
    secondary: colB,
    condition: ee.Filter.equals(
        {leftField: 'system:index', rightField: 'system:index'})
  }));
  return joined.map(function(image) {
    return image.addBands(ee.Image(image.get(propName)));
  });
}

function buildMaskFunction(cloudProb) {
  return function(img) {
    var cloud = img.select('probability').gt(ee.Image(cloudProb));

    return img.updateMask(cloud.not());
  };
}

var withCloudProbability = indexJoin(s2, s2c, 'cloud_probability');

var maskClouds = buildMaskFunction(50);
var s2Masked = ee.ImageCollection(withCloudProbability.map(maskClouds))
                   .select(ee.List.sequence(0, 12));

var median = s2Masked.median();

var difFromMedian = s2Masked.map(function(img) {
  var dif = ee.Image(img).subtract(median).pow(ee.Image.constant(2));
  return dif.reduce(ee.Reducer.sum()).addBands(img).copyProperties(img, [
    'system:time_start'
  ]);
});


var bandNames = difFromMedian.first().bandNames();
var bandPositions = ee.List.sequence(1, bandNames.length().subtract(1));
var mosaic = difFromMedian.reduce(ee.Reducer.min(bandNames.length()))
                 .select(bandPositions, bandNames.slice(1))
                 .clipToCollection(aoi);
Map.addLayer(
    mosaic, {bands: ['B11', 'B8', 'B3'], min: 225, max: 4000}, 'S2 mosaic');
    




var hansenImage = ee.ImageCollection("ESA/WorldCover/v100").first();

var datamask = hansenImage.select('datamask');

var classification = hansenImage.select('Map').clip(aoi)
var mask = classification.eq(40)

var visParams = {bands: ['B4', 'B3', 'B2'], max: 0.3};

var maskedComposite = mosaic.updateMask(mask);
Map.addLayer(maskedComposite, visParams, 'masked');
var crop = mask.not();
crop = crop.mask(crop);
var mosaic = ee.ImageCollection([
  median.visualize(visParams),
  crop.visualize({palette: '000044'}),
]).mosaic();
Map.addLayer(mosaic.clip(aoi), {}, 'custom mosaic');

var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B5', 'B4']));
};

var l8 = mosaic.map(addNDVI)
var NDVI = l8.select(['nd']);
var NDVImed = NDVI.median(); 
var ndviVis = {
  min: 0.0,
  max: 1,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.addLayer(ndvi.clip(aoi),ndviVis, 'NDVI');

var plotNDVI = ui.Chart.image.seriesByRegion(s2, aoi,ee.Reducer.mean(),'nd',30,'system:time_start', 'system:index')

the script link is: https://code.earthengine.google.com/9f9dff57ccac431a25a4a45033cef323

1 Answer 1

0

You can not map over a single image. After you calculated the median of your collection, you receive only one image and .map only works with an image collection. When you try to calculate the median of a Region use the function ee.Image.reduceRegion.

1
  • Yes you are right. what if I want to apply on image collection? because I want to generate time series chart of NDVI for cropland masked from ESA. Commented Aug 11, 2022 at 12:03

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.