proxpero / Geodesy

A Swift implementation of the geohash algorithm.
MIT License
23 stars 10 forks source link

Returned "Neighbors" incorrect for Precision of 4 #12

Open G-Steve-E opened 4 years ago

G-Steve-E commented 4 years ago

When I use Geodesy as shown below:

< let CurrentGeoHash = self.currentLocation.geohash(precision: 4) let CurrentNeighbors = self.currentLocation.neighbors(precision: 4) print("Geohash: ", CurrentGeoHash, " Geohash Neighbors: ", CurrentNeighbors) >

I get the following geohash for the current location and the array for the neighbors: Geohash: 9tbr
Geohash Neighbors: ["9w02", "9w02", "9tbr", "9tbq", "9tbq", "9tbq", "9tbr", "9w02"]

I believe the returned Geohash Neighbors for "9tbr" should be [N, NE, E, SE, S, SW, W, NW]: ["9w02", "9w08", 9tbx", 9tbw", 9tbq", 9tbn", 9tbp", 9w00"]

nidegen commented 4 years ago

i experience the same issue for precision 6. Seems to be a fundamental issue

nidegen commented 4 years ago

Well I guess it is not really supported for locations. Looking at the Unit Tests, it seems only to be supported for regions (which makes sense actually).

My workaround:

extension CLLocation {
  func getNeighboringBuckets(precision: Precision = .sixHundredTenMeters) -> [String] {
    let region = Region(hash: self.geohash(precision: precision))!
    return region.neighbors().map { $0.hash }
  }
}
G-Steve-E commented 4 years ago

Based on documentation I would have expected the "neighbors" function to compute the geohash of the current location (i.e. current region) at the specified precision, then determine the neighbors of the that region, again at the specified precision.

G-Steve-E commented 4 years ago

I have updated the Geodesy code in my app as Nicolas provided above and have verified that this does work for various precisions. Pod needs to be updated and also add the same for CLLocationCoordinate2D.