Skip to content

Commit 6af53ab

Browse files
committed
✨ Add altitude to Turf
1 parent 3b93496 commit 6af53ab

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Sources/Turf/Boundable.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ extension Coordinate2D: Boundable {
2424

2525
}
2626

27+
extension Coordinate3D: Boundable {
28+
29+
public var bbox: BoundingBox3D {
30+
BoundingBox3D(twoDimensions.bbox, lowAltitude: altitude, zHeight: .zero)
31+
}
32+
33+
}
34+
2735
extension Line2D: Boundable {
2836

2937
public var bbox: BoundingBox2D {
@@ -32,12 +40,26 @@ extension Line2D: Boundable {
3240

3341
}
3442

43+
extension Line3D: Boundable {
44+
45+
public var bbox: BoundingBox3D {
46+
Turf.bbox(for: [start, end])!
47+
}
48+
49+
}
50+
3551
extension BoundingBox2D: Boundable {
3652

3753
public var bbox: BoundingBox2D { self }
3854

3955
}
4056

57+
extension BoundingBox3D: Boundable {
58+
59+
public var bbox: BoundingBox3D { self }
60+
61+
}
62+
4163
// Extension of protocol 'Collection' cannot have an inheritance clause
4264
//extension Collection: Boundable where Element: Boundable {
4365
//

Sources/Turf/Turf.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public func minimumBBox(for coords: [Coordinate2D]) -> BoundingBox2D? {
4040
}
4141

4242
/// Returns the [bounding box](https://en.wikipedia.org/wiki/Minimum_bounding_box) enclosing the points.
43-
public func bbox(for coords: [Coordinate2D]) -> BoundingBox2D? {
43+
public func bbox<C: Collection>(for coords: C) -> BoundingBox2D? where C.Element == Coordinate2D {
4444
guard let (south, north) = coords.map(\.latitude).minAndMax(),
4545
let (west, east) = coords.map(\.longitude).minAndMax()
4646
else { return nil }
@@ -51,9 +51,17 @@ public func bbox(for coords: [Coordinate2D]) -> BoundingBox2D? {
5151
)
5252
}
5353

54-
/// Returns the [bounding box](https://en.wikipedia.org/wiki/Minimum_bounding_box) enclosing all elements.
55-
public func bbox<C: Collection & Boundable>(for boundables: C) -> C.BoundingBox? {
56-
return boundables.bbox
54+
/// Returns the [bounding box](https://en.wikipedia.org/wiki/Minimum_bounding_box) enclosing the points.
55+
public func bbox<C: Collection>(for coords: C) -> BoundingBox3D? where C.Element == Coordinate3D {
56+
guard let (south, north) = coords.map(\.latitude).minAndMax(),
57+
let (west, east) = coords.map(\.longitude).minAndMax(),
58+
let (low, high) = coords.map(\.altitude).minAndMax()
59+
else { return nil }
60+
61+
return BoundingBox3D(
62+
southWestLow: Coordinate3D(latitude: south, longitude: west, altitude: low),
63+
northEastHigh: Coordinate3D(latitude: north, longitude: east, altitude: high)
64+
)
5765
}
5866

5967
/// Returns the [bounding box](https://en.wikipedia.org/wiki/Minimum_bounding_box) enclosing all elements.

0 commit comments

Comments
 (0)