3

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.

2
  • I've used it with ee.Filter.isin() but not with a mask. The only way I can think of would be to have a series of masks that then update in series the returned image. Commented Nov 14, 2021 at 12:16
  • Could ee.Filter work in this case if I give up using mask? I thought filter will filter out images/features that don't match the conditions in imageCollection/FeatureCollection. Could it also work for filtering out pixels in an image?
    – Sara
    Commented Nov 15, 2021 at 1:03

1 Answer 1

2

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).

1
  • 1
    This will work, and they're roughly the same cost. Commented Nov 15, 2021 at 11:47

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.