Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update valhalla.js Graphhopper to V7 #948

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configurations/default/env.yml.tmp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ SLACK_WEBHOOK: optional-slack-webhook
GRAPH_HOPPER_KEY: your-graph-hopper-key
# Optional override to use a custom service instead of the graphhopper.com hosted service.
# GRAPH_HOPPER_URL: http://localhost:8989/
# If your'e hosting a version 7 graphhopper instance set this to yes, default is false or not configured
# GRAPH_HOPPER_V7: true
# Optional overrides to use custom service or different api key for certain bounding boxes.
# (uncomment below to enable)
# GRAPH_HOPPER_ALTERNATES:
Expand Down
44 changes: 28 additions & 16 deletions lib/scenario-editor/utils/valhalla.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export async function getSegment (
/**
* Call GraphHopper routing service with lat/lng coordinates.
*
* Example URL: https://graphhopper.com/api/1/route?point=49.932707,11.588051&point=50.3404,11.64705&vehicle=car&debug=true&&type=json
* Example URL V5/6: https://graphhopper.com/api/1/route?point=49.932707,11.588051&point=50.3404,11.64705&vehicle=car&debug=true&type=json
* Example URL V7: https://graphhopper.com/api/1/route?point=49.932707,11.588051&point=50.3404,11.64705&profile=car&debug=true&type=json
*/
export function routeWithGraphHopper (points: Array<LatLng>, avoidMotorways?: boolean): ?Promise<GraphHopperResponse> {
if (points.length < 2) {
Expand Down Expand Up @@ -255,30 +256,41 @@ export function routeWithGraphHopper (points: Array<LatLng>, avoidMotorways?: bo
})
}

// Version 7 of the graphhopper api uses profile instead of vehicle.
let profilecar = false
let profileveh = true
// Only check for definition not the value.
if (process.env.GRAPH_HOPPER_V7) {
profilecar = true
profileveh = false
}
Comment on lines +260 to +266
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the value is always car can we instead adjust params after it is set to either add a profile: car or a vehicle: car depending on the config?

const params = {
key: graphHopperKey,
vehicle: 'car',
debug: true,
type: 'json'
type: 'json',
...(profilecar && { profile: 'car' }),
...(profileveh && { vehicle: 'car' })
}
const locations = points.map(p => (`point=${p.lat},${p.lng}`)).join('&')
// Avoiding motorways requires a POST request with a formatted body
const avoidmotorwaysbody = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this configurable? Also please camel case

'ch.disable': true,
// Custom model disincentives motorways
custom_model: {
'priority': [{
'if': 'road_class == MOTORWAY',
'multiply_by': 0.1
}]
},
debug: params.debug,
points: points.map(p => [p.lng, p.lat]),
...(profilecar && { profile: 'car' }),
...(profileveh && { vehicle: 'car' })
Comment on lines +287 to +288
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-use the dynamic object key from earlier

}
const graphHopperRequest = avoidMotorways
? fetch(`${graphHopperUrl}route?key=${params.key}`,
{
body: JSON.stringify({
'ch.disable': true,
// Custom model disincentives motorways
custom_model: {
'priority': [{
'if': 'road_class == MOTORWAY',
'multiply_by': 0.1
}]
},
debug: params.debug,
points: points.map(p => [p.lng, p.lat]),
profile: params.vehicle
}),
body: JSON.stringify(avoidmotorwaysbody),
headers: {
'Content-Type': 'application/json'
},
Expand Down