In the below code I want to put elevation average values in to the list but returns error:

enter image description here

code link: https://code.earthengine.google.com/6337849ec707ae2e229ba4bdb02e5fff

var startDate = '2000-01-01';
var endDate = '2020-01-01';

var maxBound = ee.Image("JRC/GSW1_2/GlobalSurfaceWater")

var dem = ee.ImageCollection("JAXA/ALOS/AW3D30/V3_2")
var NDWI = function(img){
  var bands = img.select('sur_refl_b04','sur_refl_b02').multiply(0.0001).clip(geometry);
  var index = bands.normalizedDifference(['sur_refl_b04','sur_refl_b02']).rename('NDWI')
  return index.addBands(dem)

var modisNDWI = ee.ImageCollection("MODIS/006/MOD09A1")


var calcArea = function(img,list){
  var thr = img.select('NDWI').gt(0.5).and(maxBound.eq(1));
  var mask = thr.updateMask(thr);
  var area = mask.multiply(ee.Image.pixelArea());
  var totalarea = area.reduceRegion(ee.Reducer.sum(), geometry);
  var boundary = mask.reduceToVectors({
    geometry: geometry,
    scale: 250,
    geometryType: 'polygon',
    eightConnected: false,
    bestEffort: true
  var demClip = img.select('DEM').clip(boundary);
  var eleAve = demClip.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: boundary,
    scale: 250
  return ee.List(list).add(eleAve.get('DEM'));};

var first = ee.List([]);
var AREA = ee.List(modisNDWI.iterate(calcArea, first));
print('elevation average:',AREA);

1 Answer 1


It's probably a better idea to map the function, rather than using iterate. The former runs parallel and is much faster. Also, as your 'geometry' is a geometry object, there's no need to use .reduceToVectors, which can be computationally expensive.

To calculate the elevation of each image in the collection:

var calcMeanElev = function(img){
  var eleAve = img.select("DEM").reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: geometry,
    scale: 250
  return img.set('mean_elev', eleAve.get('DEM'))};

var area = modisNDWI.map(calcMeanElev)

In the same function, you can add additional statistics to the image using .set().

In case you want to calculate the elevation of just unmasked pixels (in your script you're not, but maybe you want to?), you can use:

var calcMeanElev = function(img){
  var eleAve = img.updateMask(thr).select("DEM").reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: geometry,
    scale: 250
  return img.set('mean_elev', eleAve.get('DEM'))};

var area = modisNDWI.map(calcMeanElev)

To return a list of all calculated mean elevation values use .aggregate_array():

var listOfElevationMeans = area.aggregate_array('mean_elev')
  • Agree with @hooge048. Due to reduceToVectors(), the eleAve throws the "User Memory Limit Exceed" error.
    – Padmanabha
    Jan 19, 2022 at 12:12
  • It's true but map function returns image collection but I need mean elevation values in form of a list without raster layers. Accordingly in my opinion map function doesn't work. Jan 19, 2022 at 13:09
  • If you want a list of all values, simply use area.aggregate_array('mean_elev'): this returns a list of the mean elevation values from each image in the collection.
    – hooge048
    Jan 19, 2022 at 13:38

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.