@osmix/core 0.1.10 → 0.3.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.
Files changed (97) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/LICENSE +21 -0
  3. package/README.md +136 -72
  4. package/dist/contracts.d.ts +27 -0
  5. package/dist/contracts.d.ts.map +1 -0
  6. package/dist/contracts.js +1 -0
  7. package/dist/contracts.js.map +1 -0
  8. package/dist/entities.d.ts +22 -2
  9. package/dist/entities.d.ts.map +1 -1
  10. package/dist/entities.js +54 -10
  11. package/dist/entities.js.map +1 -1
  12. package/dist/ids.d.ts +20 -5
  13. package/dist/ids.d.ts.map +1 -1
  14. package/dist/ids.js +87 -29
  15. package/dist/ids.js.map +1 -1
  16. package/dist/index.d.ts +7 -4
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +6 -4
  19. package/dist/index.js.map +1 -1
  20. package/dist/indirect-kd-index.d.ts +28 -0
  21. package/dist/indirect-kd-index.d.ts.map +1 -0
  22. package/dist/indirect-kd-index.js +163 -0
  23. package/dist/indirect-kd-index.js.map +1 -0
  24. package/dist/limits.d.ts +30 -0
  25. package/dist/limits.d.ts.map +1 -0
  26. package/dist/limits.js +51 -0
  27. package/dist/limits.js.map +1 -0
  28. package/dist/mocks.d.ts.map +1 -1
  29. package/dist/mocks.js.map +1 -1
  30. package/dist/nodes.d.ts +36 -15
  31. package/dist/nodes.d.ts.map +1 -1
  32. package/dist/nodes.js +185 -62
  33. package/dist/nodes.js.map +1 -1
  34. package/dist/osm.d.ts +60 -4
  35. package/dist/osm.d.ts.map +1 -1
  36. package/dist/osm.js +32 -6
  37. package/dist/osm.js.map +1 -1
  38. package/dist/relations.d.ts +11 -4
  39. package/dist/relations.d.ts.map +1 -1
  40. package/dist/relations.js +73 -29
  41. package/dist/relations.js.map +1 -1
  42. package/dist/renumber.d.ts +23 -0
  43. package/dist/renumber.d.ts.map +1 -0
  44. package/dist/renumber.js +64 -0
  45. package/dist/renumber.js.map +1 -0
  46. package/dist/stringtable.d.ts +2 -1
  47. package/dist/stringtable.d.ts.map +1 -1
  48. package/dist/stringtable.js +9 -14
  49. package/dist/stringtable.js.map +1 -1
  50. package/dist/tags.d.ts +45 -12
  51. package/dist/tags.d.ts.map +1 -1
  52. package/dist/tags.js +153 -27
  53. package/dist/tags.js.map +1 -1
  54. package/dist/typed-arrays.d.ts +63 -48
  55. package/dist/typed-arrays.d.ts.map +1 -1
  56. package/dist/typed-arrays.js +184 -107
  57. package/dist/typed-arrays.js.map +1 -1
  58. package/dist/ways.d.ts +52 -3
  59. package/dist/ways.d.ts.map +1 -1
  60. package/dist/ways.js +223 -46
  61. package/dist/ways.js.map +1 -1
  62. package/package.json +45 -31
  63. package/src/contracts.ts +29 -0
  64. package/src/entities.ts +242 -194
  65. package/src/ids.ts +278 -235
  66. package/src/index.ts +22 -13
  67. package/src/indirect-kd-index.ts +212 -0
  68. package/src/limits.ts +73 -0
  69. package/src/mocks.ts +114 -113
  70. package/src/nodes.ts +538 -397
  71. package/src/osm.ts +282 -207
  72. package/src/relations.ts +580 -558
  73. package/src/renumber.ts +80 -0
  74. package/src/stringtable.ts +189 -184
  75. package/src/tags.ts +406 -272
  76. package/src/typed-arrays.ts +307 -205
  77. package/src/types/geoflatbush.d.ts +28 -33
  78. package/src/ways.ts +577 -406
  79. package/test/entity-index-errors.test.ts +41 -0
  80. package/test/ids.bench.ts +33 -0
  81. package/test/ids.test.ts +122 -0
  82. package/test/limits.test.ts +83 -0
  83. package/test/memory.test.ts +29 -30
  84. package/test/no-shared-array-buffer.test.ts +41 -0
  85. package/test/nodes-spatial-index.test.ts +261 -0
  86. package/test/osm-bbox.test.ts +30 -0
  87. package/test/relations.bench.ts +32 -0
  88. package/test/relations.test.ts +124 -0
  89. package/test/renumber.test.ts +54 -0
  90. package/test/spatial-index-rebuild.test.ts +33 -0
  91. package/test/storage-compaction.test.ts +269 -0
  92. package/test/tags-search.test.ts +89 -56
  93. package/test/typed-arrays.test.ts +252 -132
  94. package/tsconfig.build.json +6 -6
  95. package/tsconfig.json +7 -7
  96. package/src/types/geokdbush.d.ts +0 -37
  97. package/src/types/kdbush.d.ts +0 -47
