File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed
Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ //
2+ // GeohashIterator.swift
3+ // Geohash
4+ //
5+ // Created by michael groble on 1/12/17.
6+ //
7+ //
8+
9+ public class GeohashIterator : IteratorProtocol , Sequence {
10+
11+ let bounds : BoundingBox
12+ var latBaseline : GeohashBits
13+ var current : GeohashBits ?
14+
15+ public init ( bounds: BoundingBox , bitPrecision: UInt8 ) throws {
16+ self . bounds = bounds
17+ self . latBaseline = try GeohashBits ( location: bounds. min, bitPrecision: bitPrecision)
18+ self . current = self . latBaseline
19+ }
20+
21+ public func next( ) -> GeohashBits ? {
22+ defer { advanceCurrent ( ) }
23+ return current
24+ }
25+
26+ private func advanceCurrent( ) {
27+ // advance eastward until we are out of the bounds then advance northward
28+ if var bits = self . current {
29+ bits = bits. neighbor ( . east)
30+ if bounds. intersects ( bits. boundingBox ( ) ) {
31+ self . current = bits
32+ }
33+ else {
34+ self . latBaseline = latBaseline. neighbor ( . north)
35+ self . current = bounds. intersects ( latBaseline. boundingBox ( ) ) ? latBaseline : nil
36+ }
37+ }
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ //
2+ // GeohashIteratorTests.swift
3+ // Geohash
4+ //
5+ // Created by michael groble on 1/12/17.
6+ //
7+ //
8+
9+ import XCTest
10+ @testable import Geohash
11+
12+ class GeohashIteratorTests : XCTestCase {
13+
14+ var bounds : BoundingBox !
15+
16+ override func setUp( ) {
17+ super. setUp ( )
18+
19+ self . bounds = try ! BoundingBox (
20+ min: Location ( longitude: 0.09991 , latitude: 51.49996 ) ,
21+ max: Location ( longitude: 0.10059 , latitude: 51.50028 )
22+ )
23+ }
24+
25+
26+ func testIterateLevel8( ) throws {
27+ let subject = try GeohashIterator ( bounds: bounds, bitPrecision: 20 )
28+ XCTAssertEqual ( subject. next ( ) !. hash ( ) , " u10hfr2c " )
29+ XCTAssertEqual ( subject. next ( ) !. hash ( ) , " u10hfr31 " )
30+ XCTAssertEqual ( subject. next ( ) !. hash ( ) , " u10hfr2f " )
31+ XCTAssertEqual ( subject. next ( ) !. hash ( ) , " u10hfr34 " )
32+ XCTAssertNil ( subject. next ( ) )
33+ }
34+ }
35+
36+ extension GeohashIteratorTests {
37+ static var allTests = [
38+ ( " testIterateLevel8 " , testIterateLevel8) ,
39+ ]
40+ }
Original file line number Diff line number Diff line change @@ -4,5 +4,6 @@ import XCTest
44XCTMain ( [
55 testCase ( BoundingBoxTests . allTests) ,
66 testCase ( GeohashBitsTests . allTests) ,
7+ testCase ( GeohashIteratorTests . allTests) ,
78 testCase ( LocationTests . allTests) ,
89] )
You can’t perform that action at this time.
0 commit comments