-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathimagecollection.js
More file actions
436 lines (398 loc) · 15.4 KB
/
imagecollection.js
File metadata and controls
436 lines (398 loc) · 15.4 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* @fileoverview Javascript representation for an Earth Engine ImageCollection.
*/
goog.provide('ee.ImageCollection');
goog.require('ee.ApiFunction');
goog.require('ee.Collection');
goog.require('ee.ComputedObject');
goog.require('ee.Image');
goog.require('ee.List');
goog.require('ee.Types');
goog.require('ee.arguments');
goog.require('ee.data');
goog.require('ee.data.images');
goog.require('goog.array');
goog.require('goog.string');
/**
* ImageCollections can be constructed from the following arguments:
* - A string: assumed to be the name of a collection,
* - A list of images, or anything that can be used to construct an image.
* - A single image.
* - A computed object - reinterpreted as a collection.
*
* @param {string|Array.<*>|ee.Image|ee.ComputedObject} args
* The constructor arguments.
* @constructor
* @extends {ee.Collection}
* @export
*/
ee.ImageCollection = function(args) {
// Constructor safety.
if (!(this instanceof ee.ImageCollection)) {
return ee.ComputedObject.construct(ee.ImageCollection, arguments);
} else if (args instanceof ee.ImageCollection) {
return args;
}
if (arguments.length != 1) {
throw Error('The ImageCollection constructor takes exactly 1 argument (' +
arguments.length + ' given)');
}
ee.ImageCollection.initialize();
// Wrap single images in an array.
if (args instanceof ee.Image) {
args = [args];
}
if (ee.Types.isString(args)) {
// An ID.
ee.ImageCollection.base(this, 'constructor', new ee.ApiFunction('ImageCollection.load'), {
'id': args
});
} else if (Array.isArray(args)) {
// A list of images.
ee.ImageCollection.base(this, 'constructor', new ee.ApiFunction('ImageCollection.fromImages'), {
'images': goog.array.map(args, function(elem) {
return new ee.Image(elem);
})
});
} else if (args instanceof ee.List) {
// A computed list of image. This can't get the extra ee.Image().
ee.ImageCollection.base(this, 'constructor', new ee.ApiFunction('ImageCollection.fromImages'), {
'images': args
});
} else if (args instanceof ee.ComputedObject) {
// A custom object to reinterpret as an ImageCollection.
ee.ImageCollection.base(this, 'constructor', args.func, args.args, args.varName, args.unbound);
} else {
throw Error('Unrecognized argument type to convert to an ' +
'ImageCollection: ' + args);
}
};
goog.inherits(ee.ImageCollection, ee.Collection);
/**
* Whether the class has been initialized with API functions.
* @type {boolean}
* @private
*/
ee.ImageCollection.initialized_ = false;
/**
* Imports API functions to this class.
*/
ee.ImageCollection.initialize = function() {
if (!ee.ImageCollection.initialized_) {
ee.ApiFunction.importApi(
ee.ImageCollection, 'ImageCollection', 'ImageCollection');
ee.ApiFunction.importApi(
ee.ImageCollection, 'reduce', 'ImageCollection');
ee.ImageCollection.initialized_ = true;
}
};
/**
* Removes imported API functions from this class.
*/
ee.ImageCollection.reset = function() {
ee.ApiFunction.clearApi(ee.ImageCollection);
ee.ImageCollection.initialized_ = false;
};
/**
* Get the URL of a tiled thumbnail for this ImageCollection.
* @param {!Object} params Parameters identical to ee.data.getMapId, plus,
* optionally:
* <table>
* <tr>
* <td><code> dimensions </code> (a number or pair of numbers in
* format WIDTHxHEIGHT) Maximum dimensions of each thumbnail frame to
* render, in pixels. If only one number is passed, it is used as the
* maximum, and the other dimension is computed by proportional
* scaling.</td>
* </tr>
* <tr>
* <td><code> region </code> (E,S,W,N or GeoJSON) Geospatial region of
* the image to render. By default, the whole image.</td>
* </tr>
* <tr>
* <td><code> format </code> (string) Encoding format. Only 'png'
* or 'jpg' are accepted.</td>
* </tr>
* </table>
* @param {function(string, string=)=} opt_callback An optional
* callback which handles the resulting URL string. If not supplied, the
* call is made synchronously.
* @return {string|undefined} A thumbnail URL, or undefined if a callback
* was specified.
* @export
*/
ee.ImageCollection.prototype.getFilmstripThumbURL = function(params, opt_callback) {
const args = ee.arguments.extractFromFunction(
ee.ImageCollection.prototype.getFilmstripThumbURL, arguments);
return ee.ImageCollection.prototype.getThumbURL_(
this, args, ['png', 'jpg', 'jpeg'],
ee.ImageCollection.ThumbTypes.FILMSTRIP, opt_callback);
};
/**
* Get the URL of an animated thumbnail for this ImageCollection.
* @param {!Object} params Parameters identical to ee.data.getMapId, plus,
* optionally:
* <table>
* <tr>
* <td><code> dimensions </code> (a number or pair of numbers in format
* WIDTHxHEIGHT) Maximum dimensions of the thumbnail to render, in
* pixels. If only one number is passed, it is used as the maximum, and
* the other dimension is computed by proportional scaling.</td>
* </tr>
* <tr>
* <td><code> region </code> (E,S,W,N or GeoJSON) Geospatial region of
* the image to render. By default, the whole image.</td>
* </tr>
* <tr>
* <td><code> format </code> (string) Encoding format. Only 'gif'
* is accepted.</td>
* </tr>
* <tr>
* <td><code> framesPerSecond </code> (number) Animation speed.</td>
* </tr>
* </table>
* @param {function(string, string=)=} opt_callback An optional
* callback which handles the resulting URL string. If not supplied, the
* call is made synchronously.
* @return {string|undefined} A thumbnail URL, or undefined if a callback
* was specified.
* @export
*/
ee.ImageCollection.prototype.getVideoThumbURL = function(params, opt_callback) {
const args = ee.arguments.extractFromFunction(
ee.ImageCollection.prototype.getVideoThumbURL, arguments);
return ee.ImageCollection.prototype.getThumbURL_(
this, args, ['gif'], ee.ImageCollection.ThumbTypes.VIDEO, opt_callback);
};
/**
* Valid thumbnail types.
* @enum {string}
*/
ee.ImageCollection.ThumbTypes = {
FILMSTRIP: 'filmstrip',
VIDEO: 'video',
IMAGE: 'image',
};
/**
* Get a thumbnail URL for this image.
* @param {!ee.ImageCollection} collection The collection to export.
* @param {!Object} args Visualization arguments.
* @param {!Array<string>} validFormats A nonempty list of valid formats.
* @param {!ee.ImageCollection.ThumbTypes=} opt_thumbType The type of
* thumbnail.
* @param {function(string, string=)=} opt_callback An optional
* callback which handles the resulting URL string. If not supplied, the
* call is made synchronously.
* @return {string|undefined} A thumbnail URL, or undefined if a callback
* was specified.
* @private
*/
ee.ImageCollection.prototype.getThumbURL_ = function(
collection, args, validFormats, opt_thumbType, opt_callback) {
const extraParams = {};
const clippedCollection = collection.map(
(image) => {
const projected = ee.data.images.applyCrsAndTransform(
/** @type {!ee.Image} */ (image), args['params']);
const scaled = ee.data.images.applySelectionAndScale(
projected, args['params'], extraParams);
return scaled;
});
/** @type {!ee.data.ThumbnailOptions} */
const request = {};
const visParams = ee.data.images.extractVisParams(extraParams, request);
request.imageCollection =
/** @type {!ee.ImageCollection} */ (clippedCollection.map((image) => {
visParams.image = /** @type {!ee.Image} */ (image);
return ee.ApiFunction._apply('Image.visualize', visParams);
}));
if (args['params']['dimensions'] != null) {
request.dimensions = args['params']['dimensions'];
}
// Only allow valid formats, using the first of `validFormats` as a default.
// The server may support other formats.
// TODO(user): remove this once separate backend endpoints do this check.
if (request.format) {
const matchesRequest = (format) => goog.string.caseInsensitiveEquals(
format, /** @type {string} */ (request.format));
if (!goog.array.some(validFormats, matchesRequest)) {
throw Error('Invalid format specified.');
}
} else {
request.format = validFormats[0];
}
let getThumbId = ee.data.getThumbId;
switch (opt_thumbType) {
case ee.ImageCollection.ThumbTypes.VIDEO:
getThumbId = ee.data.getVideoThumbId;
break;
case ee.ImageCollection.ThumbTypes.FILMSTRIP:
getThumbId = ee.data.getFilmstripThumbId;
break;
}
if (args['callback']) {
const callbackWrapper = function(thumbId, opt_error) {
let thumbUrl = '';
if (opt_error === undefined) {
try {
thumbUrl = ee.data.makeThumbUrl(thumbId);
} catch (e) {
opt_error = String(e.message);
}
}
args['callback'](thumbUrl, opt_error);
};
getThumbId(request, callbackWrapper);
} else {
return ee.data.makeThumbUrl(
/** @type {!ee.data.ThumbnailId} */ (getThumbId(request)));
}
};
/**
* An imperative function that returns a map ID via a synchronous AJAX call.
*
* This mosaics the collection to a single image and return a map ID suitable
* for building a Google Maps overlay.
*
* @param {?Object=} opt_visParams The visualization parameters.
* @param {function(!Object, string=)=} opt_callback An async callback.
* If not supplied, the call is made synchronously.
* @return {!ee.data.MapId|undefined} Returns a map ID and optional token, which
* may be passed to ee.data.getTileUrl or ui.Map.addLayer. Undefined if
* a callback was specified.
* @export
*/
ee.ImageCollection.prototype.getMapId = function(opt_visParams, opt_callback) {
var args = ee.arguments.extractFromFunction(
ee.ImageCollection.prototype.getMapId, arguments);
var mosaic = /** @type {!ee.Image} */(
ee.ApiFunction._call('ImageCollection.mosaic', this));
if (args['callback']) {
mosaic.getMapId(args['visParams'], args['callback']);
} else {
return mosaic.getMapId(args['visParams']);
}
};
/**
* An imperative function that returns a map ID via a synchronous AJAX call.
*
* This mosaics the collection to a single image and return a map ID suitable
* for building a Google Maps overlay.
*
* @deprecated Use getMapId() instead.
* @param {?Object=} opt_visParams The visualization parameters.
* @param {function(!Object, string=)=} opt_callback An async callback.
* If not supplied, the call is made synchronously.
* @return {!ee.data.MapId|undefined} Returns a map ID and optional token, which
* may be passed to ee.data.getTileUrl or ui.Map.addLayer. Undefined if
* a callback was specified.
* @export
*/
ee.ImageCollection.prototype.getMap = ee.ImageCollection.prototype.getMapId;
/**
* An imperative function that returns all the known information about this
* collection via an AJAX call.
*
* @param {function(!ee.data.ImageCollectionDescription, string=)=} opt_callback
* An optional callback. If not supplied, the call is made synchronously.
* If supplied, will be called with the first parameter if successful and
* the second if unsuccessful.
* @return {!ee.data.ImageCollectionDescription} A collection description
* whose fields include:
* - features: a list containing metadata about the images in the
* collection.
* - bands: a dictionary describing the bands of the images in this
* collection.
* - properties: an optional dictionary containing the collection's
* metadata properties.
* @export
* @override
*/
ee.ImageCollection.prototype.getInfo = function(opt_callback) {
return /** @type {!ee.data.ImageCollectionDescription} */(
ee.ImageCollection.base(this, 'getInfo', opt_callback));
};
/**
* Select bands from each image in a collection.
*
* @param {!Array.<string|number>} selectors A list of names,
* regexes or numeric indices specifying the bands to select.
* @param {!Array.<string>=} opt_names A list of new names for the output bands.
* Must match the number of bands selected.
* @return {!ee.ImageCollection} The image collection with selected bands.
* @export
*/
ee.ImageCollection.prototype.select = function(selectors, opt_names) {
var varargs = arguments;
return /** @type {!ee.ImageCollection} */(this.map(function(obj) {
var img = /** @type {!ee.Image} */(obj);
return img.select.apply(img, varargs);
}));
};
/**
* Links images in this collection to matching images from `imageCollection`.
*
* For each source image in this collection, any specified bands or metadata
* will be added to the source image from the matching image found in
* `imageCollection`. If bands or metadata are already present, they will be
* overwritten. If matching images are not found, any new or updated bands will
* be fully masked and any new or updated metadata will be null. The output
* footprint will be the same as the source image footprint.
*
* Matches are determined if a source image and an image in `imageCollection`
* have a specific equivalent metadata property. If more than one collection
* image would match, the collection image selected is arbitrary. By default,
* images are matched on their 'system:index' metadata property.
*
* This linking function is a convenience method for adding bands to target
* images based on a specified shared metadata property and is intended to
* support linking collections that apply different processing/product
* generation to the same source imagery. For more expressive linking known as
* 'joining', see https://developers.google.com/earth-engine/guides/joins_intro.
*
* @param {!ee.ImageCollection} imageCollection The image collection searched to
* find matches from this collection.
* @param {?Array<string>=} opt_linkedBands Optional list of band names to add
* or update from matching images.
* @param {?Array<string>=} opt_linkedProperties Optional list of metadata
* properties to add or update from matching images.
* @param {string=} opt_matchPropertyName The metadata property name to use as a
* match criteria. Defaults to "system:index".
* @return {!ee.ImageCollection} The linked image collection.
* @export
*/
ee.ImageCollection.prototype.linkCollection = function(
imageCollection, opt_linkedBands, opt_linkedProperties,
opt_matchPropertyName) {
let args = ee.arguments.extractFromFunction(
ee.ImageCollection.prototype.linkCollection, arguments);
return /** @type {!ee.ImageCollection} */ (this.map(function(obj) {
let img = /** @type {!ee.Image} */ (obj);
img = /** @type {!ee.Image} */ (
ee.ApiFunction._call(
'Image.linkCollection',
img,
/** @type {!ee.ImageCollection} */ (args['imageCollection']),
/** @type {?Array<string>} */ (args['linkedBands']),
/** @type {?Array<string>} */ (args['linkedProperties']),
/** @type {?string} */ (args['matchPropertyName'])));
return img;
}));
};
/**
* Returns the first entry from a given collection.
*
* @return {!ee.Image} The collection from which to select the first entry.
* @export
*/
ee.ImageCollection.prototype.first = function() {
return new ee.Image(ee.ApiFunction._call('Collection.first', this));
};
/** @override */
ee.ImageCollection.prototype.name = function() {
return 'ImageCollection';
};
/** @override */
ee.ImageCollection.prototype.elementType = function() {
return ee.Image;
};