1

I wonder how to create a dynamic mask from a list of values. At the moment, I have it hardcoded as follows:

mask = (
    clipped.neq(10)
    .And(clipped.neq(80))
    .And(clipped.neq(50))
    .And(clipped.neq(70))
    .And(clipped.neq(90))
    .And(clipped.neq(95))
    .And(clipped.neq(100))
    .selfMask()
)

However, I want the mask to be dynamically created based on a mask_values list. For example: mask_values = [10, 80, 50, 70, 90, 95, 100]

New contributor
Daniel Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

You can interatively add property to your mask:

mask = clipped
for v in mask_values:
    mask = mask.And(clipped.neq(v))
mask = mask.selfMask()
1
  • Thank you very much! This works great. Aug 7 at 1:20
0

This will produce a 1 if any pixels match any values in the list. If you want it to be a 0 if any occur, then just .not() the result.

var mask = clipped.eq([10, 80, 50, 70, 90, 95, 100]).reduce(ee.Reducer.max())

The input list of values are automatically turned into a multiband image, and the input image is compared to each of those bands, producing a multiband image of 0/1s. The reduce then gets the max over all bands.

3
  • Thank you for the response! I am not sure how to apply .not() in python. "mask = clipped.eq([10, 80, 50, 70, 90, 95, 100]).reduce(ee.Reducer.max()).not()" doesn't work in terms of syntax Aug 7 at 1:20
  • I think you can simply use "neq" instead Aug 7 at 11:03
  • In Python not is a reserved keyword, so it's .Not() (capitalized). You could use neq() but you want to know that all are false, so you need to use max().eq(0) 2 days ago

Your Answer

Daniel Nguyen is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.