itudes 0.0.1
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 +7 -0
- data/.document +11 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/README.rdoc +17 -0
- data/Rakefile +26 -0
- data/features/itudes.feature +57 -0
- data/features/step_definitions/itudes_steps.rb +86 -0
- data/features/support/env.rb +5 -0
- data/itudes.gemspec +34 -0
- data/lib/itudes.rb +132 -0
- data/lib/itudes/version.rb +5 -0
- data/spec/itudes_spec.rb +4 -0
- data/spec/spec_helper.rb +11 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: 85204104c51203d27fcbd302843c7d3bad8b3b20
|
4
|
+
data.tar.gz: 80b1ca017577dcafd66b5ecf6d27a1e11df40919
|
5
|
+
!binary "U0hBNTEy":
|
6
|
+
metadata.gz: e9923ee0ffa3eebb134fb6eda622e4c6c03a521e309d8f74d0e82c75b55fdd0a84d9d7657345812bc7dd772429208823b98bcafe90b2cf164430aae2eb2ef6ef
|
7
|
+
data.tar.gz: 33c310c83daf6fb4e1dae50ce48e87cbeb7d09de0ea9bc1e415fccc222aa324c1d542af473356eb756548ad582192463e6fd0c088656ecbad8532c139d60244a
|
data/.document
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# .document is used by rdoc and yard to know how to generate documentation
|
2
|
+
# for example, it can be used to control how rdoc gets built when you do `gem install foo`
|
3
|
+
|
4
|
+
README.rdoc
|
5
|
+
lib/**/*.rb
|
6
|
+
bin/*
|
7
|
+
|
8
|
+
# Files below this - are treated as 'extra files', and aren't parsed for ruby code
|
9
|
+
-
|
10
|
+
features/**/*.feature
|
11
|
+
LICENSE
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Alexei Matyushkin
|
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/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alexei Matyushkin
|
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,39 @@
|
|
1
|
+
# Itudes
|
2
|
+
|
3
|
+
This is a small utitility class to simplify multitude handling in ruby.
|
4
|
+
|
5
|
+
There are few handy things:
|
6
|
+
- `String` class is monkeypatched with `to_itude` method, which does
|
7
|
+
converts to `Float` any of the following strings:
|
8
|
+
- 53.1234565
|
9
|
+
- 53°11′18″N
|
10
|
+
- 53 11 18N
|
11
|
+
- The distance between two points on the Earth is calculated with one
|
12
|
+
single method call.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'itudes'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install itudes
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
itudes = Geo::Itudes.new 53.15, -18.44
|
31
|
+
puts itudes - "53°11′18″N,37°5′18″E"
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= itudes
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2013 Alexei Matyushkin. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
require 'bueller'
|
4
|
+
Bueller::Tasks.new
|
5
|
+
# Bueller::GemcutterTasks.new
|
6
|
+
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
RSpec::Core::RakeTask.new(:examples) do |examples|
|
9
|
+
examples.rspec_opts = '-Ispec'
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
13
|
+
spec.rspec_opts = '-Ispec'
|
14
|
+
spec.rcov = true
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'cucumber/rake/task'
|
18
|
+
Cucumber::Rake::Task.new(:features)
|
19
|
+
|
20
|
+
task :default => :examples
|
21
|
+
|
22
|
+
require 'yard'
|
23
|
+
YARD::Rake::YardocTask.new do |t|
|
24
|
+
t.files = ['lib/**/*.rb', 'features/**/*.feature', 'features/**/*.rb']
|
25
|
+
# t.options = ['--any', '--extra', '--opts'] # optional
|
26
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
Feature: itudes library helps to handle everything against [lat,long]itudes
|
2
|
+
In order to improve developer experience with [lat,long]itude handling
|
3
|
+
A developer simply includes the library and yields class
|
4
|
+
|
5
|
+
Scenario: handy instantiation with floats
|
6
|
+
Given the itudes are instantiated with floats (57.3,37.5)
|
7
|
+
When I call `to_s` method
|
8
|
+
And I call `to_a` method
|
9
|
+
Then the proper string is to be created
|
10
|
+
And the proper array is to be created
|
11
|
+
And the value is considered to be valid
|
12
|
+
|
13
|
+
Scenario: handy instantiation with strings
|
14
|
+
Given the itudes are instantiated with strings (57.3,37.5)
|
15
|
+
When I call `to_s` method
|
16
|
+
And I call `to_a` method
|
17
|
+
Then the proper string is to be created
|
18
|
+
And the proper array is to be created
|
19
|
+
And the value is considered to be valid
|
20
|
+
|
21
|
+
Scenario: handy instantiation with an array
|
22
|
+
Given the itudes are instantiated with an array (57.3,37.5)
|
23
|
+
When I call `to_s` method
|
24
|
+
And I call `to_a` method
|
25
|
+
Then the proper string is to be created
|
26
|
+
And the proper array is to be created
|
27
|
+
And the value is considered to be valid
|
28
|
+
|
29
|
+
Scenario: handy instantiation with string
|
30
|
+
Given the itudes are instantiated with a string (53°11′18″N,37°5′18″E)
|
31
|
+
When I call `to_s` method
|
32
|
+
And I call `to_a` method
|
33
|
+
Then the proper string is to be created
|
34
|
+
And the proper array is to be created
|
35
|
+
And the value is considered to be valid
|
36
|
+
And the rounded value should equal to (53.0,37.0)
|
37
|
+
|
38
|
+
Scenario: invalid instantiation
|
39
|
+
Given the itudes are instantiated with a string (foo:bad)
|
40
|
+
Then the value is not considered to be valid
|
41
|
+
|
42
|
+
Scenario: distance between two points
|
43
|
+
Given the itudes are instantiated with a string (53°11′18″N,37°5′18″E)
|
44
|
+
When the other itudes are instantiated with an array (58 38 38N,003 04 12W)
|
45
|
+
Then the value is considered to be valid
|
46
|
+
And the distance equals to 2533 km
|
47
|
+
And the distance in miles equals to 1574 mi
|
48
|
+
|
49
|
+
Scenario: distance between two points with lazy Itudes instantiation
|
50
|
+
Given the itudes are instantiated with a string (53°11′18″N,37°5′18″E)
|
51
|
+
When the distance is calculated by implicit “- "58 38 38N,003 04 12W"”
|
52
|
+
Then the calculated distance equals to 2533 km
|
53
|
+
|
54
|
+
Scenario: distance between two points thru class method
|
55
|
+
Given the itudes are given with a string (53°11′18″N,37°5′18″E)
|
56
|
+
And the other itudes are given with a string (58 38 38N,003 04 12W)
|
57
|
+
Then the classs method gives distance equals to 2533 km
|
@@ -0,0 +1,86 @@
|
|
1
|
+
Given(/^the itudes are instantiated with strings \((\d+\.\d+),(\d+\.\d+)\)$/) do |lat, lon|
|
2
|
+
@lat, @lon = lat, lon
|
3
|
+
@itudes = Geo::Itudes.new lat.to_s, lon.to_s
|
4
|
+
end
|
5
|
+
|
6
|
+
Given(/^the itudes are instantiated with floats \((\d+\.\d+),(\d+\.\d+)\)$/) do |lat, lon|
|
7
|
+
@lat, @lon = lat, lon
|
8
|
+
@itudes = Geo::Itudes.new lat.to_f, lon.to_f
|
9
|
+
end
|
10
|
+
|
11
|
+
Given(/^the itudes are instantiated with an array \((\d+\.\d+),(\d+\.\d+)\)$/) do |lat, lon|
|
12
|
+
@lat, @lon = lat, lon
|
13
|
+
@itudes = Geo::Itudes.new [lat, lon]
|
14
|
+
end
|
15
|
+
|
16
|
+
Given(/^the itudes are instantiated with a string \((\d+°\d+′\d+″N),(\d+°\d+′\d+″E)\)$/) do |lat, lon|
|
17
|
+
@lat, @lon = lat, lon
|
18
|
+
@itudes = Geo::Itudes.new "#{@lat},#{@lon}"
|
19
|
+
end
|
20
|
+
|
21
|
+
Given(/^the itudes are instantiated with a string \(([a-z]+):([a-z]+)\)$/) do |lat, lon|
|
22
|
+
@lat, @lon = lat, lon
|
23
|
+
@itudes = Geo::Itudes.new "#{@lat},#{@lon}"
|
24
|
+
end
|
25
|
+
|
26
|
+
When(/^I call `to_s` method$/) do
|
27
|
+
@itudes_to_s = @itudes.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
When(/^I call `to_a` method$/) do
|
31
|
+
@itudes_to_a = @itudes.to_a
|
32
|
+
end
|
33
|
+
|
34
|
+
Then(/^the proper string is to be created$/) do
|
35
|
+
@itudes_to_s.should == "#{@lat.to_itude},#{@lon.to_itude}"
|
36
|
+
end
|
37
|
+
|
38
|
+
Then(/^the proper array is to be created$/) do
|
39
|
+
@itudes_to_a.should == [@lat.to_itude.to_f, @lon.to_itude.to_f]
|
40
|
+
end
|
41
|
+
|
42
|
+
Then(/^the value is considered to be valid$/) do
|
43
|
+
@itudes.valid?.should == true
|
44
|
+
end
|
45
|
+
|
46
|
+
Then(/^the value is not considered to be valid$/) do
|
47
|
+
@itudes.valid?.should == false
|
48
|
+
end
|
49
|
+
|
50
|
+
When(/^the other itudes are instantiated with an array \((\d+ \d+ \d+N),(\d+ \d+ \d+W)\)$/) do |lat, lon|
|
51
|
+
@itudes_other_km = Geo::Itudes.new lat, lon
|
52
|
+
@itudes_other_mi = Geo::Itudes.new(lat, lon).miles!
|
53
|
+
end
|
54
|
+
|
55
|
+
Then(/^the distance equals to (\d+) km$/) do |dist_km|
|
56
|
+
(@itudes_other_km - @itudes).to_i.should == dist_km.to_i
|
57
|
+
end
|
58
|
+
|
59
|
+
Then(/^the distance in miles equals to (\d+) mi$/) do |dist_mi|
|
60
|
+
(@itudes_other_mi - @itudes).to_i.should == dist_mi.to_i
|
61
|
+
end
|
62
|
+
|
63
|
+
Given(/^the itudes are given with a string \((.+)\)$/) do |i1|
|
64
|
+
@i1 = i1
|
65
|
+
end
|
66
|
+
|
67
|
+
Given(/^the other itudes are given with a string \((.+)\)$/) do |i2|
|
68
|
+
@i2 = i2
|
69
|
+
end
|
70
|
+
|
71
|
+
Then(/^the classs method gives distance equals to (\d+) km$/) do |dist_km|
|
72
|
+
Geo::Itudes.distance(@i2, @i1).to_i.should == dist_km.to_i
|
73
|
+
end
|
74
|
+
|
75
|
+
Then(/^the rounded value should equal to \((.+)\)$/) do |rounded|
|
76
|
+
@itudes.round.should == Geo::Itudes.new(rounded)
|
77
|
+
@itudes.round.should == rounded
|
78
|
+
end
|
79
|
+
|
80
|
+
When(/^the distance is calculated by implicit “\- "(.*?)"”$/) do |other|
|
81
|
+
@calc_dist = @itudes - other
|
82
|
+
end
|
83
|
+
|
84
|
+
Then(/^the calculated distance equals to (\d+) km$/) do |dist|
|
85
|
+
dist.to_i.should == @calc_dist.to_i
|
86
|
+
end
|
data/itudes.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'itudes/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'itudes'
|
6
|
+
s.version = Geo::Itudes::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.date = '2013-08-25'
|
9
|
+
s.authors = ['Alexei Matyushkin']
|
10
|
+
s.email = 'am@mudasobwa.ru'
|
11
|
+
s.homepage = 'http://github.com/mudasobwa/itudes'
|
12
|
+
s.summary = %Q{Small utility library to work with [lat,lang]itudes}
|
13
|
+
s.description = %Q{Utility library to simplify dealing with multitudes}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
'LICENSE',
|
16
|
+
'README.rdoc',
|
17
|
+
]
|
18
|
+
|
19
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.7')
|
20
|
+
s.rubygems_version = '1.3.7'
|
21
|
+
s.specification_version = 3
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ['lib']
|
27
|
+
|
28
|
+
s.add_development_dependency 'rspec'
|
29
|
+
s.add_development_dependency 'yard'
|
30
|
+
s.add_development_dependency 'cucumber'
|
31
|
+
s.add_development_dependency 'yard-cucumber'
|
32
|
+
s.add_development_dependency 'bueller'
|
33
|
+
end
|
34
|
+
|
data/lib/itudes.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# @author Alexei Matyushkin
|
4
|
+
|
5
|
+
require "itudes/version"
|
6
|
+
|
7
|
+
class String
|
8
|
+
# Monkeypatches {String} class to parse strings representing multitudes.
|
9
|
+
# Accepts the following formats:
|
10
|
+
# - 53.1234565
|
11
|
+
# - 53°11′18″N
|
12
|
+
# - 53 11 18N
|
13
|
+
#
|
14
|
+
# @param strict [Boolean] the strictness of convertion (the method raises
|
15
|
+
# an exception if set to _true_
|
16
|
+
# @return [Float] the multitude or `nil` if the convertion was not possible.
|
17
|
+
def to_itude strict = false
|
18
|
+
case self
|
19
|
+
when /^(?<val>[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)$/ # -53.133333
|
20
|
+
$~[:val].to_f
|
21
|
+
when /(?<deg>\d+)[°\s]*(?<min>\d*)['’′\s]*(?<sec>\d*)["”″\s]*(?<ss>[NESWnesw]?)$/ # 53°11′18″N, 000°08′00″E
|
22
|
+
f = $~[:deg].to_f + $~[:min].to_f / 60.0 + $~[:sec].to_f / 3600.0
|
23
|
+
($~[:ss].to_s.downcase =~ /[sw]/).nil? ? f : -f
|
24
|
+
else
|
25
|
+
strict ? raise(TypeError.new "no implicit conversion of string “#{self}” to [Lat,Long]itude") : nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module Geo
|
31
|
+
# Convenience class to operate w/ multitudes.
|
32
|
+
class Itudes
|
33
|
+
attr_reader :latitude, :longitude, :units
|
34
|
+
# Earth radius in different measurement units (needed to calc distance
|
35
|
+
# between two points on the Earth.)
|
36
|
+
RADIUS = {
|
37
|
+
:km => 6_371, # km
|
38
|
+
:mi => 3_959 # mi
|
39
|
+
}
|
40
|
+
# Constructs the {Geo::Itudes} instance.
|
41
|
+
# If there are two parameters, they may be either strings or floats (or
|
42
|
+
# string and float.) If there is the only param, it may be one of the
|
43
|
+
# following:
|
44
|
+
# - {Itudes} — produces a dup copy of it,
|
45
|
+
# - {String} — tries to parse string (see String#to_itude)
|
46
|
+
# - {Array} — tries to parse elements
|
47
|
+
#
|
48
|
+
# @param v1 either latitude or one of possible multitudes representations
|
49
|
+
# @param v2 longitude (if given)
|
50
|
+
# @return [Itudes] the multitudes object
|
51
|
+
def initialize v1, v2 = nil
|
52
|
+
@latitude, @longitude = (v2.nil? ?
|
53
|
+
case v1
|
54
|
+
when Itudes then [v1.latitude, v1.longitude]
|
55
|
+
when String then v1.split(',').map(&:to_itude)
|
56
|
+
when Array then v1.map { |m| Itudes.tudify m }
|
57
|
+
end :
|
58
|
+
[Itudes.tudify(v1), Itudes.tudify(v2)]).map(&:to_f)
|
59
|
+
kilometers!
|
60
|
+
end
|
61
|
+
# Sets the internal measurement to miles (see #distance)
|
62
|
+
def miles!
|
63
|
+
@units = :mi
|
64
|
+
self
|
65
|
+
end
|
66
|
+
# Sets the internal measurement to kilometers (see #distance)
|
67
|
+
def kilometers!
|
68
|
+
@units = :km
|
69
|
+
self
|
70
|
+
end
|
71
|
+
# Compares against another instance.
|
72
|
+
# @return [Boolean] _true_ if other value represents the same
|
73
|
+
# multitudes values, _false_ otherwise
|
74
|
+
def == other
|
75
|
+
other = Itudes.new other
|
76
|
+
(@latitude == other.latitude) && (@longitude == other.longitude)
|
77
|
+
end
|
78
|
+
# String representation of multitudes.
|
79
|
+
# @return [String] in the form "53.121231231, -18.43534656"
|
80
|
+
def to_s
|
81
|
+
"#{@latitude},#{@longitude}"
|
82
|
+
end
|
83
|
+
# Array representation of multitudes.
|
84
|
+
# @return [Array] [@latitude, @longitude]
|
85
|
+
def to_a
|
86
|
+
[@latitude, @longitude]
|
87
|
+
end
|
88
|
+
# Calculates the nearest [lat,long] location, basing in the value of
|
89
|
+
# parameter. E. g. for [53.121231231, -18.4353465] will return [53.0, -18.5].
|
90
|
+
# It might be useful if we need to round multitudes to present them.
|
91
|
+
# @param slice [Number] the “modulo” to calculate nearest “rounded” value
|
92
|
+
# @return [Itudes] the rounded value
|
93
|
+
def round slice = 0.5
|
94
|
+
Itudes.new @latitude - @latitude.modulo(slice), @longitude - @longitude.modulo(slice)
|
95
|
+
end
|
96
|
+
# Checks if the multitudes behind represent the correct place on the Earth.
|
97
|
+
# @return [Boolean] _true_ if the multitudes are OK
|
98
|
+
def valid?
|
99
|
+
!@latitude.nil? && !@longitude.nil? && !(@latitude.zero? && @longitude.zero?) && \
|
100
|
+
@latitude > -90 && @latitude < 90 && @longitude > -90 && @longitude < 90
|
101
|
+
end
|
102
|
+
# Calculates distance between two points on the Earth.
|
103
|
+
# @param other the place on the Earth to calculate distance to
|
104
|
+
# @return [Float] the distance between two places on the Earth
|
105
|
+
def distance other
|
106
|
+
o = Itudes.new other
|
107
|
+
raise ArgumentError.new "operand must be lat-/longitudable" if (o.latitude.nil? || o.longitude.nil?)
|
108
|
+
|
109
|
+
dlat = Itudes.radians(o.latitude - @latitude)
|
110
|
+
dlon = Itudes.radians(o.longitude - @longitude)
|
111
|
+
lat1 = Itudes.radians(@latitude)
|
112
|
+
lat2 = Itudes.radians(o.latitude);
|
113
|
+
|
114
|
+
a = Math::sin(dlat/2)**2 + Math::sin(dlon/2)**2 * Math::cos(lat1) * Math::cos(lat2)
|
115
|
+
(RADIUS[@units] * 2.0 * Math::atan2(Math.sqrt(a), Math.sqrt(1-a))).abs
|
116
|
+
end
|
117
|
+
alias :- :distance
|
118
|
+
|
119
|
+
# Calculates distance between two points on the Earth. Convenient method.
|
120
|
+
def self.distance start, finish
|
121
|
+
Itudes.new(start) - Itudes.new(finish)
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
def self.tudify val #:nodoc:
|
126
|
+
String === val ? val.to_itude : val
|
127
|
+
end
|
128
|
+
def self.radians degrees #:nodoc:
|
129
|
+
Math::PI * degrees / 180
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/spec/itudes_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
require 'itudes'
|
4
|
+
|
5
|
+
# Requires supporting files with custom matchers and macros, etc,
|
6
|
+
# in ./support/ and its subdirectories.
|
7
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itudes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexei Matyushkin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: yard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cucumber
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard-cucumber
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bueller
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Utility library to simplify dealing with multitudes
|
84
|
+
email: am@mudasobwa.ru
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE
|
89
|
+
- README.rdoc
|
90
|
+
files:
|
91
|
+
- .document
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- README.rdoc
|
98
|
+
- Rakefile
|
99
|
+
- features/itudes.feature
|
100
|
+
- features/step_definitions/itudes_steps.rb
|
101
|
+
- features/support/env.rb
|
102
|
+
- itudes.gemspec
|
103
|
+
- lib/itudes.rb
|
104
|
+
- lib/itudes/version.rb
|
105
|
+
- spec/itudes_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
homepage: http://github.com/mudasobwa/itudes
|
108
|
+
licenses: []
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 1.3.7
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.0.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Small utility library to work with [lat,lang]itudes
|
130
|
+
test_files: []
|
131
|
+
has_rdoc:
|