-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgtfsUtils.ts
61 lines (52 loc) · 1.69 KB
/
gtfsUtils.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
import { parse, stringify } from 'jsr:@std/csv'
import { log } from './buildConfig.ts'
export const prefixGtfsColumnValues = async (csvFileName, prefix, column) => {
try {
const textCsv = await Deno.readTextFile(csvFileName)
const data = parse(textCsv, {
skipFirstRow: true,
strip: true,
})
const newData = data.map((line) => ({
...line,
[column]: prefix + line[column],
}))
const newCsv = stringify(newData, {
columns: Object.keys(newData[0]),
})
await Deno.writeTextFile(csvFileName, newCsv)
console.log('Prefixed service_ids in ' + csvFileName)
} catch (e) {
console.log(e)
}
}
const serviceIdFiles = ['calendar_dates.txt', 'calendars.txt', 'trips.txt']
export const prefixGtfsServiceIds = (gtfsDir, prefix) => {
serviceIdFiles.forEach((filename) => {
try {
prefixGtfsColumnValues(gtfsDir + '/' + filename, prefix, 'service_id')
} catch (e) {
console.log('Missing file ', filename)
}
})
}
export async function prefixGtfsAgencyIds(gtfsDir, prefix) {
try {
const agenciesCsv = await Deno.readTextFile(gtfsDir + '/agency.txt')
const agencies = parse(agenciesCsv, {
skipFirstRow: true,
strip: true,
})
const found = agencies.find((agency) => agency.agency_id.length < 3)
if (found) {
log(
`Dataset ${gtfsDir} has the agency_id "${found.agency_id}" that is shorter than 3 characters. This could lead to collisions, we're prefixing all its agency_ids`,
'violet'
)
prefixGtfsColumnValues(gtfsDir + '/agency.txt', prefix, 'agency_id')
prefixGtfsColumnValues(gtfsDir + '/routes.txt', prefix, 'agency_id')
}
} catch (e) {
console.log(e)
}
}