Skip to content

ualch9/GeohashKit

Repository files navigation

GeohashKit

Swift

GeohashKit is a native Swift implementation of the Geohash hashing algorithm. Supporting encode, decode and neighbor search.

The original Swift (v1) implementation is from maximveksler/GeohashKit. The v2 implementation uses a data structure to define the Geohash as a rectangular cell, rather than just a string.

Platforms

  • iOS, macOS, watchOS, tvOS
  • Ubuntu

API (v2)

A Geohash is a data structure, representing a geohash as a rectangular cell.

Initialize with coordinates ("Encode")

let geohash = Geohash(coordinates: (42.6, -5.6), precision: 5)
print(geohash.geohash)      // "ezs42"

Initialize with existing geohash ("Decode")

let geohash = Geohash(geohash: "ezs42")
print(geohash.coordinates)  // (latitude: 42.60498046875, longitude: -5.60302734375)

Neighbors

let neighbors = Geohash(geohash: "u000").neighbors  // Returns an array of neighbor geohash cells.
print(neighbors.north)                              // Get the north (top) cell.
print(geohash.neighbors.all.map { $0.geohash })     // ["u001", "u003", "u002", "spbr", "spbp", "ezzz", "gbpb", "gbpc"]

MapKit

let geohash = Geohash(geohash: "ezs42")
let region: MKCoordinateRegion = geohash.region

Re: Precision

I purposely left out a precision enum. I found that explaining the approximate size of the cell at a given precision was confusing, it is difficult to explain a size without using numbers (case 2500km is not valid Swift). Geohashes are rectangular Mercator cells with true size dependent on the latitude, so defining a case twentyFiveHundredKilometers is still not necessarily true.

v1 API (maximveksler/GeohashKit compatible) To use the maximveksler-compatible API, checkout exactly `1.0`:
.package(url: "https://github.com/ualch9/geohashkit.git", .exact("1.0"))

Encode

Geohash.encode(latitude: 42.6, longitude: -5.6) // "ezs42"

Specify desired precision

Geohash.encode(latitude: -25.382708, longitude: -49.265506, 12) // "6gkzwgjzn820"

Decode

Geohash.decode("ezs42")! // (latitude: 42.60498046875, longitude: -5.60302734375)

Neighbor Search

Geohash.neighbors("u000")! // ["u001", "u003", "u002", "spbr", "spbp", "ezzz", "gbpb", "gbpc"]

Install

Use Swift Package Manager.

License

MIT License.