geohashinator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geohashinator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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.
@@ -0,0 +1,29 @@
1
+ # Geohashinator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'geohashinator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install geohashinator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/geohashinator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Vincent Siebert"]
6
+ gem.email = ["vincent@siebert.im"]
7
+ gem.description = %q{Gem for easy adding of geohash functionality}
8
+ gem.summary = %q{Gem for easy adding of geohash functionality}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "geohashinator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Geohashinator::VERSION
17
+ end
@@ -0,0 +1,90 @@
1
+ require "geohashinator/version"
2
+ require "geohashinator/range"
3
+
4
+ module Geohashinator
5
+
6
+ DICTIONARY = "0123456789bcdefghjkmnpqrstuvwxyz".freeze
7
+
8
+ # encode a decimal lat/lng pair to a geohash-string
9
+ # the output of this function may slightly differ from machine to machine!
10
+ def geohash_encode(lat, lng)
11
+ dict_translate_binary(binary_multiplex(
12
+ value_encode(lng, (-180..180)),
13
+ value_encode(lat, (-90..90))
14
+ ))
15
+ end
16
+
17
+ # decode a geohash-string to a hash containing lat/lng as numbers
18
+ # the output of this function may slightly differ from machine to machine!
19
+ def geohash_decode(str)
20
+ bins = binary_demultiplex(dict_translate_string(str))
21
+ {
22
+ :lat => value_approximate(bins[1], (-90..90)),
23
+ :lng => value_approximate(bins[0], (-180..180))
24
+ }
25
+ end
26
+
27
+ private
28
+ # translate a string char-wise to a array of bytes with the base32-like dictionary
29
+ def dict_translate_string(str)
30
+ str.each_char.map{ |c| DICTIONARY.index(c) }
31
+ end
32
+
33
+ # translate an array of bytes to a string with the base32-like dictionary
34
+ def dict_translate_binary(arr)
35
+ arr.map{ |n| DICTIONARY[n] }.join("")
36
+ end
37
+
38
+ # approximate the decimal value of a binary lat or lng representation
39
+ # second parameter is (-90..90) for lat and (-180..180) for lng
40
+ def value_approximate(arr, approx)
41
+ return approx.avg if arr.length == 0
42
+ if arr[0] == 1
43
+ value_approximate(arr[1..-1], (approx.avg..approx.max))
44
+ else
45
+ value_approximate(arr[1..-1], (approx.min..approx.avg))
46
+ end
47
+ end
48
+
49
+ # calculate a 12-bit binary representation of a decimal lat or lng value
50
+ # second parameter is (-90..90) for lat and (-180..180) for lng
51
+ def value_encode(val, approx, n=28)
52
+ return nil if n == 0
53
+ if val > approx.avg
54
+ [1, value_encode(val, (approx.avg..approx.max), n-1)]
55
+ else
56
+ [0, value_encode(val, (approx.min..approx.avg), n-1)]
57
+ end.flatten.compact
58
+ end
59
+
60
+ # convert two binary representations of a lat + lng pair into one "bitstream"
61
+ def binary_multiplex(arr1, arr2)
62
+ bitstream = ""
63
+
64
+ # merge the two "bitstreams". lng on the even- and lat on the odd-indexed bits
65
+ (arr1.length+arr2.length).times do |i|
66
+ bitstream += (i%2==0 ? arr1.shift : arr2.shift).to_s
67
+ end
68
+
69
+ # split the merged bitstream into five-bit pieces and return them as numbers
70
+ (bitstream.length/5.0).ceil.times.map do |i|
71
+ bitstream[i*5..((i+1)*5)-1].to_i(2)
72
+ end
73
+ end
74
+
75
+ # convert a binary "bitstream" into the binary representations of lat/lng
76
+ def binary_demultiplex(arr)
77
+ [[],[]].tap do |out|
78
+
79
+ # split the bitstream into groups of 5
80
+ bitstream = arr.map do |n|
81
+ ("00000"+n.to_s(2))[-5..-1]
82
+ end.join("").each_char
83
+
84
+ # unmerge the bitstream: lng-bits for even and lat-bits for odd positions
85
+ bitstream.each_with_index do |c,i|
86
+ out[i%2] << c.to_i
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ class Range
2
+
3
+ def avg
4
+ (min+max)/2.0
5
+ end
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module Geohashinator
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geohashinator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vincent Siebert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Gem for easy adding of geohash functionality
15
+ email:
16
+ - vincent@siebert.im
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - geohashinator.gemspec
27
+ - lib/geohashinator.rb
28
+ - lib/geohashinator/range.rb
29
+ - lib/geohashinator/version.rb
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Gem for easy adding of geohash functionality
54
+ test_files: []
55
+ has_rdoc: