geo_calc 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.5
1
+ 0.7.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{geo_calc}
8
- s.version = "0.7.5"
8
+ s.version = "0.7.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kristian Mandrup}]
12
- s.date = %q{2011-06-20}
12
+ s.date = %q{2011-06-21}
13
13
  s.description = %q{Geo calculations in ruby and javascript}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -43,6 +43,8 @@ Gem::Specification.new do |s|
43
43
  "lib/geo_calc/pretty_print.rb",
44
44
  "spec/geo_calc/calculations_spec.rb",
45
45
  "spec/geo_calc/core_ext/array_ext_spec.rb",
46
+ "spec/geo_calc/core_ext/coord_mode_spec.rb",
47
+ "spec/geo_calc/core_ext/dms_coord_mode_spec.rb",
46
48
  "spec/geo_calc/core_ext/hash_ext_spec.rb",
47
49
  "spec/geo_calc/core_ext/numeric_geo_ext_spec.rb",
48
50
  "spec/geo_calc/core_ext/string_ext_spec.rb",
@@ -5,27 +5,30 @@ class Array
5
5
  end
6
6
 
7
7
  def to_lng_lat
8
- to_lat_lng.reverse
8
+ [to_lng, to_lat]
9
9
  end
10
10
 
11
11
  # Assumes by default that the order is lat, lng
12
12
  # If GeoPoint is defined, can reverse this order depending on #coord_mode class variable
13
13
  def to_lat
14
14
  raise "Array must contain at least one element to return the latitude" if empty?
15
- if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
16
- self[1].to_lat
17
- else
18
- first.to_lat
19
- end
15
+ subject, other = (defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat) ? [self[1], first] : [first, self[1]]
16
+
17
+ begin
18
+ subject.to_lat
19
+ rescue GeoDirectionMisMatch
20
+ other.to_lat
21
+ end
20
22
  end
21
23
 
22
24
  # see(#to_lat)
23
25
  def to_lng
24
26
  raise "Array must contain at least two elements to return the longitude" if !self[1]
25
- if defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat
26
- first.to_lng
27
- else
28
- self[1].to_lng
27
+ subject, other = (defined?(GeoPoint) && GeoPoint.respond_to?(:coord_mode) && GeoPoint.coord_mode == :lng_lat) ? [first, self[1]] : [self[1], first]
28
+ begin
29
+ subject.to_lng
30
+ rescue GeoDirectionMisMatch
31
+ other.to_lng
29
32
  end
30
33
  end
31
34
 
@@ -1,3 +1,5 @@
1
+ class GeoDirectionMisMatch < StandardError; end;
2
+
1
3
  class String
2
4
  def to_rad
3
5
  parse_dms.to_rad
@@ -19,20 +21,22 @@ class String
19
21
 
20
22
  def to_lng_lat
21
23
  a = geo_clean.split(',')
22
- a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
24
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
23
25
  a.to_lng_lat
24
26
  end
25
27
 
26
28
  def to_lat
27
29
  raise "An empty String has no latitude" if self.empty?
28
30
  s = geo_clean
31
+ raise GeoDirectionMisMatch, "Direction E and W signify longitude and thus can't be converted to latitude, was: #{self}" if s =~ /[W|E]$/
29
32
  s = s.parse_dms if s.respond_to? :parse_dms
30
33
  s.to_f.to_lat
31
34
  end
32
35
 
33
36
  def to_lng
34
37
  raise "An empty String has no latitude" if self.empty?
35
- s = geo_clean
38
+ s = geo_clean
39
+ raise GeoDirectionMisMatch, "Direction N and S signify latitude and thus can't be converted to longitude, was: #{self}" if s =~ /[N|S]$/
36
40
  s = s.parse_dms if s.respond_to? :parse_dms
37
41
  s.to_f.to_lng
38
42
  end
@@ -39,37 +39,3 @@ describe GeoCalc do
39
39
  end
40
40
  end
41
41
 
