Skip to content

Commit

Permalink
simplify and test decodePolyline
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Mar 5, 2024
1 parent efb073c commit 8b2b976
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
3 changes: 1 addition & 2 deletions services/frontend/www-app/src/models/Itinerary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -206,7 +206,6 @@ export class ItineraryLeg {
const points: [number, number][] = decodePolyline(
this.raw.legGeometry.points,
5,
false,
);
return {
type: 'LineString',
Expand Down
4 changes: 2 additions & 2 deletions services/frontend/www-app/src/models/Trip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions services/frontend/www-app/src/services/TravelmuxClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
18 changes: 18 additions & 0 deletions services/frontend/www-app/src/utils/decodePolyline.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -22,17 +24,15 @@
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,
lng = 0,
shift = 0,
result = 0,
byte = null,
latitude_change,
longitude_change;
latitudeChange,
longitudeChange;
const coordinates: [number, number][] = [],
factor = Math.pow(10, precision);

Expand All @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 8b2b976

Please sign in to comment.