-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbatch.js
More file actions
1090 lines (998 loc) · 35.5 KB
/
batch.js
File metadata and controls
1090 lines (998 loc) · 35.5 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @fileoverview An interface to the Earth Engine batch processing system. */
goog.module('ee.batch');
goog.module.declareLegacyNamespace();
const ComputedObject = goog.require('ee.ComputedObject');
const Element = goog.require('ee.Element');
const FeatureCollection = goog.require('ee.FeatureCollection');
const Geometry = goog.require('ee.Geometry');
const GoogPromise = goog.require('goog.Promise');
const Image = goog.require('ee.Image');
const ImageCollection = goog.require('ee.ImageCollection');
const data = goog.require('ee.data');
const eeArguments = goog.require('ee.arguments');
const googArray = goog.require('goog.array');
const googAsserts = goog.require('goog.asserts');
const googObject = goog.require('goog.object');
/** @namespace */
const Export = {};
/** @const */
Export.image = {};
/** @const */
Export.map = {};
/** @const */
Export.table = {};
/** @const */
Export.video = {};
/** @const */
Export.classifier = {};
/**
* ExportTask
*/
class ExportTask {
/** @param {!data.AbstractTaskConfig} config */
constructor(config) {
/** @const @private {!data.AbstractTaskConfig} */
this.config_ = config;
/** @export {?string} Task ID, initialized after task starts. */
this.id = null;
}
/**
* Creates a task.
*
* @param {!Object} exportArgs The export task arguments.
* @return {!ExportTask}
* @package
*/
static create(exportArgs) {
// Extract the EE element from the exportArgs.
const eeElement = Export.extractElement(exportArgs);
// Construct a configuration object for the server.
let config = {'element': eeElement};
Object.assign(config, exportArgs);
// The config is some kind of task configuration.
config = /** @type {!data.AbstractTaskConfig} */ (
googObject.filter(config, x => x != null));
return new ExportTask(config);
}
/**
* Starts processing of the task.
*
* @param {function()=} opt_success An optional success callback, to be
* invoked after processing begins. If no success callback is supplied,
* the call is made synchronously and will throw in case of an error.
* @param {function(string=)=} opt_error An optional error callback, invoked
* with an error message if the task fails to start. If no success
* callback is provided, the error callback is ignored.
* @export
*/
start(opt_success, opt_error) {
googAsserts.assert(
this.config_, 'Task config must be specified for tasks to be started.');
this.id = this.id || data.newTaskId(1)[0];
googAsserts.assertString(this.id, 'Failed to obtain task ID.');
// Synchronous task start.
if (!opt_success) {
const response = data.startProcessing(this.id, this.config_);
this.id = response.taskId ?? null;
return;
}
// Asynchronous task start.
data.startProcessing(this.id, this.config_, (response, error) => {
if (error) {
opt_error(error);
} else {
this.id = response.taskId ?? null;
opt_success();
}
});
}
}
////////////////////////////////////////////////////////////////////////////////
// Public API. //
////////////////////////////////////////////////////////////////////////////////
// Public API for exports in the JS client library.
/**
* @param {!Image} image
* @param {string=} opt_description
* @param {string=} opt_assetId
* @param {?Object=} opt_pyramidingPolicy
* @param {number|string=} opt_dimensions
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {number=} opt_scale
* @param {string=} opt_crs
* @param {!Array<number>|string=} opt_crsTransform
* @param {number=} opt_maxPixels
* @param {number=} opt_shardSize
* @param {number=} opt_priority
* @param {boolean=} opt_overwrite
* @return {!ExportTask}
* @export
*/
Export.image.toAsset = function(
image, opt_description, opt_assetId, opt_pyramidingPolicy, opt_dimensions,
opt_region, opt_scale, opt_crs, opt_crsTransform, opt_maxPixels,
opt_shardSize, opt_priority, opt_overwrite) {
const clientConfig =
eeArguments.extractFromFunction(Export.image.toAsset, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.ASSET, data.ExportType.IMAGE);
return ExportTask.create(serverConfig);
};
/**
* @param {!Image} image
* @param {string=} opt_description
* @param {string=} opt_bucket
* @param {string=} opt_fileNamePrefix
* @param {number|string=} opt_dimensions
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {number=} opt_scale
* @param {string=} opt_crs
* @param {!Array<number>|string=} opt_crsTransform
* @param {number=} opt_maxPixels
* @param {number=} opt_shardSize
* @param {number|?Array<number>=} opt_fileDimensions
* @param {boolean=} opt_skipEmptyTiles
* @param {string=} opt_fileFormat
* @param {?data.ImageExportFormatConfig=} opt_formatOptions
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.image.toCloudStorage = function(
image, opt_description, opt_bucket, opt_fileNamePrefix, opt_dimensions,
opt_region, opt_scale, opt_crs, opt_crsTransform, opt_maxPixels,
opt_shardSize, opt_fileDimensions, opt_skipEmptyTiles, opt_fileFormat,
opt_formatOptions, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.image.toCloudStorage, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.GCS, data.ExportType.IMAGE);
return ExportTask.create(serverConfig);
};
/**
* @param {!Image} image
* @param {string=} opt_description
* @param {string=} opt_folder
* @param {string=} opt_fileNamePrefix
* @param {number|string=} opt_dimensions
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {number=} opt_scale
* @param {string=} opt_crs
* @param {!Array<number>|string=} opt_crsTransform
* @param {number=} opt_maxPixels
* @param {number=} opt_shardSize
* @param {number|?Array<number>=} opt_fileDimensions
* @param {boolean=} opt_skipEmptyTiles
* @param {string=} opt_fileFormat
* @param {?data.ImageExportFormatConfig=} opt_formatOptions
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.image.toDrive = function(
image, opt_description, opt_folder, opt_fileNamePrefix, opt_dimensions,
opt_region, opt_scale, opt_crs, opt_crsTransform, opt_maxPixels,
opt_shardSize, opt_fileDimensions, opt_skipEmptyTiles, opt_fileFormat,
opt_formatOptions, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.image.toDrive, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.DRIVE, data.ExportType.IMAGE);
return ExportTask.create(serverConfig);
};
/**
* @param {!Image} image
* @param {string=} opt_description
* @param {string=} opt_bucket
* @param {string=} opt_fileFormat
* @param {string=} opt_path
* @param {boolean=} opt_writePublicTiles
* @param {number=} opt_scale
* @param {number=} opt_maxZoom
* @param {number=} opt_minZoom
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {boolean=} opt_skipEmptyTiles
* @param {string=} opt_mapsApiKey
* @param {?Array<string>=} opt_bucketCorsUris
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.map.toCloudStorage = function(
image, opt_description, opt_bucket, opt_fileFormat, opt_path,
opt_writePublicTiles, opt_scale, opt_maxZoom, opt_minZoom, opt_region,
opt_skipEmptyTiles, opt_mapsApiKey, opt_bucketCorsUris, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.map.toCloudStorage, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.GCS, data.ExportType.MAP);
return ExportTask.create(serverConfig);
};
/**
* @param {!FeatureCollection} collection
* @param {string=} opt_description
* @param {string=} opt_bucket
* @param {string=} opt_fileNamePrefix
* @param {string=} opt_fileFormat
* @param {string|!Array<string>=} opt_selectors
* @param {number=} opt_maxVertices
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.table.toCloudStorage = function(
collection, opt_description, opt_bucket, opt_fileNamePrefix, opt_fileFormat,
opt_selectors, opt_maxVertices, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.table.toCloudStorage, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.GCS, data.ExportType.TABLE);
return ExportTask.create(serverConfig);
};
/**
* @param {!FeatureCollection} collection
* @param {string=} opt_description
* @param {string=} opt_folder
* @param {string=} opt_fileNamePrefix
* @param {string=} opt_fileFormat
* @param {string|!Array<string>=} opt_selectors
* @param {number=} opt_maxVertices
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.table.toDrive = function(
collection, opt_description, opt_folder, opt_fileNamePrefix, opt_fileFormat,
opt_selectors, opt_maxVertices, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.table.toDrive, arguments);
clientConfig['type'] = data.ExportType.TABLE;
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.DRIVE, data.ExportType.TABLE);
return ExportTask.create(serverConfig);
};
/**
* @param {!FeatureCollection} collection
* @param {string=} opt_description
* @param {string=} opt_assetId
* @param {number=} opt_maxVertices
* @param {number=} opt_priority
* @param {boolean=} opt_overwrite
* @return {!ExportTask}
* @export
*/
Export.table.toAsset = function(
collection, opt_description, opt_assetId, opt_maxVertices, opt_priority,
opt_overwrite) {
const clientConfig =
eeArguments.extractFromFunction(Export.table.toAsset, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.ASSET, data.ExportType.TABLE);
return ExportTask.create(serverConfig);
};
/**
* @param {!FeatureCollection} collection
* @param {string=} opt_description
* @param {string=} opt_assetId
* @param {number=} opt_maxFeaturesPerTile
* @param {string=} opt_thinningStrategy
* @param {string|!Array<string>=} opt_thinningRanking
* @param {string|!Array<string>=} opt_zOrderRanking
* @param {number=} opt_minVisibleSizePx
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.table.toFeatureView = function(
collection, opt_description, opt_assetId, opt_maxFeaturesPerTile,
opt_thinningStrategy, opt_thinningRanking, opt_zOrderRanking,
opt_minVisibleSizePx, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.table.toFeatureView, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.FEATURE_VIEW, data.ExportType.TABLE);
return ExportTask.create(serverConfig);
};
/**
* @param {!FeatureCollection} collection
* @param {string=} opt_description
* @param {string=} opt_table
* @param {boolean=} opt_overwrite
* @param {boolean=} opt_append
* @param {string|!Array<string>=} opt_selectors
* @param {number=} opt_maxVertices
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.table.toBigQuery = function(
collection, opt_description, opt_table, opt_overwrite, opt_append,
opt_selectors, opt_maxVertices, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.table.toBigQuery, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.BIGQUERY, data.ExportType.TABLE);
return ExportTask.create(serverConfig);
};
/**
* @param {!ImageCollection} collection
* @param {string=} opt_description
* @param {string=} opt_bucket
* @param {string=} opt_fileNamePrefix
* @param {number=} opt_framesPerSecond
* @param {number|string=} opt_dimensions
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {number=} opt_scale Resolution
* @param {string=} opt_crs
* @param {!Array<number>|string=} opt_crsTransform
* @param {number=} opt_maxPixels
* @param {number=} opt_maxFrames
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.video.toCloudStorage = function(
collection, opt_description, opt_bucket, opt_fileNamePrefix,
opt_framesPerSecond, opt_dimensions, opt_region, opt_scale, opt_crs,
opt_crsTransform, opt_maxPixels, opt_maxFrames, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.video.toCloudStorage, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.GCS, data.ExportType.VIDEO);
return ExportTask.create(serverConfig);
};
/**
* @param {!ImageCollection} collection
* @param {string=} opt_description
* @param {string=} opt_folder
* @param {string=} opt_fileNamePrefix
* @param {number=} opt_framesPerSecond
* @param {number|string=} opt_dimensions
* @param {?Geometry.LinearRing|?Geometry.Polygon|string=} opt_region
* @param {number=} opt_scale
* @param {string=} opt_crs
* @param {!Array<number>|string=} opt_crsTransform
* @param {number=} opt_maxPixels
* @param {number=} opt_maxFrames
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.video.toDrive = function(
collection, opt_description, opt_folder, opt_fileNamePrefix,
opt_framesPerSecond, opt_dimensions, opt_region, opt_scale, opt_crs,
opt_crsTransform, opt_maxPixels, opt_maxFrames, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.video.toDrive, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.DRIVE, data.ExportType.VIDEO);
return ExportTask.create(serverConfig);
};
/**
* @param {!ComputedObject} classifier
* @param {string=} opt_description
* @param {string=} opt_assetId
* @param {number=} opt_priority
* @return {!ExportTask}
* @export
*/
Export.classifier.toAsset = function(
classifier, opt_description, opt_assetId, opt_priority) {
const clientConfig =
eeArguments.extractFromFunction(Export.classifier.toAsset, arguments);
const serverConfig = Export.convertToServerParams(
clientConfig, data.ExportDestination.ASSET, data.ExportType.CLASSIFIER);
return ExportTask.create(serverConfig);
};
////////////////////////////////////////////////////////////////////////////////
// Internal validation. //
////////////////////////////////////////////////////////////////////////////////
/**
* A task descriptor whose parameters have been converted from the user-facing
* syntax to a server-compatible representation. For the user-facing
* equivalent parameters, see the Public API section above.
*
* @typedef {!data.ImageTaskConfig|!data.MapTaskConfig|
* !data.TableTaskConfig|!data.FeatureViewTaskConfig|!data.VideoTaskConfig|
* !data.VideoMapTaskConfig|!data.ClassifierTaskConfig}
*/
const ServerTaskConfig = {};
const REGION_ERROR = 'Invalid format for region property. Region must be ' +
'GeoJSON LinearRing or Polygon specified as actual coordinates or ' +
'serialized as a string. See Export documentation.';
/**
* Serializes a 'region' value. Region may be a Geometry, a GeoJSON string, or a
* GeoJSON object. Only client-side validation is applied; this method does not
* support computed objects.
*
* @param {!Geometry|!Object|string} region
* @return {string}
* #visibleForTesting
*/
Export.serializeRegion = function(region) {
// Convert region to a GeoJSON object.
if (region instanceof Geometry) {
region = region.toGeoJSON();
} else if (typeof region === 'string') {
try {
region = googAsserts.assertObject(JSON.parse(region));
} catch (x) {
throw Error(REGION_ERROR);
}
}
// Ensure locally that the region is a valid LineString or Polygon geometry.
if (!goog.isObject(region) || !('type' in region)) {
try {
new Geometry.LineString(/** @type {?} */ (region));
} catch (e) {
try {
new Geometry.Polygon(/** @type {?} */ (region));
} catch (e2) {
throw Error(REGION_ERROR);
}
}
}
return JSON.stringify(region);
};
/**
* Replaces the 'region' value, if any, with a valid region string. The backend
* verifies the region, but this tries to catch errors early on. Throws errors
* if the region is not valid GeoJSON.
*
* @param {!Object} params The parameters with the region to validate.
* @return {!GoogPromise<!Object>} A promise that resolves to a copy of the
* parameters with a known-to-be valid region.
*/
Export.resolveRegionParam = function(params) {
params = googObject.clone(params);
if (!params['region']) return GoogPromise.resolve(params);
let region = params['region'];
if (region instanceof ComputedObject) {
if (region instanceof Element) {
region = region['geometry']();
}
return new GoogPromise(function(resolve, reject) {
region.getInfo(function(regionInfo, error) {
if (error) {
reject(error);
} else {
if (params['type'] === data.ExportType.IMAGE) {
params['region'] = new Geometry(regionInfo);
} else {
params['region'] = Export.serializeRegion(regionInfo);
}
resolve(params);
}
});
});
}
if (params['type'] === data.ExportType.IMAGE) {
params['region'] = new Geometry(region);
} else {
params['region'] = Export.serializeRegion(region);
}
return GoogPromise.resolve(params);
};
/**
* Extracts the EE element from a given task config.
* @param {!Object} exportArgs
* @return {!Image|!FeatureCollection|!ImageCollection|!Element|!ComputedObject}
*/
Export.extractElement = function(exportArgs) {
// Extract the EE element from the exportArgs.
const isInArgs = (key) => key in exportArgs;
const eeElementKey = Export.EE_ELEMENT_KEYS.find(isInArgs);
// Sanity check that the Image/Collection/Table was provided.
googAsserts.assert(
googArray.count(Export.EE_ELEMENT_KEYS, isInArgs) === 1,
'Expected a single "image", "collection" or "classifier" key.');
const element = exportArgs[eeElementKey];
let result;
if (element instanceof Image) {
result = /** @type {!Image} */ (element);
} else if (element instanceof FeatureCollection) {
result = /** @type {!FeatureCollection} */ (element);
} else if (element instanceof ImageCollection) {
result = /** @type {!ImageCollection} */ (element);
} else if (element instanceof Element) {
result = /** @type {!Element} */ (element);
} else if (element instanceof ComputedObject) {
result = /** @type {!ComputedObject} */ (element);
} else {
throw new Error(
'Unknown element type provided: ' + typeof (element) + '. Expected: ' +
' ee.Image, ee.ImageCollection, ee.FeatureCollection, ee.Element' +
' or ee.ComputedObject.');
}
delete exportArgs[eeElementKey];
return result;
};
/**
* Extracts task arguments into a backend friendly format.
* Sets corresponding destination configuration values to empty strings.
*
* @param {!Object} originalArgs The original arguments to the function.
* @param {!data.ExportDestination} destination Destination of the export.
* @param {!data.ExportType} exportType The type of the export.
* @param {boolean=} serializeRegion enables serializing the region param.
* @return {!ServerTaskConfig} A server-friendly task configuration.
*/
Export.convertToServerParams = function(
originalArgs, destination, exportType, serializeRegion = true) {
let taskConfig =
/** @type {!ServerTaskConfig} */ ({type: exportType});
Object.assign(taskConfig, originalArgs);
switch (exportType) {
case data.ExportType.IMAGE:
taskConfig = Export.image.prepareTaskConfig_(taskConfig, destination);
break;
case data.ExportType.MAP:
taskConfig = Export.map.prepareTaskConfig_(taskConfig, destination);
break;
case data.ExportType.TABLE:
taskConfig = Export.table.prepareTaskConfig_(taskConfig, destination);
break;
case data.ExportType.VIDEO:
taskConfig = Export.video.prepareTaskConfig_(taskConfig, destination);
break;
case data.ExportType.VIDEO_MAP:
taskConfig = Export.videoMap.prepareTaskConfig_(taskConfig, destination);
break;
case data.ExportType.CLASSIFIER:
taskConfig =
Export.classifier.prepareTaskConfig_(taskConfig, destination);
break;
default:
throw Error('Unknown export type: ' + taskConfig['type']);
}
if (serializeRegion && taskConfig['region'] != null) {
taskConfig['region'] = Export.serializeRegion(taskConfig['region']);
}
return /** {!ServerTaskConfig} */ (taskConfig);
};
/**
* Consolidates various options into a standard representation for the
* top-level ServerTaskConfig.
*
* @param {!ServerTaskConfig} taskConfig Task config to prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!ServerTaskConfig}
* @private
*/
Export.prepareDestination_ = function(taskConfig, destination) {
// Convert to deprecated backend keys or fill with empty strings.
switch (destination) {
case data.ExportDestination.GCS:
taskConfig['outputBucket'] = taskConfig['bucket'] || '';
taskConfig['outputPrefix'] =
(taskConfig['fileNamePrefix'] || taskConfig['path'] || '');
delete taskConfig['fileNamePrefix'];
delete taskConfig['path'];
delete taskConfig['bucket'];
break;
case data.ExportDestination.ASSET:
taskConfig['assetId'] = taskConfig['assetId'] || '';
taskConfig['overwrite'] = taskConfig['overwrite'] || false;
break;
case data.ExportDestination.FEATURE_VIEW:
taskConfig['mapName'] = taskConfig['mapName'] || '';
break;
case data.ExportDestination.BIGQUERY:
taskConfig['table'] = taskConfig['table'] || '';
break;
// The default is to drive.
case data.ExportDestination.DRIVE:
default:
// Catch legacy function signature for toDrive calls.
const allowedFolderType = ['string', 'undefined'];
const folderType = goog.typeOf(taskConfig['folder']);
if (!googArray.contains(allowedFolderType, folderType)) {
throw Error(
'Error: toDrive "folder" parameter must be a string, but is ' +
'of type ' + folderType + '.');
}
taskConfig['driveFolder'] = taskConfig['folder'] || '';
taskConfig['driveFileNamePrefix'] = taskConfig['fileNamePrefix'] || '';
delete taskConfig['folder'];
delete taskConfig['fileNamePrefix'];
break;
}
return taskConfig;
};
/**
* Adapts a ServerTaskConfig into a ImageTaskConfig normalizing any parameters.
*
* @param {!ServerTaskConfig} taskConfig Image export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.ImageTaskConfig}
* @private
*/
Export.image.prepareTaskConfig_ = function(taskConfig, destination) {
// Set the file format to GeoTiff if not set.
if (taskConfig['fileFormat'] == null) {
taskConfig['fileFormat'] = 'GeoTIFF';
}
// Handle format-specific options.
taskConfig = Export.reconcileImageFormat(taskConfig);
// Add top-level destination fields.
taskConfig = Export.prepareDestination_(taskConfig, destination);
// Fix the CRS transform key.
if (taskConfig['crsTransform'] != null) {
taskConfig[Export.CRS_TRANSFORM_KEY] = taskConfig['crsTransform'];
delete taskConfig['crsTransform'];
}
return /** @type {!data.ImageTaskConfig} */ (taskConfig);
};
/**
* Adapts a ServerTaskConfig into a TableTaskConfig normalizing any parameters.
*
* @param {!ServerTaskConfig} taskConfig Table export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.TableTaskConfig|!data.FeatureViewTaskConfig|
* !data.BigQueryTaskConfig}
* @private
*/
Export.table.prepareTaskConfig_ = function(taskConfig, destination) {
// Convert array-valued selectors to a comma-separated string.
if (Array.isArray(taskConfig['selectors'])) {
taskConfig['selectors'] = taskConfig['selectors'].join();
}
// Handle format-specific options.
taskConfig = Export.reconcileTableFormat(taskConfig);
// Add top-level destination fields.
taskConfig = Export.prepareDestination_(taskConfig, destination);
return /** @type {!data.TableTaskConfig|!data.FeatureViewTaskConfig|!data.BigQueryTaskConfig}*/ (taskConfig);
};
/**
* Adapts a ServerTaskConfig into a MapTaskConfig normalizing any parameters.
*
* @param {!ServerTaskConfig} taskConfig Map export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.MapTaskConfig}
* @private
*/
Export.map.prepareTaskConfig_ = function(taskConfig, destination) {
taskConfig = Export.prepareDestination_(taskConfig, destination);
// Handle format-specific options.
taskConfig = Export.reconcileMapFormat(taskConfig);
return /** @type {!data.MapTaskConfig} */ (taskConfig);
};
/**
* Adapts a ServerTaskConfig into a VideoTaskConfig normalizing any params.
*
* @param {!ServerTaskConfig} taskConfig Video export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.VideoTaskConfig}
* @private
*/
Export.video.prepareTaskConfig_ = function(taskConfig, destination) {
taskConfig = Export.reconcileVideoFormat_(taskConfig);
taskConfig = Export.prepareDestination_(taskConfig, destination);
if (taskConfig['crsTransform'] != null) {
taskConfig[Export.CRS_TRANSFORM_KEY] = taskConfig['crsTransform'];
delete taskConfig['crsTransform'];
}
return /** @type {!data.VideoTaskConfig} */ (taskConfig);
};
/**
* Adapts a ServerTaskConfig into a VideoMapTaskConfig normalizing any params
* for a video map task.
*
* @param {!ServerTaskConfig} taskConfig VideoMap export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.VideoMapTaskConfig}
* @private
*/
Export.videoMap.prepareTaskConfig_ = function(taskConfig, destination) {
taskConfig = Export.reconcileVideoFormat_(taskConfig);
taskConfig['version'] = taskConfig['version'] || VideoMapVersion.V1;
taskConfig['stride'] = taskConfig['stride'] || 1;
const width = taskConfig['tileWidth'] || 256,
height = taskConfig['tileHeight'] || 256;
taskConfig['tileDimensions'] = {width: width, height: height};
taskConfig = Export.prepareDestination_(taskConfig, destination);
return /** @type {!data.VideoMapTaskConfig} */ (taskConfig);
};
/**
* Adapts a ServerTaskConfig into a ClassifierTaskConfig normalizing any params
* for a classifier task.
*
* @param {!ServerTaskConfig} taskConfig Classifier export config to
* prepare.
* @param {!data.ExportDestination} destination Export destination.
* @return {!data.ClassifierTaskConfig}
* @private
*/
Export.classifier.prepareTaskConfig_ = function(taskConfig, destination) {
taskConfig = Export.prepareDestination_(taskConfig, destination);
return /** @type {!data.ClassifierTaskConfig} */ (taskConfig);
};
/**
* @enum {string} The valid video formats supported by export.
*/
const VideoFormat = {
MP4: 'MP4', // Default.
GIF: 'GIF',
VP9: 'VP9',
};
/**
* @enum {string} The valid map formats supported by export.
*/
const MapFormat = {
AUTO_JPEG_PNG: 'AUTO_JPEG_PNG', // Default.
JPEG: 'JPEG',
PNG: 'PNG',
};
/**
* @enum {string} The valid image formats supported by export.
*/
const ImageFormat = {
GEO_TIFF: 'GEO_TIFF', // Default.
TF_RECORD_IMAGE: 'TF_RECORD_IMAGE',
};
/**
* @enum {string} The valid table formats supported by export.
*/
const TableFormat = {
CSV: 'CSV', // Default.
GEO_JSON: 'GEO_JSON',
KML: 'KML',
KMZ: 'KMZ',
SHP: 'SHP',
TF_RECORD_TABLE: 'TF_RECORD_TABLE',
};
/** @type {!Object<string, !Array<string>>} */
const FORMAT_OPTIONS_MAP = {
'GEO_TIFF': [
'cloudOptimized',
'fileDimensions',
'noData',
'shardSize',
],
'TF_RECORD_IMAGE': [
'patchDimensions',
'kernelSize',
'compressed',
'maxFileSize',
'defaultValue',
'tensorDepths',
'sequenceData',
'collapseBands',
'maskedThreshold',
]
};
/** @type {!Object<string, string>} */
const FORMAT_PREFIX_MAP = {
'GEO_TIFF': 'tiff',
'TF_RECORD_IMAGE': 'tfrecord'
};
/**
* Parses video specific config options.
*
* @param {!ServerTaskConfig} taskConfig
* @return {!ServerTaskConfig} parsedConfig with video options set.
* @private
**/
Export.reconcileVideoFormat_ = function(taskConfig) {
taskConfig['videoOptions'] = taskConfig['framesPerSecond'] || 5.0;
taskConfig['maxFrames'] = taskConfig['maxFrames'] || 1000;
taskConfig['maxPixels'] = taskConfig['maxPixels'] || 1e8;
// Parse the video file format from the given task config.
let formatString = taskConfig['fileFormat'];
// If not specified assume the format is MP4.
if (formatString == null) {
formatString = VideoFormat.MP4;
}
formatString = formatString.toUpperCase();
switch (formatString) {
case 'MP4':
formatString = VideoFormat.MP4;
break;
case 'GIF':
case 'JIF':
formatString = VideoFormat.GIF;
break;
case 'VP9':
case 'WEBM':
formatString = VideoFormat.VP9;
break;
default:
throw new Error(
`Invalid file format ${formatString}. ` +
`Supported formats are: 'MP4', 'GIF', and 'WEBM'.`);
}
taskConfig['fileFormat'] = formatString;
return taskConfig;
};
/**
* Validates any format specific options, and converts said options to a
* backend friendly format.
* @param {!ServerTaskConfig} taskConfig Arguments
* passed to an image export "toDrive" or "toCloudStorage" request.
* @return {!ServerTaskConfig}
*/
Export.reconcileImageFormat = function(taskConfig) {
// Parse the image file format from the given task config.
let formatString = taskConfig['fileFormat'];
// If not specified assume the format is geotiff.
if (formatString == null) {
formatString = ImageFormat.GEO_TIFF;
}
formatString = formatString.toUpperCase();
switch (formatString) {
case 'TIFF':
case 'TIF':
case 'GEO_TIFF':
case 'GEOTIFF':
formatString = ImageFormat.GEO_TIFF;
break;
case 'TF_RECORD':
case 'TF_RECORD_IMAGE':
case 'TFRECORD':
formatString = ImageFormat.TF_RECORD_IMAGE;
break;
default:
throw new Error(
`Invalid file format ${formatString}. ` +
`Supported formats are: 'GEOTIFF', 'TFRECORD'.`);
}
taskConfig['fileFormat'] = formatString;
if (taskConfig['formatOptions'] != null) {
// Add the prefix to the format-specific options.
const formatOptions =
Export.prefixImageFormatOptions_(taskConfig, formatString);
delete taskConfig['formatOptions'];
// Assign the format options into the top-level request.
Object.assign(taskConfig, formatOptions);
}
return taskConfig;
};
/**
* Validates any format specific options, and converts said options to a
* backend friendly format.
* @param {!ServerTaskConfig} taskConfig Arguments
* passed to a map export "toCloudStorage" request.
* @return {!ServerTaskConfig}
*/
Export.reconcileMapFormat = function(taskConfig) {
// Parse the image file format from the given task config.
let formatString = taskConfig['fileFormat'];
// If not specified assume the format is auto.
if (formatString == null) {
formatString = MapFormat.AUTO_JPEG_PNG;
}
formatString = formatString.toUpperCase();
switch (formatString) {
case 'AUTO':
case 'AUTO_JPEG_PNG':
case 'AUTO_JPG_PNG':
formatString = MapFormat.AUTO_JPEG_PNG;
break;
case 'JPG':
case 'JPEG':
formatString = MapFormat.JPEG;
break;
case 'PNG':
formatString = MapFormat.PNG;
break;
default:
throw new Error(
`Invalid file format ${formatString}. ` +
`Supported formats are: 'AUTO', 'PNG', and 'JPEG'.`);
}
taskConfig['fileFormat'] = formatString;
return taskConfig;
};
/**
* Validates any format specific options, and converts said options to a
* backend friendly format.
* @param {!ServerTaskConfig} taskConfig Arguments
* passed to a table export "toDrive" or "toCloudStorage" request.
* @return {!ServerTaskConfig}
*/
Export.reconcileTableFormat = function(taskConfig) {
// Parse the table file format from the given task config.
let formatString = taskConfig['fileFormat'];
// If not specified assume the format is CSV.
if (formatString == null) {
formatString = TableFormat.CSV;
}
formatString = formatString.toUpperCase();
switch (formatString) {
case 'CSV':
formatString = TableFormat.CSV;
break;
case 'JSON':
case 'GEOJSON':
case 'GEO_JSON':
formatString = TableFormat.GEO_JSON;
break;
case 'KML':