I am intended to apply moving average function on a list of area values but the code returns empty list? Anyone has any idea?

enter image description here

the code link: https://code.earthengine.google.com/5cf9d5a21af90784ae3b3dce79f4a36b


var modis = imageCollection
  var band = img.normalizedDifference(['sur_refl_b04','sur_refl_b02']).rename('ndwi');
  var thr = band.gt(0.1);
  var mask = thr.updateMask(thr);
  var area_img = mask.multiply(ee.Image.pixelArea().divide(1e6));
  var area_val = ee.Number(mask.reduceRegion({
    reducer: ee.Reducer.sum(), geometry: geometry, scale: 1000
  var date = img.date().format('YYYY-MM-dd')
  return area_img
  .copyProperties(img, img.propertyNames())
var area = modis.aggregate_array('area')

print('original_area', area)

// Function to calculate moving average
var movingAverage = function(values, windowSize) {
  // Initialize an empty array to store the moving average values
  var movingAverageValues = [];
  // Iterate through each value in the input array
  for (var i = 0; i < values.length; i++) {
    // Calculate the starting index of the window
    var startIndex = Math.max(0, i - Math.floor(windowSize / 2));
    // Calculate the ending index of the window
    var endIndex = Math.min(values.length - 1, i + Math.ceil(windowSize / 2) - 1);
    // Initialize a variable to store the sum of values within the window
    var sum = 0;
    // Iterate through values within the window and calculate their sum
    for (var j = startIndex; j <= endIndex; j++) {
      sum += values[j];
    // Calculate the average of the values within the window and push it to the movingAverageValues array
    movingAverageValues.push(sum / (endIndex - startIndex + 1));
  // Return the array containing the moving average values
  return movingAverageValues;

var smoothed_area = movingAverage(area,3)

print('smoothed_area', smoothed_area)

1 Answer 1


This code violates the Best Practices Guidelines many different ways, but the biggest problem is that you're mixing client-side and server-side operations. For instance, using values.length in your for loop is meaningless since values is a server-side object. (It's also 'broken' code; values.length is actually a server-side function that you're never calling, but your intent was obvious, so we'll work with that).

The easiest (but worst and most dangerous) way to get your code to work is to download the area values (via getInfo). Then your for loop will at least execute on real numbers. But it's also a thing that wont scale well, and is likely to hang and/or crash your browser as you try to do it with more data.

var area = modis.aggregate_array('area').getInfo()

A more scalable option would be to use a self-join to gather a window's worth of images for each image in the collection, and then map over that to reduce each window.

  • Thanks dear Noel and your helps always working very well. I see the code is against the standard scripting in google earth engine, but trying to translate it through earth engine scripts. Commented May 11 at 19:29

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.