@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/relations.ts
CHANGED
|
@@ -1,564 +1,536 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
buildRelationLineStrings,
|
|
6
|
-
collectRelationPoints,
|
|
7
|
-
isAreaRelation,
|
|
8
|
-
isLineRelation,
|
|
9
|
-
isPointRelation,
|
|
10
|
-
isSuperRelation,
|
|
11
|
-
resolveRelationMembers,
|
|
12
|
-
} from "@osmix/shared/relation-kind"
|
|
13
|
-
import { buildRelationRings } from "@osmix/shared/relation-multipolygon"
|
|
1
|
+
import { buildRelationRings } from "@osmix/geo/relation-multipolygon";
|
|
2
|
+
import type { OsmPbfRelation } from "@osmix/pbf";
|
|
3
|
+
import { assertValue } from "@osmix/shared/assert";
|
|
4
|
+
import type { ContentHasher } from "@osmix/shared/content-hasher";
|
|
14
5
|
import type {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} from "@osmix/
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
6
|
+
GeoBbox2D,
|
|
7
|
+
LonLat,
|
|
8
|
+
OsmEntityType,
|
|
9
|
+
OsmRelation,
|
|
10
|
+
OsmRelationMember,
|
|
11
|
+
OsmTags,
|
|
12
|
+
} from "@osmix/types";
|
|
13
|
+
import {
|
|
14
|
+
buildRelationLineStrings,
|
|
15
|
+
collectRelationPoints,
|
|
16
|
+
isAreaRelation,
|
|
17
|
+
isLineRelation,
|
|
18
|
+
isPointRelation,
|
|
19
|
+
isSuperRelation,
|
|
20
|
+
resolveRelationMembers,
|
|
21
|
+
} from "@osmix/types/relation-kind";
|
|
22
|
+
import { bboxFromLonLats } from "@osmix/types/utils";
|
|
23
|
+
import Flatbush from "flatbush";
|
|
24
|
+
import { around as geoAround } from "geoflatbush";
|
|
25
|
+
|
|
26
|
+
import { Entities, type EntitiesTransferables } from "./entities.ts";
|
|
27
|
+
import { type IdOrIndex, Ids } from "./ids.ts";
|
|
28
|
+
import type { Nodes } from "./nodes.ts";
|
|
29
|
+
import type StringTable from "./stringtable.ts";
|
|
30
|
+
import { Tags } from "./tags.ts";
|
|
30
31
|
import {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
} from "./typed-arrays.ts"
|
|
36
|
-
import type { Ways } from "./ways.ts"
|
|
37
|
-
|
|
38
|
-
const RELATION_MEMBER_TYPES: OsmEntityType[] = ["node", "way", "relation"]
|
|
39
|
-
|
|
40
|
-
export interface RelationsTransferables<
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
32
|
+
BufferConstructor,
|
|
33
|
+
type BufferType,
|
|
34
|
+
IdArrayType,
|
|
35
|
+
ResizeableTypedArray as RTA,
|
|
36
|
+
} from "./typed-arrays.ts";
|
|
37
|
+
import type { Ways } from "./ways.ts";
|
|
38
|
+
|
|
39
|
+
const RELATION_MEMBER_TYPES: OsmEntityType[] = ["node", "way", "relation"];
|
|
40
|
+
|
|
41
|
+
export interface RelationsTransferables<
|
|
42
|
+
T extends BufferType = BufferType,
|
|
43
|
+
> extends EntitiesTransferables<T> {
|
|
44
|
+
memberStart: T;
|
|
45
|
+
memberCount: T;
|
|
46
|
+
memberRefs: T;
|
|
47
|
+
memberTypes: T;
|
|
48
|
+
memberRoles: T;
|
|
49
|
+
bbox: T;
|
|
50
|
+
/** Optional - can be rebuilt via buildSpatialIndex() */
|
|
51
|
+
spatialIndex?: T;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
export class Relations extends Entities<OsmRelation> {
|
|
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
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
while (n !== 1) {
|
|
535
|
-
n = Math.ceil(n / 128)
|
|
536
|
-
numNodes += n
|
|
537
|
-
}
|
|
538
|
-
const indexBytes = (numNodes < 16384 ? 2 : 4) * numNodes
|
|
539
|
-
const boxesBytes = numNodes * 4 * Float64Array.BYTES_PER_ELEMENT
|
|
540
|
-
const spatialIndexBytes = 8 + indexBytes + boxesBytes
|
|
541
|
-
|
|
542
|
-
return (
|
|
543
|
-
Ids.getBytesRequired(count) +
|
|
544
|
-
Tags.getBytesRequired(count) +
|
|
545
|
-
count * Uint32Array.BYTES_PER_ELEMENT + // memberStart
|
|
546
|
-
count * Uint16Array.BYTES_PER_ELEMENT + // memberCount
|
|
547
|
-
count * 4 * Float64Array.BYTES_PER_ELEMENT + // bbox
|
|
548
|
-
spatialIndexBytes
|
|
549
|
-
)
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* Update a ContentHasher with relation-specific data (members).
|
|
554
|
-
*/
|
|
555
|
-
override updateHash(hasher: ContentHasher): ContentHasher {
|
|
556
|
-
return super
|
|
557
|
-
.updateHash(hasher)
|
|
558
|
-
.update(this.memberStart.array)
|
|
559
|
-
.update(this.memberCount.array)
|
|
560
|
-
.update(this.memberRefs.array)
|
|
561
|
-
.update(this.memberTypes.array)
|
|
562
|
-
.update(this.memberRoles.array)
|
|
563
|
-
}
|
|
55
|
+
private stringTable: StringTable;
|
|
56
|
+
|
|
57
|
+
private memberStart: RTA<Uint32Array>;
|
|
58
|
+
private memberCount: RTA<Uint16Array>; // Maximum 65,535 members per relation
|
|
59
|
+
|
|
60
|
+
// Store the ID of the member because relations have other relations as members.
|
|
61
|
+
private memberRefs: RTA<Float64Array>;
|
|
62
|
+
private memberTypes: RTA<Uint8Array>;
|
|
63
|
+
private memberRoles: RTA<Uint32Array>;
|
|
64
|
+
|
|
65
|
+
// Spatial index
|
|
66
|
+
private spatialIndex: Flatbush = new Flatbush(1);
|
|
67
|
+
// Track if spatial index was properly built (vs default empty)
|
|
68
|
+
private spatialIndexBuilt = false;
|
|
69
|
+
|
|
70
|
+
// Bounding box of the relation in geographic coordinates
|
|
71
|
+
private bbox: RTA<Float64Array>;
|
|
72
|
+
|
|
73
|
+
// Node and Way indexes
|
|
74
|
+
private nodes: Nodes;
|
|
75
|
+
private ways: Ways;
|
|
76
|
+
|
|
77
|
+
// Lazily computed relation way membership. Invalidated whenever relations change.
|
|
78
|
+
private wayMemberIdsCache: ReadonlySet<number> | null = null;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Create a new Relations index.
|
|
82
|
+
*/
|
|
83
|
+
constructor(
|
|
84
|
+
stringTable: StringTable,
|
|
85
|
+
nodes: Nodes,
|
|
86
|
+
ways: Ways,
|
|
87
|
+
transferables?: RelationsTransferables,
|
|
88
|
+
) {
|
|
89
|
+
if (transferables) {
|
|
90
|
+
super("relation", new Ids(transferables), new Tags(stringTable, transferables));
|
|
91
|
+
this.memberStart = RTA.from(Uint32Array, transferables.memberStart);
|
|
92
|
+
this.memberCount = RTA.from(Uint16Array, transferables.memberCount);
|
|
93
|
+
this.memberRefs = RTA.from(IdArrayType, transferables.memberRefs);
|
|
94
|
+
this.memberTypes = RTA.from(Uint8Array, transferables.memberTypes);
|
|
95
|
+
this.memberRoles = RTA.from(Uint32Array, transferables.memberRoles);
|
|
96
|
+
this.bbox = RTA.from(Float64Array, transferables.bbox);
|
|
97
|
+
// Only load spatial index if provided (not stored in IndexedDB)
|
|
98
|
+
if (transferables.spatialIndex?.byteLength) {
|
|
99
|
+
this.spatialIndex = Flatbush.from(transferables.spatialIndex);
|
|
100
|
+
this.spatialIndexBuilt = true;
|
|
101
|
+
}
|
|
102
|
+
this.indexBuilt = true;
|
|
103
|
+
} else {
|
|
104
|
+
super("relation", new Ids(), new Tags(stringTable));
|
|
105
|
+
this.memberStart = new RTA(Uint32Array);
|
|
106
|
+
this.memberCount = new RTA(Uint16Array);
|
|
107
|
+
this.memberRefs = new RTA(IdArrayType);
|
|
108
|
+
this.memberTypes = new RTA(Uint8Array);
|
|
109
|
+
this.memberRoles = new RTA(Uint32Array);
|
|
110
|
+
this.bbox = new RTA(Float64Array);
|
|
111
|
+
}
|
|
112
|
+
this.nodes = nodes;
|
|
113
|
+
this.ways = ways;
|
|
114
|
+
this.stringTable = stringTable;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Add a single relation to the index.
|
|
119
|
+
*/
|
|
120
|
+
addRelation(relation: OsmRelation) {
|
|
121
|
+
this.wayMemberIdsCache = null;
|
|
122
|
+
const relationIndex = this.addEntity(relation.id, relation.tags ?? {});
|
|
123
|
+
this.memberStart.push(this.memberRefs.length);
|
|
124
|
+
this.memberCount.push(relation.members.length);
|
|
125
|
+
for (const member of relation.members) {
|
|
126
|
+
this.memberRefs.push(member.ref);
|
|
127
|
+
this.memberTypes.push(RELATION_MEMBER_TYPES.indexOf(member.type));
|
|
128
|
+
this.memberRoles.push(this.stringTable.add(member.role ?? ""));
|
|
129
|
+
}
|
|
130
|
+
return relationIndex;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Bulk add relations directly from a PBF PrimitiveBlock.
|
|
135
|
+
*/
|
|
136
|
+
addRelations(
|
|
137
|
+
relations: OsmPbfRelation[],
|
|
138
|
+
blockStringIndexMap: Uint32Array,
|
|
139
|
+
filter?: (relation: OsmRelation) => OsmRelation | null,
|
|
140
|
+
): number {
|
|
141
|
+
this.wayMemberIdsCache = null;
|
|
142
|
+
const blockToStringTable = (k: number) => {
|
|
143
|
+
const index = blockStringIndexMap[k];
|
|
144
|
+
if (index === undefined) throw Error("Tag key not found");
|
|
145
|
+
return index;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
let added = 0;
|
|
149
|
+
for (const relation of relations) {
|
|
150
|
+
const members: OsmRelationMember[] = [];
|
|
151
|
+
const memberRefs: number[] = [];
|
|
152
|
+
const memberTypes: number[] = [];
|
|
153
|
+
const memberRoles: number[] = [];
|
|
154
|
+
|
|
155
|
+
let refId = 0;
|
|
156
|
+
for (let i = 0; i < relation.memids.length; i++) {
|
|
157
|
+
const memid = relation.memids[i];
|
|
158
|
+
const roleSid = relation.roles_sid[i];
|
|
159
|
+
const typeIndex = relation.types[i];
|
|
160
|
+
assertValue(memid, "Relation member ID is undefined");
|
|
161
|
+
assertValue(roleSid, "Relation member role SID is undefined");
|
|
162
|
+
assertValue(typeIndex, "Relation member type is undefined");
|
|
163
|
+
|
|
164
|
+
refId += memid;
|
|
165
|
+
const roleIndex = blockToStringTable(roleSid);
|
|
166
|
+
const type = RELATION_MEMBER_TYPES[typeIndex];
|
|
167
|
+
assertValue(type, "Relation member type not found");
|
|
168
|
+
|
|
169
|
+
if (filter) {
|
|
170
|
+
members.push({
|
|
171
|
+
type,
|
|
172
|
+
ref: refId,
|
|
173
|
+
role: this.stringTable.get(roleIndex),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
memberRefs.push(refId);
|
|
178
|
+
memberTypes.push(typeIndex);
|
|
179
|
+
memberRoles.push(roleIndex);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const tagKeys: number[] = relation.keys.map(blockToStringTable);
|
|
183
|
+
const tagValues: number[] = relation.vals.map(blockToStringTable);
|
|
184
|
+
|
|
185
|
+
const filteredRelation = filter
|
|
186
|
+
? filter({
|
|
187
|
+
id: relation.id,
|
|
188
|
+
members,
|
|
189
|
+
tags: this.tags.getTagsFromIndices(tagKeys, tagValues),
|
|
190
|
+
})
|
|
191
|
+
: null;
|
|
192
|
+
if (filter && filteredRelation === null) continue;
|
|
193
|
+
added++;
|
|
194
|
+
|
|
195
|
+
this.addEntity(relation.id, tagKeys, tagValues);
|
|
196
|
+
this.memberStart.push(this.memberRefs.length);
|
|
197
|
+
this.memberCount.push(filteredRelation?.members.length ?? memberRefs.length);
|
|
198
|
+
this.memberRefs.pushMany(filteredRelation?.members.map((m) => m.ref) ?? memberRefs);
|
|
199
|
+
this.memberTypes.pushMany(
|
|
200
|
+
filteredRelation?.members.map((m) => RELATION_MEMBER_TYPES.indexOf(m.type)) ?? memberTypes,
|
|
201
|
+
);
|
|
202
|
+
this.memberRoles.pushMany(
|
|
203
|
+
filteredRelation?.members.map((m) => this.stringTable.add(m.role ?? "")) ?? memberRoles,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return added;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Compact the internal arrays to free up memory.
|
|
211
|
+
*/
|
|
212
|
+
buildEntityIndex() {
|
|
213
|
+
this.memberStart.compact();
|
|
214
|
+
this.memberCount.compact();
|
|
215
|
+
this.memberRefs.compact();
|
|
216
|
+
this.memberTypes.compact();
|
|
217
|
+
this.memberRoles.compact();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Build the spatial index for relations.
|
|
222
|
+
* Handles nested relations by resolving all descendant nodes and ways.
|
|
223
|
+
* If bbox data already exists (e.g., loaded from storage), reuses it.
|
|
224
|
+
*/
|
|
225
|
+
buildSpatialIndex() {
|
|
226
|
+
if (!this.nodes.isReady()) throw Error("Node index is not ready.");
|
|
227
|
+
if (!this.ways.isReady()) throw Error("Way index is not ready.");
|
|
228
|
+
if (this.size === 0) return this.spatialIndex;
|
|
229
|
+
console.time("RelationIndex.buildSpatialIndex");
|
|
230
|
+
|
|
231
|
+
this.spatialIndex = new Flatbush(this.size, 128, Float64Array, BufferConstructor);
|
|
232
|
+
|
|
233
|
+
// If bbox already has data (loaded from storage), use it directly
|
|
234
|
+
const hasBboxData = this.bbox.length >= this.size * 4;
|
|
235
|
+
for (let i = 0; i < this.size; i++) {
|
|
236
|
+
let minX: number;
|
|
237
|
+
let minY: number;
|
|
238
|
+
let maxX: number;
|
|
239
|
+
let maxY: number;
|
|
240
|
+
|
|
241
|
+
if (hasBboxData) {
|
|
242
|
+
// Use stored bbox values
|
|
243
|
+
minX = this.bbox.at(i * 4);
|
|
244
|
+
minY = this.bbox.at(i * 4 + 1);
|
|
245
|
+
maxX = this.bbox.at(i * 4 + 2);
|
|
246
|
+
maxY = this.bbox.at(i * 4 + 3);
|
|
247
|
+
} else {
|
|
248
|
+
// Calculate bbox from coordinates
|
|
249
|
+
const lls = this.collectRelationCoordinates(i);
|
|
250
|
+
const bbox = bboxFromLonLats(lls);
|
|
251
|
+
minX = bbox[0];
|
|
252
|
+
minY = bbox[1];
|
|
253
|
+
maxX = bbox[2];
|
|
254
|
+
maxY = bbox[3];
|
|
255
|
+
this.bbox.push(minX);
|
|
256
|
+
this.bbox.push(minY);
|
|
257
|
+
this.bbox.push(maxX);
|
|
258
|
+
this.bbox.push(maxY);
|
|
259
|
+
}
|
|
260
|
+
this.spatialIndex.add(minX, minY, maxX, maxY);
|
|
261
|
+
}
|
|
262
|
+
if (!hasBboxData) {
|
|
263
|
+
this.bbox.compact();
|
|
264
|
+
}
|
|
265
|
+
this.spatialIndex.finish();
|
|
266
|
+
this.spatialIndexBuilt = true;
|
|
267
|
+
console.timeEnd("RelationIndex.buildSpatialIndex");
|
|
268
|
+
return this.spatialIndex;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Check if the spatial index has been built.
|
|
273
|
+
*/
|
|
274
|
+
hasSpatialIndex(): boolean {
|
|
275
|
+
return this.spatialIndexBuilt;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Collect all coordinates from a relation, including nested relations.
|
|
280
|
+
* Used for building bounding boxes and spatial indexes.
|
|
281
|
+
*/
|
|
282
|
+
private collectRelationCoordinates(index: number): LonLat[] {
|
|
283
|
+
const lls: LonLat[] = [];
|
|
284
|
+
const relation = this.getByIndex(index);
|
|
285
|
+
|
|
286
|
+
// Resolve nested relations to get all descendant nodes and ways
|
|
287
|
+
const resolved = resolveRelationMembers(
|
|
288
|
+
relation,
|
|
289
|
+
(relId) => {
|
|
290
|
+
const relIndex = this.ids.getIndexFromId(relId);
|
|
291
|
+
if (relIndex === -1) return null;
|
|
292
|
+
return this.getByIndex(relIndex);
|
|
293
|
+
},
|
|
294
|
+
10, // max depth
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Collect coordinates from resolved nodes
|
|
298
|
+
for (const nodeId of resolved.nodes) {
|
|
299
|
+
const ll = this.nodes.getNodeLonLat({ id: nodeId });
|
|
300
|
+
if (ll) lls.push(ll);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Collect coordinates from resolved ways
|
|
304
|
+
for (const wayId of resolved.ways) {
|
|
305
|
+
const wayIndex = this.ways.ids.getIndexFromId(wayId);
|
|
306
|
+
if (wayIndex === -1) continue;
|
|
307
|
+
const wayPositions = this.ways.getCoordinates(wayIndex);
|
|
308
|
+
lls.push(...wayPositions);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return lls;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Get the bounding box of a relation.
|
|
316
|
+
*/
|
|
317
|
+
getEntityBbox(i: IdOrIndex): GeoBbox2D {
|
|
318
|
+
const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0];
|
|
319
|
+
return [
|
|
320
|
+
this.bbox.at(index * 4),
|
|
321
|
+
this.bbox.at(index * 4 + 1),
|
|
322
|
+
this.bbox.at(index * 4 + 2),
|
|
323
|
+
this.bbox.at(index * 4 + 3),
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Get the full relation entity.
|
|
329
|
+
*/
|
|
330
|
+
getFullEntity(index: number, id: number, tags?: OsmTags): OsmRelation {
|
|
331
|
+
return {
|
|
332
|
+
id,
|
|
333
|
+
members: this.getMembersByIndex(index),
|
|
334
|
+
tags,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Get the members of a relation.
|
|
340
|
+
*/
|
|
341
|
+
getMembersByIndex(index: number, relationMemberTypes = RELATION_MEMBER_TYPES) {
|
|
342
|
+
const start = this.memberStart.at(index);
|
|
343
|
+
const count = this.memberCount.at(index);
|
|
344
|
+
const members: OsmRelationMember[] = [];
|
|
345
|
+
for (let i = start; i < start + count; i++) {
|
|
346
|
+
const ref = this.memberRefs.at(i);
|
|
347
|
+
const type = RELATION_MEMBER_TYPES[this.memberTypes.at(i)];
|
|
348
|
+
if (type === undefined) throw Error(`Member type not found: ${i}`);
|
|
349
|
+
if (!relationMemberTypes.includes(type)) continue;
|
|
350
|
+
const role = this.stringTable.get(this.memberRoles.at(i));
|
|
351
|
+
members.push({ ref, type, role });
|
|
352
|
+
}
|
|
353
|
+
return members;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Check if a relation includes a specific member.
|
|
358
|
+
*/
|
|
359
|
+
includesMember(index: number, memberRef: number, memberType: OsmEntityType, memberRole?: string) {
|
|
360
|
+
const start = this.memberStart.at(index);
|
|
361
|
+
const count = this.memberCount.at(index);
|
|
362
|
+
for (let i = start; i < start + count; i++) {
|
|
363
|
+
const type = RELATION_MEMBER_TYPES[this.memberTypes.at(i)];
|
|
364
|
+
if (type !== memberType) continue;
|
|
365
|
+
const ref = this.memberRefs.at(i);
|
|
366
|
+
if (ref !== memberRef) continue;
|
|
367
|
+
if (memberRole !== undefined && this.stringTable.get(this.memberRoles.at(i)) !== memberRole)
|
|
368
|
+
continue;
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Get all way IDs that are members of relations, including nested relations.
|
|
376
|
+
* Used to exclude these ways from individual rendering.
|
|
377
|
+
*/
|
|
378
|
+
getWayMemberIds(): ReadonlySet<number> {
|
|
379
|
+
if (this.wayMemberIdsCache !== null) return this.wayMemberIdsCache;
|
|
380
|
+
|
|
381
|
+
const wayIds = new Set<number>();
|
|
382
|
+
for (let i = 0; i < this.size; i++) {
|
|
383
|
+
const relation = this.getByIndex(i);
|
|
384
|
+
const resolved = resolveRelationMembers(
|
|
385
|
+
relation,
|
|
386
|
+
(relId) => {
|
|
387
|
+
const relIndex = this.ids.getIndexFromId(relId);
|
|
388
|
+
if (relIndex === -1) return null;
|
|
389
|
+
return this.getByIndex(relIndex);
|
|
390
|
+
},
|
|
391
|
+
10, // max depth
|
|
392
|
+
);
|
|
393
|
+
for (const wayId of resolved.ways) {
|
|
394
|
+
wayIds.add(wayId);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
this.wayMemberIdsCache = wayIds;
|
|
398
|
+
return this.wayMemberIdsCache;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Get relation geometry based on its kind.
|
|
403
|
+
* Returns coordinates suitable for rendering based on relation type.
|
|
404
|
+
* @param index - Relation index
|
|
405
|
+
* @returns Object with geometry data based on relation kind
|
|
406
|
+
*/
|
|
407
|
+
getRelationGeometry(index: number): {
|
|
408
|
+
points?: LonLat[];
|
|
409
|
+
lineStrings?: LonLat[][];
|
|
410
|
+
rings?: LonLat[][][];
|
|
411
|
+
} {
|
|
412
|
+
const relation = this.getByIndex(index);
|
|
413
|
+
if (isAreaRelation(relation)) {
|
|
414
|
+
return {
|
|
415
|
+
rings: buildRelationRings(
|
|
416
|
+
relation,
|
|
417
|
+
(ref) => this.ways.getById(ref),
|
|
418
|
+
(id) => this.nodes.getNodeLonLat({ id }) ?? undefined,
|
|
419
|
+
),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Point relations
|
|
424
|
+
if (isPointRelation(relation)) {
|
|
425
|
+
return {
|
|
426
|
+
points: collectRelationPoints(
|
|
427
|
+
relation,
|
|
428
|
+
(id) => this.nodes.getNodeLonLat({ id }) ?? undefined,
|
|
429
|
+
),
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Line relations
|
|
434
|
+
if (isLineRelation(relation)) {
|
|
435
|
+
return {
|
|
436
|
+
lineStrings: buildRelationLineStrings(
|
|
437
|
+
relation,
|
|
438
|
+
(ref) => this.ways.getById(ref),
|
|
439
|
+
(id) => this.nodes.getNodeLonLat({ id }) ?? undefined,
|
|
440
|
+
),
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Super relations are handled by individual relation geometry methods
|
|
445
|
+
if (isSuperRelation(relation)) {
|
|
446
|
+
return {};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Area relations are handled by buildRelationRings in shared/relation-multipolygon
|
|
450
|
+
// This method doesn't duplicate that logic
|
|
451
|
+
|
|
452
|
+
return {};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Find relations that intersect a bounding box.
|
|
457
|
+
*/
|
|
458
|
+
intersects(bbox: GeoBbox2D, filterFn?: (index: number) => boolean): number[] {
|
|
459
|
+
if (this.size === 0) return [];
|
|
460
|
+
return this.spatialIndex.search(bbox[0], bbox[1], bbox[2], bbox[3], filterFn);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Find relation indexes near a point using great-circle distance.
|
|
465
|
+
* @param lon - Longitude in degrees.
|
|
466
|
+
* @param lat - Latitude in degrees.
|
|
467
|
+
* @param maxResults - Maximum number of results to return.
|
|
468
|
+
* @param maxDistanceKm - Maximum distance in kilometers.
|
|
469
|
+
* @returns Array of relation indexes sorted by distance.
|
|
470
|
+
*/
|
|
471
|
+
neighbors(lon: number, lat: number, maxResults?: number, maxDistanceKm?: number): number[] {
|
|
472
|
+
if (this.size === 0) return [];
|
|
473
|
+
// Use geoflatbush for proper geographic distance calculations
|
|
474
|
+
return geoAround(this.spatialIndex, lon, lat, maxResults, maxDistanceKm);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Get transferable objects for passing to another thread.
|
|
479
|
+
* Only includes spatialIndex if it has been built.
|
|
480
|
+
*/
|
|
481
|
+
override transferables(): RelationsTransferables {
|
|
482
|
+
const base = {
|
|
483
|
+
...super.transferables(),
|
|
484
|
+
memberStart: this.memberStart.array.buffer,
|
|
485
|
+
memberCount: this.memberCount.array.buffer,
|
|
486
|
+
memberRefs: this.memberRefs.array.buffer,
|
|
487
|
+
memberTypes: this.memberTypes.array.buffer,
|
|
488
|
+
memberRoles: this.memberRoles.array.buffer,
|
|
489
|
+
bbox: this.bbox.array.buffer,
|
|
490
|
+
};
|
|
491
|
+
// Only include spatial index if it was built
|
|
492
|
+
if (this.spatialIndexBuilt) {
|
|
493
|
+
return { ...base, spatialIndex: this.spatialIndex.data };
|
|
494
|
+
}
|
|
495
|
+
return base;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Get the approximate memory requirements for a given number of relations in bytes.
|
|
500
|
+
*/
|
|
501
|
+
static getBytesRequired(count: number) {
|
|
502
|
+
if (count === 0) return 0;
|
|
503
|
+
// Approximate members per relation
|
|
504
|
+
let numNodes = count;
|
|
505
|
+
let n = count;
|
|
506
|
+
while (n !== 1) {
|
|
507
|
+
n = Math.ceil(n / 128);
|
|
508
|
+
numNodes += n;
|
|
509
|
+
}
|
|
510
|
+
const indexBytes = (numNodes < 16384 ? 2 : 4) * numNodes;
|
|
511
|
+
const boxesBytes = numNodes * 4 * Float64Array.BYTES_PER_ELEMENT;
|
|
512
|
+
const spatialIndexBytes = 8 + indexBytes + boxesBytes;
|
|
513
|
+
|
|
514
|
+
return (
|
|
515
|
+
Ids.getBytesRequired(count) +
|
|
516
|
+
Tags.getBytesRequired(count) +
|
|
517
|
+
count * Uint32Array.BYTES_PER_ELEMENT + // memberStart
|
|
518
|
+
count * Uint16Array.BYTES_PER_ELEMENT + // memberCount
|
|
519
|
+
count * 4 * Float64Array.BYTES_PER_ELEMENT + // bbox
|
|
520
|
+
spatialIndexBytes
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Update a ContentHasher with relation-specific data (members).
|
|
526
|
+
*/
|
|
527
|
+
override updateHash(hasher: ContentHasher): ContentHasher {
|
|
528
|
+
return super
|
|
529
|
+
.updateHash(hasher)
|
|
530
|
+
.update(this.memberStart.array)
|
|
531
|
+
.update(this.memberCount.array)
|
|
532
|
+
.update(this.memberRefs.array)
|
|
533
|
+
.update(this.memberTypes.array)
|
|
534
|
+
.update(this.memberRoles.array);
|
|
535
|
+
}
|
|
564
536
|
}
|