geo_calc 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -9,7 +9,7 @@ gem 'activesupport', '>= 3.0.1'
9
9
  # Add dependencies to develop your gem here.
10
10
  # Include everything needed to run rake, tests, features, etc.
11
11
  group :development do
12
- gem "geo_point", ">= 0.2.1"
12
+ gem "geo_point", "~> 0.2.3"
13
13
  gem "rspec", ">= 2.5.0"
14
14
  gem "bundler", ">= 1"
15
15
  gem "jeweler", ">= 1.5.2"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.4
1
+ 0.7.5
data/geo_calc.gemspec CHANGED
@@ -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.4"
8
+ s.version = "0.7.5"
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-19}
12
+ s.date = %q{2011-06-20}
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 = [
@@ -66,7 +66,7 @@ Gem::Specification.new do |s|
66
66
  s.add_runtime_dependency(%q<geo_units>, ["~> 0.2.1"])
67
67
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
68
68
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
69
- s.add_development_dependency(%q<geo_point>, [">= 0.2.1"])
69
+ s.add_development_dependency(%q<geo_point>, ["~> 0.2.3"])
70
70
  s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
71
71
  s.add_development_dependency(%q<bundler>, [">= 1"])
72
72
  s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
@@ -78,7 +78,7 @@ Gem::Specification.new do |s|
78
78
  s.add_dependency(%q<geo_units>, ["~> 0.2.1"])
79
79
  s.add_dependency(%q<i18n>, [">= 0"])
80
80
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
81
- s.add_dependency(%q<geo_point>, [">= 0.2.1"])
81
+ s.add_dependency(%q<geo_point>, ["~> 0.2.3"])
82
82
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
83
83
  s.add_dependency(%q<bundler>, [">= 1"])
84
84
  s.add_dependency(%q<jeweler>, [">= 1.5.2"])
@@ -91,7 +91,7 @@ Gem::Specification.new do |s|
91
91
  s.add_dependency(%q<geo_units>, ["~> 0.2.1"])
92
92
  s.add_dependency(%q<i18n>, [">= 0"])
93
93
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
94
- s.add_dependency(%q<geo_point>, [">= 0.2.1"])
94
+ s.add_dependency(%q<geo_point>, ["~> 0.2.3"])
95
95
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
96
96
  s.add_dependency(%q<bundler>, [">= 1"])
97
97
  s.add_dependency(%q<jeweler>, [">= 1.5.2"])
@@ -8,14 +8,25 @@ class Array
8
8
  to_lat_lng.reverse
9
9
  end
10
10
 
11
+ # Assumes by default that the order is lat, lng
12
+ # If GeoPoint is defined, can reverse this order depending on #coord_mode class variable
11
13
  def to_lat
12
14
  raise "Array must contain at least one element to return the latitude" if empty?
13
- first.to_lat
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
14
20
  end
15
21
 
22
+ # see(#to_lat)
16
23
  def to_lng
17
24
  raise "Array must contain at least two elements to return the longitude" if !self[1]
18
- self[1].to_lng
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
29
+ end
19
30
  end
20
31
 
21
32
  def trim
@@ -12,11 +12,15 @@ class String
12
12
  end
13
13
 
14
14
  def to_lat_lng
15
- geo_clean.split(',').to_lat_lng
15
+ a = geo_clean.split(',').map(&:strip)
16
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
17
+ a.to_lat_lng
16
18
  end
17
19
 
18
20
  def to_lng_lat
19
- geo_clean.split(',').to_lng_lat
21
+ a = geo_clean.split(',')
22
+ a = (a.last.is_a?(String) && a.last =~ /[N|S]$/) ? a.reverse : a
23
+ a.to_lng_lat
20
24
  end
21
25
 
22
26
  def to_lat
@@ -1,39 +1,75 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  # - www.movable-type.co.uk/scripts/latlong.html
4
- describe GeoPoint do
4
+ describe GeoCalc do
5
5
  describe 'ruby core Class extensions' do
6
6
  describe 'Array extension' do
7
7
  describe '#to_lat' do
8
- it 'should return latitude as first element' do
9
- @arr = [4, 27]
10
- @arr.to_lat.should == 4
8
+ it 'should return latitude as first element' do
9
+ [4, 27].to_lat.should == 4
11
10
  end
12
11
 
13
12
  it 'should return latitude as #to_lng of first element' do
14
- @arr = ["4.1", 27]
15
- @arr.to_lat.should == 4.1
13
+ ["4.1", 27].to_lat.should == 4.1
16
14
  end
17
15
  end
18
16
 
19
17
  describe '#to_lng' do
20
18
  it 'should return latitude degree value for 360' do
21
- @arr = [4, 27]
22
- @arr.to_lng.should == 27
19
+ [4, 27].to_lng.should == 27
23
20
  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
21
+
22
+ it 'should return latitude as #to_lng of first element' do
23
+ [4, "27.2"].to_lng.should == 27.2
28
24
  end
29
25
  end
30
-
26
+
31
27
  describe '#to_lat_lng' do
32
28
  it 'should return Array with lat, lng' do
33
- @arr = ["3", {:lng => "2"}]
34
- @arr.to_lat_lng.should == [3, 2]
29
+ ["3", {:lng => "2"}].to_lat_lng.should == [3, 2]
30
+ end
31
+ end
32
+
33
+ describe '#to_lng_lat' do
34
+ it 'should return Array with lat, lng' do
35
+ ["3", {:lng => "2"}].to_lng_lat.should == [2, 3]
35
36
  end
36
37
  end
37
38
  end # Array
38
39
  end
39
- end
40
+ end
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
+
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'geo_point'
2
3
 
3
4
  describe GeoPoint do
4
5
  describe 'ruby core Class extensions' do
@@ -27,12 +28,64 @@ describe GeoPoint do
27
28
  end
28
29
  end
29
30
 
30
- describe '#to_lat_lng' do
31
+ describe '#to_lat_lng' do
31
32
  it 'should return Array with lat, lng' do
32
33
  @hash = {:lng => 2, :lat => "3"}
33
34
  @hash.to_lat_lng.should == [3, 2]
34
35
  end
35
36
  end
37
+
38
+ describe '#to_lng_lat' do
39
+ it 'should return Array with lat, lng' do
40
+ @hash = {:lng => 2, :lat => "3"}
41
+ @hash.to_lng_lat.should == [2, 3]
42
+ end
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
+
36
89
  end # Hash
37
90
  end
38
91
  end
@@ -44,6 +44,22 @@ describe GeoPoint do
44
44
  @str.to_lat_lng.should == [4, 3]
45
45
  end
46
46
 
47
+ it 'should return Array with lat, lng' do
48
+ @str = "04 38 08 W, 58 38 08 N"
49
+ arr = @str.to_lat_lng
50
+ # W = west is longitude and should be last!
51
+ arr.last.should < 0
52
+ arr.first.should > 58
53
+ end
54
+
55
+ it 'should return Array with lng, lat' do
56
+ @str = "58 38 08 N, 04 38 08 W"
57
+ arr = @str.to_lat_lng
58
+ # W = west is longitude and should be last!
59
+ arr.last.should < 0
60
+ arr.first.should > 58
61
+ end
62
+
47
63
  it 'should raise error if only latitude' do
48
64
  @str = "4"
49
65
  lambda { @str.to_lat_lng}.should raise_error
@@ -64,6 +80,50 @@ describe GeoPoint do
64
80
  lambda { @str.to_lat_lng}.should raise_error
65
81
  end
66
82
  end
83
+
84
+ describe '#to_lng_lat' do
85
+ it 'should return Array with lng, lat' do
86
+ @str = "4, 3"
87
+ @str.to_lng_lat.should == [3, 4]
88
+ end
89
+
90
+ it 'should return Array with lng, lat' do
91
+ @str = "04 38 08 W, 58 38 08 N"
92
+ arr = @str.to_lng_lat
93
+ # W = west is longitude and should be first!
94
+ arr.first.should < 5
95
+ arr.last.should > 58
96
+ end
97
+
98
+ it 'should return Array with lng, lat' do
99
+ @str = "58 38 08 N, 04 38 08 W"
100
+ arr = @str.to_lng_lat
101
+ # W = west is longitude and should be first!
102
+ arr.first.should < 5
103
+ arr.last.should > 58
104
+ end
105
+
106
+ it 'should raise error if only latitude' do
107
+ @str = "4"
108
+ lambda { @str.to_lng_lat}.should raise_error
109
+ end
110
+
111
+ it 'should raise error if "," but only latitude' do
112
+ @str = "4,"
113
+ lambda { @str.to_lng_lat}.should raise_error
114
+ end
115
+
116
+ it 'should raise error if "," in bad position' do
117
+ @str = ", 4 3"
118
+ lambda { @str.to_lng_lat}.should raise_error
119
+ end
120
+
121
+ it 'should raise error if not using "," as seperator' do
122
+ @str = "4; 3"
123
+ lambda { @str.to_lng_lat}.should raise_error
124
+ end
125
+ end
126
+
67
127
  end # String
68
128
  end
69
129
  end
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.4
4
+ version: 0.7.5
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-19 00:00:00.000000000Z
12
+ date: 2011-06-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: require_all
16
- requirement: &2164287640 !ruby/object:Gem::Requirement
16
+ requirement: &2156562180 !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: *2164287640
24
+ version_requirements: *2156562180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sugar-high
27
- requirement: &2164286840 !ruby/object:Gem::Requirement
27
+ requirement: &2156560820 !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: *2164286840
35
+ version_requirements: *2156560820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: geo_units
38
- requirement: &2164286060 !ruby/object:Gem::Requirement
38
+ requirement: &2156558720 !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: *2164286060
46
+ version_requirements: *2156558720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: i18n
49
- requirement: &2164285160 !ruby/object:Gem::Requirement
49
+ requirement: &2156557460 !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: *2164285160
57
+ version_requirements: *2156557460
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &2164284460 !ruby/object:Gem::Requirement
60
+ requirement: &2156556280 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,21 +65,21 @@ dependencies:
65
65
  version: 3.0.1
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2164284460
68
+ version_requirements: *2156556280
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: geo_point
71
- requirement: &2164283680 !ruby/object:Gem::Requirement
71
+ requirement: &2156554680 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
- - - ! '>='
74
+ - - ~>
75
75
  - !ruby/object:Gem::Version
76
- version: 0.2.1
76
+ version: 0.2.3
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2164283680
79
+ version_requirements: *2156554680
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &2164282980 !ruby/object:Gem::Requirement
82
+ requirement: &2156553040 !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: *2164282980
90
+ version_requirements: *2156553040
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: bundler
93
- requirement: &2164282340 !ruby/object:Gem::Requirement
93
+ requirement: &2156551140 !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: *2164282340
101
+ version_requirements: *2156551140
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: jeweler
104
- requirement: &2162210200 !ruby/object:Gem::Requirement
104
+ requirement: &2156549160 !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: *2162210200
112
+ version_requirements: *2156549160
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rcov
115
- requirement: &2162209620 !ruby/object:Gem::Requirement
115
+ requirement: &2156546680 !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: *2162209620
123
+ version_requirements: *2156546680
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rake
126
- requirement: &2162209080 !ruby/object:Gem::Requirement
126
+ requirement: &2156545880 !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: *2162209080
134
+ version_requirements: *2156545880
135
135
  description: Geo calculations in ruby and javascript
136
136
  email: kmandrup@gmail.com
137
137
  executables: []
@@ -188,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
188
  version: '0'
189
189
  segments:
190
190
  - 0
191
- hash: -3260923342157281303
191
+ hash: -189258895816106998
192
192
  required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  none: false
194
194
  requirements: