@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.
Files changed (71) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/LICENSE +21 -0
  3. package/README.md +86 -69
  4. package/dist/contracts.d.ts +26 -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 +1 -1
  9. package/dist/entities.d.ts.map +1 -1
  10. package/dist/entities.js +2 -7
  11. package/dist/entities.js.map +1 -1
  12. package/dist/ids.d.ts +2 -0
  13. package/dist/ids.d.ts.map +1 -1
  14. package/dist/ids.js +23 -15
  15. package/dist/ids.js.map +1 -1
  16. package/dist/index.d.ts +1 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +0 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/mocks.d.ts.map +1 -1
  21. package/dist/mocks.js.map +1 -1
  22. package/dist/nodes.d.ts +9 -3
  23. package/dist/nodes.d.ts.map +1 -1
  24. package/dist/nodes.js +6 -13
  25. package/dist/nodes.js.map +1 -1
  26. package/dist/osm.d.ts +1 -1
  27. package/dist/osm.d.ts.map +1 -1
  28. package/dist/osm.js +1 -4
  29. package/dist/osm.js.map +1 -1
  30. package/dist/relations.d.ts +3 -2
  31. package/dist/relations.d.ts.map +1 -1
  32. package/dist/relations.js +15 -9
  33. package/dist/relations.js.map +1 -1
  34. package/dist/stringtable.d.ts.map +1 -1
  35. package/dist/stringtable.js +4 -13
  36. package/dist/stringtable.js.map +1 -1
  37. package/dist/tags.d.ts +1 -1
  38. package/dist/tags.d.ts.map +1 -1
  39. package/dist/tags.js.map +1 -1
  40. package/dist/typed-arrays.d.ts +1 -1
  41. package/dist/typed-arrays.d.ts.map +1 -1
  42. package/dist/typed-arrays.js.map +1 -1
  43. package/dist/ways.d.ts +2 -1
  44. package/dist/ways.d.ts.map +1 -1
  45. package/dist/ways.js +18 -18
  46. package/dist/ways.js.map +1 -1
  47. package/package.json +46 -31
  48. package/src/contracts.ts +28 -0
  49. package/src/entities.ts +179 -194
  50. package/src/ids.ts +228 -235
  51. package/src/index.ts +12 -9
  52. package/src/mocks.ts +114 -113
  53. package/src/nodes.ts +364 -397
  54. package/src/osm.ts +196 -207
  55. package/src/relations.ts +530 -558
  56. package/src/stringtable.ts +174 -183
  57. package/src/tags.ts +263 -271
  58. package/src/typed-arrays.ts +184 -194
  59. package/src/types/geoflatbush.d.ts +28 -33
  60. package/src/types/geokdbush.d.ts +28 -33
  61. package/src/types/kdbush.d.ts +34 -34
  62. package/src/ways.ts +381 -405
  63. package/test/ids.bench.ts +29 -0
  64. package/test/ids.test.ts +84 -0
  65. package/test/memory.test.ts +29 -30
  66. package/test/relations.bench.ts +27 -0
  67. package/test/relations.test.ts +101 -0
  68. package/test/tags-search.test.ts +76 -56
  69. package/test/typed-arrays.test.ts +100 -99
  70. package/tsconfig.build.json +6 -3
  71. package/tsconfig.json +7 -7
package/src/entities.ts CHANGED
@@ -7,24 +7,19 @@
7
7
  * @module
8
8
  */
9
9
 
10
- import type { ContentHasher } from "@osmix/shared/content-hasher"
11
- import type {
12
- GeoBbox2D,
13
- OsmEntity,
14
- OsmEntityType,
15
- OsmTags,
16
- } from "@osmix/shared/types"
17
- import type { IdOrIndex, Ids, IdsTransferables } from "./ids.ts"
18
- import type { Tags, TagsTransferables } from "./tags.ts"
19
- import type { BufferType } from "./typed-arrays.ts"
10
+ import type { ContentHasher } from "@osmix/shared/content-hasher";
11
+ import type { GeoBbox2D, OsmEntity, OsmEntityType, OsmTags } from "@osmix/types";
12
+
13
+ import type { IdOrIndex, Ids, IdsTransferables } from "./ids.ts";
14
+ import type { Tags, TagsTransferables } from "./tags.ts";
15
+ import type { BufferType } from "./typed-arrays.ts";
20
16
 
