-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 2.42 KB
/
Copy pathindex.js
File metadata and controls
69 lines (59 loc) · 2.42 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
const util = require('util');
const fs = require('fs');
const csv = require('csv-parse');
const listOfStations = require('./input/odpt_Tokyo_stations.json');
const listOfLines = require('open-data-jp-railway-lines/lines.json');
const { Group, Station } = require('./model/stations');
const RAILWAY_PREFIX = 'odpt.Railway:'.length;
async function readCsv(filePath, transform = (x) => x) {
return new Promise((resolve, reject) => {
fs.createReadStream(filePath).pipe(csv({ columns: true }, (err, data) => {
if (err) reject(err);
else resolve(transform(data));
}));
});
}
async function generate() {
const ekidataStations = await readCsv('./input/station20180330free.csv');
const mapOfLinesByCode = listOfLines.reduce((all, line) => {
if (line.code && line.ekidata_id) {
all[line.ekidata_id] = (all[line.ekidata_id] || []).concat(line.code);
}
return all;
}, {});
const opendataCodesFound = [];
const groups = {};
ekidataStations.forEach((station) => {
if (station.e_status !== '0') return;
const ekiGroup = station.station_g_cd;
const ekiName = station.station_name;
const ekiCode = station.station_cd;
const ekiLine = station.line_cd;
if (!groups[ekiGroup]) {
groups[ekiGroup] = new Group(ekiGroup);
}
groups[ekiGroup].add(station);
});
const data = Object.values(groups).map((group) => {
opendataCodesFound.push(...group.opendataStationCodes());
return group.toJson();
});
const numberOfEkiStation = ekidataStations.filter((s) => s.e_status === '0').length;
const numberOfODStation = listOfStations.length;
const numberOfEkiStationInTokyo = ekidataStations.reduce((count, station) => {
if (station.e_status !== '0') return count;
const line = station.line_cd;
const prefecture = station.pref_cd;
if (['11', '12', '13', '14'].includes(prefecture)) count += 1;
return count;
}, 0);
listOfStations.filter((s) => !opendataCodesFound.includes(s['owl:sameAs'].substring(RAILWAY_PREFIX))).map((s) => {
console.log('did not find', s['odpt:stationTitle'], s['odpt:railway']);
})
console.log(`Matched ${opendataCodesFound.length} open data stations out of ${numberOfODStation}.`);
console.log(`Ekidata contains ${numberOfEkiStationInTokyo} in the Tokyo area, out of ${numberOfEkiStation}`);
return util.promisify(fs.writeFile)('./stations.json', JSON.stringify(data, null, ' '));
}
if (require.main === module) {
generate();
}