Skip to content

Commit f171bcf

Browse files
committed
try heuristic for long routes
1 parent ba92c6e commit f171bcf

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

src/api/Api.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { LineString } from 'geojson'
1818
import { getTranslation, tr } from '@/translation/Translation'
1919
import * as config from 'config'
2020
import { POIQuery } from '@/pois/AddressParseResult'
21-
import { Coordinate } from '@/utils'
21+
import { getMaxDistance } from '@/stores/QueryStore'
2222

2323
interface ApiProfile {
2424
name: string
@@ -290,6 +290,12 @@ export class ApiImpl implements Api {
290290
if (config.request?.snapPreventions) request.snap_preventions = config.request?.snapPreventions
291291

292292
if (args.customModel) {
293+
const maxDist = getMaxDistance(args.points.map(point => ({ lat: point[1], lng: point[0] })))
294+
// performance improvement for longer distances, but use with caution, as the heuristic result may deviate significantly from the optimum
295+
if (maxDist > 250_000) request['astarbi.epsilon'] = 1.8
296+
else if (maxDist > 150_000) request['astarbi.epsilon'] = 1.6
297+
else if (maxDist > 100_000) request['astarbi.epsilon'] = 1.4
298+
293299
request.custom_model = args.customModel
294300
request['ch.disable'] = true
295301
request['timeout_ms'] = 10000

src/api/graphhopper.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface RoutingRequest {
2828
snap_preventions?: string[]
2929
details?: string[]
3030
custom_model?: CustomModel
31+
'astarbi.epsilon'?: number
3132
}
3233

3334
export interface ErrorResponse {

src/layers/UsePathsLayer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function usePathsLayer(map: Map, paths: Path[], selectedPath: Pat
2525
if (showPaths) {
2626
addUnselectedPathsLayer(
2727
map,
28-
paths.filter(p => p != selectedPath)
28+
paths.filter(p => p != selectedPath),
2929
)
3030
addSelectedPathsLayer(map, selectedPath)
3131
addAccessNetworkLayer(map, selectedPath, queryPoints)
@@ -46,7 +46,7 @@ export default function usePathsLayer(map: Map, paths: Path[], selectedPath: Pat
4646
const viewport = map.getViewport()
4747
if (!viewport) return
4848

49-
viewport.tabIndex = -1 // Make element focusable but not in tab order
49+
viewport.tabIndex = -1 // Make element focusable but not in tab order
5050

5151
viewport.addEventListener('keydown', handleKeyDown)
5252
viewport.addEventListener('keyup', handleKeyUp)

src/stores/QueryStore.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export default class QueryStore extends Store<QueryStoreState> {
308308
private routeIfReady(state: QueryStoreState, zoom: boolean): QueryStoreState {
309309
if (QueryStore.isReadyToRoute(state)) {
310310
let requests
311-
const maxDistance = getMaxDistance(state.queryPoints)
311+
const maxDistance = getMaxDistance(state.queryPoints.map(qp => qp.coordinate))
312312
if (state.customModelEnabled) {
313313
if (maxDistance < 200_000) {
314314
// Use a single request, possibly including alternatives when custom models are enabled.
@@ -482,10 +482,10 @@ function replace<T>(array: T[], compare: { (element: T): boolean }, provider: {
482482
return result
483483
}
484484

485-
function getMaxDistance(queryPoints: QueryPoint[]): number {
485+
export function getMaxDistance(coordinates: Coordinate[]): number {
486486
let max = 0
487-
for (let idx = 1; idx < queryPoints.length; idx++) {
488-
const dist = calcDist(queryPoints[idx - 1].coordinate, queryPoints[idx].coordinate)
487+
for (let idx = 1; idx < coordinates.length; idx++) {
488+
const dist = calcDist(coordinates[idx - 1], coordinates[idx])
489489
max = Math.max(dist, max)
490490
}
491491
return max

0 commit comments

Comments
 (0)