@osmix/geojson 0.0.6 → 0.0.8

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 CHANGED
@@ -1,5 +1,25 @@
1
1
  # @osmix/geojson
2
2
 
3
+ ## 0.0.8
4
+
5
+ ### Patch Changes
6
+
7
+ - f468db5: Fix publishing (2)
8
+ - Updated dependencies [f468db5]
9
+ - @osmix/core@0.1.3
10
+ - @osmix/pbf@0.0.5
11
+ - @osmix/shared@0.0.8
12
+
13
+ ## 0.0.7
14
+
15
+ ### Patch Changes
16
+
17
+ - 68d6bd8: Fix publishing for packages.
18
+ - Updated dependencies [68d6bd8]
19
+ - @osmix/core@0.1.2
20
+ - @osmix/pbf@0.0.4
21
+ - @osmix/shared@0.0.7
22
+
3
23
  ## 0.0.6
4
24
 
5
25
  ### Patch Changes
@@ -0,0 +1,86 @@
1
+ /**
2
+ * OSM entity to GeoJSON Feature conversion.
3
+ *
4
+ * Converts OSM nodes, ways, and relations into GeoJSON Features with
5
+ * appropriate geometry types and preserved properties.
6
+ *
7
+ * @module
8
+ */
9
+ import type { Osm } from "@osmix/core";
10
+ import type { OsmEntity, OsmNode, OsmRelation, OsmWay } from "@osmix/shared/types";
11
+ import type { GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon } from "geojson";
12
+ import type { OsmGeoJSONFeature } from "./types";
13
+ /**
14
+ * Convert an OSM node to a GeoJSON Point feature.
15
+ *
16
+ * @param node - OSM node with id, lon, lat, and optional tags.
17
+ * @returns GeoJSON Point Feature with OSM properties.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const feature = nodeToFeature({ id: 1, lon: -122.4, lat: 47.6, tags: { name: "Seattle" } })
22
+ * // { type: "Feature", geometry: { type: "Point", coordinates: [-122.4, 47.6] }, ... }
23
+ * ```
24
+ */
25
+ export declare function nodeToFeature(node: OsmNode): OsmGeoJSONFeature<Point>;
26
+ /**
27
+ * Convert an OSM way to a GeoJSON LineString or Polygon feature.
28
+ *
29
+ * Geometry type is determined by the `wayIsArea` helper, which checks for
30
+ * area-indicating tags (building, landuse, etc.) and ring closure.
31
+ *
32
+ * @param way - OSM way with id, refs, and optional tags.
33
+ * @param refToPosition - Function to resolve node ID to [lon, lat] coordinates.
34
+ * @returns GeoJSON LineString or Polygon Feature with OSM properties.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const feature = wayToFeature(way, (ref) => osm.nodes.getNodeLonLat({ id: ref }))
39
+ * ```
40
+ */
41
+ export declare function wayToFeature(way: OsmWay, refToPosition: (id: number) => [number, number]): OsmGeoJSONFeature<LineString | Polygon>;
42
+ /**
43
+ * Convert an OSM relation to a GeoJSON feature.
44
+ *
45
+ * Geometry type is determined by relation type and tags:
46
+ * - **Multipolygon/boundary**: MultiPolygon or Polygon
47
+ * - **Route/multilinestring**: MultiLineString or LineString
48
+ * - **Site/collection**: MultiPoint or Point
49
+ * - **Other**: GeometryCollection (empty for logical relations)
50
+ *
51
+ * @param relation - OSM relation with id, members, and optional tags.
52
+ * @param refToPosition - Function to resolve node ID to [lon, lat] coordinates.
53
+ * @param getWay - Optional function to resolve way ID to OsmWay (required for polygons/lines).
54
+ * @returns GeoJSON Feature with appropriate geometry type.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const feature = relationToFeature(
59
+ * relation,
60
+ * (ref) => osm.nodes.getNodeLonLat({ id: ref }),
61
+ * (ref) => osm.ways.getById(ref)
62
+ * )
63
+ * ```
64
+ */
65
+ export declare function relationToFeature(relation: OsmRelation, refToPosition: (id: number) => [number, number], getWay?: (wayId: number) => OsmWay | null): OsmGeoJSONFeature<Polygon | MultiPolygon | LineString | MultiLineString | Point | MultiPoint | GeometryCollection>;
66
+ /**
67
+ * Convert any OSM entity to a GeoJSON feature using an Osm index.
68
+ *
69
+ * Convenience function that handles entity type detection and coordinate
70
+ * resolution automatically using the provided Osm index.
71
+ *
72
+ * @param osm - Osm index for coordinate lookups.
73
+ * @param entity - Any OSM entity (node, way, or relation).
74
+ * @returns GeoJSON Feature with appropriate geometry.
75
+ * @throws If entity type is unknown.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * for (const way of osm.ways) {
80
+ * const feature = osmEntityToGeoJSONFeature(osm, way)
81
+ * features.push(feature)
82
+ * }
83
+ * ```
84
+ */
85
+ export declare function osmEntityToGeoJSONFeature(osm: Osm, entity: OsmEntity): OsmGeoJSONFeature<GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPolygon | GeoJSON.MultiLineString | GeoJSON.MultiPoint | GeoJSON.GeometryCollection>;
86
+ //# sourceMappingURL=entity-to-feature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-to-feature.d.ts","sourceRoot":"","sources":["../src/entity-to-feature.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAWtC,OAAO,KAAK,EAEX,SAAS,EACT,OAAO,EACP,WAAW,EACX,MAAM,EACN,MAAM,qBAAqB,CAAA;AAG5B,OAAO,KAAK,EACX,kBAAkB,EAClB,UAAU,EACV,eAAe,EACf,UAAU,EACV,YAAY,EACZ,KAAK,EACL,OAAO,EACP,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAEhD;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAerE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC3B,GAAG,EAAE,MAAM,EACX,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,GAC7C,iBAAiB,CAAC,UAAU,GAAG,OAAO,CAAC,CAoBzC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,WAAW,EACrB,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/C,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,GACvC,iBAAiB,CACjB,OAAO,GACP,YAAY,GACZ,UAAU,GACV,eAAe,GACf,KAAK,GACL,UAAU,GACV,kBAAkB,CACpB,CA2OA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,yBAAyB,CACxC,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,SAAS,GACf,iBAAiB,CACjB,OAAO,CAAC,KAAK,GACb,OAAO,CAAC,UAAU,GAClB,OAAO,CAAC,OAAO,GACf,OAAO,CAAC,YAAY,GACpB,OAAO,CAAC,eAAe,GACvB,OAAO,CAAC,UAAU,GAClB,OAAO,CAAC,kBAAkB,CAC5B,CAeA"}
@@ -0,0 +1,345 @@
1
+ /**
2
+ * OSM entity to GeoJSON Feature conversion.
3
+ *
4
+ * Converts OSM nodes, ways, and relations into GeoJSON Features with
5
+ * appropriate geometry types and preserved properties.
6
+ *
7
+ * @module
8
+ */
9
+ import { buildRelationLineStrings, collectRelationPoints, getRelationKind, isAreaRelation, } from "@osmix/shared/relation-kind";
10
+ import { buildRelationRings, getWayMembersByRole, } from "@osmix/shared/relation-multipolygon";
11
+ import { isNode, isRelation, isWay } from "@osmix/shared/utils";
12
+ import { wayIsArea } from "@osmix/shared/way-is-area";
13
+ /**
14
+ * Convert an OSM node to a GeoJSON Point feature.
15
+ *
16
+ * @param node - OSM node with id, lon, lat, and optional tags.
17
+ * @returns GeoJSON Point Feature with OSM properties.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const feature = nodeToFeature({ id: 1, lon: -122.4, lat: 47.6, tags: { name: "Seattle" } })
22
+ * // { type: "Feature", geometry: { type: "Point", coordinates: [-122.4, 47.6] }, ... }
23
+ * ```
24
+ */
25
+ export function nodeToFeature(node) {
26
+ return {
27
+ type: "Feature",
28
+ id: node.id,
29
+ geometry: {
30
+ type: "Point",
31
+ coordinates: [node.lon, node.lat],
32
+ },
33
+ properties: {
34
+ id: node.id,
35
+ type: "node",
36
+ ...node.info,
37
+ ...node.tags,
38
+ },
39
+ };
40
+ }
41
+ /**
42
+ * Convert an OSM way to a GeoJSON LineString or Polygon feature.
43
+ *
44
+ * Geometry type is determined by the `wayIsArea` helper, which checks for
45
+ * area-indicating tags (building, landuse, etc.) and ring closure.
46
+ *
47
+ * @param way - OSM way with id, refs, and optional tags.
48
+ * @param refToPosition - Function to resolve node ID to [lon, lat] coordinates.
49
+ * @returns GeoJSON LineString or Polygon Feature with OSM properties.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const feature = wayToFeature(way, (ref) => osm.nodes.getNodeLonLat({ id: ref }))
54
+ * ```
55
+ */
56
+ export function wayToFeature(way, refToPosition) {
57
+ return {
58
+ type: "Feature",
59
+ id: way.id,
60
+ geometry: wayIsArea(way)
61
+ ? {
62
+ type: "Polygon",
63
+ coordinates: [way.refs.map((r) => refToPosition(r))],
64
+ }
65
+ : {
66
+ type: "LineString",
67
+ coordinates: way.refs.map((r) => refToPosition(r)),
68
+ },
69
+ properties: {
70
+ id: way.id,
71
+ type: "way",
72
+ ...way.info,
73
+ ...way.tags,
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * Convert an OSM relation to a GeoJSON feature.
79
+ *
80
+ * Geometry type is determined by relation type and tags:
81
+ * - **Multipolygon/boundary**: MultiPolygon or Polygon
82
+ * - **Route/multilinestring**: MultiLineString or LineString
83
+ * - **Site/collection**: MultiPoint or Point
84
+ * - **Other**: GeometryCollection (empty for logical relations)
85
+ *
86
+ * @param relation - OSM relation with id, members, and optional tags.
87
+ * @param refToPosition - Function to resolve node ID to [lon, lat] coordinates.
88
+ * @param getWay - Optional function to resolve way ID to OsmWay (required for polygons/lines).
89
+ * @returns GeoJSON Feature with appropriate geometry type.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const feature = relationToFeature(
94
+ * relation,
95
+ * (ref) => osm.nodes.getNodeLonLat({ id: ref }),
96
+ * (ref) => osm.ways.getById(ref)
97
+ * )
98
+ * ```
99
+ */
100
+ export function relationToFeature(relation, refToPosition, getWay) {
101
+ const getNodeCoordinates = (nodeId) => {
102
+ const pos = refToPosition(nodeId);
103
+ return pos ? [pos[0], pos[1]] : undefined;
104
+ };
105
+ const kind = getRelationKind(relation);
106
+ // Handle area relations (multipolygon, boundary)
107
+ if (isAreaRelation(relation) && getWay) {
108
+ const rings = buildRelationRings(relation, getWay, getNodeCoordinates);
109
+ if (rings.length === 0) {
110
+ // Fallback to simple MultiPolygon if we can't build rings
111
+ return {
112
+ type: "Feature",
113
+ id: relation.id,
114
+ geometry: {
115
+ type: "MultiPolygon",
116
+ coordinates: [],
117
+ },
118
+ properties: {
119
+ id: relation.id,
120
+ type: "relation",
121
+ ...relation.info,
122
+ ...relation.tags,
123
+ },
124
+ };
125
+ }
126
+ // If only one polygon, return Polygon; otherwise MultiPolygon
127
+ if (rings.length === 1) {
128
+ return {
129
+ type: "Feature",
130
+ id: relation.id,
131
+ geometry: {
132
+ type: "Polygon",
133
+ coordinates: rings[0],
134
+ },
135
+ properties: {
136
+ id: relation.id,
137
+ type: "relation",
138
+ ...relation.info,
139
+ ...relation.tags,
140
+ },
141
+ };
142
+ }
143
+ return {
144
+ type: "Feature",
145
+ id: relation.id,
146
+ geometry: {
147
+ type: "MultiPolygon",
148
+ coordinates: rings,
149
+ },
150
+ properties: {
151
+ id: relation.id,
152
+ type: "relation",
153
+ ...relation.info,
154
+ ...relation.tags,
155
+ },
156
+ };
157
+ }
158
+ // Handle line relations (route, multilinestring)
159
+ if (kind === "line" && getWay) {
160
+ const lineStrings = buildRelationLineStrings(relation, getWay, getNodeCoordinates);
161
+ if (lineStrings.length === 0) {
162
+ return {
163
+ type: "Feature",
164
+ id: relation.id,
165
+ geometry: {
166
+ type: "MultiLineString",
167
+ coordinates: [],
168
+ },
169
+ properties: {
170
+ id: relation.id,
171
+ type: "relation",
172
+ ...relation.info,
173
+ ...relation.tags,
174
+ },
175
+ };
176
+ }
177
+ if (lineStrings.length === 1) {
178
+ return {
179
+ type: "Feature",
180
+ id: relation.id,
181
+ geometry: {
182
+ type: "LineString",
183
+ coordinates: lineStrings[0],
184
+ },
185
+ properties: {
186
+ id: relation.id,
187
+ type: "relation",
188
+ ...relation.info,
189
+ ...relation.tags,
190
+ },
191
+ };
192
+ }
193
+ return {
194
+ type: "Feature",
195
+ id: relation.id,
196
+ geometry: {
197
+ type: "MultiLineString",
198
+ coordinates: lineStrings,
199
+ },
200
+ properties: {
201
+ id: relation.id,
202
+ type: "relation",
203
+ ...relation.info,
204
+ ...relation.tags,
205
+ },
206
+ };
207
+ }
208
+ // Handle point relations (multipoint)
209
+ if (kind === "point") {
210
+ const points = collectRelationPoints(relation, getNodeCoordinates);
211
+ if (points.length === 0) {
212
+ return {
213
+ type: "Feature",
214
+ id: relation.id,
215
+ geometry: {
216
+ type: "MultiPoint",
217
+ coordinates: [],
218
+ },
219
+ properties: {
220
+ id: relation.id,
221
+ type: "relation",
222
+ ...relation.info,
223
+ ...relation.tags,
224
+ },
225
+ };
226
+ }
227
+ if (points.length === 1) {
228
+ return {
229
+ type: "Feature",
230
+ id: relation.id,
231
+ geometry: {
232
+ type: "Point",
233
+ coordinates: points[0],
234
+ },
235
+ properties: {
236
+ id: relation.id,
237
+ type: "relation",
238
+ ...relation.info,
239
+ ...relation.tags,
240
+ },
241
+ };
242
+ }
243
+ return {
244
+ type: "Feature",
245
+ id: relation.id,
246
+ geometry: {
247
+ type: "MultiPoint",
248
+ coordinates: points,
249
+ },
250
+ properties: {
251
+ id: relation.id,
252
+ type: "relation",
253
+ ...relation.info,
254
+ ...relation.tags,
255
+ },
256
+ };
257
+ }
258
+ // Fallback for logical relations or when getWay is not provided
259
+ // Return GeometryCollection or null geometry for logic-only relations
260
+ if (kind === "logic" || kind === "super") {
261
+ return {
262
+ type: "Feature",
263
+ id: relation.id,
264
+ geometry: {
265
+ type: "GeometryCollection",
266
+ geometries: [],
267
+ },
268
+ properties: {
269
+ id: relation.id,
270
+ type: "relation",
271
+ ...relation.info,
272
+ ...relation.tags,
273
+ },
274
+ };
275
+ }
276
+ // Final fallback: try to create a simple representation from way members
277
+ if (getWay) {
278
+ const { outer } = getWayMembersByRole(relation);
279
+ const coordinates = [];
280
+ if (outer.length > 0) {
281
+ coordinates.push([outer.map((member) => refToPosition(member.ref))]);
282
+ }
283
+ return {
284
+ type: "Feature",
285
+ id: relation.id,
286
+ geometry: {
287
+ type: "MultiPolygon",
288
+ coordinates: coordinates.length > 0 ? coordinates : [],
289
+ },
290
+ properties: {
291
+ id: relation.id,
292
+ type: "relation",
293
+ ...relation.info,
294
+ ...relation.tags,
295
+ },
296
+ };
297
+ }
298
+ // No geometry available
299
+ return {
300
+ type: "Feature",
301
+ id: relation.id,
302
+ geometry: {
303
+ type: "GeometryCollection",
304
+ geometries: [],
305
+ },
306
+ properties: {
307
+ id: relation.id,
308
+ type: "relation",
309
+ ...relation.info,
310
+ ...relation.tags,
311
+ },
312
+ };
313
+ }
314
+ /**
315
+ * Convert any OSM entity to a GeoJSON feature using an Osm index.
316
+ *
317
+ * Convenience function that handles entity type detection and coordinate
318
+ * resolution automatically using the provided Osm index.
319
+ *
320
+ * @param osm - Osm index for coordinate lookups.
321
+ * @param entity - Any OSM entity (node, way, or relation).
322
+ * @returns GeoJSON Feature with appropriate geometry.
323
+ * @throws If entity type is unknown.
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * for (const way of osm.ways) {
328
+ * const feature = osmEntityToGeoJSONFeature(osm, way)
329
+ * features.push(feature)
330
+ * }
331
+ * ```
332
+ */
333
+ export function osmEntityToGeoJSONFeature(osm, entity) {
334
+ if (isNode(entity)) {
335
+ return nodeToFeature(entity);
336
+ }
337
+ if (isWay(entity)) {
338
+ return wayToFeature(entity, (ref) => osm.nodes.getNodeLonLat({ id: ref }));
339
+ }
340
+ if (isRelation(entity)) {
341
+ return relationToFeature(entity, (ref) => osm.nodes.getNodeLonLat({ id: ref }), (ref) => osm.ways.getById(ref));
342
+ }
343
+ throw new Error("Unknown entity type");
344
+ }
345
+ //# sourceMappingURL=entity-to-feature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-to-feature.js","sourceRoot":"","sources":["../src/entity-to-feature.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EACN,wBAAwB,EACxB,qBAAqB,EACrB,eAAe,EACf,cAAc,GACd,MAAM,6BAA6B,CAAA;AACpC,OAAO,EACN,kBAAkB,EAClB,mBAAmB,GACnB,MAAM,qCAAqC,CAAA;AAQ5C,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAYrD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,IAAa;IAC1C,OAAO;QACN,IAAI,EAAE,SAAS;QACf,EAAE,EAAE,IAAI,CAAC,EAAE;QACX,QAAQ,EAAE;YACT,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC;SACjC;QACD,UAAU,EAAE;YACX,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,IAAI,EAAE,MAAM;YACZ,GAAG,IAAI,CAAC,IAAI;YACZ,GAAG,IAAI,CAAC,IAAI;SACZ;KACD,CAAA;AACF,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAC3B,GAAW,EACX,aAA+C;IAE/C,OAAO;QACN,IAAI,EAAE,SAAS;QACf,EAAE,EAAE,GAAG,CAAC,EAAE;QACV,QAAQ,EAAE,SAAS,CAAC,GAAG,CAAC;YACvB,CAAC,CAAC;gBACA,IAAI,EAAE,SAAS;gBACf,WAAW,EAAE,CAAC,GAA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M,CAAC,MAAM,CAAC,EAAE,CAAC;QACpB,OAAO,aAAa,CAAC,MAAM,CAAC,CAAA;IAC7B,CAAC;IACD,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACnB,OAAO,YAAY,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;IAC3E,CAAC;IACD,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACxB,OAAO,iBAAiB,CACvB,MAAM,EACN,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,EAC7C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAC9B,CAAA;IACF,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;AACvC,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @osmix/geojson - Convert between OSM entities and GeoJSON.
3
+ *
4
+ * Provides bidirectional conversion between Osmix data structures and GeoJSON:
5
+ * - **Export**: Convert OSM nodes, ways, and relations to GeoJSON Features.
6
+ * - **Import**: Build an Osm index from a GeoJSON FeatureCollection.
7
+ *
8
+ * Handles geometry mapping:
9
+ * - Nodes → Point
10
+ * - Ways → LineString or Polygon (based on tags and closure)
11
+ * - Relations → MultiPolygon, MultiLineString, or GeometryCollection (by type)
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // Export OSM entity to GeoJSON
16
+ * import { nodeToFeature, wayToFeature } from "@osmix/geojson"
17
+ *
18
+ * const pointFeature = nodeToFeature(node)
19
+ * const lineFeature = wayToFeature(way, (ref) => osm.nodes.getNodeLonLat({ id: ref }))
20
+ * ```
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * // Import GeoJSON to Osm index
25
+ * import { fromGeoJSON } from "@osmix/geojson"
26
+ *
27
+ * const osm = await fromGeoJSON(geojsonFile, { id: "imported" })
28
+ * ```
29
+ *
30
+ * @module @osmix/geojson
31
+ */
32
+ export * from "./entity-to-feature";
33
+ export * from "./osm-from-geojson";
34
+ export * from "./types";
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,SAAS,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @osmix/geojson - Convert between OSM entities and GeoJSON.
3
+ *
4
+ * Provides bidirectional conversion between Osmix data structures and GeoJSON:
5
+ * - **Export**: Convert OSM nodes, ways, and relations to GeoJSON Features.
6
+ * - **Import**: Build an Osm index from a GeoJSON FeatureCollection.
7
+ *
8
+ * Handles geometry mapping:
9
+ * - Nodes → Point
10
+ * - Ways → LineString or Polygon (based on tags and closure)
11
+ * - Relations → MultiPolygon, MultiLineString, or GeometryCollection (by type)
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // Export OSM entity to GeoJSON
16
+ * import { nodeToFeature, wayToFeature } from "@osmix/geojson"
17
+ *
18
+ * const pointFeature = nodeToFeature(node)
19
+ * const lineFeature = wayToFeature(way, (ref) => osm.nodes.getNodeLonLat({ id: ref }))
20
+ * ```
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * // Import GeoJSON to Osm index
25
+ * import { fromGeoJSON } from "@osmix/geojson"
26
+ *
27
+ * const osm = await fromGeoJSON(geojsonFile, { id: "imported" })
28
+ * ```
29
+ *
30
+ * @module @osmix/geojson
31
+ */
32
+ export * from "./entity-to-feature";
33
+ export * from "./osm-from-geojson";
34
+ export * from "./types";
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,SAAS,CAAA"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * GeoJSON-to-OSM conversion utilities.
3
+ *
4
+ * Imports GeoJSON FeatureCollections into Osm indexes, mapping geometry
5
+ * types to appropriate OSM entity structures.
6
+ *
7
+ * @module
8
+ */
9
+ import { Osm, type OsmOptions } from "@osmix/core";
10
+ import { type ProgressEvent } from "@osmix/shared/progress";
11
+ import type { FeatureCollection, LineString, MultiPolygon, Point, Polygon } from "geojson";
12
+ import type { ReadOsmDataTypes } from "./types";
13
+ /**
14
+ * Create an Osm index from GeoJSON data.
15
+ *
16
+ * Accepts various input formats (string, stream, buffer, or parsed object)
17
+ * and converts features to OSM entities:
18
+ * - Point → Node
19
+ * - LineString → Way with nodes
20
+ * - Polygon → Way (simple) or Relation (with holes)
21
+ * - MultiPolygon → Relation
22
+ *
23
+ * Feature IDs are preserved if numeric; otherwise sequential negative IDs are assigned.
24
+ * Feature properties become OSM tags.
25
+ *
26
+ * @param data - GeoJSON data in any supported format.
27
+ * @param options - Osm index options (id, header).
28
+ * @param onProgress - Progress callback for UI feedback.
29
+ * @returns Populated Osm index with built indexes.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import { fromGeoJSON } from "@osmix/geojson"
34
+ *
35
+ * // From file
36
+ * const geojson = await Bun.file('./roads.geojson').json()
37
+ * const osm = await fromGeoJSON(geojson, { id: "roads" })
38
+ *
39
+ * // Query the imported data
40
+ * const highways = osm.ways.search("highway")
41
+ * ```
42
+ */
43
+ export declare function fromGeoJSON(data: ReadOsmDataTypes, options?: Partial<OsmOptions>, onProgress?: (progress: ProgressEvent) => void): Promise<Osm>;
44
+ /**
45
+ * Generator that converts GeoJSON features to OSM entities.
46
+ *
47
+ * This is the core conversion logic, yielding progress events as features
48
+ * are processed. Called by `fromGeoJSON` with progress handling.
49
+ *
50
+ * Geometry mapping:
51
+ * - **Point**: Creates a single node with feature properties as tags.
52
+ * - **LineString**: Creates nodes for each coordinate, then a way referencing them.
53
+ * - **Polygon**: Creates a way for simple polygons; for polygons with holes,
54
+ * creates separate ways for outer and inner rings plus a multipolygon relation.
55
+ * - **MultiPolygon**: Creates a multipolygon relation with all rings as way members.
56
+ *
57
+ * Coordinates are normalized using `rewind` to ensure correct winding order
58
+ * (outer rings counterclockwise, inner rings clockwise per OSM conventions).
59
+ *
60
+ * @param osm - Target Osm index to populate.
61
+ * @param geojson - Parsed GeoJSON FeatureCollection.
62
+ * @yields Progress events during conversion.
63
+ */
64
+ export declare function startCreateOsmFromGeoJSON(osm: Osm, geojson: FeatureCollection<Point | LineString | Polygon | MultiPolygon>): Generator<ProgressEvent>;
65
+ //# sourceMappingURL=osm-from-geojson.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"osm-from-geojson.d.ts","sourceRoot":"","sources":["../src/osm-from-geojson.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAEN,KAAK,aAAa,EAElB,MAAM,wBAAwB,CAAA;AAG/B,OAAO,KAAK,EACX,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,KAAK,EACL,OAAO,EACP,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,WAAW,CAChC,IAAI,EAAE,gBAAgB,EACtB,OAAO,GAAE,OAAO,CAAC,UAAU,CAAM,EACjC,UAAU,GAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,IAAkB,GACzD,OAAO,CAAC,GAAG,CAAC,CASd;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAiB,yBAAyB,CACzC,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,iBAAiB,CAAC,KAAK,GAAG,UAAU,GAAG,OAAO,GAAG,YAAY,CAAC,GACrE,SAAS,CAAC,aAAa,CAAC,CAsO1B"}
@@ -0,0 +1,303 @@
1
+ /**
2
+ * GeoJSON-to-OSM conversion utilities.
3
+ *
4
+ * Imports GeoJSON FeatureCollections into Osm indexes, mapping geometry
5
+ * types to appropriate OSM entity structures.
6
+ *
7
+ * @module
8
+ */
9
+ import { Osm } from "@osmix/core";
10
+ import { logProgress, progressEvent, } from "@osmix/shared/progress";
11
+ import { rewindFeature } from "@placemarkio/geojson-rewind";
12
+ import { readDataAsGeoJSON } from "./utils";
13
+ /**
14
+ * Create an Osm index from GeoJSON data.
15
+ *
16
+ * Accepts various input formats (string, stream, buffer, or parsed object)
17
+ * and converts features to OSM entities:
18
+ * - Point → Node
19
+ * - LineString → Way with nodes
20
+ * - Polygon → Way (simple) or Relation (with holes)
21
+ * - MultiPolygon → Relation
22
+ *
23
+ * Feature IDs are preserved if numeric; otherwise sequential negative IDs are assigned.
24
+ * Feature properties become OSM tags.
25
+ *
26
+ * @param data - GeoJSON data in any supported format.
27
+ * @param options - Osm index options (id, header).
28
+ * @param onProgress - Progress callback for UI feedback.
29
+ * @returns Populated Osm index with built indexes.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import { fromGeoJSON } from "@osmix/geojson"
34
+ *
35
+ * // From file
36
+ * const geojson = await Bun.file('./roads.geojson').json()
37
+ * const osm = await fromGeoJSON(geojson, { id: "roads" })
38
+ *
39
+ * // Query the imported data
40
+ * const highways = osm.ways.search("highway")
41
+ * ```
42
+ */
43
+ export async function fromGeoJSON(data, options = {}, onProgress = logProgress) {
44
+ const osm = new Osm(options);
45
+ for (const update of startCreateOsmFromGeoJSON(osm, await readDataAsGeoJSON(data))) {
46
+ onProgress(update);
47
+ }
48
+ return osm;
49
+ }
50
+ /**
51
+ * Generator that converts GeoJSON features to OSM entities.
52
+ *
53
+ * This is the core conversion logic, yielding progress events as features
54
+ * are processed. Called by `fromGeoJSON` with progress handling.
55
+ *
56
+ * Geometry mapping:
57
+ * - **Point**: Creates a single node with feature properties as tags.
58
+ * - **LineString**: Creates nodes for each coordinate, then a way referencing them.
59
+ * - **Polygon**: Creates a way for simple polygons; for polygons with holes,
60
+ * creates separate ways for outer and inner rings plus a multipolygon relation.
61
+ * - **MultiPolygon**: Creates a multipolygon relation with all rings as way members.
62
+ *
63
+ * Coordinates are normalized using `rewind` to ensure correct winding order
64
+ * (outer rings counterclockwise, inner rings clockwise per OSM conventions).
65
+ *
66
+ * @param osm - Target Osm index to populate.
67
+ * @param geojson - Parsed GeoJSON FeatureCollection.
68
+ * @yields Progress events during conversion.
69
+ */
70
+ export function* startCreateOsmFromGeoJSON(osm, geojson) {
71
+ yield progressEvent("Converting GeoJSON to Osmix...");
72
+ // Map to track nodes by coordinate string for reuse when creating ways and relations
73
+ const nodeMap = new Map();
74
+ let nextNodeId = -1;
75
+ let nextWayId = -1;
76
+ let nextRelationId = -1;
77
+ // Helper to get or create a node for a coordinate
78
+ const getOrCreateNode = (lon, lat) => {
79
+ const coordKey = `${lon},${lat}`;
80
+ const existingNodeId = nodeMap.get(coordKey);
81
+ if (existingNodeId !== undefined) {
82
+ return existingNodeId;
83
+ }
84
+ const nodeId = nextNodeId--;
85
+ nodeMap.set(coordKey, nodeId);
86
+ osm.nodes.addNode({
87
+ id: nodeId,
88
+ lon,
89
+ lat,
90
+ });
91
+ return nodeId;
92
+ };
93
+ // Process each feature
94
+ let count = 0;
95
+ for (const feature of geojson.features) {
96
+ // Normalize winding order using rewind (outer rings counterclockwise, inner rings clockwise)
97
+ const normalizedFeature = rewindFeature(feature);
98
+ const tags = propertiesToTags(normalizedFeature.properties);
99
+ const featureId = extractFeatureId(normalizedFeature.id);
100
+ if (normalizedFeature.geometry?.type === "Point") {
101
+ const [lon, lat] = normalizedFeature.geometry.coordinates;
102
+ if (lon === undefined || lat === undefined)
103
+ throw Error("Invalid GeoJSON coordinates in Point.");
104
+ const nodeId = featureId ?? nextNodeId--;
105
+ osm.nodes.addNode({
106
+ id: nodeId,
107
+ lon,
108
+ lat,
109
+ tags,
110
+ });
111
+ nodeMap.set(`${lon},${lat}`, nodeId);
112
+ }
113
+ else if (normalizedFeature.geometry?.type === "LineString") {
114
+ const coordinates = normalizedFeature.geometry.coordinates;
115
+ if (coordinates.length < 2)
116
+ throw Error("Invalid GeoJSON coordinates in LineString.");
117
+ // Create or get nodes for each coordinate
118
+ const nodeRefs = [];
119
+ for (const [lon, lat] of coordinates) {
120
+ if (lon === undefined || lat === undefined)
121
+ throw Error("Invalid GeoJSON coordinates in LineString.");
122
+ const nodeId = getOrCreateNode(lon, lat);
123
+ nodeRefs.push(nodeId);
124
+ }
125
+ // Create the way
126
+ const wayId = featureId ?? nextWayId--;
127
+ osm.ways.addWay({
128
+ id: wayId,
129
+ refs: nodeRefs,
130
+ tags,
131
+ });
132
+ }
133
+ else if (normalizedFeature.geometry?.type === "Polygon") {
134
+ const coordinates = normalizedFeature.geometry.coordinates;
135
+ if (coordinates.length === 0)
136
+ continue;
137
+ // If the polygon contains holes, create a relation
138
+ const createRelation = coordinates.length > 1;
139
+ // First ring is outer boundary, subsequent rings are holes
140
+ const outerRing = coordinates[0];
141
+ if (!outerRing || outerRing.length < 3)
142
+ continue; // Need at least 3 points for a polygon
143
+ // Create nodes for outer ring
144
+ const outerNodeRefs = [];
145
+ for (const [lon, lat] of outerRing) {
146
+ if (lon === undefined || lat === undefined)
147
+ throw Error("Invalid GeoJSON coordinates in Polygon.");
148
+ const nodeId = getOrCreateNode(lon, lat);
149
+ outerNodeRefs.push(nodeId);
150
+ }
151
+ // Ensure the outer ring is closed (first and last node are the same)
152
+ if (outerNodeRefs[0] !== outerNodeRefs[outerNodeRefs.length - 1])
153
+ throw Error("Outer ring of Polygon is not closed.");
154
+ // Create way for outer ring with area tags
155
+ const outerWayId = createRelation
156
+ ? nextWayId--
157
+ : (featureId ?? nextWayId--);
158
+ osm.ways.addWay({
159
+ id: outerWayId,
160
+ refs: outerNodeRefs,
161
+ tags: createRelation ? { area: "yes" } : { area: "yes", ...tags },
162
+ });
163
+ // Create separate ways for holes
164
+ const holeWayIds = [];
165
+ for (let i = 1; i < coordinates.length; i++) {
166
+ const holeRing = coordinates[i];
167
+ if (!holeRing || holeRing.length < 3)
168
+ throw Error("Hole ring of Polygon has less than 3 coordinates.");
169
+ const holeNodeRefs = [];
170
+ for (const [lon, lat] of holeRing) {
171
+ if (lon === undefined || lat === undefined)
172
+ throw Error("Invalid GeoJSON coordinates in Polygon.");
173
+ const nodeId = getOrCreateNode(lon, lat);
174
+ holeNodeRefs.push(nodeId);
175
+ }
176
+ // Ensure the ring is closed
177
+ if (holeNodeRefs[0] !== holeNodeRefs[holeNodeRefs.length - 1])
178
+ throw Error("Hole ring of Polygon is not closed.");
179
+ // Create way for hole
180
+ const holeWayId = nextWayId--;
181
+ osm.ways.addWay({
182
+ id: holeWayId,
183
+ refs: holeNodeRefs,
184
+ tags: { area: "yes" },
185
+ });
186
+ holeWayIds.push(holeWayId);
187
+ }
188
+ if (createRelation) {
189
+ osm.relations.addRelation({
190
+ id: featureId ?? nextRelationId--,
191
+ members: [
192
+ { type: "way", ref: outerWayId, role: "outer" },
193
+ ...holeWayIds.map((id) => ({ type: "way", ref: id, role: "inner" })),
194
+ ],
195
+ tags: {
196
+ type: "multipolygon",
197
+ ...tags,
198
+ },
199
+ });
200
+ }
201
+ }
202
+ else if (normalizedFeature.geometry?.type === "MultiPolygon") {
203
+ const coordinates = normalizedFeature.geometry.coordinates;
204
+ if (coordinates.length === 0)
205
+ continue;
206
+ // Process each polygon in the MultiPolygon
207
+ const relationMembers = [];
208
+ for (const polygon of coordinates) {
209
+ if (polygon.length === 0)
210
+ continue;
211
+ const outerRing = polygon[0];
212
+ if (!outerRing || outerRing.length < 3)
213
+ continue;
214
+ // Create nodes for outer ring
215
+ const outerNodeRefs = [];
216
+ for (const [lon, lat] of outerRing) {
217
+ if (lon === undefined || lat === undefined)
218
+ throw Error("Invalid GeoJSON coordinates in Polygon.");
219
+ const nodeId = getOrCreateNode(lon, lat);
220
+ outerNodeRefs.push(nodeId);
221
+ }
222
+ // Ensure the ring is closed
223
+ if (outerNodeRefs[0] !== outerNodeRefs[outerNodeRefs.length - 1])
224
+ throw Error("Outer ring of Polygon is not closed.");
225
+ // Create way for outer ring
226
+ const outerWayId = nextWayId--;
227
+ osm.ways.addWay({
228
+ id: outerWayId,
229
+ refs: outerNodeRefs,
230
+ tags: { area: "yes" },
231
+ });
232
+ relationMembers.push({ type: "way", ref: outerWayId, role: "outer" });
233
+ // Create separate ways for holes in this polygon
234
+ for (let i = 1; i < polygon.length; i++) {
235
+ const holeRing = polygon[i];
236
+ if (!holeRing || holeRing.length < 3)
237
+ throw Error("Hole ring of Polygon has less than 3 coordinates.");
238
+ const holeNodeRefs = [];
239
+ for (const [lon, lat] of holeRing) {
240
+ if (lon === undefined || lat === undefined)
241
+ throw Error("Invalid GeoJSON coordinates in Polygon.");
242
+ const nodeId = getOrCreateNode(lon, lat);
243
+ holeNodeRefs.push(nodeId);
244
+ }
245
+ // Ensure the ring is closed
246
+ if (holeNodeRefs[0] !== holeNodeRefs[holeNodeRefs.length - 1])
247
+ throw Error("Hole ring of Polygon is not closed.");
248
+ const holeWayId = nextWayId--;
249
+ osm.ways.addWay({
250
+ id: holeWayId,
251
+ refs: holeNodeRefs,
252
+ tags: { area: "yes" },
253
+ });
254
+ relationMembers.push({ type: "way", ref: holeWayId, role: "inner" });
255
+ }
256
+ }
257
+ if (relationMembers.length > 0) {
258
+ osm.relations.addRelation({
259
+ id: featureId ?? nextRelationId--,
260
+ members: relationMembers,
261
+ tags: { type: "multipolygon", ...tags },
262
+ });
263
+ }
264
+ }
265
+ yield progressEvent(`Processed ${count++} features...`);
266
+ }
267
+ yield progressEvent("Finished converting GeoJSON to Osmix, building indexes...");
268
+ // Build indexes
269
+ osm.buildIndexes();
270
+ osm.buildSpatialIndexes();
271
+ }
272
+ // Helper to convert properties to tags
273
+ function propertiesToTags(properties) {
274
+ if (!properties || Object.keys(properties).length === 0) {
275
+ return undefined;
276
+ }
277
+ const tags = {};
278
+ for (const [key, value] of Object.entries(properties)) {
279
+ // Convert value to string or number
280
+ if (typeof value === "string" || typeof value === "number") {
281
+ tags[key] = value;
282
+ }
283
+ else if (value != null) {
284
+ // Convert other types to string
285
+ tags[key] = String(value);
286
+ }
287
+ }
288
+ return Object.keys(tags).length > 0 ? tags : undefined;
289
+ }
290
+ // Helper to extract numeric ID from feature
291
+ function extractFeatureId(featureId) {
292
+ if (featureId === undefined)
293
+ return undefined;
294
+ if (typeof featureId === "number")
295
+ return featureId;
296
+ if (typeof featureId === "string") {
297
+ const numId = Number.parseInt(featureId, 10);
298
+ if (!Number.isNaN(numId))
299
+ return numId;
300
+ }
301
+ return undefined;
302
+ }
303
+ //# sourceMappingURL=osm-from-geojson.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"osm-from-geojson.js","sourceRoot":"","sources":["../src/osm-from-geojson.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,GAAG,EAAmB,MAAM,aAAa,CAAA;AAClD,OAAO,EACN,WAAW,EAEX,aAAa,GACb,MAAM,wBAAwB,CAAA;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAS3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAChC,IAAsB,EACtB,UAA+B,EAAE,EACjC,aAAgD,WAAW;IAE3D,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAA;IAC5B,KAAK,MAAM,MAAM,IAAI,yBAAyB,CAC7C,GAAG,EACH,MAAM,iBAAi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CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzD,OAAO,SAAS,CAAA;IACjB,CAAC;IAED,MAAM,IAAI,GAAY,EAAE,CAAA;IACxB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACvD,oCAAoC;QACpC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC5D,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QAClB,CAAC;aAAM,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAC1B,gCAAgC;YAChC,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;QAC1B,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;AACvD,CAAC;AAED,4CAA4C;AAC5C,SAAS,gBAAgB,CACxB,SAAsC;IAEtC,IAAI,SAAS,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAC7C,IAAI,OAAO,SAAS,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAA;IACnD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QACnC,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA;QAC5C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAA;IACvC,CAAC;IACD,OAAO,SAAS,CAAA;AACjB,CAAC"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Type definitions for OSM-GeoJSON conversion.
3
+ * @module
4
+ */
5
+ import type { OsmEntityType, OsmInfoParsed, OsmTags } from "@osmix/shared/types";
6
+ import type { Feature, GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon } from "geojson";
7
+ /**
8
+ * GeoJSON Feature properties for OSM entities.
9
+ *
10
+ * Includes the OSM entity ID, type, and optionally tags and metadata.
11
+ * All OSM tags are spread into the properties object.
12
+ */
13
+ export type OsmGeoJSONProperties = {
14
+ /** OSM entity ID. */
15
+ id: number;
16
+ /** Entity type: "node", "way", or "relation". */
17
+ type: OsmEntityType;
18
+ /** OSM tags (key-value pairs). */
19
+ tags?: OsmTags;
20
+ /** Entity metadata (version, timestamp, user, etc.). */
21
+ info?: OsmInfoParsed;
22
+ };
23
+ /**
24
+ * GeoJSON Feature with OSM-specific properties.
25
+ *
26
+ * Generic over geometry type to support all OSM entity conversions:
27
+ * - Nodes → Point
28
+ * - Ways → LineString or Polygon
29
+ * - Relations → MultiPolygon, MultiLineString, MultiPoint, or GeometryCollection
30
+ */
31
+ export type OsmGeoJSONFeature<T extends Point | LineString | Polygon | MultiPolygon | MultiLineString | MultiPoint | GeometryCollection> = Feature<T, OsmGeoJSONProperties>;
32
+ /**
33
+ * GeoJSON FeatureCollection types that can be imported into an Osm index.
34
+ *
35
+ * Supports Point, LineString, Polygon, and MultiPolygon geometries.
36
+ * Other geometry types are not supported for import.
37
+ */
38
+ export type ImportableGeoJSON = GeoJSON.FeatureCollection<GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPolygon>;
39
+ /**
40
+ * Input types accepted by `fromGeoJSON`.
41
+ *
42
+ * Supports multiple formats:
43
+ * - `ArrayBufferLike` - Binary GeoJSON data
44
+ * - `ReadableStream` - Streaming GeoJSON
45
+ * - `string` - JSON string
46
+ * - `ImportableGeoJSON` - Already-parsed FeatureCollection
47
+ */
48
+ export type ReadOsmDataTypes = ArrayBufferLike | ReadableStream | string | ImportableGeoJSON;
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAChF,OAAO,KAAK,EACX,OAAO,EACP,kBAAkB,EAClB,UAAU,EACV,eAAe,EACf,UAAU,EACV,YAAY,EACZ,KAAK,EACL,OAAO,EACP,MAAM,SAAS,CAAA;AAEhB;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IAClC,qBAAqB;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,iDAAiD;IACjD,IAAI,EAAE,aAAa,CAAA;IACnB,kCAAkC;IAClC,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,wDAAwD;IACxD,IAAI,CAAC,EAAE,aAAa,CAAA;CACpB,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,CAC5B,CAAC,SACE,KAAK,GACL,UAAU,GACV,OAAO,GACP,YAAY,GACZ,eAAe,GACf,UAAU,GACV,kBAAkB,IAClB,OAAO,CAAC,CAAC,EAAE,oBAAoB,CAAC,CAAA;AAEpC;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CACxD,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,UAAU,GAAG,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,YAAY,CAC3E,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,GACzB,eAAe,GACf,cAAc,GACd,MAAM,GACN,iBAAiB,CAAA"}
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Type definitions for OSM-GeoJSON conversion.
3
+ * @module
4
+ */
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Utility functions for GeoJSON data handling.
3
+ * @module
4
+ */
5
+ import type { ImportableGeoJSON, ReadOsmDataTypes } from "./types";
6
+ /**
7
+ * Parse GeoJSON from various input formats.
8
+ *
9
+ * Supports multiple input types for flexibility:
10
+ * - **string**: JSON string to parse
11
+ * - **ReadableStream**: Streaming JSON data
12
+ * - **ArrayBuffer/SharedArrayBuffer**: Binary data to decode and parse
13
+ * - **Object**: Already-parsed FeatureCollection (passed through)
14
+ *
15
+ * @param data - GeoJSON data in any supported format.
16
+ * @returns Parsed GeoJSON FeatureCollection.
17
+ * @throws If data is null or an unsupported type.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // From string
22
+ * const geojson = await readDataAsGeoJSON('{"type":"FeatureCollection","features":[]}')
23
+ *
24
+ * // From fetch response
25
+ * const response = await fetch('/data.geojson')
26
+ * const geojson = await readDataAsGeoJSON(response.body!)
27
+ * ```
28
+ */
29
+ export declare function readDataAsGeoJSON(data: ReadOsmDataTypes): Promise<ImportableGeoJSON>;
30
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAElE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,iBAAiB,CACtC,IAAI,EAAE,gBAAgB,GACpB,OAAO,CAAC,iBAAiB,CAAC,CAyB5B"}
package/dist/utils.js ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Utility functions for GeoJSON data handling.
3
+ * @module
4
+ */
5
+ /**
6
+ * Parse GeoJSON from various input formats.
7
+ *
8
+ * Supports multiple input types for flexibility:
9
+ * - **string**: JSON string to parse
10
+ * - **ReadableStream**: Streaming JSON data
11
+ * - **ArrayBuffer/SharedArrayBuffer**: Binary data to decode and parse
12
+ * - **Object**: Already-parsed FeatureCollection (passed through)
13
+ *
14
+ * @param data - GeoJSON data in any supported format.
15
+ * @returns Parsed GeoJSON FeatureCollection.
16
+ * @throws If data is null or an unsupported type.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * // From string
21
+ * const geojson = await readDataAsGeoJSON('{"type":"FeatureCollection","features":[]}')
22
+ *
23
+ * // From fetch response
24
+ * const response = await fetch('/data.geojson')
25
+ * const geojson = await readDataAsGeoJSON(response.body!)
26
+ * ```
27
+ */
28
+ export async function readDataAsGeoJSON(data) {
29
+ if (data == null)
30
+ throw new Error("Data is null");
31
+ if (typeof data === "string")
32
+ return JSON.parse(data);
33
+ if (data instanceof ReadableStream) {
34
+ const reader = data.pipeThrough(new TextDecoderStream()).getReader();
35
+ let result = "";
36
+ while (true) {
37
+ const { done, value } = await reader.read();
38
+ result += value;
39
+ if (done)
40
+ break;
41
+ }
42
+ return JSON.parse(result);
43
+ }
44
+ if (data instanceof ArrayBuffer || data instanceof SharedArrayBuffer) {
45
+ const decoder = new TextDecoder();
46
+ const text = decoder.decode(new Uint8Array(data));
47
+ return JSON.parse(text);
48
+ }
49
+ if (typeof data === "object" && "type" in data && "features" in data) {
50
+ return data;
51
+ }
52
+ throw new Error("Invalid data type. Accepts string, ReadableStream, ArrayBufferLike, or GeoJSON FeatureCollection.");
53
+ }
54
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACtC,IAAsB;IAEtB,IAAI,IAAI,IAAI,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;IACjD,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAsB,CAAA;IAE1E,IAAI,IAAI,YAAY,cAAc,EAAE,CAAC;QACpC,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,iBAAiB,EAAE,CAAC,CAAC,SAAS,EAAE,CAAA;QACpE,IAAI,MAAM,GAAG,EAAE,CAAA;QACf,OAAO,IAAI,EAAE,CAAC;YACb,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;YAC3C,MAAM,IAAI,KAAK,CAAA;YACf,IAAI,IAAI;gBAAE,MAAK;QAChB,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAsB,CAAA;IAC/C,CAAC;IACD,IAAI,IAAI,YAAY,WAAW,IAAI,IAAI,YAAY,iBAAiB,EAAE,CAAC;QACtE,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;QACjC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAA;QACjD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAsB,CAAA;IAC7C,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,MAAM,IAAI,IAAI,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;QACtE,OAAO,IAAyB,CAAA;IACjC,CAAC;IACD,MAAM,IAAI,KAAK,CACd,mGAAmG,CACnG,CAAA;AACF,CAAC"}
package/package.json CHANGED
@@ -2,12 +2,13 @@
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@osmix/geojson",
4
4
  "description": "Convert OSM files to and from GeoJSON.",
5
- "version": "0.0.6",
5
+ "version": "0.0.8",
6
6
  "type": "module",
7
7
  "main": "./src/index.ts",
8
8
  "types": "./src/index.ts",
9
9
  "publishConfig": {
10
10
  "access": "public",
11
+ "registry": "https://registry.npmjs.org/",
11
12
  "main": "./dist/index.js",
12
13
  "types": "./dist/index.d.ts",
13
14
  "exports": {
@@ -33,22 +34,23 @@
33
34
  },
34
35
  "sideEffects": false,
35
36
  "scripts": {
36
- "build": "tsc --excludeDirectories test",
37
+ "build": "tsc -p tsconfig.build.json",
37
38
  "bench": "bun test --bench",
38
- "prepublishOnly": "tsc",
39
+ "prepare": "bun run build",
40
+ "release": "bun publish",
39
41
  "test": "bun test",
40
42
  "typecheck": "tsgo --noEmit"
41
43
  },
42
44
  "devDependencies": {
43
- "@types/bun": "catalog:",
44
- "@types/geojson": "catalog:",
45
- "typescript": "catalog:"
45
+ "@types/bun": "^1.3.3",
46
+ "@types/geojson": "^7946.0.16",
47
+ "typescript": "^5.9.0"
46
48
  },
47
49
  "dependencies": {
48
- "@osmix/core": "workspace:*",
49
- "@osmix/pbf": "workspace:*",
50
- "@osmix/shared": "workspace:*",
51
- "@placemarkio/geojson-rewind": "catalog:",
52
- "dequal": "catalog:"
50
+ "@osmix/core": "0.1.3",
51
+ "@osmix/pbf": "0.0.5",
52
+ "@osmix/shared": "0.0.8",
53
+ "@placemarkio/geojson-rewind": "^1.0.3",
54
+ "dequal": "^2.0.3"
53
55
  }
54
56
  }
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.json",
4
+ "include": ["src"]
5
+ }