-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetNearest.js
29 lines (26 loc) · 987 Bytes
/
getNearest.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
import { getDistance } from "https://js.sabae.cc/getDistance.js";
import { Day } from "https://code4fukui.github.io/day-es/Day.js";
import { CSV } from "https://js.sabae.cc/CSV.js";
import { Geo3x3 } from "https://geo3x3.com/Geo3x3.js";
let sensors;
const getNearest = async (lat, lng) => {
if (!sensors) {
sensors = CSV.toJSON(await CSV.fetch("https://code4fukui.github.io/waterlevel_fukui/sensors.csv"));
}
sensors.forEach(d => {
const p = Geo3x3.decode(d.geo3x3);
d.distance = getDistance(lat, lng, p.lat, p.lng);
});
sensors.sort((a, b) => a.distance - b.distance);
//console.log(sensors);
for (const sensor of sensors) {
const data = CSV.toJSON(await CSV.fetch("https://code4fukui.github.io/waterlevel_fukui/data/" + new Day().toString() + ".csv")).reverse();
const d = data.find(d => d.id == sensor.id);
const res = {};
Object.assign(res, sensor);
Object.assign(res, d);
return res;
}
return null;
};
export { getNearest };