3

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?

enter image description here

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);

Code Editor script

1 Answer 1

1

You can do that fairly easy. I made something similar (or add this), but then combining Landsat and Sentinel data and aggregated that over multiple years (ignoring months). So I used part of that code, and part of your provided code to produce what you are trying to achieve.

// use function to get imagery count
var FC = metadata_collector(s2_coll, aoi, year_start, year_end, month_start, month_end);

// make the graph
var chart = ui.Chart.feature.byFeature(FC, 'system:time_start', ['S2'])
                  .setChartType('ColumnChart').setOptions(chart_options);
print(chart);

Where the function was defined as:

function metadata_collector(data, geometry, startYear, endYear, startMonth, endMonth) {

  // Map over the years 
  var listYears = ee.List.sequence(startYear, endYear, 1);
  var monthList = ee.List.sequence(startMonth, endMonth, 1);

  // map over the years, get collection size per year and return as feature
  var feats = ee.FeatureCollection(listYears.map(function(year){
      // map over the month
      var byMonth = monthList.map(function(month){
        // set start and end date from the year
        var start = ee.Date.fromYMD(year, month, 1);
        var end = start.advance(1, 'month');

        // filter the collection
        var S2_filt = data.filterBounds(geometry).filterDate(start, end);

        // daily moaic sentinel-2 images
        var S2_size = dailyMosaics(S2_filt); // see function in link

        // Return the size in a feature
        var feat = ee.Feature(geometry, {year: year, 'system:time_start': start.millis(),
                      S2: S2_size}); // geometry added to allow exporting
        return feat;
    });
    return byMonth;
  }).flatten());

  return feats;
}

Full link code

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.