-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from michael-groble/add-geohash-iterator
add geohash iterator
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// GeohashIterator.swift | ||
// Geohash | ||
// | ||
// Created by michael groble on 1/12/17. | ||
// | ||
// | ||
|
||
public class GeohashIterator : IteratorProtocol, Sequence { | ||
|
||
let bounds: BoundingBox | ||
var latBaseline: GeohashBits | ||
var current: GeohashBits? | ||
|
||
public init(bounds: BoundingBox, bitPrecision: UInt8) throws { | ||
self.bounds = bounds | ||
self.latBaseline = try GeohashBits(location: bounds.min, bitPrecision: bitPrecision) | ||
self.current = self.latBaseline | ||
} | ||
|
||
public func next() -> GeohashBits? { | ||
defer { advanceCurrent() } | ||
return current | ||
} | ||
|
||
private func advanceCurrent() { | ||
// advance eastward until we are out of the bounds then advance northward | ||
if var bits = self.current { | ||
bits = bits.neighbor(.east) | ||
if bounds.intersects(bits.boundingBox()) { | ||
self.current = bits | ||
} | ||
else { | ||
self.latBaseline = latBaseline.neighbor(.north) | ||
self.current = bounds.intersects(latBaseline.boundingBox()) ? latBaseline : nil | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// GeohashIteratorTests.swift | ||
// Geohash | ||
// | ||
// Created by michael groble on 1/12/17. | ||
// | ||
// | ||
|
||
import XCTest | ||
@testable import Geohash | ||
|
||
class GeohashIteratorTests: XCTestCase { | ||
|
||
var bounds: BoundingBox! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
self.bounds = try! BoundingBox( | ||
min: Location(longitude: 0.09991, latitude: 51.49996), | ||
max: Location(longitude: 0.10059, latitude: 51.50028) | ||
) | ||
} | ||
|
||
|
||
func testIterateLevel8() throws { | ||
let subject = try GeohashIterator(bounds: bounds, bitPrecision: 20) | ||
XCTAssertEqual(subject.next()!.hash(), "u10hfr2c") | ||
XCTAssertEqual(subject.next()!.hash(), "u10hfr31") | ||
XCTAssertEqual(subject.next()!.hash(), "u10hfr2f") | ||
XCTAssertEqual(subject.next()!.hash(), "u10hfr34") | ||
XCTAssertNil(subject.next()) | ||
} | ||
} | ||
|
||
extension GeohashIteratorTests { | ||
static var allTests = [ | ||
("testIterateLevel8", testIterateLevel8), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters