geo_calc 0.7.1 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/Gemfile +4 -1
  2. data/README.textile +8 -59
  3. data/VERSION +1 -1
  4. data/geo_calc.gemspec +15 -22
  5. data/lib/geo_calc/calc/bearing.rb +1 -1
  6. data/lib/geo_calc/calc/destination.rb +3 -2
  7. data/lib/geo_calc/calc/distance.rb +1 -1
  8. data/lib/geo_calc/calc/intersection.rb +8 -8
  9. data/lib/geo_calc/calc/midpoint.rb +8 -6
  10. data/lib/geo_calc/calc/rhumb.rb +35 -16
  11. data/lib/geo_calc/calc.rb +15 -21
  12. data/lib/geo_calc/extensions/array.rb +1 -3
  13. data/lib/geo_calc/extensions/hash.rb +1 -3
  14. data/lib/geo_calc/extensions/string.rb +8 -17
  15. data/lib/geo_calc/extensions.rb +0 -1
  16. data/lib/geo_calc/pretty_print.rb +14 -14
  17. data/lib/geo_calc.rb +7 -4
  18. data/spec/geo_calc/calculations_spec.rb +2 -1
  19. data/spec/geo_calc/core_ext/array_ext_spec.rb +1 -9
  20. data/spec/geo_calc/core_ext/hash_ext_spec.rb +3 -9
  21. data/spec/geo_calc/core_ext/string_ext_spec.rb +1 -9
  22. data/spec/geo_calc/core_ext_spec.rb +1 -7
  23. data/spec/geo_calc/include_apis_spec.rb +32 -0
  24. data/spec/spec_helper.rb +13 -5
  25. metadata +52 -35
  26. data/lib/geo_calc/dms/converter.rb +0 -106
  27. data/lib/geo_calc/dms.rb +0 -5
  28. data/lib/geo_calc/extensions/math.rb +0 -6
  29. data/lib/geo_calc/extensions/numeric.rb +0 -24
  30. data/lib/geo_calc/geo_point/class_methods.rb +0 -15
  31. data/lib/geo_calc/geo_point/core_extension.rb +0 -11
  32. data/lib/geo_calc/geo_point/shared.rb +0 -29
  33. data/lib/geo_calc/geo_point.rb +0 -135
  34. data/lib/geo_units/converter.rb +0 -123
  35. data/lib/geo_units/numeric_ext.rb +0 -117
  36. data/lib/geo_units.rb +0 -21
  37. data/spec/geo_calc/dms/converter_spec.rb +0 -60
  38. data/spec/geo_calc/geo_point/class_methods_spec.rb +0 -31
  39. data/spec/geo_calc/geo_point/initializer_spec.rb +0 -148
  40. data/spec/geo_calc/geo_point/lat_lon.rb +0 -115
  41. data/spec/geo_calc/geo_point_spec.rb +0 -99
  42. data/spec/geo_units/converter_spec.rb +0 -57
