gtfs 3.1.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/.eslintrc.json +14 -19
  2. package/.husky/pre-commit +4 -0
  3. package/@types/index.d.ts +269 -0
  4. package/@types/tests.ts +26 -0
  5. package/@types/tsconfig.json +17 -0
  6. package/CHANGELOG.md +182 -1
  7. package/README.md +168 -149
  8. package/bin/gtfs-export.js +4 -5
  9. package/bin/gtfs-import.js +6 -7
  10. package/config-sample-full.json +2 -7
  11. package/docs/images/node-gtfs-logo.svg +18 -0
  12. package/lib/db.js +63 -12
  13. package/lib/export.js +27 -13
  14. package/lib/file-utils.js +26 -9
  15. package/lib/geojson-utils.js +51 -38
  16. package/lib/gtfs/agencies.js +15 -4
  17. package/lib/gtfs/attributions.js +15 -4
  18. package/lib/gtfs/calendar-dates.js +15 -4
  19. package/lib/gtfs/calendars.js +15 -4
  20. package/lib/gtfs/fare-attributes.js +15 -4
  21. package/lib/gtfs/fare-rules.js +15 -4
  22. package/lib/gtfs/feed-info.js +15 -4
  23. package/lib/gtfs/frequencies.js +15 -4
  24. package/lib/gtfs/levels.js +15 -4
  25. package/lib/gtfs/pathways.js +15 -4
  26. package/lib/gtfs/routes.js +22 -6
  27. package/lib/gtfs/shapes.js +59 -23
  28. package/lib/gtfs/stop-times.js +15 -4
  29. package/lib/gtfs/stops.js +57 -23
  30. package/lib/gtfs/transfers.js +15 -4
  31. package/lib/gtfs/translations.js +15 -4
  32. package/lib/gtfs/trips.js +15 -4
  33. package/lib/gtfs-ride/board-alights.js +15 -4
  34. package/lib/gtfs-ride/ride-feed-infos.js +15 -4
  35. package/lib/gtfs-ride/rider-trips.js +15 -4
  36. package/lib/gtfs-ride/riderships.js +15 -4
  37. package/lib/gtfs-ride/trip-capacities.js +15 -4
  38. package/lib/import.js +203 -128
  39. package/lib/log-utils.js +8 -4
  40. package/lib/non-standard/directions.js +15 -4
  41. package/lib/non-standard/stop-attributes.js +15 -4
  42. package/lib/non-standard/timetable-notes-references.js +15 -4
  43. package/lib/non-standard/timetable-notes.js +15 -4
  44. package/lib/non-standard/timetable-pages.js +15 -4
  45. package/lib/non-standard/timetable-stop-order.js +15 -4
  46. package/lib/non-standard/timetables.js +15 -4
  47. package/lib/utils.js +26 -12
  48. package/models/gtfs/agency.js +11 -11
  49. package/models/gtfs/attributions.js +14 -14
  50. package/models/gtfs/calendar-dates.js +7 -7
  51. package/models/gtfs/calendar.js +12 -12
  52. package/models/gtfs/fare-attributes.js +9 -9
  53. package/models/gtfs/fare-rules.js +8 -8
  54. package/models/gtfs/feed-info.js +12 -12
  55. package/models/gtfs/frequencies.js +10 -10
  56. package/models/gtfs/levels.js +5 -5
  57. package/models/gtfs/pathways.js +14 -14
  58. package/models/gtfs/routes.js +14 -14
  59. package/models/gtfs/shapes.js +8 -8
  60. package/models/gtfs/stop-times.js +17 -17
  61. package/models/gtfs/stops.js +17 -17
  62. package/models/gtfs/transfers.js +7 -7
  63. package/models/gtfs/translations.js +10 -10
  64. package/models/gtfs/trips.js +12 -12
  65. package/models/gtfs-ride/board-alight.js +24 -24
  66. package/models/gtfs-ride/ride-feed-info.js +8 -8
  67. package/models/gtfs-ride/rider-trip.js +21 -21
  68. package/models/gtfs-ride/ridership.js +23 -23
  69. package/models/gtfs-ride/trip-capacity.js +10 -10
  70. package/models/models.js +1 -1
  71. package/models/non-standard/directions.js +6 -6
  72. package/models/non-standard/stop-attributes.js +5 -5
  73. package/models/non-standard/timetable-notes-references.js +9 -9
  74. package/models/non-standard/timetable-notes.js +5 -5
  75. package/models/non-standard/timetable-pages.js +5 -5
  76. package/models/non-standard/timetable-stop-order.js +6 -6
  77. package/models/non-standard/timetables.js +27 -27
  78. package/package.json +35 -13
  79. package/test/mocha/export-gtfs.js +74 -44
  80. package/test/mocha/get-agencies.js +20 -14
  81. package/test/mocha/get-attributions.js +10 -4
  82. package/test/mocha/get-board-alights.js +10 -4
  83. package/test/mocha/get-calendar-dates.js +31 -24
  84. package/test/mocha/get-calendars.js +17 -11
  85. package/test/mocha/get-db.js +71 -5
  86. package/test/mocha/get-directions.js +10 -4
  87. package/test/mocha/get-fare-attributes.js +12 -6
  88. package/test/mocha/get-fare-rules.js +17 -13
  89. package/test/mocha/get-feed-info.js +10 -4
  90. package/test/mocha/get-frequencies.js +10 -4
  91. package/test/mocha/get-levels.js +4 -4
  92. package/test/mocha/get-pathways.js +10 -4
  93. package/test/mocha/get-ride-feed-infos.js +9 -3
  94. package/test/mocha/get-rider-trips.js +10 -4
  95. package/test/mocha/get-riderships.js +10 -4
  96. package/test/mocha/get-routes.js +12 -16
  97. package/test/mocha/get-shapes-as-geojson.js +12 -6
  98. package/test/mocha/get-shapes.js +31 -39
  99. package/test/mocha/get-stop-attributes.js +10 -4
  100. package/test/mocha/get-stops-as-geojson.js +11 -5
  101. package/test/mocha/get-stops.js +62 -51
  102. package/test/mocha/get-stoptimes.js +18 -10
  103. package/test/mocha/get-timetable-pages.js +10 -4
  104. package/test/mocha/get-timetable-stop-orders.js +10 -4
  105. package/test/mocha/get-timetables.js +10 -4
  106. package/test/mocha/get-transfers.js +10 -4
  107. package/test/mocha/get-translations.js +10 -4
  108. package/test/mocha/get-trip-capacities.js +10 -4
  109. package/test/mocha/get-trips.js +6 -6
  110. package/test/mocha/import-gtfs.js +63 -46
  111. package/test/test-config.js +9 -4
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getStoptimes } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getStoptimes,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getStoptimes():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getStoptimes():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return an empty array if no stoptimes exist for given agency', async () => {
20
26
  const stopId = 'fake-stop-id';
