This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
288 lines (230 loc) · 9.2 KB
/
app.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"use strict";
mapboxgl.accessToken = '{YOUR-MAPBOX-TOKEN}';
const sentinel_tiler_url = '{YOUR-API-GATEWAY-ENDPOINT}'; //e.g https://xxxxxxxxxx.execute-api.xxxxxxx.amazonaws.com/production
const sat_api = 'https://api.developmentseed.org/satellites/?search=';
let scope = {};
////////////////////////////////////////////////////////////////////////////////
const sortScenes = (a, b) => {
return Date.parse(b.date) - Date.parse(a.date);
};
const buildQueryAndRequestS2 = (features) => {
$('.list-img').scrollTop(0);
$('.list-img').empty();
$('.errorMessage').addClass('none');
$('.sentinel-info').addClass('none');
if (map.getSource('sentinel-tiles')) map.removeSource('sentinel-tiles');
if (map.getLayer('sentinel-tiles')) map.removeLayer('sentinel-tiles');
const prStr = [].concat.apply([], features.map(function(e){
return "(grid_square:" +
e.properties.Name.slice(3, 5) +
"+AND+latitude_band:" +
e.properties.Name.slice(2, 3) +
"+AND+utm_zone:" +
e.properties.Name.slice(0, 2) +
")";
})).join('+OR+');
const query = sat_api + 'satellite_name:sentinel-2+AND+(' + prStr + ")&limit=2000";
const results = [];
$.getJSON(query, function (data) {
if (data.meta.found !== 0) {
for (let i = 0; i < data.results.length; i += 1) {
let scene = {};
scene.date = data.results[i].date;
scene.cloud = data.results[i].cloud_coverage;
scene.utm_zone = data.results[i].utm_zone.toString();
scene.grid_square = data.results[i].grid_square;
scene.coverage = data.results[i].data_coverage_percentage;
scene.latitude_band = data.results[i].latitude_band;
scene.sceneID = data.results[i].scene_id;
scene.browseURL = data.results[i].thumbnail.replace('.jp2', ".jpg");
scene.path = data.results[i].aws_path.replace('tiles', "#tiles");
scene.grid = scene.utm_zone + scene.latitude_band + scene.grid_square;
results.push(scene);
}
results.sort(sortScenes);
for (let i = 0; i < results.length; i += 1) {
$('.list-img').append(
'<div class="list-element" onclick="initScene(\'' + results[i].sceneID + '\',\'' + results[i].date + '\')">' +
'<div class="block-info">' +
'<img "class="img-item lazy lazyload" src="' + results[i].browseURL + '">' +
'</div>' +
'<div class="block-info">' +
'<span class="scene-info">' + results[i].sceneID + '</span>' +
'<span class="scene-info"><svg class="icon inline-block"><use xlink:href="#icon-clock"/></svg> ' + results[i].date + '</span>' +
'</div>' +
'</div>'
);
}
} else {
$('.errorMessage').removeClass('none');
}
})
.always(function () {
$('.spin').addClass('none');
})
.fail(function () {
$('.errorMessage').removeClass('none');
});
};
const initScene = (sceneID, sceneDate) => {
$(".metaloader").removeClass('none');
$('.errorMessage').addClass('none');
let min = $("#minCount").val();
let max = $("#maxCount").val();
const query = `${sentinel_tiler_url}/sentinel/metadata/${sceneID}?'pmim=${min}&pmax=${max}`;
$.getJSON(query, function (data) {
scope.imgMetadata = data;
updateRasterTile();
$(".sentinel-info").removeClass('none');
$(".sentinel-info .s2id").text(sceneID);
$(".sentinel-info .s2date").text(sceneDate);
})
.fail(function () {
if (map.getSource('sentinel-tiles')) map.removeSource('sentinel-tiles');
if (map.getLayer('sentinel-tiles')) map.removeLayer('sentinel-tiles');
$(".sentinel-info span").text('');
$(".sentinel-info").addClass('none');
$('.errorMessage').removeClass('none');
})
.always(function () {
$('.metaloader').addClass('none');
});
};
const updateRasterTile = () => {
if (map.getSource('sentinel-tiles')) map.removeSource('sentinel-tiles');
if (map.getLayer('sentinel-tiles')) map.removeLayer('sentinel-tiles');
let meta = scope.imgMetadata;
let rgb = $(".img-display-options .toggle-group input:checked").attr("data");
const bands = rgb.split(',');
// NOTE: Calling 512x512px tiles is a bit longer but gives a
// better quality image and reduce the number of tiles requested
// HACK: Trade-off between quality and speed. Setting source.tileSize to 512 and telling landsat-tiler
// to get 256x256px reduces the number of lambda calls (but they are faster)
// and reduce the quality because MapboxGl will oversample the tile.
const tileURL = `${sentinel_tiler_url}/sentinel/tiles/${meta.sceneid}/{z}/{x}/{y}.png?` +
`rgb=${rgb}&tile=256` +
`&histo=${meta.rgbMinMax[bands[0]]}-${meta.rgbMinMax[bands[1]]}-${meta.rgbMinMax[bands[2]]}`;
const attrib = '<span> © Copernicus / ESA 2017 | </span>';
$(".sentinel-info .s2rgb").text(rgb.toString());
map.addSource('sentinel-tiles', {
type: "raster",
tiles: [tileURL],
attribution : attrib,
bounds: scope.imgMetadata.bounds,
minzoom: 7,
maxzoom: 15,
tileSize: 256
});
map.addLayer({
'id': 'sentinel-tiles',
'type': 'raster',
'source': 'sentinel-tiles'
});
};
const updateMetadata = () => {
if (!map.getSource('sentinel-tiles')) return;
initScene(scope.imgMetadata.sceneid, scope.imgMetadata.date);
};
$(".img-display-options .toggle-group").change(function () {
if (map.getSource('sentinel-tiles')) updateRasterTile();
});
document.getElementById("btn-clear").onclick = () => {
if (map.getLayer('sentinel-tiles')) map.removeLayer('sentinel-tiles');
if (map.getSource('sentinel-tiles')) map.removeSource('sentinel-tiles');
map.setFilter("S2_Highlighted", ["in", "Name", ""]);
map.setFilter("S2_Selected", ["in", "Name", ""]);
$('.list-img').scrollLeft(0);
$('.list-img').empty();
$(".metaloader").addClass('off');
$('.errorMessage').addClass('none');
$(".sentinel-info span").text('');
$(".sentinel-info").addClass('none');
scope = {};
$("#minCount").val(5);
$("#maxCount").val(95);
$(".img-display-options .toggle-group input").prop('checked', false);
$(".img-display-options .toggle-group input[data='04,03,02']").prop('checked', true);
$('.map').removeClass('in');
$('.right-panel').removeClass('in');
map.resize();
};
////////////////////////////////////////////////////////////////////////////////
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/vincentsarago/ciynpp13900842rqqtfbzll8x',
center: [-70.50, 40],
zoom: 3,
attributionControl: true,
minZoom: 3,
maxZoom: 15
});
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
map.on('mousemove', function (e) {
const features = map.queryRenderedFeatures(e.point, {layers: ['sentinel-grid']});
if (features.length !== 0) {
const pr = ['any'];
features.forEach(function (e) {
pr.push(["==", "Name", e.properties.Name]);
});
map.setFilter("S2_Highlighted", pr);
} else {
map.setFilter("S2_Highlighted", ["in", "Name", ""]);
}
});
map.on('click', function (e) {
$(".right-panel").addClass('in');
$('.spin').removeClass('none');
const features = map.queryRenderedFeatures(e.point, {layers: ['sentinel-grid']});
if (features.length !== 0) {
$('.map').addClass('in');
$('.list-img').removeClass('none');
map.resize();
const pr = ['any'];
features.forEach(function (e) {
pr.push(["==", "Name", e.properties.Name]);
});
map.setFilter("S2_Selected", pr);
buildQueryAndRequestS2(features);
const geojson = {
'type': 'FeatureCollection',
'features': features
};
const extent = turf.bbox(geojson);
const llb = mapboxgl.LngLatBounds.convert([[extent[0], extent[1]], [extent[2], extent[3]]]);
map.fitBounds(llb, {padding: 50});
} else {
$('.spin').addClass('none');
map.setFilter("S2_Selected", ["in", "PATH", ""]);
}
});
map.on('load', function () {
map.addSource('sentinel', {
"type": "vector",
"url": "mapbox://vincentsarago.0qowxm38"
});
map.addLayer({
"id": "S2_Highlighted",
"type": "fill",
"source": "sentinel",
"source-layer": "Sentinel2_Grid",
"paint": {
"fill-outline-color": "#1386af",
"fill-color": "#0f6d8e",
"fill-opacity": 0.3
},
"filter": ["in", "Name", ""]
});
map.addLayer({
"id": "S2_Selected",
"type": "fill",
"source": "sentinel",
"source-layer": "Sentinel2_Grid",
"paint": {
"fill-outline-color": "#FFF",
"fill-color": "#FFF",
"fill-opacity": 0.2
},
"filter": ["in", "Name", ""]
});
$(".loading-map").addClass('off');
});