TransWikia.com

Min and Max for each image of NDVI to calculate Em and LST

Geographic Information Systems Asked on December 20, 2021

I am working on my script to calculate LST for all the images in the collection separately and then use the calculated LST to estimate Mean Annual LST.

I have calculated NDVI for each image, and the next step is calculating emmisivity for each image. I have tried many codes to derive min and max of NDVI for each image and then use it for Fractional vegetation and emissivity calculation.

Unfortunately, the code is not working after calculating NDVI.
I think, I am making mistake in writing the script for estimation of min and max for each image.

Code Editor script

// define the geometry 
var scotty = ee.Geometry.Polygon(
  [[[-121.29890539422013, 61.306061839957344],
    [-121.30568601860978, 61.30371278494993],
    [-121.31246664299942, 61.30029191652167],
    [-121.31538488640763, 61.29781876724274],
    [-121.30208112969376, 61.29579888402434],
    [-121.29178144707657, 61.29847829278191],
    [-121.2836275316713, 61.304042487495025],
    [-121.29006483330704, 61.306886028086545]]]);

//cloud mask landsat7,landsat5, and landsat8 based on the pixel_qa band of Landsat SR data.

// function for cloud masking on three types of lan dsat
var cloudmasklandsat7and5and8= function(image){
    var Qlandsat5and7= image.select('pixel_qa');
    var cloudShadowBitMask = (1 << 3);
    var cloudsBitMask = (1 << 5);
    var mask5and7=Qlandsat5and7.clip(Scotty).bitwiseAnd(cloudShadowBitMask).eq(0)
                                   .and(Qlandsat5and7.bitwiseAnd(cloudsBitMask).eq(0));
    return image.updateMask(mask5and7);
};


//collecting Images (1984,2013) by masking the cloud (landsat7and5)

