@osmix/geojson 0.0.4

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 (39) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +129 -0
  3. package/dist/src/entity-to-feature.d.ts +86 -0
  4. package/dist/src/entity-to-feature.d.ts.map +1 -0
  5. package/dist/src/entity-to-feature.js +345 -0
  6. package/dist/src/entity-to-feature.js.map +1 -0
  7. package/dist/src/index.d.ts +35 -0
  8. package/dist/src/index.d.ts.map +1 -0
  9. package/dist/src/index.js +35 -0
  10. package/dist/src/index.js.map +1 -0
  11. package/dist/src/osm-from-geojson.d.ts +65 -0
  12. package/dist/src/osm-from-geojson.d.ts.map +1 -0
  13. package/dist/src/osm-from-geojson.js +303 -0
  14. package/dist/src/osm-from-geojson.js.map +1 -0
  15. package/dist/src/types.d.ts +49 -0
  16. package/dist/src/types.d.ts.map +1 -0
  17. package/dist/src/types.js +5 -0
  18. package/dist/src/types.js.map +1 -0
  19. package/dist/src/utils.d.ts +30 -0
  20. package/dist/src/utils.d.ts.map +1 -0
  21. package/dist/src/utils.js +54 -0
  22. package/dist/src/utils.js.map +1 -0
  23. package/dist/test/entity-to-feature.test.d.ts +2 -0
  24. package/dist/test/entity-to-feature.test.d.ts.map +1 -0
  25. package/dist/test/entity-to-feature.test.js +57 -0
  26. package/dist/test/entity-to-feature.test.js.map +1 -0
  27. package/dist/test/osm-from-geojson.test.d.ts +2 -0
  28. package/dist/test/osm-from-geojson.test.d.ts.map +1 -0
  29. package/dist/test/osm-from-geojson.test.js +778 -0
  30. package/dist/test/osm-from-geojson.test.js.map +1 -0
  31. package/package.json +54 -0
  32. package/src/entity-to-feature.ts +427 -0
  33. package/src/index.ts +35 -0
  34. package/src/osm-from-geojson.ts +360 -0
  35. package/src/types.ts +77 -0
  36. package/src/utils.ts +58 -0
  37. package/test/entity-to-feature.test.ts +65 -0
  38. package/test/osm-from-geojson.test.ts +868 -0
  39. package/tsconfig.json +9 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # @osmix/geojson