42
- class GeoPoint
43
- mattr_accessor :coord_mode
44
- end
45
-
46
- describe 'coord mode' do
47
- context 'coord_mode = :lng_lat' do
48
- before do
49
- GeoPoint.coord_mode = :lng_lat
50
- end
51
-
52
- it 'should not reverse array' do
53
- [2, 3].to_lng_lat.should == [2, 3]
54
- end
55
-
56
- it 'should reverse array' do
57
- [2, 3].to_lat_lng.should == [3, 2]
58
- end
59
- end
60
-
61
- context 'coord_mode = :lat_lng' do
62
- before do
63
- GeoPoint.coord_mode = :lat_lng
64
- end
65
-
66
- it 'should not reverse array' do
67
- [2, 3].to_lat_lng.should == [2, 3]
68
- end
69
-
70
- it 'should reverse array' do
71
- [2, 3].to_lng_lat.should == [3, 2]
72
- end
73
- end
74
- end
75
-
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ class GeoPoint
4
+ mattr_accessor :coord_mode
5
+ end
6
+
7
+ describe 'GeoPoint.coord_mode' do
8
+ # make shared example for default mode!
9
+ context 'coord_mode is :lat_lng' do
10
+ before do
11
+ GeoPoint.coord_mode = :lat_lng
12
+ end
13
+
14
+ describe 'Array' do
15
+ describe '#to_lng_lat' do
16
+ it 'should reverse array' do
17
+ [2, 3].to_lng_lat.should == [3, 2]
18
+ end
19
+
20
+ it 'should reverse array and convert strings to floats' do
21
+ ["23.5", "-48"].to_lng_lat.should == [-48, 23.5]
22
+ end
23
+ end
24
+
25
+ describe '#to_lat_lng' do
26
+ it 'should not reverse array' do
27
+ [2, 3].to_lat_lng.should == [2, 3]
28
+ end
29
+
30
+ it 'should not reverse array but convert strings to floats' do
31
+ ["23.5", "-48"].to_lat_lng.should == [23.5, -48]
32
+ end
33
+ end
34
+ end # Array
35
+
36
+ describe 'String' do
37
+ describe '#to_lat_lng' do
38
+ it 'should return Array in same order' do
39
+ "4, 3".to_lat_lng.should == [4, 3]
40
+ end
41
+ end
42
+
43
+ describe '#to_lng_lat' do
44
+ it 'should return reversed Array' do
45
+ "4, 3".to_lng_lat.should == [3,4]
46
+ end
47
+ end
48
+ end # String
49
+ end
50
+
51
+ context 'coord_mode is :lng_lat' do
52
+ before do
53
+ GeoPoint.coord_mode = :lng_lat
54
+ end
55
+
56
+ describe 'Array' do
57
+ describe '#to_lng_lat' do
58
+ it 'should not reverse array' do
59
+ [2, 3].to_lng_lat.should == [2, 3]
60
+ end
61
+
62
+ it 'should not reverse array but convert strings to floats' do
63
+ ["23.5", "-48"].to_lng_lat.should == [23.5, -48]
64
+ end
65
+ end
66
+
67
+ describe '#to_lat_lng' do
68
+ it 'should reverse array' do
69
+ [2, 3].to_lat_lng.should == [3, 2]
70
+ end
71
+
72
+ it 'should reverse array and convert strings to floats' do
73
+ ["23.5", "-48"].to_lat_lng.should == [-48, 23.5]
74
+ end
75
+ end
76
+ end # Array
77
+
78
+ describe 'String' do
79
+ describe '#to_lng_lat' do
80
+ it 'should return Array in same order' do
81
+ "4, 3".to_lng_lat.should == [4, 3]
82
+ end
83
+ end
84
+
85
+ describe '#to_lat_lng' do
86
+ it 'should return reversed Array' do
87
+ "4, 3".to_lat_lng.should == [3,4]
88
+ end
89
+ end
90
+ end # String
91
+
92
+ describe 'Hash' do
93
+ describe '#to_lng_lat' do
94
+ it 'should return Array with lng, lat' do
95
+ hash = {:lng => 2, :lat => "3"}
96
+ hash.to_lng_lat.should == [2, 3]
97
+ end
98
+ end
99
+
100
+ describe '#to_lat_lng' do
101
+ it 'should return Array with lat, lng' do
102
+ hash = {:lng => 2, :lat => "3"}
103
+ hash.to_lat_lng.should == [3, 2]
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ # require 'geo_point'
3
+
4
+ class GeoPoint
5
+ mattr_accessor :coord_mode
6
+ end
7
+
8
+ describe 'GeoPoint.coord_mode' do
9
+ # make shared example for default mode!
10
+ context 'coord_mode is :lng_lat' do
11
+ before do
12
+ GeoPoint.coord_mode = :lng_lat
13
+ end
14
+
15
+ after do
16
+ GeoPoint.coord_mode = :lat_lng
17
+ end
18
+
19
+ describe 'DMS Array' do
20
+ it 'should convert to (lng, lat) floats' do
21
+ arr = ["58 38 38N", "003 04 12W"].to_lng_lat
22
+ arr.first.should < 4
23
+ arr.last.should > 58
24
+ end
25
+
26
+ it 'should convert to (lng, lat) floats' do
27
+ arr = ["003 04 12W", "58 38 38N"].to_lng_lat
28
+ arr.first.should < 4
29
+ arr.last.should > 58
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'coord_mode is :lat_lng' do
35
+ before do
36
+ GeoPoint.coord_mode = :lat_lng
37
+ end
38
+
39
+ describe 'DMS Array' do
40
+ it 'should convert to (lat, lng) floats' do
41
+ arr = ["58 38 38N", "003 04 12W"].to_lng_lat
42
+ arr.first.should < 4
43
+ arr.last.should > 58
44
+ end
45
+
46
+ it 'should convert to (lng, lat) floats' do
47
+ arr = ["003 04 12W", "58 38 38N"].to_lng_lat
48
+ arr.first.should < 4
49
+ arr.last.should > 58
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -41,50 +41,6 @@ describe GeoPoint do
41
41
  @hash.to_lng_lat.should == [2, 3]
