pr_geohash 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
@@ -0,0 +1,4 @@
1
+ === 1.0.0 / 2009-11-03
2
+
3
+ * Birthday!
4
+
@@ -0,0 +1,7 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/pr_geohash.rb
7
+ test/test_pr_geohash.rb
@@ -0,0 +1,98 @@
1
+ = pr_geohash
2
+
3
+ * http://github.com/masuidrive/pr_geohash
4
+
5
+
6
+ == DESCRIPTION:
7
+
8
+ GeoHash encode/decode library for pure Ruby.
9
+
10
+ It's implementation of http://en.wikipedia.org/wiki/Geohash
11
+
12
+
13
+ == FEATURES
14
+
15
+ * Encode/decode geohash
16
+ * Calculate adjacent geohash
17
+ * Tested with Ruby 1.8.7 and JRuby 1.3.1
18
+
19
+
20
+ == SYNOPSIS:
21
+
22
+ require "rubyforge"
23
+ require "pr_geohash"
24
+
25
+ #########
26
+ # Encode latitude and longitude into geohash
27
+
28
+ GeoHash.encode(47.6062095, -122.3320708)
29
+ # => "c23nb62w20st"
30
+
31
+ GeoHash.encode(47.6062095, -122.3320708, 6)
32
+ # => "c23nb6"
33
+
34
+
35
+ #########
36
+ # Decode from geohash
37
+
38
+ GeoHash.decode("c23nb6")
39
+ # => [[47.603759765625, -122.332763671875], [47.6092529296875, -122.32177734375]]
40
+
41
+
42
+ #########
43
+ # Calculate adjacent geohash
44
+
45
+ GeoHash.neighbors("xn774c")
46
+ # => ["xn774f", "xn7754", "xn7751", "xn7750", "xn774b", "xn7748", "xn7749", "xn774d"]
47
+
48
+ =begin
49
+ +--------+--------+--------+
50
+ | xn774d | xn774f | xn7754 |
51
+ +--------+--------+--------+
52
+ | xn7749 | xn774c | xn7751 |
53
+ +--------+--------+--------+
54
+ | xn7748 | xn774b | xn7750 |
55
+ +--------+--------+--------+
56
+ =end
57
+
58
+
59
+ == INSTALL:
60
+
61
+ * sudo gem install pr_geohash
62
+
63
+
64
+ == LICENSE:
65
+
66
+ (The MIT License)
67
+
68
+ Copyright (c) 2009 {Yuichiro MASUI}[http://masuidrive.jp]
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88
+
89
+
90
+ === Based library is
91
+
92
+ http://github.com/davetroy/geohash-js/blob/master/geohash.js
93
+
94
+ Geohash library for Javascript
95
+
96
+ Copyright (c) 2008 David Troy, Roundhouse Technologies LLC
97
+
98
+ Distributed under the MIT License
@@ -0,0 +1,14 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'pr_geohash' do
7
+ developer('Yuichiro MASUI', 'masui@masuidrive.jp')
8
+
9
+ self.extra_rdoc_files = Dir["*.rdoc"]
10
+ self.readme_file = 'README.rdoc'
11
+ self.rubyforge_name = 'seattlerb'
12
+ end
13
+
14
+ # vim: syntax=ruby
@@ -0,0 +1,93 @@
1
+ =begin
2
+ geohash.rb
3
+ Geohash library for pure ruby
4
+ Distributed under the MIT License
5
+
6
+ Based library is
7
+ // http://github.com/davetroy/geohash-js/blob/master/geohash.js
8
+ // geohash.js
9
+ // Geohash library for Javascript
10
+ // (c) 2008 David Troy
11
+ // Distributed under the MIT License
12
+ =end
13
+
14
+ module GeoHash
15
+ VERSION = "1.0.0"
16
+
17
+ #########
18
+ # Decode from geohash
19
+ #
20
+ # geohash:: geohash code
21
+ # return:: decoded bounding box [[north latitude, west longitude],[south latitude, east longitude]]
22
+ def decode(geohash)
23
+ latlng = [[-90.0, 90.0], [-180.0, 180.0]]
24
+ is_lng = 1
25
+ geohash.downcase.scan(/./) do |c|
26
+ BITS.each do |mask|
27
+ latlng[is_lng][(BASE32.index(c) & mask)==0 ? 1 : 0] = (latlng[is_lng][0] + latlng[is_lng][1]) / 2
28
+ is_lng ^= 1
29
+ end
30
+ end
31
+ latlng.transpose
32
+ end
33
+ module_function :decode
34
+
35
+ #########
36
+ # Encode latitude and longitude into geohash
37
+ def encode(latitude, longitude, precision=12)
38
+ latlng = [latitude, longitude]
39
+ points = [[-90.0, 90.0], [-180.0, 180.0]]
40
+ is_lng = 1
41
+ (0...precision).map {
42
+ ch = 0
43
+ 5.times do |bit|
44
+ mid = (points[is_lng][0] + points[is_lng][1]) / 2
45
+ points[is_lng][latlng[is_lng] > mid ? 0 : 1] = mid
46
+ ch |= BITS[bit] if latlng[is_lng] > mid
47
+ is_lng ^= 1
48
+ end
49
+ BASE32[ch,1]
50
+ }.join
51
+ end
52
+ module_function :encode
53
+
54
+ #########
55
+ # Calculate neighbors (8 adjacents) geohash
56
+ def neighbors(geohash)
57
+ [[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map{ |dirs|
58
+ point = adjacent(geohash, dirs[0])
59
+ [point, adjacent(point, dirs[1])]
60
+ }.flatten
61
+ end
62
+ module_function :neighbors
63
+
64
+ #########
65
+ # Calculate adjacents geohash
66
+ def adjacent(geohash, dir)
67
+ base, lastChr = geohash[0..-2], geohash[-1,1]
68
+ type = (geohash.length % 2)==1 ? :odd : :even
69
+ if BORDERS[dir][type].include?(lastChr)
70
+ base = adjacent(base, dir)
71
+ end
72
+ base + BASE32[NEIGHBORS[dir][type].index(lastChr),1]
73
+ end
74
+ module_function :adjacent
75
+
76
+
77
+ BITS = [0x10, 0x08, 0x04, 0x02, 0x01]
78
+ BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"
79
+
80
+ NEIGHBORS = {
81
+ :right => { :even => "bc01fg45238967deuvhjyznpkmstqrwx", :odd => "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
82
+ :left => { :even => "238967debc01fg45kmstqrwxuvhjyznp", :odd => "14365h7k9dcfesgujnmqp0r2twvyx8zb" },
83
+ :top => { :even => "p0r21436x8zb9dcf5h7kjnmqesgutwvy", :odd => "bc01fg45238967deuvhjyznpkmstqrwx" },
84
+ :bottom => { :even => "14365h7k9dcfesgujnmqp0r2twvyx8zb", :odd => "238967debc01fg45kmstqrwxuvhjyznp" }
85
+ }
86
+
87
+ BORDERS = {
88
+ :right => { :even => "bcfguvyz", :odd => "prxz" },
89
+ :left => { :even => "0145hjnp", :odd => "028b" },
90
+ :top => { :even => "prxz" , :odd => "bcfguvyz" },
91
+ :bottom => { :even => "028b" , :odd => "0145hjnp" }
92
+ }
93
+ end # module GeoHash
@@ -0,0 +1,49 @@
1
+ require 'pr_geohash'
2
+ require 'test/unit'
3
+
4
+ class PrGeoHashTests < Test::Unit::TestCase
5
+ def test_decode
6
+ {
7
+ 'c216ne' => [[45.3680419921875, -121.70654296875], [45.37353515625, -121.695556640625]],
8
+ 'C216Ne' => [[45.3680419921875, -121.70654296875], [45.37353515625, -121.695556640625]],
9
+ 'dqcw4' => [[39.0234375, -76.552734375], [39.0673828125, -76.5087890625]],
10
+ 'DQCW4' => [[39.0234375, -76.552734375], [39.0673828125, -76.5087890625]]
11
+ }.each do |hash, latlng|
12
+ assert_equal GeoHash.decode(hash), latlng
13
+ end
14
+ end
15
+
16
+ def test_encode
17
+ {
18
+ [ 45.37, -121.7 ] => 'c216ne',
19
+ [ 47.6062095, -122.3320708] => 'c23nb62w20sth',
20
+ [ 35.6894875, 139.6917064] => 'xn774c06kdtve',
21
+ [-33.8671390, 151.2071140] => 'r3gx2f9tt5sne',
22
+ [ 51.5001524, -0.1262362] => 'gcpuvpk44kprq'
23
+ }.each do |latlng, hash|
24
+ assert_equal GeoHash.encode(latlng[0], latlng[1], hash.length), hash
25
+ end
26
+ end
27
+
28
+ def test_neighbors
29
+ {
30
+ 'dqcw5' => ["dqcw7", "dqctg", "dqcw4", "dqcwh", "dqcw6", "dqcwk", "dqctf", "dqctu"],
31
+ 'xn774c' => ['xn774f','xn774b','xn7751','xn7749','xn774d','xn7754','xn7750','xn7748'],
32
+ 'gcpuvpk' => ['gcpuvps','gcpuvph','gcpuvpm','gcpuvp7','gcpuvpe','gcpuvpt','gcpuvpj','gcpuvp5'],
33
+ 'c23nb62w' => ['c23nb62x','c23nb62t','c23nb62y','c23nb62q','c23nb62r','c23nb62z','c23nb62v','c23nb62m']
34
+ }.each do |geohash, neighbors|
35
+ assert_equal GeoHash.neighbors(geohash).sort, neighbors.sort
36
+ end
37
+ end
38
+
39
+ def test_adjacent
40
+ {
41
+ ["dqcjq", :top] => 'dqcjw',
42
+ ["dqcjq", :bottom] => 'dqcjn',
43
+ ["dqcjq", :left] => 'dqcjm',
44
+ ["dqcjq", :right] => 'dqcjr'
45
+ }.each do |position, hash|
46
+ assert_equal GeoHash.adjacent(*position), hash
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pr_geohash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuichiro MASUI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-03 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: |-
26
+ GeoHash encode/decode library for pure Ruby.
27
+
28
+ It's implementation of http://en.wikipedia.org/wiki/Geohash
29
+ email:
30
+ - masui@masuidrive.jp
31
+ executables: []
32
+
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ files:
40
+ - .autotest
41
+ - History.txt
42
+ - Manifest.txt
43
+ - README.rdoc
44
+ - Rakefile
45
+ - lib/pr_geohash.rb
46
+ - test/test_pr_geohash.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/masuidrive/pr_geohash
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: seattlerb
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: GeoHash encode/decode library for pure Ruby
76
+ test_files:
77
+ - test/test_pr_geohash.rb