geo_calc 0.5.3 → 0.6.0

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.
@@ -1,4 +1,5 @@
1
- require 'geo_calc/calculations'
1
+ require 'geo_calc/geo'
2
+ require 'geo_calc/calc'
2
3
 
3
4
  # Sample usage:
4
5
  # p1 = GeoPoint.new(51.5136, -0.0983)
@@ -13,7 +14,7 @@ require 'geo_calc/calculations'
13
14
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14
15
 
15
16
  class GeoPoint
16
- include GeoCalc
17
+ include GeoCalc::Calc::All
17
18
  # Creates a point on the earth's surface at the supplied latitude / longitude
18
19
  #
19
20
  # Constructor
@@ -79,6 +80,16 @@ class GeoPoint
79
80
 
80
81
  alias_method :to_dms, :to_s
81
82
 
83
+ def reverse_point!
84
+ self.lat = lat * -1
85
+ self.lng = lng * -1
86
+ self
87
+ end
88
+
89
+ def reverse_point
90
+ self.dup.reverse_point!
91
+ end
92
+
82
93
  def to_lat_lng
83
94
  [lat, lng]
84
95
  end
@@ -0,0 +1,52 @@
1
+ module GeoCalc
2
+ module PrettyPrint
3
+ # Returns the latitude of this point; signed numeric degrees if no format, otherwise format & dp
4
+ # as per Geo.to_lat
5
+ #
6
+ # - String [format]: Return value as 'd', 'dm', 'dms'
7
+ # - Numeric [dp=0|2|4]: No of decimal places to display
8
+ #
9
+ # Returns {Numeric|String}: Numeric degrees if no format specified, otherwise deg/min/sec
10
+ #
11
+
12
+ def to_lat format = :dms, dp = 0
13
+ return lat if !format
14
+ Geo.to_lat lat, format, dp
15
+ end
16
+
17
+
18
+ # Returns the longitude of this point; signed numeric degrees if no format, otherwise format & dp
19
+ # as per Geo.toLon()
20
+ #
21
+ # @param {String} [format]: Return value as 'd', 'dm', 'dms'
22
+ # @param {Number} [dp=0|2|4]: No of decimal places to display
23
+ # @returns {Number|String} Numeric degrees if no format specified, otherwise deg/min/sec
24
+ #
25
+ # @requires Geo
26
+
27
+ def to_lon format, dp
28
+ return lon if !format
29
+ Geo.to_lon lon, format, dp
30
+ end
31
+
32
+
33
+ # Returns a string representation of this point; format and dp as per lat()/lon()
34
+ #
35
+ # @param {String} [format]: Return value as 'd', 'dm', 'dms'
36
+ # @param {Number} [dp=0|2|4]: No of decimal places to display
37
+
38
+ # @returns {String} Comma-separated latitude/longitude
39
+ #
40
+
41
+ def to_s format = :dms, dp = 0
42
+ format ||= :dms
43
+
44
+ return '-,-' if !lat || !lon
45
+
46
+ _lat = Geo.to_lat lat, format, dp
47
+ _lon = Geo.to_lon lon, format, dp
48
+
49
+ "#{_lat}, #{_lon}"
50
+ end
51
+ end
52
+ end
@@ -51,7 +51,8 @@ describe GeoCalc do
51
51
  # Midpoint: 54°21′44″N, 004°31′50″W
52
52
  describe '#midpoint_to' do
53
53
  it 'should return the initial bearing from p1 to p2 as 011 16 31' do
54
- @p1.midpoint_to(@p2).to_dms.should match /54.+21.+44.+N, 004.+31.+50.+W/
54
+ mp = @p1.midpoint_to(@p2)
55
+ mp.to_dms.should match /54.+21.+44.+N, 004.+31.+50.+W/
55
56
  end
56
57
  end
57
58
  end # context
@@ -94,7 +95,7 @@ describe GeoCalc do
94
95
  # Intersection point: 50°54′06″N, 004°29′39″E
95
96
  describe '#intersection' do
