Skip to content

Commit

Permalink
Merge pull request #4 from michael-groble/bbox-intersection
Browse files Browse the repository at this point in the history
add bounding box intersection test
  • Loading branch information
michael-groble authored Jan 8, 2017
2 parents 3d8eea3 + 170cb52 commit ac0b2e1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
24 changes: 21 additions & 3 deletions Sources/Geohash/BoundingBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
13 changes: 13 additions & 0 deletions Sources/Geohash/Geohash.swift
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 1 addition & 6 deletions Sources/Geohash/GeohashBits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
47 changes: 47 additions & 0 deletions Tests/GeohashTests/BoundingBoxTests.swift
Original file line number Diff line number Diff line change
@@ -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),
]
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XCTest
@testable import GeohashTests

XCTMain([
testCase(BoundingBoxTests.allTests),
testCase(GeohashBitsTests.allTests),
testCase(LocationTests.allTests),
])

0 comments on commit ac0b2e1

Please sign in to comment.