Google Earth Engine – ‘is in list’ Check for Image in GEE

google-earth-enginegoogle-earth-engine-javascript-api

I am using the MCD12Q1.006 MODIS Land Cover Type Yearly Global 500m dataset. I want to mask out non-forest types. I am using LC_Type1 and keeping only the pixels with values 1-6. I am currently doing it this way:

var maskForest = function(image){
  var forestMask = image.gte(1).and(image.lte(6))
  return image.updateMask(forestMask)
}
var forest = landCover.select('LC_Type1').map(maskForest)

But since I may also want pixels with value 10 (for example) I wonder if there is a method like series.isin([1,2,3,4,5,6]) in pandas, python, instead of using a lot of and, or.

Best Answer

I do not think that something like .isin() exists. ee.Filter.isin() won't do what you would like to do since as you said it only works on Image Collections.

However a not too complicated workaround is posible with .eq() and .reduce().

// Random image with range from 0-10
var test = ee.Image.random().multiply(10).floor()
// Making boolean mask with 1-6 as True
var isin = test.eq([1,2,3,4,5,6]).reduce('sum')

Map.addLayer(test)
Map.addLayer(isin)

As a short disclaimer: I do not know how computationally efficient this is. My gut instinct is telling me that the greater than/less than approach would be faster, but I haven't tested it. So I do agree that it would be handy to have a dedicated .isin() solution (It shouldn't be too hard to implement for the Earth Engine devs).

Related Question