Skip to content
This repository has been archived by the owner on Nov 27, 2021. It is now read-only.

Commit

Permalink
Issue #150 ongoing
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwouaiebe committed Jan 15, 2021
1 parent fb37512 commit 76e1aa3
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 597 deletions.
2 changes: 1 addition & 1 deletion buildNumber.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "buildNumber" : "00004"}
{ "buildNumber" : "00166"}
8 changes: 3 additions & 5 deletions src/core/FileCompactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Tests ...
import { polyline } from '../polyline/Polyline.js';
import { theTravelNotesData } from '../data/TravelNotesData.js';
import { newTravel } from '../data/Travel.js';
import { ROUTE_EDITION_STATUS, ELEV, ZERO, ONE, INVALID_OBJ_ID } from '../util/Constants.js';
import { ROUTE_EDITION_STATUS, ELEV, ZERO, ONE, INVALID_OBJ_ID, LAT_LNG } from '../util/Constants.js';

/**
@------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -63,8 +63,6 @@ import { ROUTE_EDITION_STATUS, ELEV, ZERO, ONE, INVALID_OBJ_ID } from '../util/C

function ourNewFileCompactor ( ) {

const POLYLINE_PRECISION = 6;

/**
@--------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -92,7 +90,7 @@ function ourNewFileCompactor ( ) {
}
);
compressedItineraryPoints.latLngs =
polyline.encode ( compressedItineraryPoints.latLngs, POLYLINE_PRECISION );
polyline.encode ( compressedItineraryPoints.latLngs, LAT_LNG.fixed );
routeObject.itinerary.itineraryPoints = compressedItineraryPoints;

// routeObject.itinerary.maneuvers = [];
Expand All @@ -112,7 +110,7 @@ function ourNewFileCompactor ( ) {

function myDecompressRoute ( routeObject ) {
routeObject.itinerary.itineraryPoints.latLngs =
polyline.decode ( routeObject.itinerary.itineraryPoints.latLngs, POLYLINE_PRECISION );
polyline.decode ( routeObject.itinerary.itineraryPoints.latLngs, LAT_LNG.fixed, false );
let decompressedItineraryPoints = [];
let latLngsCounter = ZERO;
routeObject.itinerary.itineraryPoints.latLngs.forEach (
Expand Down
159 changes: 17 additions & 142 deletions src/graphHopperRouteProvider/GraphHopperRouteProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,15 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/* eslint camelcase: "off" */
/* eslint no-bitwise: "off" */
import { polyline } from '../polyline/Polyline.js';
import { ZERO, LAT_LNG, HTTP_STATUS_OK } from '../util/Constants.js';