21
17
  /**
22
18
  * Serializable representation of an Entities collection for worker transfer.
23
19
  * Combines ID and tag transferables; subclasses add entity-specific data.
24
20
  */
25
21
  export interface EntitiesTransferables<T extends BufferType = BufferType>
26
- extends IdsTransferables<T>,
27
- TagsTransferables<T> {}
22
+ extends IdsTransferables<T>, TagsTransferables<T> {}
28
23
 
29
24
  /**
30
25
  * Abstract base for typed entity collections.
@@ -35,186 +30,176 @@ export interface EntitiesTransferables<T extends BufferType = BufferType>
35
30
  * 3. **Query**: Lookups and iteration enabled.
36
31
  */
37
32
  export abstract class Entities<T extends OsmEntity> {
38
- /** The type of entity stored in this collection ("node", "way", or "relation"). */
39
- indexType: OsmEntityType
40
- /** ID storage and lookup */
41
- ids: Ids
42
- /** Tag storage and search */
43
- tags: Tags
44
-
45
- /** Whether buildIndex() has been called */
46
- protected indexBuilt = false
47
-
48
- /**
49
- * Create a new Entities collection.
50
- * @param indexType - The entity type ("node", "way", or "relation").
51
- * @param ids - The ID storage instance.
52
- * @param tags - The tag storage instance.
53
- */
54
- constructor(indexType: OsmEntityType, ids: Ids, tags: Tags) {
55
- this.indexType = indexType
56
- this.ids = ids
57
- this.tags = tags
58
- }
59
-
60
- /**
61
- * Get transferable objects for passing to another thread.
62
- */
63
- transferables(): EntitiesTransferables {
64
- return {
65
- ...this.ids.transferables(),
66
- ...this.tags.transferables(),
67
- }
68
- }
69
-
70
- /**
71
- * Check if the index is built and ready for use.
72
- */
73
- isReady() {
74
- return this.ids.isReady() && this.tags.isReady() && this.indexBuilt
75
- }
76
-
77
- /** Number of entities in this collection. */
78
- get size() {
79
- return this.ids.size
80
- }
81
-
82
- /**
83
- * Compact entity-specific typed arrays.
84
- * Called by `buildIndex()` after ID and tag indexes are built.
85
- * @abstract
86
- */
87
- abstract buildEntityIndex(): void
88
-
89
- /**
90
- * Finalize indexes.
91
- * Must be called after adding entities and before querying.
92
- */
93
- buildIndex() {
94
- if (this.indexBuilt) return
95
- console.time(`${this.indexType}Index.buildIndex`)
96
- this.ids.buildIndex()
97
- this.tags.buildIndex()
98
- this.buildEntityIndex()
99
- this.indexBuilt = true
100
- console.timeEnd(`${this.indexType}Index.buildIndex`)
101
- }
102
-
103
- /**
104
- * Get the bounding box of an entity.
105
- * @param idOrIndex - Entity identifier (by ID or internal index).
106
- * @returns Geographic bounding box [minLon, minLat, maxLon, maxLat].
107
- * @abstract
108
- */
109
- abstract getEntityBbox(idOrIndex: IdOrIndex): GeoBbox2D
110
-
111
- /**
112
- * Reconstruct a full entity object from index data.
113
- * @param index - Internal array index.
114
- * @param id - OSM entity ID.
115
- * @param tags - Optional pre-fetched tags.
116
- * @returns The complete entity object.
117
- * @abstract
118
- */
119
- abstract getFullEntity(index: number, id: number, tags?: OsmTags): T
120
-
121
- /**
122
- * Add an entity to the index. Tags can be provided as an array of keys and values, or as an object.
123
- */
124
- addEntity(id: number, tags: number[], values: number[]): number
125
- addEntity(id: number, tags: OsmTags): number
126
- addEntity(id: number, tags: OsmTags | number[], values?: number[]): number {
127
- const entityIndex = this.ids.add(id)
128
- if (Array.isArray(tags)) {
129
- if (values === undefined)
130
- throw Error("Values are required when tags is an array")
131
- this.tags.addTagKeysAndValues(entityIndex, tags, values)
132
- } else {
133
- this.tags.addTags(entityIndex, tags)
134
- }
135
- return entityIndex
136
- }
137
-
138
- /**
139
- * Get an entity by ID or index.
140
- */
141
- get(idOrIndex: IdOrIndex): T | null {
142
- const [index, id] = this.ids.idOrIndex(idOrIndex)
143
- if (index === -1) return null
144
- return this.getFullEntity(index, id, this.tags.getTags(index))
145
- }
146
-
147
- /**
148
- * Get an entity by index.
149
- */
150
- getByIndex(index: number): T {
151
- return this.getFullEntity(
152
- index,
153
- this.ids.at(index),
154
- this.tags.getTags(index),
155
- )
156
- }
157
-
158
- /**
159
- * Get multiple entities by their indexes.
160
- */
161
- getEntitiesByIndex(indexes: number[]): T[] {
162
- const entities: T[] = []
163
- for (const index of indexes) {
164
- const entity = this.getByIndex(index)
165
- if (entity) entities.push(entity)
166
- else throw Error(`Entity not found at index ${index}`)
167
- }
168
- return entities
169
- }
170
-
171
- /**
172
- * Iterate over all entities in the index.
173
- */
174
- *[Symbol.iterator](): Generator<T> {
175
- for (let i = 0; i < this.size; i++) {
176
- yield this.getFullEntity(i, this.ids.at(i), this.tags.getTags(i))
177
- }
178
- }
179
-
180
- /**
181
- * Get an entity by ID.
182
- */
183
- getById(id: number): T | null {
184
- const index = this.ids.getIndexFromId(id)
185
- if (index !== -1)
186
- return this.getFullEntity(index, id, this.tags.getTags(index))
187
- return null
188
- }
189
-
190
- /**
191
- * Iterate over entities sorted by ID.
192
- */
193
- *sorted(): Generator<T> {
194
- for (const id of this.ids.sorted) {
195
- const index = this.ids.getIndexFromId(id)
196
- if (index === -1) throw Error(`Entity not found at id ${id}`)
197
- yield this.getFullEntity(index, id, this.tags.getTags(index))
198
- }
199
- }
200
-
201
- /**
202
- * Search for entities with a specific tag key and optional value.
203
- */
204
- search(key: string, val?: string): T[] {
205
- const keyIndex = this.tags.find(key)
206
- const entities = this.tags
207
- .hasKey(keyIndex)
208
- .map((index) => this.getByIndex(index))
209
- if (val === undefined) return entities
210
- return entities.filter((entity) => entity.tags?.[key] === val)
211
- }
212
-
213
- /**
214
- * Update a ContentHasher with this entity collection's base data (IDs and tags).
215
- * Subclasses should override to add entity-specific data.
216
- */
217
- updateHash(hasher: ContentHasher): ContentHasher {
218
- return this.ids.updateHash(this.tags.updateHash(hasher))
219
- }
33
+ /** The type of entity stored in this collection ("node", "way", or "relation"). */
34
+ indexType: OsmEntityType;
35
+ /** ID storage and lookup */
36
+ ids: Ids;
37
+ /** Tag storage and search */
38
+ tags: Tags;
39
+
40
+ /** Whether buildIndex() has been called */
41
+ protected indexBuilt = false;
42
+
43
+ /**
44
+ * Create a new Entities collection.
45
+ * @param indexType - The entity type ("node", "way", or "relation").
46
+ * @param ids - The ID storage instance.
47
+ * @param tags - The tag storage instance.
48
+ */
49
+ constructor(indexType: OsmEntityType, ids: Ids, tags: Tags) {
50
+ this.indexType = indexType;
51
+ this.ids = ids;
52
+ this.tags = tags;
53
+ }
54
+
55
+ /**
56
+ * Get transferable objects for passing to another thread.
57
+ */
58
+ transferables(): EntitiesTransferables {
59
+ return {
60
+ ...this.ids.transferables(),
61
+ ...this.tags.transferables(),
62
+ };
63
+ }
64
+
65
+ /**
66
+ * Check if the index is built and ready for use.
67
+ */
68
+ isReady() {
69
+ return this.ids.isReady() && this.tags.isReady() && this.indexBuilt;
70
+ }
71
+
72
+ /** Number of entities in this collection. */
73
+ get size() {
74
+ return this.ids.size;
75
+ }
76
+
77
+ /**
78
+ * Compact entity-specific typed arrays.
79
+ * Called by `buildIndex()` after ID and tag indexes are built.
80
+ * @abstract
81
+ */
82
+ abstract buildEntityIndex(): void;
83
+
84
+ /**
85
+ * Finalize indexes.
86
+ * Must be called after adding entities and before querying.
87
+ */
88
+ buildIndex() {
89
+ if (this.indexBuilt) return;
90
+ console.time(`${this.indexType}Index.buildIndex`);
91
+ this.ids.buildIndex();
92
+ this.tags.buildIndex();
93
+ this.buildEntityIndex();
94
+ this.indexBuilt = true;
95
+ console.timeEnd(`${this.indexType}Index.buildIndex`);
96
+ }
97
+
98
+ /**
99
+ * Get the bounding box of an entity.
100
+ * @param idOrIndex - Entity identifier (by ID or internal index).
101
+ * @returns Geographic bounding box [minLon, minLat, maxLon, maxLat].
102
+ * @abstract
103
+ */
104
+ abstract getEntityBbox(idOrIndex: IdOrIndex): GeoBbox2D;
105
+
106
+ /**
107
+ * Reconstruct a full entity object from index data.
108
+ * @param index - Internal array index.
109
+ * @param id - OSM entity ID.
110
+ * @param tags - Optional pre-fetched tags.
111
+ * @returns The complete entity object.
112
+ * @abstract
113
+ */
114
+ abstract getFullEntity(index: number, id: number, tags?: OsmTags): T;
115
+
116
+ /**
117
+ * Add an entity to the index. Tags can be provided as an array of keys and values, or as an object.
118
+ */
119
+ addEntity(id: number, tags: number[], values: number[]): number;
120
+ addEntity(id: number, tags: OsmTags): number;
121
+ addEntity(id: number, tags: OsmTags | number[], values?: number[]): number {
122
+ const entityIndex = this.ids.add(id);
123
+ if (Array.isArray(tags)) {
124
+ if (values === undefined) throw Error("Values are required when tags is an array");
125
+ this.tags.addTagKeysAndValues(entityIndex, tags, values);
126
+ } else {
127
+ this.tags.addTags(entityIndex, tags);
128
+ }
129
+ return entityIndex;
130
+ }
131
+
132
+ /**
133
+ * Get an entity by ID or index.
134
+ */
135
+ get(idOrIndex: IdOrIndex): T | null {
136
+ const [index, id] = this.ids.idOrIndex(idOrIndex);
137
+ if (index === -1) return null;
138
+ return this.getFullEntity(index, id, this.tags.getTags(index));
139
+ }
140
+
141
+ /**
142
+ * Get an entity by index.
143
+ */
144
+ getByIndex(index: number): T {
145
+ return this.getFullEntity(index, this.ids.at(index), this.tags.getTags(index));
146
+ }
147
+
148
+ /**
149
+ * Get multiple entities by their indexes.
150
+ */
151
+ getEntitiesByIndex(indexes: number[]): T[] {
152
+ const entities: T[] = [];
153
+ for (const index of indexes) {
154
+ const entity = this.getByIndex(index);
155
+ if (entity) entities.push(entity);
156
+ else throw Error(`Entity not found at index ${index}`);
157
+ }
158
+ return entities;
159
+ }
160
+
161
+ /**
162
+ * Iterate over all entities in the index.
163
+ */
164
+ *[Symbol.iterator](): Generator<T> {
165
+ for (let i = 0; i < this.size; i++) {
166
+ yield this.getFullEntity(i, this.ids.at(i), this.tags.getTags(i));
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Get an entity by ID.
172
+ */
173
+ getById(id: number): T | null {
174
+ const index = this.ids.getIndexFromId(id);
175
+ if (index !== -1) return this.getFullEntity(index, id, this.tags.getTags(index));
176
+ return null;
177
+ }
178
+
179
+ /**
180
+ * Iterate over entities sorted by ID.
181
+ */
182
+ *sorted(): Generator<T> {
183
+ for (const [id, index] of this.ids.sortedEntries()) {
184
+ yield this.getFullEntity(index, id, this.tags.getTags(index));
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Search for entities with a specific tag key and optional value.
190
+ */
191
+ search(key: string, val?: string): T[] {
192
+ const keyIndex = this.tags.find(key);
193
+ const entities = this.tags.hasKey(keyIndex).map((index) => this.getByIndex(index));
194
+ if (val === undefined) return entities;
195
+ return entities.filter((entity) => entity.tags?.[key] === val);
196
+ }
197
+
198
+ /**
199
+ * Update a ContentHasher with this entity collection's base data (IDs and tags).
200
+ * Subclasses should override to add entity-specific data.
201
+ */
202
+ updateHash(hasher: ContentHasher): ContentHasher {
203
+ return this.ids.updateHash(this.tags.updateHash(hasher));
204
+ }
220
205
  }