@osmix/router 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +149 -0
- package/dist/src/algorithms/bidirectional.d.ts +19 -0
- package/dist/src/algorithms/bidirectional.d.ts.map +1 -0
- package/dist/src/algorithms/bidirectional.js +128 -0
- package/dist/src/algorithms/bidirectional.js.map +1 -0
- package/dist/src/algorithms/index.d.ts +17 -0
- package/dist/src/algorithms/index.d.ts.map +1 -0
- package/dist/src/algorithms/index.js +19 -0
- package/dist/src/algorithms/index.js.map +1 -0
- package/dist/src/algorithms/shortest-path.d.ts +24 -0
- package/dist/src/algorithms/shortest-path.d.ts.map +1 -0
- package/dist/src/algorithms/shortest-path.js +115 -0
- package/dist/src/algorithms/shortest-path.js.map +1 -0
- package/dist/src/binary-heap.d.ts +40 -0
- package/dist/src/binary-heap.d.ts.map +1 -0
- package/dist/src/binary-heap.js +122 -0
- package/dist/src/binary-heap.js.map +1 -0
- package/dist/src/graph.d.ts +135 -0
- package/dist/src/graph.d.ts.map +1 -0
- package/dist/src/graph.js +339 -0
- package/dist/src/graph.js.map +1 -0
- package/dist/src/index.d.ts +41 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +41 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/router.d.ts +55 -0
- package/dist/src/router.d.ts.map +1 -0
- package/dist/src/router.js +189 -0
- package/dist/src/router.js.map +1 -0
- package/dist/src/types.d.ts +118 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +5 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils.d.ts +20 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +101 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/test/algorithms.test.d.ts +2 -0
- package/dist/test/algorithms.test.d.ts.map +1 -0
- package/dist/test/algorithms.test.js +382 -0
- package/dist/test/algorithms.test.js.map +1 -0
- package/dist/test/router.bench.d.ts +2 -0
- package/dist/test/router.bench.d.ts.map +1 -0
- package/dist/test/router.bench.js +191 -0
- package/dist/test/router.bench.js.map +1 -0
- package/dist/test/router.test.d.ts +2 -0
- package/dist/test/router.test.d.ts.map +1 -0
- package/dist/test/router.test.js +515 -0
- package/dist/test/router.test.js.map +1 -0
- package/package.json +51 -0
- package/src/algorithms/bidirectional.ts +147 -0
- package/src/algorithms/index.ts +23 -0
- package/src/algorithms/shortest-path.ts +149 -0
- package/src/binary-heap.ts +147 -0
- package/src/graph.ts +414 -0
- package/src/index.ts +41 -0
- package/src/router.ts +262 -0
- package/src/types.ts +138 -0
- package/src/utils.ts +114 -0
- package/test/algorithms.test.ts +466 -0
- package/test/router.bench.ts +257 -0
- package/test/router.test.ts +738 -0
- package/tsconfig.json +9 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# @osmix/router
|
|
2
|
+
|
|
3
|
+
`@osmix/router` builds a routable street network from OSM data and provides routing functionality to find paths between coordinates.
|
|
4
|
+
|
|
5
|
+
## Highlights
|
|
6
|
+
|
|
7
|
+
- Builds a directed graph from OSM ways and nodes
|
|
8
|
+
- Configurable highway type filtering
|
|
9
|
+
- Multiple routing algorithms (Dijkstra, A*, bidirectional search)
|
|
10
|
+
- Support for both distance and time-based routing
|
|
11
|
+
- Returns detailed route information including coordinates, way IDs, and node IDs
|
|
12
|
+
- Serializable graph format for Web Worker support
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
bun install @osmix/router
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { RoutingGraph, Router, findNearestNodeOnGraph } from "@osmix/router"
|
|
24
|
+
import { Osm } from "@osmix/core"
|
|
25
|
+
|
|
26
|
+
const osm = new Osm()
|
|
27
|
+
// ... load OSM data into osm ...
|
|
28
|
+
|
|
29
|
+
// Build routing graph
|
|
30
|
+
const graph = new RoutingGraph(osm)
|
|
31
|
+
|
|
32
|
+
// Snap coordinates to nearest routable nodes
|
|
33
|
+
const from = findNearestNodeOnGraph(osm, graph, [-73.989, 40.733], 0.5)
|
|
34
|
+
const to = findNearestNodeOnGraph(osm, graph, [-73.988, 40.734], 0.5)
|
|
35
|
+
|
|
36
|
+
if (from && to) {
|
|
37
|
+
const router = new Router(osm, graph)
|
|
38
|
+
const path = router.route(from.nodeIndex, to.nodeIndex)
|
|
39
|
+
|
|
40
|
+
if (path) {
|
|
41
|
+
const result = router.buildResult(path)
|
|
42
|
+
console.log(result.coordinates) // Array of [lon, lat] coordinates
|
|
43
|
+
console.log(result.wayIndexes) // Array of way indexes used
|
|
44
|
+
console.log(result.nodeIndexes) // Array of node indexes for turns
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `RoutingGraph`
|
|
52
|
+
|
|
53
|
+
Build and manage a routing graph from OSM data. The graph uses a CSR (Compressed Sparse Row) format for efficient memory usage and cache locality.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { RoutingGraph, defaultHighwayFilter } from "@osmix/router"
|
|
57
|
+
|
|
58
|
+
// Build from OSM data
|
|
59
|
+
const graph = new RoutingGraph(osm, defaultHighwayFilter)
|
|
60
|
+
|
|
61
|
+
// Properties
|
|
62
|
+
graph.size // Number of routable nodes
|
|
63
|
+
graph.edges // Total edge count
|
|
64
|
+
graph.isRouteable(nodeIndex) // Check if node is routable
|
|
65
|
+
graph.isIntersection(nodeIndex) // Check if node is an intersection
|
|
66
|
+
graph.getEdges(nodeIndex) // Get outgoing edges from node
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### `findNearestRoutableNode(osm, point, maxKm)`
|
|
70
|
+
|
|
71
|
+
Snap a geographic coordinate to the nearest routable node.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const nearest = graph.findNearestRoutableNode(osm, [-73.989, 40.733], 0.5)
|
|
75
|
+
if (nearest) {
|
|
76
|
+
console.log(nearest.nodeIndex) // Internal node index
|
|
77
|
+
console.log(nearest.coordinates) // Snapped [lon, lat]
|
|
78
|
+
console.log(nearest.distance) // Distance from input point (meters)
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Constructor parameters:**
|
|
83
|
+
|
|
84
|
+
- `osm` - The `@osmix/core` dataset.
|
|
85
|
+
- `filter` - Optional function `(tags?) => boolean` to select routable ways. Default: common vehicle highways.
|
|
86
|
+
- `defaultSpeeds` - Optional speed limits (km/h) by highway type.
|
|
87
|
+
|
|
88
|
+
#### Serialization (Web Worker support)
|
|
89
|
+
|
|
90
|
+
`RoutingGraph` can be serialized and transferred between Web Workers:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
// Build graph and get transferables
|
|
94
|
+
const graph = new RoutingGraph(osm)
|
|
95
|
+
const transferables = graph.transferables()
|
|
96
|
+
|
|
97
|
+
// Transfer to worker (zero-copy with SharedArrayBuffer)
|
|
98
|
+
worker.postMessage(transferables)
|
|
99
|
+
|
|
100
|
+
// Reconstruct in worker
|
|
101
|
+
const reconstructed = new RoutingGraph(transferables)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The `transferables()` method returns an object containing:
|
|
105
|
+
|
|
106
|
+
- `nodeCount`, `edgeCount` - Graph dimensions
|
|
107
|
+
- `edgeOffsets`, `edgeTargets`, `edgeWayIndexes` - CSR structure
|
|
108
|
+
- `edgeDistances`, `edgeTimes` - Edge weights
|
|
109
|
+
- `routableBits`, `intersectionBits` - Node flags
|
|
110
|
+
|
|
111
|
+
### `Router`
|
|
112
|
+
|
|
113
|
+
High-level routing interface.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const router = new Router(osm, graph, { algorithm: "astar", metric: "time" })
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Methods:**
|
|
120
|
+
|
|
121
|
+
- `route(fromNodeIndex, toNodeIndex, options?)` - Find path between nodes. Returns `PathSegment[]` or `null`.
|
|
122
|
+
- `buildResult(path)` - Convert path to `RouteResult` with coordinates and metadata.
|
|
123
|
+
|
|
124
|
+
**Options:**
|
|
125
|
+
|
|
126
|
+
- `algorithm` - `"dijkstra"` | `"astar"` | `"bidirectional"` (default: `"astar"`)
|
|
127
|
+
- `metric` - `"distance"` | `"time"` (default: `"distance"`)
|
|
128
|
+
|
|
129
|
+
### Algorithms
|
|
130
|
+
|
|
131
|
+
| Algorithm | Optimal? | Speed | Best For |
|
|
132
|
+
| --------------- | -------- | ------- | ------------------------------------- |
|
|
133
|
+
| `dijkstra` | Yes | Slower | When you need guaranteed shortest path |
|
|
134
|
+
| `astar` | Yes | Fast | Point-to-point queries (default) |
|
|
135
|
+
| `bidirectional` | No | Fastest | Quick connectivity checks |
|
|
136
|
+
|
|
137
|
+
## Related Packages
|
|
138
|
+
|
|
139
|
+
- [`@osmix/core`](../core/README.md) - In-memory OSM index with spatial queries.
|
|
140
|
+
- [`@osmix/shared`](../shared/README.md) - Haversine distance and coordinate utilities.
|
|
141
|
+
- [`osmix`](../osmix/README.md) - High-level API with worker support for routing.
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
- `bun run test packages/router`
|
|
146
|
+
- `bun run lint packages/router`
|
|
147
|
+
- `bun run typecheck packages/router`
|
|
148
|
+
|
|
149
|
+
Run `bun run check` at the repo root before publishing to ensure formatting, lint, and type coverage.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bidirectional breadth-first search algorithm.
|
|
3
|
+
*
|
|
4
|
+
* Searches from both start and end simultaneously, terminating when the
|
|
5
|
+
* frontiers meet. Very fast for finding any path, but does not guarantee
|
|
6
|
+
* the shortest path. Use Dijkstra or A* when optimality is required.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { RoutingAlgorithmFn } from "../types";
|
|
11
|
+
/**
|
|
12
|
+
* Bidirectional BFS - fast path finding from both ends.
|
|
13
|
+
*
|
|
14
|
+
* Uses breadth-first search from both start and end, terminating when
|
|
15
|
+
* the frontiers meet. Very fast for finding any path, though not always
|
|
16
|
+
* the optimal shortest path. Use Dijkstra or A* if optimality is required.
|
|
17
|
+
*/
|
|
18
|
+
export declare const bidirectional: RoutingAlgorithmFn;
|
|
19
|
+
//# sourceMappingURL=bidirectional.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bidirectional.d.ts","sourceRoot":"","sources":["../../../src/algorithms/bidirectional.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAe,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAE/D;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,EAAE,kBA+H3B,CAAA"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bidirectional breadth-first search algorithm.
|
|
3
|
+
*
|
|
4
|
+
* Searches from both start and end simultaneously, terminating when the
|
|
5
|
+
* frontiers meet. Very fast for finding any path, but does not guarantee
|
|
6
|
+
* the shortest path. Use Dijkstra or A* when optimality is required.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Bidirectional BFS - fast path finding from both ends.
|
|
12
|
+
*
|
|
13
|
+
* Uses breadth-first search from both start and end, terminating when
|
|
14
|
+
* the frontiers meet. Very fast for finding any path, though not always
|
|
15
|
+
* the optimal shortest path. Use Dijkstra or A* if optimality is required.
|
|
16
|
+
*/
|
|
17
|
+
export const bidirectional = (graph, start, end, getWeight) => {
|
|
18
|
+
if (start === end)
|
|
19
|
+
return [{ nodeIndex: start, cost: 0 }];
|
|
20
|
+
// Forward search state
|
|
21
|
+
const fDist = new Map();
|
|
22
|
+
const fPrev = new Map();
|
|
23
|
+
const fQueue = [start];
|
|
24
|
+
// Backward search state
|
|
25
|
+
const bDist = new Map();
|
|
26
|
+
const bPrev = new Map();
|
|
27
|
+
const bQueue = [end];
|
|
28
|
+
fDist.set(start, 0);
|
|
29
|
+
bDist.set(end, 0);
|
|
30
|
+
let meetNode = null;
|
|
31
|
+
while (fQueue.length > 0 && bQueue.length > 0) {
|
|
32
|
+
// Expand forward
|
|
33
|
+
if (fQueue.length > 0) {
|
|
34
|
+
const current = fQueue.shift();
|
|
35
|
+
const currentD = fDist.get(current);
|
|
36
|
+
// Check if backward search reached this node
|
|
37
|
+
if (bDist.has(current)) {
|
|
38
|
+
meetNode = current;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
for (const edge of graph(current)) {
|
|
42
|
+
const neighbor = edge.targetNodeIndex;
|
|
43
|
+
if (fDist.has(neighbor))
|
|
44
|
+
continue;
|
|
45
|
+
const newD = currentD + getWeight(edge);
|
|
46
|
+
fDist.set(neighbor, newD);
|
|
47
|
+
fPrev.set(neighbor, {
|
|
48
|
+
nodeIndex: neighbor,
|
|
49
|
+
wayIndex: edge.wayIndex,
|
|
50
|
+
previousNodeIndex: current,
|
|
51
|
+
cost: newD,
|
|
52
|
+
});
|
|
53
|
+
fQueue.push(neighbor);
|
|
54
|
+
if (bDist.has(neighbor)) {
|
|
55
|
+
meetNode = neighbor;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (meetNode !== null)
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
// Expand backward
|
|
63
|
+
if (bQueue.length > 0) {
|
|
64
|
+
const current = bQueue.shift();
|
|
65
|
+
const currentD = bDist.get(current);
|
|
66
|
+
// Check if forward search reached this node
|
|
67
|
+
if (fDist.has(current)) {
|
|
68
|
+
meetNode = current;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
for (const edge of graph(current)) {
|
|
72
|
+
const neighbor = edge.targetNodeIndex;
|
|
73
|
+
if (bDist.has(neighbor))
|
|
74
|
+
continue;
|
|
75
|
+
const newD = currentD + getWeight(edge);
|
|
76
|
+
bDist.set(neighbor, newD);
|
|
77
|
+
bPrev.set(neighbor, {
|
|
78
|
+
nodeIndex: neighbor,
|
|
79
|
+
wayIndex: edge.wayIndex,
|
|
80
|
+
previousNodeIndex: current,
|
|
81
|
+
cost: newD,
|
|
82
|
+
});
|
|
83
|
+
bQueue.push(neighbor);
|
|
84
|
+
if (fDist.has(neighbor)) {
|
|
85
|
+
meetNode = neighbor;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (meetNode !== null)
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (meetNode === null)
|
|
94
|
+
return null;
|
|
95
|
+
// Build forward path: start -> meet
|
|
96
|
+
const forward = [];
|
|
97
|
+
let curr = meetNode;
|
|
98
|
+
while (curr !== undefined && curr !== start) {
|
|
99
|
+
const seg = fPrev.get(curr);
|
|
100
|
+
if (!seg)
|
|
101
|
+
break;
|
|
102
|
+
forward.unshift(seg);
|
|
103
|
+
curr = seg.previousNodeIndex;
|
|
104
|
+
}
|
|
105
|
+
forward.unshift({ nodeIndex: start, cost: 0 });
|
|
106
|
+
// Build backward path: meet -> end (reverse direction)
|
|
107
|
+
// bDist represents distance FROM end TO node, so it decreases as we approach end.
|
|
108
|
+
// Correct cost = forward cost to meet + (backward dist at meet - backward dist at curr)
|
|
109
|
+
const meetForwardCost = fDist.get(meetNode);
|
|
110
|
+
const meetBackwardCost = bDist.get(meetNode);
|
|
111
|
+
curr = meetNode;
|
|
112
|
+
while (curr !== undefined && curr !== end) {
|
|
113
|
+
const seg = bPrev.get(curr);
|
|
114
|
+
if (!seg)
|
|
115
|
+
break;
|
|
116
|
+
curr = seg.previousNodeIndex;
|
|
117
|
+
if (curr !== undefined) {
|
|
118
|
+
forward.push({
|
|
119
|
+
nodeIndex: curr,
|
|
120
|
+
wayIndex: seg.wayIndex,
|
|
121
|
+
previousNodeIndex: seg.nodeIndex,
|
|
122
|
+
cost: meetForwardCost + meetBackwardCost - (bDist.get(curr) ?? 0),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return forward;
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=bidirectional.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bidirectional.js","sourceRoot":"","sources":["../../../src/algorithms/bidirectional.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,aAAa,GAAuB,CAChD,KAAK,EACL,KAAK,EACL,GAAG,EACH,SAAS,EACR,EAAE;IACH,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;IAEzD,uBAAuB;IACvB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA;IACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAuB,CAAA;IAC5C,MAAM,MAAM,GAAa,CAAC,KAAK,CAAC,CAAA;IAEhC,wBAAwB;IACxB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA;IACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAuB,CAAA;IAC5C,MAAM,MAAM,GAAa,CAAC,GAAG,CAAC,CAAA;IAE9B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;IACnB,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;IAEjB,IAAI,QAAQ,GAAkB,IAAI,CAAA;IAElC,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/C,iBAAiB;QACjB,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,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,CAAC,OAAO,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;IAE9C,uDAAuD;IACvD,kFAAkF;IAClF,wFAAwF;IACxF,MAAM,eAAe,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAA;IAC5C,MAAM,gBAAgB,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAA;IAC7C,IAAI,GAAG,QAAQ,CAAA;IACf,OAAO,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;QAC3C,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAC3B,IAAI,CAAC,GAAG;YAAE,MAAK;QACf,IAAI,GAAG,GAAG,CAAC,iBAAiB,CAAA;QAC5B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,CAAC,IAAI,CAAC;gBACZ,SAAS,EAAE,IAAI;gBACf,QAAQ,EAAE,GAAG,CAAC,QAAQ;gBACtB,iBAAiB,EAAE,GAAG,CAAC,SAAS;gBAChC,IAAI,EAAE,eAAe,GAAG,gBAAgB,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACjE,CAAC,CAAA;QACH,CAAC;IACF,CAAC;IAED,OAAO,OAAO,CAAA;AACf,CAAC,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routing algorithm implementations.
|
|
3
|
+
*
|
|
4
|
+
* Provides three algorithms:
|
|
5
|
+
* - `dijkstra`: Optimal shortest path, explores all directions equally.
|
|
6
|
+
* - `astar`: Optimal with heuristic guidance, faster for point-to-point.
|
|
7
|
+
* - `bidirectional`: Fast BFS from both ends, finds *a* path (not always optimal).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { RoutingAlgorithm, RoutingAlgorithmFn } from "../types";
|
|
12
|
+
import { bidirectional } from "./bidirectional";
|
|
13
|
+
import { astar, dijkstra } from "./shortest-path";
|
|
14
|
+
export { astar, bidirectional, dijkstra };
|
|
15
|
+
export type { RoutingAlgorithmFn };
|
|
16
|
+
export declare const routingAlgorithms: Record<RoutingAlgorithm, RoutingAlgorithmFn>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAA;AACzC,YAAY,EAAE,kBAAkB,EAAE,CAAA;AAElC,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,gBAAgB,EAAE,kBAAkB,CAI1E,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routing algorithm implementations.
|
|
3
|
+
*
|
|
4
|
+
* Provides three algorithms:
|
|
5
|
+
* - `dijkstra`: Optimal shortest path, explores all directions equally.
|
|
6
|
+
* - `astar`: Optimal with heuristic guidance, faster for point-to-point.
|
|
7
|
+
* - `bidirectional`: Fast BFS from both ends, finds *a* path (not always optimal).
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { bidirectional } from "./bidirectional";
|
|
12
|
+
import { astar, dijkstra } from "./shortest-path";
|
|
13
|
+
export { astar, bidirectional, dijkstra };
|
|
14
|
+
export const routingAlgorithms = {
|
|
15
|
+
dijkstra,
|
|
16
|
+
astar,
|
|
17
|
+
bidirectional,
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/algorithms/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAA;AAGzC,MAAM,CAAC,MAAM,iBAAiB,GAAiD;IAC9E,QAAQ;IACR,KAAK;IACL,aAAa;CACb,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortest path algorithms (Dijkstra and A*).
|
|
3
|
+
*
|
|
4
|
+
* Both algorithms guarantee optimal paths. A* uses a heuristic to guide
|
|
5
|
+
* the search toward the destination, making it faster for most queries.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { RoutingAlgorithmFn } from "../types";
|
|
10
|
+
/**
|
|
11
|
+
* Dijkstra's algorithm - optimal shortest path without heuristic guidance.
|
|
12
|
+
* Explores nodes in order of increasing distance from start.
|
|
13
|
+
*/
|
|
14
|
+
export declare const dijkstra: RoutingAlgorithmFn;
|
|
15
|
+
/**
|
|
16
|
+
* A* algorithm - uses heuristic to guide search toward destination.
|
|
17
|
+
* Typically faster than Dijkstra for point-to-point queries.
|
|
18
|
+
*
|
|
19
|
+
* The heuristic is adapted based on the metric:
|
|
20
|
+
* - distance: haversine distance in meters
|
|
21
|
+
* - time: haversine distance / max speed (lower bound on travel time in seconds)
|
|
22
|
+
*/
|
|
23
|
+
export declare const astar: RoutingAlgorithmFn;
|
|
24
|
+
//# sourceMappingURL=shortest-path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shortest-path.d.ts","sourceRoot":"","sources":["../../../src/algorithms/shortest-path.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAA0B,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAkG1E;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAE,kBAEtB,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,EAAE,kBAuBnB,CAAA"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortest path algorithms (Dijkstra and A*).
|
|
3
|
+
*
|
|
4
|
+
* Both algorithms guarantee optimal paths. A* uses a heuristic to guide
|
|
5
|
+
* the search toward the destination, making it faster for most queries.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { haversineDistance } from "@osmix/shared/haversine-distance";
|
|
10
|
+
import { BinaryHeap } from "../binary-heap";
|
|
11
|
+
/**
|
|
12
|
+
* Maximum speed (m/s) for time-based heuristic.
|
|
13
|
+
* Uses 130 km/h (~36.1 m/s) to ensure admissibility on all road types.
|
|
14
|
+
*/
|
|
15
|
+
const MAX_SPEED_MS = (130 * 1000) / 3600;
|
|
16
|
+
/**
|
|
17
|
+
* A* shortest path algorithm with configurable heuristic.
|
|
18
|
+
*
|
|
19
|
+
* When heuristic returns 0 for all nodes, this is equivalent to Dijkstra's algorithm.
|
|
20
|
+
* With an admissible heuristic (never overestimates), guarantees optimal paths.
|
|
21
|
+
*/
|
|
22
|
+
function shortestPath(graph, start, end, getWeight, heuristic) {
|
|
23
|
+
// Same node - trivial path
|
|
24
|
+
if (start === end)
|
|
25
|
+
return [{ nodeIndex: start, cost: 0 }];
|
|
26
|
+
const gScore = new Map(); // Best known cost to reach node
|
|
27
|
+
const previous = new Map(); // How we reached each node
|
|
28
|
+
const closed = new Set(); // Fully processed nodes
|
|
29
|
+
const heap = new BinaryHeap();
|
|
30
|
+
gScore.set(start, 0);
|
|
31
|
+
heap.push(start, heuristic(start));
|
|
32
|
+
while (heap.size > 0) {
|
|
33
|
+
const current = heap.pop();
|
|
34
|
+
// Reached destination
|
|
35
|
+
if (current === end)
|
|
36
|
+
return reconstructPath(previous, start, end);
|
|
37
|
+
// Already seen this node
|
|
38
|
+
if (closed.has(current))
|
|
39
|
+
continue;
|
|
40
|
+
closed.add(current);
|
|
41
|
+
const currentG = gScore.get(current);
|
|
42
|
+
for (const edge of graph(current)) {
|
|
43
|
+
const neighbor = edge.targetNodeIndex;
|
|
44
|
+
if (closed.has(neighbor))
|
|
45
|
+
continue;
|
|
46
|
+
const tentativeG = currentG + getWeight(edge);
|
|
47
|
+
const existingG = gScore.get(neighbor);
|
|
48
|
+
if (existingG === undefined || tentativeG < existingG) {
|
|
49
|
+
gScore.set(neighbor, tentativeG);
|
|
50
|
+
previous.set(neighbor, {
|
|
51
|
+
nodeIndex: neighbor,
|
|
52
|
+
wayIndex: edge.wayIndex,
|
|
53
|
+
previousNodeIndex: current,
|
|
54
|
+
cost: tentativeG,
|
|
55
|
+
});
|
|
56
|
+
const f = tentativeG + heuristic(neighbor);
|
|
57
|
+
heap.push(neighbor, f);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Reconstruct path from predecessor map.
|
|
65
|
+
*/
|
|
66
|
+
function reconstructPath(previous, start, end) {
|
|
67
|
+
const path = [];
|
|
68
|
+
let current = end;
|
|
69
|
+
while (current !== undefined) {
|
|
70
|
+
const segment = previous.get(current);
|
|
71
|
+
if (!segment) {
|
|
72
|
+
path.unshift({ nodeIndex: current, cost: 0 });
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
path.unshift(segment);
|
|
76
|
+
current = segment.previousNodeIndex;
|
|
77
|
+
}
|
|
78
|
+
if (path.length === 0 || path[0]?.nodeIndex !== start) {
|
|
79
|
+
path.unshift({ nodeIndex: start, cost: 0 });
|
|
80
|
+
}
|
|
81
|
+
return path;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Dijkstra's algorithm - optimal shortest path without heuristic guidance.
|
|
85
|
+
* Explores nodes in order of increasing distance from start.
|
|
86
|
+
*/
|
|
87
|
+
export const dijkstra = (graph, start, end, getWeight) => {
|
|
88
|
+
return shortestPath(graph, start, end, getWeight, () => 0);
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* A* algorithm - uses heuristic to guide search toward destination.
|
|
92
|
+
* Typically faster than Dijkstra for point-to-point queries.
|
|
93
|
+
*
|
|
94
|
+
* The heuristic is adapted based on the metric:
|
|
95
|
+
* - distance: haversine distance in meters
|
|
96
|
+
* - time: haversine distance / max speed (lower bound on travel time in seconds)
|
|
97
|
+
*/
|
|
98
|
+
export const astar = (graph, start, end, getWeight, getCoord, metric) => {
|
|
99
|
+
if (!getCoord)
|
|
100
|
+
return null;
|
|
101
|
+
const endCoord = getCoord(end);
|
|
102
|
+
if (!endCoord)
|
|
103
|
+
return null;
|
|
104
|
+
// Haversine heuristic scaled by metric
|
|
105
|
+
// For time: divide by max speed to get admissible time estimate
|
|
106
|
+
const heuristic = (nodeIndex) => {
|
|
107
|
+
const coord = getCoord(nodeIndex);
|
|
108
|
+
if (!coord)
|
|
109
|
+
return 0;
|
|
110
|
+
const distance = haversineDistance(endCoord, coord);
|
|
111
|
+
return metric === "time" ? distance / MAX_SPEED_MS : distance;
|
|
112
|
+
};
|
|
113
|
+
return shortestPath(graph, start, end, getWeight, heuristic);
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=shortest-path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shortest-path.js","sourceRoot":"","sources":["../../../src/algorithms/shortest-path.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAA;AACpE,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAG3C;;;GAGG;AACH,MAAM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-0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Priority queue implementation for pathfinding algorithms.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Min-heap priority queue optimized for pathfinding.
|
|
7
|
+
*
|
|
8
|
+
* Uses a binary heap stored in a flat array for cache-friendly access.
|
|
9
|
+
* Tracks item positions to support O(log n) priority updates (decrease-key).
|
|
10
|
+
*
|
|
11
|
+
* Operations:
|
|
12
|
+
* - `push(item, priority)`: O(log n) - adds or updates item priority.
|
|
13
|
+
* - `pop()`: O(log n) - removes and returns minimum priority item.
|
|
14
|
+
* - `has(item)`: O(1) - checks if item is in heap.
|
|
15
|
+
*/
|
|
16
|
+
export declare class BinaryHeap {
|
|
17
|
+
private heap;
|
|
18
|
+
private priorities;
|
|
19
|
+
private positions;
|
|
20
|
+
get size(): number;
|
|
21
|
+
/**
|
|
22
|
+
* Add an item with given priority. If item exists, updates priority if lower.
|
|
23
|
+
*/
|
|
24
|
+
push(item: number, priority: number): void;
|
|
25
|
+
/**
|
|
26
|
+
* Remove and return the minimum-priority item.
|
|
27
|
+
*/
|
|
28
|
+
pop(): number | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Check if item is in the heap.
|
|
31
|
+
*/
|
|
32
|
+
has(item: number): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Clear all items.
|
|
35
|
+
*/
|
|
36
|
+
clear(): void;
|
|
37
|
+
private bubbleUp;
|
|
38
|
+
private bubbleDown;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=binary-heap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-heap.d.ts","sourceRoot":"","sources":["../../src/binary-heap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,IAAI,CAAe;IAC3B,OAAO,CAAC,UAAU,CAAe;IACjC,OAAO,CAAC,SAAS,CAAiC;IAElD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAiB1C;;OAEG;IACH,GAAG,IAAI,MAAM,GAAG,SAAS;IAmBzB;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI1B;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb,OAAO,CAAC,QAAQ;IAwBhB,OAAO,CAAC,UAAU;CAuClB"}
|