csquares 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/README.rdoc +50 -0
- data/Rakefile +27 -0
- data/lib/csquares.rb +9 -0
- data/lib/csquares/core_ext.rb +34 -0
- data/lib/csquares/csquares.rb +70 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_csquares.rb +17 -0
- metadata +86 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= csquares
|
2
|
+
|
3
|
+
http://github.com/broughcut/csquares
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
http://www.cmar.csiro.au/csquares/csq-faq.htm
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
|
12
|
+
== SYNOPSIS:
|
13
|
+
|
14
|
+
csquare = CSquare.new(38.8894, -77.0356)
|
15
|
+
csquare.code
|
16
|
+
csquare.digits
|
17
|
+
csquare.sq(0.01)
|
18
|
+
|
19
|
+
== REQUIREMENTS:
|
20
|
+
|
21
|
+
Ruby 1.8
|
22
|
+
|
23
|
+
== INSTALL:
|
24
|
+
|
25
|
+
sudo gem install csquares
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
(The MIT License)
|
30
|
+
|
31
|
+
Copyright (c) 2008 Jamie Brough
|
32
|
+
|
33
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
34
|
+
a copy of this software and associated documentation files (the
|
35
|
+
'Software'), to deal in the Software without restriction, including
|
36
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
37
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
38
|
+
permit persons to whom the Software is furnished to do so, subject to
|
39
|
+
the following conditions:
|
40
|
+
|
41
|
+
The above copyright notice and this permission notice shall be
|
42
|
+
included in all copies or substantial portions of the Software.
|
43
|
+
|
44
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
45
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
46
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
47
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
48
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
49
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
50
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/csquares'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('csquares', CSquares::VERSION) do |p|
|
7
|
+
p.developer('Jamie Brough', 'broughcut@gmail.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.rubyforge_name = p.name # TODO this is default value
|
10
|
+
# p.extra_deps = [
|
11
|
+
# ['activesupport','>= 2.0.2'],
|
12
|
+
# ]
|
13
|
+
p.extra_dev_deps = [
|
14
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
15
|
+
]
|
16
|
+
|
17
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
18
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
19
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
20
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
24
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
25
|
+
|
26
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
27
|
+
# task :default => [:spec, :features]
|
data/lib/csquares.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/6663
|
4
|
+
|
5
|
+
def interleave(enum2)
|
6
|
+
e1, e2, res = to_a.reverse, enum2.to_a.reverse, []
|
7
|
+
res.push e1.pop, e2.pop until e1.empty? or e2.empty?
|
8
|
+
res + (e1 + e2) .reverse
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Array
|
13
|
+
|
14
|
+
# http://blog.jayfields.com/2007/09/ruby-arraychunk.html
|
15
|
+
|
16
|
+
def chunk(number_of_chunks)
|
17
|
+
chunks = Array.new(number_of_chunks) { [] }
|
18
|
+
count = 0
|
19
|
+
self.each do |e|
|
20
|
+
chunks[count] << e
|
21
|
+
count = (count < number_of_chunks-1) ? count + 1 : 0
|
22
|
+
end
|
23
|
+
chunks
|
24
|
+
end
|
25
|
+
|
26
|
+
def chunk_into(max_size_of_array)
|
27
|
+
chunks = [[]]
|
28
|
+
self.each do |e|
|
29
|
+
chunks << [] unless chunks.last.length < max_size_of_array
|
30
|
+
chunks.last << e
|
31
|
+
end
|
32
|
+
chunks
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class CSquare
|
2
|
+
|
3
|
+
attr_reader :lat, :lng, :code, :digits
|
4
|
+
|
5
|
+
KM = {10 => 0.1, 50 => 0.5, 100 => 1, 500 => 5, 1000 => 10}
|
6
|
+
|
7
|
+
def initialize(lat,lng)
|
8
|
+
@lat, @lng = lat.to_f,lng.to_f
|
9
|
+
digits = pad_and_interleave.insert(0,global_quadrant)
|
10
|
+
chunks = chunk_up(digits)
|
11
|
+
chunks[1..-1].each {|it| it.insert(0,intermediate_quadrant(it))}
|
12
|
+
@code = delimit(chunks)
|
13
|
+
@digits = chunks.join.split("").map {|it| it.to_i}
|
14
|
+
end
|
15
|
+
|
16
|
+
def sq(i=0.1,int=false)
|
17
|
+
n = 6 + (3 * Math.log10(i).abs).ceil
|
18
|
+
it = case i
|
19
|
+
when 10 then @digits[0..3]
|
20
|
+
when 5 then @digits[0..4]
|
21
|
+
when 1 then @digits[0..6]
|
22
|
+
else @digits[0..n]
|
23
|
+
end
|
24
|
+
result = delimit [it[0..3],*it[4..-1].chunk_into(3)]
|
25
|
+
int ? result.gsub(/:/){}.to_i : result
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def global_quadrant
|
31
|
+
if lat >= 0
|
32
|
+
lng >= 0 ? 1 : 7
|
33
|
+
else
|
34
|
+
lng >= 0 ? 3 : 5
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def intermediate_quadrant(q)
|
39
|
+
a = (0..4).include?(q.first)
|
40
|
+
b = (0..4).include?(q.last)
|
41
|
+
if a && b then 1
|
42
|
+
elsif a && !b then 2
|
43
|
+
elsif !a && b then 3
|
44
|
+
else 4 end
|
45
|
+
end
|
46
|
+
|
47
|
+
def pad_and_interleave
|
48
|
+
a,b = [lat,lng].map {|it| it.to_f.abs.to_s.split(".")}
|
49
|
+
[a,b].each_with_index do |it,i|
|
50
|
+
it.first.insert(0,"0") while it.first.size <= i+1
|
51
|
+
if i == 1
|
52
|
+
it[0] = it.first[0,2],it.first[2,2]
|
53
|
+
else
|
54
|
+
it[0] = it.first.split("")
|
55
|
+
end
|
56
|
+
it[1] = it.last.split("")
|
57
|
+
it.flatten!
|
58
|
+
end
|
59
|
+
a.interleave(b).join.split("").map {|it| it.to_i}
|
60
|
+
end
|
61
|
+
|
62
|
+
def chunk_up(digits)
|
63
|
+
chunks = digits.chunk_into(2)
|
64
|
+
chunks = [[chunks.first,chunks[1]].flatten,*chunks[2..-1]]
|
65
|
+
end
|
66
|
+
|
67
|
+
def delimit(chunks)
|
68
|
+
chunks.map {|it| it.join("")}.join(":").sub(/:$/){}
|
69
|
+
end
|
70
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/csquares.rb'}"
|
9
|
+
puts "Loading csquares gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/csquares'
|
3
|
+
|
4
|
+
class TestCSquares < Test::Unit::TestCase
|
5
|
+
def test_squares
|
6
|
+
assert_equal "7307:487:380:383:495:2", CSquare.new(38.8894,-77.0356).sq(0.0005)
|
7
|
+
assert_equal "7307:487:380:383:495", CSquare.new(38.8894,-77.0356).sq(0.001)
|
8
|
+
assert_equal "7307:487:380:383:4", CSquare.new(38.8894,-77.0356).sq(0.005)
|
9
|
+
assert_equal "7307:487:380:383", CSquare.new(38.8894,-77.0356).sq(0.01)
|
10
|
+
assert_equal "7307:487:380:3", CSquare.new(38.8894,-77.0356).sq(0.05)
|
11
|
+
assert_equal "7307:487:380", CSquare.new(38.8894,-77.0356).sq(0.1)
|
12
|
+
assert_equal "7307:487:3", CSquare.new(38.8894,-77.0356).sq(0.5)
|
13
|
+
assert_equal "7307:487", CSquare.new(38.8894,-77.0356).sq(1)
|
14
|
+
assert_equal "7307:4", CSquare.new(38.8894,-77.0356).sq(5)
|
15
|
+
assert_equal "7307", CSquare.new(38.8894,-77.0356).sq(10)
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csquares
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jamie Brough
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-22 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.0
|
34
|
+
version:
|
35
|
+
description: http://www.cmar.csiro.au/csquares/csq-faq.htm
|
36
|
+
email:
|
37
|
+
- broughcut@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- History.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- lib/csquares.rb
|
52
|
+
- lib/csquares/csquares.rb
|
53
|
+
- lib/csquares/core_ext.rb
|
54
|
+
- script/console
|
55
|
+
- script/destroy
|
56
|
+
- script/generate
|
57
|
+
- test/test_csquares.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/broughcut/csquares
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --main
|
63
|
+
- README.rdoc
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: csquares
|
81
|
+
rubygems_version: 1.3.1
|
82
|
+
signing_key:
|
83
|
+
specification_version: 2
|
84
|
+
summary: http://www.cmar.csiro.au/csquares/csq-faq.htm
|
85
|
+
test_files:
|
86
|
+
- test/test_csquares.rb
|