vincenty 1.0.6 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/History.txt +34 -0
- data/README.md +3 -3
- data/Rakefile +17 -10
- data/lib/angle.rb +178 -194
- data/lib/coordinate.rb +18 -19
- data/lib/core_extensions.rb +40 -59
- data/lib/latitude.rb +24 -26
- data/lib/longitude.rb +16 -20
- data/lib/track_and_distance.rb +36 -37
- data/lib/vincenty.rb +137 -128
- data/test/ts_all.rb +8 -9
- data/test/ts_angle.rb +49 -48
- data/test/ts_coordinate.rb +6 -5
- data/test/ts_latitude.rb +10 -7
- data/test/ts_longitude.rb +10 -7
- data/test/ts_track_and_distance.rb +12 -9
- data/test/ts_vincenty.rb +74 -65
- metadata +12 -14
- data/.gemtest +0 -0
data/lib/core_extensions.rb
CHANGED
@@ -1,96 +1,77 @@
|
|
1
1
|
require 'scanf'
|
2
2
|
|
3
|
-
#Extends Numeric, hence Fixed & Float to_r & to_d
|
4
|
-
#Also adds in sign.
|
3
|
+
# Extends Numeric, hence Fixed & Float to_r & to_d
|
4
|
+
# Also adds in sign.
|
5
5
|
class Numeric
|
6
|
-
#Convert Radians to Degrees
|
6
|
+
# Convert Radians to Degrees
|
7
7
|
# @return [Numeric] degrees
|
8
8
|
# @param [true,false] mod Optional argument mod == true, then applies % 360
|
9
|
-
def to_degrees(mod=false)
|
10
|
-
if mod
|
11
|
-
(self * 180
|
12
|
-
else
|
13
|
-
self * 180
|
9
|
+
def to_degrees(mod = false)
|
10
|
+
if mod
|
11
|
+
(self * 180 / Math::PI) % 360
|
12
|
+
else
|
13
|
+
self * 180 / Math::PI
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
17
|
-
#Converts degrees to Radians
|
16
|
+
|
17
|
+
# Converts degrees to Radians
|
18
18
|
# @return [Numeric] radians
|
19
19
|
# @param [true,false] mod Optional argument mod == true, then applies % Math::PI
|
20
|
-
def to_radians(mod=false)
|
21
|
-
if mod
|
20
|
+
def to_radians(mod = false)
|
21
|
+
if mod
|
22
22
|
(self * Math::PI / 180) % Math::PI
|
23
|
-
else
|
23
|
+
else
|
24
24
|
self * Math::PI / 180
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
28
|
-
alias to_r to_radians
|
27
|
+
|
29
28
|
alias to_rad to_radians
|
30
29
|
alias to_deg to_degrees
|
31
|
-
|
30
|
+
|
32
31
|
# @return [Fixnum] 1 if number is positive, -1 if negative.
|
33
32
|
def sign
|
34
33
|
self < 0 ? -1 : 1
|
35
34
|
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
#Alters round method to have an optional number of decimal places.
|
40
|
-
class Float
|
41
|
-
|
42
|
-
#Replace default round, so we can reference it later.
|
43
|
-
# @return [Float]
|
44
|
-
alias round0 round
|
45
|
-
|
46
|
-
#Compatible Replacement for Float.round
|
47
|
-
# @return [Float] float rounded to n decimal places.
|
48
|
-
# @param [Numeric] n Optional argument n is the number of decimal places to round to.
|
49
|
-
def round(n = 0)
|
50
|
-
m = 10.0**n
|
51
|
-
(self * m).round0 / m
|
52
|
-
end
|
53
35
|
end
|
54
36
|
|
55
|
-
#Extends String to to_dec_degrees, add to_r and to_d
|
37
|
+
# Extends String to to_dec_degrees, add to_r and to_d
|
56
38
|
class String
|
57
|
-
#string expected to be degrees, returns decimal degrees.
|
58
|
-
#common forms are S37 001'7.5'', 37 001'7.5''S , -37 001'7.5'', -37 0 1.512'. -37.01875 0, 37 001'.512S, S37 001'.512, ...
|
39
|
+
# string expected to be degrees, returns decimal degrees.
|
40
|
+
# common forms are S37 001'7.5'', 37 001'7.5''S , -37 001'7.5'', -37 0 1.512'. -37.01875 0, 37 001'.512S, S37 001'.512, ...
|
59
41
|
# @return [Float] angle in decimal degrees
|
60
|
-
def to_dec_degrees
|
61
|
-
#reorder 37 001'.512S, S37 001'.512 into 37 001.512'S, S37 001.512' respectively
|
62
|
-
s = self.gsub(/([0-9])(
|
63
|
-
#add in minutes and seconds to get 3 values 'deg 0 0'from S37 0, 37 0S
|
64
|
-
s.gsub!(/^([^0-9
|
65
|
-
#add in seconds get 3 values 'deg min 0' from S37 01.512', 37 01.512'S
|
66
|
-
s.gsub!(/^([^0-9
|
67
|
-
|
68
|
-
#look for anything of the form S37 001'7.5'', S37 01.512', S37.01875 0, ...
|
69
|
-
s.scanf(
|
42
|
+
def to_dec_degrees
|
43
|
+
# reorder 37 001'.512S, S37 001'.512 into 37 001.512'S, S37 001.512' respectively
|
44
|
+
s = self.gsub(/([0-9])(')\.([0-9]+)/, '\1.\3\2')
|
45
|
+
# add in minutes and seconds to get 3 values 'deg 0 0'from S37 0, 37 0S
|
46
|
+
s.gsub!(/^([^0-9.\-]*)([0-9\-.]+)([^0-9\-.]*)$/, '\1\2\3 0 0\5')
|
47
|
+
# add in seconds get 3 values 'deg min 0' from S37 01.512', 37 01.512'S
|
48
|
+
s.gsub!(/^([^0-9.\-]*)([0-9\-.]+)([^0-9\-.]+)([0-9\-.]+)([^0-9\-.]*)$/, '\1\2\3\4 0\5')
|
49
|
+
|
50
|
+
# look for anything of the form S37 001'7.5'', S37 01.512', S37.01875 0, ...
|
51
|
+
s.scanf('%[NSEW]%f%[^0-9-]%f%[^0-9-]%f') do |direction, deg, _sep1, min, _sep2, sec|
|
70
52
|
return Angle.decimal_deg( deg, min, sec, direction)
|
71
53
|
end
|
72
|
-
|
73
|
-
#look for anything of the form 37 001'7.5''S , -37 001'7.5'', -37 0 1.512'. -37.01875 0, ...
|
74
|
-
s.scanf(
|
54
|
+
|
55
|
+
# look for anything of the form 37 001'7.5''S , -37 001'7.5'', -37 0 1.512'. -37.01875 0, ...
|
56
|
+
s.scanf('%f%[^0-9-]%f%[^0-9-]%f%[^NSEW]%[NSEW]') do |deg, _sep1, min, _sep2, sec, _sep3, direction|
|
75
57
|
return Angle.decimal_deg( deg, min, sec, direction)
|
76
58
|
end
|
77
59
|
end
|
78
|
-
|
79
|
-
#Convert String number in Radians to Degrees
|
60
|
+
|
61
|
+
# Convert String number in Radians to Degrees
|
80
62
|
# @return [Float] degrees
|
81
63
|
# @param [true,false] mod Optional argument mod == true, then applies % 360
|
82
|
-
def to_degrees(mod=false)
|
83
|
-
self.to_f.to_degrees(mod)
|
64
|
+
def to_degrees(mod = false)
|
65
|
+
return self.to_f.to_degrees(mod)
|
84
66
|
end
|
85
|
-
|
86
|
-
#Converts string degrees to to_decimal_degrees, then to Radians
|
67
|
+
|
68
|
+
# Converts string degrees to to_decimal_degrees, then to Radians
|
87
69
|
# @return [Float] radians
|
88
70
|
# @param [true,false] mod Optional argument mod == true, then applies % Math::PI
|
89
|
-
def to_radians(mod=false)
|
90
|
-
self.to_dec_degrees.to_radians(mod)
|
71
|
+
def to_radians(mod = false)
|
72
|
+
return self.to_dec_degrees.to_radians(mod)
|
91
73
|
end
|
92
|
-
|
74
|
+
|
93
75
|
alias to_rad to_radians
|
94
|
-
alias to_r to_radians
|
95
76
|
alias to_deg to_degrees
|
96
77
|
end
|
data/lib/latitude.rb
CHANGED
@@ -1,44 +1,42 @@
|
|
1
|
+
require_relative 'angle.rb'
|
1
2
|
|
2
|
-
|
3
|
-
|
4
|
-
#Subclass of Angle to add in special treatment of to_d, to_r , to_s
|
5
|
-
#Latitude degrees are between -PI and PI, South to North (+/- 90 degrees)
|
6
|
-
|
3
|
+
# Subclass of Angle to add in special treatment of to_d, to_r , to_s
|
4
|
+
# Latitude degrees are between -PI and PI, South to North (+/- 90 degrees)
|
7
5
|
class Latitude < Angle
|
8
|
-
|
9
6
|
# @return [Float] angle as degrees in range -90 and 90
|
10
7
|
def to_degrees
|
11
8
|
degrees = super
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
if degrees > 270
|
10
|
+
-(360 - degrees)
|
11
|
+
elsif degrees > 180 || degrees > 90 || degrees < -90
|
12
|
+
180 - degrees
|
13
|
+
else
|
14
|
+
degrees
|
18
15
|
end
|
19
16
|
end
|
20
|
-
|
17
|
+
|
21
18
|
# @return [Float] angle as degrees in range -PI and PI
|
22
19
|
def to_radians
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
if @angle > 3 * Math::PI / 2
|
21
|
+
@angle - Math::PI * 2
|
22
|
+
elsif @angle > Math::PI || @angle > Math::PI / 2
|
23
|
+
Math::PI - @angle
|
24
|
+
elsif @angle < -Math::PI / 2
|
25
|
+
-Math::PI - @angle
|
26
|
+
else
|
27
|
+
@angle
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
32
31
|
# @return [String] angle as string in degrees minutes seconds direction.
|
33
|
-
#A South angle is negative, North is Positive.
|
32
|
+
# A South angle is negative, North is Positive.
|
34
33
|
# @param [String] fmt Optional format string passed to Angle#to_s
|
35
|
-
def to_s(fmt="%2d %2m'%2.4s\"%N")
|
36
|
-
|
34
|
+
def to_s(fmt = "%2d %2m'%2.4s\"%N")
|
35
|
+
super(fmt)
|
37
36
|
end
|
38
|
-
|
37
|
+
|
39
38
|
alias to_r to_radians
|
40
39
|
alias to_rad to_radians
|
41
|
-
#alias to_d to_degrees
|
40
|
+
# alias to_d to_degrees
|
42
41
|
alias to_deg to_degrees
|
43
|
-
|
44
|
-
end
|
42
|
+
end
|
data/lib/longitude.rb
CHANGED
@@ -1,38 +1,34 @@
|
|
1
|
+
require_relative 'angle.rb'
|
1
2
|
|
2
|
-
|
3
|
-
|
4
|
-
#Subclass of Angle to add in special treatment of to_d, to_r and to_s
|
5
|
-
#Longitude degrees are between -2PI and 2PI, West to East (+/- 180 degrees)
|
6
|
-
|
3
|
+
# Subclass of Angle to add in special treatment of to_d, to_r and to_s
|
4
|
+
# Longitude degrees are between -2PI and 2PI, West to East (+/- 180 degrees)
|
7
5
|
class Longitude < Angle
|
8
|
-
|
9
6
|
# @return [Float] angle as degrees in range -180 and 180
|
10
7
|
def to_degrees
|
11
8
|
degrees = super
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
if degrees > 180 then degrees - 360
|
10
|
+
else
|
11
|
+
degrees
|
15
12
|
end
|
16
13
|
end
|
17
|
-
|
14
|
+
|
18
15
|
# @return [Float] angle as degrees in range -2PI and 2PI
|
19
16
|
def to_radians
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
if @angle > Math::PI then @angle - 2 * Math::PI
|
18
|
+
else
|
19
|
+
@angle
|
23
20
|
end
|
24
21
|
end
|
25
|
-
|
22
|
+
|
26
23
|
# @return [String] angle as string in degrees minutes seconds direction.
|
27
|
-
#A West angle is negative, East is Positive.
|
24
|
+
# A West angle is negative, East is Positive.
|
28
25
|
# @param [String] fmt Optional format string passed to Angle#to_s
|
29
|
-
def to_s(fmt="%3d %2m'%2.4s\"%E")
|
26
|
+
def to_s(fmt = "%3d %2m'%2.4s\"%E")
|
30
27
|
super(fmt)
|
31
28
|
end
|
32
|
-
|
29
|
+
|
33
30
|
alias to_r to_radians
|
34
31
|
alias to_rad to_radians
|
35
|
-
#alias to_d to_degrees
|
32
|
+
# alias to_d to_degrees
|
36
33
|
alias to_deg to_degrees
|
37
|
-
|
38
|
-
end
|
34
|
+
end
|
data/lib/track_and_distance.rb
CHANGED
@@ -1,41 +1,40 @@
|
|
1
|
+
require_relative 'angle.rb'
|
1
2
|
|
2
|
-
|
3
|
-
|
4
|
-
#Holds a bearing and distance
|
3
|
+
# Holds a bearing and distance
|
5
4
|
class TrackAndDistance
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# @param [String, Numeric, #to_radian, #to_f] Bearing can be a String or Numeric or any object with to_radians and to_f
|
12
|
-
# @param [Numeric] distance
|
13
|
-
# @param [true,false, :radians] radians Bearing is in degrees unless radians == true (or set to :radians).
|
14
|
-
def initialize(bearing, distance, radians=false)
|
15
|
-
@bearing = Angle.new(bearing, radians)
|
16
|
-
@distance = distance
|
17
|
-
end
|
18
|
-
|
19
|
-
#format string fmt is currently just for the bearing angle.
|
20
|
-
#Need to change this to include the distance is single format string.
|
21
|
-
# @return [String] Bearing angle and distance in meters.
|
22
|
-
# @param [String] fmt Optional format string passed to Coordinate#strf
|
23
|
-
def to_s(fmt = nil)
|
24
|
-
if(fmt)
|
25
|
-
#needs work to include distance as well as angle fmt.
|
26
|
-
"#{@bearing.strf(fmt)} #{distance.round(4)}m"
|
27
|
-
else
|
28
|
-
"#{@bearing.strf} #{distance.round(4)}m"
|
29
|
-
end
|
30
|
-
end
|
5
|
+
# @return [Angle]
|
6
|
+
attr_accessor :bearing
|
7
|
+
# @return [Float]
|
8
|
+
attr_accessor :distance
|
31
9
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
10
|
+
# @param [String, Numeric, #to_radian, #to_f] Bearing can be a String or Numeric or any object with to_radians and to_f
|
11
|
+
# @param [Numeric] distance
|
12
|
+
# @param [true,false, :radians] radians Bearing is in degrees unless radians == true (or set to :radians).
|
13
|
+
def initialize(bearing, distance, radians = false)
|
14
|
+
@bearing = Angle.new(bearing, radians)
|
15
|
+
@distance = distance
|
16
|
+
end
|
17
|
+
|
18
|
+
# format string fmt is currently just for the bearing angle.
|
19
|
+
# Need to change this to include the distance is single format string.
|
20
|
+
# @return [String] Bearing angle and distance in meters.
|
21
|
+
# @param [String] fmt Optional format string passed to Coordinate#strf
|
22
|
+
def to_s(fmt = nil)
|
23
|
+
if fmt
|
24
|
+
# needs work to include distance as well as angle fmt.
|
25
|
+
return "#{@bearing.strf(fmt)} #{distance.round(4)}m"
|
26
|
+
else
|
27
|
+
return "#{@bearing.strf} #{distance.round(4)}m"
|
40
28
|
end
|
41
|
-
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Array] with members bearing and distance.
|
32
|
+
def to_ary
|
33
|
+
return [ @bearing, @distance ]
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [Hash] with keys :bearing and :distance
|
37
|
+
def to_hash
|
38
|
+
return { bearing: @bearing, distance: @distance }
|
39
|
+
end
|
40
|
+
end
|