@@ -1,123 +0,0 @@
1
- require 'geo_calc/calc'
2
- require 'geo_calc/extensions'
3
-
4
- module GeoUnits
5
- module Converter
6
- # Convert numeric degrees to deg/min/sec latitude (suffixed with N/S)
7
- #
8
- # @param {Number} deg: Degrees
9
- # @param {String} [format=dms]: Return value as 'd', 'dm', 'dms'
10
- # @param {Number} [dp=0|2|4]: No of decimal places to use - default 0 for dms, 2 for dm, 4 for d
11
- # @returns {String} Deg/min/seconds
12
-
13
- def to_lat deg, format = :dms, dp = 0
14
- deg = deg.normalize_lat
15
- _lat = GeoCalc::Dms::Converter.to_dms deg, format, dp
16
- _lat == '' ? '' : _lat[1..-1] + (deg<0 ? 'S' : 'N') # knock off initial '0' for lat!
17
- end
18
-
19
- # Convert numeric degrees to deg/min/sec longitude (suffixed with E/W)
20
- #
21
- # @param {Number} deg: Degrees
22
- # @param {String} [format=dms]: Return value as 'd', 'dm', 'dms'
23
- # @param {Number} [dp=0|2|4]: No of decimal places to use - default 0 for dms, 2 for dm, 4 for d
24
- # @returns {String} Deg/min/seconds
25
-
26
- def to_lon deg, format = :dms, dp = 0
27
- deg = deg.normalize_lng
28
- lon = GeoCalc::Dms::Converter.to_dms deg, format, dp
29
- lon == '' ? '' : lon + (deg<0 ? 'W' : 'E')
30
- end
31
-
32
-
33
- # Convert numeric degrees to deg/min/sec as a bearing (0º..360º)
34
- #
35
- # @param {Number} deg: Degrees
36
- # @param {String} [format=dms]: Return value as 'd', 'dm', 'dms'
37
- # @param {Number} [dp=0|2|4]: No of decimal places to use - default 0 for dms, 2 for dm, 4 for d
38
- # @returns {String} Deg/min/seconds
39
-
40
- def to_brng deg, format = :dms, dp = 0
41
- deg = (deg.to_f + 360) % 360 # normalise -ve values to 180º..360º
42
- brng = GeoCalc::Dms::Converter.to_dms deg, format, dp
43
- brng.gsub /360/, '0' # just in case rounding took us up to 360º!
44
- end
45
-
46
- protected
47
-
48
- include ::GeoCalc::NumericCheckExt
49
-
50
- # Converts numeric degrees to radians
51
- def to_rad degrees
52
- degrees * Math::PI / 180
53
- end
54
- alias_method :to_radians, :to_rad
55
- alias_method :as_rad, :to_rad
56
- alias_method :as_radians, :to_rad
57
- alias_method :in_rad, :to_rad
58
- alias_method :in_radians, :to_rad
59
-
60
-
61
- # Converts radians to numeric (signed) degrees
62
- # latitude (north to south) from equator +90 up then -90 down (equator again) = 180 then 180 for south = 360 total
63
- # longitude (west to east) east +180, west -180 = 360 total
64
- def to_deg radians
65
- radians * 180 / Math::PI
66
- end
67
-
68
- alias_method :to_degrees, :to_deg
69
- alias_method :as_deg, :to_deg
70
- alias_method :as_degrees, :to_deg
71
- alias_method :in_deg, :to_deg
72
- alias_method :in_degrees, :to_deg
73
-
74
- extend self
75
- end
76
-
77
- # all degrees between -180 and 180
78
- def normalize_lng deg
79
- case deg
80
- when -360..-180
81
- deg % 180
82
- when -180..0
83
- -180 + (deg % 180)
84
- when 0..180
85
- deg
86
- when 180..360
87
- deg % 180
88
- else
89
- raise ArgumentError, "Degrees #{deg} out of range, must be between -360 to 360"
90
- end
91
- end
92
-
93
- # all degrees between -90 and 90
94
- def normalize_lat deg
95
- case deg
96
- when -360..-270
97
- deg % 90
98
- when -270..-180
99
- 90 - (deg % 90)
100
- when -180..-90
101
- - (deg % 90)
102
- when -90..0
103
- -90 + (deg % 90)
104
- when 0..90
105
- deg
106
- when 90..180
107
- deg % 90
108
- when 180..270
109
- - (deg % 90)
110
- when 270..360
111
- - 90 + (deg % 90)
112
- else
113
- raise ArgumentError, "Degrees #{deg} out of range, must be between -360 to 360"
114
- end
115
- end
116
-
117
- def normalize_deg degrees, shift = 0
118
- (degrees + shift) % 360
119
- end
120
- alias_method :normalize_degrees, :normalize_deg
121
-
122
- extend self
123
- end
@@ -1,117 +0,0 @@
1
- module GeoUnits
2
- module NumericExt
3
- def to_lat
4
- normalize_lat
5
- end
6
-
7
- def to_lng
8
- normalize_lng
9
- end
10
-
11
- def is_between? lower, upper
12
- (lower..upper).cover? self
13
- end
14
-
15
-
16
- def to_dms format = :dms, dp = nil
17
- GeoCalc::Dms::Converter.to_dms self, format, dp
18
- end
19
-
20
- def to_lat_dms format = :dms, dp = nil
21
- GeoUnits::Converter.to_lat self, format, dp
22
- end
23
-
24
- def to_lon_dms format = :dms, dp = nil
25
- GeoUnits::Converter.to_lon self, format, dp
26
- end
27
-
28
- # Converts numeric degrees to radians
29
- def to_rad
30
- self * Math::PI / 180
31
- end
32
- alias_method :to_radians, :to_rad
33
- alias_method :as_rad, :to_rad
34
- alias_method :as_radians, :to_rad
35
- alias_method :in_rad, :to_rad
36
- alias_method :in_radians, :to_rad
37
-
38
-
39
- # Converts radians to numeric (signed) degrees
40
- # latitude (north to south) from equator +90 up then -90 down (equator again) = 180 then 180 for south = 360 total
41
- # longitude (west to east) east +180, west -180 = 360 total
42
- def to_deg
43
- self * 180 / Math::PI
44
- end
45
-
46
- alias_method :to_degrees, :to_deg
47
- alias_method :as_deg, :to_deg
48
- alias_method :as_degrees, :to_deg
49
- alias_method :in_deg, :to_deg
50
- alias_method :in_degrees, :to_deg
51
-
52
-
53
- # Formats the significant digits of a number, using only fixed-point notation (no exponential)
54
- #
55
- # @param {Number} precision: Number of significant digits to appear in the returned string
56
- # @returns {String} A string representation of number which contains precision significant digits
57
- def to_precision precision
58
- self.round(precision).to_s
59
- end
60
- alias_method :to_fixed, :to_precision
61
-
62
- # all degrees between -180 and 180
63
- def normalize_lng
64
- case self
65
- when -360, 0, 360
66
- 0
67
- when -360..-180
68
- self % 180
69
- when -180..0
70
- -180 + (self % 180)
71
- when 0..180
72
- self
73
- when 180..360
74
- self % 180
75
- else
76
- return (self % 360).normalize_lng if self > 360
77
- return (360 - (self % 360)).normalize_lng if self < -360
78
- raise ArgumentError, "Degrees #{self} out of range"
79
- end
80
- end
81
-
82
- # all degrees between -90 and 90
83
- def normalize_lat
84
- case self
85
- when -360, 0, 360
86
- 0
87
- when -180, 180
88
- 0
89
- when -360..-270
90
- self % 90
91
- when -270..-180
92
- 90 - (self % 90)
93
- when -180..-90
94
- - (self % 90)
95
- when -90..0
96
- -90 + (self % 90)
97
- when 0..90
98
- self
99
- when 90..180
100
- self % 90
101
- when 180..270
102
- - (self % 90)
103
- when 270..360
104
- - 90 + (self % 90)
105
- else
106
- return (self % 360).normalize_lat if self > 360
107
- return (360 - (self % 360)).normalize_lat if self < -360
108
- raise ArgumentError, "Degrees #{self} out of range"
109
- end
110
- end
111
-
112
- def normalize_deg shift = 0
113
- (self + shift) % 360
114
- end
115
- alias_method :normalize_degrees, :normalize_deg
116
- end
117
- end
data/lib/geo_units.rb DELETED
@@ -1,21 +0,0 @@
1
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2
- # Geodesy representation conversion functions (c) Chris Veness 2002-2010
3
- # - www.movable-type.co.uk/scripts/latlong.html
4
- #
5
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6
-
7
- # Parses string representing degrees/minutes/seconds into numeric degrees
8
- #
9
- # This is very flexible on formats, allowing signed decimal degrees, or deg-min-sec optionally
10
- # suffixed by compass direction (NSEW). A variety of separators are accepted (eg 3º 37' 09"W)
11
- # or fixed-width format without separators (eg 0033709W). Seconds and minutes may be omitted.
12
- # (Note minimal validation is done).
13
- #
14
- # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
15
- # @returns {Number} Degrees as decimal number
16
- # @throws ArgumentError
17
-
18
- module GeoUnits
19
- autoload :Converter, 'geo_units/converter'
20
- autoload :NumericExt, 'geo_units/numeric_ext'
21
- end
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class Parser
4
- include GeoCalc::Dms::Converter
5
- end
6
-
7
- def parser
8
- Parser.new
9
- end
10
-
11
- # - www.movable-type.co.uk/scripts/latlong.html
12
- describe GeoCalc::Dms::Converter do
13
- # # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
14
- # @returns {Number} Degrees as decimal number
15
- describe '#parse_dms' do
16
- it 'should convert "58 38 38N" to a Float of degrees (58..59)' do
17
- deg = parser.parse_dms("58 38 38N")
18
- deg.should be_a(Float)
19
- deg.should be_between(58, 59)
20
- end
21
-
22
- it 'should convert "01 38 38W" to a Float of degrees (1..2)' do
23
- deg = parser.parse_dms("01 38 38W")
24
- deg.should be_a(Float)
25
- deg.should < 0
26
- deg.should > -2
27
- end
28
-
29
- it 'should convert "005 38 E" to a Float of degrees (5..6)' do
30
- deg = parser.parse_dms("005 38 E")
31
- deg.should be_a(Float)
32
- deg.should be_between(5, 6)
33
- end
34
- end
35
-
36
- # deg, format = :dms, dp = 0
37
- describe '#to_dms' do
38
- it 'should convert 58.3 to a String in DMS format' do
39
- dms = parser.to_dms(58.3)
40
- dms.should be_a(String)
41
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032", "00", "\u2033")
42
- dms.should match expr
43
- end
44
-
45
- it 'should convert 58.3 to a String in DM format' do
46
- dm = parser.to_dms(58.3, :dm, 2)
47
- dm.should be_a(String)
48
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032")
49
- dm.should match expr
50
- end
51
-
52
- it 'should convert 58.3 to a String in D format' do
53
- d = parser.to_dms(58.3, :d, 2)
54
- d.should be_a(String)
55
- m = Regexp.escape "058".concat("\u00B0")
56
- d.should match m
57
- end
58
- end
59
- end
60
-
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # - www.movable-type.co.uk/scripts/latlong.html
4
- describe GeoPoint do
5
- describe 'Class methods' do
6
- describe '#coord_mode' do
7
- it 'should change global coordinates mode' do
8
- GeoPoint.coord_mode = :lng_lat
9
- GeoPoint.coord_mode.should == :lng_lat
10
-
11
- GeoPoint.coord_mode = :lat_lng
12
- GeoPoint.coord_mode.should == :lat_lng
13
- end
14
-
15
- it 'shoould not allow setting invalid coord mode' do
16
- lambda { GeoPoint.coord_mode = :blip }.should raise_error
17
- end
18
- end
19
-
20
- describe '#earth_radius_km' do
21
- it 'should change global earth_radius_km' do
22
- GeoPoint.earth_radius_km = 6360
23
- GeoPoint.earth_radius_km.should == 6360
24
- end
25
-
26
- it 'shoould not allow setting invalid earth radius' do
27
- lambda { GeoPoint.earth_radius_km = 6100 }.should raise_error
28
- end
29
- end
30
- end
31
- end
@@ -1,148 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # - www.movable-type.co.uk/scripts/latlong.html
4
- describe GeoPoint do
5
- describe '#initializer' do
6
- describe '1 argument' do
7
- describe 'single String' do
8
- describe '50.1, 5.0 ' do
9
- it 'should create a GeoPoint' do
10
- p1 = GeoPoint.new "50.1, 5.0"
11
- p1.should be_a(GeoPoint)
12
- p1.lat.should == 50.1
13
- p1.lon.should == 5.0
14
- p1.unit.should == :degrees
15
- p1.earth_radius_km.should == 6371
16
- end
17
- end
18
-
19
- describe '(50.1, 5.0)' do
20
- it 'should create a GeoPoint' do
21
- p1 = GeoPoint.new "(50.1, 5.2)"
22
- p1.should be_a(GeoPoint)
23
- p1.lat.should == 50.1
24
- p1.lon.should == 5.2
25
- p1.unit.should == :degrees
26
- p1.earth_radius_km.should == 6371
27
- end
28
- end
29
-
30
- describe '58 38 38N, 003 04 12W' do
31
- it 'should create a GeoPoint' do
32
- p1 = GeoPoint.new "58 38 38N, 003 04 12W"
33
- p1.should be_a(GeoPoint)
34
- p1.lat.should be_within(0.5).of(58.38)
35
- p1.lon.should be_within(0.5).of(-3)
36
- p1.unit.should == :degrees
37
- p1.earth_radius_km.should == 6371
38
- end
39
- end
40
-
41
- describe '(58 38 38N, 003 04 12W)' do
42
- it 'should create a GeoPoint' do
43
- p1 = GeoPoint.new "(58 38 38N, 003 04 12W)"
44
- p1.should be_a(GeoPoint)
45
- p1.lat.should be_within(0.5).of(58.38)
46
- p1.lon.should be_within(0.5).of(-3) # W is negative, then normalize
47
- p1.unit.should == :degrees
48
- p1.earth_radius_km.should == 6371
49
- end
50
- end
51
- end
52
-
53
- describe 'single Array' do
54
- describe '2 Fixed numbers: 50,5 ' do
55
- it 'should create a GeoPoint' do
56
- p1 = GeoPoint.new [50, 5]
57
- p1.should be_a(GeoPoint)
58
- p1.lat.should == 50
59
- p1.lon.should == 5
60
- p1.unit.should == :degrees
61
- p1.earth_radius_km.should == 6371
62
- end
63
- end
64
-
65
- describe '2 Float numbers: 50.1, 5.0 ' do
66
- it 'should create a GeoPoint' do
67
- p1 = GeoPoint.new [50.1, 5.0]
68
- p1.should be_a(GeoPoint)
69
- p1.lat.should == 50.1
70
- p1.lon.should == 5.0
71
- p1.unit.should == :degrees
72
- p1.earth_radius_km.should == 6371
73
- end
74
- end
75
-
76
- describe 'single Hash' do
77
- describe 'with: {:lat => 50.1, :lng => 5.1}' do
78
- it 'should create a GeoPoint' do
79
- p1 = GeoPoint.new :lat => 50.1, :lng => 5.1
80
- p1.should be_a(GeoPoint)
81
- p1.lat.should == 50.1
82
- p1.lon.should == 5.1
83
- p1.unit.should == :degrees
84
- p1.earth_radius_km.should == 6371
85
- end
86
- end
87
-
88
- describe 'with: {:lat => 50.1, :long => 5.1}' do
89
- it 'should create a GeoPoint' do
90
- p1 = GeoPoint.new :lat => 50.1, :long => 5.1
91
- p1.should be_a(GeoPoint)
92
- p1.lat.should == 50.1
93
- p1.lon.should == 5.1
94
- p1.unit.should == :degrees
95
- p1.earth_radius_km.should == 6371
96
- end
97
- end
98
-
99
- describe 'with: {:latitude => 50.1, :longitude => 5.1}' do
100
- it 'should create a GeoPoint' do
101
- p1 = GeoPoint.new :latitude => 50.1, :longitude => 5.1
102
- p1.should be_a(GeoPoint)
103
- p1.lat.should == 50.1
104
- p1.lon.should == 5.1
105
- p1.unit.should == :degrees
106
- p1.earth_radius_km.should == 6371
107
- end
108
- end
109
- end
110
- end
111
- end
112
-
113
- describe 'with 2 arguments' do
114
- describe '2 Fixed numbers (Fixnum)' do
115
- it 'should create a GeoPoint' do
116
- p1 = GeoPoint.new 50, 5
117
- p1.should be_a(GeoPoint)
118
- p1.lat.should == 50
119
- p1.lon.should == 5
120
- p1.unit.should == :degrees
121
- p1.earth_radius_km.should == 6371
122
- end
123
- end
124
-
125
- describe '2 Float numbers' do
126
- it 'should create a GeoPoint' do
127
- p1 = GeoPoint.new 50.1, 5.0
128
- p1.should be_a(GeoPoint)
129
- p1.lat.should == 50.1
130
- p1.lon.should == 5.0
131
- p1.unit.should == :degrees
132
- p1.earth_radius_km.should == 6371
133
- end
134
- end
135
-
136
- describe '2 Strings: "58 38 38N", "003 04 12W"' do
137
- it 'should create a GeoPoint' do
138
- p1 = GeoPoint.new "58 38 38N", "003 04 12W"
139
- p1.should be_a(GeoPoint)
140
- p1.lat.should be_within(0.5).of(58.38)
141
- p1.lon.should be_within(0.5).of(-3)
142
- p1.unit.should == :degrees
143
- p1.earth_radius_km.should == 6371
144
- end
145
- end
146
- end
147
- end # initializer
148
- end
@@ -1,115 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # - www.movable-type.co.uk/scripts/latlong.html
4
- describe GeoPoint do
5
- describe '#lat' do
6
- before :each do
7
- @p1 = GeoPoint.new 50, 5
8
- end
9
-
10
- it 'should return latitude' do
11
- @p1.lat.should == 50
12
- end
13
-
14
- describe '#latitude (alias)' do
15
- it 'should return latitude' do
16
- @p1.latitude.should == 50
17
- end
18
- end
19
-
20
- describe '#to_lat (alias)' do
21
- it 'should return latitude' do
22
- @p1.to_lat.should == 50
23
- end
24
- end
25
- end
26
-
27
- describe '#lat=' do
28
- before :each do
29
- @p1 = GeoPoint.new 50, 5
30
- end
31
-
32
- it 'should set new latitude' do
33
- @p1.lat = 60
34
- @p1.lat.should == 60
35
- end
36
-
37
- it 'should set new latitude -2' do
38
- @p1.lat = -2
39
- @p1.lat.should == -2
40
- end
41
-
42
- it 'should convert latitude -182 to -2' do
43
- @p1.lat = -2
44
- @p1.lat.should == -2
45
- end
46
-
47
-
48
- it 'should set new latitude within allowed range' do
49
- @p1.lat = 520
50
- @p1.lat.should be_between(0, 360)
51
- end
52
-
53
- describe '#latitude (alias)' do
54
- it 'should set latitude' do
55
- @p1.lat = 72
56
- @p1.latitude.should == 72
57
- end
58
- end
59
- end
60
-
61
- describe '#lon' do
62
- before :each do
63
- @p1 = GeoPoint.new 5, 50
64
- end
65
-
66
- it 'should return longitude' do
67
- @p1.lon.should == 50
68
- end
69
-
70
- it 'should return longitude (via lng)' do
71
- @p1.lng.should == 50
72
- end
73
-
74
- describe '#longitude (alias)' do
75
- it 'should return longitude' do
76
- @p1.longitude.should == 50
77
- end
78
- end
79
-
80
- describe '#to_lng (alias)' do
81
- it 'should return latitude' do
82
- @p1.to_lng.should == 50
83
- end
84
- end
85
- end
86
-
87
- describe '#lon=' do
88
- before :each do
89
- @p1 = GeoPoint.new 5, 50
90
- end
91
-
92
- it 'should set new longitude' do
93
- @p1.lat.should == 5
94
- @p1.lon = 60
95
- @p1.lon.should == 60
96
- end
97
-
98
- it 'should return longitude (via lng)' do
99
- @p1.lng = 70
100
- @p1.lng.should == 70
101
- end
102
-
103
- it 'should set new latitude within allowed range' do
104
- @p1.lon = 520
105
- @p1.longitude.should be_between(-180, 180)
106
- end
107
-
108
- describe '#latitude (alias)' do
109
- it 'should set latitude' do
110
- @p1.longitude = 72
111
- @p1.longitude.should == 72
112
- end
113
- end
114
- end
115
- end