geo_units 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -5,6 +5,61 @@ Distance unit functionality (conversions etc.) for Geo libraries. See specs for
5
5
  This gem is used by the _geo_point_ and _geo_calc_ gems and perhaps by others. The aim is that it will contain most of the basic
6
6
  distance unit functionality required by typical Geo projects.
7
7
 
8
+ h2. Usage
9
+
10
+ in Gemfile:
11
+
12
+ @gem 'geo_units'@
13
+
14
+ Then run the bundler!
15
+
16
+ @$ bundle@
17
+
18
+ h2. GeoUnits API
19
+
20
+ <pre>GeoUnits.key(:foot) # will convert any kind of unit into one of (:feet, :meters, :kms, :miles, :radians)
21
+ => :feet
22
+ </pre>
23
+
24
+ <pre>GeoUnits.kms_to :meters, 5 # convert number of one distance unit to another unit
25
+ => 5000
26
+ </pre>
27
+
28
+ h2. Core extensions API
29
+
30
+ <pre>2.radians_to(:kms)
31
+ => 222.34
32
+ </pre>
33
+
34
+ <pre>@2.miles_to(:kms)
35
+ => 3.21
36
+ </pre>
37
+
38
+ h2. Dms Converter
39
+
40
+ module _GeoUnits::DmsConverter_
41
+
42
+ * parse_dms(string)
43
+ * to_dms(deg, format = :dms, dp = nil)
44
+
45
+ h2. Numeric conversions
46
+
47
+ module _GeoUnits::NumericExt_
48
+
49
+ This module is included on the _Fixnum_ and _Float_ classes
50
+
51
+ * to_lat
52
+ * to_lng
53
+ * to_dms
54
+ * to_lat_dms
55
+ * to_lon_dms
56
+ * to_rad
57
+ * to_deg
58
+ * normalize_lng
59
+ * normalize_lat
60
+ * normalize_deg(shift)
61
+
62
+
8
63
  h2. Contributing to geo_units
9
64
 
10
65
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.3
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.1"
8
+ s.version = "0.2.3"
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-22}
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 = [
@@ -31,8 +31,10 @@ Gem::Specification.new do |s|
31
31
  "lib/geo_units/dms_converter.rb",
32
32
  "lib/geo_units/numeric_ext.rb",
33
33
  "spec/geo_units/converter_spec.rb",
34
+ "spec/geo_units/core_ext_spec.rb",
34
35
  "spec/geo_units/dms_converter_spec.rb",
35
36
  "spec/geo_units/numeric_ext_spec.rb",
37
+ "spec/geo_units_spec.rb",
36
38
  "spec/spec_helper.rb"
37
39
  ]