package/src/nodes.ts CHANGED
@@ -1,404 +1,545 @@
1
- import type { OsmPbfBlock, OsmPbfDenseNodes } from "@osmix/pbf"
2
- import { assertValue } from "@osmix/shared/assert"
3
- import type { ContentHasher } from "@osmix/shared/content-hasher"
4
- import { microToDegrees, toMicroDegrees } from "@osmix/shared/coordinates"
5
- import type { GeoBbox2D, OsmNode, OsmTags } from "@osmix/shared/types"
6
- import { around as geoAround } from "geokdbush"
7
- import KDBush from "kdbush"
8
- import { Entities, type EntitiesTransferables } from "./entities.ts"
9
- import { type IdOrIndex, Ids } from "./ids.ts"
10
- import type StringTable from "./stringtable.ts"
11
- import { Tags } from "./tags.ts"
12
- import {
13
- BufferConstructor,
14
- type BufferType,
15
- ResizeableTypedArray as RTA,
16
- } from "./typed-arrays.ts"
17
-
18
- export interface NodesTransferables<T extends BufferType = BufferType>
19
- extends EntitiesTransferables<T> {
20
- lons: T
21
- lats: T
22
- bbox: GeoBbox2D
23
- /** Optional - can be rebuilt via buildSpatialIndex() */
24
- spatialIndex?: T
1
+ import { microToDegrees, OSM_COORD_SCALE, toMicroDegrees } from "@osmix/geo/coordinates";
2
+ import type { OsmPbfBlock, OsmPbfDenseNodes } from "@osmix/pbf";
3
+ import { assertValue } from "@osmix/shared/assert";
4
+ import type { ContentHasher } from "@osmix/shared/content-hasher";
5
+ import type { GeoBbox2D, OsmNode, OsmTags } from "@osmix/types";
6
+
7
+ import { Entities, type EntitiesTransferables } from "./entities.ts";
8
+ import { type IdOrIndex, Ids } from "./ids.ts";
9
+ import { IndirectKdIndex } from "./indirect-kd-index.ts";
10
+ import type StringTable from "./stringtable.ts";
11
+ import { Tags } from "./tags.ts";
12
+ import { type BufferType, ResizeableTypedArray as RTA } from "./typed-arrays.ts";
13
+
14
+ const EARTH_RADIUS_KM = 6371.0088;
15
+ const COORDINATE_PADDING_DEGREES = 1 / OSM_COORD_SCALE;
16
+
17
+ export type NodeSpatialIndexKind = "all" | "tagged";
18
+
19
+ export class SpatialIndexNotBuiltError extends Error {
20
+ readonly code = "SPATIAL_INDEX_NOT_BUILT";
21
+ readonly entityType = "node";
22
+ readonly indexKind: NodeSpatialIndexKind;
23
+
24
+ constructor(indexKind: NodeSpatialIndexKind) {
25
+ super(`The ${indexKind} node spatial index has not been built.`);
26
+ this.name = "SpatialIndexNotBuiltError";
27
+ this.indexKind = indexKind;
28
+ }
29
+ }
30
+
31
+ export interface NodesTransferables<
32
+ T extends BufferType = BufferType,
33
+ > extends EntitiesTransferables<T> {
34
+ lons: T;
35
+ lats: T;
36
+ bbox: GeoBbox2D;
37
+ /** Optional all-node Uint32 permutation; can be rebuilt via buildSpatialIndex("all"). */
38
+ allSpatialIndex?: T;
39
+ /** Optional tagged-node Uint32 permutation; can be rebuilt via buildSpatialIndex("tagged"). */
40
+ taggedSpatialIndex?: T;
25
41
  }
26
42
 
27
43
  export interface AddNodeOptions {
28
- filter?: (node: OsmNode) => boolean
44
+ filter?: (node: OsmNode) => boolean;
29
45
  }
30
46
 
31
47
  export class Nodes extends Entities<OsmNode> {
32
- /**
33
- * Coordinates are stored as integer microdegrees (Int32Array).
34
- * Use OSM_COORD_SCALE (1e7) to convert between degrees and microdegrees.
35
- */
36
- private lons: RTA<Int32Array>
37
- private lats: RTA<Int32Array>
38
- // Bbox stored in microdegrees internally
39
- private bbox: [
40
- minLon: number,
41
- minLat: number,
42
- maxLon: number,
43
- maxLat: number,
44
- ] = [
45
- Number.MAX_SAFE_INTEGER,
46
- Number.MAX_SAFE_INTEGER,
47
- Number.MIN_SAFE_INTEGER,
48
- Number.MIN_SAFE_INTEGER,
49
- ]
50
- // Spatial index stores coordinates in degrees (Float64Array) for geokdbush compatibility
51
- private spatialIndex: KDBush = new KDBush(
52
- 0,
53
- 128,
54
- Float64Array,
55
- BufferConstructor,
56
- )
57
- // Track if spatial index was properly built (vs default empty)
58
- private spatialIndexBuilt = false
59
-
60
- /**
61
- * Create a new Nodes index.
62
- */
63
- constructor(stringTable: StringTable, transferables?: NodesTransferables) {
64
- if (transferables) {
65
- super(
66
- "node",
67
- new Ids(transferables),
68
- new Tags(stringTable, transferables),
69
- )
70
- this.lons = RTA.from(Int32Array, transferables.lons)
71
- this.lats = RTA.from(Int32Array, transferables.lats)
72
- // Only load spatial index if provided (not stored in IndexedDB)
73
- if (transferables.spatialIndex?.byteLength) {
74
- this.spatialIndex = KDBush.from(transferables.spatialIndex)
75
- this.spatialIndexBuilt = true
76
- }
77
- this.bbox = transferables.bbox
78
- this.indexBuilt = true
79
- } else {
80
- super("node", new Ids(), new Tags(stringTable))
81
- this.lons = new RTA(Int32Array)
82
- this.lats = new RTA(Int32Array)
83
- // Spatial index uses Float64Array with degrees for geokdbush compatibility
84
- this.spatialIndex = new KDBush(0, 128, Float64Array, BufferConstructor)
85
- }
86
- }
87
-
88
- /**
89
- * Add a single node to the index.
90
- */
91
- addNode(node: OsmNode): number {
92
- const nodeIndex = this.addEntity(node.id, node.tags ?? {})
93
-
94
- const lonMicro = toMicroDegrees(node.lon)
95
- const latMicro = toMicroDegrees(node.lat)
96
-
97
- this.lons.push(lonMicro)
98
- this.lats.push(latMicro)
99
-
100
- if (node.lon < this.bbox[0]) this.bbox[0] = node.lon
101
- if (node.lat < this.bbox[1]) this.bbox[1] = node.lat
102
- if (node.lon > this.bbox[2]) this.bbox[2] = node.lon
103
- if (node.lat > this.bbox[3]) this.bbox[3] = node.lat
104
-
105
- return nodeIndex
106
- }
107
-
108
- /**
109
- * Add dense nodes from a PBF block.
110
- */
111
- addDenseNodes(
112
- dense: OsmPbfDenseNodes,
113
- block: OsmPbfBlock,
114
- blockStringIndexMap: Uint32Array,
115
- filter?: (node: OsmNode) => boolean,
116
- ): number {
117
- // PBF block already has offsets and granularity converted to degrees
118
- const lon_offset = block.lon_offset ?? 0
119
- const lat_offset = block.lat_offset ?? 0
120
- const granularity = block.granularity ?? 1e7
121
-
122
- const delta = {
123
- id: 0,
124
- lat: 0,
125
- lon: 0,
126
- timestamp: 0,
127
- changeset: 0,
128
- uid: 0,
129
- user_sid: 0,
130
- }
131
-
132
- const getStringTableIndex = (keyIndex: number) => {
133
- const key = dense.keys_vals[keyIndex]
134
- assertValue(key, "Block string key is undefined")
135
- const index = blockStringIndexMap[key]
136
- assertValue(index, "Block string not found")
137
- return index
138
- }
139
-
140
- let keysValsIndex = 0
141
- let added = 0
142
- for (let i = 0; i < dense.id.length; i++) {
143
- const idSid = dense.id[i]
144
- const latSid = dense.lat[i]
145
- const lonSid = dense.lon[i]
146
- assertValue(idSid, "ID SID is undefined")
147
- assertValue(latSid, "Latitude SID is undefined")
148
- assertValue(lonSid, "Longitude SID is undefined")
149
-
150
- delta.id += idSid
151
- delta.lat += latSid
152
- delta.lon += lonSid
153
-
154
- // Calculate degrees from PBF delta encoding
155
- const lon = lon_offset + delta.lon / granularity
156
- const lat = lat_offset + delta.lat / granularity
157
-
158
- // Convert to microdegrees for storage
159
- const lonMicro = toMicroDegrees(lon)
160
- const latMicro = toMicroDegrees(lat)
161
-
162
- const tagKeys: number[] = []
163
- const tagValues: number[] = []
164
- if (dense.keys_vals.length > 0) {
165
- while (dense.keys_vals[keysValsIndex] !== 0) {
166
- const key = getStringTableIndex(keysValsIndex)
167
- const val = getStringTableIndex(keysValsIndex + 1)
168
- if (key && val) {
169
- tagKeys.push(key)
170
- tagValues.push(val)
171
- }
172
- keysValsIndex += 2
173
- }
174
- keysValsIndex++
175
- }
176
-
177
- const shouldInclude = filter
178
- ? filter({
179
- id: delta.id,
180
- lon,
181
- lat,
182
- tags: this.tags.getTagsFromIndices(tagKeys, tagValues),
183
- })
184
- : true
185
- if (!shouldInclude) continue
186
-
187
- this.addEntity(delta.id, tagKeys, tagValues)
188
- this.lons.push(lonMicro)
189
- this.lats.push(latMicro)
190
-
191
- if (lon < this.bbox[0]) this.bbox[0] = lon
192
- if (lat < this.bbox[1]) this.bbox[1] = lat
193
- if (lon > this.bbox[2]) this.bbox[2] = lon
194
- if (lat > this.bbox[3]) this.bbox[3] = lat
195
- added++
196
- }
197
-
198
- return added
199
- }
200
-
201
- /**
202
- * Compact the internal arrays to free up memory.
203
- */
204
- buildEntityIndex() {
205
- this.lons.compact()
206
- this.lats.compact()
207
- }
208
-
209
- /**
210
- * Build the spatial index for nodes.
211
- * Spatial index stores degrees (Float64Array) for geokdbush compatibility.
212
- */
213
- buildSpatialIndex() {
214
- console.time("NodeIndex.buildSpatialIndex")
215
- // Use Float64Array with degrees for geokdbush geographic queries
216
- this.spatialIndex = new KDBush(
217
- this.size,
218
- 64,
219
- Float64Array,
220
- BufferConstructor,
221
- )
222
- for (let i = 0; i < this.size; i++) {
223
- // Convert microdegrees to degrees for spatial index
224
- const lon = microToDegrees(this.lons.at(i))
225
- const lat = microToDegrees(this.lats.at(i))
226
- this.spatialIndex.add(lon, lat)
227
- }
228
- this.spatialIndex.finish()
229
- this.spatialIndexBuilt = true
230
- console.timeEnd("NodeIndex.buildSpatialIndex")
231
- }
232
-
233
- /**
234
- * Check if the spatial index has been built.
235
- */
236
- hasSpatialIndex(): boolean {
237
- return this.spatialIndexBuilt
238
- }
239
-
240
- /**
241
- * Get the bounding box of all nodes.
242
- */
243
- getBbox(): GeoBbox2D {
244
- return this.bbox
245
- }
246
-
247
- /**
248
- * Get the bounding box of a specific node.
249
- */
250
- getEntityBbox(i: IdOrIndex): GeoBbox2D {
251
- const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0]
252
- const lon = microToDegrees(this.lons.at(index))
253
- const lat = microToDegrees(this.lats.at(index))
254
- return [lon, lat, lon, lat] as GeoBbox2D
255
- }
256
-
257
- /**
258
- * Get the longitude and latitude of a specific node.
259
- */
260
- getNodeLonLat(i: IdOrIndex): [number, number] {
261
- const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0]
262
- return [
263
- microToDegrees(this.lons.at(index)),
264
- microToDegrees(this.lats.at(index)),
265
- ]
266
- }
267
-
268
- /**
269
- * Get the full node entity.
270
- */
271
- getFullEntity(index: number, id: number, tags?: OsmTags): OsmNode {
272
- const [lon, lat] = this.getNodeLonLat({ index })
273
- if (tags) {
274
- return {
275
- id,
276
- lat,
277
- lon,
278
- tags,
279
- }
280
- }
281
- return {
282
- id,
283
- lat,
284
- lon,
285
- }
286
- }
287
-
288
- // Spatial operations
289
- /**
290
- * Find node indexes within a bounding box.
291
- */
292
- findIndexesWithinBbox(bbox: GeoBbox2D): number[] {
293
- // Spatial index stores degrees, so query with degrees directly
294
- return this.spatialIndex.range(bbox[0], bbox[1], bbox[2], bbox[3])
295
- }
296
-
297
- /**
298
- * Find node indexes within a radius of a point.
299
- * Uses geokdbush for proper great-circle distance calculations.
300
- * @param lon - Longitude in degrees.
301
- * @param lat - Latitude in degrees.
302
- * @param radiusKm - Radius in kilometers.
303
- * @returns Array of node indexes within the radius.
304
- */
305
- findIndexesWithinRadius(
306
- lon: number,
307
- lat: number,
308
- radiusKm: number,
309
- ): number[] {
310
- // Use geokdbush for proper geographic distance calculations
311
- return geoAround(
312
- this.spatialIndex,
313
- lon,
314
- lat,
315
- Number.POSITIVE_INFINITY,
316
- radiusKm,
317
- )
318
- }
319
-
320
- /**
321
- * Get nodes within a bounding box.
322
- * @param bbox - The bounding box to search within.
323
- * @param include - A function to filter nodes. If provided, only nodes for which the function returns true will be included.
324
- * @returns An object containing the IDs and positions of the nodes within the bounding box.
325
- */
326
- withinBbox(
327
- bbox: GeoBbox2D,
328
- include?: (i: number) => boolean,
329
- ): {
330
- ids: Float64Array
331
- positions: Float64Array
332
- } {
333
- console.time("Nodes.withinBbox")
334
- const nodeCandidates = this.findIndexesWithinBbox(bbox)
335
- const nodePositions = new Float64Array(nodeCandidates.length * 2)
336
- const ids = new Float64Array(nodeCandidates.length)
337
-
338
- let skipped = 0
339
- nodeCandidates.forEach((nodeIndex, i) => {
340
- if (include && !include(nodeIndex)) {
341
- skipped++
342
- return
343
- }
344
-
345
- const [lon, lat] = this.getNodeLonLat({ index: nodeIndex })
346
- ids[i - skipped] = this.ids.at(nodeIndex)
347
- nodePositions[(i - skipped) * 2] = lon
348
- nodePositions[(i - skipped) * 2 + 1] = lat
349
- })
350
- console.timeEnd("Nodes.withinBbox")
351
- return {
352
- ids: ids.subarray(0, nodeCandidates.length - skipped),
353
- positions: nodePositions.slice(0, (nodeCandidates.length - skipped) * 2),
354
- }
355
- }
356
-
357
- /**
358
- * Get transferable objects for passing to another thread.
359
- * Only includes spatialIndex if it has been built.
360
- */
361
- override transferables(): NodesTransferables {
362
- const base = {
363
- ...super.transferables(),
364
- lons: this.lons.array.buffer,
365
- lats: this.lats.array.buffer,
366
- bbox: this.bbox,
367
- }
368
- // Only include spatial index if it was built
369
- if (this.spatialIndexBuilt) {
370
- return { ...base, spatialIndex: this.spatialIndex.data }
371
- }
372
- return base
373
- }
374
-
375
- /**
376
- * Get the approximate memory requirements for a given number of nodes in bytes.
377
- */
378
- static getBytesRequired(count: number) {
379
- if (count === 0) return 0
380
- const indexBytes = (count < 65536 ? 2 : 4) * count
381
- // Spatial index stores coordinates in degrees (Float64Array)
382
- const coordsBytes = count * 2 * Float64Array.BYTES_PER_ELEMENT
383
- const padding = (8 - (indexBytes % 8)) % 8
384
- const spatialIndexBytes = 8 + indexBytes + coordsBytes + padding
385
-
386
- return (
387
- Ids.getBytesRequired(count) +
388
- Tags.getBytesRequired(count) +
389
- count * Int32Array.BYTES_PER_ELEMENT + // lons (stored in microdegrees)
390
- count * Int32Array.BYTES_PER_ELEMENT + // lats (stored in microdegrees)
391
- spatialIndexBytes
392
- )
393
- }
394
-
395
- /**
396
- * Update a ContentHasher with node-specific data (coordinates).
397
- */
398
- override updateHash(hasher: ContentHasher): ContentHasher {
399
- return super
400
- .updateHash(hasher)
401
- .update(this.lons.array)
402
- .update(this.lats.array)
403
- }
48
+ /**
49
+ * Coordinates are stored as integer microdegrees (Int32Array).
50
+ * Use OSM_COORD_SCALE (1e7) to convert between degrees and microdegrees.
51
+ */
52
+ private lons: RTA<Int32Array>;
53
+ private lats: RTA<Int32Array>;
54
+ // Bounding box is exposed and transferred in degrees.
55
+ private bbox: [minLon: number, minLat: number, maxLon: number, maxLat: number] = [
56
+ Number.MAX_SAFE_INTEGER,
57
+ Number.MAX_SAFE_INTEGER,
58
+ Number.MIN_SAFE_INTEGER,
59
+ Number.MIN_SAFE_INTEGER,
60
+ ];
61
+ private allSpatialIndex?: IndirectKdIndex;
62
+ private taggedSpatialIndex?: IndirectKdIndex;
63
+
64
+ /**
65
+ * Create a new Nodes index.
66
+ */
67
+ constructor(stringTable: StringTable, transferables?: NodesTransferables) {
68
+ if (transferables) {
69
+ super("node", new Ids(transferables), new Tags(stringTable, transferables));
70
+ this.lons = RTA.from(Int32Array, transferables.lons);
71
+ this.lats = RTA.from(Int32Array, transferables.lats);
72
+ if (transferables.allSpatialIndex !== undefined) {
73
+ this.allSpatialIndex = IndirectKdIndex.from(
74
+ this.lons.array,
75
+ this.lats.array,
76
+ transferables.allSpatialIndex,
77
+ );
78
+ }
79
+ if (transferables.taggedSpatialIndex !== undefined) {
80
+ this.taggedSpatialIndex = IndirectKdIndex.from(
81
+ this.lons.array,
82
+ this.lats.array,
83
+ transferables.taggedSpatialIndex,
84
+ );
85
+ }
86
+ this.bbox = transferables.bbox;
87
+ this.indexBuilt = true;
88
+ } else {
89
+ super("node", new Ids(), new Tags(stringTable));
90
+ this.lons = new RTA(Int32Array);
91
+ this.lats = new RTA(Int32Array);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Add a single node to the index.
97
+ */
98
+ addNode(node: OsmNode): number {
99
+ const nodeIndex = this.addEntity(node.id, node.tags ?? {});
100
+
101
+ const lonMicro = toMicroDegrees(node.lon);
102
+ const latMicro = toMicroDegrees(node.lat);
103
+
104
+ this.lons.push(lonMicro);
105
+ this.lats.push(latMicro);
106
+
107
+ if (node.lon < this.bbox[0]) this.bbox[0] = node.lon;
108
+ if (node.lat < this.bbox[1]) this.bbox[1] = node.lat;
109
+ if (node.lon > this.bbox[2]) this.bbox[2] = node.lon;
110
+ if (node.lat > this.bbox[3]) this.bbox[3] = node.lat;
111
+
112
+ return nodeIndex;
113
+ }
114
+
115
+ /**
116
+ * Add dense nodes from a PBF block.
117
+ */
118
+ addDenseNodes(
119
+ dense: OsmPbfDenseNodes,
120
+ block: OsmPbfBlock,
121
+ blockStringIndexMap: Uint32Array,
122
+ filter?: (node: OsmNode) => boolean,
123
+ ): number {
124
+ // PBF block already has offsets and granularity converted to degrees
125
+ const lon_offset = block.lon_offset ?? 0;
126
+ const lat_offset = block.lat_offset ?? 0;
127
+ const granularity = block.granularity ?? 1e7;
128
+
129
+ const delta = {
130
+ id: 0,
131
+ lat: 0,
132
+ lon: 0,
133
+ timestamp: 0,
134
+ changeset: 0,
135
+ uid: 0,
136
+ user_sid: 0,
137
+ };
138
+
139
+ const getStringTableIndex = (keyIndex: number) => {
140
+ const key = dense.keys_vals[keyIndex];
141
+ assertValue(key, "Block string key is undefined");
142
+ const index = blockStringIndexMap[key];
143
+ assertValue(index, "Block string not found");
144
+ return index;
145
+ };
146
+
147
+ let keysValsIndex = 0;
148
+ let added = 0;
149
+ for (let i = 0; i < dense.id.length; i++) {
150
+ const idSid = dense.id[i];
151
+ const latSid = dense.lat[i];
152
+ const lonSid = dense.lon[i];
153
+ assertValue(idSid, "ID SID is undefined");
154
+ assertValue(latSid, "Latitude SID is undefined");
155
+ assertValue(lonSid, "Longitude SID is undefined");
156
+
157
+ delta.id += idSid;
158
+ delta.lat += latSid;
159
+ delta.lon += lonSid;
160
+
161
+ // Calculate degrees from PBF delta encoding
162
+ const lon = lon_offset + delta.lon / granularity;
163
+ const lat = lat_offset + delta.lat / granularity;
164
+
165
+ // Convert to microdegrees for storage
166
+ const lonMicro = toMicroDegrees(lon);
167
+ const latMicro = toMicroDegrees(lat);
168
+
169
+ const tagKeys: number[] = [];
170
+ const tagValues: number[] = [];
171
+ if (dense.keys_vals.length > 0) {
172
+ while (dense.keys_vals[keysValsIndex] !== 0) {
173
+ const key = getStringTableIndex(keysValsIndex);
174
+ const val = getStringTableIndex(keysValsIndex + 1);
175
+ if (key && val) {
176
+ tagKeys.push(key);
177
+ tagValues.push(val);
178
+ }
179
+ keysValsIndex += 2;
180
+ }
181
+ keysValsIndex++;
182
+ }
183
+
184
+ const shouldInclude = filter
185
+ ? filter({
186
+ id: delta.id,
187
+ lon,
188
+ lat,
189
+ tags: this.tags.getTagsFromIndices(tagKeys, tagValues),
190
+ })
191
+ : true;
192
+ if (!shouldInclude) continue;
193
+
194
+ this.addEntity(delta.id, tagKeys, tagValues);
195
+ this.lons.push(lonMicro);
196
+ this.lats.push(latMicro);
197
+
198
+ if (lon < this.bbox[0]) this.bbox[0] = lon;
199
+ if (lat < this.bbox[1]) this.bbox[1] = lat;
200
+ if (lon > this.bbox[2]) this.bbox[2] = lon;
201
+ if (lat > this.bbox[3]) this.bbox[3] = lat;
202
+ added++;
203
+ }
204
+
205
+ return added;
206
+ }
207
+
208
+ /**
209
+ * Compact the internal arrays to free up memory.
210
+ */
211
+ buildEntityIndex() {
212
+ this.lons.compact();
213
+ this.lats.compact();
214
+ }
215
+
216
+ /**
217
+ * Build one of the independent node spatial indexes.
218
+ */
219
+ buildSpatialIndex(kind: NodeSpatialIndexKind = "all") {
220
+ if (this.hasSpatialIndex(kind)) return;
221
+
222
+ console.time(`NodeIndex.buildSpatialIndex.${kind}`);
223
+ if (kind === "all") {
224
+ this.allSpatialIndex = IndirectKdIndex.build(
225
+ this.lons.array,
226
+ this.lats.array,
227
+ this.size,
228
+ (indexes) => {
229
+ for (let i = 0; i < indexes.length; i++) indexes[i] = i;
230
+ },
231
+ );
232
+ } else {
233
+ this.taggedSpatialIndex = IndirectKdIndex.build(
234
+ this.lons.array,
235
+ this.lats.array,
236
+ this.taggedSize,
237
+ (indexes) => {
238
+ let position = 0;
239
+ for (const entityIndex of this.tags.taggedEntityIndexes()) {
240
+ indexes[position++] = entityIndex;
241
+ }
242
+ if (position !== indexes.length) {
243
+ throw new Error(
244
+ `Expected ${indexes.length} tagged nodes while building index, received ${position}`,
245
+ );
246
+ }
247
+ },
248
+ );
249
+ }
250
+ console.timeEnd(`NodeIndex.buildSpatialIndex.${kind}`);
251
+ }
252
+
253
+ /**
254
+ * Check if the spatial index has been built.
255
+ */
256
+ hasSpatialIndex(kind: NodeSpatialIndexKind = "all"): boolean {
257
+ return kind === "all"
258
+ ? this.allSpatialIndex !== undefined
259
+ : this.taggedSpatialIndex !== undefined;
260
+ }
261
+
262
+ /** Number of nodes that carry at least one tag. */
263
+ get taggedSize(): number {
264
+ return this.tags.taggedEntityCount;
265
+ }
266
+
267
+ /**
268
+ * Get the bounding box of all nodes, or `null` when there are no nodes.
269
+ */
270
+ getBbox(): GeoBbox2D | null {
271
+ // The bbox starts inverted and only becomes valid once a node extends it.
272
+ if (this.bbox[0] > this.bbox[2] || this.bbox[1] > this.bbox[3]) return null;
273
+ return this.bbox;
274
+ }
275
+
276
+ /**
277
+ * Get the bounding box of a specific node.
278
+ */
279
+ getEntityBbox(i: IdOrIndex): GeoBbox2D {
280
+ const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0];
281
+ const lon = microToDegrees(this.lons.at(index));
282
+ const lat = microToDegrees(this.lats.at(index));
283
+ return [lon, lat, lon, lat] as GeoBbox2D;
284
+ }
285
+
286
+ /**
287
+ * Get the longitude and latitude of a specific node.
288
+ * ID lookups return `null` when the node is not present; index lookups remain strict.
289
+ */
290
+ getNodeLonLat(i: { index: number }): [number, number];
291
+ getNodeLonLat(i: { id: number }): [number, number] | null;
292
+ getNodeLonLat(i: IdOrIndex): [number, number] | null {
293
+ const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0];
294
+ if (index === -1) return null;
295
+ return [microToDegrees(this.lons.at(index)), microToDegrees(this.lats.at(index))];
296
+ }
297
+
298
+ /**
299
+ * Get the full node entity.
300
+ */
301
+ getFullEntity(index: number, id: number, tags?: OsmTags): OsmNode {
302
+ const [lon, lat] = this.getNodeLonLat({ index });
303
+ if (tags) {
304
+ return {
305
+ id,
306
+ lat,
307
+ lon,
308
+ tags,
309
+ };
310
+ }
311
+ return {
312
+ id,
313
+ lat,
314
+ lon,
315
+ };
316
+ }
317
+
318
+ // Spatial operations
319
+ /**
320
+ * Find node indexes within a bounding box.
321
+ */
322
+ findIndexesWithinBbox(bbox: GeoBbox2D): number[] {
323
+ return findIndexesWithinBbox(this.getSpatialIndex("all"), bbox);
324
+ }
325
+
326
+ /** Find tagged node indexes within a bounding box. */
327
+ findTaggedIndexesWithinBbox(bbox: GeoBbox2D): number[] {
328
+ return findIndexesWithinBbox(this.getSpatialIndex("tagged"), bbox);
329
+ }
330
+
331
+ /**
332
+ * Find node indexes within a radius of a point.
333
+ * Uses an exact haversine-distance filter over a conservative KD bbox query.
334
+ * @param lon - Longitude in degrees.
335
+ * @param lat - Latitude in degrees.
336
+ * @param radiusKm - Radius in kilometers.
337
+ * @returns Array of node indexes within the radius.
338
+ */
339
+ findIndexesWithinRadius(lon: number, lat: number, radiusKm: number): number[] {
340
+ const spatialIndex = this.getSpatialIndex("all");
341
+ if (!Number.isFinite(lon) || !Number.isFinite(lat)) {
342
+ throw new RangeError("Longitude and latitude must be finite numbers");
343
+ }
344
+ if (lat < -90 || lat > 90) throw new RangeError("Latitude must be between -90 and 90");
345
+ if (Number.isNaN(radiusKm) || radiusKm < 0) return [];
346
+
347
+ const normalizedLon = normalizeLongitude(lon);
348
+ const bbox = radiusBoundingBox(normalizedLon, lat, radiusKm);
349
+ const candidates = findIndexesWithinBbox(spatialIndex, bbox);
350
+ const matches: { distance: number; index: number }[] = [];
351
+ for (const index of candidates) {
352
+ const nodeLon = microToDegrees(this.lons.at(index));
353
+ const nodeLat = microToDegrees(this.lats.at(index));
354
+ const distance = haversineDistanceKm(normalizedLon, lat, nodeLon, nodeLat);
355
+ if (distance <= radiusKm) matches.push({ distance, index });
356
+ }
357
+ matches.sort((a, b) => a.distance - b.distance || a.index - b.index);
358
+ return matches.map(({ index }) => index);
359
+ }
360
+
361
+ /**
362
+ * Get nodes within a bounding box.
363
+ * @param bbox - The bounding box to search within.
364
+ * @param include - A function to filter nodes. If provided, only nodes for which the function returns true will be included.
365
+ * @returns An object containing the IDs and positions of the nodes within the bounding box.
366
+ */
367
+ withinBbox(
368
+ bbox: GeoBbox2D,
369
+ include?: (i: number) => boolean,
370
+ ): {
371
+ ids: Float64Array;
372
+ positions: Float64Array;
373
+ } {
374
+ console.time("Nodes.withinBbox");
375
+ const nodeCandidates = this.findIndexesWithinBbox(bbox);
376
+ const nodePositions = new Float64Array(nodeCandidates.length * 2);
377
+ const ids = new Float64Array(nodeCandidates.length);
378
+
379
+ let skipped = 0;
380
+ nodeCandidates.forEach((nodeIndex, i) => {
381
+ if (include && !include(nodeIndex)) {
382
+ skipped++;
383
+ return;
384
+ }
385
+
386
+ const [lon, lat] = this.getNodeLonLat({ index: nodeIndex });
387
+ ids[i - skipped] = this.ids.at(nodeIndex);
388
+ nodePositions[(i - skipped) * 2] = lon;
389
+ nodePositions[(i - skipped) * 2 + 1] = lat;
390
+ });
391
+ console.timeEnd("Nodes.withinBbox");
392
+ return {
393
+ ids: ids.subarray(0, nodeCandidates.length - skipped),
394
+ positions: nodePositions.slice(0, (nodeCandidates.length - skipped) * 2),
395
+ };
396
+ }
397
+
398
+ /**
399
+ * Get transferable objects for passing to another thread.
400
+ * Only includes spatial indexes that have been built.
401
+ */
402
+ override transferables(): NodesTransferables {
403
+ const base = {
404
+ ...super.transferables(),
405
+ lons: this.lons.array.buffer,
406
+ lats: this.lats.array.buffer,
407
+ bbox: this.bbox,
408
+ };
409
+ return {
410
+ ...base,
411
+ ...(this.allSpatialIndex ? { allSpatialIndex: this.allSpatialIndex.buffer } : {}),
412
+ ...(this.taggedSpatialIndex ? { taggedSpatialIndex: this.taggedSpatialIndex.buffer } : {}),
413
+ };
414
+ }
415
+
416
+ /**
417
+ * Get the approximate memory requirements for a given number of nodes in bytes.
418
+ */
419
+ static getBytesRequired(count: number, taggedCount = count, tagCount = 0) {
420
+ if (count === 0) return 0;
421
+
422
+ return (
423
+ Ids.getBytesRequired(count) +
424
+ Tags.getBytesRequired(count, taggedCount, tagCount) +
425
+ count * Int32Array.BYTES_PER_ELEMENT + // lons (stored in microdegrees)
426
+ count * Int32Array.BYTES_PER_ELEMENT + // lats (stored in microdegrees)
427
+ Nodes.getSpatialIndexBytesRequired(count) +
428
+ Nodes.getSpatialIndexBytesRequired(taggedCount)
429
+ );
430
+ }
431
+
432
+ /** Exact bytes in an indirect node spatial-index permutation. */
433
+ static getSpatialIndexBytesRequired(count: number): number {
434
+ return count * Uint32Array.BYTES_PER_ELEMENT;
435
+ }
436
+
437
+ /**
438
+ * Update a ContentHasher with node-specific data (coordinates).
439
+ */
440
+ override updateHash(hasher: ContentHasher): ContentHasher {
441
+ return super.updateHash(hasher).update(this.lons.array).update(this.lats.array);
442
+ }
443
+
444
+ private getSpatialIndex(kind: NodeSpatialIndexKind): IndirectKdIndex {
445
+ const index = kind === "all" ? this.allSpatialIndex : this.taggedSpatialIndex;
446
+ if (!index) throw new SpatialIndexNotBuiltError(kind);
447
+ return index;
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Round-trip float error tolerance for degree → microdegree conversion.
453
+ * `microToDegrees(x) * OSM_COORD_SCALE` can differ from `x` by a few ulps
454
+ * (~1e-6 at ±1.8e9), so ceil/floor without a tolerance could exclude a node
455
+ * whose exact stored coordinate sits on the query boundary.
456
+ */
457
+ const MICRO_EPSILON = 1e-5;
458
+
459
+ /** Smallest microdegree integer inside an inclusive degree lower bound. */
460
+ function microLowerBound(degrees: number): number {
461
+ return Math.ceil(degrees * OSM_COORD_SCALE - MICRO_EPSILON);
462
+ }
463
+
464
+ /** Largest microdegree integer inside an inclusive degree upper bound. */
465
+ function microUpperBound(degrees: number): number {
466
+ return Math.floor(degrees * OSM_COORD_SCALE + MICRO_EPSILON);
467
+ }
468
+
469
+ function findIndexesWithinBbox(index: IndirectKdIndex, bbox: GeoBbox2D): number[] {
470
+ const [minLon, minLat, maxLon, maxLat] = bbox;
471
+ if (minLat > maxLat) return [];
472
+
473
+ const microMinLat = microLowerBound(minLat);
474
+ const microMaxLat = microUpperBound(maxLat);
475
+ if (microMinLat > microMaxLat) return [];
476
+
477
+ let result: number[];
478
+ if (minLon <= maxLon) {
479
+ result = index.range(
480
+ microLowerBound(minLon),
481
+ microMinLat,
482
+ microUpperBound(maxLon),
483
+ microMaxLat,
484
+ );
485
+ } else {
486
+ result = index
487
+ .range(microLowerBound(minLon), microMinLat, 180 * OSM_COORD_SCALE, microMaxLat)
488
+ .concat(
489
+ index.range(-180 * OSM_COORD_SCALE, microMinLat, microUpperBound(maxLon), microMaxLat),
490
+ );
491
+ }
492
+ return result;
493
+ }
494
+
495
+ function radiusBoundingBox(lon: number, lat: number, radiusKm: number): GeoBbox2D {
496
+ if (!Number.isFinite(radiusKm)) return [-180, -90, 180, 90];
497
+ const angularRadius = radiusKm / EARTH_RADIUS_KM;
498
+ if (angularRadius >= Math.PI) return [-180, -90, 180, 90];
499
+
500
+ const latRadians = degreesToRadians(lat);
501
+ const minLatRadians = Math.max(-Math.PI / 2, latRadians - angularRadius);
502
+ const maxLatRadians = Math.min(Math.PI / 2, latRadians + angularRadius);
503
+ const minLat = Math.max(-90, radiansToDegrees(minLatRadians) - COORDINATE_PADDING_DEGREES);
504
+ const maxLat = Math.min(90, radiansToDegrees(maxLatRadians) + COORDINATE_PADDING_DEGREES);
505
+
506
+ if (
507
+ angularRadius >= Math.PI / 2 ||
508
+ minLatRadians <= -Math.PI / 2 ||
509
+ maxLatRadians >= Math.PI / 2
510
+ ) {
511
+ return [-180, minLat, 180, maxLat];
512
+ }
513
+
514
+ const ratio = Math.sin(angularRadius) / Math.cos(latRadians);
515
+ const deltaLon =
516
+ radiansToDegrees(Math.asin(Math.min(1, Math.max(-1, ratio)))) + COORDINATE_PADDING_DEGREES;
517
+ const rawMinLon = lon - deltaLon;
518
+ const rawMaxLon = lon + deltaLon;
519
+ return [normalizeLongitude(rawMinLon), minLat, normalizeLongitude(rawMaxLon), maxLat];
520
+ }
521
+
522
+ function haversineDistanceKm(lon1: number, lat1: number, lon2: number, lat2: number): number {
523
+ const latDelta = degreesToRadians(lat2 - lat1);
524
+ const lonDelta = degreesToRadians(lon2 - lon1);
525
+ const lat1Radians = degreesToRadians(lat1);
526
+ const lat2Radians = degreesToRadians(lat2);
527
+ const a =
528
+ Math.sin(latDelta / 2) ** 2 +
529
+ Math.sin(lonDelta / 2) ** 2 * Math.cos(lat1Radians) * Math.cos(lat2Radians);
530
+ return 2 * EARTH_RADIUS_KM * Math.atan2(Math.sqrt(a), Math.sqrt(Math.max(0, 1 - a)));
531
+ }
532
+
533
+ function normalizeLongitude(lon: number): number {
534
+ if (lon >= -180 && lon <= 180) return lon;
535
+ const normalized = ((((lon + 180) % 360) + 360) % 360) - 180;
536
+ return normalized === -180 && lon > 0 ? 180 : normalized;
537
+ }
538
+
539
+ function degreesToRadians(value: number): number {
540
+ return value * (Math.PI / 180);
541
+ }
542
+
543
+ function radiansToDegrees(value: number): number {
544
+ return value * (180 / Math.PI);
404
545
  }