Skip to content

Commit

Permalink
✨ Encode bounding boxes in GeoJSON
Browse files Browse the repository at this point in the history
Also fix bounding boxes computation
  • Loading branch information
RemiBardon committed Feb 10, 2022
1 parent da78ab2 commit 990a409
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Sources/GeoJSON/GeoJSON+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension LinearRingCoordinates {

fileprivate enum SingleGeometryCodingKeys: String, CodingKey {
case geoJSONType = "type"
case coordinates
case coordinates, bbox
}

extension SingleGeometry {
Expand All @@ -171,6 +171,7 @@ extension SingleGeometry {

try container.encode(Self.geoJSONType, forKey: .geoJSONType)
try container.encode(self.coordinates, forKey: .coordinates)
try container.encode(self.bbox, forKey: .bbox)
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/GeoJSON/Helpers/GeoJSON+Boundable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Turf
extension NonEmpty: Boundable where Collection: Hashable, Element: Boundable {

public var _bbox: Element.BoundingBox {
self.reduce(.zero, { $0.union($1.bbox) })
self.reduce(nil, { $0.union($1.bbox) }) ?? .zero
}

}
8 changes: 8 additions & 0 deletions Sources/GeoModels/BoundingBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ public protocol BoundingBox: Hashable {
func union(_ other: Self) -> Self

}

extension Optional where Wrapped: BoundingBox {

public func union(_ bbox: Wrapped) -> Wrapped {
self?.union(bbox) ?? bbox
}

}
6 changes: 3 additions & 3 deletions Sources/Turf/Boundable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ extension BoundingBox3D: Boundable {
//extension Collection: Boundable where Element: Boundable {
//
// public var _bbox: Element.BoundingBox {
// self.reduce(.zero, { $0.union($1.bbox) })
// self.reduce(nil, { $0.union($1.bbox) }) ?? .zero
// }
//
//}

extension Array: Boundable where Element: Boundable {

public var _bbox: Element.BoundingBox {
self.reduce(.zero, { $0.union($1.bbox) })
self.reduce(nil, { $0.union($1.bbox) }) ?? .zero
}

}

extension Set: Boundable where Element: Boundable {

public var _bbox: Element.BoundingBox {
self.reduce(.zero, { $0.union($1.bbox) })
self.reduce(nil, { $0.union($1.bbox) }) ?? .zero
}

}
2 changes: 1 addition & 1 deletion Sources/Turf/Turf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public func bbox<C: Collection>(for coords: C) -> BoundingBox3D? where C.Element
public func bbox<T: Boundable, C: Collection>(for boundables: C) -> T.BoundingBox? where C.Element == T {
guard !boundables.isEmpty else { return nil }

return boundables.reduce(.zero, { $0.union($1.bbox) })
return boundables.reduce(nil, { $0.union($1.bbox) }) ?? .zero
}

/// Returns the absolute center of a polygon.
Expand Down
24 changes: 16 additions & 8 deletions Tests/GeoJSONTests/GeoJSON+EncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ final class GeoJSONEncodableTests: XCTestCase {
XCTAssertEqual(string, [
"{",
"\"type\":\"Point\",",
"\"coordinates\":[-1.55366,47.21881]",
"\"coordinates\":[-1.55366,47.21881],",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"}",
].joined())
}
Expand All @@ -41,7 +42,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"\"type\":\"MultiPoint\",",
"\"coordinates\":[",
"[-1.55366,47.21881]",
"]",
"],",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"}",
].joined())
}
Expand All @@ -57,7 +59,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"\"coordinates\":[",
"[-1.55366,47.21881],",
"[2.3529,48.85719]",
"]",
"],",
"\"bbox\":[-1.55366,47.21881,2.3529,48.85719]",
"}",
].joined())
}
Expand All @@ -79,7 +82,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"[2.3529,48.85719],",
"[5.36468,43.29868]",
"]",
"]",
"],",
"\"bbox\":[-1.55366,43.29868,5.36468,48.85719]",
"}",
].joined())
}
Expand All @@ -102,7 +106,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"[2.3529,48.85719],",
"[-1.55366,47.21881]",
"]",
"]",
"],",
"\"bbox\":[-1.55366,43.29868,5.36468,48.85719]",
"}",
].joined())
}
Expand Down Expand Up @@ -133,7 +138,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"[-1.55366,47.21881]",
"]",
"]",
"]",
"],",
"\"bbox\":[-1.55366,43.29868,5.36468,48.85719]",
"}",
].joined())
}
Expand All @@ -155,7 +161,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"\"type\":\"Feature\",",
"\"geometry\":{",
"\"type\":\"Point\",",
"\"coordinates\":[-1.55366,47.21881]",
"\"coordinates\":[-1.55366,47.21881],",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"},",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"}",
Expand Down Expand Up @@ -183,7 +190,8 @@ final class GeoJSONEncodableTests: XCTestCase {
"\"type\":\"Feature\",",
"\"geometry\":{",
"\"type\":\"Point\",",
"\"coordinates\":[-1.55366,47.21881]",
"\"coordinates\":[-1.55366,47.21881],",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"},",
"\"bbox\":[-1.55366,47.21881,-1.55366,47.21881]",
"}",
Expand Down

0 comments on commit 990a409

Please sign in to comment.