proxpero / Geodesy

A Swift implementation of the geohash algorithm.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Geodesy

MIT CocoaPods Swift

A Swift implementation of the geohash algorithm.

Geodesy comes from the ancient Greek word γεωδαισία which means dividing the earth. This is basically what the geohash algorithm does. The hash is just a string of characters, the first divides the earth into 32 separate regions, measured precisely along lines of latitude and longitude. Then next character subdivides a region further into 32 subsections. On and on it goes, each added character more precisely specifying a location on the globe. Nice!

The beauty of the algorithm really shines when you want to filter a list of locations by proximity to a given location. Instead of running some complicated trigonometry on all the points, you can simply compare geohashes. The nearest points will be the ones who share the longest prefix with the given point's geohash.

let currentGeohash = user.location.geohash()
let nearby = db.hotspots.where("geohash", beginsWith: currentGeohash.prefix(8))

Voilà. A hash length 8 characters long defines a square roughly 38 meters per side.

Usage

Geodesy is designed to be convenient to use with CoreLocation. There are a number of useful properties added in extensions on CLLocation and CLLocationCoordinate2D.

let location: CLLocation = manager.currentLocation
let geohash = location.geohash(precision: 8) // An eight-character geohash of type String
let neighbors = location.neighbors(precision: 8) // An array of nine eight-character strings representing the eight regions surrounding the original region.

Neighbors?

To handle cases where a location sits near the border of its enclosing region, so that nearby locations potentially are just in the next region over, it is easy to include the eight neighboring regions in addition to actual one.

About

A Swift implementation of the geohash algorithm.

License:MIT License


Languages

Language:Swift 95.5%Language:Ruby 2.9%Language:Objective-C 1.6%