-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
88 lines (83 loc) · 2.48 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import fs from 'fs';
export function parseAreaCodeCSV(filename) {
const areaCodeMap = {};
const allAreaCodes = fs.readFileSync(filename).toString().split('\n');
for (const line of allAreaCodes) {
let [city, areaCode, lng, lat] = line.split(';');
areaCodeMap[areaCode] = {
city: city,
lng: lng,
lat: lat,
occurrences: 0,
};
}
return areaCodeMap;
}
export function isCountryNumber(countryNumber, number) {
return number.startsWith(countryNumber);
}
export function createFeatureJSON(areaCodeMap) {
let featureData = [];
Object.keys(areaCodeMap).forEach((key) => {
if (areaCodeMap[key].occurrences) {
featureData.push({
type: 'Feature',
properties: {
name: `<b>${areaCodeMap[key].city}</b><br>Anrufe: ${areaCodeMap[key].occurrences}`,
},
geometry: {
type: 'Point',
coordinates: [areaCodeMap[key].lng, areaCodeMap[key].lat],
},
});
}
});
return JSON.stringify(
{
type: 'FeatureCollection',
features: featureData,
},
null,
2
);
}
export function createHeatJSON(areaCodeMap, maxOccurrence) {
let heatData = [];
Object.keys(areaCodeMap).forEach((key) => {
if (areaCodeMap[key].occurrences) {
heatData.push([
areaCodeMap[key].lat,
areaCodeMap[key].lng,
(areaCodeMap[key].occurrences / maxOccurrence) * 100,
]);
}
});
return JSON.stringify(heatData, null, 2);
}
export function forecast(areaCodeMap, maxSlice) {
let maxOccurrences = [];
Object.keys(areaCodeMap).forEach((key) => {
if (areaCodeMap[key].occurrences) {
maxOccurrences.push([
areaCodeMap[key].occurrences,
{
key: key,
city: areaCodeMap[key].city,
lng: areaCodeMap[key].lng,
lat: areaCodeMap[key].lat,
},
]);
}
});
maxOccurrences.sort((a, b) => b[0] - a[0]);
let newAreaMap = {};
for (let city of maxOccurrences.slice(0, maxSlice)) {
newAreaMap[city[1].key] = {
occurrences: city[0],
city: city[1].city,
lng: city[1].lng,
lat: city[1].lat,
};
}
return newAreaMap;
}