-
Notifications
You must be signed in to change notification settings - Fork 2
/
planner.ts
244 lines (237 loc) · 6.71 KB
/
planner.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type {
Feature,
FeatureCollection,
Geometry,
GeoJsonProperties,
} from "geojson"; // eslint-disable-line import/no-extraneous-dependencies
// "./planner-config" (and PlannerJS) is imported dynamically by calculatePlan
import { ElementWithCoordinates } from "./overpass";
interface RouteGeometries {
coordinates: Array<[number, number]>;
obstacles: Array<[number, number]>;
obstacleWays: Array<Array<[number, number]>>;
imaginaryWays: Array<Array<[number, number]>>;
}
function extractGeometry(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
path: any
): RouteGeometries {
const coordinates = [] as Array<[number, number]>;
const obstacles = [] as Array<[number, number]>;
const obstacleWays = new Map();
const imaginaryWays = [] as Array<Array<[number, number]>>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
path.legs[0].getSteps().forEach((step: any, index: number) => {
/* XXX: Would be nice to get a null step.through from PlannerJS here.
Heuristic below: PlannerJS connects the origin and destination to
the nearest OSM ways through a node which does not have an id,
so the steps whose geometry is not based on an OSM way have
nodes without ids at both ends. Additionally, the second step is a
false positive if origin and destination connect to same OSM edge.
*/
if (!step.startLocation.id && !step.stopLocation.id && index !== 1) {
imaginaryWays.push([
[
step.startLocation.longitude as number,
step.startLocation.latitude as number,
],
[
step.stopLocation.longitude as number,
step.stopLocation.latitude as number,
],
]);
return; // guessed segment
}
const node = step.stopLocation;
if (
path.context[step.through]?.definedTags[
"https://w3id.org/openstreetmap/terms#highway"
] === "https://w3id.org/openstreetmap/terms#Steps"
) {
if (!obstacleWays.has(step.through)) {
obstacleWays.set(step.through, []);
}
obstacleWays
.get(step.through)
.push(
[
step.startLocation.longitude as number,
step.startLocation.latitude as number,
],
[
step.stopLocation.longitude as number,
step.stopLocation.latitude as number,
]
);
}
if (node.definedTags?.["https://w3id.org/openstreetmap/terms#barrier"]) {
// eslint-disable-next-line no-console
console.log(
step.through,
node.definedTags[
"https://w3id.org/openstreetmap/terms#barrier"
].replace(/^.*#/, ""),
node.id,
node.definedTags,
node.freeformTags
);
obstacles.push([node.longitude as number, node.latitude as number]);
}
coordinates.push([
step.startLocation.longitude as number,
step.startLocation.latitude as number,
]);
coordinates.push([
step.stopLocation.longitude as number,
step.stopLocation.latitude as number,
]);
});
return {
coordinates,
obstacles,
obstacleWays: Array.from(obstacleWays.values()),
imaginaryWays,
};
}
export function geometryToGeoJSON(
origin?: [number, number],
targets?: Array<ElementWithCoordinates>,
entrances?: Array<ElementWithCoordinates>,
routeGeometries?: RouteGeometries
): FeatureCollection {
const features = [] as Array<Feature<Geometry, GeoJsonProperties>>;
if (origin) {
features.push({
type: "Feature",
geometry: {
type: "Point",
coordinates: [origin[1], origin[0]],
},
properties: {
color: "#00afff",
},
});
}
if (targets) {
targets.forEach((target) => {
features.push({
type: "Feature",
geometry: {
type: "Point",
coordinates: [target.lon, target.lat],
},
properties: {
color: "#64be14",
},
});
});
}
if (entrances) {
entrances.forEach((entrance) => {
features.push({
type: "Feature",
geometry: {
type: "Point",
coordinates: [entrance.lon, entrance.lat],
},
properties: {
color: "#00ffff",
ref: entrance.tags?.["ref"] || entrance.tags?.["addr:unit"],
opacity: 0,
},
});
});
}
if (routeGeometries?.coordinates) {
features.push({
type: "Feature",
geometry: {
type: "LineString",
coordinates: routeGeometries.coordinates,
},
properties: {
color: "#000",
},
});
}
if (routeGeometries?.obstacleWays) {
features.push({
type: "Feature",
geometry: {
type: "MultiLineString",
coordinates: routeGeometries.obstacleWays,
},
properties: {
color: "#dc0451",
opacity: 1,
},
});
}
if (routeGeometries?.imaginaryWays) {
features.push({
type: "Feature",
geometry: {
type: "MultiLineString",
coordinates: routeGeometries.imaginaryWays,
},
properties: {
color: "#000",
imaginary: true,
},
});
}
if (routeGeometries?.obstacles) {
features.push({
type: "Feature",
geometry: {
type: "MultiPoint",
coordinates: routeGeometries.obstacles,
},
properties: {
color: "#dc0451",
ref: "!",
},
});
}
return {
type: "FeatureCollection",
features,
};
}
export default async function calculatePlan(
origin: [number, number],
targets: Array<ElementWithCoordinates>,
callback: (f: FeatureCollection) => void
): Promise<void> {
const { Planner } = await import(
/* webpackChunkName: "planner-config" */
/* webpackPrefetch: true */
"./planner-config"
);
targets.forEach((target) => {
const planner = new Planner();
// XXX setProfileID requires URL to start with scheme, so guess
const protocol = process.env.NODE_ENV === "production" ? "https" : "http";
planner
.setProfileID(`${protocol}://${process.env.PUBLIC_URL}/delivery.json`)
.query({
from: { latitude: origin[0], longitude: origin[1] },
to: { latitude: target.lat, longitude: target.lon },
})
.take(1)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.on("data", async (path: any) => {
const completePath = await planner.completePath(path);
// eslint-disable-next-line no-console
console.log("Plan", completePath, "from", origin, "to", target);
const routeGeometries = extractGeometry(completePath);
const geoJSON = geometryToGeoJSON(
origin,
[target],
undefined,
routeGeometries
);
callback(geoJSON);
});
});
}