96
97
  it 'should return the intersection between p1 and p2 as (50 54 06 N, 004 29 39 E)' do
97
- GeoCalc.intersection(@p1, @brng1, @p2, @brng2).to_dms.should match /50.+54.+06.+N, 004.+29.+39.+E/
98
+ GeoCalc::Calc::Intersection.intersection(@p1, @brng1, @p2, @brng2).to_dms.should match /50.+54.+06.+N, 004.+29.+39.+E/
98
99
  end
99
100
  end
100
101
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ # - www.movable-type.co.uk/scripts/latlong.html
4
+ describe GeoPoint do
5
+ describe 'ruby core Class extensions' do
6
+ describe 'Array extension' do
7
+ describe '#to_lat' do
8
+ it 'should return latitude as first element' do
9
+ @arr = [4, 27]
10
+ @arr.to_lat.should == 4
11
+ end
12
+
13
+ it 'should return latitude as #to_lng of first element' do
14
+ @arr = ["4.1", 27]
15
+ @arr.to_lat.should == 4.1
16
+ end
17
+ end
18
+
19
+ describe '#to_lng' do
20
+ it 'should return latitude degree value for 360' do
21
+ @arr = [4, 27]
22
+ @arr.to_lng.should == 27
23
+ end
24
+
25
+ it 'should return latitude as #to_lng of first element' do
26
+ @arr = [4, "27.2"]
27
+ @arr.to_lng.should == 27.2
28
+ end
29
+ end
30
+
31
+ describe '#to_lat_lng' do
32
+ it 'should return Array with lat, lng' do
33
+ @arr = ["3", {:lng => "2"}]
34
+ @arr.to_lat_lng.should == [3, 2]
35
+ end
36
+ end
37
+
38
+ describe '#geo_point' do
39
+ it 'should return a GeoPoint' do
40
+ @p = [3, 2].geo_point
41
+ @p.should be_a(GeoPoint)
42
+ @p.to_lat_lng.should == [3, 2]
43
+ end
44
+ end
45
+ end # Array
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ describe GeoPoint do
2
+ describe 'ruby core Class extensions' do
3
+ describe 'Hash extension' do
4
+ describe '#to_lat' do
5
+ it 'should return latitude as #to_lat on the value for key :lat' do
6
+ @hash = {:lat => 4}
7
+ @hash.to_lat.should == 4
8
+ end
9
+
10
+ it 'should return latitude as #to_lat on the value for key :latitude' do
11
+ @hash = {:latitude => "7"}
12
+ @hash.to_lat.should == 7
13
+ end
14
+ end
15
+
16
+ describe '#to_lng' do
17
+ it 'should return latitude as #to_lng on the value for key :lng' do
18
+ @hash = {:lng => 2}
19
+ @hash.to_lng.should == 2
20
+ end
21
+
22
+ it 'should return latitude as #to_lng on the value for key :longitude' do
23
+ @hash = {:longitude => "3.1"}
24
+ @hash.to_lng.should == 3.1
25
+ end
26
+ end
27
+
28
+ describe '#to_lat_lng' do
29
+ it 'should return Array with lat, lng' do
30
+ @hash = {:lng => 2, :lat => "3"}
31
+ @hash.to_lat_lng.should == [3, 2]
32
+ end
33
+ end
34
+
35
+ describe '#geo_point' do
36
+ it 'should return a GeoPoint' do
37
+ @p = {:lng => 2, :lat => "3"}.geo_point
38
+ @p.should be_a(GeoPoint)
39
+ @p.to_lat_lng.should == [3, 2]
40
+ end
41
+ end
42
+ end # Hash
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ # - www.movable-type.co.uk/scripts/latlong.html
4
+ describe GeoPoint do
5
+ describe 'ruby core Class extensions' do
6
+ describe NumericGeoExt do
7
+ describe '#to_rad' do
8
+ it 'should convert 0 degrees to 0 radians' do
9
+ 0.to_rad.should == 0
10
+ end
11
+
12
+ it 'should convert 180 degrees to PI radians' do
13
+ 180.to_rad.should == Math::PI
14
+ end
15
+
16
+ it 'should convert 360 degrees to 6.28 radians' do
17
+ 360.to_rad.should == Math::PI*2
18
+ end
19
+ end
20
+
21
+ describe '#to_radians' do
22
+ it 'should alias to_rad' do
23
+ 360.to_rad.should == 360.to_radians
24
+ end
25
+ end
26
+
27
+ describe '#to_deg' do
28
+ it 'should convert 0 radians to 0 degrees' do
29
+ 0.to_deg.should == 0
30
+ end
31
+
32
+ it 'should convert PI radians to 180 degrees' do
33
+ 180.to_rad.to_deg.should be_within(0.01).of(180)
34
+ end
35
+
36
+ it 'should convert 6.28 radians to 360 degrees' do
37
+ 360.to_rad.to_deg.should be_within(0.01).of(360)
38
+ end
39
+ end
40
+
41
+ describe '#to_degrees' do
42
+ it 'should alias to_deg' do
43
+ 360.to_deg.should == 360.to_degrees
44
+ end
45
+ end
46
+
47
+ describe '#to_fixed' do
48
+ it 'should make precision 4' do
49
+ 1.123456.to_fixed(2).should == '1.12'
50
+ end
51
+ end
52
+
53
+ describe '#to_precision' do
54
+ it 'should alis to_fixed' do
55
+ 1.123456.to_precision(4).should == '1.1235'
56
+ end
57
+ end
58
+
59
+ describe '#normalize' do
60
+ it 'should turn 180 deg and normalize' do
61
+ 361.normalize_deg(180).should == 181
62
+ end
63
+ it 'should normalize deg' do
64
+ 361.normalize_deg.should == 1
65
+ end
66
+
67
+ it 'should alias with #normalize_degrees' do
68
+ 362.normalize_degrees.should == 2
69
+ end
70
+ end
71
+ end # NumericGeoExt
72
+ end
73
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ # - www.movable-type.co.uk/scripts/latlong.html
4
+ describe GeoPoint do
5
+ describe 'ruby core Class extensions' do
6
+ describe 'String extension' do
7
+ describe '#to_lat' do
8
+ it 'should not return latitude on empty String' do
9
+ @str = ""
10
+ lambda { @str.to_lat}.should raise_error
11
+ end
12
+
13
+ it 'should return latitude' do
14
+ @str = "4"
15
+ @str.to_lat.should == 4
16
+ end
17
+
18
+ it 'should convert to latitude' do
19
+ @str = "50 03 59N"
20
+ @str.to_lat.should be_within(0.4).of(50)
21
+ end
22
+ end
23
+
24
+ describe '#to_lng' do
25
+ it 'should not return longitude on empty String' do
26
+ @str = ""
27
+ lambda { @str.to_lng}.should raise_error
28
+ end
29
+
30
+ it 'should return latitude' do
31
+ @str = "4"
32
+ @str.to_lat.should == 4
33
+ end
34
+
35
+ it 'should convert to latitude' do
36
+ @str = "50 03 59E"
37
+ @str.to_lat.should be_within(0.4).of(50)
38
+ end
39
+ end
40
+
41
+ describe '#to_lat_lng' do
42
+ it 'should return Array with lat, lng' do
43
+ @str = "4, 3"
44
+ @str.to_lat_lng.should == [4, 3]
45
+ end
46
+
47
+ it 'should raise error if only latitude' do
48
+ @str = "4"
49
+ lambda { @str.to_lat_lng}.should raise_error
50
+ end
51
+
52
+ it 'should raise error if "," but only latitude' do
53
+ @str = "4,"
54
+ lambda { @str.to_lat_lng}.should raise_error
55
+ end
56
+
57
+ it 'should raise error if "," in bad position' do
58
+ @str = ", 4 3"
59
+ lambda { @str.to_lat_lng}.should raise_error
60
+ end
61
+
62
+ it 'should raise error if not using "," as seperator' do
63
+ @str = "4; 3"
64
+ lambda { @str.to_lat_lng}.should raise_error
65
+ end
66
+ end
67
+
68
+ describe '#geo_point' do
69
+ it 'should return a GeoPoint' do
70
+ @p = "3, 2".geo_point
71
+ @p.should be_a(GeoPoint)
72
+ @p.to_lat_lng.should == [3, 2]
73
+ end
74
+ end
75
+ end # String
76
+ end
77
+ end
@@ -3,76 +3,15 @@ require 'spec_helper'
3
3
  # - www.movable-type.co.uk/scripts/latlong.html
