@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.
- package/CHANGELOG.md +33 -0
- package/README.md +49 -19
- package/dist/assert.d.ts +24 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +28 -0
- package/dist/assert.js.map +1 -0
- package/dist/bbox-intersects.d.ts +15 -0
- package/dist/bbox-intersects.d.ts.map +1 -0
- package/dist/bbox-intersects.js +24 -0
- package/dist/bbox-intersects.js.map +1 -0
- package/dist/bytes-to-stream.d.ts +18 -0
- package/dist/bytes-to-stream.d.ts.map +1 -0
- package/dist/bytes-to-stream.js +25 -0
- package/dist/bytes-to-stream.js.map +1 -0
- package/dist/color.d.ts +4 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +33 -0
- package/dist/color.js.map +1 -0
- package/dist/concat-bytes.d.ts +5 -0
- package/dist/concat-bytes.d.ts.map +1 -0
- package/dist/concat-bytes.js +14 -0
- package/dist/concat-bytes.js.map +1 -0
- package/dist/coordinates.d.ts +28 -0
- package/dist/coordinates.d.ts.map +1 -0
- package/dist/coordinates.js +38 -0
- package/dist/coordinates.js.map +1 -0
- package/dist/haversine-distance.d.ts +16 -0
- package/dist/haversine-distance.d.ts.map +1 -0
- package/dist/haversine-distance.js +26 -0
- package/dist/haversine-distance.js.map +1 -0
- package/dist/lineclip.d.ts +2 -0
- package/dist/lineclip.d.ts.map +1 -0
- package/dist/lineclip.js +3 -0
- package/dist/lineclip.js.map +1 -0
- package/dist/progress.d.ts +42 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +53 -0
- package/dist/progress.js.map +1 -0
- package/dist/relation-kind.d.ts +69 -0
- package/dist/relation-kind.d.ts.map +1 -0
- package/dist/relation-kind.js +375 -0
- package/dist/relation-kind.js.map +1 -0
- package/dist/relation-multipolygon.d.ts +43 -0
- package/dist/relation-multipolygon.d.ts.map +1 -0
- package/dist/relation-multipolygon.js +195 -0
- package/dist/relation-multipolygon.js.map +1 -0
- package/dist/src/assert.d.ts +20 -1
- package/dist/src/assert.d.ts.map +1 -1
- package/dist/src/assert.js +20 -1
- package/dist/src/assert.js.map +1 -1
- package/dist/src/bbox-intersects.d.ts +15 -0
- package/dist/src/bbox-intersects.d.ts.map +1 -0
- package/dist/src/bbox-intersects.js +24 -0
- package/dist/src/bbox-intersects.js.map +1 -0
- package/dist/src/bytes-to-stream.d.ts +17 -1
- package/dist/src/bytes-to-stream.d.ts.map +1 -1
- package/dist/src/bytes-to-stream.js +16 -0
- package/dist/src/bytes-to-stream.js.map +1 -1
- package/dist/src/color.d.ts +4 -0
- package/dist/src/color.d.ts.map +1 -0
- package/dist/src/color.js +33 -0
- package/dist/src/color.js.map +1 -0
- package/dist/src/coordinates.d.ts +28 -0
- package/dist/src/coordinates.d.ts.map +1 -0
- package/dist/src/coordinates.js +38 -0
- package/dist/src/coordinates.js.map +1 -0
- package/dist/src/haversine-distance.d.ts +9 -1
- package/dist/src/haversine-distance.d.ts.map +1 -1
- package/dist/src/haversine-distance.js +9 -1
- package/dist/src/haversine-distance.js.map +1 -1
- package/dist/src/progress.d.ts +42 -0
- package/dist/src/progress.d.ts.map +1 -0
- package/dist/src/progress.js +53 -0
- package/dist/src/progress.js.map +1 -0
- package/dist/src/relation-kind.d.ts +69 -0
- package/dist/src/relation-kind.d.ts.map +1 -0
- package/dist/src/relation-kind.js +375 -0
- package/dist/src/relation-kind.js.map +1 -0
- package/dist/src/relation-multipolygon.d.ts +43 -0
- package/dist/src/relation-multipolygon.d.ts.map +1 -0
- package/dist/src/relation-multipolygon.js +195 -0
- package/dist/src/relation-multipolygon.js.map +1 -0
- package/dist/src/stream-to-bytes.d.ts +16 -0
- package/dist/src/stream-to-bytes.d.ts.map +1 -1
- package/dist/src/stream-to-bytes.js +16 -0
- package/dist/src/stream-to-bytes.js.map +1 -1
- package/dist/src/test/fixtures.d.ts +1 -1
- package/dist/src/test/fixtures.d.ts.map +1 -1
- package/dist/src/test/fixtures.js +16 -8
- package/dist/src/test/fixtures.js.map +1 -1
- package/dist/src/throttle.d.ts +25 -0
- package/dist/src/throttle.d.ts.map +1 -0
- package/dist/src/throttle.js +34 -0
- package/dist/src/throttle.js.map +1 -0
- package/dist/src/tile.d.ts +34 -0
- package/dist/src/tile.d.ts.map +1 -0
- package/dist/src/tile.js +72 -0
- package/dist/src/tile.js.map +1 -0
- package/dist/src/transform-bytes.d.ts +22 -0
- package/dist/src/transform-bytes.d.ts.map +1 -1
- package/dist/src/transform-bytes.js +22 -0
- package/dist/src/transform-bytes.js.map +1 -1
- package/dist/src/types.d.ts +76 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +8 -0
- package/dist/src/types.js.map +1 -1
- package/dist/src/utils.d.ts +30 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +70 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/src/way-is-area.d.ts +24 -0
- package/dist/src/way-is-area.d.ts.map +1 -0
- package/dist/src/way-is-area.js +104 -0
- package/dist/src/way-is-area.js.map +1 -0
- package/dist/src/zigzag.d.ts +33 -0
- package/dist/src/zigzag.d.ts.map +1 -0
- package/dist/src/zigzag.js +40 -0
- package/dist/src/zigzag.js.map +1 -0
- package/dist/stream-to-bytes.d.ts +18 -0
- package/dist/stream-to-bytes.d.ts.map +1 -0
- package/dist/stream-to-bytes.js +30 -0
- package/dist/stream-to-bytes.js.map +1 -0
- package/dist/test/fixtures.d.ts +36 -0
- package/dist/test/fixtures.d.ts.map +1 -0
- package/dist/test/fixtures.js +175 -0
- package/dist/test/fixtures.js.map +1 -0
- package/dist/test/haversine-distance.test.js +2 -2
- package/dist/test/haversine-distance.test.js.map +1 -1
- package/dist/test/relation-kind.test.d.ts +2 -0
- package/dist/test/relation-kind.test.d.ts.map +1 -0
- package/dist/test/relation-kind.test.js +367 -0
- package/dist/test/relation-kind.test.js.map +1 -0
- package/dist/test/relation-multipolygon.test.d.ts +2 -0
- package/dist/test/relation-multipolygon.test.d.ts.map +1 -0
- package/dist/test/relation-multipolygon.test.js +237 -0
- package/dist/test/relation-multipolygon.test.js.map +1 -0
- package/dist/test/utils.test.d.ts +2 -0
- package/dist/test/utils.test.d.ts.map +1 -0
- package/dist/test/utils.test.js +76 -0
- package/dist/test/utils.test.js.map +1 -0
- package/dist/test/way-is-area.test.d.ts +2 -0
- package/dist/test/way-is-area.test.d.ts.map +1 -0
- package/dist/test/way-is-area.test.js +31 -0
- package/dist/test/way-is-area.test.js.map +1 -0
- package/dist/throttle.d.ts +25 -0
- package/dist/throttle.d.ts.map +1 -0
- package/dist/throttle.js +34 -0
- package/dist/throttle.js.map +1 -0
- package/dist/tile.d.ts +34 -0
- package/dist/tile.d.ts.map +1 -0
- package/dist/tile.js +72 -0
- package/dist/tile.js.map +1 -0
- package/dist/transform-bytes.d.ts +24 -0
- package/dist/transform-bytes.d.ts.map +1 -0
- package/dist/transform-bytes.js +28 -0
- package/dist/transform-bytes.js.map +1 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +70 -0
- package/dist/utils.js.map +1 -0
- package/dist/way-is-area.d.ts +24 -0
- package/dist/way-is-area.d.ts.map +1 -0
- package/dist/way-is-area.js +104 -0
- package/dist/way-is-area.js.map +1 -0
- package/dist/zigzag.d.ts +33 -0
- package/dist/zigzag.d.ts.map +1 -0
- package/dist/zigzag.js +40 -0
- package/dist/zigzag.js.map +1 -0
- package/package.json +11 -10
- package/src/assert.ts +21 -1
- package/src/bbox-intersects.ts +30 -0
- package/src/bytes-to-stream.ts +17 -0
- package/src/color.ts +37 -0
- package/src/coordinates.ts +45 -0
- package/src/haversine-distance.ts +10 -1
- package/src/progress.ts +74 -0
- package/src/relation-kind.ts +446 -0
- package/src/relation-multipolygon.ts +225 -0
- package/src/stream-to-bytes.ts +17 -0
- package/src/test/fixtures.ts +16 -12
- package/src/throttle.ts +37 -0
- package/src/tile.ts +89 -0
- package/src/transform-bytes.ts +23 -0
- package/src/types.ts +93 -1
- package/src/utils.ts +79 -0
- package/src/way-is-area.ts +107 -0
- package/src/zigzag.ts +42 -0
- package/test/haversine-distance.test.ts +2 -2
- package/test/relation-kind.test.ts +426 -0
- package/test/relation-multipolygon.test.ts +265 -0
- package/test/utils.test.ts +103 -0
- package/test/way-is-area.test.ts +42 -0
- package/tsconfig/test.json +1 -0
- package/tsconfig.build.json +5 -0
- package/dist/src/spherical-mercator.d.ts +0 -15
- package/dist/src/spherical-mercator.d.ts.map +0 -1
- package/dist/src/spherical-mercator.js +0 -35
- package/dist/src/spherical-mercator.js.map +0 -1
- package/dist/src/spherical-mercator.test.d.ts +0 -2
- package/dist/src/spherical-mercator.test.d.ts.map +0 -1
- package/dist/src/spherical-mercator.test.js +0 -25
- package/dist/src/spherical-mercator.test.js.map +0 -1
- package/src/spherical-mercator.test.ts +0 -42
- package/src/spherical-mercator.ts +0 -42
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
package/src/progress.ts
ADDED
|
@@ -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
|
+
}
|