I've filtered an imagecollcetion down to all images in an area between a specified range of dates annually (imagecollection A) and then filtered it further by narrowing it down to the most cloud-free images within that range.
//-------------------- Generate AOI
var pointBuffer = geometry.buffer({'distance': 6000});
//-------------------- Set Parameters
// Range of years
var startyear = 2022;
var endyear = 2023;
// Rage of months
var startMonth = 5;
var endMonth = 7;
// Minimum yellow threshold - for rapseed
var min_y_tresh = 0.035
//-------------------- Load Sentinel-2 TOA reflectance data.
var imagecollection_A = ee.ImageCollection('COPERNICUS/S2')
.filterDate(startyear+'-01-01', endyear+1+'-01-01')
.filterBounds(geometry)
.filter(ee.Filter.calendarRange(startMonth, endMonth, 'month'));
print(imagecollection_A,"imagecollection_A Check")
//-------------------- Generate and map NDYI and NDYI Mask to image collection
// Function generates NDYI
var addNDYI = function(image) {
var NDYI = image.normalizedDifference(['B3','B2']).rename('NDYI');
return image.addBands(NDYI);
};
// Function masks NDYI to specified treshold
var addNDYI_Mask = function(image) {
var masked = image.select('NDYI').updateMask(image.select('NDYI').gte(min_y_tresh)).rename('NDYI_Masked');
return image.addBands(masked);
};
var imagecollection_B = imagecollection_A.map(addNDYI);
var imagecollection_C = imagecollection_B.map(addNDYI_Mask);
print(imagecollection_C,"Collection Check");
//-------------------- Get Cloud Free Image Per Year
var years = ee.List.sequence(startyear, endyear).getInfo();
var imagecollection_D = years.map(function(year){
// Get least cloudy annual single scene.
var annual = imagecollection_C.filterDate(year+'-01-01', (year+1)+'-01-01')
.sort('CLOUD_COVERAGE_ASSESSMENT');
var annual_img = ee.Image(annual.first());
return annual_img;
});
imagecollection_D = ee.ImageCollection(imagecollection_D);
print(imagecollection_D,"annual_least_ccCheck")
What I would like to do is filter imagecollection A based on the pixel values of the NDYI band that has been added.
I know I can filter band values using .reduce(ee.Reducer.max())
, but this results in a single band when I try to apply it.
I'm trying to generate an imagecollection that has been filtered down to a specific date range annually, then filtered by an NDYI thresholded pixel value, and finally to the least cloudy image.
I can get everything else but I just can't figure out the NDYI thresholded filter. Any help would be greatly appreciated!
Below is a link to the complete code: https://code.earthengine.google.com/bb51958baa978cd2fc82130a3975b8fa