Repository navigation
Expand file tree
/
Copy path04_timelapse.js
More file actions
195 lines (149 loc) · 5.11 KB
/
Copy path04_timelapse.js
File metadata and controls
195 lines (149 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2024 Google LLC
/**
* Visualize imagery with timelapse prototype
*
* @author Gennadii Donchyts (dgena@google.com)
* @author Tyler Erickson (tyler@vorgeo.com)
*/
/*******************************************************************************
* Model *
******************************************************************************/
var m = {};
m.start = '2022';
m.end = '2023';
var images = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.filterDate(m.start, m.end);
var clouds = ee.ImageCollection('GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED')
.filterDate(m.start, m.end)
.select('cs_cdf');
// Combine collections
images = images.linkCollection(clouds, ['cs_cdf']);
/*******************************************************************************
* Styling *
******************************************************************************/
var s = {};
s.vis = {
bands: ['B12', 'B8', 'B3'],
min: 500,
max: 3500
};
/*******************************************************************************
* Components *
******************************************************************************/
var c = {};
function Timelapse() {
this.panel = null;
this.mapControl = Map; // default map
this.mapLayers = [];
this.currentIndex = 0;
}
Timelapse.prototype.buildUI = function() {
var self = this;
var label = ui.Label();
var slider = ui.Slider(0, 1, 0, 1);
slider.style().set({ width: '300px' });
slider.onSlide(function(i) {
self.mapLayers[self.currentIndex].setOpacity(0);
self.currentIndex = i;
self.mapLayers[self.currentIndex].setShown(true);
self.mapLayers[self.currentIndex].setOpacity(1);
label.setValue(self.mapLayers[self.currentIndex].getName());
});
var button = ui.Button('Clear');
button.style().set({
margin: '0px',
padding: '0px',
width: '60px'
});
button.onClick(function() {
self.clear();
});
label.setValue(self.mapLayers[self.currentIndex].getName());
slider.setMax(self.mapLayers.length - 1);
self.panel = ui.Panel([
label,
ui.Panel([slider, button], ui.Panel.Layout.flow('horizontal'))
]);
return self.panel;
};
Timelapse.prototype.inspect = function(images, pt, vis) {
var self = this;
self.images = images;
self.pt = pt;
self.clear();
var scale = self.mapControl.getScale() ;
var r = 200;
var d = 50;
var aoi = pt.buffer(r * scale);
var mask = ee.Image().paint(pt.buffer(scale * (r - d)), 1).fastDistanceTransform().sqrt()
.reproject(ee.Projection('EPSG:3857').atScale(scale))
.unitScale(0, d)
.clip(aoi);
mask = ee.Image(1).subtract(mask).pow(2).selfMask();
images.aggregate_array('system:time_start')
.evaluate(function(times) {
times.map(function(t, i) {
var image = images
.filter(ee.Filter.eq('system:time_start', t))
.first()
.select(s.vis.bands)
.clip(aoi)
.updateMask(mask);
var mapLayer = ui.Map.Layer(image, vis, new Date(t).toISOString(), true, i === 0 ? 1 : 0);
self.mapLayers.push(mapLayer);
self.mapControl.layers().add(mapLayer);
});
if(self.panel) {
self.mapControl.remove(self.panel);
}
self.panel = self.buildUI();
self.mapControl.add(self.panel);
});
};
Timelapse.prototype.clear = function() {
var self = this;
self.mapControl.widgets().remove(self.panel);
self.panel = null;
self.panel = null;
self.mapLayers.map(function(mapLayer) {
self.mapControl.layers().remove(mapLayer);
});
self.mapLayers.length = 0;
self.currentIndex = 0;
self.pt = null;
self.images = null;
};
c.timelapse = new Timelapse();
/*******************************************************************************
* Composition *
******************************************************************************/
Map.setOptions('HYBRID');
Map.style().set({ cursor: 'crosshair' });
Map.addLayer(images.select(s.vis.bands)
.reduce(ee.Reducer.percentile([25]))
.rename(s.vis.bands),
s.vis,
'images',
false);
Map.addLayer(clouds, {}, 'clouds', false);
/*******************************************************************************
* Behaviors *
******************************************************************************/
Map.onClick(function(pt) {
pt = ee.Geometry.Point([pt.lon, pt.lat]);
// Remove cloudy images
var scale = Map.getScale();
var geom = pt.buffer(scale*50, scale*5);
var imagesLocal = images.filterBounds(pt)
.map(function(i) {
var quality = i.select('cs_cdf')
.reduceRegion(ee.Reducer.mean(), geom, scale*5);
return i.set({ quality: quality.values().get(0) });
})
.filter(ee.Filter.gt('quality', 0.7));
// Inspect images
c.timelapse.inspect(imagesLocal, pt, s.vis);
});
/*******************************************************************************
* Initialize *
******************************************************************************/