function newGraphHopperRouteProvider ( ) {

const ZERO = 0;
const ONE = 1;

const MY_LAT_LNG_ROUND = 6;
const GRAPHHOPPER_LAT_LNG_ROUND = 5;
const FOUR = 4;
const METERS_IN_KILOMETERS = 1000;

const NUMBER5 = 5;
const NUMBER31 = 0x1f;
const NUMBER32 = 0x20;
const NUMBER63 = 0x3f;
const NUMBER100 = 100;
const NUMBER1emin5 = 1e-5;

const LAT = 0;
const LNG = 1;
const ELEV = 2;
Expand All @@ -58,67 +48,6 @@ function newGraphHopperRouteProvider ( ) {
'kRoundaboutRight' // USE_ROUNDABOUT = 6
];

/*
--- myDecodePath function -----------------------------------------------------------------------------------------
Adapted from https://github.com/graphhopper/directions-api-js-client/blob/master/src/GHUtil.js
See GHUtil.prototype.decodePath
See also https://developers.google.com/maps/documentation/utilities/polylinealgorithm
Some adaptation for eslint and inverted lat and lng in the results...
-------------------------------------------------------------------------------------------------------------------
*/

function myDecodePath ( encoded, is3D ) {
let len = encoded.length;
let index = ZERO;
let array = [];
let lat = ZERO;
let lng = ZERO;
let ele = ZERO;

while ( index < len ) {
let byte = null;
let shift = ZERO;
let result = ZERO;
do {
byte = encoded.charCodeAt ( index ++ ) - NUMBER63;
result |= ( byte & NUMBER31 ) << shift;
shift += NUMBER5;
} while ( NUMBER32 <= byte );
let deltaLat = ( ( result & ONE ) ? ~ ( result >> ONE ) : ( result >> ONE ) );
lat += deltaLat;

shift = ZERO;
result = ZERO;
do {
byte = encoded.charCodeAt ( index ++ ) - NUMBER63;
result |= ( byte & NUMBER31 ) << shift;
shift += NUMBER5;
} while ( NUMBER32 <= byte );
let deltaLon = ( ( result & ONE ) ? ~ ( result >> ONE ) : ( result >> ONE ) );
lng += deltaLon;

if ( is3D ) {
shift = ZERO;
result = ZERO;
do {
byte = encoded.charCodeAt ( index ++ ) - NUMBER63;
result |= ( byte & NUMBER31 ) << shift;
shift += NUMBER5;
} while ( NUMBER32 <= byte );
let deltaEle = ( ( result & ONE ) ? ~ ( result >> ONE ) : ( result >> ONE ) );
ele += deltaEle;
array.push ( [ lat * NUMBER1emin5, lng * NUMBER1emin5, ele / NUMBER100 ] );
}
else {
array.push ( [ lat * NUMBER1emin5, lng * NUMBER1emin5 ] );
}
}

return array;
}

/*
--- myParseResponse function --------------------------------------------------------------------------------------
Expand All @@ -140,8 +69,8 @@ function newGraphHopperRouteProvider ( ) {
myRoute.itinerary.descent = ZERO;
response.paths.forEach (
path => {
path.points = myDecodePath ( path.points, true );
path.snapped_waypoints = myDecodePath ( path.snapped_waypoints, true );
path.points = polyline.decode ( path.points, GRAPHHOPPER_LAT_LNG_ROUND, true );
path.snapped_waypoints = polyline.decode ( path.snapped_waypoints, GRAPHHOPPER_LAT_LNG_ROUND, true ); // eslint-disable-line
let itineraryPoints = [];
for ( let pointsCounter = ZERO; pointsCounter < path.points.length; pointsCounter ++ ) {
let itineraryPoint = window.L.travelNotes.itineraryPoint;
Expand Down Expand Up @@ -200,8 +129,8 @@ function newGraphHopperRouteProvider ( ) {
wayPointsString = wayPointsString ? wayPointsString + '&' : '';
wayPointsString +=
'point=' +
wayPoint.lat.toFixed ( MY_LAT_LNG_ROUND ) + ',' +
wayPoint.lng.toFixed ( MY_LAT_LNG_ROUND );
wayPoint.lat.toFixed ( LAT_LNG.fixed ) + ',' +
wayPoint.lng.toFixed ( LAT_LNG.fixed );
}
);

Expand All @@ -226,78 +155,24 @@ function newGraphHopperRouteProvider ( ) {
}

/*
--- myGetXHRJsonPromise function ----------------------------------------------------------------------------------
This function ...
--- myGetRoute function -------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
*/

function myGetXHRJsonPromise ( url, requestHeaders ) {

/*
--- jsonRequest function --------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
*/

function jsonRequest ( onOk, onError ) {

const READY_STATE_DONE = 4;
const HTTP_STATUS_OK = 200;
const HTTP_STATUS_ERR = 400;
const REQUEST_TIME_OUT = 15000;

let xmlHttpRequest = new XMLHttpRequest ( );
xmlHttpRequest.timeout = REQUEST_TIME_OUT;

xmlHttpRequest.onreadystatechange = function ( ) {
if ( READY_STATE_DONE === xmlHttpRequest.readyState ) {
if ( HTTP_STATUS_OK === xmlHttpRequest.status ) {
let response = null;
try {
response = JSON.parse ( xmlHttpRequest.responseText );
onOk ( response );
}
catch ( err ) {
onError ( new Error ( 'JSON parsing error. File : ' + xmlHttpRequest.responseURL ) );
}
}
else if ( HTTP_STATUS_ERR <= xmlHttpRequest.status ) {
onError (
new Error ( 'Error HTTP ' + xmlHttpRequest.status + ' ' + xmlHttpRequest.statusText )
);
function myGetRoute ( onOk, onError ) {
fetch ( myGetUrl ( ) )
.then (
response => {
if ( HTTP_STATUS_OK === response.status && response.ok ) {
response.json ( )
.then ( result => myParseResponse ( result, onOk, onError ) );
}
else {
onError ( new Error ( 'Error XMLHttpRequest - File : ' + xmlHttpRequest.responseURL ) );
onError ( new Error ( 'Invalid status ' + response.status ) );
}
}
};
xmlHttpRequest.open ( 'GET', url, true );
if ( requestHeaders ) {
requestHeaders.forEach (
header => xmlHttpRequest.setRequestHeader ( header.headerName, header.headerValue )
);
}
xmlHttpRequest.overrideMimeType ( 'application/json' );
xmlHttpRequest.send ( null );
}

return new Promise ( jsonRequest );
}

/*
--- myGetRoute function -------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
*/

function myGetRoute ( onOk, onError ) {

myGetXHRJsonPromise ( myGetUrl ( ) )
.then ( response => myParseResponse ( response, onOk, onError ) )
.catch ( err => onError ( err ) );

);
}

/*
Expand Down

0 comments on commit 76e1aa3

Please sign in to comment.