geo_units 0.2.4.1 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
 
4
- gem "sugar-high", '~> 0.4.6.2'
4
+ gem "sugar-high", '~> 0.4.9.3'
5
+ gem 'i18n'
6
+ gem 'activesupport'
5
7
 
6
8
  # Add dependencies to develop your gem here.
7
9
  # Include everything needed to run rake, tests, features, etc.
data/README.textile CHANGED
@@ -59,6 +59,17 @@ This module is included on the _Fixnum_ and _Float_ classes
59
59
  * normalize_lat
60
60
  * normalize_deg(shift)
61
61
 
62
+ h2. Using specific GeoUnits modules
63
+
64
+ The main GeoUnits module consist of various sub-modules that can be used on a case basis.
65
+ Including the _GeoUnits_ module will include and extend the base class with all these sub-modules.
66
+
67
+ class MyGeoThingy
68
+ include GeoUnits::Maps # various maps, mapping unit to some distance etc.
69
+ extend GeoUnits::Constants # useful geo constants
70
+
71
+ ...
72
+ end
62
73
 
63
74
  h2. Contributing to geo_units
64
75
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4.1
1
+ 0.2.5
data/geo_units.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{geo_units}
8
- s.version = "0.2.4.1"
8
+ s.version = "0.2.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-22}
12
+ s.date = %q{2011-06-24}
13
13
  s.description = %q{Easily convert between different distance units such as kms, miles etc.}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,10 +26,13 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "geo_units.gemspec",
28
28
  "lib/geo_units.rb",
29
+ "lib/geo_units/constants.rb",
29
30
  "lib/geo_units/converter.rb",
30
31
  "lib/geo_units/core_ext.rb",
31
32
  "lib/geo_units/dms_converter.rb",
33
+ "lib/geo_units/maps.rb",
32
34
  "lib/geo_units/numeric_ext.rb",
35
+ "lib/geo_units/unit_conversions.rb",
33
36
  "spec/geo_units/converter_spec.rb",
34
37
  "spec/geo_units/core_ext_spec.rb",
35
38
  "spec/geo_units/dms_converter_spec.rb",
@@ -47,20 +50,26 @@ Gem::Specification.new do |s|
47
50
  s.specification_version = 3
48
51
 
49
52
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
- s.add_runtime_dependency(%q<sugar-high>, ["~> 0.4.6.2"])
53
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.4.9.3"])
54
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
55
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
51
56
  s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
52
57
  s.add_development_dependency(%q<bundler>, ["~> 1.0.6"])
53
58
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
54
59
  s.add_development_dependency(%q<rcov>, [">= 0"])
55
60
  else
56
- s.add_dependency(%q<sugar-high>, ["~> 0.4.6.2"])
61
+ s.add_dependency(%q<sugar-high>, ["~> 0.4.9.3"])
62
+ s.add_dependency(%q<i18n>, [">= 0"])
63
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
64
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
58
65
  s.add_dependency(%q<bundler>, ["~> 1.0.6"])
59
66
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
60
67
  s.add_dependency(%q<rcov>, [">= 0"])
61
68
  end
62
69
  else
63
- s.add_dependency(%q<sugar-high>, ["~> 0.4.6.2"])
70
+ s.add_dependency(%q<sugar-high>, ["~> 0.4.9.3"])
71
+ s.add_dependency(%q<i18n>, [">= 0"])
72
+ s.add_dependency(%q<activesupport>, [">= 0"])
64
73
  s.add_dependency(%q<rspec>, [">= 2.5.0"])
65
74
  s.add_dependency(%q<bundler>, ["~> 1.0.6"])
