ibotta_geohash 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/ibotta_geohash.gemspec +23 -0
- data/lib/ibotta_geohash.rb +256 -0
- data/lib/ibotta_geohash/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f2c5a167cf6728297d65045b95e53115d92859d6
|
4
|
+
data.tar.gz: 80dc62007eba4fd9f5bcaa4371fc7ddf6614d9bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 313550462ef150ac3880fef80bd6ad0a15b21f74f21bc4948e533b1bcf91b5011c8e23c2ae1fdbe9b9ef64b526e0c7aea13879ae93288cb5f3cee89dba64a2ec
|
7
|
+
data.tar.gz: 91563363bc0043aae3834614c71fe5d0b5df652104757a0d00c6e8c21f24df8a6298bb522fa55262474faf3c2607bb2d6a8b01ee7e0dd252a3a41490cc4bca37
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Justin Hart
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# Pure Ruby Geohash
|
2
|
+
|
3
|
+
This is a pure Ruby implementation of the [Geohash](http://en.wikipedia.org/wiki/Geohash) encoding.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ibotta_geohash'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ `bundle`
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ `gem install ibotta_geohash`
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'ibotta_geohash'
|
25
|
+
```
|
26
|
+
|
27
|
+
Encode a latitude and longitude into a geohash
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
GeoHash.encode(47.6062095, -122.3320708, 6)
|
31
|
+
# => "c23nb62w20st"
|
32
|
+
```
|
33
|
+
|
34
|
+
Decode a geohash into a bounding box
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
GeoHash.decode("c23nb6")
|
38
|
+
# => [[47.603759765625, -122.332763671875], [47.6092529296875, -122.32177734375]]
|
39
|
+
```
|
40
|
+
|
41
|
+
Calculate ajacent hashes
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
GeoHash.neighbors("xn774c")
|
45
|
+
# => ["xn774f", "xn7754", "xn7751", "xn7750", "xn774b", "xn7748", "xn7749", "xn774d"]
|
46
|
+
```
|
47
|
+
|
48
|
+
Calculate hashes covering a radius
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
GeoHash.areas_by_radius(45.37, -121.7, 50_000)
|
52
|
+
# => ["c216nekg2", "c216nekg8", "c216nekg9", "c216nekg3", "c216nekg1", "c216nekg0", "c216nekep", "c216neker", "c216nekex"]
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Known issues
|
62
|
+
|
63
|
+
* This pure ruby implementation is not fast.
|
64
|
+
* There are micro-optimizations, but a native C gem will be faster
|
65
|
+
* David Troy's geohash gem does not compile under Ruby 2
|
66
|
+
* Currently working on using C code from the Redis project
|
67
|
+
* Possible to convert to using integer math instead (like C)
|
68
|
+
* Ordering and structure of return types is not ideal
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Ibotta/ibotta_geohash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
77
|
+
|
78
|
+
## Forked From
|
79
|
+
|
80
|
+
http://github.com/masuidrive/pr_geohash
|
81
|
+
|
82
|
+
### Original LICENSE:
|
83
|
+
|
84
|
+
> (The MIT License)
|
85
|
+
|
86
|
+
> Copyright (c) 2009 [Yuichiro MASUI](http://masuidrive.jp)
|
87
|
+
|
88
|
+
> Permission is hereby granted, free of charge, to any person obtaining
|
89
|
+
> a copy of this software and associated documentation files (the
|
90
|
+
> 'Software'), to deal in the Software without restriction, including
|
91
|
+
> without limitation the rights to use, copy, modify, merge, publish,
|
92
|
+
> distribute, sublicense, and/or sell copies of the Software, and to
|
93
|
+
> permit persons to whom the Software is furnished to do so, subject to
|
94
|
+
> the following conditions:
|
95
|
+
|
96
|
+
> The above copyright notice and this permission notice shall be
|
97
|
+
> included in all copies or substantial portions of the Software.
|
98
|
+
|
99
|
+
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
100
|
+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
101
|
+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
102
|
+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
103
|
+
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
104
|
+
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
105
|
+
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
106
|
+
|
107
|
+
### Based library is
|
108
|
+
|
109
|
+
> http://github.com/davetroy/geohash-js/blob/master/geohash.js
|
110
|
+
|
111
|
+
> Geohash library for Javascript
|
112
|
+
> Copyright (c) 2008 David Troy, Roundhouse Technologies LLC
|
113
|
+
> Distributed under the MIT License
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ibotta_geohash"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ibotta_geohash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ibotta_geohash"
|
8
|
+
spec.version = IbottaGeohash::VERSION
|
9
|
+
spec.authors = ["Justin Hart", "Yuichiro MASUI", "kazuki.kuriyama"]
|
10
|
+
spec.email = ["jhart@onyxraven.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Pure Ruby Geohash}
|
13
|
+
spec.description = %q{Geohash methods originally written by @masuidrive, with fixes and additions}
|
14
|
+
spec.homepage = "https://github.com/Ibotta/ibotta_geohash"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1,256 @@
|
|
1
|
+
require 'ibotta_geohash/version'
|
2
|
+
|
3
|
+
# TODO document encode/decode
|
4
|
+
# TODO perf test
|
5
|
+
# TODO compare to C impl
|
6
|
+
|
7
|
+
#Pure ruby geohash library
|
8
|
+
#
|
9
|
+
# Based on: pr_geohash
|
10
|
+
# https://github.com/masuidrive/pr_geohash
|
11
|
+
# Yuichiro MASUI
|
12
|
+
# Geohash library for pure ruby
|
13
|
+
# Distributed under the MIT License
|
14
|
+
#
|
15
|
+
# Based library is
|
16
|
+
#// http://github.com/davetroy/geohash-js/blob/master/geohash.js
|
17
|
+
#// geohash.js
|
18
|
+
#// Geohash library for Javascript
|
19
|
+
#// (c) 2008 David Troy
|
20
|
+
#// Distributed under the MIT License
|
21
|
+
#
|
22
|
+
module IbottaGeohash
|
23
|
+
|
24
|
+
#needed for geohash encoding
|
25
|
+
BITS = [0x10, 0x08, 0x04, 0x02, 0x01].freeze
|
26
|
+
BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz".freeze
|
27
|
+
|
28
|
+
#not sure min is needed
|
29
|
+
MERCATOR = (-20037726.37..20037726.37).freeze
|
30
|
+
|
31
|
+
#not sure what these are needed by?
|
32
|
+
# WGS84_LAT = (-85.05113..85.05113).freeze
|
33
|
+
# WGS84_LON = (-180.0..180.0).freeze
|
34
|
+
|
35
|
+
#needed for bounding box calculations
|
36
|
+
EARTH_RADIUS_IN_METERS = 6372797.560856
|
37
|
+
DEG_LAT_IN_METERS = 2 * Math::PI * EARTH_RADIUS_IN_METERS / 360
|
38
|
+
DEG_TO_RAD = Math::PI / 180.0
|
39
|
+
RAD_TO_DEG = 180.0 / Math::PI
|
40
|
+
|
41
|
+
NEIGHBORS = {
|
42
|
+
:right => { :even => "bc01fg45238967deuvhjyznpkmstqrwx", :odd => "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
|
43
|
+
:left => { :even => "238967debc01fg45kmstqrwxuvhjyznp", :odd => "14365h7k9dcfesgujnmqp0r2twvyx8zb" },
|
44
|
+
:top => { :even => "p0r21436x8zb9dcf5h7kjnmqesgutwvy", :odd => "bc01fg45238967deuvhjyznpkmstqrwx" },
|
45
|
+
:bottom => { :even => "14365h7k9dcfesgujnmqp0r2twvyx8zb", :odd => "238967debc01fg45kmstqrwxuvhjyznp" }
|
46
|
+
}.freeze
|
47
|
+
|
48
|
+
BORDERS = {
|
49
|
+
:right => { :even => "bcfguvyz", :odd => "prxz" },
|
50
|
+
:left => { :even => "0145hjnp", :odd => "028b" },
|
51
|
+
:top => { :even => "prxz" , :odd => "bcfguvyz" },
|
52
|
+
:bottom => { :even => "028b" , :odd => "0145hjnp" }
|
53
|
+
}.freeze
|
54
|
+
|
55
|
+
class << self
|
56
|
+
|
57
|
+
#decode bounding box from geohash string
|
58
|
+
# @todo reorder bounds?
|
59
|
+
# @todo see if faster way (less array access?)
|
60
|
+
# @param [String] geohash string
|
61
|
+
# @return [Array<Array>] decoded bounding box [[south latitude, west longitude],[north latitude, east longitude]]
|
62
|
+
def decode(geohash)
|
63
|
+
#starting bounding box (as if entire world)
|
64
|
+
latlng = [[-90.0, 90.0], [-180.0, 180.0]]
|
65
|
+
#alternate lat/long (starting at long)
|
66
|
+
is_lng = 1
|
67
|
+
geohash.downcase.each_char do |c|
|
68
|
+
BITS.each do |mask|
|
69
|
+
#working on low or high bits?
|
70
|
+
lowhigh = (BASE32.index(c) & mask) == 0 ? 1 : 0
|
71
|
+
#calculate as half of current box
|
72
|
+
latlng[is_lng][lowhigh] = (latlng[is_lng][0] + latlng[is_lng][1]) / 2
|
73
|
+
#flip longitude bits
|
74
|
+
is_lng ^= 1
|
75
|
+
end
|
76
|
+
end
|
77
|
+
#transpose to low, high lat/long
|
78
|
+
latlng.transpose
|
79
|
+
end
|
80
|
+
|
81
|
+
#decode center of geohash area
|
82
|
+
# @todo see if faster way? other libs do this first, calc bounding from it
|
83
|
+
# @param [String] geohash string
|
84
|
+
# @return [Array<Float>] latitude, longitude of center
|
85
|
+
def decode_center(geohash)
|
86
|
+
res = decode(geohash)
|
87
|
+
#get center of each set
|
88
|
+
[((res[0][0] + res[1][0]) / 2), ((res[0][1] + res[1][1]) / 2)]
|
89
|
+
end
|
90
|
+
|
91
|
+
#Encode latitude and longitude into geohash string
|
92
|
+
# @todo see if faster way? less array access?
|
93
|
+
# @param [Float] latitude
|
94
|
+
# @param [Float] longitude
|
95
|
+
# @param [Integer] precision number of characters
|
96
|
+
# @return [String] encoded
|
97
|
+
def encode(latitude, longitude, precision=12)
|
98
|
+
latlng = [latitude, longitude]
|
99
|
+
#start at whole world
|
100
|
+
points = [[-90.0, 90.0], [-180.0, 180.0]]
|
101
|
+
#alternate lat/long (starting at long)
|
102
|
+
is_lng = 1
|
103
|
+
#for each character
|
104
|
+
(0...precision).map do
|
105
|
+
#character value is ch
|
106
|
+
ch = 0
|
107
|
+
5.times do |bit|
|
108
|
+
#get midpoints of current calculation
|
109
|
+
mid = (points[is_lng][0] + points[is_lng][1]) / 2
|
110
|
+
#min or max value
|
111
|
+
minmax = latlng[is_lng] > mid ? 0 : 1
|
112
|
+
points[is_lng][minmax] = mid
|
113
|
+
#add bits to character
|
114
|
+
ch |= BITS[bit] if latlng[is_lng] > mid
|
115
|
+
#flip bits
|
116
|
+
is_lng ^= 1
|
117
|
+
end
|
118
|
+
#map value to character
|
119
|
+
BASE32[ch,1]
|
120
|
+
end.join
|
121
|
+
end
|
122
|
+
|
123
|
+
# Calculate each 8 direction neighbors as geohash
|
124
|
+
# @param [String] geohash
|
125
|
+
# @return [Array<String>] neighbors in clockwise (top, topright, right, ...). Invalid regions at the poles are excluded
|
126
|
+
def neighbors(geohash)
|
127
|
+
#get all adjacencies (going around)
|
128
|
+
[[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map do |dirs|
|
129
|
+
point = adjacent(geohash, dirs[0])
|
130
|
+
[point, adjacent(point, dirs[1])]
|
131
|
+
end.flatten.compact
|
132
|
+
end
|
133
|
+
|
134
|
+
#calculate one adjacent neighbor
|
135
|
+
# @param [String] geohash
|
136
|
+
# @param [Symbol] dir which direction (top, right, bottom left)
|
137
|
+
# @return [String] neighbor in that direction (or nil if invalid)
|
138
|
+
def adjacent(geohash, dir)
|
139
|
+
#stop recursion if empty
|
140
|
+
return if geohash.nil? || geohash.empty?
|
141
|
+
#get last character
|
142
|
+
base, lastChr = geohash[0..-2], geohash[-1,1]
|
143
|
+
type = (geohash.length % 2)==1 ? :odd : :even
|
144
|
+
#if bit is border, get adjacency of base hash
|
145
|
+
if BORDERS[dir][type].include?(lastChr)
|
146
|
+
base = adjacent(base, dir)
|
147
|
+
if base.nil?
|
148
|
+
return if dir == :top || dir == :bottom
|
149
|
+
base = ''
|
150
|
+
end
|
151
|
+
end
|
152
|
+
#add back new neighbor character
|
153
|
+
base + BASE32[NEIGHBORS[dir][type].index(lastChr),1]
|
154
|
+
end
|
155
|
+
|
156
|
+
#get geohashes covering a radius
|
157
|
+
# @param [Float] lat
|
158
|
+
# @param [Flaot] lon
|
159
|
+
# @param [Float] radius_meters
|
160
|
+
# @return [Array<String>] hashes (neighbors and center) covering radius in clockwise (top, topright, right, ...). neighbors not covering radius are excluded
|
161
|
+
def areas_by_radius(lat, lon, radius_meters)
|
162
|
+
#get min/max latitude and longitude of radius around point
|
163
|
+
min_lat, min_lon, max_lat, max_lon = radius_box = bounding_box(lat, lon, radius_meters)
|
164
|
+
|
165
|
+
#estimate the size of boxes to target
|
166
|
+
steps = estimate_steps_by_radius(radius_meters)
|
167
|
+
#re-encode point using steps
|
168
|
+
#the geohashes are composed of 32 distinct numbers/letters, basically base 32
|
169
|
+
#bits are composed of 1s and 0s, base 2 or binary
|
170
|
+
#steps is the length of the binary number for longitude and latitude, and the combined length of the binary string (which interleaves both the longitude and latitude) is 2*steps
|
171
|
+
# since 32 is 2^5, while 2 is 2^1, the length of a base 32 number will be the length of a binary number divided by 5 and plus 1 (32 base 10 = 10000 base 2 = 10 base 32).
|
172
|
+
str_len = steps*2/5 + 1
|
173
|
+
hash = encode(lat, lon, str_len)
|
174
|
+
|
175
|
+
#get neighbors of box
|
176
|
+
neighbors = neighbors(hash)
|
177
|
+
neighbors_neighbors = neighbors.each_with_object([]) {|neighbor, nb| nb << neighbors(neighbor)}
|
178
|
+
|
179
|
+
# 25 geohashes surrounding the original
|
180
|
+
nb = neighbors_neighbors.flatten.uniq
|
181
|
+
|
182
|
+
# remove those geohashes that are outside the bounding box
|
183
|
+
nb.each do |neighbor|
|
184
|
+
n_latlng_low, n_latlng_high = decode(neighbor)
|
185
|
+
if n_latlng_low[0] > max_lat or n_latlng_low[1] > max_lon or n_latlng_high[0] < min_lat or n_latlng_high[1] < min_lon
|
186
|
+
nb -= [neighbor]
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
#return remaining neighbor list
|
191
|
+
nb
|
192
|
+
end
|
193
|
+
|
194
|
+
#estimate steps needed to cover radius
|
195
|
+
# @param [Float] radius_meters
|
196
|
+
# @return [Integer] steps required
|
197
|
+
def estimate_steps_by_radius(radius_meters)
|
198
|
+
step = 1
|
199
|
+
v = radius_meters
|
200
|
+
while v < MERCATOR.max
|
201
|
+
v *= 2
|
202
|
+
step += 1
|
203
|
+
end
|
204
|
+
step -= 1
|
205
|
+
step
|
206
|
+
end
|
207
|
+
|
208
|
+
#get bounding box around a radius
|
209
|
+
# NOTE: this breaks down at the poles (which is fine, we stop geocoding at the poles too)
|
210
|
+
# NOTE: this this is not WGS84 accurate (does not take into account oblong latitude). But as this is used for
|
211
|
+
# estimation anyway, thats probably fine
|
212
|
+
# @todo reorder return to match decode?
|
213
|
+
# @param [Float] lat
|
214
|
+
# @param [Float] lon
|
215
|
+
# @param [Flaot] radius_meters
|
216
|
+
# @return [Array] min_lat, min_long, max_lat, max_long
|
217
|
+
def bounding_box(lat, lon, radius_meters)
|
218
|
+
radius_meters = radius_meters.to_f
|
219
|
+
delta_lat = radius_meters / DEG_LAT_IN_METERS
|
220
|
+
delta_lon = radius_meters / (DEG_LAT_IN_METERS * Math.cos(lat * DEG_TO_RAD))
|
221
|
+
[
|
222
|
+
lat - delta_lat,
|
223
|
+
lon - delta_lon,
|
224
|
+
lat + delta_lat,
|
225
|
+
lon + delta_lon
|
226
|
+
]
|
227
|
+
end
|
228
|
+
|
229
|
+
#haversine distance (distance between two coordinate points) in meters
|
230
|
+
def haversine_distance(lat1, lon1, lat2, lon2)
|
231
|
+
r = EARTH_RADIUS_IN_METERS
|
232
|
+
phi1 = lat1 * DEG_TO_RAD
|
233
|
+
phi2 = lat2 * DEG_TO_RAD
|
234
|
+
delta_phi= (lat2-lat1) * DEG_TO_RAD
|
235
|
+
delta_lambda = (lon2-lon1) * DEG_TO_RAD
|
236
|
+
|
237
|
+
a = Math.sin(delta_phi/2) ** 2 +
|
238
|
+
Math.cos(phi1) * Math.cos(phi2) *
|
239
|
+
Math.sin(delta_lambda/2) ** 2
|
240
|
+
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
|
241
|
+
d = r * c
|
242
|
+
end
|
243
|
+
|
244
|
+
def geohash_area(geohash)
|
245
|
+
latlngmin, latlngmax = decode(geohash)
|
246
|
+
delta_lat_meters = (latlngmax[0] - latlngmin[0]) * DEG_LAT_IN_METERS
|
247
|
+
delta_lng_meters = (latlngmax[1] - latlngmin[1]) * (DEG_LAT_IN_METERS * Math.cos(latlngmax[0] * DEG_TO_RAD))
|
248
|
+
area = delta_lat_meters * delta_lng_meters
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
end # module Geohash
|
254
|
+
|
255
|
+
#setup class aliases
|
256
|
+
GeoHash = IbottaGeohash
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ibotta_geohash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Hart
|
8
|
+
- Yuichiro MASUI
|
9
|
+
- kazuki.kuriyama
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2016-07-07 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.10'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '10.0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '10.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: Geohash methods originally written by @masuidrive, with fixes and additions
|
58
|
+
email:
|
59
|
+
- jhart@onyxraven.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- ibotta_geohash.gemspec
|
75
|
+
- lib/ibotta_geohash.rb
|
76
|
+
- lib/ibotta_geohash/version.rb
|
77
|
+
homepage: https://github.com/Ibotta/ibotta_geohash
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Pure Ruby Geohash
|
101
|
+
test_files: []
|