@osmix/shared 0.0.2 → 0.0.7

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 (208) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +49 -19
  3. package/dist/assert.d.ts +24 -0
  4. package/dist/assert.d.ts.map +1 -0
  5. package/dist/assert.js +28 -0
  6. package/dist/assert.js.map +1 -0
  7. package/dist/bbox-intersects.d.ts +15 -0
  8. package/dist/bbox-intersects.d.ts.map +1 -0
  9. package/dist/bbox-intersects.js +24 -0
  10. package/dist/bbox-intersects.js.map +1 -0
  11. package/dist/bytes-to-stream.d.ts +18 -0
  12. package/dist/bytes-to-stream.d.ts.map +1 -0
  13. package/dist/bytes-to-stream.js +25 -0
  14. package/dist/bytes-to-stream.js.map +1 -0
  15. package/dist/color.d.ts +4 -0
  16. package/dist/color.d.ts.map +1 -0
  17. package/dist/color.js +33 -0
  18. package/dist/color.js.map +1 -0
  19. package/dist/concat-bytes.d.ts +5 -0
  20. package/dist/concat-bytes.d.ts.map +1 -0
  21. package/dist/concat-bytes.js +14 -0
  22. package/dist/concat-bytes.js.map +1 -0
  23. package/dist/coordinates.d.ts +28 -0
  24. package/dist/coordinates.d.ts.map +1 -0
  25. package/dist/coordinates.js +38 -0
  26. package/dist/coordinates.js.map +1 -0
  27. package/dist/haversine-distance.d.ts +16 -0
  28. package/dist/haversine-distance.d.ts.map +1 -0
  29. package/dist/haversine-distance.js +26 -0
  30. package/dist/haversine-distance.js.map +1 -0
  31. package/dist/lineclip.d.ts +2 -0
  32. package/dist/lineclip.d.ts.map +1 -0
  33. package/dist/lineclip.js +3 -0
  34. package/dist/lineclip.js.map +1 -0
  35. package/dist/progress.d.ts +42 -0
  36. package/dist/progress.d.ts.map +1 -0
  37. package/dist/progress.js +53 -0
  38. package/dist/progress.js.map +1 -0
  39. package/dist/relation-kind.d.ts +69 -0
  40. package/dist/relation-kind.d.ts.map +1 -0
  41. package/dist/relation-kind.js +375 -0
  42. package/dist/relation-kind.js.map +1 -0
  43. package/dist/relation-multipolygon.d.ts +43 -0
  44. package/dist/relation-multipolygon.d.ts.map +1 -0
  45. package/dist/relation-multipolygon.js +195 -0
  46. package/dist/relation-multipolygon.js.map +1 -0
  47. package/dist/src/assert.d.ts +20 -1
  48. package/dist/src/assert.d.ts.map +1 -1
  49. package/dist/src/assert.js +20 -1
  50. package/dist/src/assert.js.map +1 -1
  51. package/dist/src/bbox-intersects.d.ts +15 -0
  52. package/dist/src/bbox-intersects.d.ts.map +1 -0
  53. package/dist/src/bbox-intersects.js +24 -0
  54. package/dist/src/bbox-intersects.js.map +1 -0
  55. package/dist/src/bytes-to-stream.d.ts +17 -1
  56. package/dist/src/bytes-to-stream.d.ts.map +1 -1
  57. package/dist/src/bytes-to-stream.js +16 -0
  58. package/dist/src/bytes-to-stream.js.map +1 -1
  59. package/dist/src/color.d.ts +4 -0
  60. package/dist/src/color.d.ts.map +1 -0
  61. package/dist/src/color.js +33 -0
  62. package/dist/src/color.js.map +1 -0
  63. package/dist/src/coordinates.d.ts +28 -0
  64. package/dist/src/coordinates.d.ts.map +1 -0
  65. package/dist/src/coordinates.js +38 -0
  66. package/dist/src/coordinates.js.map +1 -0
  67. package/dist/src/haversine-distance.d.ts +9 -1
  68. package/dist/src/haversine-distance.d.ts.map +1 -1
  69. package/dist/src/haversine-distance.js +9 -1
  70. package/dist/src/haversine-distance.js.map +1 -1
  71. package/dist/src/progress.d.ts +42 -0
  72. package/dist/src/progress.d.ts.map +1 -0
  73. package/dist/src/progress.js +53 -0
  74. package/dist/src/progress.js.map +1 -0
  75. package/dist/src/relation-kind.d.ts +69 -0
  76. package/dist/src/relation-kind.d.ts.map +1 -0
  77. package/dist/src/relation-kind.js +375 -0
  78. package/dist/src/relation-kind.js.map +1 -0
  79. package/dist/src/relation-multipolygon.d.ts +43 -0
  80. package/dist/src/relation-multipolygon.d.ts.map +1 -0
  81. package/dist/src/relation-multipolygon.js +195 -0
  82. package/dist/src/relation-multipolygon.js.map +1 -0
  83. package/dist/src/stream-to-bytes.d.ts +16 -0
  84. package/dist/src/stream-to-bytes.d.ts.map +1 -1
  85. package/dist/src/stream-to-bytes.js +16 -0
  86. package/dist/src/stream-to-bytes.js.map +1 -1
  87. package/dist/src/test/fixtures.d.ts +1 -1
  88. package/dist/src/test/fixtures.d.ts.map +1 -1
  89. package/dist/src/test/fixtures.js +16 -8
  90. package/dist/src/test/fixtures.js.map +1 -1
  91. package/dist/src/throttle.d.ts +25 -0
  92. package/dist/src/throttle.d.ts.map +1 -0
  93. package/dist/src/throttle.js +34 -0
  94. package/dist/src/throttle.js.map +1 -0
  95. package/dist/src/tile.d.ts +34 -0
  96. package/dist/src/tile.d.ts.map +1 -0
  97. package/dist/src/tile.js +72 -0
  98. package/dist/src/tile.js.map +1 -0
  99. package/dist/src/transform-bytes.d.ts +22 -0
  100. package/dist/src/transform-bytes.d.ts.map +1 -1
  101. package/dist/src/transform-bytes.js +22 -0
  102. package/dist/src/transform-bytes.js.map +1 -1
  103. package/dist/src/types.d.ts +76 -1
  104. package/dist/src/types.d.ts.map +1 -1
  105. package/dist/src/types.js +8 -0
  106. package/dist/src/types.js.map +1 -1
  107. package/dist/src/utils.d.ts +30 -0
  108. package/dist/src/utils.d.ts.map +1 -0
  109. package/dist/src/utils.js +70 -0
  110. package/dist/src/utils.js.map +1 -0
  111. package/dist/src/way-is-area.d.ts +24 -0
  112. package/dist/src/way-is-area.d.ts.map +1 -0
  113. package/dist/src/way-is-area.js +104 -0
  114. package/dist/src/way-is-area.js.map +1 -0
  115. package/dist/src/zigzag.d.ts +33 -0
  116. package/dist/src/zigzag.d.ts.map +1 -0
  117. package/dist/src/zigzag.js +40 -0
  118. package/dist/src/zigzag.js.map +1 -0
  119. package/dist/stream-to-bytes.d.ts +18 -0
  120. package/dist/stream-to-bytes.d.ts.map +1 -0
  121. package/dist/stream-to-bytes.js +30 -0
  122. package/dist/stream-to-bytes.js.map +1 -0
  123. package/dist/test/fixtures.d.ts +36 -0
  124. package/dist/test/fixtures.d.ts.map +1 -0
  125. package/dist/test/fixtures.js +175 -0
  126. package/dist/test/fixtures.js.map +1 -0
  127. package/dist/test/haversine-distance.test.js +2 -2
  128. package/dist/test/haversine-distance.test.js.map +1 -1
  129. package/dist/test/relation-kind.test.d.ts +2 -0
  130. package/dist/test/relation-kind.test.d.ts.map +1 -0
  131. package/dist/test/relation-kind.test.js +367 -0
  132. package/dist/test/relation-kind.test.js.map +1 -0
  133. package/dist/test/relation-multipolygon.test.d.ts +2 -0
  134. package/dist/test/relation-multipolygon.test.d.ts.map +1 -0
  135. package/dist/test/relation-multipolygon.test.js +237 -0
  136. package/dist/test/relation-multipolygon.test.js.map +1 -0
  137. package/dist/test/utils.test.d.ts +2 -0
  138. package/dist/test/utils.test.d.ts.map +1 -0
  139. package/dist/test/utils.test.js +76 -0
  140. package/dist/test/utils.test.js.map +1 -0
  141. package/dist/test/way-is-area.test.d.ts +2 -0
  142. package/dist/test/way-is-area.test.d.ts.map +1 -0
  143. package/dist/test/way-is-area.test.js +31 -0
  144. package/dist/test/way-is-area.test.js.map +1 -0
  145. package/dist/throttle.d.ts +25 -0
  146. package/dist/throttle.d.ts.map +1 -0
  147. package/dist/throttle.js +34 -0
  148. package/dist/throttle.js.map +1 -0
  149. package/dist/tile.d.ts +34 -0
  150. package/dist/tile.d.ts.map +1 -0
  151. package/dist/tile.js +72 -0
  152. package/dist/tile.js.map +1 -0
  153. package/dist/transform-bytes.d.ts +24 -0
  154. package/dist/transform-bytes.d.ts.map +1 -0
  155. package/dist/transform-bytes.js +28 -0
  156. package/dist/transform-bytes.js.map +1 -0
  157. package/dist/types.d.ts +99 -0
  158. package/dist/types.d.ts.map +1 -0
  159. package/dist/types.js +9 -0
  160. package/dist/types.js.map +1 -0
  161. package/dist/utils.d.ts +30 -0
  162. package/dist/utils.d.ts.map +1 -0
  163. package/dist/utils.js +70 -0
  164. package/dist/utils.js.map +1 -0
  165. package/dist/way-is-area.d.ts +24 -0
  166. package/dist/way-is-area.d.ts.map +1 -0
  167. package/dist/way-is-area.js +104 -0
  168. package/dist/way-is-area.js.map +1 -0
  169. package/dist/zigzag.d.ts +33 -0
  170. package/dist/zigzag.d.ts.map +1 -0
  171. package/dist/zigzag.js +40 -0
  172. package/dist/zigzag.js.map +1 -0
  173. package/package.json +11 -10
  174. package/src/assert.ts +21 -1
  175. package/src/bbox-intersects.ts +30 -0
  176. package/src/bytes-to-stream.ts +17 -0
  177. package/src/color.ts +37 -0
  178. package/src/coordinates.ts +45 -0
  179. package/src/haversine-distance.ts +10 -1
  180. package/src/progress.ts +74 -0
  181. package/src/relation-kind.ts +446 -0
  182. package/src/relation-multipolygon.ts +225 -0
  183. package/src/stream-to-bytes.ts +17 -0
  184. package/src/test/fixtures.ts +16 -12
  185. package/src/throttle.ts +37 -0
  186. package/src/tile.ts +89 -0
  187. package/src/transform-bytes.ts +23 -0
  188. package/src/types.ts +93 -1
  189. package/src/utils.ts +79 -0
  190. package/src/way-is-area.ts +107 -0
  191. package/src/zigzag.ts +42 -0
  192. package/test/haversine-distance.test.ts +2 -2
  193. package/test/relation-kind.test.ts +426 -0
  194. package/test/relation-multipolygon.test.ts +265 -0
  195. package/test/utils.test.ts +103 -0
  196. package/test/way-is-area.test.ts +42 -0
  197. package/tsconfig/test.json +1 -0
  198. package/tsconfig.build.json +5 -0
  199. package/dist/src/spherical-mercator.d.ts +0 -15
  200. package/dist/src/spherical-mercator.d.ts.map +0 -1
  201. package/dist/src/spherical-mercator.js +0 -35
  202. package/dist/src/spherical-mercator.js.map +0 -1
  203. package/dist/src/spherical-mercator.test.d.ts +0 -2
  204. package/dist/src/spherical-mercator.test.d.ts.map +0 -1
  205. package/dist/src/spherical-mercator.test.js +0 -25
  206. package/dist/src/spherical-mercator.test.js.map +0 -1
  207. package/src/spherical-mercator.test.ts +0 -42
  208. package/src/spherical-mercator.ts +0 -42
