2

I want to create a new band (emissivity band) for each image in my image collection based on the NDVI values of each pixel (NDVI already is a band).

This means if the NDVI < 0.2 I want to give the pixel the value 0.97, if the NDVI value > 0.5 I want to give the pixel the value 0.98 and if 0.2 <= NDVI <= 0.5 I want to give the pixel a value according to the following formula: Fractional Vegetation X 0.004 + 0.986. Fractional Vegetation is also a band each image in my collection has.

So far I have the following code (based on those codes: Calculating LST from Landsat 8 in Google Earth Engine? and Writing code to add calculated image to display in Google Earth Engine?):

Map.addLayer(geometry);

//cloud mask
function maskL8sr(image) {
  // Bits 3 and 5 = cloud shadow and clouds
  var cloudShadowBitMask = (2.5 << 3);
  var cloudsBitMask = (1 << 5);
  // pixel QA band
  var qa = image.select('pixel_qa');
  // clear conditions if set to zero
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask)
  .copyProperties(image, ['system:time_start'])
}

var start = ee.Date('2013-05-01');

var finish = ee.Date('2013-10-01');


{
var col = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.map(maskL8sr)
.filterDate(start, finish)
.filterBounds(geometry);
}

//calculate NDVI
function add_ndvi(img) {
  var ndvi = img.normalizedDifference(['B5', 'B4'])
  .rename('NDVI')
  .copyProperties(img, ['system:time_start']);
  return(img.addBands(ndvi));
}

var col_ndvi = col.map(add_ndvi);
print(col_ndvi, 'NDVI');


//From DN to at sensor temperature in Kelvin
function add_thermal(img) {
  var thermal = img.select('B10').multiply(0.1)
  .rename('THERMAL')
  .copyProperties(img, ['system:time_start']);
  return(img.addBands(thermal));
}

var col_thermal = col_ndvi.map(add_thermal);
print(col_thermal, 'BT_Kelvin');


// Combined reducer for min and max
var minMaxReducer = ee.Reducer.min().combine({
  reducer2: ee.Reducer.max(),
  sharedInputs: true
});

// Calculate regional min and max 
var minmax = col_thermal.map(function(img) {
  var minMax =  img.reduceRegion(
    {reducer: minMaxReducer, bestEffort:true, scale: 30});
  return img.set({
    ndvi_min: minMax.get('NDVI_min'),
    ndvi_max: minMax.get('NDVI_max')
  });
});

// Filter min/max areas = null
var filtered = minmax
  .filterMetadata('ndvi_min', 'not_equals', null)
  .filterMetadata('ndvi_max', 'not_equals', null);

print(minmax, 'MINMAX');


// Calculate Fractional Vegetation
var fv =filtered.map(function (img) {
  var ndvi = img.select(['NDVI']);
  var max = img.getNumber('max');
  var min = img.getNumber('min');
  var fracveg = img.expression("(NDVI-min)/(max-min)", {
    "NDVI": img.select('NDVI'),
    "min" : img.getNumber('ndvi_min'),
    "max" : img.getNumber('ndvi_max'),
  })
  .rename('FV')
  .copyProperties(img, ['system:time_start']);
  return img.addBands(fracveg);
} );


print(fv, 'FV'); 

//Calculate Emissivity
function emissivity(img){
  var ndvi = img.select('NDVI');
  var soil = ee.Number(0.97);
  var veg = ee.Number (0.98);
  var a= ee.Number(0.004);
  var b= ee.Number(0.986);
  var fv_1 = img.select(['FV']);
  var EM=fv_1.multiply(a).add(b);
  var emis = ee.Image(ee.Algorithms.If(ndvi.lt(0.2), soil, (ee.Algorithms.If(ndvi.gt(0.5), veg, EM))))
  .rename('EM')
  .copyProperties(img, ['system:time_start']);
  return img.addBands(emis);
}

var col_EM= fv.map(emissivity);
print(col_EM, 'EM');  

This is the error I get so far after fixing other errors:

ImageCollection (Error)
Error in map(ID=LC08_193026_20130506): Image.rename, argument 'input': Invalid type. 
Expected type: Image<unknown bands>. Actual type: Float. Actual value: 0.97

Does anybody know how to fix this?

2
  • 1
    Can you add your full script? This will help us to troubleshoot your work.
    – AdoMath
    Feb 11, 2021 at 10:29
  • i have updated the code and there is now the full code until the problem
    – Marlene
    Feb 11, 2021 at 13:44

1 Answer 1

3

