-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.ts
163 lines (145 loc) · 4.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type {
Feature,
FeatureCollection,
LineString,
Point,
Position,
} from "@turf/helpers";
import { compactNode } from "./compactor";
import findPath from "./dijkstra";
import preprocess from "./preprocessor";
import roundCoord from "./round-coord";
import { defaultKey } from "./topology";
import { Key, PathFinderGraph, PathFinderOptions } from "./types";
export default class PathFinder<TEdgeReduce, TProperties> {
graph: PathFinderGraph<TEdgeReduce>;
options: PathFinderOptions<TEdgeReduce, TProperties>;
constructor(
network: FeatureCollection<LineString, TProperties>,
options: PathFinderOptions<TEdgeReduce, TProperties> = {}
) {
this.graph = preprocess(network, options);
this.options = options;
if (
Object.keys(this.graph.compactedVertices).filter(function (k) {
return k !== "edgeData";
}).length === 0
) {
throw new Error(
"Compacted graph contains no forks (topology has no intersections)."
);
}
}
findPath(a: Feature<Point>, b: Feature<Point>) {
const { key = defaultKey, tolerance = 1e-5 } = this.options;
const start = key(roundCoord(a.geometry.coordinates, tolerance));
const finish = key(roundCoord(b.geometry.coordinates, tolerance));
// We can't find a path if start or finish isn't in the
// set of non-compacted vertices
if (!this.graph.vertices[start] || !this.graph.vertices[finish]) {
return undefined;
}
const phantomStart = this._createPhantom(start);
const phantomEnd = this._createPhantom(finish);
try {
const pathResult = findPath(this.graph.compactedVertices, start, finish);
if (pathResult) {
const [weight, path] = pathResult;
return {
path: path
.reduce(
(
coordinates: Position[],
vertexKey: Key,
index: number,
vertexKeys: Key[]
) => {
if (index > 0) {
coordinates = coordinates.concat(
this.graph.compactedCoordinates[vertexKeys[index - 1]][
vertexKey
]
);
}
return coordinates;
},
[]
)
.concat([this.graph.sourceCoordinates[finish]]),
weight,
edgeDatas: this.graph.compactedEdges
? path.reduce(
(
edges: (TEdgeReduce | undefined)[],
vertexKey: Key,
index: number,
vertexKeys: Key[]
) => {
if (index > 0) {
edges.push(
this.graph.compactedEdges[vertexKeys[index - 1]][
vertexKey
]
);
}
return edges;
},
[]
)
: undefined,
};
} else {
return null;
}
} finally {
this._removePhantom(phantomStart);
this._removePhantom(phantomEnd);
}
}
_createPhantom(n: Key) {
if (this.graph.compactedVertices[n]) return undefined;
const phantom = compactNode(
n,
this.graph.vertices,
this.graph.compactedVertices,
this.graph.sourceCoordinates,
this.graph.edgeData,
true,
this.options
);
this.graph.compactedVertices[n] = phantom.edges;
this.graph.compactedCoordinates[n] = phantom.coordinates;
if (this.graph.compactedEdges) {
this.graph.compactedEdges[n] = phantom.reducedEdges;
}
Object.keys(phantom.incomingEdges).forEach((neighbor) => {
this.graph.compactedVertices[neighbor][n] =
phantom.incomingEdges[neighbor];
this.graph.compactedCoordinates[neighbor][n] =
phantom.incomingCoordinates[neighbor];
if (this.graph.compactedEdges) {
this.graph.compactedEdges[neighbor][n] = phantom.reducedEdges[neighbor];
}
});
return n;
}
_removePhantom(n: Key | undefined) {
if (!n) return;
Object.keys(this.graph.compactedVertices[n]).forEach((neighbor) => {
delete this.graph.compactedVertices[neighbor][n];
});
Object.keys(this.graph.compactedCoordinates[n]).forEach((neighbor) => {
delete this.graph.compactedCoordinates[neighbor][n];
});
if (this.graph.compactedEdges) {
Object.keys(this.graph.compactedEdges[n]).forEach((neighbor) => {
delete this.graph.compactedEdges[neighbor][n];
});
}
delete this.graph.compactedVertices[n];
delete this.graph.compactedCoordinates[n];
if (this.graph.compactedEdges) {
delete this.graph.compactedEdges[n];
}
}
}