@@ -1,5 +1,14 @@
1
1
  /**
2
- * Calculate the haversine distance between two LonLat points.
2
+ * Great-circle distance calculation using the Haversine formula.
3
+ *
4
+ * Computes the shortest distance between two points on a sphere,
5
+ * accounting for Earth's curvature. Accurate for most geographic distances.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ /**
11
+ * Calculate the great-circle distance between two geographic points.
3
12
  * @param p1 - The first point
4
13
  * @param p2 - The second point
5
14
  * @returns The haversine distance in meters
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Progress event helpers for long-running operations.
3
+ *
4
+ * Provides a standard interface for reporting progress from workers and
5
+ * async operations. Uses CustomEvent for cross-context communication.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ export type ProgressLevel = "info" | "warn" | "error"
11
+
12
+ /**
13
+ * Progress payload containing a message and timestamp.
14
+ * Planned expansion to include percentage completion.
15
+ */
16
+ export type Progress = {
17
+ msg: string
18
+ timestamp: number
19
+ level: ProgressLevel
20
+ }
21
+
22
+ /** CustomEvent carrying progress details. */
23
+ export interface ProgressEvent extends CustomEvent<Progress> {}
24
+
25
+ /**
26
+ * Create a Progress payload with current timestamp.
27
+ * @param msg - The progress message.
28
+ */
29
+ export function progress(msg: string, level: ProgressLevel = "info"): Progress {
30
+ return {
31
+ msg,
32
+ timestamp: Date.now(),
33
+ level,
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Create a ProgressEvent with the given message.
39
+ * @param msg - The progress message.
40
+ */
41
+ export function progressEvent(
42
+ msg: string,
43
+ level: ProgressLevel = "info",
44
+ ): ProgressEvent {
45
+ return new CustomEvent("progress", { detail: progress(msg, level) })
46
+ }
47
+
48
+ /**
49
+ * Extract the message string from a progress event.
50
+ * @param event - The event to extract from.
51
+ */
52
+ export function progressEventMessage(event: Event): string {
53
+ return (event as ProgressEvent).detail.msg
54
+ }
55
+
56
+ /**
57
+ * Log a progress event's message to the console.
58
+ * @param progress - The progress event to log.
59
+ */
60
+ export function logProgress(progress: ProgressEvent) {
61
+ const level = progress.detail.level
62
+ const message = progressEventMessage(progress)
63
+ switch (level) {
64
+ case "info":
65
+ console.log(message)
66
+ break
67
+ case "warn":
68
+ console.warn(message)
69
+ break
70
+ case "error":
71
+ console.error(message)
72
+ break
73
+ }
74
+ }
@@ -0,0 +1,446 @@
1
+ /**
2
+ * Relation kind detection and geometry building.
3
+ *
4
+ * Determines the semantic type of OSM relations (area, line, point, logic, super)
5
+ * based on their type tag and member structure. Also provides utilities for
6
+ * building geometries from relation members.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ import type {
12
+ LonLat,
13
+ OsmRelation,
14
+ OsmRelationMember,
15
+ OsmWay,
16
+ RelationKind,
17
+ RelationKindMetadata,
18
+ } from "./types"
19
+
20
+ /**
21
+ * Get the semantic kind of a relation based on its type tag.
22
+ * Based on [OSM relation documentation](https://wiki.openstreetmap.org/wiki/Relation):
23
+ * - Areas: multipolygon, boundary, site
24
+ * - Lines: route, waterway, multilinestring, canal
25
+ * - Points: multipoint
26
+ * - Logic: restriction, route_master, network, collection
27
+ * - [Super: relations that contain other relations](https://wiki.openstreetmap.org/wiki/Super-relation)
28
+ */
29
+ export function getRelationKind(relation: OsmRelation): RelationKind {
30
+ const typeTag = relation.tags?.["type"]
31
+ if (!typeTag || typeof typeTag !== "string") {
32
+ // If no type tag, check if it has relation members (super-relation)
33
+ if (relation.members.some((m) => m.type === "relation")) {
34
+ return "super"
35
+ }
36
+ // Default to logic for untyped relations
37
+ return "logic"
38
+ }
39
+
40
+ const normalizedType = typeTag.toLowerCase()
41
+
42
+ // Area relations
43
+ if (
44
+ normalizedType === "multipolygon" ||
45
+ normalizedType === "boundary" ||
46
+ normalizedType === "site"
47
+ ) {
48
+ return "area"
49
+ }
50
+
51
+ // Line relations
52
+ if (
53
+ normalizedType === "route" ||
54
+ normalizedType === "waterway" ||
55
+ normalizedType === "multilinestring" ||
56
+ normalizedType === "canal"
57
+ ) {
58
+ return "line"
59
+ }
60
+
61
+ // Point relations
62
+ if (normalizedType === "multipoint") {
63
+ return "point"
64
+ }
65
+
66
+ // Check for super-relation (has relation members)
67
+ if (relation.members.some((m) => m.type === "relation")) {
68
+ return "super"
69
+ }
70
+
71
+ // Default to logic for other types (restriction, route_master, network, collection, etc.)
72
+ return "logic"
73
+ }
74
+
75
+ /**
76
+ * Get metadata about a relation kind.
77
+ */
78
+ export function getRelationKindMetadata(
79
+ relation: OsmRelation,
80
+ ): RelationKindMetadata {
81
+ const kind = getRelationKind(relation)
82
+
83
+ switch (kind) {
84
+ case "area":
85
+ return {
86
+ kind: "area",
87
+ expectedRoles: ["outer", "inner"],
88
+ orderMatters: false,
89
+ description: "Area relation (multipolygon, boundary, site)",
90
+ }
91
+ case "line":
92
+ return {
93
+ kind: "line",
94
+ expectedRoles: undefined, // Routes can have various roles
95
+ orderMatters: true,
96
+ description: "Line relation (route, waterway, multilinestring)",
97
+ }
98
+ case "point":
99
+ return {
100
+ kind: "point",
101
+ expectedRoles: undefined,
102
+ orderMatters: false,
103
+ description: "Point relation (multipoint)",
104
+ }
105
+ case "super":
106
+ return {
107
+ kind: "super",
108
+ expectedRoles: undefined,
109
+ orderMatters: false,
110
+ description: "Super-relation (contains other relations)",
111
+ }
112
+ case "logic":
113
+ return {
114
+ kind: "logic",
115
+ expectedRoles: undefined,
116
+ orderMatters: false,
117
+ description:
118
+ "Logical relation (restriction, route_master, network, collection, etc.)",
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Check if a relation is an area relation.
125
+ */
126
+ export function isAreaRelation(relation: OsmRelation): boolean {
127
+ return getRelationKind(relation) === "area"
128
+ }
129
+
130
+ /**
131
+ * Check if a relation is a line relation.
132
+ */
133
+ export function isLineRelation(relation: OsmRelation): boolean {
134
+ return getRelationKind(relation) === "line"
135
+ }
136
+
137
+ /**
138
+ * Check if a relation is a point relation.
139
+ */
140
+ export function isPointRelation(relation: OsmRelation): boolean {
141
+ return getRelationKind(relation) === "point"
142
+ }
143
+
144
+ /**
145
+ * Check if a relation is a super-relation (contains other relations).
146
+ */
147
+ export function isSuperRelation(relation: OsmRelation): boolean {
148
+ return getRelationKind(relation) === "super"
149
+ }
150
+
151
+ /**
152
+ * Check if a relation is a logical relation (non-geometric).
153
+ */
154
+ export function isLogicRelation(relation: OsmRelation): boolean {
155
+ return getRelationKind(relation) === "logic"
156
+ }
157
+
158
+ /**
159
+ * Build MultiLineString geometry from a line relation by connecting way members.
160
+ * Orders way members using their refs and handles role-based reversal.
161
+ * Returns an array of LineString coordinates (each LineString is an array of LonLat).
162
+ */
163
+ export function buildRelationLineStrings(
164
+ relation: OsmRelation,
165
+ getWay: (wayId: number) => OsmWay | null,
166
+ getNodeCoordinates: (nodeId: number) => LonLat | undefined,
167
+ ): LonLat[][] {
168
+ const lineStrings: LonLat[][] = []
169
+ const wayMembers = relation.members.filter((m) => m.type === "way")
170
+
171
+ if (wayMembers.length === 0) return lineStrings
172
+
173
+ // Group ways by role if roles are used
174
+ const roleGroups = new Map<string, OsmRelationMember[]>()
175
+ for (const member of wayMembers) {
176
+ const role = member.role?.toLowerCase() ?? ""
177
+ if (!roleGroups.has(role)) {
178
+ roleGroups.set(role, [])
179
+ }
180
+ roleGroups.get(role)!.push(member)
181
+ }
182
+
183
+ // If no roles or single role group, process all ways together
184
+ const groupsToProcess =
185
+ roleGroups.size === 1 || !relation.members.some((m) => m.role)
186
+ ? [wayMembers]
187
+ : Array.from(roleGroups.values())
188
+
189
+ for (const group of groupsToProcess) {
190
+ // Try to connect ways into continuous linestrings
191
+ const connected = connectWaysToLineStrings(
192
+ group,
193
+ getWay,
194
+ getNodeCoordinates,
195
+ )
196
+ lineStrings.push(...connected)
197
+ }
198
+
199
+ return lineStrings
200
+ }
201
+
202
+ /**
203
+ * Connect ways that share endpoints to form continuous LineStrings.
204
+ * Returns an array of LineStrings (each is an array of LonLat coordinates).
205
+ */
206
+ function connectWaysToLineStrings(
207
+ wayMembers: OsmRelationMember[],
208
+ getWay: (wayId: number) => OsmWay | null,
209
+ getNodeCoordinates: (nodeId: number) => LonLat | undefined,
210
+ ): LonLat[][] {
211
+ if (wayMembers.length === 0) return []
212
+
213
+ const lineStrings: LonLat[][] = []
214
+ const used = new Set<number>()
215
+ const wayMap = new Map<number, OsmWay>()
216
+
217
+ // Build map of way ID to way
218
+ for (const member of wayMembers) {
219
+ const way = getWay(member.ref)
220
+ if (way) {
221
+ wayMap.set(member.ref, way)
222
+ }
223
+ }
224
+
225
+ // Build linestrings by connecting ways
226
+ for (const member of wayMembers) {
227
+ if (used.has(member.ref)) continue
228
+ const startWay = wayMap.get(member.ref)
229
+ if (!startWay || startWay.refs.length < 2) continue
230
+
231
+ const coords: LonLat[] = []
232
+ const currentWay = startWay
233
+ used.add(member.ref)
234
+
235
+ // Get coordinates for the starting way
236
+ for (const nodeId of currentWay.refs) {
237
+ const coord = getNodeCoordinates(nodeId)
238
+ if (coord) coords.push(coord)
239
+ }
240
+
241
+ // Try to extend forward
242
+ while (true) {
243
+ let found = false
244
+ const lastCoord = coords[coords.length - 1]
245
+ if (!lastCoord) break
246
+
247
+ for (const nextMember of wayMembers) {
248
+ if (used.has(nextMember.ref)) continue
249
+ const nextWay = wayMap.get(nextMember.ref)
250
+ if (!nextWay || nextWay.refs.length < 2) continue
251
+
252
+ const nextStart = getNodeCoordinates(nextWay.refs[0]!)
253
+ const nextEnd = getNodeCoordinates(
254
+ nextWay.refs[nextWay.refs.length - 1]!,
255
+ )
256
+
257
+ if (!nextStart || !nextEnd) continue
258
+
259
+ // Check if next way connects to current end
260
+ if (lastCoord[0] === nextStart[0] && lastCoord[1] === nextStart[1]) {
261
+ // Connect normally
262
+ for (let i = 1; i < nextWay.refs.length; i++) {
263
+ const nodeId = nextWay.refs[i]
264
+ if (nodeId === undefined) continue
265
+ const coord = getNodeCoordinates(nodeId)
266
+ if (coord) coords.push(coord)
267
+ }
268
+ used.add(nextMember.ref)
269
+ found = true
270
+ break
271
+ }
272
+ if (lastCoord[0] === nextEnd[0] && lastCoord[1] === nextEnd[1]) {
273
+ // Need to reverse next way
274
+ for (let i = nextWay.refs.length - 2; i >= 0; i--) {
275
+ const nodeId = nextWay.refs[i]
276
+ if (nodeId === undefined) continue
277
+ const coord = getNodeCoordinates(nodeId)
278
+ if (coord) coords.push(coord)
279
+ }
280
+ used.add(nextMember.ref)
281
+ found = true
282
+ break
283
+ }
284
+ }
285
+
286
+ if (!found) break
287
+ }
288
+
289
+ // Try to extend backward
290
+ while (true) {
291
+ let found = false
292
+ const firstCoord = coords[0]
293
+ if (!firstCoord) break
294
+
295
+ for (const prevMember of wayMembers) {
296
+ if (used.has(prevMember.ref)) continue
297
+ const prevWay = wayMap.get(prevMember.ref)
298
+ if (!prevWay || prevWay.refs.length < 2) continue
299
+
300
+ const prevStart = getNodeCoordinates(prevWay.refs[0]!)
301
+ const prevEnd = getNodeCoordinates(
302
+ prevWay.refs[prevWay.refs.length - 1]!,
303
+ )
304
+
305
+ if (!prevStart || !prevEnd) continue
306
+
307
+ // Check if prev way connects to current start
308
+ if (firstCoord[0] === prevEnd[0] && firstCoord[1] === prevEnd[1]) {
309
+ // Connect normally (prepend)
310
+ const newCoords: LonLat[] = []
311
+ for (let i = 0; i < prevWay.refs.length - 1; i++) {
312
+ const nodeId = prevWay.refs[i]
313
+ if (nodeId === undefined) continue
314
+ const coord = getNodeCoordinates(nodeId)
315
+ if (coord) newCoords.push(coord)
316
+ }
317
+ coords.unshift(...newCoords)
318
+ used.add(prevMember.ref)
319
+ found = true
320
+ break
321
+ }
322
+ if (firstCoord[0] === prevStart[0] && firstCoord[1] === prevStart[1]) {
323
+ // Need to reverse prev way (prepend reversed)
324
+ const newCoords: LonLat[] = []
325
+ for (let i = prevWay.refs.length - 1; i > 0; i--) {
326
+ const nodeId = prevWay.refs[i]
327
+ if (nodeId === undefined) continue
328
+ const coord = getNodeCoordinates(nodeId)
329
+ if (coord) newCoords.push(coord)
330
+ }
331
+ coords.unshift(...newCoords)
332
+ used.add(prevMember.ref)
333
+ found = true
334
+ break
335
+ }
336
+ }
337
+
338
+ if (!found) break
339
+ }
340
+
341
+ if (coords.length >= 2) {
342
+ lineStrings.push(coords)
343
+ }
344
+ }
345
+
346
+ return lineStrings
347
+ }
348
+
349
+ /**
350
+ * Collect point coordinates from a point relation.
351
+ * Returns an array of LonLat coordinates from node members.
352
+ */
353
+ export function collectRelationPoints(
354
+ relation: OsmRelation,
355
+ getNodeCoordinates: (nodeId: number) => LonLat | undefined,
356
+ ): LonLat[] {
357
+ const points: LonLat[] = []
358
+ for (const member of relation.members) {
359
+ if (member.type === "node") {
360
+ const coord = getNodeCoordinates(member.ref)
361
+ if (coord) {
362
+ points.push(coord)
363
+ }
364
+ }
365
+ }
366
+ return points
367
+ }
368
+
369
+ /**
370
+ * Resolve nested relation members, flattening the hierarchy with cycle detection.
371
+ * Returns all nodes, ways, and relations that are members (directly or indirectly).
372
+ * @param relation - The relation to resolve
373
+ * @param getRelation - Function to get a relation by ID
374
+ * @param maxDepth - Maximum recursion depth (default: 10)
375
+ * @param visited - Set of relation IDs already visited (for cycle detection)
376
+ */
377
+ export function resolveRelationMembers(
378
+ relation: OsmRelation,
379
+ getRelation: (relationId: number) => OsmRelation | null,
380
+ maxDepth = 10,
381
+ visited = new Set<number>(),
382
+ ): {
383
+ nodes: number[]
384
+ ways: number[]
385
+ relations: number[]
386
+ } {
387
+ const nodes = new Set<number>()
388
+ const ways = new Set<number>()
389
+ const relations = new Set<number>()
390
+
391
+ // Cycle detection or max depth reached
392
+ if (visited.has(relation.id) || maxDepth <= 0) {
393
+ return { nodes: [], ways: [], relations: [] }
394
+ }
395
+
396
+ visited.add(relation.id)
397
+
398
+ for (const member of relation.members) {
399
+ if (member.type === "node") {
400
+ if (!nodes.has(member.ref)) {
401
+ nodes.add(member.ref)
402
+ }
403
+ } else if (member.type === "way") {
404
+ if (!ways.has(member.ref)) {
405
+ ways.add(member.ref)
406
+ }
407
+ } else if (member.type === "relation") {
408
+ if (!relations.has(member.ref)) {
409
+ relations.add(member.ref)
410
+
411
+ // Recursively resolve nested relation
412
+ const nestedRelation = getRelation(member.ref)
413
+ if (nestedRelation) {
414
+ const nested = resolveRelationMembers(
415
+ nestedRelation,
416
+ getRelation,
417
+ maxDepth - 1,
418
+ visited,
419
+ )
420
+ // Merge nested results
421
+ for (const nodeId of nested.nodes) {
422
+ if (!nodes.has(nodeId)) {
423
+ nodes.add(nodeId)
424
+ }
425
+ }
426
+ for (const wayId of nested.ways) {
427
+ if (!ways.has(wayId)) {
428
+ ways.add(wayId)
429
+ }
430
+ }
431
+ for (const relId of nested.relations) {
432
+ if (!relations.has(relId)) {
433
+ relations.add(relId)
434
+ }
435
+ }
436
+ }
437
+ }
438
+ }
439
+ }
440
+
441
+ return {
442
+ nodes: Array.from(nodes),
443
+ ways: Array.from(ways),
444
+ relations: Array.from(relations),
445
+ }
446
+ }