How does one negate a ".and()" operation on an image in GEE if this image does not contain 0/1 values but NULL/1 values?

From the documentation:

not() - Returns 0 if the input is non-zero, and 1 otherwise. Arguments: this:value (Image): The image to which the operation is applied. Returns: Image

For example if I want to retreive all loss in the Hansen tree loss dataset that does not overlap with a tree cover density >= 30. I use this code:

var gfw = ee.Image('UMD/hansen/global_forest_change_2018_v1_6');
var tc = gfw.select('treecover2000');
var loss = gfw.select('loss');
var lossyear = gfw.select('lossyear');
var frst = tc.gte(30);
var not_overlapping_loss = lossyear.and(frst.not())

which works fine. But when I try the same code replacing "frst" by an "asset" I uploaded that contain only "NULL/1" values, it seems the resulting image contains only NULL values as the (not existing) 0's are never converted to 1's...


Without knowing how your actual asset image looks like, I think you mean that all 'NULL' values are actually masked pixels? If your first operation on those asset image will be:

var newImage = ee.Image('ASSSET_ID').unmask(0);

All the masked pixels will have a value and thus will be used in the "and()" operation.


Thanks. I found a solution. One can use expression to transform the null values to 1 and 1 values to 0 so that the .and() operator will filter correctly the null values... Not sure how efficient it is or if there are better solutions but it works...

var inv_frst = frst.gte(1).expression('s ? 0 : 1', { 's': frst.select(0) });
var not_overlapping_loss = lossyear.gte(1).and(inv_frst );

Your Answer

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.