-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildDistinctStreets.ts
87 lines (63 loc) · 2.17 KB
/
buildDistinctStreets.ts
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
import * as fs from "fs";
import { parse as jsonToCSV } from "json2csv";
interface Street {
FULLNAME: string;
STREETPREFIX: string;
STREETNAME: string;
STREETTYPE: string;
STREETSUFFIX: string;
LEFTFROMADDRESS: number;
LEFTTOADDRESS: number;
RIGHTFROMADDRESS: number;
RIGHTTOADDRESS: number;
MUNICIPALITY: string;
};
// load streets
console.log("Loading streets.json");
const streets: Street[] = JSON.parse(fs.readFileSync("data/streets.json"));
const distinctStreetsMap = new Map<string, Street>();
for (const street of streets) {
if (street.MUNICIPALITY !== "Sault Ste. Marie") {
continue;
}
const mapKey = street.MUNICIPALITY + "::" + street.FULLNAME;
if (distinctStreetsMap.has(mapKey)) {
const distinctStreet = distinctStreetsMap.get(mapKey);
distinctStreet.LEFTFROMADDRESS = Math.min(distinctStreet.LEFTFROMADDRESS, street.LEFTFROMADDRESS);
distinctStreet.LEFTTOADDRESS = Math.max(distinctStreet.LEFTTOADDRESS, street.LEFTTOADDRESS);
distinctStreet.RIGHTFROMADDRESS = Math.min(distinctStreet.RIGHTFROMADDRESS, street.RIGHTFROMADDRESS);
distinctStreet.RIGHTTOADDRESS = Math.max(distinctStreet.RIGHTTOADDRESS, street.RIGHTTOADDRESS);
} else {
distinctStreetsMap.set(mapKey, {
FULLNAME: street.FULLNAME,
STREETPREFIX: street.STREETPREFIX,
STREETNAME: street.STREETNAME,
STREETTYPE: street.STREETTYPE,
STREETSUFFIX: street.STREETSUFFIX,
LEFTFROMADDRESS: street.LEFTFROMADDRESS,
LEFTTOADDRESS: street.LEFTTOADDRESS,
RIGHTFROMADDRESS: street.RIGHTFROMADDRESS,
RIGHTTOADDRESS: street.RIGHTTOADDRESS,
MUNICIPALITY: street.MUNICIPALITY
});
}
}
// write records
const distinctStreets: Street[] = Array.from(distinctStreetsMap.values());
distinctStreets.sort((streetA, streetB) => {
if (streetA.FULLNAME > streetB.FULLNAME) {
return 1;
}
return -1;
});
try {
fs.writeFileSync("./data/streets-distinct.json", JSON.stringify(distinctStreets, null, " "));
} catch (error) {
console.error(error);
}
const csvData = jsonToCSV(distinctStreets);
try {
fs.writeFileSync("./data/streets-distinct.csv", csvData);
} catch (error) {
console.error(error);
}