4
4
  describe GeoPoint do
5
5
  describe 'ruby core Class extensions' do
6
- describe NumericGeoExt do
7
- describe '#to_rad' do
8
- it 'should convert 0 degrees to 0 radians' do
9
- 0.to_rad.should == 0
10
- end
11
-
12
- it 'should convert 180 degrees to PI radians' do
13
- 180.to_rad.should == Math::PI
14
- end
15
-
16
- it 'should convert 360 degrees to 6.28 radians' do
17
- 360.to_rad.should == Math::PI*2
18
- end
19
- end
20
-
21
- describe '#to_radians' do
22
- it 'should alias to_rad' do
23
- 360.to_rad.should == 360.to_radians
24
- end
25
- end
26
-
27
- describe '#to_deg' do
28
- it 'should convert 0 radians to 0 degrees' do
29
- 0.to_deg.should == 0
30
- end
31
-
32
- it 'should convert PI radians to 180 degrees' do
33
- 180.to_rad.to_deg.should be_within(0.01).of(180)
34
- end
35
-
36
- it 'should convert 6.28 radians to 360 degrees' do
37
- 360.to_rad.to_deg.should be_within(0.01).of(360)
38
- end
39
- end
40
-
41
- describe '#to_degrees' do
42
- it 'should alias to_deg' do
43
- 360.to_deg.should == 360.to_degrees
44
- end
45
- end
46
-
47
- describe '#to_fixed' do
48
- it 'should make precision 4' do
49
- 1.123456.to_fixed(2).should == '1.12'
50
- end
51
- end
52
-
53
- describe '#to_precision' do
54
- it 'should alis to_fixed' do
55
- 1.123456.to_precision(4).should == '1.1235'
56
- end
57
- end
58
-
59
- describe '#normalize' do
60
- it 'should turn 180 deg and normalize' do
61
- 361.normalize_deg(180).should == 181
62
- end
63
- it 'should normalize deg' do
64
- 361.normalize_deg.should == 1
65
- end
66
-
67
- it 'should alias with #normalize_degrees' do
68
- 362.normalize_degrees.should == 2
69
- end
70
- end
71
- end # NumericGeoExt
72
-
73
6
  describe NumericLatLngExt do
