@osmix/core 0.1.8 → 0.2.0
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 +37 -0
- package/LICENSE +21 -0
- package/README.md +86 -69
- package/dist/contracts.d.ts +26 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +1 -0
- package/dist/contracts.js.map +1 -0
- package/dist/entities.d.ts +1 -1
- package/dist/entities.d.ts.map +1 -1
- package/dist/entities.js +2 -7
- package/dist/entities.js.map +1 -1
- package/dist/ids.d.ts +2 -0
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +23 -15
- package/dist/ids.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/mocks.d.ts.map +1 -1
- package/dist/mocks.js.map +1 -1
- package/dist/nodes.d.ts +9 -3
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +6 -13
- package/dist/nodes.js.map +1 -1
- package/dist/osm.d.ts +1 -1
- package/dist/osm.d.ts.map +1 -1
- package/dist/osm.js +1 -4
- package/dist/osm.js.map +1 -1
- package/dist/relations.d.ts +3 -2
- package/dist/relations.d.ts.map +1 -1
- package/dist/relations.js +15 -9
- package/dist/relations.js.map +1 -1
- package/dist/stringtable.d.ts.map +1 -1
- package/dist/stringtable.js +4 -13
- package/dist/stringtable.js.map +1 -1
- package/dist/tags.d.ts +1 -1
- package/dist/tags.d.ts.map +1 -1
- package/dist/tags.js.map +1 -1
- package/dist/typed-arrays.d.ts +1 -1
- package/dist/typed-arrays.d.ts.map +1 -1
- package/dist/typed-arrays.js.map +1 -1
- package/dist/ways.d.ts +2 -1
- package/dist/ways.d.ts.map +1 -1
- package/dist/ways.js +18 -18
- package/dist/ways.js.map +1 -1
- package/package.json +46 -31
- package/src/contracts.ts +28 -0
- package/src/entities.ts +179 -194
- package/src/ids.ts +228 -235
- package/src/index.ts +12 -9
- package/src/mocks.ts +114 -113
- package/src/nodes.ts +364 -397
- package/src/osm.ts +196 -207
- package/src/relations.ts +530 -558
- package/src/stringtable.ts +174 -183
- package/src/tags.ts +263 -271
- package/src/typed-arrays.ts +184 -194
- package/src/types/geoflatbush.d.ts +28 -33
- package/src/types/geokdbush.d.ts +28 -33
- package/src/types/kdbush.d.ts +34 -34
- package/src/ways.ts +381 -405
- package/test/ids.bench.ts +29 -0
- package/test/ids.test.ts +84 -0
- package/test/memory.test.ts +29 -30
- package/test/relations.bench.ts +27 -0
- package/test/relations.test.ts +101 -0
- package/test/tags-search.test.ts +76 -56
- package/test/typed-arrays.test.ts +100 -99
- package/tsconfig.build.json +6 -3
- package/tsconfig.json +7 -7
package/src/ways.ts
CHANGED
|
@@ -1,410 +1,386 @@
|
|
|
1
|
-
import type { OsmPbfWay } from "@osmix/pbf"
|
|
2
|
-
import type { ContentHasher } from "@osmix/shared/content-hasher"
|
|
3
|
-
import type { GeoBbox2D, LonLat, OsmTags, OsmWay } from "@osmix/
|
|
4
|
-
import Flatbush from "flatbush"
|
|
5
|
-
import { around as geoAround } from "geoflatbush"
|
|
6
|
-
|
|
7
|
-
import { type
|
|
8
|
-
import type
|
|
9
|
-
import type
|
|
10
|
-
import
|
|
1
|
+
import type { OsmPbfWay } from "@osmix/pbf";
|
|
2
|
+
import type { ContentHasher } from "@osmix/shared/content-hasher";
|
|
3
|
+
import type { GeoBbox2D, LonLat, OsmTags, OsmWay } from "@osmix/types";
|
|
4
|
+
import Flatbush from "flatbush";
|
|
5
|
+
import { around as geoAround } from "geoflatbush";
|
|
6
|
+
|
|
7
|
+
import { Entities, type EntitiesTransferables } from "./entities.ts";
|
|
8
|
+
import { type IdOrIndex, Ids } from "./ids.ts";
|
|
9
|
+
import type { Nodes } from "./nodes.ts";
|
|
10
|
+
import type StringTable from "./stringtable.ts";
|
|
11
|
+
import { Tags } from "./tags.ts";
|
|
11
12
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from "./typed-arrays.ts"
|
|
17
|
-
|
|
18
|
-
export interface WaysTransferables<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
BufferConstructor,
|
|
14
|
+
type BufferType,
|
|
15
|
+
IdArrayType,
|
|
16
|
+
ResizeableTypedArray as RTA,
|
|
17
|
+
} from "./typed-arrays.ts";
|
|
18
|
+
|
|
19
|
+
export interface WaysTransferables<
|
|
20
|
+
T extends BufferType = BufferType,
|
|
21
|
+
> extends EntitiesTransferables<T> {
|
|
22
|
+
refStart: T;
|
|
23
|
+
refCount: T;
|
|
24
|
+
refs: T;
|
|
25
|
+
bbox: T;
|
|
26
|
+
/** Optional - can be rebuilt via buildSpatialIndex() */
|
|
27
|
+
spatialIndex?: T;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export class Ways extends Entities<OsmWay> {
|
|
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
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
numNodes += n
|
|
385
|
-
}
|
|
386
|
-
const indexBytes = (numNodes < 16384 ? 2 : 4) * numNodes
|
|
387
|
-
const boxesBytes = numNodes * 4 * Float64Array.BYTES_PER_ELEMENT
|
|
388
|
-
const spatialIndexBytes = 8 + indexBytes + boxesBytes
|
|
389
|
-
|
|
390
|
-
return (
|
|
391
|
-
Ids.getBytesRequired(count) +
|
|
392
|
-
Tags.getBytesRequired(count) +
|
|
393
|
-
count * Uint32Array.BYTES_PER_ELEMENT + // refStart
|
|
394
|
-
count * Uint16Array.BYTES_PER_ELEMENT + // refCount
|
|
395
|
-
count * 4 * Float64Array.BYTES_PER_ELEMENT + // bbox
|
|
396
|
-
spatialIndexBytes
|
|
397
|
-
)
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Update a ContentHasher with way-specific data (node references).
|
|
402
|
-
*/
|
|
403
|
-
override updateHash(hasher: ContentHasher): ContentHasher {
|
|
404
|
-
return super
|
|
405
|
-
.updateHash(hasher)
|
|
406
|
-
.update(this.refStart.array)
|
|
407
|
-
.update(this.refCount.array)
|
|
408
|
-
.update(this.refs.array)
|
|
409
|
-
}
|
|
31
|
+
private spatialIndex: Flatbush = new Flatbush(1);
|
|
32
|
+
// Track if spatial index was properly built (vs default empty)
|
|
33
|
+
private spatialIndexBuilt = false;
|
|
34
|
+
|
|
35
|
+
private refStart: RTA<Uint32Array>;
|
|
36
|
+
private refCount: RTA<Uint16Array>; // Maximum 2,000 nodes per way
|
|
37
|
+
|
|
38
|
+
// Node IDs
|
|
39
|
+
private refs: RTA<Float64Array>;
|
|
40
|
+
|
|
41
|
+
// Bounding box of the way in geographic coordinates
|
|
42
|
+
private bbox: RTA<Float64Array>;
|
|
43
|
+
|
|
44
|
+
// Node reference index
|
|
45
|
+
private nodes: Nodes;
|
|
46
|
+
|
|
47
|
+
private getNodeLonLat(id: number): [number, number] {
|
|
48
|
+
const coordinate = this.nodes.getNodeLonLat({ id });
|
|
49
|
+
if (!coordinate) throw Error(`Node ${id} not found for way geometry`);
|
|
50
|
+
return coordinate;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Create a new Ways index.
|
|
55
|
+
*/
|
|
56
|
+
constructor(stringTable: StringTable, nodes: Nodes, transferables?: WaysTransferables) {
|
|
57
|
+
if (transferables) {
|
|
58
|
+
super("way", new Ids(transferables), new Tags(stringTable, transferables));
|
|
59
|
+
this.refStart = RTA.from(Uint32Array, transferables.refStart);
|
|
60
|
+
this.refCount = RTA.from(Uint16Array, transferables.refCount);
|
|
61
|
+
this.refs = RTA.from(IdArrayType, transferables.refs);
|
|
62
|
+
this.bbox = RTA.from(Float64Array, transferables.bbox);
|
|
63
|
+
// Only load spatial index if provided (not stored in IndexedDB)
|
|
64
|
+
if (transferables.spatialIndex?.byteLength) {
|
|
65
|
+
this.spatialIndex = Flatbush.from(transferables.spatialIndex);
|
|
66
|
+
this.spatialIndexBuilt = true;
|
|
67
|
+
}
|
|
68
|
+
this.indexBuilt = true;
|
|
69
|
+
} else {
|
|
70
|
+
super("way", new Ids(), new Tags(stringTable));
|
|
71
|
+
this.refStart = new RTA(Uint32Array);
|
|
72
|
+
this.refCount = new RTA(Uint16Array);
|
|
73
|
+
this.refs = new RTA(IdArrayType);
|
|
74
|
+
this.bbox = new RTA(Float64Array);
|
|
75
|
+
}
|
|
76
|
+
this.nodes = nodes;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Add a single way to the index.
|
|
81
|
+
*/
|
|
82
|
+
addWay(way: OsmWay) {
|
|
83
|
+
const wayIndex = this.addEntity(way.id, way.tags ?? {});
|
|
84
|
+
this.refStart.push(this.refs.length);
|
|
85
|
+
this.refCount.push(way.refs.length);
|
|
86
|
+
for (const ref of way.refs) this.refs.push(ref);
|
|
87
|
+
return wayIndex;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Bulk add ways directly from a PBF PrimitiveBlock.
|
|
92
|
+
*/
|
|
93
|
+
addWays(
|
|
94
|
+
ways: OsmPbfWay[],
|
|
95
|
+
blockStringIndexMap: Uint32Array,
|
|
96
|
+
filter?: (way: OsmWay) => OsmWay | null,
|
|
97
|
+
) {
|
|
98
|
+
let added = 0;
|
|
99
|
+
for (const way of ways) {
|
|
100
|
+
let prevRefId = 0;
|
|
101
|
+
const refs = way.refs.map((refId) => {
|
|
102
|
+
prevRefId += refId;
|
|
103
|
+
return prevRefId;
|
|
104
|
+
});
|
|
105
|
+
const tagKeys: number[] = way.keys.map((key) => {
|
|
106
|
+
const index = blockStringIndexMap[key];
|
|
107
|
+
if (index === undefined) throw Error("Tag key not found");
|
|
108
|
+
return index;
|
|
109
|
+
});
|
|
110
|
+
const tagValues: number[] = way.vals.map((val) => {
|
|
111
|
+
const index = blockStringIndexMap[val];
|
|
112
|
+
if (index === undefined) throw Error("Tag value not found");
|
|
113
|
+
return index;
|
|
114
|
+
});
|
|
115
|
+
const filteredWay = filter
|
|
116
|
+
? filter({
|
|
117
|
+
id: way.id,
|
|
118
|
+
refs,
|
|
119
|
+
tags: this.tags.getTagsFromIndices(tagKeys, tagValues),
|
|
120
|
+
})
|
|
121
|
+
: null;
|
|
122
|
+
if (filter && filteredWay === null) continue;
|
|
123
|
+
|
|
124
|
+
this.addEntity(way.id, tagKeys, tagValues);
|
|
125
|
+
this.refStart.push(this.refs.length);
|
|
126
|
+
this.refCount.push(filteredWay?.refs.length ?? refs.length);
|
|
127
|
+
this.refs.pushMany(filteredWay?.refs ?? refs);
|
|
128
|
+
added++;
|
|
129
|
+
}
|
|
130
|
+
return added;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Compact the internal arrays to free up memory.
|
|
135
|
+
*/
|
|
136
|
+
buildEntityIndex() {
|
|
137
|
+
this.refStart.compact();
|
|
138
|
+
this.refCount.compact();
|
|
139
|
+
this.refs.compact();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Build the spatial index for ways.
|
|
144
|
+
* If bbox data already exists (e.g., loaded from storage), reuses it.
|
|
145
|
+
*/
|
|
146
|
+
buildSpatialIndex() {
|
|
147
|
+
if (!this.nodes.isReady()) throw Error("Node index is not ready.");
|
|
148
|
+
if (this.size === 0) return this.spatialIndex;
|
|
149
|
+
|
|
150
|
+
this.spatialIndex = new Flatbush(this.size, 128, Float64Array, BufferConstructor);
|
|
151
|
+
|
|
152
|
+
// If bbox already has data (loaded from storage), use it directly
|
|
153
|
+
const hasBboxData = this.bbox.length >= this.size * 4;
|
|
154
|
+
for (let i = 0; i < this.size; i++) {
|
|
155
|
+
let minX: number;
|
|
156
|
+
let minY: number;
|
|
157
|
+
let maxX: number;
|
|
158
|
+
let maxY: number;
|
|
159
|
+
|
|
160
|
+
if (hasBboxData) {
|
|
161
|
+
// Use stored bbox values
|
|
162
|
+
minX = this.bbox.at(i * 4);
|
|
163
|
+
minY = this.bbox.at(i * 4 + 1);
|
|
164
|
+
maxX = this.bbox.at(i * 4 + 2);
|
|
165
|
+
maxY = this.bbox.at(i * 4 + 3);
|
|
166
|
+
} else {
|
|
167
|
+
// Calculate bbox from node coordinates
|
|
168
|
+
minX = Number.POSITIVE_INFINITY;
|
|
169
|
+
minY = Number.POSITIVE_INFINITY;
|
|
170
|
+
maxX = Number.NEGATIVE_INFINITY;
|
|
171
|
+
maxY = Number.NEGATIVE_INFINITY;
|
|
172
|
+
const start = this.refStart.at(i);
|
|
173
|
+
const count = this.refCount.at(i);
|
|
174
|
+
for (let j = start; j < start + count; j++) {
|
|
175
|
+
const refId = this.refs.at(j);
|
|
176
|
+
const [lon, lat] = this.getNodeLonLat(refId);
|
|
177
|
+
if (lon < minX) minX = lon;
|
|
178
|
+
if (lon > maxX) maxX = lon;
|
|
179
|
+
if (lat < minY) minY = lat;
|
|
180
|
+
if (lat > maxY) maxY = lat;
|
|
181
|
+
}
|
|
182
|
+
this.bbox.push(minX);
|
|
183
|
+
this.bbox.push(minY);
|
|
184
|
+
this.bbox.push(maxX);
|
|
185
|
+
this.bbox.push(maxY);
|
|
186
|
+
}
|
|
187
|
+
this.spatialIndex.add(minX, minY, maxX, maxY);
|
|
188
|
+
}
|
|
189
|
+
// Compact bbox if we just calculated it
|
|
190
|
+
if (!hasBboxData) {
|
|
191
|
+
this.bbox.compact();
|
|
192
|
+
}
|
|
193
|
+
this.spatialIndex.finish();
|
|
194
|
+
this.spatialIndexBuilt = true;
|
|
195
|
+
return this.spatialIndex;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Check if the spatial index has been built.
|
|
200
|
+
*/
|
|
201
|
+
hasSpatialIndex(): boolean {
|
|
202
|
+
return this.spatialIndexBuilt;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Get the full way entity.
|
|
207
|
+
*/
|
|
208
|
+
getFullEntity(index: number, id: number, tags?: OsmTags): OsmWay {
|
|
209
|
+
return {
|
|
210
|
+
id,
|
|
211
|
+
refs: [...this.getRefIds(index)],
|
|
212
|
+
tags,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Get the node IDs referenced by a way.
|
|
218
|
+
*/
|
|
219
|
+
getRefIds(index: number): number[] {
|
|
220
|
+
const start = this.refStart.at(index);
|
|
221
|
+
const count = this.refCount.at(index);
|
|
222
|
+
return Array.from(this.refs.slice(start, start + count));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Get the bounding box of a way.
|
|
227
|
+
*/
|
|
228
|
+
getEntityBbox(idOrIndex: IdOrIndex): GeoBbox2D {
|
|
229
|
+
const index = "index" in idOrIndex ? idOrIndex.index : this.ids.idOrIndex(idOrIndex)[0];
|
|
230
|
+
return [
|
|
231
|
+
this.bbox.at(index * 4),
|
|
232
|
+
this.bbox.at(index * 4 + 1),
|
|
233
|
+
this.bbox.at(index * 4 + 2),
|
|
234
|
+
this.bbox.at(index * 4 + 3),
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Get the coordinates of a way as a flat array.
|
|
240
|
+
*/
|
|
241
|
+
getLine(index: number) {
|
|
242
|
+
const count = this.refCount.at(index);
|
|
243
|
+
const start = this.refStart.at(index);
|
|
244
|
+
const line = new Float64Array(count * 2);
|
|
245
|
+
for (let i = 0; i < count; i++) {
|
|
246
|
+
const ref = this.refs.at(start + i);
|
|
247
|
+
const [lon, lat] = this.getNodeLonLat(ref);
|
|
248
|
+
line[i * 2] = lon;
|
|
249
|
+
line[i * 2 + 1] = lat;
|
|
250
|
+
}
|
|
251
|
+
return line;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Get the coordinates of a way as an array of [lon, lat] pairs.
|
|
256
|
+
*/
|
|
257
|
+
getCoordinates(index: number): LonLat[] {
|
|
258
|
+
const count = this.refCount.at(index);
|
|
259
|
+
const start = this.refStart.at(index);
|
|
260
|
+
const coords: [number, number][] = [];
|
|
261
|
+
for (let refIndex = start; refIndex < start + count; refIndex++) {
|
|
262
|
+
const ref = this.refs.at(refIndex);
|
|
263
|
+
coords.push(this.getNodeLonLat(ref));
|
|
264
|
+
}
|
|
265
|
+
return coords;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Find way indexes that intersect a bounding box.
|
|
270
|
+
*/
|
|
271
|
+
intersects(bbox: GeoBbox2D, filterFn?: (index: number) => boolean): number[] {
|
|
272
|
+
if (this.size === 0) return [];
|
|
273
|
+
return this.spatialIndex.search(bbox[0], bbox[1], bbox[2], bbox[3], filterFn);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Find way indexes near a point using great-circle distance.
|
|
278
|
+
* @param lon - Longitude in degrees.
|
|
279
|
+
* @param lat - Latitude in degrees.
|
|
280
|
+
* @param maxResults - Maximum number of results to return.
|
|
281
|
+
* @param maxDistanceKm - Maximum distance in kilometers.
|
|
282
|
+
* @returns Array of way indexes sorted by distance.
|
|
283
|
+
*/
|
|
284
|
+
neighbors(lon: number, lat: number, maxResults?: number, maxDistanceKm?: number): number[] {
|
|
285
|
+
if (this.size === 0) return [];
|
|
286
|
+
// Use geoflatbush for proper geographic distance calculations
|
|
287
|
+
return geoAround(this.spatialIndex, lon, lat, maxResults, maxDistanceKm);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Get ways within a bounding box.
|
|
292
|
+
*/
|
|
293
|
+
withinBbox(
|
|
294
|
+
bbox: GeoBbox2D,
|
|
295
|
+
include?: (index: number) => boolean,
|
|
296
|
+
): {
|
|
297
|
+
ids: Float64Array;
|
|
298
|
+
positions: Float64Array;
|
|
299
|
+
startIndices: Uint32Array;
|
|
300
|
+
} {
|
|
301
|
+
const wayCandidates = this.intersects(bbox, include);
|
|
302
|
+
const ids = new Float64Array(wayCandidates.length);
|
|
303
|
+
const wayPositions: Float64Array[] = [];
|
|
304
|
+
const wayStartIndices = new Uint32Array(wayCandidates.length + 1);
|
|
305
|
+
wayStartIndices[0] = 0;
|
|
306
|
+
|
|
307
|
+
let size = 0;
|
|
308
|
+
wayCandidates.forEach((wayIndex, i) => {
|
|
309
|
+
ids[i] = this.ids.at(wayIndex);
|
|
310
|
+
const way = this.getLine(wayIndex);
|
|
311
|
+
size += way.length;
|
|
312
|
+
wayPositions.push(way);
|
|
313
|
+
const prevIndex = wayStartIndices[i];
|
|
314
|
+
if (prevIndex === undefined) throw Error("Previous index is undefined");
|
|
315
|
+
wayStartIndices[i + 1] = prevIndex + way.length / 2;
|
|
316
|
+
});
|
|
317
|
+
const wayPositionsArray = new Float64Array(size);
|
|
318
|
+
let pIndex = 0;
|
|
319
|
+
for (const way of wayPositions) {
|
|
320
|
+
wayPositionsArray.set(way, pIndex);
|
|
321
|
+
pIndex += way.length;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
ids,
|
|
326
|
+
positions: wayPositionsArray,
|
|
327
|
+
startIndices: wayStartIndices,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Get transferable objects for passing to another thread.
|
|
333
|
+
* Only includes spatialIndex if it has been built.
|
|
334
|
+
*/
|
|
335
|
+
override transferables(): WaysTransferables {
|
|
336
|
+
const base = {
|
|
337
|
+
...super.transferables(),
|
|
338
|
+
refStart: this.refStart.array.buffer,
|
|
339
|
+
refCount: this.refCount.array.buffer,
|
|
340
|
+
refs: this.refs.array.buffer,
|
|
341
|
+
bbox: this.bbox.array.buffer,
|
|
342
|
+
};
|
|
343
|
+
// Only include spatial index if it was built
|
|
344
|
+
if (this.spatialIndexBuilt) {
|
|
345
|
+
return { ...base, spatialIndex: this.spatialIndex.data };
|
|
346
|
+
}
|
|
347
|
+
return base;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Get the approximate memory requirements for a given number of ways in bytes.
|
|
352
|
+
*/
|
|
353
|
+
static getBytesRequired(count: number) {
|
|
354
|
+
if (count === 0) return 0;
|
|
355
|
+
// Approximate nodes per way
|
|
356
|
+
let numNodes = count;
|
|
357
|
+
let n = count;
|
|
358
|
+
while (n !== 1) {
|
|
359
|
+
n = Math.ceil(n / 128);
|
|
360
|
+
numNodes += n;
|
|
361
|
+
}
|
|
362
|
+
const indexBytes = (numNodes < 16384 ? 2 : 4) * numNodes;
|
|
363
|
+
const boxesBytes = numNodes * 4 * Float64Array.BYTES_PER_ELEMENT;
|
|
364
|
+
const spatialIndexBytes = 8 + indexBytes + boxesBytes;
|
|
365
|
+
|
|
366
|
+
return (
|
|
367
|
+
Ids.getBytesRequired(count) +
|
|
368
|
+
Tags.getBytesRequired(count) +
|
|
369
|
+
count * Uint32Array.BYTES_PER_ELEMENT + // refStart
|
|
370
|
+
count * Uint16Array.BYTES_PER_ELEMENT + // refCount
|
|
371
|
+
count * 4 * Float64Array.BYTES_PER_ELEMENT + // bbox
|
|
372
|
+
spatialIndexBytes
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Update a ContentHasher with way-specific data (node references).
|
|
378
|
+
*/
|
|
379
|
+
override updateHash(hasher: ContentHasher): ContentHasher {
|
|
380
|
+
return super
|
|
381
|
+
.updateHash(hasher)
|
|
382
|
+
.update(this.refStart.array)
|
|
383
|
+
.update(this.refCount.array)
|
|
384
|
+
.update(this.refs.array);
|
|
385
|
+
}
|
|
410
386
|
}
|