Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Nirwing authored and Ole Nirwing committed Oct 11, 2018
0 parents commit 8ebc146
Show file tree
Hide file tree
Showing 445 changed files with 66,452 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.js
EXPOSE 8082
23 changes: 23 additions & 0 deletions bearingCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function toRadians(degrees) {
return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
function toDegrees(radians) {
return radians * 180 / Math.PI;
}


exports.bearing = function(startLat, startLng, destLat, destLng){
startLat = toRadians(startLat);
startLng = toRadians(startLng);
destLat = toRadians(destLat);
destLng = toRadians(destLng);

y = Math.sin(destLng - startLng) * Math.cos(destLat);
x = Math.cos(startLat) * Math.sin(destLat) -
Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
brng = Math.atan2(y, x);
brng = toDegrees(brng);
return (((brng + 360) % 360) | 0);
}
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//Load express module with `require` directive
var express = require('express')
var proxy = require('express-http-proxy');
var proxyService = express();

var mapper = require('./mapper')
proxyService.use(proxy("https://graphhopper.com",{
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
data = JSON.parse(proxyResData.toString('utf8'));
var mapBoxResponse = mapper.map(data)
return JSON.stringify(mapBoxResponse);
}
})
)
proxyService.listen(3000)
181 changes: 181 additions & 0 deletions mapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
var polyline = require('@mapbox/polyline')
var bearingCalc = require('bearingCalc');

exports.map = function (jsonRes) {
var paths = jsonRes.paths[0];
var allCoordinatesGEO = paths.points.coordinates
var allCoordinates = getAdaptedCoordinates(paths.points.coordinates);
var steps = getSteps(paths,allCoordinates,allCoordinatesGEO);
var mapBoxResponse =
{
"distance":paths.distance,
"duration":paths.time / 1000,
"geometry":polyline.encode(allCoordinates),
"weight": paths.time/1000,
"weight_name":"routability",
"legs": [{
"distance":paths.distance,
"duration":paths.time/1000,
"summary":"",
"steps": steps,
}],
"routeOptions": getRouteOptions(paths)
}

return mapBoxResponse;
};

function getAdaptedCoordinates(paths){
var allCoordinates = paths
var index;
var len;
var newCoordinates = [];
for (index = 0, len = allCoordinates.length;index < len; ++index){
var pair = allCoordinates[index];
var x = pair[1] * 10
var y = pair[0] * 10
newCoordinates[index] = [x,y];
}
return newCoordinates;
}


function getMapboxModifier(sign){
var modifier = "straight";
switch(sign){
case -7:
modifier = "straight"
break;
case -3:
modifier = "sharp left";
break;
case -2:
modifier = "left";
break;
case -1:
modifier = "slight left";
break;
case 0:
modifier = "straight";
break;
case 1:
modifier = "slight right";
break;
case 2:
modifier = "right";
break;
case 3:
modifier = "sharp right";
break;
case 6:
modifier = "straight";
break;
case 7:
modifier = "straight";
break;
}
return modifier;
}


function getSteps(paths,allCoordinates, allCoordinatesGEO){
var steps = []
var index;
var len;
for (index = 0, len = paths.instructions.length; index < len; ++index){
var instruction = paths.instructions[index]

var nextInstruction = instruction;
var type = "";
if (paths.instructions.indexOf(instruction) < paths.instructions.length - 1){
nextInstruction = paths.instructions[paths.instructions.indexOf(instruction)+1]
if(paths.instructions.indexOf(instruction) == 0){
type = "depart";
} else {type = "turn";}
} else{ type = "arrive";}

var sectionPoints = []
sectionPoints.push(allCoordinates[instruction.interval[0]])
sectionPoints.push(allCoordinates[instruction.interval[1]])

var bearingBfP1 = allCoordinatesGEO[instruction.interval[0]]
var bearingBfP2 = allCoordinatesGEO[instruction.interval[1]]
var bearingBf = bearingCalc.bearing(bearingBfP1[0],bearingBfP1[1],bearingBfP2[0],bearingBfP2[1])
var bearingAfP1 = allCoordinatesGEO[nextInstruction.interval[0]]
var bearingAfP2 = allCoordinatesGEO[nextInstruction.interval[1]]
var bearingAf = bearingCalc.bearing(bearingAfP1[0],bearingAfP1[1],bearingAfP2[0],bearingAfP2[1])
console.log(bearingBf,bearingAf)
var step = {
"name":instruction.street_name,
"duration":instruction.time / 1000,
"weight": instruction.time/1000,
"distance":instruction.distance,
"geometry": polyline.encode(sectionPoints),
"driving_side":"right",
"mode":"driving",
"maneuver":{
"bearing_before": bearingBf,
"bearing_after": bearingAf,
"location":allCoordinatesGEO[instruction.interval[0]],
"modifier": getMapboxModifier(instruction.sign),
"type": type,
"instruction":instruction.text,
},
"intersections":[{
"location":[0,0]
}],
"voiceInstructions":[],
"bannerInstructions":[
{
"distanceAlongGeometry":instruction.distance,
"primary":{
"text": nextInstruction.street_name,
"type": "text",
"modifier": getMapboxModifier(nextInstruction.sign), //////// ADD getMapboxModifier
"components":[
{
"text": nextInstruction.street_name,
"type": "text"
}

]
}
}
],
}
steps.push(step);
}
return steps;
}

function generateUuid(){
var id = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 25; i++)
id += possible.charAt(Math.floor(Math.random() * possible.length));
console.log(id)
return id;
}

function getRouteOptions(paths){
var routeOptions = {
"baseUrl":"https://api.mapbox.com",
"user":"mapbox",
"profile":"driving",
"coordinates":paths.snapped_waypoints.coordinates,
"language":"de",
"bearings":";",
"continueStraight":true,
"roundaboutExits":true,
"geometries":"polyline",
"overview":"full",
"steps":true,
"annotations":"",
"voiceInstructions":true,
"bannerInstructions":true,
"voiceUnits":"metric",
"accessToken":"pk.eyJ1Ijoib2xlbmlyd2luZyIsImEiOiJjam1xM21vaWQwdGdqM3ZwbmM5Z3A0Mjl1In0.GV0vOW4dT8uWB7wYwj9AoQ",
"requestUuid": generateUuid()
}
return routeOptions;
}
1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/polyline

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions node_modules/@mapbox/polyline/.eslintrc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions node_modules/@mapbox/polyline/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions node_modules/@mapbox/polyline/API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/@mapbox/polyline/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions node_modules/@mapbox/polyline/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node_modules/@mapbox/polyline/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions node_modules/@mapbox/polyline/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ebc146

Please sign in to comment.