I'm trying to select Sentinel-2 (S2) images with less cloud probability inside my AOI (not on the whole scene).
So I followed the steps bellow:
- I've generated an auxiliary feature, getting the mean cloud probability inside my geometry for each image
- I joined the S2 Image Collection with this auxiliary feature, filtering only images with less cloud probability inside my geometry AOI.
It worked good until this point.
But, when I tried to reduce (using median) the ImageCollection, EE seems not to understand that the product of the join is an Image Collection and does not recognize median function.
This is the error returned:
"joined.select(...). median is not a function"
var geometry: Polygon, 4 vertices
// Define start and end
var start = '2024-03-24';
var end = '2024-05-24';
// Collect S2 images and cloud probabilty
var s2sr = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
.filterBounds(geometry)
.filterDate(start, end)
var s2c = ee.ImageCollection("COPERNICUS/S2_CLOUD_PROBABILITY")
.filterBounds(geometry)
.filterDate(start, end)
.select("probability")
// Get cloud probability mean from my geometry AOI
var stats = function(indexCollection) {
return indexCollection.reduceRegions({
collection: geometry,
reducer: ee.Reducer.mean(),
tileScale: 10
}).map(function(feature) {
return feature.set({
'imageID': indexCollection.id(),
}).setGeometry(null);
});
};
var cloud_mean = s2c.map(stats).flatten().filter(ee.Filter.lt('mean', 25));
// Join S2 Image Collection with geometry AOI mean from images
// This step generates a new Image Collection with few images and less cloud incidence
// So far, all is working good
var filter = ee.Filter.equals({
leftField: 'system:index',
rightField: 'imageID'
});
var theJoin = ee.Join.simple()
var joined = theJoin.apply(s2sr, cloud_mean, filter);
print(s2sr, "S2")
print(joined, "S2_Join")
// Here I got the error. Even inputing an ImageCollection, I get an error running the median function.
// "joined.select(...). median is not a function"
var final = joined
.select(["B2", "B3", "B4"])
.median()
.clip(geometry)
print(final, "final")
Note that the structure of the joined Image Collection seems to be the same of original Image Collection. But in the second one I can't run the median reduction.