38
40
  s.homepage = %q{http://github.com/kristianmandrup/geo_units}
@@ -1,10 +1,32 @@
1
+ require 'sugar-high/numeric'
2
+
3
+ module GeoUnitExt
4
+ ::GeoUnits.units.each do |unit|
5
+ class_eval %{
6
+ def #{unit}_to unit
7
+ unit = GeoUnits.key(unit)
8
+ (self.to_f / GeoUnits.meters_map[:#{unit}]) * GeoUnits.meters_map[unit]
9
+ end
10
+ }
11
+ end
12
+
13
+ include NumberDslExt # from sugar-high
14
+
15
+ def rpd
16
+ self * GeoUnits.radians_per_degree
17
+ end
18
+ alias_method :to_radians, :rpd
19
+ end
20
+
1
21
  class Fixnum
2
- include ::GeoUnits::NumericExt
22
+ include GeoUnitExt
23
+ include ::GeoUnits::NumericExt
3
24
  end
4
25
 
5
26
  class Float
6
- include ::GeoUnits::NumericExt
7
- end
27
+ include GeoUnitExt
28
+ include ::GeoUnits::NumericExt
29
+ end
8
30
 
9
31
  class String
10
32
  def parse_dms
@@ -11,7 +11,6 @@ module GeoUnits
11
11
  def is_between? lower, upper
12
12
  (lower..upper).cover? self
13
13
  end
14
-
15
14
 
16
15
  def to_dms format = :dms, dp = nil
17
16
  GeoUnits::DmsConverter.to_dms self, format, dp
data/lib/geo_units.rb CHANGED
@@ -11,8 +11,8 @@
11
11
  # or fixed-width format without separators (eg 0033709W). Seconds and minutes may be omitted.
12
12
  # (Note minimal validation is done).
13
13
  #
14
- # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
15
- # @returns {Number} Degrees as decimal number
14
+ # @param [String|Number] Degrees or deg/min/sec in variety of formats
15
+ # @returns [Number] Degrees as decimal number
16
16
  # @throws ArgumentError
17
17
 
18
18
  require 'sugar-high/numeric'
@@ -22,15 +22,82 @@ module GeoUnits
22
22
  autoload :DmsConverter, 'geo_units/dms_converter'
23
23
  autoload :NumericExt, 'geo_units/numeric_ext'
24
24
 
25
+ EARTH_RADIUS = {
26
+ :miles => 3963.1676,
27
+ :kilometers => 6378.135,
28
+ :meters => 6378135,
29
+ :feet => 20925639.8
30
+ }
31
+
32
+ EARTH_MAJOR_AXIS_RADIUS = {
33
+ :miles => 3963.19059,
34
+ :kilometers => 6378.137,
35
+ :meters => 6378137,
36
+ :feet => 20925646.36
37
+ }
38
+
39
+ EARTH_MINOR_AXIS_RADIUS = {
40
+ :kilometers => 6356.7523142,
41
+ :miles => 3949.90276,
42
+ :meters => 6356752.3142,
43
+ :feet => 20855486.627
44
+ }
45
+
46
+ RADIAN_PER_DEGREE = Math::PI / 180.0
47
+
48
+ # Haversine Formula
49
+ # Adapted from Geokit Gem
50
+ # https://github.com/andre/geokit-gem.git
51
+ # By: Andre Lewis
52
+ PI_DIV_RAD = 0.0174
53
+
54
+ KMS_PER_MILE = 1.609
55
+ METERS_PER_FEET = 3.2808399
56
+
57
+ MILES_PER_LATITUDE_DEGREE = 69.1
58
+ KMS_PER_LATITUDE_DEGREE = MILES_PER_LATITUDE_DEGREE * KMS_PER_MILE
59
+ LATITUDE_DEGREES = EARTH_RADIUS[:miles] / MILES_PER_LATITUDE_DEGREE
60
+
25
61
  module ClassMethods
26
62
  def key unit = :km
27
63
  unit = unit.to_sym
28
- methods.grep(/_unit/).each do |meth|
64
+ methods.grep(/_unit$/).each do |meth|
29
65
  return meth.to_s.chomp('_unit').to_sym if send(meth).include? unit
30
66
  end
31
67
  raise ArgumentError, "Unknown unit key: #{unit}"
32
68
  end
33
69
 
70
+ def units
71
+ [:feet, :meters, :kms, :miles, :radians]
72
+ end
73
+
74
+ def all_units
75
+ [:miles, :mile, :kms, :km, :feet, :foot, :meter, :meters, :radians, :rad]
76
+ end
77
+
78
+ [:feet, :meters, :kms, :miles, :radians].each do |unit|
79
+ class_eval %{
80
+ def #{unit}_to unit, number = 0
81
+ return 0 if number <= 0
82
+ unit = key(unit)
83
+ m = number / GeoUnits.meters_map[:#{unit}]
84
+ m * meters_map[unit]
85
+ end
86
+ }
87
+ end
88
+
89
+ def earth_radius units
90
+ GeoUnits.EARTH_RADIUS[units.to_sym]
91
+ end
92
+
93
+ def radians_per_degree
94
+ 0.017453293 # PI/180
95
+ end
96
+
97
+ def radians_ratio unit
98
+ radians_per_degree * earth_radius[unit]
99
+ end
100
+
34
101
  def precision
35
102
  {
36
103
  :feet => 0,
@@ -44,30 +111,31 @@ module GeoUnits
44
111
  # from mongoid-geo, as suggested by niedhui :)
45
112
  def radian_multiplier
46
113
  {
47
- :feet => 364491.8,
48
- :meters => 111170,
49
- :kms => 111.17,
50
- :miles => 69.407,
51
- :radians => 1
114
+ :feet => 364491.8,
115
+ :meters => 111170,
116
+ :kms => 111.17,
117
+ :miles => 69.407,
118
+ :radians => 1
52
119
  }
53
120
  end
54
121
 
55
122
  def meters_multiplier
56
123
  {
57
- :feet => 0.305,
58
- :meters => 1,
59
- :kms => 6371,
60
- :miles => 3959
124
+ :feet => 0.305,
125
+ :meters => 1,
126
+ :kms => 6371,
127
+ :miles => 3959,
128
+ :radians => 111170
61
129
  }
62
130
  end
63
131
 
64
132
  def meters_map
65
133
  {
66
- :feet => 3.2808,
67
- :meters => 1,
68
- :kms => 0.001,
69
- :miles => 0.00062137,
70
- :radians => 111170
134
+ :feet => 3.2808399,
135
+ :meters => 1,
136
+ :kms => 0.001,
137
+ :miles => 0.00062137,
138
+ :radians => 0.00000899
71
139
  }
72
140
  end
73
141
 
@@ -91,7 +159,7 @@ module GeoUnits
91
159
 
92
160
  def radians_unit
93
161
  [:rad, :radians]
94
- end
162
+ end
95
163
  end
96
164
 
97
165
  extend ClassMethods
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe GeoUnits do
4
+ describe 'Core extensions' do
5
+ describe '#radians_to' do
6
+ it 'should convert radians to kms' do
7
+ 2.radians_to(:kms).should be_within(0.5).of 111.17 * 2
8
+ end
9
+ end
10
+
11
+ describe '#miles_to' do
12
+ it 'should convert miles to kms' do
13
+ 2.miles_to(:kms).should be_within(0.2).of 3.21
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe GeoUnits do
4
+ describe '#key' do
5
+ it 'should return the unit key' do
6
+ GeoUnits.key(:foot).should == :feet
7
+ end
8
+ end
9
+
10
+ describe '#radians_to' do
11
+ it 'should convert radians to kms' do
12
+ GeoUnits.radians_to(:kms, 2).should be_within(0.5).of 111.17 * 2
13
+ end
14
+ end
15
+
16
+ describe '#miles_to' do
17
+ it 'should convert miles to kms' do
18
+ GeoUnits.miles_to(:kms, 2).should be_within(0.2).of 3.21
19
+ end
20
+ end
21
+ end
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.1
4
+ version: 0.2.3
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-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sugar-high
16
- requirement: &2157728000 !ruby/object:Gem::Requirement
16
+ requirement: &2153948000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.6.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2157728000
24
+ version_requirements: *2153948000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2157727160 !ruby/object:Gem::Requirement
27
+ requirement: &2153947180 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.5.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2157727160
35
+ version_requirements: *2153947180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2157726400 !ruby/object:Gem::Requirement
38
+ requirement: &2153940460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.6
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2157726400
46
+ version_requirements: *2153940460
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2157725640 !ruby/object:Gem::Requirement
49
+ requirement: &2153939720 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.2
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2157725640
57
+ version_requirements: *2153939720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &2157724940 !ruby/object:Gem::Requirement
60
+ requirement: &2153938740 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2157724940
68
+ version_requirements: *2153938740
69
69
  description: Easily convert between different distance units such as kms, miles etc.
70
70
  email: kmandrup@gmail.com
71
71
  executables: []
@@ -88,8 +88,10 @@ files:
88
88
  - lib/geo_units/dms_converter.rb
89
89
  - lib/geo_units/numeric_ext.rb
90
90
  - spec/geo_units/converter_spec.rb
91
+ - spec/geo_units/core_ext_spec.rb
91
92
  - spec/geo_units/dms_converter_spec.rb
92
93
  - spec/geo_units/numeric_ext_spec.rb
94
+ - spec/geo_units_spec.rb
93
95
  - spec/spec_helper.rb
94
96
  homepage: http://github.com/kristianmandrup/geo_units
95
97
  licenses:
@@ -106,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
108
  version: '0'
107
109
  segments:
108
110
  - 0
109
- hash: 1770670745232636098
111
+ hash: -2024456778129613620
110
112
  required_rubygems_version: !ruby/object:Gem::Requirement
111
113
  none: false
112
114
  requirements: