-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathupdate-previews.js
43 lines (38 loc) · 1.72 KB
/
update-previews.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
import axios from 'axios';
import config from './src/store/config';
const fs = require('fs');
async function updatePreviews(previewsDir, previewsIndexFile) {
// remove old previews:
for (let fileName of fs.readdirSync(previewsDir)) {
fs.unlinkSync(`${previewsDir}/${fileName}`);
}
// fetch new previews:
const datasources = config.datasources;
let previews = [];
for (let ds of datasources) {
const [servicesRootUrl, instanceId] = ds.url.split('/ogc/wms/');
const shortInstanceId = instanceId.substring(0, 8);
const getCapabilitiesUrl = `${servicesRootUrl}/ogc/wms/${instanceId}?request=GetCapabilities&format=application%2Fjson`;
const capabilities = await axios.get(getCapabilitiesUrl);
for (let layer of capabilities.data.layers) {
const bbox = '15,45.95347718,15.03818374,45.98047579';
const crs = 'CRS:84';
const previewUrl = `${servicesRootUrl}/ogc/wms/${instanceId}?showLogo=false&SERVICE=WMS&REQUEST=GetMap&LAYERS=${
layer.id
}&BBOX=${bbox}&CRS=${crs}&MAXCC=100&WIDTH=50&HEIGHT=50&gain=1&FORMAT=image/jpeg&bgcolor=00000000&transparent=1&TIME=2019-01-01/2019-07-01`;
const fileName = `${shortInstanceId}-${layer.id}.jpeg`;
const fullFileName = `${previewsDir}/${fileName}`;
const thumbnail = await axios.get(previewUrl, { responseType: 'arraybuffer' });
fs.writeFileSync(fullFileName, thumbnail.data);
previews.push(fileName);
}
}
// write an index file so we know (in Playground app) which files exist:
fs.writeFileSync(previewsIndexFile, JSON.stringify(previews, null, 2));
}
updatePreviews('./public/previews', './src/previews.json')
.then(() => console.log('DONE.'))
.catch(err => {
console.error(err);
process.exit(1);
});