geohashr 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.coveralls.yml +2 -0
- data/.gemtest +0 -0
- data/.gitignore +43 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +5 -0
- data/CONTRIBUTING.md +46 -0
- data/LICENSE.txt +20 -0
- data/Manifest.txt +16 -0
- data/README.md +64 -0
- data/Rakefile +81 -0
- data/geohashr.gemspec +41 -0
- data/lib/geohashr/constants.rb +18 -0
- data/lib/geohashr/geohash.rb +92 -0
- data/lib/geohashr/version.rb +3 -0
- data/lib/geohashr.rb +14 -0
- data/test/helper.rb +18 -0
- data/test/test_geohashr.rb +52 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NWM2NmFjNjM4Njg5Mzk0NTI5ODc1NDdkMjdjZDQyZWEyOGJhMzNhMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDI5YmY3Yjg1YTlkY2M2MmMwZjVlOWQxYmRkNjYxZTkxNzEyZDE2Yw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTY0ZWMzNjYxZmY1MzA1MWU0OTIzMzY1NGM2OWRiNTM3MzIzOTBlNGJjOTQy
|
10
|
+
ZjJlNmU2YTgyYmNjZmM5NDRkYWM1ZWVjYjYyOWUxMDQwZDM2YzEwMmFkNjgy
|
11
|
+
ZWIxMmExMGMyMzU0ZmY5ZmFhMjdmMTU1MzgzYmU4YzEwMTBjYjY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjkxYTAwNTNjYmEwNDQ5MmUwMTJkNjRjNjg3ZTk5MmFhMDYzNDdjYWZlNjZh
|
14
|
+
YjI2MGM5MDE3YzMxZTljMDQ2NTE3ZjYxMjczYzQ4NTViZWI4YmY1MzEzNTQ0
|
15
|
+
YTcxODEwZWI5MDdhM2ZkNjY0MWM3NDc5NWNmODg0N2VhYWQ1NGU=
|
data/.coveralls.yml
ADDED
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
## CI Artifacts
|
2
|
+
Gemfile.ci.lock
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
.yardoc
|
18
|
+
coverage
|
19
|
+
doc
|
20
|
+
log
|
21
|
+
pkg
|
22
|
+
rdoc
|
23
|
+
tmp
|
24
|
+
|
25
|
+
## BUNDLER
|
26
|
+
*.gem
|
27
|
+
.bundle
|
28
|
+
pkg
|
29
|
+
|
30
|
+
## RCOV
|
31
|
+
coverage.data
|
32
|
+
|
33
|
+
## RUBINIUS
|
34
|
+
*.rbc
|
35
|
+
|
36
|
+
## Hoe?
|
37
|
+
.source_index
|
38
|
+
|
39
|
+
# Mac Finder files
|
40
|
+
*.DS_Store
|
41
|
+
|
42
|
+
# Node / NPM
|
43
|
+
node_modules
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
## Contributing
|
2
|
+
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
3
|
+
improve this project.
|
4
|
+
|
5
|
+
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
6
|
+
|
7
|
+
Here are some ways *you* can contribute:
|
8
|
+
|
9
|
+
* by using alpha, beta, and prerelease versions
|
10
|
+
* by reporting bugs
|
11
|
+
* by suggesting new features
|
12
|
+
* by writing or editing documentation
|
13
|
+
* by writing specifications
|
14
|
+
* by writing code ( **no patch is too small**: fix typos, add comments, clean up
|
15
|
+
inconsistent whitespace)
|
16
|
+
* by refactoring code
|
17
|
+
* by closing [issues][]
|
18
|
+
* by reviewing patches
|
19
|
+
|
20
|
+
[issues]: https://github.com/johnnyt/geohashr/issues
|
21
|
+
|
22
|
+
## Submitting an Issue
|
23
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
24
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
25
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
26
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
27
|
+
the bug, including your gem version, Ruby version, and operating system.
|
28
|
+
Ideally, a bug report should include a pull request with failing specs.
|
29
|
+
|
30
|
+
[gist]: https://gist.github.com/
|
31
|
+
|
32
|
+
## Submitting a Pull Request
|
33
|
+
1. [Fork the repository.][fork]
|
34
|
+
2. [Create a topic branch.][branch]
|
35
|
+
3. Add specs for your unimplemented feature or bug fix.
|
36
|
+
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
37
|
+
5. Implement your feature or bug fix.
|
38
|
+
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
39
|
+
7. Run `open coverage/index.html`. If your changes are not completely covered
|
40
|
+
by your tests, return to step 3.
|
41
|
+
8. Add, commit, and push your changes.
|
42
|
+
9. [Submit a pull request.][pr]
|
43
|
+
|
44
|
+
[fork]: http://help.github.com/fork-a-repo/
|
45
|
+
[branch]: http://learn.github.com/p/branching.html
|
46
|
+
[pr]: http://help.github.com/send-pull-requests/
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 JohnnyT
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
.coveralls.yml
|
2
|
+
.gitignore
|
3
|
+
.travis.yml
|
4
|
+
CHANGELOG.md
|
5
|
+
CONTRIBUTING.md
|
6
|
+
LICENSE.txt
|
7
|
+
Manifest.txt
|
8
|
+
README.md
|
9
|
+
Rakefile
|
10
|
+
geohashr.gemspec
|
11
|
+
lib/geohashr.rb
|
12
|
+
lib/geohashr/constants.rb
|
13
|
+
lib/geohashr/geohash.rb
|
14
|
+
lib/geohashr/version.rb
|
15
|
+
test/helper.rb
|
16
|
+
test/test_geohashr.rb
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Geohashr
|
2
|
+
[![Version][Version image]][Version]
|
3
|
+
[![Build][Build image]][Build]
|
4
|
+
|
5
|
+
Geohash is a latitude/longitude geocoding system. It is a hierarchical
|
6
|
+
spatial data structure which subdivides space into buckets of grid shape.
|
7
|
+
|
8
|
+
|
9
|
+
### Geohash precision
|
10
|
+
|
11
|
+
Here is some information regarding the precision/length of the geohash string:
|
12
|
+
|
13
|
+
Precision | Size | Rough Size | Width (tiles) | Height (tiles) | Approx # Tiles
|
14
|
+
---|---|---|---|---|---|---|---
|
15
|
+
1 | 5003 km (3109 mi) | Continent | 8 | 4 | 32
|
16
|
+
2 | 625 km (388 mi) | US State; Small country | 32 | 32 | 1k
|
17
|
+
3 | 123 km (76 mi) | US County | 256 | 128 | 32k
|
18
|
+
4 | 19 km (12 mi) | City | 1024 | 1024 | 1.04m
|
19
|
+
5 | 3.8 km (2 mi) | US Postal Code | 8192 | 4096 | 33.55m
|
20
|
+
6 | 610 m (2001 ft) | Neighborhood | 32768 | 32768 | 1.074b
|
21
|
+
7 | 118 m (387 ft) | Street | 262144 | 1310762 | 34.36b
|
22
|
+
8 | 19 m (62 ft) | House / Office | 1048576 | 1048576 | 1.09t
|
23
|
+
9 | 3.7 m (12 ft) | Room | 8388608 | 4194304 | 35.18t
|
24
|
+
10 | 0.6 m (2 ft) | Desk | 33554432 | 33554432 | 1.12quadrillion
|
25
|
+
|
26
|
+
|
27
|
+
### Usefull geohash links
|
28
|
+
|
29
|
+
* [Geohash (slides)](http://www.slideshare.net/sandeepbhaskar2/geohash)
|
30
|
+
* [Geohash in mapping applications (slides)](http://www.slideshare.net/AlexTumanoff/geohash-in-mapping-applications)
|
31
|
+
* [Geohash intro (blog post)](http://www.bigfastblog.com/geohash-intro)
|
32
|
+
* [Geohash explorer](http://openlocation.org/geohash/geohash-js/)
|
33
|
+
* [Another geohash explorer](http://geohash.gofreerange.com/)
|
34
|
+
* [Geohash Wikipedia page](https://en.wikipedia.org/wiki/Geohash)
|
35
|
+
|
36
|
+
|
37
|
+
#### xkcd comic
|
38
|
+
<img src="http://wiki.xkcd.com/wgh/images/5/51/Coordinates.png" alt="xkcd" height="200px" style="width: 200px;"/>
|
39
|
+
|
40
|
+
|
41
|
+
### Contributing
|
42
|
+
|
43
|
+
If you'd like to contribute, that's awesome. There's a guide to contributing
|
44
|
+
(both code and general help) over in [CONTRIBUTING.md](CONTRIBUTING.md)
|
45
|
+
|
46
|
+
|
47
|
+
### Development
|
48
|
+
|
49
|
+
To see what has changed in recent versions, see the [CHANGELOG.md](CHANGELOG.md).
|
50
|
+
|
51
|
+
|
52
|
+
### License
|
53
|
+
|
54
|
+
Copyright 2014 JohnnyT
|
55
|
+
|
56
|
+
Licensed under the MIT License
|
57
|
+
|
58
|
+
[Version]: https://rubygems.org/gems/geohashr
|
59
|
+
[Build]: https://travis-ci.org/johnnyt/geohashr
|
60
|
+
[Coverage]: https://coveralls.io/r/johnnyt/geohashr
|
61
|
+
|
62
|
+
[Version image]: https://badge.fury.io/rb/geohashr.png
|
63
|
+
[Build image]: https://travis-ci.org/johnnyt/geohashr.png
|
64
|
+
[Coverage image]: https://coveralls.io/repos/johnnyt/geohashr/badge.png?branch=master
|
data/Rakefile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require './lib/geohashr/version'
|
4
|
+
require 'hoe' rescue LoadError
|
5
|
+
|
6
|
+
if defined? Hoe
|
7
|
+
|
8
|
+
[ :debug, :gemcutter, :newb, :publish, :signing ].each do |plugin|
|
9
|
+
Hoe.plugins.delete plugin
|
10
|
+
end
|
11
|
+
|
12
|
+
Hoe.plugin :git
|
13
|
+
Hoe.plugin :gemspec
|
14
|
+
|
15
|
+
Hoe.spec 'geohashr' do
|
16
|
+
developer 'JohnnyT', 'ubergeek3141@gmail.com'
|
17
|
+
license 'MIT'
|
18
|
+
|
19
|
+
self.summary = 'GeoHash - encode, decode, neighbors'
|
20
|
+
self.description = 'GeoHash - encode, decode, neighbors'
|
21
|
+
self.urls = ['https://github.com/johnnyt/geohashr']
|
22
|
+
self.history_file = 'CHANGELOG.md'
|
23
|
+
self.readme_file = 'README.md'
|
24
|
+
self.testlib = :minitest
|
25
|
+
|
26
|
+
dependency 'minitest', '~> 5.2', :development
|
27
|
+
dependency 'coveralls', '~> 1.0', :development
|
28
|
+
end
|
29
|
+
|
30
|
+
desc '- Changelog, manifest, new spec'
|
31
|
+
task :prepare => %w[ changelog git:manifest gem:spec ] do
|
32
|
+
puts "Now update the CHANGELOG, then run\n\nrake commit\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
desc '- Create commit, push to origin, tag'
|
37
|
+
task :commit do
|
38
|
+
files = 'CHANGELOG.md lib/geohashr/version.rb geohashr.gemspec'
|
39
|
+
puts "== Adding #{files}"
|
40
|
+
`git add #{files}`
|
41
|
+
puts '== Creating commit'
|
42
|
+
`git commit -m ':checkered_flag: Release #{Geohashr::VERSION}'`
|
43
|
+
puts '== Pushing master to origin'
|
44
|
+
`git push origin master`
|
45
|
+
puts '== Running rake git:tag'
|
46
|
+
Rake::Task['git:tag'].invoke
|
47
|
+
puts "Now run\n\nrake gem && gem push pkg/geohashr-#{Geohashr::VERSION}.gem\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Prepend new changes to CHANGELOG'
|
51
|
+
task :changelog do
|
52
|
+
spec = Gem::Specification::load('geohashr.gemspec')
|
53
|
+
|
54
|
+
changes_io = StringIO.new
|
55
|
+
$stdout = changes_io
|
56
|
+
ENV['VERSION'] = Geohashr::VERSION
|
57
|
+
Rake::Task['git:changelog'].invoke
|
58
|
+
$stdout = STDOUT
|
59
|
+
changes = changes_io.string.gsub('===', '###')
|
60
|
+
|
61
|
+
current = File.read('CHANGELOG.md')
|
62
|
+
File.write('CHANGELOG.md', [changes, current].join)
|
63
|
+
|
64
|
+
puts "Prepended these new changes:\n\n#{changes}"
|
65
|
+
end
|
66
|
+
|
67
|
+
desc 'Console'
|
68
|
+
task :console do
|
69
|
+
sh "irb -rubygems -r ./lib/geohashr"
|
70
|
+
end
|
71
|
+
|
72
|
+
end # has_hoe
|
73
|
+
|
74
|
+
desc 'Setup your local dev environment'
|
75
|
+
task :setup do
|
76
|
+
spec = Gem::Specification::load('geohashr.gemspec')
|
77
|
+
sh "gem install #{spec.dependencies.map{|d| d.name}.join(' ')}"
|
78
|
+
sh "rake install_plugins"
|
79
|
+
end
|
80
|
+
|
81
|
+
# vim: syntax=ruby
|
data/geohashr.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# stub: geohashr 0.1.0.20140214080544 ruby lib
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "geohashr"
|
6
|
+
s.version = "0.1.0.20140214080544"
|
7
|
+
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
|
+
s.require_paths = ["lib"]
|
10
|
+
s.authors = ["JohnnyT"]
|
11
|
+
s.date = "2014-02-14"
|
12
|
+
s.description = "GeoHash - encode, decode, neighbors"
|
13
|
+
s.email = ["ubergeek3141@gmail.com"]
|
14
|
+
s.extra_rdoc_files = ["CHANGELOG.md", "CONTRIBUTING.md", "LICENSE.txt", "Manifest.txt", "README.md"]
|
15
|
+
s.files = [".coveralls.yml", ".gemtest", ".gitignore", ".travis.yml", "CHANGELOG.md", "CONTRIBUTING.md", "LICENSE.txt", "Manifest.txt", "README.md", "Rakefile", "geohashr.gemspec", "lib/geohashr.rb", "lib/geohashr/constants.rb", "lib/geohashr/geohash.rb", "lib/geohashr/version.rb", "test/helper.rb", "test/test_geohashr.rb"]
|
16
|
+
s.homepage = "https://github.com/johnnyt/geohashr"
|
17
|
+
s.licenses = ["MIT"]
|
18
|
+
s.rdoc_options = ["--main", "README.md"]
|
19
|
+
s.rubyforge_project = "geohashr"
|
20
|
+
s.rubygems_version = "2.2.1"
|
21
|
+
s.summary = "GeoHash - encode, decode, neighbors"
|
22
|
+
s.test_files = ["test/test_geohashr.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
s.specification_version = 4
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_development_dependency(%q<minitest>, ["~> 5.2"])
|
29
|
+
s.add_development_dependency(%q<coveralls>, ["~> 1.0"])
|
30
|
+
s.add_development_dependency(%q<hoe>, ["~> 3.8"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<minitest>, ["~> 5.2"])
|
33
|
+
s.add_dependency(%q<coveralls>, ["~> 1.0"])
|
34
|
+
s.add_dependency(%q<hoe>, ["~> 3.8"])
|
35
|
+
end
|
36
|
+
else
|
37
|
+
s.add_dependency(%q<minitest>, ["~> 5.2"])
|
38
|
+
s.add_dependency(%q<coveralls>, ["~> 1.0"])
|
39
|
+
s.add_dependency(%q<hoe>, ["~> 3.8"])
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module GeoHashr
|
2
|
+
BITS = [0x10, 0x08, 0x04, 0x02, 0x01]
|
3
|
+
BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"
|
4
|
+
|
5
|
+
NEIGHBORS = {
|
6
|
+
:right => { :even => "bc01fg45238967deuvhjyznpkmstqrwx", :odd => "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },
|
7
|
+
:left => { :even => "238967debc01fg45kmstqrwxuvhjyznp", :odd => "14365h7k9dcfesgujnmqp0r2twvyx8zb" },
|
8
|
+
:top => { :even => "p0r21436x8zb9dcf5h7kjnmqesgutwvy", :odd => "bc01fg45238967deuvhjyznpkmstqrwx" },
|
9
|
+
:bottom => { :even => "14365h7k9dcfesgujnmqp0r2twvyx8zb", :odd => "238967debc01fg45kmstqrwxuvhjyznp" }
|
10
|
+
}
|
11
|
+
|
12
|
+
BORDERS = {
|
13
|
+
:right => { :even => "bcfguvyz", :odd => "prxz" },
|
14
|
+
:left => { :even => "0145hjnp", :odd => "028b" },
|
15
|
+
:top => { :even => "prxz" , :odd => "bcfguvyz" },
|
16
|
+
:bottom => { :even => "028b" , :odd => "0145hjnp" }
|
17
|
+
}
|
18
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module GeoHashr
|
2
|
+
class GeoHash
|
3
|
+
attr_accessor :geohash, :x, :y, :box
|
4
|
+
|
5
|
+
def self.[](geohash)
|
6
|
+
new(geohash)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(geohash)
|
10
|
+
@geohash = geohash
|
11
|
+
decode
|
12
|
+
end
|
13
|
+
|
14
|
+
def decode
|
15
|
+
@box = [[-90.0, 90.0], [-180.0, 180.0]]
|
16
|
+
is_lng = 1
|
17
|
+
positions = ['','']
|
18
|
+
geohash.downcase.scan(/./) do |c|
|
19
|
+
BITS.each do |mask|
|
20
|
+
bit = (BASE32.index(c) & mask).zero? ? 1 : 0
|
21
|
+
refine = box[is_lng]
|
22
|
+
positions[is_lng] += (bit ^ 1).to_s
|
23
|
+
refine[bit] = (refine[0] + refine[1]) / 2
|
24
|
+
is_lng ^= 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
@x = positions.first.to_i(2)
|
28
|
+
@y = positions.last.to_i(2)
|
29
|
+
@box = box.transpose
|
30
|
+
end
|
31
|
+
|
32
|
+
##########
|
33
|
+
## Encode latitude and longitude into geohash
|
34
|
+
#def self.encode(latitude, longitude, precision=12)
|
35
|
+
# latlng = [latitude, longitude]
|
36
|
+
# points = [[-90.0, 90.0], [-180.0, 180.0]]
|
37
|
+
# is_lng = 1
|
38
|
+
# (0...precision).map {
|
39
|
+
# ch = 0
|
40
|
+
# 5.times do |bit|
|
41
|
+
# mid = (points[is_lng][0] + points[is_lng][1]) / 2
|
42
|
+
# points[is_lng][latlng[is_lng] > mid ? 0 : 1] = mid
|
43
|
+
# ch |= BITS[bit] if latlng[is_lng] > mid
|
44
|
+
# is_lng ^= 1
|
45
|
+
# end
|
46
|
+
# BASE32[ch,1]
|
47
|
+
# }.join
|
48
|
+
#end
|
49
|
+
|
50
|
+
#########
|
51
|
+
# Calculate neighbors (8 adjacents) geohash
|
52
|
+
def neighbors
|
53
|
+
[[:top, :right], [:right, :bottom], [:bottom, :left], [:left, :top]].map{ |dirs|
|
54
|
+
point = adjacent(dirs[0])
|
55
|
+
[point, adjacent(dirs[1], point)]
|
56
|
+
}.flatten
|
57
|
+
end
|
58
|
+
|
59
|
+
def adjacent(dir, source_hash=geohash)
|
60
|
+
source_hash = source_hash.to_s
|
61
|
+
base, lastChr = source_hash[0..-2], source_hash[-1,1]
|
62
|
+
type = (source_hash.length % 2)==1 ? :odd : :even
|
63
|
+
p [source_hash, dir, type, base, lastChr]
|
64
|
+
if BORDERS[dir][type].include?(lastChr)
|
65
|
+
p [:before, dir, base]
|
66
|
+
base = adjacent(dir, base)
|
67
|
+
end
|
68
|
+
|
69
|
+
GeoHash.new(base + BASE32[NEIGHBORS[dir][type].index(lastChr),1])
|
70
|
+
end
|
71
|
+
|
72
|
+
##########
|
73
|
+
## Calculate adjacents geohash
|
74
|
+
#def self.adjacent(geohash, dir)
|
75
|
+
# base, lastChr = geohash[0..-2], geohash[-1,1]
|
76
|
+
# type = (geohash.length % 2)==1 ? :odd : :even
|
77
|
+
# if BORDERS[dir][type].include?(lastChr)
|
78
|
+
# base = adjacent(base, dir)
|
79
|
+
# end
|
80
|
+
|
81
|
+
# GeoHash.new(base + BASE32[NEIGHBORS[dir][type].index(lastChr),1])
|
82
|
+
#end
|
83
|
+
|
84
|
+
def to_s
|
85
|
+
geohash
|
86
|
+
end
|
87
|
+
|
88
|
+
def inspect
|
89
|
+
"#<GeoHash:#{geohash} #{x},#{y} #{box}>"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/geohashr.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
lib_path = File.expand_path '..', __FILE__
|
2
|
+
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include? lib_path
|
3
|
+
|
4
|
+
module GeoHashr
|
5
|
+
def [](geohash)
|
6
|
+
GeoHashr::GeoHash.new(geohash)
|
7
|
+
end
|
8
|
+
alias :decode :[]
|
9
|
+
module_function :[], :decode
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'geohashr/version'
|
13
|
+
require 'geohashr/constants'
|
14
|
+
require 'geohashr/geohash'
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
if ENV['CI']
|
5
|
+
require 'coveralls'
|
6
|
+
Coveralls.wear!
|
7
|
+
else
|
8
|
+
require 'simplecov'
|
9
|
+
SimpleCov.start
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'minitest'
|
13
|
+
require 'minitest/autorun'
|
14
|
+
require 'minitest/pride'
|
15
|
+
require 'rack/test'
|
16
|
+
|
17
|
+
Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require file }
|
18
|
+
require File.expand_path '../../lib/geohashr', __FILE__
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path '../helper', __FILE__
|
2
|
+
|
3
|
+
class TestGeohashr < Minitest::Test
|
4
|
+
def test_truth
|
5
|
+
refute Geohashr::VERSION.nil?
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_decode
|
9
|
+
{
|
10
|
+
'c216ne' => [[45.3680419921875, -121.70654296875], [45.37353515625, -121.695556640625]],
|
11
|
+
'C216Ne' => [[45.3680419921875, -121.70654296875], [45.37353515625, -121.695556640625]],
|
12
|
+
'dqcw4' => [[39.0234375, -76.552734375], [39.0673828125, -76.5087890625]],
|
13
|
+
'DQCW4' => [[39.0234375, -76.552734375], [39.0673828125, -76.5087890625]]
|
14
|
+
}.each do |hash, latlng|
|
15
|
+
assert_equal GeoHashr::GeoHash.new(hash).box, latlng
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#def test_encode
|
20
|
+
# {
|
21
|
+
# [ 45.37, -121.7 ] => 'c216ne',
|
22
|
+
# [ 47.6062095, -122.3320708] => 'c23nb62w20sth',
|
23
|
+
# [ 35.6894875, 139.6917064] => 'xn774c06kdtve',
|
24
|
+
# [-33.8671390, 151.2071140] => 'r3gx2f9tt5sne',
|
25
|
+
# [ 51.5001524, -0.1262362] => 'gcpuvpk44kprq'
|
26
|
+
# }.each do |latlng, hash|
|
27
|
+
# assert_equal GeoHash.encode(latlng[0], latlng[1], hash.length), hash
|
28
|
+
# end
|
29
|
+
#end
|
30
|
+
|
31
|
+
#def test_neighbors
|
32
|
+
# {
|
33
|
+
# 'dqcw5' => ["dqcw7", "dqctg", "dqcw4", "dqcwh", "dqcw6", "dqcwk", "dqctf", "dqctu"],
|
34
|
+
# 'xn774c' => ['xn774f','xn774b','xn7751','xn7749','xn774d','xn7754','xn7750','xn7748'],
|
35
|
+
# 'gcpuvpk' => ['gcpuvps','gcpuvph','gcpuvpm','gcpuvp7','gcpuvpe','gcpuvpt','gcpuvpj','gcpuvp5'],
|
36
|
+
# 'c23nb62w' => ['c23nb62x','c23nb62t','c23nb62y','c23nb62q','c23nb62r','c23nb62z','c23nb62v','c23nb62m']
|
37
|
+
# }.each do |geohash, neighbors|
|
38
|
+
# assert_equal GeoHash.neighbors(geohash).sort, neighbors.sort
|
39
|
+
# end
|
40
|
+
#end
|
41
|
+
|
42
|
+
#def test_adjacent
|
43
|
+
# {
|
44
|
+
# ["dqcjq", :top] => 'dqcjw',
|
45
|
+
# ["dqcjq", :bottom] => 'dqcjn',
|
46
|
+
# ["dqcjq", :left] => 'dqcjm',
|
47
|
+
# ["dqcjq", :right] => 'dqcjr'
|
48
|
+
# }.each do |position, hash|
|
49
|
+
# assert_equal GeoHash.adjacent(*position), hash
|
50
|
+
# end
|
51
|
+
#end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geohashr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JohnnyT
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coveralls
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hoe
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.8'
|
55
|
+
description: GeoHash - encode, decode, neighbors
|
56
|
+
email:
|
57
|
+
- ubergeek3141@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files:
|
61
|
+
- CHANGELOG.md
|
62
|
+
- CONTRIBUTING.md
|
63
|
+
- LICENSE.txt
|
64
|
+
- Manifest.txt
|
65
|
+
- README.md
|
66
|
+
files:
|
67
|
+
- .coveralls.yml
|
68
|
+
- .gemtest
|
69
|
+
- .gitignore
|
70
|
+
- .travis.yml
|
71
|
+
- CHANGELOG.md
|
72
|
+
- CONTRIBUTING.md
|
73
|
+
- LICENSE.txt
|
74
|
+
- Manifest.txt
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- geohashr.gemspec
|
78
|
+
- lib/geohashr.rb
|
79
|
+
- lib/geohashr/constants.rb
|
80
|
+
- lib/geohashr/geohash.rb
|
81
|
+
- lib/geohashr/version.rb
|
82
|
+
- test/helper.rb
|
83
|
+
- test/test_geohashr.rb
|
84
|
+
homepage: https://github.com/johnnyt/geohashr
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options:
|
90
|
+
- --main
|
91
|
+
- README.md
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project: geohashr
|
106
|
+
rubygems_version: 2.2.1
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: GeoHash - encode, decode, neighbors
|
110
|
+
test_files:
|
111
|
+
- test/test_geohashr.rb
|