@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/nodes.ts
CHANGED
|
@@ -1,404 +1,371 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import type { GeoBbox2D, OsmNode, OsmTags } from "@osmix/
|
|
6
|
-
import { around as geoAround } from "geokdbush"
|
|
7
|
-
import KDBush from "kdbush"
|
|
8
|
-
|
|
9
|
-
import { type
|
|
10
|
-
import type
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/** Optional - can be rebuilt via buildSpatialIndex() */
|
|
24
|
-
spatialIndex?: T
|
|
1
|
+
import { microToDegrees, 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
|
+
import { around as geoAround } from "geokdbush";
|
|
7
|
+
import KDBush from "kdbush";
|
|
8
|
+
|
|
9
|
+
import { Entities, type EntitiesTransferables } from "./entities.ts";
|
|
10
|
+
import { type IdOrIndex, Ids } from "./ids.ts";
|
|
11
|
+
import type StringTable from "./stringtable.ts";
|
|
12
|
+
import { Tags } from "./tags.ts";
|
|
13
|
+
import { BufferConstructor, type BufferType, ResizeableTypedArray as RTA } from "./typed-arrays.ts";
|
|
14
|
+
|
|
15
|
+
export interface NodesTransferables<
|
|
16
|
+
T extends BufferType = BufferType,
|
|
17
|
+
> extends EntitiesTransferables<T> {
|
|
18
|
+
lons: T;
|
|
19
|
+
lats: T;
|
|
20
|
+
bbox: GeoBbox2D;
|
|
21
|
+
/** Optional - can be rebuilt via buildSpatialIndex() */
|
|
22
|
+
spatialIndex?: T;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
export interface AddNodeOptions {
|
|
28
|
-
|
|
26
|
+
filter?: (node: OsmNode) => boolean;
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
export class Nodes extends Entities<OsmNode> {
|
|
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
|
-
* 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
|
-
}
|
|
30
|
+
/**
|
|
31
|
+
* Coordinates are stored as integer microdegrees (Int32Array).
|
|
32
|
+
* Use OSM_COORD_SCALE (1e7) to convert between degrees and microdegrees.
|
|
33
|
+
*/
|
|
34
|
+
private lons: RTA<Int32Array>;
|
|
35
|
+
private lats: RTA<Int32Array>;
|
|
36
|
+
// Bbox stored in microdegrees internally
|
|
37
|
+
private bbox: [minLon: number, minLat: number, maxLon: number, maxLat: number] = [
|
|
38
|
+
Number.MAX_SAFE_INTEGER,
|
|
39
|
+
Number.MAX_SAFE_INTEGER,
|
|
40
|
+
Number.MIN_SAFE_INTEGER,
|
|
41
|
+
Number.MIN_SAFE_INTEGER,
|
|
42
|
+
];
|
|
43
|
+
// Spatial index stores coordinates in degrees (Float64Array) for geokdbush compatibility
|
|
44
|
+
private spatialIndex: KDBush = new KDBush(0, 128, Float64Array, BufferConstructor);
|
|
45
|
+
// Track if spatial index was properly built (vs default empty)
|
|
46
|
+
private spatialIndexBuilt = false;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a new Nodes index.
|
|
50
|
+
*/
|
|
51
|
+
constructor(stringTable: StringTable, transferables?: NodesTransferables) {
|
|
52
|
+
if (transferables) {
|
|
53
|
+
super("node", new Ids(transferables), new Tags(stringTable, transferables));
|
|
54
|
+
this.lons = RTA.from(Int32Array, transferables.lons);
|
|
55
|
+
this.lats = RTA.from(Int32Array, transferables.lats);
|
|
56
|
+
// Only load spatial index if provided (not stored in IndexedDB)
|
|
57
|
+
if (transferables.spatialIndex?.byteLength) {
|
|
58
|
+
this.spatialIndex = KDBush.from(transferables.spatialIndex);
|
|
59
|
+
this.spatialIndexBuilt = true;
|
|
60
|
+
}
|
|
61
|
+
this.bbox = transferables.bbox;
|
|
62
|
+
this.indexBuilt = true;
|
|
63
|
+
} else {
|
|
64
|
+
super("node", new Ids(), new Tags(stringTable));
|
|
65
|
+
this.lons = new RTA(Int32Array);
|
|
66
|
+
this.lats = new RTA(Int32Array);
|
|
67
|
+
// Spatial index uses Float64Array with degrees for geokdbush compatibility
|
|
68
|
+
this.spatialIndex = new KDBush(0, 128, Float64Array, BufferConstructor);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Add a single node to the index.
|
|
74
|
+
*/
|
|
75
|
+
addNode(node: OsmNode): number {
|
|
76
|
+
const nodeIndex = this.addEntity(node.id, node.tags ?? {});
|
|
77
|
+
|
|
78
|
+
const lonMicro = toMicroDegrees(node.lon);
|
|
79
|
+
const latMicro = toMicroDegrees(node.lat);
|
|
80
|
+
|
|
81
|
+
this.lons.push(lonMicro);
|
|
82
|
+
this.lats.push(latMicro);
|
|
83
|
+
|
|
84
|
+
if (node.lon < this.bbox[0]) this.bbox[0] = node.lon;
|
|
85
|
+
if (node.lat < this.bbox[1]) this.bbox[1] = node.lat;
|
|
86
|
+
if (node.lon > this.bbox[2]) this.bbox[2] = node.lon;
|
|
87
|
+
if (node.lat > this.bbox[3]) this.bbox[3] = node.lat;
|
|
88
|
+
|
|
89
|
+
return nodeIndex;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Add dense nodes from a PBF block.
|
|
94
|
+
*/
|
|
95
|
+
addDenseNodes(
|
|
96
|
+
dense: OsmPbfDenseNodes,
|
|
97
|
+
block: OsmPbfBlock,
|
|
98
|
+
blockStringIndexMap: Uint32Array,
|
|
99
|
+
filter?: (node: OsmNode) => boolean,
|
|
100
|
+
): number {
|
|
101
|
+
// PBF block already has offsets and granularity converted to degrees
|
|
102
|
+
const lon_offset = block.lon_offset ?? 0;
|
|
103
|
+
const lat_offset = block.lat_offset ?? 0;
|
|
104
|
+
const granularity = block.granularity ?? 1e7;
|
|
105
|
+
|
|
106
|
+
const delta = {
|
|
107
|
+
id: 0,
|
|
108
|
+
lat: 0,
|
|
109
|
+
lon: 0,
|
|
110
|
+
timestamp: 0,
|
|
111
|
+
changeset: 0,
|
|
112
|
+
uid: 0,
|
|
113
|
+
user_sid: 0,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const getStringTableIndex = (keyIndex: number) => {
|
|
117
|
+
const key = dense.keys_vals[keyIndex];
|
|
118
|
+
assertValue(key, "Block string key is undefined");
|
|
119
|
+
const index = blockStringIndexMap[key];
|
|
120
|
+
assertValue(index, "Block string not found");
|
|
121
|
+
return index;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
let keysValsIndex = 0;
|
|
125
|
+
let added = 0;
|
|
126
|
+
for (let i = 0; i < dense.id.length; i++) {
|
|
127
|
+
const idSid = dense.id[i];
|
|
128
|
+
const latSid = dense.lat[i];
|
|
129
|
+
const lonSid = dense.lon[i];
|
|
130
|
+
assertValue(idSid, "ID SID is undefined");
|
|
131
|
+
assertValue(latSid, "Latitude SID is undefined");
|
|
132
|
+
assertValue(lonSid, "Longitude SID is undefined");
|
|
133
|
+
|
|
134
|
+
delta.id += idSid;
|
|
135
|
+
delta.lat += latSid;
|
|
136
|
+
delta.lon += lonSid;
|
|
137
|
+
|
|
138
|
+
// Calculate degrees from PBF delta encoding
|
|
139
|
+
const lon = lon_offset + delta.lon / granularity;
|
|
140
|
+
const lat = lat_offset + delta.lat / granularity;
|
|
141
|
+
|
|
142
|
+
// Convert to microdegrees for storage
|
|
143
|
+
const lonMicro = toMicroDegrees(lon);
|
|
144
|
+
const latMicro = toMicroDegrees(lat);
|
|
145
|
+
|
|
146
|
+
const tagKeys: number[] = [];
|
|
147
|
+
const tagValues: number[] = [];
|
|
148
|
+
if (dense.keys_vals.length > 0) {
|
|
149
|
+
while (dense.keys_vals[keysValsIndex] !== 0) {
|
|
150
|
+
const key = getStringTableIndex(keysValsIndex);
|
|
151
|
+
const val = getStringTableIndex(keysValsIndex + 1);
|
|
152
|
+
if (key && val) {
|
|
153
|
+
tagKeys.push(key);
|
|
154
|
+
tagValues.push(val);
|
|
155
|
+
}
|
|
156
|
+
keysValsIndex += 2;
|
|
157
|
+
}
|
|
158
|
+
keysValsIndex++;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const shouldInclude = filter
|
|
162
|
+
? filter({
|
|
163
|
+
id: delta.id,
|
|
164
|
+
lon,
|
|
165
|
+
lat,
|
|
166
|
+
tags: this.tags.getTagsFromIndices(tagKeys, tagValues),
|
|
167
|
+
})
|
|
168
|
+
: true;
|
|
169
|
+
if (!shouldInclude) continue;
|
|
170
|
+
|
|
171
|
+
this.addEntity(delta.id, tagKeys, tagValues);
|
|
172
|
+
this.lons.push(lonMicro);
|
|
173
|
+
this.lats.push(latMicro);
|
|
174
|
+
|
|
175
|
+
if (lon < this.bbox[0]) this.bbox[0] = lon;
|
|
176
|
+
if (lat < this.bbox[1]) this.bbox[1] = lat;
|
|
177
|
+
if (lon > this.bbox[2]) this.bbox[2] = lon;
|
|
178
|
+
if (lat > this.bbox[3]) this.bbox[3] = lat;
|
|
179
|
+
added++;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return added;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Compact the internal arrays to free up memory.
|
|
187
|
+
*/
|
|
188
|
+
buildEntityIndex() {
|
|
189
|
+
this.lons.compact();
|
|
190
|
+
this.lats.compact();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Build the spatial index for nodes.
|
|
195
|
+
* Spatial index stores degrees (Float64Array) for geokdbush compatibility.
|
|
196
|
+
*/
|
|
197
|
+
buildSpatialIndex() {
|
|
198
|
+
console.time("NodeIndex.buildSpatialIndex");
|
|
199
|
+
// Use Float64Array with degrees for geokdbush geographic queries
|
|
200
|
+
this.spatialIndex = new KDBush(this.size, 64, Float64Array, BufferConstructor);
|
|
201
|
+
for (let i = 0; i < this.size; i++) {
|
|
202
|
+
// Convert microdegrees to degrees for spatial index
|
|
203
|
+
const lon = microToDegrees(this.lons.at(i));
|
|
204
|
+
const lat = microToDegrees(this.lats.at(i));
|
|
205
|
+
this.spatialIndex.add(lon, lat);
|
|
206
|
+
}
|
|
207
|
+
this.spatialIndex.finish();
|
|
208
|
+
this.spatialIndexBuilt = true;
|
|
209
|
+
console.timeEnd("NodeIndex.buildSpatialIndex");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Check if the spatial index has been built.
|
|
214
|
+
*/
|
|
215
|
+
hasSpatialIndex(): boolean {
|
|
216
|
+
return this.spatialIndexBuilt;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get the bounding box of all nodes.
|
|
221
|
+
*/
|
|
222
|
+
getBbox(): GeoBbox2D {
|
|
223
|
+
return this.bbox;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Get the bounding box of a specific node.
|
|
228
|
+
*/
|
|
229
|
+
getEntityBbox(i: IdOrIndex): GeoBbox2D {
|
|
230
|
+
const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0];
|
|
231
|
+
const lon = microToDegrees(this.lons.at(index));
|
|
232
|
+
const lat = microToDegrees(this.lats.at(index));
|
|
233
|
+
return [lon, lat, lon, lat] as GeoBbox2D;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Get the longitude and latitude of a specific node.
|
|
238
|
+
* ID lookups return `null` when the node is not present; index lookups remain strict.
|
|
239
|
+
*/
|
|
240
|
+
getNodeLonLat(i: { index: number }): [number, number];
|
|
241
|
+
getNodeLonLat(i: { id: number }): [number, number] | null;
|
|
242
|
+
getNodeLonLat(i: IdOrIndex): [number, number] | null {
|
|
243
|
+
const index = "index" in i ? i.index : this.ids.idOrIndex(i)[0];
|
|
244
|
+
if (index === -1) return null;
|
|
245
|
+
return [microToDegrees(this.lons.at(index)), microToDegrees(this.lats.at(index))];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Get the full node entity.
|
|
250
|
+
*/
|
|
251
|
+
getFullEntity(index: number, id: number, tags?: OsmTags): OsmNode {
|
|
252
|
+
const [lon, lat] = this.getNodeLonLat({ index });
|
|
253
|
+
if (tags) {
|
|
254
|
+
return {
|
|
255
|
+
id,
|
|
256
|
+
lat,
|
|
257
|
+
lon,
|
|
258
|
+
tags,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
id,
|
|
263
|
+
lat,
|
|
264
|
+
lon,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Spatial operations
|
|
269
|
+
/**
|
|
270
|
+
* Find node indexes within a bounding box.
|
|
271
|
+
*/
|
|
272
|
+
findIndexesWithinBbox(bbox: GeoBbox2D): number[] {
|
|
273
|
+
// Spatial index stores degrees, so query with degrees directly
|
|
274
|
+
return this.spatialIndex.range(bbox[0], bbox[1], bbox[2], bbox[3]);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Find node indexes within a radius of a point.
|
|
279
|
+
* Uses geokdbush for proper great-circle distance calculations.
|
|
280
|
+
* @param lon - Longitude in degrees.
|
|
281
|
+
* @param lat - Latitude in degrees.
|
|
282
|
+
* @param radiusKm - Radius in kilometers.
|
|
283
|
+
* @returns Array of node indexes within the radius.
|
|
284
|
+
*/
|
|
285
|
+
findIndexesWithinRadius(lon: number, lat: number, radiusKm: number): number[] {
|
|
286
|
+
// Use geokdbush for proper geographic distance calculations
|
|
287
|
+
return geoAround(this.spatialIndex, lon, lat, Number.POSITIVE_INFINITY, radiusKm);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Get nodes within a bounding box.
|
|
292
|
+
* @param bbox - The bounding box to search within.
|
|
293
|
+
* @param include - A function to filter nodes. If provided, only nodes for which the function returns true will be included.
|
|
294
|
+
* @returns An object containing the IDs and positions of the nodes within the bounding box.
|
|
295
|
+
*/
|
|
296
|
+
withinBbox(
|
|
297
|
+
bbox: GeoBbox2D,
|
|
298
|
+
include?: (i: number) => boolean,
|
|
299
|
+
): {
|
|
300
|
+
ids: Float64Array;
|
|
301
|
+
positions: Float64Array;
|
|
302
|
+
} {
|
|
303
|
+
console.time("Nodes.withinBbox");
|
|
304
|
+
const nodeCandidates = this.findIndexesWithinBbox(bbox);
|
|
305
|
+
const nodePositions = new Float64Array(nodeCandidates.length * 2);
|
|
306
|
+
const ids = new Float64Array(nodeCandidates.length);
|
|
307
|
+
|
|
308
|
+
let skipped = 0;
|
|
309
|
+
nodeCandidates.forEach((nodeIndex, i) => {
|
|
310
|
+
if (include && !include(nodeIndex)) {
|
|
311
|
+
skipped++;
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const [lon, lat] = this.getNodeLonLat({ index: nodeIndex });
|
|
316
|
+
ids[i - skipped] = this.ids.at(nodeIndex);
|
|
317
|
+
nodePositions[(i - skipped) * 2] = lon;
|
|
318
|
+
nodePositions[(i - skipped) * 2 + 1] = lat;
|
|
319
|
+
});
|
|
320
|
+
console.timeEnd("Nodes.withinBbox");
|
|
321
|
+
return {
|
|
322
|
+
ids: ids.subarray(0, nodeCandidates.length - skipped),
|
|
323
|
+
positions: nodePositions.slice(0, (nodeCandidates.length - skipped) * 2),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Get transferable objects for passing to another thread.
|
|
329
|
+
* Only includes spatialIndex if it has been built.
|
|
330
|
+
*/
|
|
331
|
+
override transferables(): NodesTransferables {
|
|
332
|
+
const base = {
|
|
333
|
+
...super.transferables(),
|
|
334
|
+
lons: this.lons.array.buffer,
|
|
335
|
+
lats: this.lats.array.buffer,
|
|
336
|
+
bbox: this.bbox,
|
|
337
|
+
};
|
|
338
|
+
// Only include spatial index if it was built
|
|
339
|
+
if (this.spatialIndexBuilt) {
|
|
340
|
+
return { ...base, spatialIndex: this.spatialIndex.data };
|
|
341
|
+
}
|
|
342
|
+
return base;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Get the approximate memory requirements for a given number of nodes in bytes.
|
|
347
|
+
*/
|
|
348
|
+
static getBytesRequired(count: number) {
|
|
349
|
+
if (count === 0) return 0;
|
|
350
|
+
const indexBytes = (count < 65536 ? 2 : 4) * count;
|
|
351
|
+
// Spatial index stores coordinates in degrees (Float64Array)
|
|
352
|
+
const coordsBytes = count * 2 * Float64Array.BYTES_PER_ELEMENT;
|
|
353
|
+
const padding = (8 - (indexBytes % 8)) % 8;
|
|
354
|
+
const spatialIndexBytes = 8 + indexBytes + coordsBytes + padding;
|
|
355
|
+
|
|
356
|
+
return (
|
|
357
|
+
Ids.getBytesRequired(count) +
|
|
358
|
+
Tags.getBytesRequired(count) +
|
|
359
|
+
count * Int32Array.BYTES_PER_ELEMENT + // lons (stored in microdegrees)
|
|
360
|
+
count * Int32Array.BYTES_PER_ELEMENT + // lats (stored in microdegrees)
|
|
361
|
+
spatialIndexBytes
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Update a ContentHasher with node-specific data (coordinates).
|
|
367
|
+
*/
|
|
368
|
+
override updateHash(hasher: ContentHasher): ContentHasher {
|
|
369
|
+
return super.updateHash(hasher).update(this.lons.array).update(this.lats.array);
|
|
370
|
+
}
|
|
404
371
|
}
|