2

I have 3 different independent layers which I want to merge into one layer:

enter image description here

For creating those layers, I have calculted NDVI, calculted the mean and then I have created one layer that is lower than the mean value, one layer that is higher and one ehich is the mean. I want now to take those 3 layers and merge them into one.

I have read all the section of the mosaicking but as I understand, I can't just merge those 3 layers.

Is there any way to merge those layers into one ? I thought to create visualization based ont their values but I saw I can't use this calssify.

This is how I created the 3 different layers:

var MyImage=ee.Image(withNDVI.first());

Map.addLayer(MyImage,colorizedVis,'ndvi');





//var std2 = ee.Number(STDDictionary.get("NDVI")).divide(2);
// var mean1 = ee.Number(meanDictionary.get("NDVI"));

///////the good function I want to work with:///////
for (var i in MyImage){

  var reducers = ee.Reducer.mean().combine({
  reducer2: ee.Reducer.stdDev(),
  sharedInputs: true
});

var meanDictionary = MyImage.reduceRegion({
  reducer: reducers,
  bestEffort: true,
});

}

print(meanDictionary);


//calculation
var std2 = ee.Number(meanDictionary.get('NDVI_stdDev')).divide(2);
var mean1 = ee.Number(meanDictionary.get("NDVI_mean"));

// the classes borders
var negBorder=mean1.subtract(std2);
var posBorder=mean1.add(std2);

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

var PositiveCOL = {
  palette: [
  '006622'
  ],
};

var NegativeCOL = {
  palette: [
  'ff0000'
  ],
};

var MeanCOL = {
  palette: [
  '00ff00'
  ],
};




Map.addLayer(gtPOS,PositiveCOL,'High');
Map.addLayer(ltNEG,NegativeCOL,'Low');
Map.addLayer(betMEAN,MeanCOL,'Mean');

I have try mosaic like this but got null image:

var mosaic = ee.ImageCollection.fromImages([gtPOS, ltNEG,betMEAN]).mosaic();
var PalMos={
  palette:[
    '006622','ff0000','00ff00']
};

Map.addLayer(mosaic,PalMos,'Mosaic');

but got this error:

Mosaic: Tile error: Expected a homogeneous image collection, but an image with incompatible bands was encountered: First image type: 1 bands ([PositiveBorder]). Current image type: 1 bands ([NegativeBorder]). Image ID: 1 Some bands might require explicit casts.

My end goal: to merge those 3 different classifications into 1 layers, right now they are 3 independent layers.

0
4
+50
  1. Make sure all images are of the same data type.
  2. Make sure all images have the same number of bands.
  3. Make sure all images have the same band names.

Here is a reproducible conceptual example:

Code Editor script

// Import a Landsat 8 image and calculate NDVI.
var img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20190712');
var ndvi = img.normalizedDifference(['B5', 'B4']);

// Classify NDVI into 3 classes by conditioning three separate layers.
// Use selfMask() to mask out all pixels that do not meet the condition,
// which is important when later mosaicking the images. Multiply images
// as needed to set the class value.
var low = ndvi.lt(-0.25)
  .selfMask(); // class value 1
var med = ndvi.gte(-0.25).and(ndvi.lte(0.25))
  .selfMask()
  .multiply(2); // set as class value 2
var hi = ndvi.gt(0.25)
  .selfMask()
  .multiply(3); // set as class value 3

// 1. Make sure all images are of the same type.
// 2. Make sure all images have the same number of bands.
// 3. Make sure all images have the same band names.
// This chunk of code just demonstrates the steps that may
// need to be taken. If your images meet the above criteria
// already, there is no need to do these steps.
low = low.toShort().select(0).rename('NDVI');
med = med.toShort().select(0).rename('NDVI');
hi = hi.toShort().select(0).rename('NDVI');

// Add the three classed images to an ImageCollection. Note that the
// order of the images determines the priority when mosaicing, the first
// image in the list is at the bottom of the stack and the last is on top.
var ndviClassCol = ee.ImageCollection.fromImages([low, med, hi]);

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

// Display the classified mosaic to the Map.
Map.centerObject(ndvi, 8);
Map.addLayer(ndviClassImg, {palette: ['blue', 'orange', 'green'], min: 1, max: 3});
1
  • Thank you, it has alomst worked, beside the last line that when I run it I get the error :range map: Layer error: Number.divide: Parameter 'left' is required.". maybe it's because I take my values from dictionary? @Justin Braaten – Reut Jan 12 '20 at 17:11

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.