Skip to main content
Bumped by Community user
title case
Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

fill Fill nodata values in an image with another image in Google Earth Engine

Source Link
user44796
  • 2k
  • 3
  • 30
  • 53

fill nodata values in an image with another image in Google Earth Engine

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");