@osmix/core 0.0.1 → 0.0.6
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 +52 -0
- package/README.md +118 -107
- package/dist/core/src/entities.d.ts +114 -0
- package/dist/core/src/entities.d.ts.map +1 -0
- package/dist/core/src/entities.js +152 -0
- package/dist/core/src/entities.js.map +1 -0
- package/dist/core/src/ids.d.ts +113 -0
- package/dist/core/src/ids.d.ts.map +1 -0
- package/dist/core/src/ids.js +236 -0
- package/dist/core/src/ids.js.map +1 -0
- package/dist/core/src/index.d.ts +32 -0
- package/dist/core/src/index.d.ts.map +1 -0
- package/dist/core/src/index.js +35 -0
- package/dist/core/src/index.js.map +1 -0
- package/dist/core/src/nodes.d.ts +101 -0
- package/dist/core/src/nodes.d.ts.map +1 -0
- package/dist/core/src/nodes.js +309 -0
- package/dist/core/src/nodes.js.map +1 -0
- package/dist/core/src/osm.d.ts +74 -0
- package/dist/core/src/osm.d.ts.map +1 -0
- package/dist/core/src/osm.js +120 -0
- package/dist/core/src/osm.js.map +1 -0
- package/dist/core/src/relations.d.ts +118 -0
- package/dist/core/src/relations.d.ts.map +1 -0
- package/dist/core/src/relations.js +412 -0
- package/dist/core/src/relations.js.map +1 -0
- package/dist/core/src/stringtable.d.ts +95 -0
- package/dist/core/src/stringtable.d.ts.map +1 -0
- package/dist/core/src/stringtable.js +170 -0
- package/dist/core/src/stringtable.js.map +1 -0
- package/dist/core/src/tags.d.ts +126 -0
- package/dist/core/src/tags.d.ts.map +1 -0
- package/dist/core/src/tags.js +228 -0
- package/dist/core/src/tags.js.map +1 -0
- package/dist/core/src/typed-arrays.d.ts +124 -0
- package/dist/core/src/typed-arrays.d.ts.map +1 -0
- package/dist/core/src/typed-arrays.js +198 -0
- package/dist/core/src/typed-arrays.js.map +1 -0
- package/dist/core/src/utils.d.ts +11 -0
- package/dist/core/src/utils.d.ts.map +1 -0
- package/dist/core/src/utils.js +26 -0
- package/dist/core/src/utils.js.map +1 -0
- package/dist/core/src/ways.d.ts +101 -0
- package/dist/core/src/ways.d.ts.map +1 -0
- package/dist/core/src/ways.js +336 -0
- package/dist/core/src/ways.js.map +1 -0
- package/dist/core/test/memory.test.d.ts +7 -0
- package/dist/core/test/memory.test.d.ts.map +1 -0
- package/dist/core/test/memory.test.js +29 -0
- package/dist/core/test/memory.test.js.map +1 -0
- package/dist/core/test/mock-osm.d.ts +13 -0
- package/dist/core/test/mock-osm.d.ts.map +1 -0
- package/dist/core/test/mock-osm.js +122 -0
- package/dist/core/test/mock-osm.js.map +1 -0
- package/dist/core/test/tags-search.test.d.ts +2 -0
- package/dist/core/test/tags-search.test.d.ts.map +1 -0
- package/dist/core/test/tags-search.test.js +56 -0
- package/dist/core/test/tags-search.test.js.map +1 -0
- package/dist/core/test/typed-arrays.test.d.ts +2 -0
- package/dist/core/test/typed-arrays.test.d.ts.map +1 -0
- package/dist/core/test/typed-arrays.test.js +102 -0
- package/dist/core/test/typed-arrays.test.js.map +1 -0
- package/dist/osmix/src/extract.d.ts +43 -0
- package/dist/osmix/src/extract.d.ts.map +1 -0
- package/dist/osmix/src/extract.js +156 -0
- package/dist/osmix/src/extract.js.map +1 -0
- package/dist/osmix/src/pbf.d.ts +55 -0
- package/dist/osmix/src/pbf.d.ts.map +1 -0
- package/dist/osmix/src/pbf.js +187 -0
- package/dist/osmix/src/pbf.js.map +1 -0
- package/dist/osmix/src/utils.d.ts +36 -0
- package/dist/osmix/src/utils.d.ts.map +1 -0
- package/dist/osmix/src/utils.js +108 -0
- package/dist/osmix/src/utils.js.map +1 -0
- package/dist/osmix/test/extract.bench.d.ts +2 -0
- package/dist/osmix/test/extract.bench.d.ts.map +1 -0
- package/dist/osmix/test/extract.bench.js +26 -0
- package/dist/osmix/test/extract.bench.js.map +1 -0
- package/dist/osmix/test/extract.test.d.ts +2 -0
- package/dist/osmix/test/extract.test.d.ts.map +1 -0
- package/dist/osmix/test/extract.test.js +392 -0
- package/dist/osmix/test/extract.test.js.map +1 -0
- package/package.json +11 -11
- package/src/entities.ts +138 -22
- package/src/ids.ts +117 -45
- package/src/index.ts +34 -5
- package/src/nodes.ts +237 -58
- package/src/osm.ts +174 -0
- package/src/relations.ts +383 -54
- package/src/stringtable.ts +111 -36
- package/src/tags.ts +207 -46
- package/src/typed-arrays.ts +97 -10
- package/src/types/geoflatbush.d.ts +37 -0
- package/src/types/geokdbush.d.ts +37 -0
- package/src/types/kdbush.d.ts +24 -6
- package/src/utils.ts +9 -14
- package/src/ways.ts +248 -84
- package/test/memory.test.ts +38 -0
- package/test/mock-osm.ts +8 -8
- package/test/tags-search.test.ts +60 -0
- package/test/typed-arrays.test.ts +5 -6
- package/tsconfig.json +7 -1
- package/src/osmix.ts +0 -556
- package/test/extract.bench.ts +0 -29
- package/test/extract.test.ts +0 -156
- package/test/read.test.ts +0 -22
- package/test/write.test.ts +0 -66
package/src/entities.ts
CHANGED
|
@@ -1,59 +1,151 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Base class for OSM entity collections.
|
|
3
|
+
*
|
|
4
|
+
* Provides common ID and tag storage, streaming iteration, and sorted access.
|
|
5
|
+
* Subclasses implement entity-specific storage and spatial indexing.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
import type {
|
|
11
|
+
GeoBbox2D,
|
|
12
|
+
OsmEntity,
|
|
13
|
+
OsmEntityType,
|
|
14
|
+
OsmTags,
|
|
15
|
+
} from "@osmix/shared/types"
|
|
16
|
+
import type { IdOrIndex, Ids, IdsTransferables } from "./ids"
|
|
17
|
+
import type { Tags, TagsTransferables } from "./tags"
|
|
18
|
+
import type { BufferType } from "./typed-arrays"
|
|
9
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Serializable representation of an Entities collection for worker transfer.
|
|
22
|
+
* Combines ID and tag transferables; subclasses add entity-specific data.
|
|
23
|
+
*/
|
|
24
|
+
export interface EntitiesTransferables<T extends BufferType = BufferType>
|
|
25
|
+
extends IdsTransferables<T>,
|
|
26
|
+
TagsTransferables<T> {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Abstract base for typed entity collections.
|
|
30
|
+
*
|
|
31
|
+
* Lifecycle:
|
|
32
|
+
* 1. **Ingest**: `addEntity()` (no lookups).
|
|
33
|
+
* 2. **Finalize**: `buildIndex()`.
|
|
34
|
+
* 3. **Query**: Lookups and iteration enabled.
|
|
35
|
+
*/
|
|
10
36
|
export abstract class Entities<T extends OsmEntity> {
|
|
37
|
+
/** The type of entity stored in this collection ("node", "way", or "relation"). */
|
|
11
38
|
indexType: OsmEntityType
|
|
12
|
-
|
|
39
|
+
/** ID storage and lookup */
|
|
13
40
|
ids: Ids
|
|
41
|
+
/** Tag storage and search */
|
|
14
42
|
tags: Tags
|
|
15
43
|
|
|
16
|
-
|
|
44
|
+
/** Whether buildIndex() has been called */
|
|
45
|
+
protected indexBuilt = false
|
|
17
46
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Create a new Entities collection.
|
|
49
|
+
* @param indexType - The entity type ("node", "way", or "relation").
|
|
50
|
+
* @param ids - The ID storage instance.
|
|
51
|
+
* @param tags - The tag storage instance.
|
|
52
|
+
*/
|
|
53
|
+
constructor(indexType: OsmEntityType, ids: Ids, tags: Tags) {
|
|
25
54
|
this.indexType = indexType
|
|
26
|
-
this.ids = ids
|
|
27
|
-
this.tags = tags
|
|
55
|
+
this.ids = ids
|
|
56
|
+
this.tags = tags
|
|
28
57
|
}
|
|
29
58
|
|
|
30
|
-
|
|
31
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Get transferable objects for passing to another thread.
|
|
61
|
+
*/
|
|
62
|
+
transferables(): EntitiesTransferables {
|
|
63
|
+
return {
|
|
64
|
+
...this.ids.transferables(),
|
|
65
|
+
...this.tags.transferables(),
|
|
66
|
+
}
|
|
32
67
|
}
|
|
33
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Check if the index is built and ready for use.
|
|
71
|
+
*/
|
|
72
|
+
isReady() {
|
|
73
|
+
return this.ids.isReady() && this.tags.isReady() && this.indexBuilt
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Number of entities in this collection. */
|
|
34
77
|
get size() {
|
|
35
78
|
return this.ids.size
|
|
36
79
|
}
|
|
37
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Compact entity-specific typed arrays.
|
|
83
|
+
* Called by `buildIndex()` after ID and tag indexes are built.
|
|
84
|
+
* @abstract
|
|
85
|
+
*/
|
|
38
86
|
abstract buildEntityIndex(): void
|
|
39
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Finalize indexes.
|
|
90
|
+
* Must be called after adding entities and before querying.
|
|
91
|
+
*/
|
|
40
92
|
buildIndex() {
|
|
41
|
-
|
|
93
|
+
if (this.indexBuilt) return
|
|
94
|
+
console.time(`${this.indexType}Index.buildIndex`)
|
|
42
95
|
this.ids.buildIndex()
|
|
43
96
|
this.tags.buildIndex()
|
|
44
97
|
this.buildEntityIndex()
|
|
45
98
|
this.indexBuilt = true
|
|
46
|
-
console.timeEnd(`${this.indexType}Index.
|
|
99
|
+
console.timeEnd(`${this.indexType}Index.buildIndex`)
|
|
47
100
|
}
|
|
48
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Get the bounding box of an entity.
|
|
104
|
+
* @param idOrIndex - Entity identifier (by ID or internal index).
|
|
105
|
+
* @returns Geographic bounding box [minLon, minLat, maxLon, maxLat].
|
|
106
|
+
* @abstract
|
|
107
|
+
*/
|
|
108
|
+
abstract getEntityBbox(idOrIndex: IdOrIndex): GeoBbox2D
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Reconstruct a full entity object from index data.
|
|
112
|
+
* @param index - Internal array index.
|
|
113
|
+
* @param id - OSM entity ID.
|
|
114
|
+
* @param tags - Optional pre-fetched tags.
|
|
115
|
+
* @returns The complete entity object.
|
|
116
|
+
* @abstract
|
|
117
|
+
*/
|
|
49
118
|
abstract getFullEntity(index: number, id: number, tags?: OsmTags): T
|
|
50
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Add an entity to the index. Tags can be provided as an array of keys and values, or as an object.
|
|
122
|
+
*/
|
|
123
|
+
addEntity(id: number, tags: number[], values: number[]): number
|
|
124
|
+
addEntity(id: number, tags: OsmTags): number
|
|
125
|
+
addEntity(id: number, tags: OsmTags | number[], values?: number[]): number {
|
|
126
|
+
const entityIndex = this.ids.add(id)
|
|
127
|
+
if (Array.isArray(tags)) {
|
|
128
|
+
if (values === undefined)
|
|
129
|
+
throw Error("Values are required when tags is an array")
|
|
130
|
+
this.tags.addTagKeysAndValues(entityIndex, tags, values)
|
|
131
|
+
} else {
|
|
132
|
+
this.tags.addTags(entityIndex, tags)
|
|
133
|
+
}
|
|
134
|
+
return entityIndex
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get an entity by ID or index.
|
|
139
|
+
*/
|
|
51
140
|
get(idOrIndex: IdOrIndex): T | null {
|
|
52
141
|
const [index, id] = this.ids.idOrIndex(idOrIndex)
|
|
53
142
|
if (index === -1) return null
|
|
54
143
|
return this.getFullEntity(index, id, this.tags.getTags(index))
|
|
55
144
|
}
|
|
56
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Get an entity by index.
|
|
148
|
+
*/
|
|
57
149
|
getByIndex(index: number): T {
|
|
58
150
|
return this.getFullEntity(
|
|
59
151
|
index,
|
|
@@ -62,6 +154,9 @@ export abstract class Entities<T extends OsmEntity> {
|
|
|
62
154
|
)
|
|
63
155
|
}
|
|
64
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Get multiple entities by their indexes.
|
|
159
|
+
*/
|
|
65
160
|
getEntitiesByIndex(indexes: number[]): T[] {
|
|
66
161
|
const entities: T[] = []
|
|
67
162
|
for (const index of indexes) {
|
|
@@ -72,12 +167,18 @@ export abstract class Entities<T extends OsmEntity> {
|
|
|
72
167
|
return entities
|
|
73
168
|
}
|
|
74
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Iterate over all entities in the index.
|
|
172
|
+
*/
|
|
75
173
|
*[Symbol.iterator](): Generator<T> {
|
|
76
174
|
for (let i = 0; i < this.size; i++) {
|
|
77
175
|
yield this.getFullEntity(i, this.ids.at(i), this.tags.getTags(i))
|
|
78
176
|
}
|
|
79
177
|
}
|
|
80
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Get an entity by ID.
|
|
181
|
+
*/
|
|
81
182
|
getById(id: number): T | null {
|
|
82
183
|
const index = this.ids.getIndexFromId(id)
|
|
83
184
|
if (index !== -1)
|
|
@@ -85,6 +186,9 @@ export abstract class Entities<T extends OsmEntity> {
|
|
|
85
186
|
return null
|
|
86
187
|
}
|
|
87
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Iterate over entities sorted by ID.
|
|
191
|
+
*/
|
|
88
192
|
*sorted(): Generator<T> {
|
|
89
193
|
for (const id of this.ids.sorted) {
|
|
90
194
|
const index = this.ids.getIndexFromId(id)
|
|
@@ -92,4 +196,16 @@ export abstract class Entities<T extends OsmEntity> {
|
|
|
92
196
|
yield this.getFullEntity(index, id, this.tags.getTags(index))
|
|
93
197
|
}
|
|
94
198
|
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Search for entities with a specific tag key and optional value.
|
|
202
|
+
*/
|
|
203
|
+
search(key: string, val?: string): T[] {
|
|
204
|
+
const keyIndex = this.tags.find(key)
|
|
205
|
+
const entities = this.tags
|
|
206
|
+
.hasKey(keyIndex)
|
|
207
|
+
.map((index) => this.getByIndex(index))
|
|
208
|
+
if (val === undefined) return entities
|
|
209
|
+
return entities.filter((entity) => entity.tags?.[key] === val)
|
|
210
|
+
}
|
|
95
211
|
}
|
package/src/ids.ts
CHANGED
|
@@ -1,92 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ID storage and lookup for OSM entities.
|
|
3
|
+
*
|
|
4
|
+
* Uses a two-level binary search (anchor array + block search) to map 64-bit OSM IDs
|
|
5
|
+
* to internal array indices. Balances memory usage with O(log n) lookup speed.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
1
10
|
import { assertValue } from "@osmix/shared/assert"
|
|
2
11
|
import {
|
|
3
12
|
BufferConstructor,
|
|
4
13
|
type BufferType,
|
|
5
|
-
ResizeableTypedArray,
|
|
14
|
+
ResizeableTypedArray as RTA,
|
|
6
15
|
} from "./typed-arrays"
|
|
7
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Union type for looking up entities by either OSM ID or internal array index.
|
|
19
|
+
*/
|
|
8
20
|
export type IdOrIndex = { id: number } | { index: number }
|
|
9
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Number of IDs per anchor block for the two-level binary search.
|
|
24
|
+
* Smaller values increase anchor array size but speed up block-level search;
|
|
25
|
+
* larger values reduce anchor overhead but slow the final search.
|
|
26
|
+
*/
|
|
10
27
|
const BLOCK_SIZE = 256
|
|
11
28
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Serializable state for worker transfer.
|
|
31
|
+
*/
|
|
32
|
+
export interface IdsTransferables<T extends BufferType = BufferType> {
|
|
33
|
+
/** Entity IDs in insertion order. */
|
|
34
|
+
ids: T
|
|
35
|
+
/** Sorted IDs for binary search (aliases `ids` if already sorted). */
|
|
36
|
+
sortedIds: T
|
|
37
|
+
/** Maps sorted position → original insertion index. */
|
|
38
|
+
sortedIdPositionToIndex: T
|
|
39
|
+
/** Anchor array: every BLOCK_SIZE-th sorted ID. */
|
|
40
|
+
anchors: T
|
|
41
|
+
/** True if IDs were inserted in ascending order. */
|
|
17
42
|
idsAreSorted: boolean
|
|
18
43
|
}
|
|
19
44
|
|
|
20
45
|
/**
|
|
21
|
-
* Efficiently store and lookup IDs.
|
|
46
|
+
* Efficiently store and lookup OSM entity IDs.
|
|
22
47
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
48
|
+
* Stores 64-bit IDs in a Float64Array and maintains a sorted index for O(log n) lookups.
|
|
49
|
+
* Max capacity: ~2^32 entities.
|
|
25
50
|
*/
|
|
26
51
|
export class Ids {
|
|
27
|
-
|
|
52
|
+
/** All IDs in insertion order */
|
|
53
|
+
private ids: RTA<Float64Array>
|
|
54
|
+
/** Whether buildIndex() has been called */
|
|
28
55
|
private indexBuilt = false
|
|
56
|
+
/** True if IDs were added in ascending order (allows index optimization) */
|
|
29
57
|
private idsAreSorted = true
|
|
58
|
+
/** Sorted view of IDs for binary search */
|
|
30
59
|
private idsSorted: Float64Array
|
|
60
|
+
/** Maps position in sorted array → original insertion index */
|
|
31
61
|
private sortedIdPositionToIndex: Uint32Array
|
|
62
|
+
/** Anchor array for two-level binary search: every BLOCK_SIZE-th sorted ID */
|
|
32
63
|
private anchors: Float64Array
|
|
33
64
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.idsSorted = new Float64Array(new BufferConstructor(0))
|
|
54
|
-
this.sortedIdPositionToIndex = new Uint32Array(new BufferConstructor(0))
|
|
55
|
-
this.anchors = new Float64Array(new BufferConstructor(0))
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
transferables(): IdsTransferables {
|
|
59
|
-
return {
|
|
60
|
-
ids: this.ids.array.buffer,
|
|
61
|
-
sortedIds: this.idsSorted.buffer,
|
|
62
|
-
sortedIdPositionToIndex: this.sortedIdPositionToIndex.buffer,
|
|
63
|
-
anchors: this.anchors.buffer,
|
|
64
|
-
idsAreSorted: this.idsAreSorted,
|
|
65
|
+
/**
|
|
66
|
+
* Create a new Ids index.
|
|
67
|
+
* @param transferables - Optional serialized state to reconstruct from.
|
|
68
|
+
*/
|
|
69
|
+
constructor(transferables?: IdsTransferables) {
|
|
70
|
+
if (transferables) {
|
|
71
|
+
this.ids = RTA.from(Float64Array, transferables.ids)
|
|
72
|
+
this.idsSorted = new Float64Array(transferables.sortedIds)
|
|
73
|
+
this.sortedIdPositionToIndex = new Uint32Array(
|
|
74
|
+
transferables.sortedIdPositionToIndex,
|
|
75
|
+
)
|
|
76
|
+
this.anchors = new Float64Array(transferables.anchors)
|
|
77
|
+
this.idsAreSorted = transferables.idsAreSorted
|
|
78
|
+
this.indexBuilt = true
|
|
79
|
+
} else {
|
|
80
|
+
this.ids = new RTA(Float64Array)
|
|
81
|
+
this.idsSorted = new Float64Array(new BufferConstructor(0))
|
|
82
|
+
this.sortedIdPositionToIndex = new Uint32Array(new BufferConstructor(0))
|
|
83
|
+
this.anchors = new Float64Array(new BufferConstructor(0))
|
|
65
84
|
}
|
|
66
85
|
}
|
|
67
86
|
|
|
87
|
+
/** Number of IDs stored in this index. */
|
|
68
88
|
get size() {
|
|
69
89
|
return this.ids.length
|
|
70
90
|
}
|
|
71
91
|
|
|
72
|
-
|
|
92
|
+
/** Returns true if buildIndex() has been called and lookups are enabled. */
|
|
93
|
+
isReady() {
|
|
73
94
|
return this.indexBuilt
|
|
74
95
|
}
|
|
75
96
|
|
|
97
|
+
/** Returns true if IDs were inserted in ascending order. */
|
|
76
98
|
isSorted() {
|
|
77
99
|
return this.idsAreSorted
|
|
78
100
|
}
|
|
79
101
|
|
|
80
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Add an ID to the index.
|
|
104
|
+
* @param id - The OSM entity ID to add.
|
|
105
|
+
* @returns The internal array index where this ID was stored.
|
|
106
|
+
* @throws If the index has already been built.
|
|
107
|
+
*/
|
|
108
|
+
add(id: number): number {
|
|
81
109
|
if (this.indexBuilt) throw Error("ID index already built.")
|
|
82
110
|
if (this.ids.length > 0 && id < this.ids.at(-1)) this.idsAreSorted = false
|
|
83
|
-
this.ids.push(id)
|
|
111
|
+
return this.ids.push(id)
|
|
84
112
|
}
|
|
85
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Get the ID at a specific internal index.
|
|
116
|
+
* @param index - The internal array index.
|
|
117
|
+
* @returns The OSM entity ID at that index.
|
|
118
|
+
*/
|
|
86
119
|
at(index: number): number {
|
|
87
120
|
return this.ids.at(index)
|
|
88
121
|
}
|
|
89
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Check if an ID exists in this index.
|
|
125
|
+
* @param id - The OSM entity ID to check.
|
|
126
|
+
* @returns True if the ID exists in this index.
|
|
127
|
+
*/
|
|
90
128
|
has(id: number): boolean {
|
|
91
129
|
return this.getIndexFromId(id) !== -1
|
|
92
130
|
}
|
|
@@ -158,7 +196,13 @@ export class Ids {
|
|
|
158
196
|
this.indexBuilt = true
|
|
159
197
|
}
|
|
160
198
|
|
|
161
|
-
|
|
199
|
+
/**
|
|
200
|
+
* Look up the internal array index for an OSM entity ID.
|
|
201
|
+
*
|
|
202
|
+
* @param id - The OSM entity ID to look up.
|
|
203
|
+
* @returns The internal array index, or -1 if not found.
|
|
204
|
+
* @throws If the index has not been built.
|
|
205
|
+
*/
|
|
162
206
|
getIndexFromId(id: number): number {
|
|
163
207
|
if (!this.indexBuilt) throw Error("IdIndex not built.")
|
|
164
208
|
|
|
@@ -204,7 +248,35 @@ export class Ids {
|
|
|
204
248
|
return [i.index, this.at(i.index)]
|
|
205
249
|
}
|
|
206
250
|
|
|
251
|
+
/** Returns the sorted array of IDs for iteration. */
|
|
207
252
|
get sorted() {
|
|
208
253
|
return this.idsSorted
|
|
209
254
|
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Get transferable buffers for passing to another thread.
|
|
258
|
+
* @returns Serializable representation of this index.
|
|
259
|
+
*/
|
|
260
|
+
transferables(): IdsTransferables {
|
|
261
|
+
return {
|
|
262
|
+
ids: this.ids.array.buffer,
|
|
263
|
+
sortedIds: this.idsSorted.buffer,
|
|
264
|
+
sortedIdPositionToIndex: this.sortedIdPositionToIndex.buffer,
|
|
265
|
+
anchors: this.anchors.buffer,
|
|
266
|
+
idsAreSorted: this.idsAreSorted,
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get the approximate memory requirements for a given number of IDs in bytes.
|
|
272
|
+
*/
|
|
273
|
+
static getBytesRequired(count: number) {
|
|
274
|
+
if (count === 0) return 0
|
|
275
|
+
return (
|
|
276
|
+
count * Float64Array.BYTES_PER_ELEMENT + // ids
|
|
277
|
+
count * Float64Array.BYTES_PER_ELEMENT + // sortedIds
|
|
278
|
+
count * Uint32Array.BYTES_PER_ELEMENT + // sortedIdPositionToIndex
|
|
279
|
+
Math.ceil(count / BLOCK_SIZE) * Float64Array.BYTES_PER_ELEMENT // anchors
|
|
280
|
+
)
|
|
281
|
+
}
|
|
210
282
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @osmix/core - In-memory OSM entity storage with spatial indexing.
|
|
3
|
+
*
|
|
4
|
+
* Efficiently stores and queries OpenStreetMap entities (nodes, ways, relations).
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - **Memory-efficient**: Uses typed arrays and Int32Array microdegrees.
|
|
8
|
+
* - **Spatial indexing**: KDBush (points) and Flatbush (bboxes) for fast geographic queries.
|
|
9
|
+
* - **Worker-ready**: Zero-copy transfer via `transferables()`.
|
|
10
|
+
* - **Tag indexing**: Fast reverse lookup by tag key.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Osm } from "@osmix/core"
|
|
15
|
+
* const osm = new Osm({ id: "example" })
|
|
16
|
+
* osm.nodes.addNode({ id: 1, lon: -122.4, lat: 47.6, tags: { name: "Seattle" } })
|
|
17
|
+
* osm.buildIndexes()
|
|
18
|
+
* osm.buildSpatialIndexes()
|
|
19
|
+
* const nearby = osm.nodes.findIndexesWithinRadius(-122.4, 47.6, 10)
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @module @osmix/core
|
|
23
|
+
*/
|
|
24
|
+
|
|
1
25
|
/// <reference path="./types/kdbush.d.ts" />
|
|
26
|
+
/// <reference path="./types/geokdbush.d.ts" />
|
|
27
|
+
/// <reference path="./types/geoflatbush.d.ts" />
|
|
2
28
|
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
29
|
+
export * from "./nodes"
|
|
30
|
+
export * from "./osm"
|
|
31
|
+
export * from "./relations"
|
|
32
|
+
export * from "./stringtable"
|
|
33
|
+
export * from "./tags"
|
|
34
|
+
export { BufferConstructor, type BufferType } from "./typed-arrays"
|
|
35
|
+
export * from "./utils" // TODO Move into shared?
|
|
36
|
+
export * from "./ways"
|