Skip to content

Commit

Permalink
Merge pull request #1 from michael-groble/add-implementation
Browse files Browse the repository at this point in the history
Add implementation
  • Loading branch information
michael-groble authored Jan 8, 2017
2 parents 04ba6aa + ef7c859 commit 73da652
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 66 deletions.
69 changes: 4 additions & 65 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,65 +1,4 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xcuserstate

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
.DS_Store
/.build
/Packages
/*.xcodeproj
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode8
script:
- eval "$(curl -sL https://swift.vapor.sh/ci)"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2017, michael-groble
Copyright (c) 2017, Michael Groble
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import PackageDescription

let package = Package(
name: "Geohash"
)
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# Geohash
[![Build Status](https://api.travis-ci.org/michael-groble/Geohash.svg?branch=master)](https://travis-ci.org/michael-groble/Geohash)

Native Swift geohash package supporting binary and character encoding

Geohashes are represented internally as 64 bit integers. A hash can be constructed either using character
precision (up to 12 characters) or binary precision (up to 32 bits per angle).

```swift
GeohashBits(location: Location(longitude: -0.1, latitude: 51.5), characterPrecision: 12).hash()
=> "gcpuvxr1jzf"
GeohashBits(location: Location(longitude: -0.1, latitude: 51.5), bitPrecision: 26).hash()
=> "gcpuvxr1jz"
```

Note that the last value is truncated at 10 characters (50 bits) even though the "full" representation is
52 bits total. Since the character encoding is Base32, we require 5 bits for each.

The Geohash boundaries and centroids are correctly handled when the character representation provides a
different number of bits for latitude and longitude (e.g. geohash 7 which has 18 bits of longitude and 17
bits of latitude).

```swift
GeohashBits(hash: "u10hfr2").boundingBox().center()
=> (longitude: 0.0995635986328125, latitude: 51.5004730224609)
```

This is the same answer you will get from PostGIS

```sql
select ST_AsText(ST_PointFromGeoHash('u10hfr2'));
st_astext
--------------------------------------------
POINT(0.0995635986328125 51.5004730224609)
```

The library also supports computing neighbors

```swift
GeohashBits(hash: "u10hfr2c4pv").neighbor(.north).hash()
=> "u10hfr2c60j"
```

## Acknowledgements
Based on the [Redis implementation](https://github.com/antirez/redis/blob/unstable/src/geohash.c),
* Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
* Copyright (c) 2014, Matt Stancliff <matt@genges.com>
* Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>
24 changes: 24 additions & 0 deletions Sources/Geohash/BoundingBox.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// BoundingBox.swift
// Geohash
//
// Created by michael groble on 1/6/17.
//
//

public class BoundingBox {
let min: Location
let max: Location

public init(min: Location, max: Location) {
self.min = min
self.max = max
}

public func center() -> Location {
return Location(
longitude: 0.5 * (self.min.longitude + self.max.longitude),
latitude: 0.5 * (self.min.latitude + self.max.latitude)
)
}
}
Loading

0 comments on commit 73da652

Please sign in to comment.