I have a code that computes NDVI but I want help on how can I get the image and date with the highest NDVI within the collection. I am still new to GEE I saw a couple of codes but I failed to understand them.
/// Filtering the image collection
var filtered = S2A.filterBounds(geometry)
.filterDate('2021-02-18','2021-04-30')
//.sort('CLOUD_COVERAGE_ASSESSMENT', false)
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 10);
// testing the collection
print(filtered);
///// computing kNDVI using a function
var addKNDVI = function(img){
var RED = img.select('B4');
var NIR = img.select('B8');
// Compute D2 a rename it to d2
var D2 = NIR.subtract(RED).pow(2)
.select([0],['d2']);
// Gamma, defined as 1/sigmaˆ2
var gamma = ee.Number(4e6).multiply(-2.0);
// Compute kernel (k) and KNDVI
var k = D2.divide(gamma).exp();
var kndvi = ee.Image.constant(1)
.subtract(k).divide(ee.Image.constant(1).add(k))
.select([0],['kndvi']);
return img.addBands(kndvi).clip(geometry);
};
////// maping the whole collection using function
var with_kndvi = filtered.map(addKNDVI);
var greenest = with_kndvi.qualityMosaic('kndvi');
Map.addLayer(with_kndvi,{min:2, max:5, },'kndvi' );
Map.centerObject(geometry,8);
Best Answer
It is unclear whether you want a single image date that represents e.g. the max mean KNDVI within the region of interest or a composite image of the pixels in the time series that are the max KNDVI with an associated date band attached. The answer below includes both. The chunks of code following UPPER CASE comments are edits.
In the mapped function to calculate and add the KNDVI band, an additional band that represents the image timestamp (milliseconds from unix epoch) is added for tracking the dates of the pixels in the time series with max KNDVI. The mean KNDVI region reduction is also added as a property of each image, so that the image with the greatest mean KNDVI can be filtered out later.