var landsat5= ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
                  .filterDate('1984-01-01', '2012-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);
var landsat7= ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
                  .filterDate('1984-01-01', '2012-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);

//merge landsat5 and landsat7 
var landsat7and5=landsat7.merge(landsat5);

//collecting Images (2013,2019) by masking the cloud (landsat8)

var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
                  .filterDate('2013-01-01', '2019-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);

var visParams8= {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};

var visParams7and5= {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};
// Visualization of all the images collected from three types of landsat
Map.addLayer(landsat7, visParams7and5,'Landsat7');
Map.addLayer(landsat8, visParams8,'Landsat8');
Map.addLayer(landsat5, visParams7and5,'Landsat5');
Map.addLayer(landsat7and5, visParams7and5,'Landsat7and5');



// NDVI   calculation for each image seperately

// calculate NDCI for each collected image from landsat8
// Function landsat8 NDVI
var NDVI8=function(image){
  return image.addBands(image.select('B5').subtract(image.select('B4')).divide(image.select('B5').add(image.select('B4'))).rename('NDVI'));

};

// Function landsat7and5 NDVI

var NDVI7and5=function(image){
  return image.addBands(image.select('B4').subtract(image.select('B3')).divide(image.select('B4').add(image.select('B3'))).rename('NDVI'));

};


// Mapping the NDVI functions on all the collected images seperately 
var landsat8Ndvi = landsat8.map(NDVI8);
var landsat5Ndvi = landsat5.map(NDVI7and5);
var landsat7Ndvi = landsat7.map(NDVI7and5);
var landsat7and5Ndvi = landsat7and5.map(NDVI7and5);

//NDVI visualization
var ndviParams = {
  min: -1,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};
Map.addLayer(landsat8Ndvi.select('NDVI'), 
            ndviParams, 'landsat8Ndvi');
Map.addLayer(landsat5Ndvi.select('NDVI'), 
            ndviParams, 'landsat5Ndvi');
Map.addLayer(landsat7Ndvi.select('NDVI'), 
            ndviParams, 'landsat7Ndvi');
Map.addLayer(landsat7and5Ndvi.select('NDVI'), 
            ndviParams, 'landsat7and5Ndvi');

// calculating Emissivity 
    //1: estimate min and max of each image in  NDVI layer
var min = image.aggregate_min('min');
var max = image.aggregate_max('max');

  //2; fractional vegetation for each image 
var fv7and5=function(landsat5Ndvi){
  return image.addBands(landsat5Ndvi.subtract(min).divide(max.subtract(min)).rename('FV'));

};

2 Answers

The problem is solved by adding (to float ) and filtering the null data.

Answered by Shae on December 20, 2021

If I understand your process okay, the below script will work for the "calculating Emissivity " section. It is calculating and adding the "FV" band to each image of each Landsat collection based on image specific min and max values.

Code Editor script

// I want to calculate the mean annual land surface temp(LST)  for my case study for (1984-2019). 

// define the geometry 
var Scotty = ee.Geometry.Polygon(
  [[[-121.29890539422013, 61.306061839957344],
    [-121.30568601860978, 61.30371278494993],
    [-121.31246664299942, 61.30029191652167],
    [-121.31538488640763, 61.29781876724274],
    [-121.30208112969376, 61.29579888402434],
    [-121.29178144707657, 61.29847829278191],
    [-121.2836275316713, 61.304042487495025],
    [-121.29006483330704, 61.306886028086545]]]);

Map.centerObject(Scotty, 13);
//cloud mask landsat7,landsat5, and landsat8 based on the pixel_qa band of Landsat SR data.

// function for cloud masking on three types of lan dsat
var cloudmasklandsat7and5and8= function(image){
    var Qlandsat5and7= image.select('pixel_qa');
    var cloudShadowBitMask = (1 << 3);
    var cloudsBitMask = (1 << 5);
    var mask5and7=Qlandsat5and7.clip(Scotty).bitwiseAnd(cloudShadowBitMask).eq(0)
                                   .and(Qlandsat5and7.bitwiseAnd(cloudsBitMask).eq(0));
    return image.updateMask(mask5and7);
};

//collecting Images (1984,2013) by masking the cloud (landsat7and5)
var landsat5= ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
                  .filterDate('1984-01-01', '2012-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);
var landsat7= ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
                  .filterDate('1984-01-01', '2012-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);

//merge landsat5 and landsat7 
var landsat7and5=landsat7.merge(landsat5);

//collecting Images (2013,2019) by masking the cloud (landsat8)
var landsat8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
                  .filterDate('2013-01-01', '2019-12-31')
                  .filterBounds(Scotty)
                  .filter(ee.Filter.lt('CLOUD_COVER', 25))
                  .map(cloudmasklandsat7and5and8);

var visParams8= {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};

var visParams7and5= {
  bands: ['B3', 'B2', 'B1'],
  min: 0,
  max: 3000,
  gamma: 1.4,
};

// Visualization of all the images collected from three types of landsat
Map.addLayer(landsat7, visParams7and5,'Landsat7');
Map.addLayer(landsat8, visParams8,'Landsat8');
Map.addLayer(landsat5, visParams7and5,'Landsat5');
Map.addLayer(landsat7and5, visParams7and5,'Landsat7and5');

// NDVI   calculation for each image seperately
function setNdviMinMax(img) {
  var minMax = img
    .select('NDVI')
    .reduceRegion({
      reducer: ee.Reducer.minMax(),
      scale: 30,
      maxPixels: 1e13
    });
  return img.set({
    'NDVI_min': minMax.get('NDVI_min'),
    'NDVI_max': minMax.get('NDVI_max'),
  });
}

// calculate NDCI for each collected image from landsat8
// Function landsat8 NDVI
var NDVI8=function(image){
  var ndvi = image.addBands(image.normalizedDifference(['B5', 'B4']).rename('NDVI'));
  return setNdviMinMax(ndvi);
};

// Function landsat7and5 NDVI
var NDVI7and5=function(image){
  var ndvi = image.addBands(image.normalizedDifference(['B4', 'B3']).rename('NDVI'));
  return setNdviMinMax(ndvi);
};

// Mapping the NDVI functions on all the collected images seperately 
var landsat8Ndvi = landsat8.map(NDVI8)
  .filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max']));
var landsat5Ndvi = landsat5.map(NDVI7and5)
  .filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max']));
var landsat7Ndvi = landsat7.map(NDVI7and5)
  .filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max']));
var landsat7and5Ndvi = landsat7and5.map(NDVI7and5)
  .filter(ee.Filter.notNull(['NDVI_min', 'NDVI_max']));

//NDVI visualization
var ndviParams = {
  min: -1,
  max: 1.0,
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};

Map.addLayer(landsat8Ndvi.select('NDVI'), ndviParams, 'landsat8Ndvi');
Map.addLayer(landsat5Ndvi.select('NDVI'), ndviParams, 'landsat5Ndvi');
Map.addLayer(landsat7Ndvi.select('NDVI'), ndviParams, 'landsat7Ndvi');
Map.addLayer(landsat7and5Ndvi.select('NDVI'), ndviParams, 'landsat7and5Ndvi');


// calculating Emissivity 
  //1: estimate min and max of each image in  NDVI layer
function addMinMaxBands(img) {
  var imgMin = ee.Image(img.getNumber('NDVI_min')).toFloat();
  var imgMax = ee.Image(img.getNumber('NDVI_max')).toFloat();
  return img.addBands(
    ee.Image.cat(imgMin, imgMax).rename(['NDVI_min', 'NDVI_max'])
  );
}

landsat8Ndvi = landsat8Ndvi.map(addMinMaxBands);
landsat5Ndvi = landsat5Ndvi.map(addMinMaxBands);
landsat7Ndvi = landsat7Ndvi.map(addMinMaxBands);
landsat7and5Ndvi = landsat7and5Ndvi.map(addMinMaxBands);


  //2; fractional vegetation for each image 
function addFVband(img) {
  var ndvi = img.select('NDVI');
  var ndviMin = img.select('NDVI_min').toDouble();
  var ndviMax = img.select('NDVI_max').toDouble();

  var fvBand = ndvi
    .subtract(ndviMin)
    .divide(ndviMax.subtract(ndviMin))
    .rename('FV');

  return img.addBands(fvBand);
}

landsat8Ndvi = landsat8Ndvi.map(addFVband);
landsat5Ndvi = landsat5Ndvi.map(addFVband);
landsat7Ndvi = landsat7Ndvi.map(addFVband);
landsat7and5Ndvi = landsat7and5Ndvi.map(addFVband);

print(landsat8Ndvi);
Map.addLayer(landsat8Ndvi.select('FV'), null, 'FV');

Answered by Justin Braaten on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP