-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateWardSpecificBusData.js
66 lines (54 loc) · 1.56 KB
/
calculateWardSpecificBusData.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
const turf = require("@turf/turf");
const fs = require("fs");
// load geojson data
require.extensions[".geojson"] = require.extensions[".json"];
let wardGeoJson = require("./data/chicagoWards2023.geojson");
let busGeoJson = require("./data/busData.json");
let wards = wardGeoJson["features"];
let busRoutes = busGeoJson["features"];
// get bus route data by ward
wardBusRouteData = [];
for (const ward of wards) {
let wardName = ward["properties"]["ward"];
// get bus routes in ward
let busRoutesInWard = busRoutes.filter((route) => {
return IsBus(route) && turf.booleanIntersects(ward, route);
});
// separate out route names
let routeNames = busRoutesInWard.map(
(route) => route["properties"]["route_id"]
);
routeNames = removeDuplicates(routeNames);
routeNames.sort();
// calculate trip totals
let trip_count_rt = 0;
let trip_count_sched = 0;
for (const route of busRoutesInWard) {
trip_count_rt += route["properties"]["trip_count_rt"];
trip_count_sched += route["properties"]["trip_count_sched"];
}
let ratio = trip_count_rt / trip_count_sched;
wardBusRouteData.push({
ward: wardName,
trip_count_rt,
trip_count_sched,
ratio,
routes: routeNames,
});
}
// save data
fs.writeFile(
"./data/wardBusRouteMappings.json",
JSON.stringify(wardBusRouteData),
(err) => {
if (err) console.log(err);
else console.log("Data saved to file.");
}
);
// helper functions
function IsBus(route) {
return route["properties"]["route_type"] == 3;
}
function removeDuplicates(arr) {
return [...new Set(arr)];
}