Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Only request new monthly layers when selecting a new month interval

ZachTRicec5662a92129d
diff --git a/web/js/map/layerbuilder.js b/web/js/map/layerbuilder.js
index 2cbc6855b..2b665b277 100644
--- a/web/js/map/layerbuilder.js
+++ b/web/js/map/layerbuilder.js
@@ -112,7 +112,47 @@ export function mapLayerBuilder(models, config, cache, Parent) {
date = util.toISOStringDate(date);
dateId = date;
} else if (def.period === 'monthly') {
- date = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1));
+ var dateArray = [];
+ // Offset timezone
+ var currentDate = new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
+ lodashEach(def.dateRanges, function (dateRange) {
+ var monthDifference;
+ let dateInterval = dateRange.dateInterval;
+ let minDate = new Date(dateRange.startDate);
+ let maxDate = new Date(dateRange.endDate);
+
+ // Offset timezone
+ minDate = new Date(minDate.getTime() + (minDate.getTimezoneOffset() * 60000));
+ maxDate = new Date(maxDate.getTime() + (maxDate.getTimezoneOffset() * 60000));
+
+ // Check if date is between dateRange.startDate && dateRange.endDate
+ if (currentDate >= minDate && currentDate <= maxDate) {
+ // Find the monthDifference of the endDate vs startDate (i.e. 10)
+ monthDifference = util.monthDiff(minDate, maxDate);
+ }
+
+ // Create array of all possible request dates by saying for interval++ <= monthDifference
+ for (dateInterval = 0; dateInterval <= monthDifference; dateInterval++) {
+ dateArray.push(new Date(minDate.getUTCFullYear(), minDate.getUTCMonth() + dateInterval, minDate.getUTCDate(), 0, 0, 0));
+ }
+ });
+
+ // Find the closest dates within the current aray
+ dateArray.sort(function(a, b) {
+ var distancea = Math.abs(currentDate - a);
+ var distanceb = Math.abs(currentDate - b);
+ return distancea - distanceb; // sort a before b when the distance is smaller
+ });
+
+ // Filter the closest dates to only show dates before the currently selected date
+ var closestBeforeMonths = dateArray.filter(function(d) {
+ return d - currentDate < 0;
+ });
+
+ // Closest month, in date range, before current date = closestBeforeMonths[0]
+ if (closestBeforeMonths[0]) {
+ date = closestBeforeMonths[0];
+ }
date = util.toISOStringDate(date);
dateId = date;
} else if (def.period === 'yearly') {
diff --git a/web/js/util/util.js b/web/js/util/util.js
index fdd65da78..06f9fcb6c 100644
--- a/web/js/util/util.js
+++ b/web/js/util/util.js
@@ -814,5 +814,19 @@ export default (function (self) {
return value;
};
+ // Returns the number of months between two dates
+ self.monthDiff = function(date1, date2) {
+ var year1 = date1.getFullYear();
+ var year2 = date2.getFullYear();
+ var month1 = date1.getMonth();
+ var month2 = date2.getMonth();
+ if (month1 === 0) {
+ month1++;
+ month2++;
+ }
+ var numberOfMonths = (year2 - year1) * 12 + (month2 - month1);
+ return numberOfMonths;
+ };
+
return self;
})({});