74
7
  describe 'Fixnum extension' do
75
8
  describe '#to_lat' do
9
+ it 'should set origin at 0,0' do
10
+ origin = [0, 0].geo_point
11
+ origin.lat.should == 0
12
+ origin.lng.should == 0
13
+ end
14
+
76
15
  it 'should return latitude degree value for 360' do
77
16
  360.to_lat.should == 0
78
17
  end
@@ -114,159 +53,6 @@ describe GeoPoint do
114
53
  end
115
54
  end
116
55
  end
117
-
118
- describe 'Array extension' do
119
- describe '#to_lat' do
120
- it 'should return latitude as first element' do
121
- @arr = [4, 27]
122
- @arr.to_lat.should == 4
123
- end
124
-
125
- it 'should return latitude as #to_lng of first element' do
126
- @arr = ["4.1", 27]
127
- @arr.to_lat.should == 4.1
128
- end
129
- end
130
-
131
- describe '#to_lng' do
132
- it 'should return latitude degree value for 360' do
133
- @arr = [4, 27]
134
- @arr.to_lng.should == 27
135
- end
136
-
137
- it 'should return latitude as #to_lng of first element' do
138
- @arr = [4, "27.2"]
139
- @arr.to_lng.should == 27.2
140
- end
141
- end
142
-
143
- describe '#to_lat_lng' do
144
- it 'should return Array with lat, lng' do
145
- @arr = ["3", {:lng => "2"}]
146
- @arr.to_lat_lng.should == [3, 2]
147
- end
148
- end
149
-
150
- describe '#geo_point' do
151
- it 'should return a GeoPoint' do
152
- @p = [3, 2].geo_point
153
- @p.should be_a(GeoPoint)
154
- @p.to_lat_lng.should == [3, 2]
155
- end
156
- end
157
- end # Array
158
-
159
- describe 'Hash extension' do
160
- describe '#to_lat' do
161
- it 'should return latitude as #to_lat on the value for key :lat' do
162
- @hash = {:lat => 4}
163
- @hash.to_lat.should == 4
164
- end
165
-
166
- it 'should return latitude as #to_lat on the value for key :latitude' do
167
- @hash = {:latitude => "7"}
168
- @hash.to_lat.should == 7
169
- end
170
- end
171
-
172
- describe '#to_lng' do
173
- it 'should return latitude as #to_lng on the value for key :lng' do
174
- @hash = {:lng => 2}
175
- @hash.to_lng.should == 2
176
- end
177
-
178
- it 'should return latitude as #to_lng on the value for key :longitude' do
179
- @hash = {:longitude => "3.1"}
180
- @hash.to_lng.should == 3.1
181
- end
182
- end
183
-
184
- describe '#to_lat_lng' do
185
- it 'should return Array with lat, lng' do
186
- @hash = {:lng => 2, :lat => "3"}
187
- @hash.to_lat_lng.should == [3, 2]
188
- end
189
- end
190
-
191
- describe '#geo_point' do
192
- it 'should return a GeoPoint' do
193
- @p = {:lng => 2, :lat => "3"}.geo_point
194
- @p.should be_a(GeoPoint)
195
- @p.to_lat_lng.should == [3, 2]
196
- end
197
- end
198
- end # Hash
199
-
200
- describe 'String extension' do
201
- describe '#to_lat' do
202
- it 'should not return latitude on empty String' do
203
- @str = ""
204
- lambda { @str.to_lat}.should raise_error
205
- end
206
-
207
- it 'should return latitude' do
208
- @str = "4"
209
- @str.to_lat.should == 4
210
- end
211
-
212
- it 'should convert to latitude' do
213
- @str = "50 03 59N"
214
- @str.to_lat.should be_within(0.4).of(50)
215
- end
216
- end
217
-
218
- describe '#to_lng' do
219
- it 'should not return longitude on empty String' do
220
- @str = ""
221
- lambda { @str.to_lng}.should raise_error
222
- end
223
-
224
- it 'should return latitude' do
225
- @str = "4"
226
- @str.to_lat.should == 4
227
- end
228
-
229
- it 'should convert to latitude' do
230
- @str = "50 03 59E"
231
- @str.to_lat.should be_within(0.4).of(50)
232
- end
233
- end
234
-
235
- describe '#to_lat_lng' do
236
- it 'should return Array with lat, lng' do
237
- @str = "4, 3"
238
- @str.to_lat_lng.should == [4, 3]
239
- end
240
-
241
- it 'should raise error if only latitude' do
242
- @str = "4"
243
- lambda { @str.to_lat_lng}.should raise_error
244
- end
245
-
246
- it 'should raise error if "," but only latitude' do
247
- @str = "4,"
248
- lambda { @str.to_lat_lng}.should raise_error
249
- end
250
-
251
- it 'should raise error if "," in bad position' do
252
- @str = ", 4 3"
253
- lambda { @str.to_lat_lng}.should raise_error
254
- end
255
-
256
- it 'should raise error if not using "," as seperator' do
257
- @str = "4; 3"
258
- lambda { @str.to_lat_lng}.should raise_error
259
- end
260
- end
261
-
262
- describe '#geo_point' do
263
- it 'should return a GeoPoint' do
264
- @p = "3, 2".geo_point
265
- @p.should be_a(GeoPoint)
266
- @p.to_lat_lng.should == [3, 2]
267
- end
268
- end
269
- end # String
270
56
  end
271
57
  end
272
58
  end
@@ -32,7 +32,7 @@ describe GeoPoint do
32
32
  p1 = GeoPoint.new "58 38 38N, 003 04 12W"
33
33
  p1.should be_a(GeoPoint)
34
34
  p1.lat.should be_within(0.5).of(58.38)
35
- p1.lon.should be_within(0.5).of(356.5)
35
+ p1.lon.should be_within(0.5).of(-3)
36
36
  p1.unit.should == :degrees
37
37
  p1.radius.should == 6371
38
38
  end
@@ -43,7 +43,7 @@ describe GeoPoint do
43
43
  p1 = GeoPoint.new "(58 38 38N, 003 04 12W)"
44
44
  p1.should be_a(GeoPoint)
45
45
  p1.lat.should be_within(0.5).of(58.38)
46
- p1.lon.should be_within(0.5).of(356.5) # W is negative, then normalize
46
+ p1.lon.should be_within(0.5).of(-3) # W is negative, then normalize
47
47
  p1.unit.should == :degrees
48
48
  p1.radius.should == 6371
49
49
  end
@@ -138,7 +138,7 @@ describe GeoPoint do
138
138
  p1 = GeoPoint.new "58 38 38N", "003 04 12W"
139
139
  p1.should be_a(GeoPoint)
