-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildWardAddresses.ts
95 lines (73 loc) · 2.18 KB
/
buildWardAddresses.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
88
89
90
91
92
93
94
95
import fs from 'node:fs'
import Point from '@arcgis/core/geometry/Point.js'
import Polygon from '@arcgis/core/geometry/Polygon.js'
import { parse as jsonToCSV } from 'json2csv'
interface MunicipalWard {
WARDNUMBER: number
'geometry.rings': number[][][]
polygon?: Polygon
}
interface Address {
CIVICNUMBER: number
STREETNAME: string
'geometry.x': number
'geometry.y': number
point?: Point
}
// load wards
console.log('Loading municipalWards.json')
const municipalWards: MunicipalWard[] = JSON.parse(
fs.readFileSync('data/municipalWards.json')
)
const wardAddresses: { [wardNumber: string]: Address[] } = {}
for (const municipalWard of municipalWards) {
municipalWard.polygon = new Polygon(municipalWard['geometry.rings'])
wardAddresses[municipalWard.WARDNUMBER.toString()] = []
}
// load addresses
console.log('Loading addresses.json')
const addresses: Address[] = JSON.parse(fs.readFileSync('data/addresses.json'))
// loop through addresses
for (const address of addresses) {
address.point = new Point(address['geometry.x'], address['geometry.y'])
let wardFound = false
for (const municipalWard of municipalWards) {
if (municipalWard.polygon.contains(address.point)) {
delete address.point
wardAddresses[municipalWard.WARDNUMBER.toString()].push(address)
wardFound = true
break
}
}
if (!wardFound) {
console.warn(
'No ward found: ' + address.CIVICNUMBER + ' ' + address.STREETNAME
)
}
}
// write records
for (const [wardNumber, wardNumberAddresses] of Object.entries(wardAddresses)) {
wardNumberAddresses.sort((addressA, addressB) => {
if (addressA.STREETNAME === addressB.STREETNAME) {
return addressA.CIVICNUMBER - addressB.CIVICNUMBER
}
if (addressA.STREETNAME > addressB.STREETNAME) {
return 1
}
return -1
})
try {
fs.writeFileSync(
'./data/addresses-ward' + wardNumber + '.json',
JSON.stringify(wardNumberAddresses, null, ' ')
)
} catch (error) {
console.error(error)
}
const csvData = jsonToCSV(wardNumberAddresses)
try {
fs.writeFileSync('./data/addresses-ward' + wardNumber + '.csv', csvData)
} catch (error) {
console.error(error)
}
}