1

I am wanting to fill the values in one image that have a nodata value with values from the previous image in a collection, but can't quite figure out how to do it. For example, suppose you have NDVI for a time series in a collection, and image1 has cloud holes that you want to fill with value from image0. How does one do this? I am guessing it is with the iterate function.

Here is some example code:

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 the masked and scaled data, without the QA bands.
  return image.updateMask(mask).divide(10000)
      .select("B.*")
      .copyProperties(image, ["system:time_start"])
}

function ndvi_Sentinel2(image){
    // Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
    var ndvi = image.normalizedDifference(['B8', 'B4']).rename("NDVI");
    return image.addBands(ndvi);
}

// Define the area of interest
var ROI = ee.Geometry.Rectangle(-81.9309, 35.8033,-81.8403, 35.9191);

var start = ee.Date('2021-05-01');
var finish = ee.Date('2022-07-30');

// Load a Sentinel collection for the area of interest.
var collection = ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(ROI)
    .filterDate(start,finish)
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
    .map(maskS2clouds)
    .map(ndvi_Sentinel2);
    
print("new collection");
print(collection);
print("size: ",collection.size())

var coll_list = ee.ImageCollection(collection).toList(collection.size());

var image0 = ee.Image(ee.List(coll_list).get(8));
var image1 = ee.Image(ee.List(coll_list).get(9));

// plot images
Map.centerObject(ROI);

var ndviParams = {bands: ['NDVI'], min: -1, max: 1, palette: ['blue', 'white', 'green']};

Map.addLayer(image0,ndviParams,"NDVI 0");
Map.addLayer(image1,ndviParams,"NDVI 1");

1 Answer 1

1

I found the answer here.

I have modified the above code. The following code does what I was looking for.

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 the masked and scaled data, without the QA bands.
  return image.updateMask(mask).divide(10000)
      .select("B.*")
      .copyProperties(image, ["system:time_start"])
}

function ndvi_Sentinel2(image){
    // Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
    var ndvi = image.normalizedDifference(['B8', 'B4']).rename("NDVI");
    return image.addBands(ndvi);
}

// Define the area of interest
var ROI = ee.Geometry.Rectangle(-81.9309, 35.8033,-81.8403, 35.9191);

var start = ee.Date('2021-05-01');
var finish = ee.Date('2022-07-30');

// Load a Sentinel collection for the area of interest.
var collection = ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(ROI)
    .filterDate(start,finish)
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
    .map(maskS2clouds)
    .map(ndvi_Sentinel2);
    
print("new collection");
print(collection);
print("size: ",collection.size());

var coll_list = ee.ImageCollection(collection).toList(collection.size());

var image0 = ee.Image(ee.List(coll_list).get(8));
var image1 = ee.Image(ee.List(coll_list).get(9));

// plot images
Map.centerObject(ROI);

var ndviParams = {bands: ['NDVI'], min: -1, max: 1, palette: ['blue', 'white', 'green']};

Map.addLayer(image0,ndviParams,"NDVI 0");
Map.addLayer(image1,ndviParams,"NDVI 1");

var image1_unmasked = image1.unmask(-999);
Map.addLayer(image1_unmasked,ndviParams,"unmasked");

var newImage = image1_unmasked.where(image1_unmasked.select('NDVI').eq(-999),image0);

Map.addLayer(newImage,ndviParams,"composite");

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.