66
75
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
@@ -0,0 +1,33 @@
1
+ module GeoUnits
2
+ module Constants
3
+ def radians_per_degree
4
+ Math::PI / 180.0
5
+ end
6
+
7
+ def pi_div_rad
8
+ 0.0174
9
+ end
10
+
11
+ def kms_per_mile
12
+ 1.609
13
+ end
14
+
15
+ def meters_per_feet
16
+ 3.2808399
17
+ end
18
+
19
+ def miles_per_latitude_degree
20
+ 69.1
21
+ end
22
+
23
+ def kms_per_latitude_degree
24
+ miles_per_latitude_degree * kms_per_mile
25
+ end
26
+
27
+ def latitude_degrees
28
+ earth_radius_map[:miles] / miles_per_latitude_degree
29
+ end
30
+
31
+ extend self
32
+ end
33
+ end
@@ -0,0 +1,73 @@
1
+ module GeoUnits
2
+ module Maps
3
+ def earth_radius_map
4
+ {
5
+ :miles => 3963.1676,
6
+ :kilometers => 6378.135,
7
+ :meters => 6378135,
8
+ :feet => 20925639.8
9
+ }
10
+ end
11
+
12
+ def earth_major_axis_radius_map
13
+ {
14
+ :miles => 3963.19059,
15
+ :kilometers => 6378.137,
16
+ :meters => 6378137,
17
+ :feet => 20925646.36
18
+ }
19
+ end
20
+
21
+ def earth_minor_axis_radius_map
22
+ {
23
+ :kilometers => 6356.7523142,
24
+ :miles => 3949.90276,
25
+ :meters => 6356752.3142,
26
+ :feet => 20855486.627
27
+ }
28
+ end
29
+
30
+ # from mongoid-geo, as suggested by niedhui :)
31
+ def radian_multiplier
32
+ {
33
+ :feet => 364491.8,
34
+ :meters => 111170,
35
+ :kms => 111.17,
36
+ :miles => 69.407,
37
+ :radians => 1
38
+ }
39
+ end
40
+
41
+ def meters_multiplier
42
+ {
43
+ :feet => 0.305,
44
+ :meters => 1,
45
+ :kms => 6371,
46
+ :miles => 3959,
47
+ :radians => 111170
48
+ }
49
+ end
50
+
51
+ def meters_map
52
+ {
53
+ :feet => 3.2808399,
54
+ :meters => 1,
55
+ :kms => 0.001,
56
+ :miles => 0.00062137,
57
+ :radians => 0.00000899
58
+ }
59
+ end
60
+
61
+ def precision
62
+ {
63
+ :feet => 0,
64
+ :meters => 2,
65
+ :kms => 4,
66
+ :miles => 4,
67
+ :radians => 4
68
+ }
69
+ end
70
+
71
+ extend self
72
+ end
73
+ end
@@ -0,0 +1,35 @@
1
+ module GeoUnits
2
+ module UnitConversions
3
+ def degrees_to_radians(degrees)
4
+ degrees.to_f * GeoUnits::Constants.radians_per_degree
5
+ end
6
+
7
+ def units_sphere_multiplier(units)
8
+ units = GeoUnits.key units
9
+ GeoUnits::Mapsearth_radius_map[units]
10
+ end
11
+
12
+ def units_per_latitude_degree(units)
13
+ units = GeoUnits.key units
14
+ GeoUnits::Maps.radian_multiplier[units]
15
+ end
16
+
17
+ def units_per_longitude_degree(lat, units)
18
+ miles_per_longitude_degree = (latitude_degrees * Math.cos(lat * pi_div_rad)).abs
19
+ units = GeoUnits.key units
20
+ miles_per_longitude_degree.miles_to(units)
21
+ end
22
+
23
+ def earth_radius units
24
+ units = GeoUnits.key units
25
+ GeoUnits::Maps.earth_radius_map[units]
26
+ end
27
+
28
+ def radians_ratio units
29
+ units = GeoUnits.key units
30
+ radians_per_degree * earth_radius(units)
31
+ end
32
+
33
+ extend self
34
+ end
35
+ end
data/lib/geo_units.rb CHANGED
@@ -16,98 +16,31 @@
16
16
  # @throws ArgumentError
17
17
 
18
18
  require 'sugar-high/numeric'
19
+ require 'sugar-high/class_ext'
19
20
 
20
21
  module GeoUnits
21
- autoload :Converter, 'geo_units/converter'
22
- autoload :DmsConverter, 'geo_units/dms_converter'
23
- autoload :NumericExt, 'geo_units/numeric_ext'
24
-
25
- def earth_radius_map
26
- {
27
- :miles => 3963.1676,
28
- :kilometers => 6378.135,
29
- :meters => 6378135,
30
- :feet => 20925639.8
31
- }
32
- end
33
-
34
- def earth_major_axis_radius_map
35
- {
36
- :miles => 3963.19059,
37
- :kilometers => 6378.137,
38
- :meters => 6378137,
39
- :feet => 20925646.36
40
- }
41
- end
42
-
43
- def earth_minor_axis_radius_map
44
- {
45
- :kilometers => 6356.7523142,
46
- :miles => 3949.90276,
47
- :meters => 6356752.3142,
48
- :feet => 20855486.627
49
- }
50
- end
51
-
52
- def radians_per_degree
53
- Math::PI / 180.0
54
- end
55
-
56
- # Haversine Formula
57
- # Adapted from Geokit Gem
58
- # https://github.com/andre/geokit-gem.git
59
- # By: Andre Lewis
60
- def pi_div_rad
61
- 0.0174
62
- end
63
-
64
- def kms_per_mile
65
- 1.609
66
- end
22
+ autoload_modules :Converter, :DmsConverter, :NumericExt, :Maps, :Constants, :UnitConversions, :from => 'geo_units'
67
23
 
