-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (60 loc) · 1.86 KB
/
index.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
import createWalk from "hafas-discover-stations";
import createClient from "hafas-client";
import * as turf from "@turf/turf";
import geoCountriesCoastline from "@geo-maps/countries-coastline-500m/map.geo.json" assert { type: "json" };
import fs from "fs";
const clientName = "testing";
export function buildStationsFile(profile, name, countrycode, entrypoint) {
console.log("Building Stations...");
fs.writeFileSync(`stations/${name}.ndjson`, "");
const client = createClient(profile, clientName);
const walk = createWalk(client);
const collection = turf.featureCollection(geoCountriesCoastline).features;
const feature = collection.features.find(
(f) => f.properties["A3"] === countrycode
);
const multiPolygon = turf.multiPolygon(feature.geometry.coordinates);
walk(entrypoint)
.on("data", (data) => {
if (
inInsideCountry(
multiPolygon,
data.location.latitude,
data.location.longitude
)
) {
fs.appendFileSync(
`stations/${name}.ndjson`,
JSON.stringify({
id: data.id,
name: data.name,
location: data.location,
weight: getWeight(data.products),
}) + "\n",
"utf8"
);
}
})
.on("error", console.error);
}
function inInsideCountry(multiPolygon, latitude, longitude) {
const point = turf.point([longitude, latitude]);
let inside = false;
multiPolygon.geometry.coordinates.forEach((coordinates) => {
const polygon = turf.polygon(coordinates);
if (turf.booleanPointInPolygon(point, polygon)) {
inside = true;
return;
}
});
return inside;
}
function getWeight(products) {
let total = 0;
let available = 0;
for (const [_, isAvailable] of Object.entries(products)) {
total++;
if (isAvailable) available++;
}
return (available / total) * 100;
}