42
42
  end
43
43
  end
44
-
45
- context 'GeoPoint.coord_mode == :lng_lat' do
46
- describe '#to_coords' do
47
- it 'should return Array with lng, lat' do
48
- GeoPoint.coord_mode = :lng_lat
49
- @hash = {:lng => 2, :lat => "3"}
50
- @hash.to_coords.should == [2, 3]
51
- end
52
- end
53
- end
54
-
55
- context 'GeoPoint.coord_mode == :lat_lng' do
56
- describe '#to_coords' do
57
- it 'should return Array with lat, lng' do
58
- GeoPoint.coord_mode = :lat_lng
59
- @hash = {:lng => 2, :lat => "3"}
60
- @hash.to_coords.should == [3, 2]
61
- end
62
- end
63
- end
64
-
65
- describe '#geo_point' do
66
- context 'GeoPoint.coord_mode == :lat_lng' do
67
- it 'should return Array with lng, lat' do
68
- GeoPoint.coord_mode = :lat_lng
69
- @hash = {:lng => 2, :lat => "3"}
70
- p = @hash.geo_point
71
- puts p.inspect
72
-
73
- p.to_lng_lat.should == [2, 3]
74
- end
75
- end
76
-
77
- context 'GeoPoint.coord_mode == :lng_lat' do
78
- it 'should return Array with lng, lat' do
79
- GeoPoint.coord_mode = :lng_lat
80
- @hash = {:lng => 2, :lat => "3"}
81
- p = @hash.geo_point
82
- puts p.inspect
83
-
84
- p.to_lng_lat.should == [2, 3]
85
- end
86
- end
87
- end
88
44
 
89
45
  end # Hash
90
46
  end
@@ -19,6 +19,11 @@ describe GeoPoint do
19
19
  @str = "50 03 59N"
20
20
  @str.to_lat.should be_within(0.4).of(50)
21
21
  end
22
+
23
+ it 'should not allow conversion of longitude format to latitude' do
24
+ str = "50 03 59E"
25
+ lambda { str.to_lat}.should raise_error GeoDirectionMisMatch
26
+ end
22
27
  end