21
27
 
22
28
  const results = await getStoptimes({
23
- stop_id: stopId
29
+ stop_id: stopId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -30,7 +36,7 @@ describe('getStoptimes():', () => {
30
36
  const stopId = '70011';
31
37
 
32
38
  const results = await getStoptimes({
33
- stop_id: stopId
39
+ stop_id: stopId,
34
40
  });
35
41
  should.exist(results);
36
42
  results.should.have.length(80);
@@ -43,11 +49,13 @@ describe('getStoptimes():', () => {
43
49
  it('should return array of stoptimes for given trip_id ordered by stop_sequence', async () => {
44
50
  const tripId = '421a';
45
51
 
46
- const results = await getStoptimes({
47
- trip_id: tripId
48
- }, [], [
49
- ['stop_sequence', 'ASC']
50
- ]);
52
+ const results = await getStoptimes(
53
+ {
54
+ trip_id: tripId,
55
+ },
56
+ [],
57
+ [['stop_sequence', 'ASC']]
58
+ );
51
59
 
52
60
  should.exist(results);
53
61
  results.should.have.length(24);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTimetablePages } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTimetablePages,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTimetablePages():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTimetablePages():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no timetable pages (GTFS-to-HTML timetables)', async () => {
20
26
  const timetablePageId = 'fake-timetable-page-id';
21
27
 
22
28
  const results = await getTimetablePages({
23
- timetable_page_id: timetablePageId
29
+ timetable_page_id: timetablePageId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTimetableStopOrders } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTimetableStopOrders,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTimetableStopOrders():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTimetableStopOrders():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no timetable stop orders (GTFS-to-HTML timetables)', async () => {
20
26
  const timetableId = 'fake-timetable-id';
21
27
 
22
28
  const results = await getTimetableStopOrders({
23
- timetable_id: timetableId
29
+ timetable_id: timetableId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTimetables } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTimetables,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTimetables():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTimetables():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no timetables (GTFS-to-HTML timetables)', async () => {
20
26
  const timetableId = 'fake-timetable-id';
21
27
 
22
28
  const results = await getTimetables({
23
- timetable_id: timetableId
29
+ timetable_id: timetableId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTransfers } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTransfers,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTransfers():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTransfers():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no transfers', async () => {
20
26
  const fromStopId = 'fake-stop-id';
21
27
 
22
28
  const results = await getTransfers({
23
- from_stop_id: fromStopId
29
+ from_stop_id: fromStopId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTranslations } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTranslations,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTranslations():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTranslations():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no translations', async () => {
20
26
  const fieldName = 'fake-field-name';
21
27
 
22
28
  const results = await getTranslations({
23
- field_name: fieldName
29
+ field_name: fieldName,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,14 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTripCapacities } from '../../index.js';
6
+ import {
7
+ openDb,
8
+ getDb,
9
+ closeDb,
10
+ importGtfs,
11
+ getTripCapacities,
12
+ } from '../../index.js';
8
13
 
9
14
  describe('getTripCapacities():', () => {
10
15
  before(async () => {
@@ -13,14 +18,15 @@ describe('getTripCapacities():', () => {
13
18
  });
14
19
 
15
20
  after(async () => {
16
- await closeDb();
21
+ const db = getDb(config);
22
+ await closeDb(db);
17
23
  });
18
24
 
19
25
  it('should return empty array if no trip capacities (GTFS-ride)', async () => {
20
26
  const tripId = 'fake-trip-id';
21
27
 
22
28
  const results = await getTripCapacities({
23
- trip_id: tripId
29
+ trip_id: tripId,
24
30
  });
25
31
  should.exists(results);
26
32
  results.should.have.length(0);
@@ -2,9 +2,8 @@
2
2
 
3
3
  import should from 'should';
4
4
 
5
- import { openDb, closeDb } from '../../lib/db.js';
6
5
  import config from '../test-config.js';
7
- import { importGtfs, getTrips } from '../../index.js';
6
+ import { openDb, getDb, closeDb, importGtfs, getTrips } from '../../index.js';
8
7
 
9
8
  describe('getTrips():', () => {
10
9
  before(async () => {
@@ -13,14 +12,15 @@ describe('getTrips():', () => {
13
12
  });
14
13
 
15
14
  after(async () => {
16
- await closeDb();
15
+ const db = getDb(config);
16
+ await closeDb(db);
17
17
  });
18
18
 
19
19
  it('should return empty array if no trips exist', async () => {
20
20
  const tripId = 'fake-trip-id';
21
21
 
22
22
  const results = await getTrips({
23
- trip_id: tripId
23
+ trip_id: tripId,
24
24
  });
25
25
  should.exists(results);
26
26
  results.should.have.length(0);
@@ -30,7 +30,7 @@ describe('getTrips():', () => {
30
30
  const routeId = 'Bu-16APR';
31
31
 
32
32
  const results = await getTrips({
33
- route_id: routeId
33
+ route_id: routeId,
34
34
  });
35
35
 
36
36
  const expectedResult = {
@@ -43,7 +43,7 @@ describe('getTrips():', () => {
43
43
  block_id: null,
44
44
  shape_id: 'cal_tam_sf',
45
45
  wheelchair_accessible: 1,
46
- bikes_allowed: 1
46
+ bikes_allowed: 1,
47
47
  };
48
48
 
49
49
  should.exists(results);
@@ -4,24 +4,30 @@
4
4
  import { createReadStream, existsSync } from 'node:fs';
5
5
  import path from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
- import parse from 'csv-parse';
7
+ import { parse } from 'csv-parse';
8
8
  import should from 'should';
9
9
 
10
- import { openDb, closeDb } from '../../lib/db.js';
11
10
  import { unzip } from '../../lib/file-utils.js';
12
11
  import config from '../test-config.js';
13
- import { importGtfs, getRoutes } from '../../index.js';
12
+ import { openDb, getDb, closeDb, importGtfs, getRoutes } from '../../index.js';
14
13
  import models from '../../models/models.js';
15
14
 
16
15
  let db;
17
16
 
18
- const agenciesFixturesRemote = [{
19
- url: 'http://transitfeeds.com/p/caltrain/122/20160406/download'
20
- }];
21
-
22
- const agenciesFixturesLocal = [{
23
- path: path.join(path.dirname(fileURLToPath(import.meta.url)), '../fixture/caltrain_20160406.zip')
24
- }];
17
+ const agenciesFixturesRemote = [
18
+ {
19
+ url: 'http://transitfeeds.com/p/caltrain/122/20160406/download',
20
+ },
21
+ ];
22
+
23
+ const agenciesFixturesLocal = [
24
+ {
25
+ path: path.join(
26
+ path.dirname(fileURLToPath(import.meta.url)),
27
+ '../fixture/caltrain_20160406.zip'
28
+ ),
29
+ },
30
+ ];
25
31
 
26
32
  describe('importGtfs():', function () {
27
33
  before(async () => {
@@ -29,7 +35,8 @@ describe('importGtfs():', function () {
29
35
  });
30
36
 
31
37
  after(async () => {
32
- await closeDb();
38
+ const db = getDb(config);
39
+ await closeDb(db);
33
40
  });
34
41
 
35
42
  this.timeout(10000);
@@ -37,7 +44,7 @@ describe('importGtfs():', function () {
37
44
  it('should be able to download and import from HTTP', async () => {
38
45
  await importGtfs({
39
46
  ...config,
40
- agencies: agenciesFixturesRemote
47
+ agencies: agenciesFixturesRemote,
41
48
  });
42
49
 
43
50
  const routes = await getRoutes();
@@ -48,7 +55,7 @@ describe('importGtfs():', function () {
48
55
  it('should be able to download and import from local filesystem', async () => {
49
56
  await importGtfs({
50
57
  ...config,
51
- agencies: agenciesFixturesLocal
58
+ agencies: agenciesFixturesLocal,
52
59
  });
53
60
 
54
61
  const routes = await getRoutes();
@@ -56,63 +63,73 @@ describe('importGtfs():', function () {
56
63
  routes.length.should.equal(4);
57
64
  });
58
65
 
59
- it('should throw an error when importing from local filesystem which doesn\'t exist', async () => {
66
+ it("should throw an error when importing from local filesystem which doesn't exist", async () => {
60
67
  return importGtfs({
61
68
  ...config,
62
69
  agencies: [
63
70
  {
64
- path: '/does/not/exist'
65
- }
66
- ]
71
+ path: '/does/not/exist',
72
+ },
73
+ ],
67
74
  }).should.be.rejected();
68
75
  });
69
76
  });
70
77
 
71
78
  describe('Verify data imported into database', () => {
72
79
  const countData = {};
73
- const temporaryDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '../fixture/tmp/');
80
+ const temporaryDir = path.join(
81
+ path.dirname(fileURLToPath(import.meta.url)),
82
+ '../fixture/tmp/'
83
+ );
74
84
 
75
85
  before(async () => {
76
86
  await unzip(agenciesFixturesLocal[0].path, temporaryDir);
77
87
 
78
- await Promise.all(models.map(model => {
79
- const filePath = path.join(temporaryDir, `${model.filenameBase}.txt`);
80
-
81
- // GTFS has optional files
82
- if (!existsSync(filePath)) {
83
- countData[model.filenameBase] = 0;
84
- return false;
85
- }
86
-
87
- const parser = parse({
88
- columns: true,
89
- relax: true,
90
- trim: true
91
- }, (error, data) => {
92
- if (error) {
93
- throw new Error(error);
94
- }
88
+ await Promise.all(
89
+ models.map((model) => {
90
+ const filePath = path.join(temporaryDir, `${model.filenameBase}.txt`);
95
91
 
96
- countData[model.filenameBase] = data.length;
97
- });
92
+ // GTFS has optional files
93
+ if (!existsSync(filePath)) {
94
+ countData[model.filenameBase] = 0;
95
+ return false;
96
+ }
98
97
 
99
- return createReadStream(filePath)
100
- .pipe(parser)
101
- .on('error', error => {
102
- countData[model.collection] = 0;
103
- throw new Error(error);
104
- });
105
- }));
98
+ const parser = parse(
99
+ {
100
+ columns: true,
101
+ relax: true,
102
+ trim: true,
103
+ },
104
+ (error, data) => {
105
+ if (error) {
106
+ throw new Error(error);
107
+ }
108
+
109
+ countData[model.filenameBase] = data.length;
110
+ }
111
+ );
112
+
113
+ return createReadStream(filePath)
114
+ .pipe(parser)
115
+ .on('error', (error) => {
116
+ countData[model.collection] = 0;
117
+ throw new Error(error);
118
+ });
119
+ })
120
+ );
106
121
 
107
122
  await importGtfs({
108
123
  ...config,
109
- agencies: agenciesFixturesLocal
124
+ agencies: agenciesFixturesLocal,
110
125
  });
111
126
  });
112
127
 
113
128
  for (const model of models) {
114
129
  it(`should import the same number of ${model.filenameBase}`, async () => {
115
- const result = await db.get(`SELECT COUNT(*) FROM ${model.filenameBase};`);
130
+ const result = await db.get(
131
+ `SELECT COUNT(*) FROM ${model.filenameBase};`
132
+ );
116
133
  result['COUNT(*)'].should.equal(countData[model.filenameBase]);
117
134
  });
118
135
  }
@@ -2,10 +2,15 @@ import path from 'node:path';
2
2
  import { fileURLToPath } from 'node:url';
3
3
 
4
4
  const config = {
5
- agencies: [{
6
- path: path.join(path.dirname(fileURLToPath(import.meta.url)), 'fixture/caltrain_20160406.zip')
7
- }],
8
- verbose: false
5
+ agencies: [
6
+ {
7
+ path: path.join(
8
+ path.dirname(fileURLToPath(import.meta.url)),
9
+ 'fixture/caltrain_20160406.zip'
10
+ ),
11
+ },
12
+ ],
13
+ verbose: false,
9
14
  };
10
15
 
11
16
  export default config;