-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
540 lines (507 loc) · 15.3 KB
/
index.js
File metadata and controls
540 lines (507 loc) · 15.3 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
const url = require('url');
const got = require('got');
const PNG = require('pngjs').PNG;
const area = require('@turf/area').default;
const intersect = require('@turf/intersect').default;
const {
featureCollection,
polygon,
multiPolygon,
round,
} = require('@turf/helpers');
const rewind = require('geojson-rewind');
const { union } = require('polygon-clipping');
const rgbHex = require('rgb-hex');
const { Feed } = require('feed');
const boundaryFeature = require('./sg-region-boundary.json');
// Rain area center and boundaries
const lowerLat = 1.156,
upperLat = 1.475,
lowerLong = 103.565,
upperLong = 104.13;
const distanceLat = Math.abs(upperLat - lowerLat);
const distanceLong = Math.abs(upperLong - lowerLong);
// Color scales
const intensityColors = [
'#40FFFD',
'#3BEEEC',
'#32D0D2',
'#2CB9BD',
'#229698',
'#1C827D',
'#1B8742',
'#229F44',
'#27B240',
'#2CC53B',
'#30D43E',
'#38EF46',
'#3BFB49',
'#59FA61',
'#FEFB63',
'#FDFA53',
'#FDEB50',
'#FDD74A',
'#FCC344',
'#FAB03F',
'#FAA23D',
'#FB8938',
'#FB7133',
'#F94C2D',
'#F9282A',
'#DD1423',
'#BE0F1D',
'#B21867',
'#D028A6',
'#F93DF5',
];
const intensityColorsCount = intensityColors.length;
const nearestColor = require('nearest-color').from(intensityColors);
const getIntensity = (color) => {
const c = nearestColor(color);
const index = intensityColors.indexOf(c);
return Math.ceil(((index + 1) / intensityColorsCount) * 100);
};
const offset = 8; // Singapore timezone +0800
function datetimeNowStr(customMinutes) {
// https://stackoverflow.com/a/11124448/20838
const d = new Date(new Date().getTime() + offset * 3600 * 1000);
if (customMinutes) d.setUTCMinutes(d.getUTCMinutes() + customMinutes);
const year = d.getUTCFullYear();
const month = ('' + (d.getUTCMonth() + 1)).padStart(2, '0');
const day = ('' + d.getUTCDate()).padStart(2, '0');
const hour = ('' + d.getUTCHours()).padStart(2, '0');
const min = ('' + d.getUTCMinutes()).padStart(2, '0');
return parseInt(year + month + day + hour + min, 10);
}
function datetimeStr(customMinutes) {
const d = datetimeNowStr(customMinutes);
return Math.floor(d / 5) * 5;
}
function getDateFromStr(str) {
const dateNums = `${str}`
.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/)
.slice(1)
.map(Number);
const [year, month, ...rest] = dateNums;
return new Date(year, month - 1, ...rest);
}
let coverage = 0;
let sgCoverage = 0;
function convertPNG2GeoJSON(png, id) {
const { width, height, data } = png;
const polygons = [];
const polygonsByColor = {};
let colorsCount = 0;
const dataCache = { id, data: [] };
for (let y = 0; y < height; y++) {
dataCache.data.push([]);
for (let x = 0; x < width; x++) {
const idx = (width * y + x) << 2;
const r = data[idx];
const g = data[idx + 1];
const b = data[idx + 2];
const alpha = data[idx + 3];
const hasColor = alpha > 0;
if (hasColor) {
const lLong = round(lowerLong + (x / width) * distanceLong, 4);
const uLong = round(lowerLong + ((x + 1) / width) * distanceLong, 4);
const lLat = round(upperLat - (y / height) * distanceLat, 4);
const uLat = round(upperLat - ((y + 1) / height) * distanceLat, 4);
const key = `${r},${g},${b}`;
// const p = polygon([[
// [lLong, uLat],
// [uLong, uLat],
// [uLong, lLat],
// [lLong, lLat],
// [lLong, uLat]
// ]], {
// color,
// intensity,
// });
const p = [
[
[lLong, uLat],
[uLong, uLat],
[uLong, lLat],
[lLong, lLat],
[lLong, uLat],
],
];
if (!polygonsByColor[key]) polygonsByColor[key] = [];
polygonsByColor[key].push(p);
// polygons.push(p);
colorsCount++;
}
dataCache.data[y].push(hasColor ? getIntensity({ r, g, b }) : 0);
}
}
coverage = (colorsCount / (width * height)) * 100;
for (const key in polygonsByColor) {
const allP = polygonsByColor[key];
const unionP = union(...allP);
const [r, g, b] = key.split(',').map(Number);
const mp = multiPolygon(unionP, {
color: '#' + rgbHex(r, g, b),
intensity: getIntensity({ r, g, b }),
});
polygons.push(mp);
}
const fc = rewind(
featureCollection(polygons, {
bbox: [lowerLong, lowerLat, upperLong, upperLat],
id,
}),
);
const oneWholePolygon = multiPolygon(
polygons.length
? union(...polygons.map((p) => p.geometry.coordinates))
: [],
);
let boundaryArea = 0,
sgArea = 0;
boundaryFeature.features.forEach((feature) => {
boundaryArea += area(feature);
const intersectArea = intersect(feature, oneWholePolygon);
if (intersectArea) sgArea += area(intersectArea);
// const { name } = feature.properties;
// if (intersectArea) {
// console.log(name, area(intersectArea)/area(feature)*100, '%');
// } else {
// console.log(name, 'NOT INTERSECTING');
// }
});
sgCoverage = (sgArea / boundaryArea) * 100;
console.log(`Coverage: ${sgCoverage.toFixed(2)}% / ${coverage.toFixed(2)}%`);
return [fc, dataCache];
}
let prevURL = '';
const fetchImage = (dt) =>
new Promise((resolve, reject) => {
const url = `http://www.weather.gov.sg/files/rainarea/50km/v2/dpsri_70km_${dt}0000dBR.dpsri.png`;
console.log(url !== prevURL ? `➡️ ${url}` : '♻️');
prevURL = url;
let imgReq;
got
.stream(url, {
responseType: 'buffer',
headers: { 'user-agent': undefined },
})
.on('error', (e) => {
if (e.statusCode == 404) {
reject(new Error('Page not found'));
} else {
console.error(e.message || e);
reject(e);
}
})
.on('request', (req) => (imgReq = req))
.on('response', (msg) => {
if (msg.headers['content-type'] !== 'image/png') {
imgReq && imgReq.abort();
const e = new Error('Radar image is not a PNG image.');
console.error(e);
reject(e);
}
})
.pipe(
new PNG({
checkCRC: false,
}),
)
.on('error', (e) => {
console.error(e);
reject(e);
})
.on('parsed', function () {
resolve(this);
});
});
// function fetchImage(dt){
// const url = `http://www.weather.gov.sg/files/rainarea/50km/v2/dpsri_70km_${dt}0000dBR.dpsri.png`;
// console.log(`➡️ ${url}`);
// return got(url, { encoding: null });
// }
const grabGeoJSON = async (dt) => {
if (dt === cachedDt) return geoJSONCache;
const img = await fetchImage(dt);
const [geojson, data] = convertPNG2GeoJSON(img, dt);
const geojsonStr = JSON.stringify(geojson);
const dataStr = JSON.stringify(data);
console.log('GeoJSON generated', dt);
return [geojsonStr, dataStr];
};
let cachedDt;
let geoJSONCache = '';
let dataCache = {};
const getGeoJSON = async () => {
let dt = datetimeStr();
console.log('❇️ Get GeoJSON', dt);
if (dt === cachedDt) return [geoJSONCache, dataCache];
let img;
try {
img = await fetchImage(dt);
} catch (e) {
// dt = datetimeStr(-5);
// console.log('Retrying with older radar image', dt);
// // If older radar image is already cached, return immediately
// if (dt === cachedDt) return [geoJSONCache, dataCache];
// try {
// img = await fetchImage(dt);
// } catch (e) {
// return [geoJSONCache, dataCache];
// }
for (let i = 1; i <= 5; i++) {
dt = datetimeStr(i * -5);
console.log('Retrying with older radar image', dt);
if (dt === cachedDt) return [geoJSONCache, dataCache];
try {
img = await fetchImage(dt);
break;
} catch (e) {}
}
if (!img) return [geoJSONCache, dataCache];
}
cachedDt = dt;
const [geojson, data] = convertPNG2GeoJSON(img, cachedDt);
geoJSONCache = JSON.stringify(geojson);
dataCache = data;
console.log('GeoJSON cached', dt);
return [geoJSONCache, dataCache];
};
let geoJSONPromise = getGeoJSON();
const geojsonInt = setInterval(() => {
geoJSONPromise = getGeoJSON();
}, 30 * 1000); // every half minute
process.on('SIGINT', () => clearInterval(geojsonInt));
// const stations = {};
// (async () => {
// const stationsURL =
// 'http://www.weather.gov.sg/mobile/json/rest-get-all-climate-stations.json';
// const { body } = await got(stationsURL, {
// responseType: 'json',
// headers: { 'user-agent': undefined },
// });
// body.data.forEach((d) => {
// stations[d.id] = d;
// });
// })();
// const observationsCache = new Map();
// let lastObservations = {};
// const dataURL =
// 'http://www.weather.gov.sg/mobile/json/rest-get-latest-observation-for-all-locs.json';
const formatAscii = ({ id, data }) => {
return (
id +
'\n' +
data
.map((y) => {
let text = '';
y.forEach((x) => {
text += x ? String.fromCharCode(x + 33) : ' ';
});
return text.trimEnd();
})
.join('\n')
);
};
let feedCache;
module.exports = async (req, res) => {
const { pathname, query } = url.parse(req.url, true);
const ageDiff = datetimeNowStr() - cachedDt;
const proxyMaxAge = Math.max(0, 5 - ageDiff) * 60;
const { json, ascii } = query;
switch (pathname) {
case '/':
const memoryUsage = process.memoryUsage();
const used = memoryUsage.heapUsed / 1024 / 1024;
res.setHeader('content-type', 'application/json');
res.end(
JSON.stringify({
repo: 'https://github.com/cheeaun/rain-geojson-sg',
author: 'Lim Chee Aun',
data: {
datetime: cachedDt,
},
process: {
version: process.versions,
memoryUsageReadable: `${Math.round(used * 100) / 100} MB`,
memoryUsage,
},
}),
);
break;
case '/favicon.ico':
res.setHeader('content-type', 'image/x-icon');
res.end();
break;
case '/rainarea': {
const { datetime } = query;
if (/\d{11}[05]/.test(datetime)) {
try {
const [geojson, data] = await grabGeoJSON(datetime);
const response = !ascii
? !!json
? JSON.stringify(data)
: geojson
: formatAscii(data);
res.setHeader(
'content-type',
!ascii ? 'application/json' : 'text/plain',
);
res.setHeader('content-length', response.length);
res.setHeader('cache-control', 'public, max-age=31536000'); // 1 year
res.end(response);
} catch (e) {
res.statusCode = 404;
res.end('Radar image not found.');
}
} else {
res.statusCode = 400;
res.end(
'Invalid request. `datetime` query is required as a 12-digit YYYYMMDDHHMM string. Last MM is in 5-minute intervals. Timezone in SGT.',
);
}
break;
}
case '/now': {
const [geojson, data] = await geoJSONPromise;
const response = !ascii
? !!json
? JSON.stringify(data)
: geojson
: formatAscii(data);
res.setHeader('content-type', !ascii ? 'application/json' : 'text/plain');
res.setHeader('content-length', response.length);
res.setHeader(
'cache-control',
`public, max-age=60, s-maxage=${proxyMaxAge}`,
);
res.end(response);
break;
}
case '/now-id':
await geoJSONPromise;
res.setHeader('content-type', 'text/plain');
if (cachedDt) {
res.setHeader(
'cache-control',
`public, max-age=60, s-maxage=${proxyMaxAge}`,
);
res.end('' + cachedDt);
} else {
res.setHeader('cache-control', `public, no-cache`);
res.end('');
}
break;
case '/observations':
res.end(
JSON.stringify({
message: 'Please use /v2/observations',
}),
);
// const compact = !!query.compact;
// res.setHeader('content-type', 'application/json');
// res.setHeader('cache-control', 'public, max-age=60');
// try {
// const { body, fromCache } = await got(dataURL, {
// responseType: 'json',
// cache: observationsCache,
// headers: { 'user-agent': undefined },
// });
// if (!fromCache || !lastObservations[compact]) {
// const features = [];
// Object.entries(body.data.station).forEach(([id, values]) => {
// const { name, lat, long } = stations[id];
// const props = {};
// for (let k in values) {
// const v = values[k];
// const nv = isNaN(v) ? v : Number(v);
// if (
// !compact ||
// (compact && /rain|temp|humidity|wind_direction/i.test(k) && nv)
// ) {
// props[k] = nv;
// }
// }
// if (!compact) {
// features.push({
// type: 'Feature',
// properties: {
// id,
// name,
// ...props,
// },
// geometry: {
// type: 'Point',
// coordinates: [long, lat].map(Number),
// },
// });
// } else if (compact && Object.keys(props).length) {
// features.push({
// type: 'Feature',
// properties: props,
// geometry: {
// type: 'Point',
// coordinates: [long, lat].map(Number),
// },
// });
// }
// });
// lastObservations[compact] = JSON.stringify({
// type: 'FeatureCollection',
// features,
// });
// }
// } catch (e) {}
// res.end(lastObservations[compact] || '');
break;
case '/coverage':
await geoJSONPromise;
res.setHeader('content-type', 'application/json');
res.setHeader('cache-control', 'public, max-age=900, s-maxage=900');
res.end(
JSON.stringify({
id: cachedDt,
coverage,
sgCoverage,
}),
);
break;
case '/feed':
await geoJSONPromise;
console.log(`Feed request from: ${req.headers['user-agent']}`);
if (!feedCache) {
const date = getDateFromStr(cachedDt);
const feed = new Feed({
title: 'Rain GeoJSON SG',
id: 'rain-geojson-sg',
updated: date,
generator: `Rain feed - ${cachedDt} - ${sgCoverage}`,
});
if (sgCoverage > 5 && cachedDt) {
feed.addItem({
title: `${'🌧'.repeat(
Math.ceil(coverage / 20),
)} Rain coverage: ${coverage.toFixed(2)}%`,
description: `Rain coverage over Singapore: ${sgCoverage.toFixed(
2,
)}%`,
id: cachedDt,
link: 'https://checkweather.sg',
date,
});
}
feedCache = feed.atom1();
setTimeout(() => {
feedCache = null;
}, 14 * 60 * 1000); // 14 minutes
}
res.setHeader('content-type', 'application/atom+xml');
res.setHeader('cache-control', 'public, max-age=900, s-maxage=900');
res.end(feedCache);
break;
default:
res.statusCode = 404;
res.end('404.');
}
};