2

I'm trying to calculate the VCI (NDVI - minNDVI)/(maxNDVI-minNDVI) for a collection of landsat 5 merged with a collection of landsat 8 because I will need to do this for different periods covering from 1990 to 2018. For testing, I'm using a period from 1998 to 2000 for Switzerland. If I run the following code, I run into the error:

VCI image: Tile error: Expected a homogeneous image collection, but an image with an incompatible band was encountered. Mismatched type for band 'NDVI': Expected type: Float<-0.29947625586281806, 1.2251065502612928>. Actual type: Float<-0.4166935481920486, 1.2483430624070735>. Image ID: null This band might require an explicit cast.

Is there a problem with how I calculate the NDVI? How could I solve this error?

var bbox = ee.Geometry.Polygon([
  [5.9559,45.818],
  [5.9559,47.8084],
  [10.4921,47.8084],
  [10.4921,45.818],
  [5.9559,45.818]]);
  
Map.centerObject(bbox);
Map.addLayer(bbox, {color: 'FF0000'}, 'geodesic polygon');

// Assign a common name to the sensor-specific bands.
var LC8_BANDS = ['B2',   'B3',    'B4',  'B5',  'B6',    'B7',    'B10']; //Landsat 8
var LC5_BANDS = ['B1',   'B2',    'B3',  'B4',  'B5',    'B7',    'B6']; //Llandsat 5
var STD_NAMES = ['blue', 'green', 'red', 'nir', 'swir1', 'swir2', 'temp'];

var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA').filterDate('1998-1-1', '2000-12-31')
                                                      .filterBounds(bbox);// Landsat 8
//print(l8, 'Landsat 8')

var l5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA').filterDate('1998-1-1', '2000-12-31')
                                                      .filterBounds(bbox); //Landsat 5
//print(l5, 'Landsat 5')

var withCloudiness = l8.map(function(image) {
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
  var cloudiness = cloud.reduceRegion({
    reducer: 'mean', 
    geometry: bbox, 
    bestEffort: true,
    scale: 30,
  });

  return image.set(cloudiness);
});

var filteredCollectionl8 = withCloudiness.filter(ee.Filter.lt('cloud', 20)).select(LC8_BANDS, STD_NAMES);

var withCloudiness = l5.map(function(image) {
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');
  var cloudiness = cloud.reduceRegion({
    reducer: 'mean', 
    geometry: bbox, 
    bestEffort: true,
    scale: 30,
  });

  return image.set(cloudiness);
});

var filteredCollectionl5 = withCloudiness.filter(ee.Filter.lt('cloud', 20)).select(LC5_BANDS, STD_NAMES);

var lall = ee.ImageCollection(filteredCollectionl5.merge(filteredCollectionl8));
//print(lall, 'Merged')

var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['nir', 'red']).rename('NDVI');
  return image.addBands(ndvi);
};

var ndviColl = lall.map(addNDVI);

var vci = ndviColl.map(function(img){
 var id = img.id();
 var min =  img.reduceRegion(ee.Reducer.min(), bbox,300).get('NDVI');
 var max = img.reduceRegion(ee.Reducer.max(), bbox,300).get('NDVI');
 return img.expression(
   "(NDVI-min)/(max-min)",{
     "NDVI" : img,
     "max" : ee.Number(max),
     "min" : ee.Number(min)
   }).copyProperties(img,['system:time_start','system:time_end']);
});

print(vci, 'VCI')

// Display the result.
//Map.centerObject(vci, 9);
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
var vciParams = {min: 0, max:1}
Map.addLayer(vci.mean(), vciParams, 'VCI image');

1 Answer 1

1

That error means that not all bands in your ImageCollection are of the same type. Try casting them to float:

var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['nir', 'red']).rename('NDVI');
  return image.addBands(ndvi).toFloat();
};

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.