Error is because nested Ifs are selecting a float number instead an image. For avoiding complexity inherent to nested Ifs, you can use an 'image.expression' method for producing several intermediate images with conditionals. This approach will produce an image with 1 values where condition is reached and zeros where is not. So, for producing expected values where condition is reached you only need to multiply for original images (pixels remain zero value where condition is not reached). So, final result (EM images) it will be the sum of intermediate raster.

By using an arbitrary geometry in my assets, following code works without any error.

var geometry = ee.FeatureCollection("users/joseguerreroa/indice_severidad/chapelco_32719");

Map.centerObject(geometry, 15);
Map.addLayer(geometry);

//cloud mask
function maskL8sr(image) {
  // Bits 3 and 5 = cloud shadow and clouds
  var cloudShadowBitMask = (2.5 << 3);
  var cloudsBitMask = (1 << 5);
  // pixel QA band
  var qa = image.select('pixel_qa');
  // clear conditions if set to zero
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask)
  .copyProperties(image, ['system:time_start'])
}

var start = ee.Date('2013-05-01');

var finish = ee.Date('2013-10-01');

{
var col = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.map(maskL8sr)
.filterDate(start, finish)
.filterBounds(geometry);
}

//calculate NDVI
function add_ndvi(img) {
  var ndvi = img.normalizedDifference(['B5', 'B4'])
  .rename('NDVI')
  .copyProperties(img, ['system:time_start']);
  return(img.addBands(ndvi));
}

var col_ndvi = col.map(add_ndvi);
print(col_ndvi, 'NDVI');

//From DN to at sensor temperature in Kelvin
function add_thermal(img) {
  var thermal = img.select('B10').multiply(0.1)
  .rename('THERMAL')
  .copyProperties(img, ['system:time_start']);
  return(img.addBands(thermal));
}

var col_thermal = col_ndvi.map(add_thermal);
print(col_thermal, 'BT_Kelvin');


// Combined reducer for min and max
var minMaxReducer = ee.Reducer.min().combine({
  reducer2: ee.Reducer.max(),
  sharedInputs: true
});

// Calculate regional min and max 
var minmax = col_thermal.map(function(img) {
  var minMax =  img.reduceRegion(
    {reducer: minMaxReducer, bestEffort:true, scale: 30});
  return img.set({
    ndvi_min: minMax.get('NDVI_min'),
    ndvi_max: minMax.get('NDVI_max')
  });
});

// Filter min/max areas = null
var filtered = minmax
  .filterMetadata('ndvi_min', 'not_equals', null)
  .filterMetadata('ndvi_max', 'not_equals', null);

print(minmax, 'MINMAX');

// Calculate Fractional Vegetation
var fv =filtered.map(function (img) {
  var ndvi = img.select(['NDVI']);
  var max = img.getNumber('max');
  var min = img.getNumber('min');
  var fracveg = img.expression("(NDVI-min)/(max-min)", {
    "NDVI": img.select('NDVI'),
    "min" : img.getNumber('ndvi_min'),
    "max" : img.getNumber('ndvi_max'),
  })
  .rename('FV')
  .copyProperties(img, ['system:time_start']);
  return img.addBands(fracveg);
} );


print(fv, 'FV'); 

//Calculate Emissivity
function emissivity(img){
  var ndvi = img.select('NDVI');
  var soil = ee.Number(0.97);
  var veg = ee.Number (0.98);

  var fv_1 = img.select(['FV']);
  
  var emis1 =  ee.Image(ndvi.lt(0.2)).multiply(soil)
    .rename('emis1');
  var emis2 =  ee.Image(ndvi.gt(0.5)).multiply(veg)
    .rename('emis2');
  var emis3 =  ee.Image(ndvi.gte(0.2)).and(ndvi.lte(0.5))
    .rename('emis3');
    
  var EM = img.expression(
    'emis1 + emis2 + emis3 * (fv_1 * 0.004 + 0.986)', {
      'emis1': emis1.select('emis1'),
      'emis2': emis2.select('emis2'),
      'emis3': emis3.select('emis3'),
      'fv_1' : fv_1.select('FV')
  }).rename('EM')
  .copyProperties(img, ['system:time_start']);
  
  return img.addBands(EM);
}

var col_EM = fv.map(emissivity);
print(col_EM, 'EM'); 

var EM_img = col_EM.first().clip(geometry)
  .select('EM');

var imageVisParam = {"opacity":1,"bands":["EM"],
  "min":0.98,"max":0.9889986984729767,
  "palette":["ffae4b","59ff23","196bff"]};

Map.addLayer(EM_img, imageVisParam, 'result');

After running above code in GEE code editor, I got following result.

enter image description here

1
  • Thanks a lot!!! This has solved the issue! And I know now how to implement an "if condition" in GEE!
    – Marlene
    Feb 12, 2021 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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