Created
December 10, 2017 02:57
-
-
Save vincentsarago/6d720c68c2357e744b10cd053a6249b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const moment = require('moment'); | |
const AWS = require('aws-sdk'); | |
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 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 {}; | |
}); | |
}; | |
//Bucket: sentinel-s2-l1c | |
//Key: tiles/38/S/NG/2017/10/9/1/ | |
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()); | |
// 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`}; | |
info.scene_id = `S2A_tile_${info.date}_${info.utm_zone}${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}_${info.utm_zone}${info.latitude_band}${info.grid_square}_${info.num}`; | |
return info; | |
}); | |
} else { | |
return info; | |
} | |
})); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment