Skip to content

Commit

Permalink
improve & fix lib/build-trajectory tests ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed May 27, 2021
1 parent 4553d3f commit 732b193
Showing 1 changed file with 222 additions and 125 deletions.
347 changes: 222 additions & 125 deletions test/build-trajectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const buildTrajectory = require('../lib/build-trajectory')

test('buildTrajectory works with imaginary shape & schedule', async (t) => {
const stopLocs = inMemoryStore()
stopLocs.raw.set('airport', [1.0, 1.0])
stopLocs.raw.set('train', [1.02, 1.02])
stopLocs.raw.set('museum', [1.1, 1.1])
stopLocs.raw.set('mall', [1.2, 1.2])
stopLocs.raw.set('center', [1.3, 1.3])
stopLocs.raw.set('foo', [-1, -1])
stopLocs.raw.set('airport', [ 1.1, 1.1])
stopLocs.raw.set('train', [ 1.102, 1.102])
stopLocs.raw.set('museum', [ 1.11, 1.11])
stopLocs.raw.set('mall', [ 1.12, 1.12])
stopLocs.raw.set('center', [ 1.13, 1.13])
stopLocs.raw.set('foo', [-1, -1])

const schedule = {
id: '12',
Expand All @@ -27,17 +27,17 @@ test('buildTrajectory works with imaginary shape & schedule', async (t) => {

const shapeId = 's0'
const shapePoints = [
// <-- airport is here, at 1.0 | 1.0
[1.01, 1.01, 0],
// <-- train is here, at 1.02 | 1.02
[1.04, 1.04, 1],
[1.05, 1.05, 2],
[1.08, 1.08, 4],
// <-- museum is here, at 1.1 | 1.1
[1.11, 1.11, 5],
[1.20, 1.20, 6], // <-- mall is right here, at 1.2 | 1.2
[1.29, 1.29, 7],
// <-- center is here, at 1.3 | 1.3
// <-- airport is here, at 1.10 | 1.10
[1.101, 1.101, 0],
// <-- train is here, at 1.102 | 1.102
[1.104, 1.104, 1],
[1.105, 1.105, 2],
[1.108, 1.108, 4],
// <-- museum is here, at 1.11 | 1.11
[1.111, 1.111, 5],
[1.12, 1.12, 6], // <-- mall is right here, at 1.12 | 1.12
[1.129, 1.129, 7],
// <-- center is here, at 1.13 | 1.13
].map(([shape_pt_lon, shape_pt_lat, shape_pt_sequence]) => ({
shape_pt_lat,
shape_pt_lon,
Expand All @@ -58,17 +58,17 @@ test('buildTrajectory works with imaginary shape & schedule', async (t) => {
...r,
])
t.deepEqual(coords, [
[1.00, 1.00, null, 0, 30],
[1.01, 1.01, null, 45, 45],
[1.0199999, 1.0199999, null, 60, 80],
[1.04, 1.04, null, 113, 113],
[1.05, 1.05, null, 129, 129],
[1.08, 1.08, null, 178, 178],
[1.0999999, 1.0999999, null, 210, 240],
[1.11, 1.11, null, 267, 267],
[1.20, 1.20, null, 510, 540],
[1.29, 1.29, null, 594, 594],
[1.30, 1.30, null, 600, 720],
// [1.1, 1.1, null, 0, 30],
[1.101, 1.101, null, 45, 45],
// [1.102, 1.102, null, 60, 80],
[1.104, 1.104, null, 113, 113],
[1.105, 1.105, null, 129, 129],
[1.108, 1.108, null, 178, 178],
// [1.11, 1.11, null, 210, 240],
[1.111, 1.111, null, 267, 267],
[1.12, 1.12, null, 510, 540],
[1.129, 1.129, null, 594, 594],
// [1.13, 1.13, null, 600, 720],
])
})

Expand Down Expand Up @@ -174,104 +174,201 @@ test('buildTrajectory works with shape `190067` & schedule `ZimPNw` from DELFI f
])
})

test.skip('buildTrajectory works with shape `todo` & schedule `todo` from VVS feed', async (t) => {
// const stopLocs = inMemoryStore()
// stopLocs.raw.set('de:03255:43633::7', [9.453118, 51.820517])
// stopLocs.raw.set('de:03255:43651::2', [9.450299, 51.827289])
// stopLocs.raw.set('de:03255:43693::2', [9.461404, 51.827777])
// stopLocs.raw.set('de:03255:43643::2', [9.482121, 51.825886])
// stopLocs.raw.set('de:03255:43669::3', [9.470128, 51.827976])
// stopLocs.raw.set('de:03255:43678::1', [9.474612, 51.827568])
// stopLocs.raw.set('de:03255:43680::2', [9.487562, 51.819148])
// stopLocs.raw.set('de:03255:43681::1', [9.484809, 51.823526])
// stopLocs.raw.set('de:03255:65768::2', [9.521271, 51.75306])
// stopLocs.raw.set('de:03255:84673::2', [9.542856, 51.765074])
// stopLocs.raw.set('de:03255:84670::2', [9.544986, 51.767456])
// stopLocs.raw.set('de:03255:84674::2', [9.541251, 51.770171])
// stopLocs.raw.set('de:03255:84672::2', [9.535901, 51.772434])
// stopLocs.raw.set('de:03255:84671::2', [9.529166, 51.772851])
// stopLocs.raw.set('de:03255:63479::2', [9.513741, 51.795416])

// const schedule = {
// id: 'todo',
// trips: [
// {tripId: '1062817478', start: 35580},
// ],
// stops: [
// 'de:03255:65768::2',
// 'de:03255:84673::2',
// 'de:03255:84670::2',
// 'de:03255:84674::2',
// 'de:03255:84672::2',
// 'de:03255:84671::2',
// 'de:03255:63479::2',
// 'de:03255:43680::2',
// 'de:03255:43681::1',
// 'de:03255:43643::2',
// 'de:03255:43678::1',
// 'de:03255:43669::3',
// 'de:03255:43693::2',
// 'de:03255:43651::2',
// 'de:03255:43633::7'
// ],
// arrivals: [0, 180, 240, 300, 360, 420, 600, 840, 900, 960, 1020, 1080, 1140, 1260, 1500],
// departures: [0, 180, 240, 300, 360, 420, 600, 840, 900, 960, 1020, 1080, 1140, 1260, 1500],
// headwayBasedStarts: [],
// headwayBasedEnds: [],
// headwayBasedHeadways: []
// }
// `31-779-j21-2.1.H` has a circular/loop route: It starts and ends at
// `de:08115:4512:5:F`. We check if the stop -> shape matching algorithm
// matches properly.
test('buildTrajectory works with shape `31-779-j21-2.1.H` & schedule `ZVU8cE` from VVS feed', async (t) => {
const stopLocs = inMemoryStore()
stopLocs.raw.set('de:08115:4512:5:F', [8.8647526, 48.5943370])
stopLocs.raw.set('de:08115:3237:0:3', [8.8608930, 48.5928306])
stopLocs.raw.set('de:08115:7055:0:3', [8.8597890, 48.5900775])
stopLocs.raw.set('de:08115:7056:0:3', [8.8579596, 48.5898775])
stopLocs.raw.set('de:08115:7057:0:3', [8.8557810, 48.5884989])
stopLocs.raw.set('de:08115:7042:0:3', [8.8554886, 48.5864752])
stopLocs.raw.set('de:08115:7059:0:3', [8.8603502, 48.5881178])
stopLocs.raw.set('de:08115:7040:0:3', [8.8614449, 48.5893062])
stopLocs.raw.set('de:08115:4512:5:F', [8.8647526, 48.5943370])

// const shapeId = 'todo'
// const shapePoints = [
// [9.521271, 51.75306, 0],
// [9.542856, 51.765074, 1],
// [9.544986, 51.767456, 3],
// [9.541251, 51.770171, 5],
// [9.535901, 51.772434, 7],
// [9.529166, 51.772851, 9],
// [9.513741, 51.795416, 11],
// [9.487562, 51.819148, 13],
// [9.484809, 51.823526, 15],
// [9.482121, 51.825886, 17],
// [9.474612, 51.827568, 19],
// [9.470128, 51.827976, 21],
// [9.461404, 51.827777, 23],
// [9.450299, 51.827289, 25],
// [9.453118, 51.820517, 27],
// ].map(([shape_pt_lon, shape_pt_lat, shape_pt_sequence]) => ({
// shape_pt_lat,
// shape_pt_lon,
// shape_pt_sequence,
// shape_dist_traveled: null,
// }))
const shapeId = '31-779-j21-2.1.H'
const shapePoints = [
[8.86477834270177, 48.5943037905581, 1, 0.00],
[8.86377026621193, 48.5939490162386, 2, 83.98],
[8.8637804491298, 48.5937912969797, 3, 101.53],
[8.86090805475374, 48.5928092068706, 4, 339.34],
[8.86090805475374, 48.5928092068706, 5, 339.34],
[8.86066535367219, 48.5927259124475, 6, 359.45],
[8.86135449249283, 48.5918533733072, 7, 468.91],
[8.86161936843633, 48.591607561355, 8, 502.47],
[8.86099347284096, 48.5912161694282, 9, 565.82],
[8.86084888396262, 48.5910978319371, 10, 582.74],
[8.86076702924912, 48.5909409013504, 11, 601.20],
[8.86075865937038, 48.5908786620345, 12, 608.15],
[8.86092978926045, 48.5905606175429, 13, 645.69],
[8.86111829704448, 48.5900829738342, 14, 700.58],
[8.85978458165628, 48.5900762386263, 15, 798.67],
[8.85978458165628, 48.5900762386263, 16, 798.67],
[8.85926693995909, 48.5900596028839, 17, 836.79],
[8.85796312688962, 48.5898787849496, 18, 934.77],
[8.85796312688962, 48.5898787849496, 19, 934.77],
[8.85760887512488, 48.5898466950899, 20, 961.07],
[8.85762282690398, 48.5896566072989, 21, 982.23],
[8.85751263640893, 48.5892637618058, 22, 1026.66],
[8.85743854844347, 48.5890972162153, 23, 1045.96],
[8.85724129446941, 48.5889608238704, 24, 1066.95],
[8.85671044383442, 48.5887292355562, 25, 1113.72],
[8.85579044095832, 48.5884935630313, 26, 1186.29],
[8.85579044095832, 48.5884935630313, 27, 1186.29],
[8.85510385259979, 48.5884052060187, 28, 1237.73],
[8.85407361285452, 48.58842243527, 29, 1313.53],
[8.85397128161815, 48.588182741372, 30, 1341.23],
[8.85361496574432, 48.5877422794815, 31, 1396.78],
[8.85363915111485, 48.5876280132644, 32, 1409.61],
[8.85377554091393, 48.5874770190497, 33, 1429.16],
[8.85444084021872, 48.5869468413189, 34, 1505.78],
[8.8549393407577, 48.5866766085486, 35, 1553.19],
[8.85549349996355, 48.5864701112995, 36, 1599.97],
[8.85549349996355, 48.5864701112995, 37, 1599.97],
[8.85559213354871, 48.5864378609082, 38, 1608.06],
[8.8557413567994, 48.5866337277136, 39, 1632.45],
[8.85592167295924, 48.5867635369326, 40, 1652.05],
[8.85934166399869, 48.5877915631428, 41, 1928.36],
[8.86033335430432, 48.5881342167597, 42, 2010.65],
[8.86033335430432, 48.5881342167597, 43, 2010.65],
[8.86171032217549, 48.588666618826, 44, 2127.96],
[8.86144614249089, 48.589309817913, 45, 2202.08],
[8.86144614249089, 48.589309817913, 46, 2202.08],
[8.86092978926045, 48.5905606175429, 47, 2346.25],
[8.86075865937038, 48.5908786620345, 48, 2383.79],
[8.86084888396262, 48.5910978319371, 49, 2409.05],
[8.86099347284096, 48.5912161694282, 50, 2425.97],
[8.86181539388917, 48.5917052762119, 51, 2507.28],
[8.86215707737929, 48.5919790609188, 52, 2546.75],
[8.86287844635044, 48.5928664150682, 53, 2658.78],
[8.86302102899034, 48.5929782728798, 54, 2675.05],
[8.86355681561948, 48.5931658636236, 55, 2719.64],
[8.86363553151238, 48.5932339409746, 56, 2729.17],
[8.86376864076299, 48.5932394934337, 57, 2738.97],
[8.86424337519592, 48.5933963440731, 58, 2778.00],
[8.86450755287211, 48.5934105924721, 59, 2797.50],
[8.8647457159111, 48.5935727391088, 60, 2822.63],
[8.86574037764176, 48.5939248814712, 61, 2905.60],
[8.86594718452105, 48.5940532667335, 62, 2926.46],
[8.86636784605579, 48.5944667863207, 63, 2981.88],
[8.86618479791561, 48.5944273667084, 64, 2996.04],
[8.86579203907502, 48.5944349151284, 65, 3024.94],
[8.86561275626872, 48.5944072793585, 66, 3038.48],
[8.86523109401469, 48.5942687979757, 67, 3070.49],
[8.86502635667213, 48.5943827660098, 68, 3090.17],
[8.86477834270177, 48.5943037905581, 69, 3110.42],
].map(([shape_pt_lon, shape_pt_lat, shape_pt_sequence]) => ({
shape_pt_lat,
shape_pt_lon,
shape_pt_sequence,
shape_dist_traveled: null,
}))
const schedule = {
id: 'ZVU8cE',
trips: [
{tripId: '1.T0.31-779-j21-2.1.H', start: 33000},
],
stops: [
'de:08115:4512:5:F',
'de:08115:3237:0:3',
'de:08115:7055:0:3',
'de:08115:7056:0:3',
'de:08115:7057:0:3',
'de:08115:7042:0:3',
'de:08115:7059:0:3',
'de:08115:7040:0:3',
'de:08115:4512:5:F',
],
arrivals: [0, 60, 120, 180, 240, 300, 360, 420, 600],
departures: [0, 60, 120, 180, 240, 300, 360, 420, 600],
headwayBasedStarts: [],
headwayBasedEnds: [],
headwayBasedHeadways: [],
}

// const trajectory = await buildTrajectory(shapeId, shapePoints, schedule, stopLocs)
const trajectory = await buildTrajectory(shapeId, shapePoints, schedule, stopLocs)

// t.deepEqual(trajectory.properties, {
// shapeId: 'todo',
// scheduleId: 'todo',
// })
// const coords = trajectory.geometry.coordinates
// .map(([lon, lat, ...r]) => [
// parseFloat(lon.toFixed(7)),
// parseFloat(lat.toFixed(7)),
// ...r,
// ])
// t.deepEqual(coords, [
// [9.521271, 51.75306, null, 0, 0],
// [9.542856, 51.765074, null, 180, 180],
// [9.544986, 51.767456, null, 240, 240],
// [9.541251, 51.770171, null, 300, 300],
// [9.535901, 51.772434, null, 360, 360],
// [9.529166, 51.772851, null, 420, 420],
// [9.513741, 51.795416, null, 600, 600],
// [9.487562, 51.819148, null, 840, 840],
// [9.484809, 51.823526, null, 900, 900],
// [9.482121, 51.825886, null, 960, 960],
// [9.474612, 51.827568, null, 1020, 1020],
// [9.470128, 51.827976, null, 1080, 1080],
// [9.461404, 51.827777, null, 1140, 1140],
// [9.450299, 51.827289, null, 1260, 1260],
// [9.453118, 51.820517, null, 1500, 1500],
// ])
t.deepEqual(trajectory.properties, {
shapeId,
scheduleId: 'ZVU8cE',
})
const coords = trajectory.geometry.coordinates.map(([lon, lat, ...r]) => [
parseFloat(lon.toFixed(7)),
parseFloat(lat.toFixed(7)),
...r,
])
t.deepEqual(coords, [
[8.8647783, 48.5943038, null, 1, 1],
[8.8637703, 48.593949, null, 15, 15],
[8.8637804, 48.5937913, null, 18, 18],
[8.8609081, 48.5928092, null, 60, 60],
[8.8609081, 48.5928092, null, 60, 60],
[8.8606654, 48.5927259, null, 63, 63],
[8.8613545, 48.5918534, null, 77, 77],
[8.8616194, 48.5916076, null, 81, 81],
[8.8609935, 48.5912162, null, 90, 90],
[8.8608489, 48.5910978, null, 92, 92],
[8.860767, 48.5909409, null, 94, 94],
[8.8607587, 48.5908787, null, 95, 95],
[8.8609298, 48.5905606, null, 100, 100],
[8.8611183, 48.590083, null, 107, 107],
[8.8597846, 48.5900762, null, 120, 120],
[8.8597846, 48.5900762, null, 120, 120],
[8.8592669, 48.5900596, null, 137, 137],
[8.8579631, 48.5898788, null, 180, 180],
[8.8579631, 48.5898788, null, 180, 180],
[8.8576089, 48.5898467, null, 186, 186],
[8.8576228, 48.5896566, null, 191, 191],
[8.8575126, 48.5892638, null, 202, 202],
[8.8574385, 48.5890972, null, 207, 207],
[8.8572413, 48.5889608, null, 212, 212],
[8.8567104, 48.5887292, null, 223, 223],
[8.8557904, 48.5884936, null, 240, 240],
[8.8557904, 48.5884936, null, 240, 240],
[8.8551039, 48.5884052, null, 300, 300],
[8.8540736, 48.5884224, null, 306, 306],
[8.8539713, 48.5881827, null, 308, 308],
[8.853615, 48.5877423, null, 312, 312],
[8.8536392, 48.587628, null, 313, 313],
[8.8537755, 48.587477, null, 315, 315],
[8.8544408, 48.5869468, null, 321, 321],
[8.8549393, 48.5866766, null, 324, 324],
[8.8554935, 48.5864701, null, 328, 328],
[8.8554935, 48.5864701, null, 328, 328],
[8.8555921, 48.5864379, null, 329, 329],
[8.8557414, 48.5866337, null, 331, 331],
[8.8559217, 48.5867635, null, 332, 332],
[8.8593417, 48.5877916, null, 354, 354],
[8.8603334, 48.5881342, null, 360, 360],
[8.8603334, 48.5881342, null, 360, 360],
[8.8617103, 48.5886666, null, 397, 397],
[8.8614461, 48.5893098, null, 420, 420],
[8.8614461, 48.5893098, null, 420, 420],
[8.8609298, 48.5905606, null, 449, 449],
[8.8607587, 48.5908787, null, 456, 456],
[8.8608489, 48.5910978, null, 461, 461],
[8.8609935, 48.5912162, null, 464, 464],
[8.8618154, 48.5917053, null, 480, 480],
[8.8621571, 48.5919791, null, 488, 488],
[8.8628784, 48.5928664, null, 510, 510],
[8.863021, 48.5929783, null, 513, 513],
[8.8635568, 48.5931659, null, 522, 522],
[8.8636355, 48.5932339, null, 524, 524],
[8.8637686, 48.5932395, null, 526, 526],
[8.8642434, 48.5933963, null, 534, 534],
[8.8645076, 48.5934106, null, 537, 537],
[8.8647457, 48.5935727, null, 542, 542],
[8.8657404, 48.5939249, null, 559, 559],
[8.8659472, 48.5940533, null, 563, 563],
[8.8663678, 48.5944668, null, 574, 574],
[8.8661848, 48.5944274, null, 577, 577],
[8.865792, 48.5944349, null, 582, 582],
[8.8656128, 48.5944073, null, 585, 585],
[8.8652311, 48.5942688, null, 591, 591],
[8.8650264, 48.5943828, null, 595, 595],
[8.8647783, 48.5943038, null, 599, 599],
])
})

0 comments on commit 732b193

Please sign in to comment.