2

I am using ui.DateSlider() on Google Earth Engine. The period argument for that object seems to be the number of days (ref: https://developers.google.com/earth-engine/api_docs).

I would like to control the period to be monthly, or semi-monthly (say 1st-14th, and 15th-end-of-month). Are these two configurations possible?

I am unable to achieve it with the period being restricted to number of days as the months have different days.

Sample code:

// Use a DateSlider to create annual composites of this collection.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1');
// Use the start of the collection and now to bound the slider.
var start = ee.Image(collection.first()).date().get('year').format();
var now = Date.now();
var end = ee.Date(now).format();

// Run this function on a change of the dateSlider.
var showMosaic = function(range) {
  var mosaic = ee.Algorithms.Landsat.simpleComposite({
    collection: collection.filterDate(range.start(), range.end())
  });
  // Asynchronously compute the name of the composite.  Display it.
  range.start().get('year').evaluate(function(name) {
    var visParams = {bands: ['B4', 'B3', 'B2'], max: 100};
    var layer = ui.Map.Layer(mosaic, visParams, name + ' composite');
    Map.layers().set(0, layer);
  });
};

// Asynchronously compute the date range and show the slider.
var dateRange = ee.DateRange(start, end).evaluate(function(range) {
  var dateSlider = ui.DateSlider({
    start: range['dates'][0],
    end: range['dates'][1],
    value: null,
    period: 365, //<= I would like to change this to monthly or semi-monthly
    onChange: showMosaic
  });
  Map.add(dateSlider.setValue(now));
});

How do I change the period to be monthly or semi-monthly?

1 Answer 1

1

Yes, you can. Just pass the period: 15 for semi-monthly in the DateSlider constructor.

var dateSlider = ui.DateSlider({
    start: range['dates'][0],
    end: range['dates'][1],
    value: null,
    period: 15, // semi-monthly
    onChange: showMosaic
  });

This way, when an user clicks on a date, the range passed to showMosaic will be [clikedDate, clickedDate+15). Note that in most computations with date ranges the end of the interval is open, so the last day is not included.

What you cannot easily do is to restrict the date to start either on the 1st or on the 15th.

The period seems to start somewhat randomly. I have just tried with a date range between 2016-Mar-01 -> 2020-Mar-31. If you Jump to date 1/Mar/2016, then the period is the first half of March. Each consecutive square is the following half-month. But since some months have 31 days, it deviates from 1st->15th structure.

2
  • period: 15 is not a solution since every month does have not 30 days. I am accepting your answer since you mentioned that it is not possible to have the slider jump to 1st and 15th of every month.
    – kkrao
    Apr 15, 2020 at 18:17
  • @kkrao Thanks. See my edit, please :) Apr 16, 2020 at 12:53

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.