[GIS] Removing images based on band threshold using Google Earth Engine

google-earth-engine

I am using GEE and wish to set a threshold and exclude images based on an the image's band value and remove it from a collection. I do not want to mask the image, I want it physically out of the collection. As far as I can tell using ee.filter only works with metadata, not the images' bands.

Anyone have a clue?

Best Answer

This can be done in two steps. First, you need to turn information stored in images into metadata (image property). Then it can be used in the same way as all other filters. For example, this is how you can filter all images with some number of potential water pixels within some distance from a given point: https://code.earthengine.google.com/491ecb33a4b5fbe5f4cb9a3669254d05

// get all images for a given geometry
images = images.filterBounds(geometry)
print('Number of images: ', images.size())

// estimate potential water probability using MNDWI
var water = images.map(function(i) {
  var water = i.divide(10000).normalizedDifference(['B3', 'B8']).unitScale(-0.05, 0.15)

  return water.mask(water)
})

// estimate number of pixels above some thredhold within 500 m buffer around given point

// define area of interest and scale
var aoi = geometry.buffer(500)
Map.addLayer(aoi)

var scale = Map.getScale()

// applies thresholds and computes new property to be used for filtering
function threshold(i) {
  var count = i.gt(0.5).reduceRegion(ee.Reducer.sum(), aoi, scale).values().get(0)

  return i.set({water_pixel_count: count})
}

water = water.map(threshold)

print('Number of water pixels for all images:', water.aggregate_array('water_pixel_count'))

// filter
water = water.filter(ee.Filter.gt('water_pixel_count', 100))

print('Number if images after filtering: ', water.size())

require('users/gena/packages:animation').animate(water)

The above code computes a new property for every image and then uses that property to filter image collection, resulting in only images where water is present.

enter image description here