From 8b2b976f8ba1d51ffbba26116dc56a88477b0196 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 4 Mar 2024 17:08:03 -0800 Subject: [PATCH] simplify and test decodePolyline --- .../frontend/www-app/src/models/Itinerary.ts | 3 +-- services/frontend/www-app/src/models/Trip.ts | 4 ++-- .../www-app/src/services/TravelmuxClient.ts | 2 -- .../www-app/src/utils/decodePolyline.test.ts | 18 +++++++++++++++ .../decodePath.ts => utils/decodePolyline.ts} | 23 ++++++++----------- 5 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 services/frontend/www-app/src/utils/decodePolyline.test.ts rename services/frontend/www-app/src/{third_party/decodePath.ts => utils/decodePolyline.ts} (81%) diff --git a/services/frontend/www-app/src/models/Itinerary.ts b/services/frontend/www-app/src/models/Itinerary.ts index eddd8b934..8b871866f 100644 --- a/services/frontend/www-app/src/models/Itinerary.ts +++ b/services/frontend/www-app/src/models/Itinerary.ts @@ -10,7 +10,7 @@ import { } from 'src/services/OpenTripPlannerAPI'; import { DistanceUnits, TravelMode } from 'src/utils/models'; import { formatDistance, formatTime } from 'src/utils/format'; -import { decodePolyline } from 'src/third_party/decodePath'; +import { decodePolyline } from 'src/utils/decodePolyline'; export enum ItineraryErrorCode { Other, @@ -206,7 +206,6 @@ export class ItineraryLeg { const points: [number, number][] = decodePolyline( this.raw.legGeometry.points, 5, - false, ); return { type: 'LineString', diff --git a/services/frontend/www-app/src/models/Trip.ts b/services/frontend/www-app/src/models/Trip.ts index 9e0ba6b66..55485a369 100644 --- a/services/frontend/www-app/src/models/Trip.ts +++ b/services/frontend/www-app/src/models/Trip.ts @@ -11,7 +11,7 @@ import { travelModeFromTravelmuxMode, } from 'src/services/TravelmuxClient'; import { formatDistance, formatDuration } from 'src/utils/format'; -import { decodePolyline } from 'src/third_party/decodePath'; +import { decodePolyline } from 'src/utils/decodePolyline'; export default class Trip { raw: TravelmuxItinerary; @@ -76,7 +76,7 @@ export class TripLeg { constructor(raw: TravelmuxLeg) { this.raw = raw; - const points = decodePolyline(this.raw.geometry, 6, false); + const points = decodePolyline(this.raw.geometry, 6); this.geometry = { type: 'LineString', coordinates: points, diff --git a/services/frontend/www-app/src/services/TravelmuxClient.ts b/services/frontend/www-app/src/services/TravelmuxClient.ts index 5bd706bd1..1113f41a5 100644 --- a/services/frontend/www-app/src/services/TravelmuxClient.ts +++ b/services/frontend/www-app/src/services/TravelmuxClient.ts @@ -179,9 +179,7 @@ export class TravelmuxClient { return Err({ transit: true, itineraryError }); } else { const errorBody = await response.json(); - console.log('errorBody', errorBody); const valhallaErrorBody = errorBody['valhalla']; - console.log('Valhalla errorBody', valhallaErrorBody); const routeError = RouteError.fromValhalla(valhallaErrorBody); return Err({ transit: false, routeError }); } diff --git a/services/frontend/www-app/src/utils/decodePolyline.test.ts b/services/frontend/www-app/src/utils/decodePolyline.test.ts new file mode 100644 index 000000000..908b70bd9 --- /dev/null +++ b/services/frontend/www-app/src/utils/decodePolyline.test.ts @@ -0,0 +1,18 @@ +import { expect, test } from '@jest/globals'; + +import { decodePolyline } from './decodePolyline'; + +test('decode', () => { + const input = + 'wpxvyA~w~ihFxIDAhDEnQCnA]nC_BdJ]Va@hASvCorD_@k~DnAkNDoxFAke@a@iKq@cG}AuEaCqG}EwMcQiI{K_CmDcAkAqNgPoYcVqIeFaGiD}IaEwcAi_@yLuDaIeCcD_AeYaIyKa@aEMyOmDmSyH}t@u[cEiCuFkFkDmDuAyAaSoJcNuD}OcAwVMoU?klBCmEUeDqA_C}Bg@{AAuDqHg@aAG_Uf@_YhBqMx@_I`@cM`@oAAiJCiIVgCDqYf@oJJeFP}FFwYPaKTgDDuCByGIyJQeNGeE\\_@DmD`@gCp@cATaB^mDfA_DfAwD`B_MnHcJYmCbB}BtAq_@zYyFrFyBlBw_@~\\cFnEsBsG{Mmb@eBmFcBqFqF}PkAoDaBcFmHyToBaG_CtByJvIoUjS}CnCmC~Bid@z`@{ApAaBxAud@da@YTaBvAwChCwc@j`@iB`BkCbCsDjDeUxR{X|UcCrBqC`Cyg@zc@qIpHOLyEfEoC~B{CnCkPxNaGbFyU~RwCbCcCvBmSrQuCfCwB{CeCoDoKgOEIeMsQoDcFmBmCsAoBwNqSwAoBsK_OcDqEwD{EgAkBiJqL{F}H{GsJwB_DwAsBmCyDaOwSkNeS}@oA}CuE{@oAqHoKcE}FuEf@iEd@yTrB{BRqBRwGf@oE`@kKfAuANqMzAeCXkD`@gUrC_BRcCZc@x@{@dB_CtE{@dBiTtb@oBxDkCjF_HfNaJvQiSja@iBrDwBhEkh@vdA{AzC_AnBg@bAiYll@iAzBuAlB_BxAeBdAkBn@oBXoBBcA?mMCeEAcDAeQGqQGmIEqFAwFE_E?wCCuWIme@KsCAyDA{b@EsIA{@?yDAsFA{DA}C?}NBwO?g\\?yC@wDAiL@sF?aUByD?Y?eH@sCAqD?}FDiC?kA?oOQ_CAaCAeGAuEA_BAmD?wCCyi@YgDCuEA{XImQGsCAqCA_H?sAAaj@Iwz@MeVCiSAeBAiD?qe@MeEAaFA}WI_OEiA?qAAoe@M_LEkA?_C?uC?aEAiF?oLA}EA{B?{D@iH@uE@mb@GeECg`AMqFAy@?_QEoC@oCPgANgANmCl@mC~@iClAeD|AkFhCeGvCaUtKs@\\sHtDgb@lSyL`G{CvAcErBsQ~IgKdFkN~Ec`AhTcB^ib@lJwMvCq@|DUvASpAGZ}CzAyLrCsQhEeE`AuCx@qCjAmCzAiClBuLtI}AxAwA`BuAfBqAnBgX`c@`@vYeDbR]n@_B[O[o@bAy@v@{PbE}B^{Cd@eMlD{@z@eATuCToQtA}ZfAuPRel@Z}_@PexIC'; + const result = decodePolyline(input, 6); + + expect(result.length).toBe(350); + const firstCoord = result[0]; + expect(firstCoord[0]).toBeCloseTo(-122.339216); + expect(firstCoord[1]).toBeCloseTo(47.575836); + + const lastCoord = result[349]; + expect(lastCoord[0]).toBeCloseTo(-122.347199); + expect(lastCoord[1]).toBeCloseTo(47.651048); +}); diff --git a/services/frontend/www-app/src/third_party/decodePath.ts b/services/frontend/www-app/src/utils/decodePolyline.ts similarity index 81% rename from services/frontend/www-app/src/third_party/decodePath.ts rename to services/frontend/www-app/src/utils/decodePolyline.ts index f4f1c3212..e75e74c8f 100644 --- a/services/frontend/www-app/src/third_party/decodePath.ts +++ b/services/frontend/www-app/src/utils/decodePolyline.ts @@ -1,3 +1,5 @@ +// The polline decode method was originally based on the following (though it's changed a bit): + // Copyright (c) 2018 Valhalla contributors // Copyright (c) 2015-2017 Mapillary AB, Mapzen @@ -22,8 +24,6 @@ export function decodePolyline( str: string, precision: number, - // REVIEW: get rid of this bool now that we've normalized the API - latBeforelng: boolean, ): [number, number][] { let index = 0, lat = 0, @@ -31,8 +31,8 @@ export function decodePolyline( shift = 0, result = 0, byte = null, - latitude_change, - longitude_change; + latitudeChange, + longitudeChange; const coordinates: [number, number][] = [], factor = Math.pow(10, precision); @@ -51,7 +51,7 @@ export function decodePolyline( shift += 5; } while (byte >= 0x20); - latitude_change = result & 1 ? ~(result >> 1) : result >> 1; + latitudeChange = result & 1 ? ~(result >> 1) : result >> 1; shift = result = 0; @@ -61,16 +61,13 @@ export function decodePolyline( shift += 5; } while (byte >= 0x20); - longitude_change = result & 1 ? ~(result >> 1) : result >> 1; + longitudeChange = result & 1 ? ~(result >> 1) : result >> 1; - lat += latitude_change; - lng += longitude_change; + lat += latitudeChange; + lng += longitudeChange; - if (latBeforelng) { - coordinates.push([lat / factor, lng / factor]); - } else { - coordinates.push([lng / factor, lat / factor]); - } + const coord: [number, number] = [lng / factor, lat / factor]; + coordinates.push(coord); } return coordinates;