Skip to content

Commit

Permalink
v2 (#2)
Browse files Browse the repository at this point in the history
* Geohash is a data struct now
* Consolidate GeohashBox into Geohash
* Add MapKit/CoreLocation helpers to Geohash
* Update README.md
* Create LICENSE
  • Loading branch information
ualch9 committed Aug 4, 2020
1 parent 45b05d0 commit c136935
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 119 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Alan Chu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 53 additions & 10 deletions README.md
@@ -1,34 +1,77 @@
# GeohashKit
![Swift](https://github.com/ualch9/GeohashKit/workflows/Swift/badge.svg)

GeohashKit is a native Swift implementation of the geohash hashing algorithem. Supporting encode, decode and neighbor search. This is an indirect fork of [maximveksler/Geohashkit](https://github.com/maximveksler/GeohashKit), meant for Swift Package Manager support.
GeohashKit is a native Swift implementation of the [Geohash hashing algorithm](https://www.movable-type.co.uk/scripts/geohash.html). Supporting encode, decode and neighbor search.

The original Swift (v1) implementation is from [maximveksler/GeohashKit](https://github.com/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
## API (v2)
A `Geohash` is a data structure, representing a geohash as a rectangular cell.

### Encode
#### Initialize with coordinates ("Encode")
```swift
Geohash.encode(latitude: 42.6, longitude: -5.6) // "ezs42"
let geohash = Geohash(coordinates: (42.6, -5.6), precision: 5)
print(geohash.geohash) // "ezs42"
```

###### Specify desired precision
#### Initialize with existing geohash ("Decode")
```swift
Geohash.encode(latitude: -25.382708, longitude: -49.265506, 12) // "6gkzwgjzn820"
let geohash = Geohash(geohash: "ezs42")
print(geohash.coordinates) // (latitude: 42.60498046875, longitude: -5.60302734375)
```

### Decode
#### Neighbors
```swift
Geohash.decode("ezs42")! // (latitude: 42.60498046875, longitude: -5.60302734375)
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"]
```

### Neighbor Search
#### MapKit
```swift
Geohash.neighbors("u000")! // ["u001", "u003", "u002", "spbr", "spbp", "ezzz", "gbpb", "gbpc"]
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.

<details>
<summary>v1 API (maximveksler/GeohashKit compatible)</summary>
To use the maximveksler-compatible API, checkout exactly `1.0`:

```swift
.package(url: "https://github.com/ualch9/geohashkit.git", .exact("1.0"))
```

### Encode
```swift
Geohash.encode(latitude: 42.6, longitude: -5.6) // "ezs42"
```

### Specify desired precision
```swift
Geohash.encode(latitude: -25.382708, longitude: -49.265506, 12) // "6gkzwgjzn820"
```

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

### Neighbor Search
```swift
Geohash.neighbors("u000")! // ["u001", "u003", "u002", "spbr", "spbp", "ezzz", "gbpb", "gbpc"]
```
</details>

## Install
Use Swift Package Manager.

Expand Down

0 comments on commit c136935

Please sign in to comment.