68
- def meters_per_feet
69
- 3.2808399
70
- end
71
-
72
- def miles_per_latitude_degree
73
- 69.1
74
- end
75
-
76
- def kms_per_latitude_degree
77
- miles_per_latitude_degree * kms_per_mile
78
- end
79
-
80
- def latitude_degrees
81
- earth_radius_map[:miles] / miles_per_latitude_degree
82
- end
83
-
84
- def degrees_to_radians(degrees)
85
- degrees.to_f * radians_per_degree
86
- end
87
-
88
- def units_sphere_multiplier(units)
89
- units = GeoUnits.key units
90
- earth_radius_map[units]
24
+ def self.included(base)
25
+ [Maps, Constants, UnitConversions].each do |module_name|
26
+ base.send :include, module_name
27
+ base.extend module_name
28
+ end
91
29
  end
92
30
 
93
- def units_per_latitude_degree(units)
94
- units = GeoUnits.key units
95
- GeoUnits.radian_multiplier[units]
31
+ def self.units
32
+ [:feet, :meters, :kms, :miles, :radians]
96
33
  end
97
34
 
98
- def units_per_longitude_degree(lat, units)
99
- miles_per_longitude_degree = (latitude_degrees * Math.cos(lat * pi_div_rad)).abs
100
- units = GeoUnits.key units
101
- case units
102
- when :feet
103
- miles_per_longitude_degree * kms_per_mile * 1000 * meters_per_feet
104
- when :meters
105
- miles_per_longitude_degree * kms_per_mile * 1000
106
- when :kms
107
- miles_per_longitude_degree * kms_per_mile
108
- when :miles
109
- miles_per_longitude_degree
110
- end
35
+ units.each do |unit|
36
+ class_eval %{
37
+ def self.#{unit}_to unit, number = 0
38
+ return 0 if number <= 0
39
+ unit = key(unit)
40
+ m = number / GeoUnits::Maps.meters_map[:#{unit}]
41
+ m * GeoUnits::Maps.meters_map[unit]
42
+ end
43
+ }
111
44
  end
112
45
 
113
46
  module ClassMethods
@@ -119,80 +52,10 @@ module GeoUnits
119
52
  raise ArgumentError, "Unknown unit key: #{unit}"
120
53
  end
121
54
 
122
- def units
123
- [:feet, :meters, :kms, :miles, :radians]
124
- end
125
-
126
55
  def all_units
127
56
  [:miles, :mile, :kms, :km, :feet, :foot, :meter, :meters, :radians, :rad]
128
57
  end
129
58
 
130
- [:feet, :meters, :kms, :miles, :radians].each do |unit|
131
- class_eval %{
132
- def #{unit}_to unit, number = 0
133
- return 0 if number <= 0
134
- unit = key(unit)
135
- m = number / GeoUnits.meters_map[:#{unit}]
136
- m * meters_map[unit]
137
- end
138
- }
139
- end
140
-
141
- def earth_radius units
142
- units = GeoUnits.key units
143
- GeoUnits.earth_radius_map[units]
144
- end
145
-
146
- def radians_per_degree
147
- 0.017453293 # PI/180
148
- end
149
-
150
- def radians_ratio units
151
- units = GeoUnits.key units
152
- radians_per_degree * earth_radius[units]
153
- end
154
-
155
- def precision
156
- {
157
- :feet => 0,
158
- :meters => 2,
159
- :kms => 4,
160
- :miles => 4,
161
- :radians => 4
162
- }
163
- end
164
-
165
- # from mongoid-geo, as suggested by niedhui :)
166
- def radian_multiplier
167
- {
168
- :feet => 364491.8,
169
- :meters => 111170,
170
- :kms => 111.17,
171
- :miles => 69.407,
172
- :radians => 1
173
- }
174
- end
175
-
176
- def meters_multiplier
177
- {
178
- :feet => 0.305,
179
- :meters => 1,
180
- :kms => 6371,
181
- :miles => 3959,
182
- :radians => 111170
183
- }
184
- end
185
-
186
- def meters_map
187
- {
188
- :feet => 3.2808399,
189
- :meters => 1,
190
- :kms => 0.001,
191
- :miles => 0.00062137,
192
- :radians => 0.00000899
193
- }
194
- end
195
-
196
59
  protected
197
60
 
198
61
  def feet_unit
@@ -1,4 +1,9 @@
1
1
  require 'spec_helper'
2
+ require 'active_support/inflector'
3
+
4
+ class MyGeoThingy
5
+ include GeoUnits
6
+ end
2
7
 
3
8
  describe GeoUnits do
4
9
  describe '#key' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_units
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4.1
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,44 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-22 00:00:00.000000000Z
12
+ date: 2011-06-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sugar-high
16
- requirement: &2158096100 !ruby/object:Gem::Requirement
16
+ requirement: &2158101860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.4.6.2
21
+ version: 0.4.9.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2158096100
24
+ version_requirements: *2158101860
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &2158101160 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2158101160
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &2158100380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2158100380
25
47
  - !ruby/object:Gem::Dependency
26
48
  name: rspec
27
- requirement: &2158095220 !ruby/object:Gem::Requirement
49
+ requirement: &2158099700 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ! '>='
@@ -32,10 +54,10 @@ dependencies:
32
54
  version: 2.5.0
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *2158095220
57
+ version_requirements: *2158099700
36
58
  - !ruby/object:Gem::Dependency
37
59
  name: bundler
38
- requirement: &2158093940 !ruby/object:Gem::Requirement
60
+ requirement: &2158099020 !ruby/object:Gem::Requirement
39
61
  none: false
40
62
  requirements:
41
63
  - - ~>
@@ -43,10 +65,10 @@ dependencies:
43
65
  version: 1.0.6
44
66
  type: :development
45
67
  prerelease: false
46
- version_requirements: *2158093940
68
+ version_requirements: *2158099020
47
69
  - !ruby/object:Gem::Dependency
48
70
  name: jeweler
49
- requirement: &2158093160 !ruby/object:Gem::Requirement
71
+ requirement: &2158098140 !ruby/object:Gem::Requirement
50
72
  none: false
51
73
  requirements:
52
74
  - - ~>
@@ -54,10 +76,10 @@ dependencies:
54
76
  version: 1.6.2
55
77
  type: :development
56
78
  prerelease: false
57
- version_requirements: *2158093160
79
+ version_requirements: *2158098140
58
80
  - !ruby/object:Gem::Dependency
59
81
  name: rcov
60
- requirement: &2158091120 !ruby/object:Gem::Requirement
82
+ requirement: &2158090660 !ruby/object:Gem::Requirement
61
83
  none: false
62
84
  requirements:
63
85
  - - ! '>='
@@ -65,7 +87,7 @@ dependencies:
65
87
  version: '0'
66
88
  type: :development
67
89
  prerelease: false
68
- version_requirements: *2158091120
90
+ version_requirements: *2158090660
69
91
  description: Easily convert between different distance units such as kms, miles etc.
70
92
  email: kmandrup@gmail.com
71
93
  executables: []
@@ -83,10 +105,13 @@ files:
83
105
  - VERSION
84
106
  - geo_units.gemspec
85
107
  - lib/geo_units.rb
108
+ - lib/geo_units/constants.rb
86
109
  - lib/geo_units/converter.rb
87
110
  - lib/geo_units/core_ext.rb
88
111
  - lib/geo_units/dms_converter.rb
112
+ - lib/geo_units/maps.rb
89
113
  - lib/geo_units/numeric_ext.rb
114
+ - lib/geo_units/unit_conversions.rb
90
115
  - spec/geo_units/converter_spec.rb
91
116
  - spec/geo_units/core_ext_spec.rb
92
117
  - spec/geo_units/dms_converter_spec.rb
@@ -108,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
133
  version: '0'
109
134
  segments:
110
135
  - 0
111
- hash: -3577635044061108096
136
+ hash: -1839405070661526357
112
137
  required_rubygems_version: !ruby/object:Gem::Requirement
113
138
  none: false
114
139
  requirements: