latitude 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.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p545
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ - "2.1.1"
6
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in latitude.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Trey Springer
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,50 @@
1
+ # Latitude
2
+
3
+ [![Build
4
+ Status](https://travis-ci.org/umtrey/latitude-gem.svg?branch=master)](https://travis-ci.org/umtrey/latitude-gem)
5
+
6
+ Latitude is a simple gem for calculating distances and headings between two
7
+ geographic locations, using great circle math.
8
+
9
+ For now, this gem uses the WGS84 measurements for the shape of the
10
+ earth.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'latitude'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install latitude
25
+
26
+ ## Usage
27
+
28
+ Note that coordinates are positive for N/E and negative for S/W.
29
+
30
+ `Latitude.great_circle_distance(start_latitude, start_longitude, end_latitude,
31
+ end_longitude)`
32
+
33
+ Calculates the great circle distance in kilometers between two
34
+ coordinates.
35
+
36
+ ```
37
+ Latitude.initial_heading(start_latitude, start_longitude, end_latitude, end_longitude)
38
+ Latitude.final_heading(start_latitude, start_longitude, end_latitude, end_longitude)
39
+ ```
40
+
41
+ Calculates the initial and final headings if traveling between two
42
+ points using a great circle path.
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/[my-github-username]/latitude/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/latitude.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'latitude/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "latitude"
8
+ spec.version = Latitude::VERSION
9
+ spec.authors = ["Trey Springer"]
10
+ spec.email = ["dfsiii@gmail.com"]
11
+ spec.summary = %q{Calculates distances between two geographic coordinates.}
12
+ spec.description = %q{Uses the great-circle distance calculation to determine the distance between two locations with just latitudes and longitudes.}
13
+ spec.homepage = "http://www.treyspringer.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module Latitude
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,112 @@
1
+ # thanks to http://www.movable-type.co.uk/scripts/latlong-vincenty.html
2
+
3
+ module Vincenty
4
+ extend self
5
+
6
+ # in meters
7
+ WGS84_A = 6378137
8
+ WGS84_B = 6356752.314245
9
+ WGS84_F = 1 / 298.257223563
10
+
11
+ def great_circle_distance(start_lat, start_long, end_lat, end_long)
12
+ return 0 if (start_lat == end_lat) && (start_long == end_long)
13
+
14
+ solution_set(start_lat, start_long, end_lat, end_long)[:distance]
15
+ end
16
+
17
+ def initial_bearing(start_lat, start_long, end_lat, end_long)
18
+ return nil if (start_lat == end_lat) && (start_long == end_long)
19
+
20
+ solution_set(start_lat, start_long, end_lat, end_long)[:initial_bearing]
21
+ end
22
+
23
+ def final_bearing(start_lat, start_long, end_lat, end_long)
24
+ return nil if (start_lat == end_lat) && (start_long == end_long)
25
+
26
+ solution_set(start_lat, start_long, end_lat, end_long)[:final_bearing]
27
+ end
28
+
29
+ private
30
+ def solution_set(start_lat, start_long, end_lat, end_long)
31
+ a = WGS84_A
32
+ b = WGS84_B
33
+ f = WGS84_F
34
+
35
+ phi_1 = to_radians(start_lat)
36
+ lambda_1 = to_radians(start_long)
37
+ phi_2 = to_radians(end_lat)
38
+ lambda_2 = to_radians(end_long)
39
+
40
+ iterative_solver(phi_1, lambda_1,
41
+ phi_2, lambda_2,
42
+ a, b, f)
43
+ end
44
+
45
+ def iterative_solver(phi_1, lambda_1, phi_2, lambda_2, a, b, f)
46
+ l = lambda_2 - lambda_1
47
+ sin_u1, cos_u1, tan_u1 = get_trig_trio(phi_1, f)
48
+ sin_u2, cos_u2, tan_u2 = get_trig_trio(phi_2, f)
49
+
50
+ lam = l
51
+ iterations = 0
52
+
53
+ begin
54
+ sin_lam = Math.sin(lam)
55
+ cos_lam = Math.cos(lam)
56
+
57
+ sin_sigma = Math.sqrt((cos_u2 * sin_lam)**2 + ((cos_u1 * sin_u2) - (sin_u1 * cos_u2 * cos_lam))**2)
58
+ return 0 if sin_sigma == 0 # co-incident points
59
+
60
+ cos_sigma = (sin_u1 * sin_u2) + (cos_u1 * cos_u2 * cos_lam)
61
+ sigma = Math.atan2(sin_sigma, cos_sigma)
62
+
63
+ sin_alpha = cos_u1 * cos_u2 * sin_lam / sin_sigma
64
+ cos_sq_alpha = 1 - sin_alpha**2
65
+
66
+ if cos_sq_alpha == 0
67
+ cos_2sigma_m = 0
68
+ else
69
+ cos_2sigma_m = cos_sigma - 2 * sin_u1 * sin_u2 / cos_sq_alpha
70
+ end
71
+
72
+ c = f/16 * cos_sq_alpha * (4 + f*(4 - 3*cos_sq_alpha))
73
+ lam_prime = lam
74
+ lam = l + (1 - c) * f * sin_alpha *
75
+ (sigma + c * sin_sigma * (cos_2sigma_m + c * cos_sigma * (-1 + 2 * cos_2sigma_m**2)))
76
+ end while ((lam - lam_prime).abs >= 1e-12) && ((iterations += 1) < 100)
77
+
78
+ raise Exception("Failed to converge") if iterations >= 100
79
+
80
+ u_sq = cos_sq_alpha * (a**2 - b**2) / (b**2)
81
+ big_a = 1 + u_sq/16384 * (4096 + u_sq * (-768 + u_sq * (320 - 175 * u_sq)))
82
+ big_b = u_sq/1024 * (256 + u_sq * (-128 + u_sq * (74-47 * u_sq)))
83
+ delta_sigma = big_b * sin_sigma * (cos_2sigma_m + big_b/4 * (cos_sigma * (-1 + 2*(cos_2sigma_m**2)) -
84
+ big_b/6 * cos_2sigma_m * (-3 + 4*(sin_sigma**2)) * (-3 + 4*(cos_sigma**2))))
85
+
86
+ distance = (b * big_a * (sigma - delta_sigma)).round(3) # 1mm precision
87
+ fwd_az = Math.atan2(cos_u2 * sin_lam, (cos_u1 * sin_u2) - (sin_u1 * cos_u2 * cos_lam))
88
+ rev_az = Math.atan2(cos_u1 * sin_lam, (cos_u1 * sin_u2 * cos_lam) - (sin_u1 * cos_u2))
89
+
90
+ # var fwdAz = Math.atan2(cosU2*sinλ, cosU1*sinU2-sinU1*cosU2*cosλ);
91
+ # var revAz = Math.atan2(cosU1*sinλ, -sinU1*cosU2+cosU1*sinU2*cosλ);
92
+ # return { distance: s, initialBearing: fwdAz.toDegrees(), finalBearing: revAz.toDegrees() };
93
+ { :distance => distance,
94
+ :initial_bearing => to_degrees(fwd_az),
95
+ :final_bearing => to_degrees(rev_az) }
96
+ end
97
+
98
+ def to_radians(degrees)
99
+ degrees * Math::PI / 180
100
+ end
101
+
102
+ def to_degrees(rads)
103
+ rads * 180 / Math::PI
104
+ end
105
+
106
+ def get_trig_trio(rads, f)
107
+ tan = (1-f) * Math.tan(rads)
108
+ cos = 1 / Math.sqrt(1 + tan**2)
109
+ sin = tan * cos
110
+ [sin, cos, tan]
111
+ end
112
+ end
data/lib/latitude.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "latitude/version"
2
+ require "latitude/vincenty"
3
+
4
+ module Latitude
5
+ extend self
6
+ def great_circle_distance(start_latitude, start_longitude, end_latitude, end_longitude)
7
+ # in kilometers
8
+ m_distance = Vincenty.great_circle_distance(start_latitude, start_longitude,
9
+ end_latitude, end_longitude)
10
+
11
+ return m_distance / 1000.0
12
+ end
13
+
14
+ def initial_bearing(start_latitude, start_longitude, end_latitude, end_longitude)
15
+ Vincenty.initial_bearing(start_latitude, start_longitude,
16
+ end_latitude, end_longitude)
17
+ end
18
+
19
+ def final_bearing(start_latitude, start_longitude, end_latitude, end_longitude)
20
+ Vincenty.final_bearing(start_latitude, start_longitude,
21
+ end_latitude, end_longitude)
22
+ end
23
+
24
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Latitude do
4
+ describe "#great_circle_distance" do
5
+ it "should return 0 if both coordinates are equal" do
6
+ expect(Latitude.great_circle_distance(0,0,0,0)).to eq(0)
7
+ end
8
+
9
+ it "should use the vincenty formulae to calculate the great-circle distance between two coordinates" do
10
+ expect(Latitude.great_circle_distance(50, -5, 58, -3)).to be_within(0.001).of(899.937706)
11
+ expect(Latitude.great_circle_distance(50, -5, -58, 3)).to be_within(0.001).of(11994.498924)
12
+ expect(Latitude.great_circle_distance(10, 10, 50, 50)).to be_within(0.001).of(5758.331041)
13
+ end
14
+ end
15
+
16
+ describe "#initial_bearing" do
17
+ it "should return nil if both coordinates are equal" do
18
+ expect(Latitude.initial_bearing(0,0,0,0)).to be_nil
19
+ end
20
+
21
+ it "should use the vincenty formulae to calculate the initial bearing from one point to another along the great circle route" do
22
+ expect(Latitude.initial_bearing(50, -5, 58, -3)).to be_within(0.001).of(7.575056)
23
+ expect(Latitude.initial_bearing(50, -5, -58, 3)).to be_within(0.001).of(175.531128)
24
+ expect(Latitude.initial_bearing(10, 10, 50, 50)).to be_within(0.001).of(31.830619)
25
+ end
26
+ end
27
+
28
+ describe "#final_bearing" do
29
+ it "should return nil if both coordinates are equal" do
30
+ expect(Latitude.initial_bearing(0,0,0,0)).to be_nil
31
+ end
32
+
33
+ it "should use the vincenty formulae to calculate the initial bearing from one point to another along the great circle route" do
34
+ expect(Latitude.final_bearing(50, -5, 58, -3)).to be_within(0.001).of(9.197103)
35
+ expect(Latitude.final_bearing(50, -5, -58, 3)).to be_within(0.001).of(174.579117)
36
+ expect(Latitude.final_bearing(10, 10, 50, 50)).to be_within(0.001).of(53.758428)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,80 @@
1
+ require 'latitude'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, make a
12
+ # separate helper file that requires this one and then use it only in the specs
13
+ # that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # The settings below are suggested to provide a good initial experience
21
+ # with RSpec, but feel free to customize to your heart's content.
22
+ =begin
23
+ # These two settings work together to allow you to limit a spec run
24
+ # to individual examples or groups you care about by tagging them with
25
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
26
+ # get run.
27
+ config.filter_run :focus
28
+ config.run_all_when_everything_filtered = true
29
+
30
+ # Many RSpec users commonly either run the entire suite or an individual
31
+ # file, and it's useful to allow more verbose output when running an
32
+ # individual spec file.
33
+ if config.files_to_run.one?
34
+ # Use the documentation formatter for detailed output,
35
+ # unless a formatter has already been configured
36
+ # (e.g. via a command-line flag).
37
+ config.default_formatter = 'doc'
38
+ end
39
+
40
+ # Print the 10 slowest examples and example groups at the
41
+ # end of the spec run, to help surface which specs are running
42
+ # particularly slow.
43
+ config.profile_examples = 10
44
+
45
+ # Run specs in random order to surface order dependencies. If you find an
46
+ # order dependency and want to debug it, you can fix the order by providing
47
+ # the seed, which is printed after each run.
48
+ # --seed 1234
49
+ config.order = :random
50
+
51
+ # Seed global randomization in this process using the `--seed` CLI option.
52
+ # Setting this allows you to use `--seed` to deterministically reproduce
53
+ # test failures related to randomization by passing the same `--seed` value
54
+ # as the one that triggered the failure.
55
+ Kernel.srand config.seed
56
+
57
+ # rspec-expectations config goes here. You can use an alternate
58
+ # assertion/expectation library such as wrong or the stdlib/minitest
59
+ # assertions if you prefer.
60
+ config.expect_with :rspec do |expectations|
61
+ # Enable only the newer, non-monkey-patching expect syntax.
62
+ # For more details, see:
63
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
64
+ expectations.syntax = :expect
65
+ end
66
+
67
+ # rspec-mocks config goes here. You can use an alternate test double
68
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
69
+ config.mock_with :rspec do |mocks|
70
+ # Enable only the newer, non-monkey-patching expect syntax.
71
+ # For more details, see:
72
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
73
+ mocks.syntax = :expect
74
+
75
+ # Prevents you from mocking or stubbing a method that does not exist on
76
+ # a real object. This is generally recommended.
77
+ mocks.verify_partial_doubles = true
78
+ end
79
+ =end
80
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: latitude
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Trey Springer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Uses the great-circle distance calculation to determine the distance
63
+ between two locations with just latitudes and longitudes.
64
+ email:
65
+ - dfsiii@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - .ruby-version
73
+ - .travis.yml
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - latitude.gemspec
79
+ - lib/latitude.rb
80
+ - lib/latitude/version.rb
81
+ - lib/latitude/vincenty.rb
82
+ - spec/latitude_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: http://www.treyspringer.com
85
+ licenses:
86
+ - MIT
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.23.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Calculates distances between two geographic coordinates.
109
+ test_files:
110
+ - spec/latitude_spec.rb
111
+ - spec/spec_helper.rb