My goal is to chart the number of Sentinel-2 images within AOI aggregated by month on a histogram. Currently I use ui.Chart.feature.histogram
and plot by property 'month' that I extracted from image 'system:time_start'
. This works for intervals within 1 year, but if I increase the interval it starts agregating corresponding months for different years.
But the goal is to have an X-axis representing Years and Months like on time series chart (below) and Y-axis represent number of images aggregated by month, so how do I do it?
Here is my current implementation:
// ================================ VARS & PARAMS ==========================================
// data
var s2_coll = ee.ImageCollection('COPERNICUS/S2');
// aoi
var aoi = ee.Geometry.Polygon(
[[[12.073281620012267, 42.652666751867734],
[12.073281620012267, 42.60214521751987],
[12.150185916887267, 42.60214521751987],
[12.150185916887267, 42.652666751867734]]], null, false);
// dates
var year_start = 2019;
var year_end = null; // DO NOT FUNCTION YET
var month_start = 1;
var month_end = 12;
// styling and display
var chart_options = {
title: 'S2 imagery number',
hAxis: {title: 'Month'},
vAxis: {title: 'Image count'},
colors: ['orange']
};
// misc
// ================================ FUNCTIONS ==============================================
var metadata_collector = function(img) {
var month = img.date().format("M"); // get month
return img.set('month', ee.Number.parse(month)); //
};
// ================================ IMPLEMENTATION =========================================
// preliminary filtering collection by aoi and dates
var s2_coll_filt = s2_coll
.filterBounds(aoi)
.filter(ee.Filter.calendarRange(year_start, year_end, 'year'))
.filter(ee.Filter.calendarRange(month_start, month_end, 'month'));
var s2_coll_filt = s2_coll_filt.map(metadata_collector);
var s2_fc = ee.FeatureCollection(s2_coll_filt);
var num_scn_total = ee.FeatureCollection(s2_coll_filt).size();
var txt_total_scns = 'Total number of scenes within AOI is: ';
var info_text = ee.String(txt_total_scns).cat(num_scn_total);
// ================================ VISUALIZATION ==========================================
// map
Map.centerObject(aoi);
// console
info_text.evaluate(function(result){
print(result);
});
var histogram = ui.Chart.feature.histogram({
features: s2_fc,
// property: 'system:time_start',
property: 'month',
minBucketWidth: 1
}).setOptions(chart_options);
print(histogram);