This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
283 lines (244 loc) · 8.47 KB
/
utils.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
'use strict';
const moment = require('moment');
const AWS = require('aws-sdk');
const zeroPad = (n, c) => {
let s = String(n);
if (s.length < c) s = zeroPad('0' + n, c);
return s;
};
const generate_year_range = (start, end) => {
const years = [];
for(var year = start; year <= end; year++){
years.push(year);
}
return years;
};
const aws_list_directory = (bucket, prefix, s3) => {
const params = {
Bucket: bucket,
Delimiter: '/',
Prefix: prefix};
return s3.listObjectsV2(params).promise()
.then(data => {
return data.CommonPrefixes.map(e => {
return e.Prefix;
});
})
.catch(() => {
return [];
});
};
const parseSceneid_c1 = (sceneid) => {
const sceneid_info = sceneid.split('_');
return {
scene_id: sceneid,
satellite: sceneid_info[0].slice(0,1) + sceneid_info[0].slice(3),
sensor: sceneid_info[0].slice(1,2),
correction_level: sceneid_info[1],
path: sceneid_info[2].slice(0,3),
row: sceneid_info[2].slice(3),
acquisition_date: sceneid_info[3],
ingestion_date: sceneid_info[4],
collection: sceneid_info[5],
category: sceneid_info[6]
};
};
const parseSceneid_pre = (sceneid) => {
return {
scene_id: sceneid,
satellite: sceneid.slice(2,3),
sensor: sceneid.slice(1,2),
path: sceneid.slice(3,6),
row: sceneid.slice(6,9),
acquisitionYear: sceneid.slice(9,13),
acquisitionJulianDay: sceneid.slice(13,16),
acquisition_date: moment().utc().year(sceneid.slice(9,13)).dayOfYear(sceneid.slice(13,16)).format('YYYYMMDD'),
groundStationIdentifier: sceneid.slice(16,19),
archiveVersion: sceneid.slice(19,21)
};
};
const parseCBERSid = (sceneid) => {
return {
scene_id: sceneid,
satellite: sceneid.split('_')[0],
version: sceneid.split('_')[1],
sensor: sceneid.split('_')[2],
path: sceneid.split('_')[4],
row: sceneid.split('_')[5],
acquisition_date: sceneid.split('_')[3],
processing_level: sceneid.split('_')[6]
};
};
const get_l8_info = (bucket, key, s3) => {
const params = {
Bucket: bucket,
Key: key};
return s3.getObject(params).promise()
.then(data => {
data = JSON.parse(data.Body.toString());
let metadata = data.L1_METADATA_FILE;
return {
cloud_coverage: metadata.IMAGE_ATTRIBUTES.CLOUD_COVER,
cloud_coverage_land: metadata.IMAGE_ATTRIBUTES.CLOUD_COVER_LAND,
sun_azimuth: metadata.IMAGE_ATTRIBUTES.SUN_AZIMUTH,
sun_elevation: metadata.IMAGE_ATTRIBUTES.SUN_ELEVATION,
geometry: {
type: 'Polygon',
coordinates: [[
[metadata.PRODUCT_METADATA.CORNER_UR_LON_PRODUCT, metadata.PRODUCT_METADATA.CORNER_UR_LAT_PRODUCT],
[metadata.PRODUCT_METADATA.CORNER_UL_LON_PRODUCT, metadata.PRODUCT_METADATA.CORNER_UL_LAT_PRODUCT],
[metadata.PRODUCT_METADATA.CORNER_LL_LON_PRODUCT, metadata.PRODUCT_METADATA.CORNER_LL_LAT_PRODUCT],
[metadata.PRODUCT_METADATA.CORNER_LR_LON_PRODUCT, metadata.PRODUCT_METADATA.CORNER_LR_LAT_PRODUCT],
[metadata.PRODUCT_METADATA.CORNER_UR_LON_PRODUCT, metadata.PRODUCT_METADATA.CORNER_UR_LAT_PRODUCT]
]]
}};
})
.catch(() => {
return {};
});
};
const get_s2_info = (bucket, key, s3) => {
const params = {
Bucket: bucket,
Key: key};
return s3.getObject(params).promise()
.then(data => {
data = JSON.parse(data.Body.toString());
return {
cloud_coverage: data.cloudyPixelPercentage,
coverage: data.dataCoveragePercentage,
geometry : data.tileGeometry,
sat: data.productName.slice(0,3)};
})
.catch(() => {
return {};
});
};
const get_landsat = (path, row, full=false) => {
const s3 = new AWS.S3({region: 'us-west-2'});
const landsat_bucket = 'landsat-pds';
row = utils.zeroPad(row, 3);
path = utils.zeroPad(path, 3);
const level = ['L8', 'c1/L8'];
// get list sceneid
return Promise.all(level.map(e => {
let prefix = `${e}/${path}/${row}/`;
return utils.aws_list_directory(landsat_bucket, prefix, s3);
}))
.then(dirs => {
dirs = [].concat.apply([], dirs);
return Promise.all(dirs.map(e => {
let landsat_id = e.split('/').slice(-2,-1)[0];
let info, aws_url;
if (/L[COTEM]08_L\d{1}[A-Z]{2}_\d{6}_\d{8}_\d{8}_\d{2}_(T1|RT)/.exec(landsat_id)) {
info = utils.parseSceneid_c1(landsat_id);
info.type = info.category;
aws_url = 'https://landsat-pds.s3.amazonaws.com/c1';
} else {
info = utils.parseSceneid_pre(landsat_id);
info.type = 'pre';
aws_url = 'https://landsat-pds.s3.amazonaws.com';
}
info.browseURL = `${aws_url}/L8/${info.path}/${info.row}/${info.scene_id}/${info.scene_id}_thumb_large.jpg`;
info.thumbURL = `${aws_url}/L8/${info.path}/${info.row}/${info.scene_id}/${info.scene_id}_thumb_small.jpg`;
if (full) {
let json_path = `${e}${landsat_id}_MTL.json`;
return utils.get_l8_info(landsat_bucket, json_path, s3)
.then(data => {
return Object.assign({}, info, data);
});
} else {
return info;
}
}));
});
};
const get_cbers = (path, row) => {
const s3 = new AWS.S3({region: 'us-east-1'});
const cbers_bucket = 'cbers-meta-pds';
row = utils.zeroPad(row, 3);
path = utils.zeroPad(path, 3);
// get list sceneid
const prefix = `CBERS4/MUX/${path}/${row}/`;
return utils.aws_list_directory(cbers_bucket, prefix, s3)
.then(dirs => {
dirs = [].concat.apply([], dirs);
return dirs.map(e => {
let cbers_id = e.split('/').slice(-2,-1)[0];
let info = utils.parseCBERSid(cbers_id);
let preview_id = cbers_id.split('_').slice(0,-1).join('_');
info.browseURL = `https://${cbers_bucket}.s3.amazonaws.com/CBERS4/MUX/${path}/${row}/${cbers_id}/${preview_id}_small.jpeg`;
return info;
});
});
};
const get_sentinel = (utm, lat, grid, full=false) => {
const s3 = new AWS.S3({region: 'eu-central-1'});
const sentinel_bucket = 'sentinel-s2-l1c';
const img_year = utils.generate_year_range(2015, moment().year());
utm = utm.replace(/^0/, '');
// get list of month
return Promise.all(img_year.map(e => {
let prefix = `tiles/${utm}/${lat}/${grid}/${e}/`;
return utils.aws_list_directory(sentinel_bucket, prefix, s3);
}))
.then(dirs => {
// get list of day
dirs = [].concat.apply([], dirs);
return Promise.all(dirs.map(e => {
return utils.aws_list_directory(sentinel_bucket, e, s3);
}));
})
.then(dirs => {
// get list of version
dirs = [].concat.apply([], dirs);
return Promise.all(dirs.map(e => {
return utils.aws_list_directory(sentinel_bucket, e, s3);
}));
})
.then(data => {
//create list of image
data = [].concat.apply([], data);
return Promise.all(data.map(e => {
let s2path = e.replace(/\/$/, '');
let yeah = s2path.split('/')[4];
let month = utils.zeroPad(s2path.split('/')[5], 2);
let day = utils.zeroPad(s2path.split('/')[6], 2);
let info = {
path: s2path,
utm_zone: s2path.split('/')[1],
latitude_band: s2path.split('/')[2],
grid_square: s2path.split('/')[3],
num: s2path.split('/')[7],
acquisition_date: `${yeah}${month}${day}`,
browseURL: `https://sentinel-s2-l1c.s3.amazonaws.com/${s2path}/preview.jpg`};
const utm = utils.zeroPad(info.utm_zone, 2);
info.scene_id = `S2A_tile_${info.acquisition_date}_${utm}${info.latitude_band}${info.grid_square}_${info.num}`;
if (full) {
let json_path = `${e}tileInfo.json`;
return utils.get_s2_info(sentinel_bucket, json_path, s3)
.then(data => {
info = Object.assign({}, info, data);
info.scene_id = `${info.sat}_tile_${info.acquisition_date}_${utm}${info.latitude_band}${info.grid_square}_${info.num}`;
return info;
});
} else {
return info;
}
}));
});
};
const utils = {
generate_year_range: generate_year_range,
aws_list_directory: aws_list_directory,
parseSceneid_pre: parseSceneid_pre,
parseSceneid_c1: parseSceneid_c1,
parseCBERSid: parseCBERSid,
get_sentinel: get_sentinel,
get_landsat: get_landsat,
get_cbers: get_cbers,
get_s2_info: get_s2_info,
get_l8_info: get_l8_info,
zeroPad: zeroPad
};
module.exports = utils;