geohash_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c296b6b794f2ebd0d80215369312c35348695c76
4
+ data.tar.gz: 52883d58d3830f0f6d5f56166c03b26770337625
5
+ SHA512:
6
+ metadata.gz: 5ec20fd3cc83f39101238d476b43929d718cd3b0bf1563258131e21d35c7a531b59b1400f681ca44af673f650485840aea6446d60d08fd113151ca10d1bc600f
7
+ data.tar.gz: 239410dc6459bd9439471b89b85394efcc98e0df32b69c66609a7ea4380e102513a36de5b840d44d502e377afb6b2c27c4994c288807a7bd06ac475bb0146516
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yuki Iwanaga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ GeoHash
2
+ =======
3
+
4
+ GeoHash en/decode library written in ruby.
5
+
6
+ Based on [masuidrive/pr_geohash](https://github.com/masuidrive/pr_geohash), which isn't good implementation.
7
+
8
+
9
+ Install
10
+ -------
11
+
12
+ ```ruby
13
+ gem 'geohash_ruby'
14
+ ```
15
+
16
+
17
+ Usage
18
+ -----
19
+
20
+ ### Encode latitude and longitude into geohash
21
+
22
+ ```ruby
23
+ GeoHash.encode 47.6062095, -122.3320708
24
+ #=> "c23nb62w20st"
25
+
26
+ GeoHash.encode 47.6062095, -122.3320708, 6
27
+ #=> "c23nb6"
28
+ ```
29
+
30
+ ### Decode from geohash
31
+
32
+ ```ruby
33
+ GeoHash.decode 'c23nb6'
34
+ #=> [[47.603759765625, -122.332763671875], [47.6092529296875, -122.32177734375]]
35
+ ```
36
+
37
+ ### Adjacents
38
+
39
+ ```ruby
40
+ GeoHash.adjacent 'c23nb6', :top
41
+ #=> "c23nb7"
42
+ ```
43
+
44
+ ### Neighbors
45
+
46
+ ```ruby
47
+ GeoHash.neighbors 'xn774c'
48
+ #=> ["xn774f", "xn7754", "xn7751", "xn7750", "xn774b", "xn7748", "xn7749", "xn774d"]
49
+
50
+ =begin
51
+ +--------+--------+--------+
52
+ | xn774d | xn774f | xn7754 |
53
+ +--------+--------+--------+
54
+ | xn7749 | xn774c | xn7751 |
55
+ +--------+--------+--------+
56
+ | xn7748 | xn774b | xn7750 |
57
+ +--------+--------+--------+
58
+ =end
59
+ ```
60
+
61
+ ### Walk
62
+
63
+ ```ruby
64
+ GeoHash.walk 'xn774c', [
65
+ :top,
66
+ :right,
67
+ :bottom, :bottom,
68
+ :left, :left,
69
+ :top, :top
70
+ ]
71
+ #=> ["xn774f", "xn7754", "xn7751", "xn7750", "xn774b", "xn7748", "xn7749", "xn774d"]
72
+ # implementation of `neighbors`
73
+ ```
74
+
75
+
76
+ License
77
+ -------
78
+
79
+ This project is copyright by [Creasty](http://www.creasty.com), released under the MIT lisence.
80
+ See `LICENSE` file for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -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 'geohash'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'geohash_ruby'
8
+ spec.version = GeoHash::VERSION
9
+ spec.authors = ['Yuki Iwanaga']
10
+ spec.email = ['yuki@creasty.com']
11
+ spec.summary = 'GeoHash en/decode library written in ruby'
12
+ spec.description = 'GeoHash en/decode library written in ruby'
13
+ spec.homepage = 'https://github.com/creasty/geohash_ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake', '~> 10.3'
23
+ end
data/lib/geohash.rb ADDED
@@ -0,0 +1,173 @@
1
+ module GeoHash
2
+
3
+ VERSION = '1.0.0'
4
+
5
+ BASE32 = '0123456789bcdefghjkmnpqrstuvwxyz'
6
+
7
+ NEIGHBORS = {
8
+ right: {
9
+ even: 'bc01fg45238967deuvhjyznpkmstqrwx',
10
+ odd: 'p0r21436x8zb9dcf5h7kjnmqesgutwvy',
11
+ },
12
+ left: {
13
+ even: '238967debc01fg45kmstqrwxuvhjyznp',
14
+ odd: '14365h7k9dcfesgujnmqp0r2twvyx8zb',
15
+ },
16
+ top: {
17
+ even: 'p0r21436x8zb9dcf5h7kjnmqesgutwvy',
18
+ odd: 'bc01fg45238967deuvhjyznpkmstqrwx',
19
+ },
20
+ bottom: {
21
+ even: '14365h7k9dcfesgujnmqp0r2twvyx8zb',
22
+ odd: '238967debc01fg45kmstqrwxuvhjyznp',
23
+ }
24
+ }
25
+
26
+ BORDERS = {
27
+ right: { even: 'bcfguvyz', odd: 'prxz' },
28
+ left: { even: '0145hjnp', odd: '028b' },
29
+ top: { even: 'prxz', odd: 'bcfguvyz' },
30
+ bottom: { even: '028b', odd: '0145hjnp' },
31
+ }
32
+
33
+ @@neighbors_cache = {}
34
+
35
+ @@adjacent_cache = {
36
+ right: {}, left: {}, top: {}, bottom: {}
37
+ }
38
+
39
+
40
+ module_function
41
+
42
+ ###
43
+ # Decode geohash into bounding box of latitudes and longitudes
44
+ #
45
+ # @params {String} geohash - geohash code
46
+ #
47
+ # @return {[[Float]]} decoded bounding box
48
+ # [
49
+ # [north_latitude, west_longitude],
50
+ # [south_latitude, east_longitude]
51
+ # ]
52
+ ###
53
+ def decode(geohash)
54
+ bounds = [[-90.0, +90.0], [-180.0, +180.0]]
55
+
56
+ geohash.downcase.each_char.with_index do |c, i|
57
+ d = BASE32.index c
58
+
59
+ 5.times do |j|
60
+ bit = (d & (1 << (4 - j))) >> (4 - j)
61
+ k = (~i & 1) ^ (j & 1)
62
+ bounds[k][bit ^ 1] = (bounds[k][0] + bounds[k][1]) / 2
63
+ end
64
+ end
65
+
66
+ bounds.transpose
67
+ end
68
+
69
+ ###
70
+ # Encode latitude and longitude into geohash
71
+ #
72
+ # @params {Float} latitude
73
+ # @params {Float} longitude
74
+ # @params {Integer} precision - scale from 1 to 12
75
+ #
76
+ # @return {String} geohash string
77
+ ###
78
+ def encode(latitude, longitude, precision = 12)
79
+ mids = [latitude, longitude]
80
+ bounds = [[-90.0, +90.0], [-180.0, +180.0]]
81
+
82
+ geohash = []
83
+
84
+ precision.times do |i|
85
+ d = 0
86
+
87
+ 5.times do |j|
88
+ k = (~i & 1) ^ (j & 1)
89
+ mid = (bounds[k][0] + bounds[k][1]) / 2
90
+ bit = mids[k] > mid ? 1 : 0
91
+ bounds[k][bit ^ 1] = mid
92
+ d |= bit << (4 - j)
93
+ end
94
+
95
+ geohash << BASE32[d]
96
+ end
97
+
98
+ geohash.join
99
+ end
100
+
101
+ ###
102
+ # Calculate neighbors geohash
103
+ #
104
+ # @params {String} geohash
105
+ #
106
+ # @return {[String]} neighbors array
107
+ ###
108
+ def neighbors(geohash)
109
+ return @@neighbors_cache[geohash] if @@neighbors_cache[geohash]
110
+
111
+ # walk path:
112
+ #
113
+ # 8 1 - 2
114
+ # | | |
115
+ # 7 B 3
116
+ # | |
117
+ # 6 - 5 - 4
118
+ #
119
+ @@neighbors_cache[geohash] = walk geohash, [
120
+ :top,
121
+ :right,
122
+ :bottom, :bottom,
123
+ :left, :left,
124
+ :top, :top
125
+ ]
126
+ end
127
+
128
+ ###
129
+ # Calculate adjacent geohashes
130
+ #
131
+ # @params {String} geohash
132
+ # @params {Symbol} dir - one of :right, :left, :top, :bottom
133
+ #
134
+ # @return {String} adjacent geohash
135
+ ###
136
+ def adjacent(geohash, dir)
137
+ return @@adjacent_cache[dir][geohash] if @@adjacent_cache[dir][geohash]
138
+
139
+ head, last = geohash[0..-2], geohash[-1]
140
+ type = [:even, :odd][geohash.length % 2]
141
+
142
+ head = adjacent(head, dir) if BORDERS[dir][type].include?(last)
143
+
144
+ # NOTICE:
145
+ # do not use append `<<` instead of `+`
146
+ # head possibly be cached so that mustn't be mutated,
147
+ # just create new String instance with it
148
+ head = head + BASE32[NEIGHBORS[dir][type].index(last)]
149
+
150
+ @@adjacent_cache[dir][geohash] = head
151
+ end
152
+
153
+ ###
154
+ # Walk down
155
+ #
156
+ # @params {String} base - base geohash to begin walk
157
+ # @params {[Symbol]} path - directions to walk with
158
+ #
159
+ # @return {[String]} result geohashes of the walk
160
+ ###
161
+ def walk(base, path)
162
+ hashes = []
163
+
164
+ h = base
165
+
166
+ path.each do |dir|
167
+ hashes << (h = adjacent(h, dir))
168
+ end
169
+
170
+ hashes
171
+ end
172
+
173
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geohash_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuki Iwanaga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ description: GeoHash en/decode library written in ruby
42
+ email:
43
+ - yuki@creasty.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - geohash_ruby.gemspec
54
+ - lib/geohash.rb
55
+ homepage: https://github.com/creasty/geohash_ruby
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: GeoHash en/decode library written in ruby
79
+ test_files: []