-
Notifications
You must be signed in to change notification settings - Fork 110
/
batch
135 lines (109 loc) · 3.43 KB
/
batch
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
/*
* Author: Rodrigo E. Principe
* License: Apache 2.0
* email: fitoprincipe82@gmail.com
Some batch useful tools
*/
var tools = require('users/fitoprincipe/geetools:tools')
// TASK CLASS
var Task = function(taskId, config) {
this.id = taskId
this.config = config
}
Task.prototype.start = function() {
ee.data.startProcessing(this.id, this.config)
}
var IMAGE_TYPES = function(img, type) {
var types = { "float":img.toFloat(),
"byte":img.toByte(),
"int":img.toInt(),
"double":img.toDouble(),
"long": img.toLong(),
"short": img.toShort(),
"int8": img.toInt8(),
"int16": img.toInt16(),
"int32": img.toInt32(),
"int64": img.toInt64(),
"uint8": img.toUint8(),
"uint16": img.toUint16(),
"uint32": img.toUint32()}
return types[type]
}
var Download = {'ImageCollection': {}, 'Table': {}}
Download.ImageCollection.toAsset = function(collection, assetFolder, options) {
var root = ee.data.getAssetRoots()[0]['id']
var folder = assetFolder
if (folder !== null && folder !== undefined) {
var assetFolder = root+'/'+folder+'/'
} else {
var assetFolder = root+'/'
}
var defaults = {
name: null,
scale: 1000,
maxPixels: 1e13,
region: null,
}
var opt = tools.get_options(defaults, options)
var n = collection.size().getInfo();
var colList = collection.toList(n);
var colID = opt.name || collection.getInfo()['id'] || "ImageCollection"
colID = colID.replace('/','_')
for (var i = 0; i < n; i++) {
var img = ee.Image(colList.get(i));
var id = img.id().getInfo() || 'image_'+i.toString();
var region = opt.region || img.geometry().bounds().getInfo()["coordinates"];
Export.image.toAsset({
image: img,
description: id,
assetId: assetFolder+colID+'/'+id,
region: region,
scale: opt.scale,
maxPixels: opt.maxPixels})
}
}
Download.ImageCollection.toDrive = function(collection, folder, options) {
var defaults = {
scale: 1000,
maxPixels: 1e13,
type: 'float',
region: null,
name: null,
}
var opt = tools.get_options(defaults, options)
var n = collection.size().getInfo();
var colList = collection.toList(n);
var colID = opt.name || collection.getInfo()['id'] || ""
colID = colID.replace('/','_')
for (var i = 0; i < n; i++) {
var img = ee.Image(colList.get(i));
var id = img.id().getInfo() || colID+'_image_'+i.toString();
var region = opt.region || img.geometry().bounds().getInfo()["coordinates"];
var imtype = IMAGE_TYPES(img, opt.type)
Export.image.toDrive({
image: imtype,
description: id,
folder: folder,
fileNamePrefix: id,
region: region,
scale: opt.scale,
maxPixels: opt.maxPixels})
}
}
Download.Table.toAsset = function(collection, options) {
var def = {
description: 'myExportTableTask',
assetId: null
}
var opt = tools.get_options(def, options)
var full_config = {
'type': 'EXPORT_FEATURES',
'json': collection.serialize(),
'description': opt.description,
'state': 'UNSUBMITTED',
}
full_config.assetId = opt.assetId
var task = new Task(ee.data.newTaskId()[0], full_config)
return task
}
exports.Download = Download