diff --git a/Sources/Geohash/BoundingBox.swift b/Sources/Geohash/BoundingBox.swift index 360b3dc..12b97a0 100644 --- a/Sources/Geohash/BoundingBox.swift +++ b/Sources/Geohash/BoundingBox.swift @@ -7,10 +7,16 @@ // public class BoundingBox { - let min: Location - let max: Location + public let min: Location + public let max: Location + + public init(min: Location, max: Location) throws { + guard + min.longitude <= max.longitude && + min.latitude <= max.latitude else { + throw GeohashError.invalidArguments + } - public init(min: Location, max: Location) { self.min = min self.max = max } @@ -21,4 +27,16 @@ public class BoundingBox { latitude: 0.5 * (self.min.latitude + self.max.latitude) ) } + + /// - todo: support wrap-around at the 180th meridian + public func intersects(_ other: BoundingBox) -> Bool { + if + max.longitude < other.min.longitude || + max.latitude < other.min.latitude || + min.longitude > other.max.longitude || + min.latitude > other.max.latitude { + return false + } + return true + } } diff --git a/Sources/Geohash/Geohash.swift b/Sources/Geohash/Geohash.swift new file mode 100644 index 0000000..6821d78 --- /dev/null +++ b/Sources/Geohash/Geohash.swift @@ -0,0 +1,13 @@ +// +// Geohash.swift +// Geohash +// +// Created by michael groble on 1/8/17. +// +// + +public enum GeohashError : Error { + case invalidPrecision + case invalidLocation + case invalidArguments +} diff --git a/Sources/Geohash/GeohashBits.swift b/Sources/Geohash/GeohashBits.swift index 17d3af1..c736288 100644 --- a/Sources/Geohash/GeohashBits.swift +++ b/Sources/Geohash/GeohashBits.swift @@ -8,11 +8,6 @@ import Foundation -public enum GeohashError : Error { - case invalidPrecision - case invalidLocation -} - public struct GeohashBits { public let bits : UInt64 public let precision : UInt8 @@ -89,7 +84,7 @@ public struct GeohashBits { latPrecision -= 1; } - return BoundingBox( + return try! BoundingBox( min: Location(longitude: unscaledBits(lonBits, range: longitudeRange, precision: self.precision), latitude: unscaledBits(latBits, range: latitudeRange, precision: latPrecision)), max: Location(longitude: unscaledBits(lonBits + 1, range: longitudeRange, precision: self.precision), diff --git a/Tests/GeohashTests/BoundingBoxTests.swift b/Tests/GeohashTests/BoundingBoxTests.swift new file mode 100644 index 0000000..82deeca --- /dev/null +++ b/Tests/GeohashTests/BoundingBoxTests.swift @@ -0,0 +1,47 @@ +// +// BoundingBoxTests.swift +// Geohash +// +// Created by michael groble on 1/8/17. +// +// + +import XCTest +@testable import Geohash + +class BoundingBoxTests: XCTestCase { + + var subject: BoundingBox! + + override func setUp() { + super.setUp() + + self.subject = try! BoundingBox( + min: Location(longitude: -10, latitude: -10), + max: Location(longitude: +10, latitude: +10) + ) + } + + func testIntersectsIntersecting() throws { + let intersecting = try BoundingBox( + min: Location(longitude: -20, latitude: -20), + max: Location(longitude: -9, latitude: -9) + ) + XCTAssertTrue(subject.intersects(intersecting)) + } + + func testIntersectsNonIntersecting() throws { + let intersecting = try BoundingBox( + min: Location(longitude: -20, latitude: -20), + max: Location(longitude: -9, latitude: -10.1) + ) + XCTAssertFalse(subject.intersects(intersecting)) + } +} + +extension BoundingBoxTests { + static var allTests = [ + ("testIntersectsIntersecting", testIntersectsIntersecting), + ("testIntersectsNonIntersecting", testIntersectsNonIntersecting), + ] +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 24078ae..c8eff9c 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -2,6 +2,7 @@ import XCTest @testable import GeohashTests XCTMain([ + testCase(BoundingBoxTests.allTests), testCase(GeohashBitsTests.allTests), testCase(LocationTests.allTests), ])