2
+
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [3846a0c]
8
+ - Updated dependencies [0cd8a2e]
9
+ - Updated dependencies [cdab4db]
10
+ - @osmix/core@0.0.6
11
+ - @osmix/shared@0.0.6
12
+ - @osmix/pbf@0.0.3
13
+
14
+ ## 0.0.3
15
+
16
+ ### Patch Changes
17
+
18
+ - edbb26b: Handle more Relation types
19
+ - Updated dependencies [bb629cf]
20
+ - Updated dependencies [a33a280]
21
+ - Updated dependencies [edbb26b]
22
+ - Updated dependencies [69a36bd]
23
+ - @osmix/shared@0.0.5
24
+ - @osmix/core@0.0.5
25
+ - @osmix/pbf@0.0.3
26
+
27
+ ## 0.0.2
28
+
29
+ ### Patch Changes
30
+
31
+ - d001d9a: Refactor to align around new main external API
32
+ - Updated dependencies [572cbd8]
33
+ - Updated dependencies [d001d9a]
34
+ - Updated dependencies [4303c40]
35
+ - @osmix/shared@0.0.4
36
+ - @osmix/core@0.0.4
37
+ - @osmix/pbf@0.0.3
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # @osmix/geojson
2
+
3
+ Bidirectional conversion between OSM entities and GeoJSON. Export `Osm` entities as GeoJSON Features (preserving all tags) or import GeoJSON FeatureCollections into a fresh `Osm` index.
4
+
5
+ ## Highlights
6
+
7
+ - **Export** nodes, ways, and relations to typed GeoJSON Features.
8
+ - **Import** Point, LineString, Polygon, and MultiPolygon features into Osm indexes.
9
+ - **Preserve** OSM tags and metadata in GeoJSON properties.
10
+ - **Handle** complex geometries like multipolygons with holes automatically.
11
+
12
+ ## Installation
13
+
14
+ ```sh
15
+ bun add @osmix/geojson
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### Export entities to GeoJSON
21
+
22
+ ```ts
23
+ import { nodeToFeature, wayToFeature, relationToFeature } from "@osmix/geojson"
24
+
25
+ // Convert individual entities
26
+ const nodeFeature = nodeToFeature(osm.nodes.get({ id: 1 })!)
27
+ const wayFeature = wayToFeature(
28
+ osm.ways.get({ id: 10 })!,
29
+ (ref) => osm.nodes.getNodeLonLat({ id: ref })
30
+ )
31
+ const relationFeature = relationToFeature(
32
+ osm.relations.get({ id: 50 })!,
33
+ (ref) => osm.nodes.getNodeLonLat({ id: ref }),
34
+ (wayId) => osm.ways.getById(wayId)
35
+ )
36
+ ```
37
+
38
+ ### Export using the helper function
39
+
40
+ ```ts
41
+ import { osmEntityToGeoJSONFeature } from "@osmix/geojson"
42
+
43
+ // Convert any entity type with automatic coordinate resolution
44
+ const features = []
45
+ for (const way of osm.ways) {
46
+ features.push(osmEntityToGeoJSONFeature(osm, way))
47
+ }
48
+
49
+ const featureCollection = {
50
+ type: "FeatureCollection",
51
+ features,
52
+ }
53
+ ```
54
+
55
+ ### Import GeoJSON to Osm
56
+
57
+ ```ts
58
+ import { fromGeoJSON } from "@osmix/geojson"
59
+
60
+ const geojsonFile = await Bun.file('./roads.geojson').json()
61
+ const osm = await fromGeoJSON(geojsonFile, { id: "roads" })
62
+
63
+ // Query imported data
64
+ const highways = osm.ways.search("highway")
65
+ console.log(`Imported ${osm.ways.size} ways`)
66
+ ```
67
+
68
+ `fromGeoJSON` handles `Point`, `LineString`, `Polygon`, and `MultiPolygon` geometries. Polygons with holes automatically create multipolygon relations with separate ways for outer and inner rings.
69
+
70
+ ## API
71
+
72
+ ### Export (OSM → GeoJSON)
73
+
74
+ | Export | Description |
75
+ |--------|-------------|
76
+ | `nodeToFeature(node)` | Node → Point Feature |
77
+ | `wayToFeature(way, refToPosition)` | Way → LineString or Polygon Feature |
78
+ | `relationToFeature(relation, refToPosition, getWay?)` | Relation → Multi* or GeometryCollection |
79
+ | `osmEntityToGeoJSONFeature(osm, entity)` | Any entity → Feature with auto coordinate resolution |
80
+
81
+ ### Import (GeoJSON → OSM)
82
+
83
+ | Export | Description |
84
+ |--------|-------------|
85
+ | `fromGeoJSON(data, options?, onProgress?)` | Create Osm index from GeoJSON |
86
+ | `startCreateOsmFromGeoJSON(osm, geojson)` | Generator for custom progress handling |
87
+
88
+ ### Types
89
+
90
+ | Export | Description |
91
+ |--------|-------------|
92
+ | `OsmGeoJSONFeature<T>` | GeoJSON Feature with OSM properties |
93
+ | `OsmGeoJSONProperties` | Properties type (id, type, tags, info) |
94
+ | `ImportableGeoJSON` | FeatureCollection types supported for import |
95
+ | `ReadOsmDataTypes` | Input types for `fromGeoJSON` |
96
+
97
+ ## Geometry Mapping
98
+
99
+ | OSM Entity | GeoJSON Geometry |
100
+ |------------|------------------|
101
+ | Node | Point |
102
+ | Way (linear) | LineString |
103
+ | Way (area) | Polygon |
104
+ | Relation (multipolygon) | MultiPolygon or Polygon |
105
+ | Relation (route) | MultiLineString or LineString |
106
+ | Relation (site) | MultiPoint or Point |
107
+ | Relation (other) | GeometryCollection |
108
+
109
+ ## Related Packages
110
+
111
+ - [`@osmix/core`](../core/README.md) – In-memory OSM storage these features convert from/to.
112
+ - [`@osmix/json`](../json/README.md) – JSON entity types used in conversion.
113
+ - [`@osmix/pbf`](../pbf/README.md) – PBF reading/writing for complete workflows.
114
+
115
+ ## Environment and Limitations
116
+
117
+ - Requires `Map`/`Set`, typed arrays, and `ReadableStream`/`TextDecoder` (Bun, Node 20+, modern browsers).
118
+ - Import supports Point, LineString, Polygon, MultiPolygon only.
119
+ - Relation export focuses on multipolygon/route relations; other types return GeometryCollection.
120
+
121
+ ## Development
122
+
123
+ ```sh
124
+ bun run test packages/geojson
125
+ bun run lint packages/geojson
126
+ bun run typecheck packages/geojson
127
+ ```
128
+
129
+ Run `bun run check` at the repo root before publishing.
@@ -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,CA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mix 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"}
@@ -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