23
28
 
24
29
  describe '#to_lng' do
@@ -29,12 +34,17 @@ describe GeoPoint do
29
34
 
30
35
  it 'should return latitude' do
31
36
  @str = "4"
32
- @str.to_lat.should == 4
37
+ @str.to_lng.should == 4
33
38
  end
34
39
 
35
40
  it 'should convert to latitude' do
36
41
  @str = "50 03 59E"
37
- @str.to_lat.should be_within(0.4).of(50)
42
+ @str.to_lng.should be_within(0.4).of(50)
43
+ end
44
+
45
+ it 'should not allow conversion of latitude format to longitude' do
46
+ str = "50 03 59N"
47
+ lambda { str.to_lng}.should raise_error GeoDirectionMisMatch
38
48
  end
39
49
  end
40
50
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-20 00:00:00.000000000Z
12
+ date: 2011-06-21 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: require_all
16
- requirement: &2156562180 !ruby/object:Gem::Requirement
16
+ requirement: &2156297360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156562180
24
+ version_requirements: *2156297360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sugar-high
27
- requirement: &2156560820 !ruby/object:Gem::Requirement
27
+ requirement: &2156295980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.4.6.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156560820
35
+ version_requirements: *2156295980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: geo_units
38
- requirement: &2156558720 !ruby/object:Gem::Requirement
38
+ requirement: &2156295020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.2.1
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2156558720
46
+ version_requirements: *2156295020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &2156557460 !ruby/object:Gem::Requirement
49
+ requirement: &2156293800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2156557460
57
+ version_requirements: *2156293800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &2156556280 !ruby/object:Gem::Requirement
60
+ requirement: &2156292260 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 3.0.1
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2156556280
68
+ version_requirements: *2156292260
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: geo_point
71
- requirement: &2156554680 !ruby/object:Gem::Requirement
71
+ requirement: &2156290460 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.2.3
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2156554680
79
+ version_requirements: *2156290460
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &2156553040 !ruby/object:Gem::Requirement
82
+ requirement: &2156289600 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 2.5.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2156553040
90
+ version_requirements: *2156289600
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bundler
93
- requirement: &2156551140 !ruby/object:Gem::Requirement
93
+ requirement: &2156288620 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '1'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2156551140
101
+ version_requirements: *2156288620
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: jeweler
104
- requirement: &2156549160 !ruby/object:Gem::Requirement
104
+ requirement: &2156287480 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 1.5.2
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2156549160
112
+ version_requirements: *2156287480
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rcov
115
- requirement: &2156546680 !ruby/object:Gem::Requirement
115
+ requirement: &2156286380 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *2156546680
123
+ version_requirements: *2156286380
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rake
126
- requirement: &2156545880 !ruby/object:Gem::Requirement
126
+ requirement: &2156285480 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,7 +131,7 @@ dependencies:
131
131
  version: '0.9'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *2156545880
134
+ version_requirements: *2156285480
135
135
  description: Geo calculations in ruby and javascript
136
136
  email: kmandrup@gmail.com
137
137
  executables: []
@@ -166,6 +166,8 @@ files:
166
166
  - lib/geo_calc/pretty_print.rb
167
167
  - spec/geo_calc/calculations_spec.rb
168
168
  - spec/geo_calc/core_ext/array_ext_spec.rb
169
+ - spec/geo_calc/core_ext/coord_mode_spec.rb
170
+ - spec/geo_calc/core_ext/dms_coord_mode_spec.rb
169
171
  - spec/geo_calc/core_ext/hash_ext_spec.rb
170
172
  - spec/geo_calc/core_ext/numeric_geo_ext_spec.rb
171
173
  - spec/geo_calc/core_ext/string_ext_spec.rb
@@ -188,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
190
  version: '0'
189
191
  segments:
190
192
  - 0
191
- hash: -189258895816106998
193
+ hash: 1564414330621469388
192
194
  required_rubygems_version: !ruby/object:Gem::Requirement
193
195
  none: false
194
196
  requirements: