How do you filter an activeList of MapLayers in Google Earth Engine (js api)?

Context: in an app where users use a dateSlider to visually interpret imagery and assign classifications. Classifications assigned by placing points. Since it is possible for multiple layers to be present but hidden, I want the point to recognise only the top-most, non-hidden layer. Code and psuedocode below:

var roi = ee.Geometry.Point([-105.61, 41.14]);
var s2sr = ee.ImageCollection("COPERNICUS/S2_SR").filterBounds(roi)

// stacked images
var image1 = s2sr.filterDate('2021-01-01', '2021-01-07').first()
var image2 = s2sr.filterDate('2020-02-01', '2020-02-07').first()
var image3 = s2sr.filterDate('2020-03-01', '2020-03-07').first()

 * In this toy example I am explicitly setting image3 to be hidden
 * In actual use case this will be in an app and the user can toggle layers on and off
Map.addLayer(image1, {}, 'im1')
Map.addLayer(image2, {}, 'im2')
Map.addLayer(image3, {}, 'im3', false) 

var layers = Map.layers()

 * How to filter this ActiveList to return only the visible layers?
 * Psuedocode: 
 * var shownLayers = Map.layers().filter('shown', true)
// Then get the top image of the *visible* layers
var topImage = shownLayers.get(shownLayers.length() - 1) 
print(topImage) // should return image2

Is there a way to filter a ActiveList without resorting to for loops? I see in the docs I need to use the function getShown.


1 Answer 1


You can convert to a regular Javascript array and use Array.filter there. This will only work for ActiveList().

var topImage = Map.layers().getJsArray()
    .filter(function(f) { return f.getShown() })

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.