0
var S2Collection = ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(geometry)
    .filter(ee.Filter.calendarRange(10,02,'month'));

var cloudBitMask = ee.Number(2).pow(10).int();
var cirrusBitMask = ee.Number(2).pow(11).int();

function MaskS2Clouds(image) {
  var qa = image.select('QA60');
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
             qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask);
}

var S2CloudMasked = S2Collection.map(MaskS2Clouds);

function addNDVI(image) {
  return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
}

var unfNDVI = S2CloudMasked.map(addNDVI).select(['NDVI']);

function filterNDVI(image){
  var masked = image.updateMask(image.gt(0.2));
  var mean = ee.Number(masked.reduceRegion({
      geometry: geometry,
      reducer: ee.Reducer.mean(),
  }).get('NDVI'));
  return image.addBands(masked.divide(mean.getInfo()).rename('stdNDVI'));
}

var fNDVI = unfNDVI.map(filterNDVI).select(['stdNDVI']);

My code fails on the last function. I want to divide each masked image by its mean (pixel by pixel) but I get:

Failed to decode JSON. Error: Field 'value' of object '{"type":"ArgumentRef","value":null}' is missing or null. Object: {"type":"ArgumentRef","value":null}.

What is wrong?

  • Provide a link to your code or specify geometry. Do you have tested print(unfNDVI) to check if it contains an image? – till Kadabra Mar 9 at 20:22
1

I don't know what is your goal, but I got the error. You cannot use the .getInfo() function, which basically is a client side function that sends a request to the server to retrieve it's content, inside a map function. But also, after correcting that, you'll have another issue: as you mask out many pixels using a 0.2 threshold, reducing over a geometry can return a null value (all pixels inside that geometry are null), so when you try to create an Image using that reduction (mean in your case) it will fail again. To overcome that error I used ee.Algorithms.If and created a zero image in that case.

var S2Collection = ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(geometry)
    .filter(ee.Filter.calendarRange(10,02,'month'));

var cloudBitMask = ee.Number(2).pow(10).int();
var cirrusBitMask = ee.Number(2).pow(11).int();

function MaskS2Clouds(image) {
  var qa = image.select('QA60');
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
             qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask);
}

var S2CloudMasked = S2Collection.map(MaskS2Clouds);

function addNDVI(image) {
  return image.addBands(image.normalizedDifference(['B8', 'B4']).rename('NDVI'));
}

var unfNDVI = S2CloudMasked.map(addNDVI).select(['NDVI']);

function filterNDVI(image){
  var masked = image.updateMask(image.gt(0.2));
  var mean = ee.Number(masked.reduceRegion({
      geometry: geometry,
      reducer: ee.Reducer.mean(),
  }).get('NDVI'));
  var result = ee.Algorithms.If(mean, 
                                image.addBands(masked.divide(mean).rename('stdNDVI')),
                                image.addBands(ee.Image(0).rename('stdNDVI')))
  return ee.Image(result)
}

var fNDVI = unfNDVI.map(filterNDVI).select(['stdNDVI']);
print(fNDVI)

link: https://code.earthengine.google.com/2df2ff809ab970582d3ea8cad46b728c

  • Thanks!! That solved my problem – Andrés Mar 11 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.