cross-track-distance 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cross-track-distance.gemspec +34 -0
- data/lib/cross/track/distance.rb +85 -0
- data/lib/cross/track/distance/version.rb +7 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b05d000da965d6dfe0265cf0f105433f50544dc1
|
4
|
+
data.tar.gz: 2d35561f38a791992601d71e90f91e5b8277bf0e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25b09e56fdd03be848096a1c910efc153febf0b7486b84c55c7bc6eb715a9d3d68393bcd50d13e47aeedb9973a93c3bec4ec4e1f1b834105b727e0c585edaad4
|
7
|
+
data.tar.gz: 2883284b387caaaf2e6cf7bbe89b358f8e3c3c18871c55d63bb0dad90f2a66810c687833dba20c2821aa53ccb6af828798c0bcbb2a12296909701248735f01af
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 merlos
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Cross::Track::Distance
|
2
|
+
Uber simple gem to calculate the cross track distance (aka cross track error).
|
3
|
+
|
4
|
+
Suppose you are proceeding on a great circle route from `start_segment_pt` to `end_segment_pt` but you are at `point`, You can calculate the shortest distance to the segment that conforms the route
|
5
|
+
|
6
|
+
```
|
7
|
+
start_segment_pt * ------+------------------ * end_segment
|
8
|
+
|
|
9
|
+
|
|
10
|
+
* point
|
11
|
+
```
|
12
|
+
If `start_segment_pt`, `end_segment_pt` and `point` are given in __latitude and longitude__ (WGS84), and the radius of the great circle is the Earth, you can use this gem to calculate the shortest distance from point to the segment line also called cross track distance or cross track error.
|
13
|
+
|
14
|
+
You have more info abou the theory behind the formula in these links:
|
15
|
+
|
16
|
+
* This gem is based on http://www.movable-type.co.uk/scripts/latlong.html
|
17
|
+
* You also have more information on http://williams.best.vwh.net/avform.htm#XTE
|
18
|
+
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'cross-track-distance'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install cross-track-distance
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'cross/track/distance'
|
39
|
+
|
40
|
+
segment_start = {lat: 53.3206, lon: -1.7297}
|
41
|
+
segment_end = {lat: 53.1887, lon: 0.1334}
|
42
|
+
point = {lat: 53.2611, lon: -0.7972}
|
43
|
+
|
44
|
+
puts Cross::Track::Distance.cross_track_distance(segment_start,segment_end,point)
|
45
|
+
# => -307.549570418756
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/merlos/cross-track-distance.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cross/track/distance"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cross/track/distance/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cross-track-distance"
|
8
|
+
spec.version = Cross::Track::Distance::VERSION
|
9
|
+
spec.authors = ["merlos"]
|
10
|
+
spec.email = ["jmmerlos@merlos.org"]
|
11
|
+
spec.summary = %q{Cross track error / Cross track distance calculator}
|
12
|
+
spec.description = %q{Gem to calculate the distance of a point to a segment on a great circle.}
|
13
|
+
spec.homepage = "http://github.com/merlos/cross-track-distance."
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
#if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
#else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
#end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "haversine", "~>0.3.1"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "cross/track/distance/version"
|
2
|
+
require "haversine"
|
3
|
+
# Based on http://www.movable-type.co.uk/scripts/latlong.html
|
4
|
+
#
|
5
|
+
|
6
|
+
# helper methods
|
7
|
+
class Float
|
8
|
+
def to_rad
|
9
|
+
self*Math::PI/180
|
10
|
+
end
|
11
|
+
def to_deg
|
12
|
+
180*self/Math::PI
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Cross
|
17
|
+
module Track
|
18
|
+
module Distance
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
# Earth ratio in meters
|
23
|
+
@@R = 6371000
|
24
|
+
|
25
|
+
#
|
26
|
+
# http://www.movable-type.co.uk/scripts/latlong.html
|
27
|
+
|
28
|
+
def self.bearing(from, to)
|
29
|
+
# see http://mathforum.org/library/drmath/view/55417.html
|
30
|
+
# phi_1 = from.lat to radians
|
31
|
+
# phi_2 = to.lat to radians
|
32
|
+
# delta_lambda = to.lon - from.lon to radians
|
33
|
+
# y = sin(d_lambda) * cos(phi_2)
|
34
|
+
# x = cos(phi_1) * sin(phi_2) - sin(phi_1)*cos(phi_2) * cos(delta_lambda)
|
35
|
+
# thita = atan2(y, x)
|
36
|
+
# (thita to_degrees + 360) % 360)
|
37
|
+
phi_1 = from[:lat].to_rad
|
38
|
+
phi_2 = to[:lat].to_rad
|
39
|
+
delta_lambda= (to[:lon] - from[:lon]).to_rad
|
40
|
+
y = Math::sin(delta_lambda) * Math::cos(phi_2)
|
41
|
+
x = Math::cos(phi_1) * Math::sin(phi_2) - Math::sin(phi_1) * Math::cos(phi_2) * Math::cos(delta_lambda)
|
42
|
+
thita = Math::atan2(y,x)
|
43
|
+
#puts from
|
44
|
+
#puts to
|
45
|
+
#puts phi_1
|
46
|
+
#puts phi_2
|
47
|
+
#puts delta_lambda
|
48
|
+
#puts y
|
49
|
+
#puts x
|
50
|
+
#puts thita
|
51
|
+
(thita.to_deg + 360) % 360
|
52
|
+
end
|
53
|
+
#
|
54
|
+
#
|
55
|
+
# Calculates the shortest distance from point to the segment line on a great circle.
|
56
|
+
# segment_start_pt is point in which the segment starts. It's a hash with :lat and :lon
|
57
|
+
# segment_end_pt is point in which the segment ends. It's a hash with :lat and :lon
|
58
|
+
# point is the point to calculate the distance to the segment.
|
59
|
+
#
|
60
|
+
# Returns the distance in meters
|
61
|
+
def self.cross_track_distance(segment_start_pt, segment_end_pt, point)
|
62
|
+
#var δ13 = pathStart.distanceTo(this, radius)/radius;
|
63
|
+
#var θ13 = pathStart.bearingTo(this).toRadians();
|
64
|
+
#var θ12 = pathStart.bearingTo(pathEnd).toRadians();
|
65
|
+
#var dxt = Math.asin( Math.sin(δ13) * Math.sin(θ13-θ12) ) * radius;
|
66
|
+
|
67
|
+
#puts segment_start_pt
|
68
|
+
#puts segment_end_pt
|
69
|
+
#puts point
|
70
|
+
|
71
|
+
delta_13 = Haversine.distance(segment_start_pt[:lat],segment_start_pt[:lon],point[:lat],point[:lon]).to_m / @@R
|
72
|
+
thita_13 = Cross::Track::Distance::bearing(segment_start_pt, point).to_rad
|
73
|
+
thita_12 = Cross::Track::Distance::bearing(segment_start_pt, segment_end_pt).to_rad
|
74
|
+
|
75
|
+
#puts delta_13
|
76
|
+
#puts thita_13
|
77
|
+
#puts thita_12
|
78
|
+
|
79
|
+
Math::asin(Math::sin(delta_13) * Math::sin(thita_13 - thita_12) ) * @@R
|
80
|
+
|
81
|
+
end
|
82
|
+
# Your code goes here...
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cross-track-distance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- merlos
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: haversine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: Gem to calculate the distance of a point to a segment on a great circle.
|
70
|
+
email:
|
71
|
+
- jmmerlos@merlos.org
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- cross-track-distance.gemspec
|
85
|
+
- lib/cross/track/distance.rb
|
86
|
+
- lib/cross/track/distance/version.rb
|
87
|
+
homepage: http://github.com/merlos/cross-track-distance.
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Cross track error / Cross track distance calculator
|
111
|
+
test_files: []
|