140
140
  p1.lat.should be_within(0.5).of(58.38)
141
- p1.lon.should be_within(0.5).of(356.5)
141
+ p1.lon.should be_within(0.5).of(-3)
142
142
  p1.unit.should == :degrees
143
143
  p1.radius.should == 6371
144
144
  end
@@ -146,6 +146,31 @@ describe GeoPoint do
146
146
  end
147
147
  end # initializer
148
148
 
149
+ describe '#reverse_point' do
150
+ before :each do
151
+ @p = GeoPoint.new -2, 5
152
+ end
153
+
154
+ it 'should return reverse GeoPoint (2, -5)' do
155
+ @p2 = @p.reverse_point
156
+ @p2.should_not == @p
157
+ @p2.lat.should == 2
158
+ @p2.lng.should == -5
159
+ end
160
+ end
161
+
162
+ describe '#reverse_point!' do
163
+ before :each do
164
+ @p = GeoPoint.new -2, 5
165
+ end
166
+
167
+ it 'should return reverse GeoPoint (2, -5)' do
168
+ @p.reverse_point!
169
+ @p.lat.should == 2
170
+ @p.lng.should == -5
171
+ end
172
+ end
173
+
149
174
  describe '#to_s' do
150
175
  before :each do
151
176
  @p1 = GeoPoint.new 50.1, 5
@@ -192,6 +217,17 @@ describe GeoPoint do
192
217
  @p1.lat.should == 60
193
218
  end
194
219
 
220
+ it 'should set new latitude -2' do
221
+ @p1.lat = -2
222
+ @p1.lat.should == -2
223
+ end
224
+
225
+ it 'should convert latitude -182 to -2' do
226
+ @p1.lat = -2
227
+ @p1.lat.should == -2
228
+ end
229
+
230
+
195
231
  it 'should set new latitude within allowed range' do
196
232
  @p1.lat = 520
197
233
  @p1.lat.should be_between(0, 360)
@@ -91,7 +91,7 @@ describe GeoPoint do
91
91
  it 'should convert 58.3 to a longitude String' do
92
92
  brng = Geo.to_brng(-58.3)
93
93
  brng.to_f.should be_between(0, 360)
94
- expr = Regexp.escape "42".concat("\u00B0", "42", "\u2032", "00")
94
+ expr = Regexp.escape "301".concat("\u00B0", "42", "\u2032", "00")
95
95
  brng.should match expr
96
96
  end
97
97
  end