forked from ideal-postcodes/postcodes.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_missing.js
More file actions
executable file
·108 lines (97 loc) · 2.07 KB
/
Copy pathfind_missing.js
File metadata and controls
executable file
·108 lines (97 loc) · 2.07 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
/*
*
* This script checks for missing ONS Codes in the ONS Postcode Directory
*
* To run: find_missing.js /path/to/postcode/directory.csv
*
* This parses every line of the directory returns result to stdout
*
*/
"use strict";
const fs = require("fs");
const path = require("path");
const csv = require("csv");
const parse = csv.parse;
const transform = csv.transform;
const argv = require('minimist')(process.argv.slice(2));
const codeTypes = [
"nhsHa",
"counties",
"districts",
"wards",
"parishes",
"constituencies",
"european_registers",
"regions",
"pcts",
"lsoa",
"msoa",
"nuts",
"ccgs"
];
const typeOffset = {
nhsHa: 12,
counties: 5,
districts: 6,
wards: 7,
parishes: 44,
constituencies: 17,
european_registers: 18,
regions: 15,
pcts: 21,
lsoa: 42,
msoa: 43,
nuts: 22,
ccgs: 46
};
const source = argv._[0];
if (!source || !fs.existsSync(source)) {
console.log("Please specificy ONSPD Directory source file");
process.exit(0);
}
const type = argv.type;
if (type) {
if (!codeTypes.some(codeType => codeType === type )) {
console.log("Please specify a valid code type using --type=", codeTypes);
process.exit(0);
} else {
codeTypes = [type];
}
}
// Load data sources
const data = {};
const missingData = {};
codeTypes.forEach(codeType => {
missingData[codeType] = {};
data[codeType] = require(`../data/${codeType}.json`);
});
const check = (row, type) => {
const elem = row[typeOffset[type]];
if (elem === "") {
return;
} else {
if (typeof data[type][elem] === 'undefined') {
if (typeof missingData[type][elem] !== 'number') {
missingData[type][elem] = 0;
} else {
missingData[type][elem] += 1;
}
}
}
};
const parser = parse({delimiter: ","});
fs.createReadStream(source)
.pipe(parser)
.on("data", row => {
if (row[4].length !== 0) return null; // Skip row if terminated postcode
codeTypes.forEach(codeType => check(row, codeType));
})
.on("end", () => {
console.log(JSON.stringify(missingData, 2, 2));
process.exit(0);
})
.on("error", error => {
console.log(error.message);
process.exit(1);
});