0

I have code that creates 3 different layers using masking, each time other different pixels get mask and the others get 1 so the final result is 3 layers that are complemntary:

The three different layers

My problem is that when I mosaic those three layers, because each one has only values of 1, when I mosic them I get one layer with the same color and I lose their classes.

My question is if there is any way to determine the unmasked pixels values (for example, instead of 1, that it will be 2,or 3,) and then when I mosaic it i'll not lose the difference colores.

Here is my code, the part of the masking and the mosaicing:

//create the layers
var imageNDVI=MyImage.select('NDVI');
var gtPOS=MyImage.gt(posBorder).selfMask().rename('range');
var ltNEG=MyImage.lt(negBorder).selfMask().rename('range');
var betMEAN=MyImage.gt(negBorder).and(imageNDVI.lt(posBorder)).selfMask().rename('range');

var ndviClassCol = ee.ImageCollection.fromImages([ltNEG,betMEAN, gtPOS]);

// Mosaic the ImageCollection.
var ndviClassImg = ndviClassCol.mosaic();

// Display the classified mosaic to the Map.
Map.centerObject(geometry, 16);

Map.addLayer(gtPOS,{palette:['#9370DB']});
Map.addLayer(ltNEG,{palette:['fff200']});
Map.addLayer(betMEAN,{palette:['0fff50']});

Map.addLayer(ndviClassImg, {palette: ['ff0000', '00ff00', '006622'], min: 1, max: 3},'mosaic');

My end goal: to determine the value of the unmasked values

EDIT: I have tried to multiply the values as Daniel suggested, and it worked but the mosaic failed: enter image description here

This is how I claculted:

//create the layers
var imageNDVI=MyImage.select('NDVI');
var gtPOS=MyImage.gt(posBorder).selfMask().rename('range');
var ltNEG=MyImage.lt(negBorder).selfMask().rename('range');
var betMEAN=MyImage.gt(negBorder).and(imageNDVI.lt(posBorder)).selfMask().rename('range');

var pos=gtPOS.multiply(3);
var neg=ltNEG.multiply(2);


var ndviClassCol = ee.ImageCollection.fromImages([neg,betMEAN, pos]);

// Mosaic the ImageCollection.
var ndviClassImg = ndviClassCol.mosaic();

// Display the classified mosaic to the Map.
Map.centerObject(geometry, 16);

Map.addLayer(pos,{palette:['9370DB']});
Map.addLayer(neg,{palette:['0fff50']});
Map.addLayer(betMEAN,{palette:['fff200']});

Map.addLayer(ndviClassImg, {palette: ['ff0000', '00ff00', '006622'], min: 1, max: 3},'mosaic');

also tried:

//create the layers
var imageNDVI=MyImage.select('NDVI');
var gtPOS=MyImage.gt(posBorder).selfMask().rename('range');
var ltNEG=MyImage.lt(negBorder).selfMask().rename('range');
var betMEAN=MyImage.gt(negBorder).and(imageNDVI.lt(posBorder)).selfMask().rename('range');

var pos=gtPOS.select('range').multiply(3);
var neg=ltNEG.select('range').multiply(2);


var ndviClassCol = ee.ImageCollection.fromImages([neg,betMEAN, pos]);

// Mosaic the ImageCollection.
var ndviClassImg = ndviClassCol.mosaic();

// Display the classified mosaic to the Map.
Map.centerObject(geometry, 16);

Map.addLayer(pos,{palette:['9370DB']});
Map.addLayer(neg,{palette:['0fff50']});
Map.addLayer(betMEAN,{palette:['fff200']});

Map.addLayer(ndviClassImg, {palette: ['ff0000', '00ff00', '006622'], min: 1, max: 3},'mosaic');

as you can see, the layers genereted but the mosaic do not.

https://code.earthengine.google.com/a95798e6365e2142a23d48a7fcef89c7

1

This is a little trick I've been doing:

var type1 = ndvi.lt(0)
var type2 = ndvi.gte(0).and(ndvi.lt(0.6))
var type3 = ndvi.gte(0.6)

var classes = ee.Image([type1, type2, type3])
  .selfMask()  // Mask 0's
  .multiply(ee.Image([1, 2, 3])) // Assign values to the classes
  .reduce(ee.Reducer.firstNonNull()) // Pick first class
  .unmask(0) // Pixels without class get 0
  .updateMask(ndvi.mask()) // Mask out pixels outside of original image

https://code.earthengine.google.com/e507d33bec5463aaa7cce725917a4b37

4

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.