My app has a dateSlider that searches multiple collections, and selects the image nearest to a date within a window. I want to conditionally display those images, with error handling for empty images.
var image = l8sr
.filterDate('2016-01-01', '2016-07-01')
.first()
var nullImage = s2sr
.filterDate('2016-01-01', '2016-07-01') // returns empty collection
.first()
print(nullImage) // null
Map.addLayer(nullImage) // Error! Expression does not evaluate to an image.
// Check before trying to add the image to map
if(nullImage !== null){Map.addLayer(nullImage)} // Same error
// Dosen't work server-side either
ee.Algorithms.If(nullImage !== null, Map.addLayer(nullImage)) // Same error
I want my script to check each image before it tries to add a layer, to avoid the error message. In other words, I want my script to be robust to handle empty image collections, so it can flexibly add layers to the map. How can I accomplish this?
https://code.earthengine.google.com/0502525f29a25d7f1d4ab7b63acde203