geo_units 0.2.6 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class Parser
4
- include GeoUnits::DmsConverter
4
+ include GeoUnits::Converter::Dms
5
5
  end
6
6
 
7
7
  def parser
@@ -9,7 +9,7 @@ def parser
9
9
  end
10
10
 
11
11
  # - www.movable-type.co.uk/scripts/latlong.html
12
- describe GeoUnits::DmsConverter do
12
+ describe GeoUnits::Converter::Dms do
13
13
  # # @param {String|Number} dmsStr: Degrees or deg/min/sec in variety of formats
14
14
  # @returns {Number} Degrees as decimal number
15
15
  describe '#parse_dms' do
@@ -38,21 +38,21 @@ describe GeoUnits::DmsConverter do
38
38
  it 'should convert 58.3 to a String in DMS format' do
39
39
  dms = parser.to_dms(58.3)
40
40
  dms.should be_a(String)
41
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032", "00", "\u2033")
41
+ expr = Regexp.escape "058".concats("\u00B0", "18", "\u2032", "00", "\u2033")
42
42
  dms.should match expr
43
43
  end
44
44
 
45
45
  it 'should convert 58.3 to a String in DM format' do
46
46
  dm = parser.to_dms(58.3, :dm, 2)
47
47
  dm.should be_a(String)
48
- expr = Regexp.escape "058".concat("\u00B0", "18", "\u2032")
48
+ expr = Regexp.escape "058".concats("\u00B0", "18", "\u2032")
49
49
  dm.should match expr
50
50
  end
51
51
 
52
52
  it 'should convert 58.3 to a String in D format' do
53
53
  d = parser.to_dms(58.3, :d, 2)
54
54
  d.should be_a(String)
55
- m = Regexp.escape "058".concat("\u00B0")
55
+ m = Regexp.escape "058".concats("\u00B0")
56
56
  d.should match m
57
57
  end
58
58
  end
File without changes
@@ -10,19 +10,19 @@ end
10
10
 
11
11
  # - www.movable-type.co.uk/scripts/latlong.html
12
12
  describe GeoUnits::Converter do
13
- # deg, format, dp
13
+ # deg, format, dp
14
14
  describe '#to_lat' do
15
15
  it 'should convert 58.3 to a latitude String in North direction' do
16
16
  str_lat = converter.to_lat(58.3)
17
17
  str_lat.should be_a(String)
18
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "N")
18
+ expr = Regexp.escape "58".concats("\u00B0", "18", "\u2032", "00", "\u2033", "N")
19
19
  str_lat.should match expr
20
20
  end
21
21
 
22
22
  it 'should convert -58.3 to a latitude String in South direction' do
23
23
  str_lat = converter.to_lat(-58.3)
24
24
  str_lat.should be_a(String)
25
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "S")
25
+ expr = Regexp.escape "58".concats("\u00B0", "18", "\u2032", "00", "\u2033", "S")
26
26
  str_lat.should match expr
27
27
  end
28
28
  end
@@ -32,14 +32,14 @@ describe GeoUnits::Converter do
32
32
  it 'should convert 58.3 to a longitude String' do
33
33
  str_lat = converter.to_lon(58.3)
34
34
  str_lat.should be_a(String)
35
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "E")
35
+ expr = Regexp.escape "58".concats("\u00B0", "18", "\u2032", "00", "\u2033", "E")
36
36
  str_lat.should match expr
37
37
  end
38
38
 
39
39
  it 'should convert 58.3 to a longitude String' do
40
40
  str_lat = converter.to_lon(-58.3)
41
41
  str_lat.should be_a(String)
42
- expr = Regexp.escape "58".concat("\u00B0", "18", "\u2032", "00", "\u2033", "W")
42
+ expr = Regexp.escape "58".concats("\u00B0", "18", "\u2032", "00", "\u2033", "W")
43
43
  str_lat.should match expr
44
44
  end
45
45
  end
@@ -50,8 +50,8 @@ describe GeoUnits::Converter do
50
50
  it 'should convert 58.3 to a longitude String' do
51
51
  brng = converter.to_brng(-58.3)
52
52
  brng.to_f.should be_between(0, 360)
53
- expr = Regexp.escape "301".concat("\u00B0", "42", "\u2032", "00")
53
+ expr = Regexp.escape "301".concats("\u00B0", "42", "\u2032", "00")
54
54
  brng.should match expr
55
55
  end
56
56
  end
57
- end
57
+ end
@@ -2,16 +2,71 @@ require 'spec_helper'
2
2
 
3
3
  describe GeoUnits do
4
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
5
+
6
+ describe Numeric do
7
+ describe '#miles_to' do
8
+ it 'should convert meters to kms' do
9
+ 100.meters_to(:kms).should == 0.1
10
+ end
11
+ end
12
+
13
+ describe '#miles_to' do
14
+ it 'should convert miles to kms' do
15
+ 2.miles_to(:kms).should be_within(0.2).of 3.21
16
+ end
17
+ end
18
+
19
+ describe '#feet_to' do
20
+ it 'should convert feet to kms' do
21
+ 2.feet_to(:kms).should be_within(0.5).of (2 * 3.28 * 0.001)
22
+ end
23
+ end
24
+
25
+ describe '#to_radians' do
26
+ it 'should convert degrees to radians' do
27
+ 1.to_rad.should be_within(0.002).of 0.017
28
+ 1.to_radians.should be_within(0.002).of 0.017
29
+ 1.deg_to_rad.should be_within(0.002).of 0.017
30
+ 1.degrees_to_rad.should be_within(0.002).of 0.017
31
+ end
32
+ end
33
+ end
34
+
35
+ describe String do
36
+ describe '#parse_dms' do
37
+ it 'should raise error if no valid compass direction' do
38
+ lambda { "58 18 00 X".parse_dms }.should raise_error
39
+ end
40
+
41
+ it 'should raise error if invalid format and set to raise' do
42
+ lambda { "5.8 E".parse_dms }.should raise_error
43
+ end
44
+
45
+ it 'should parse valid format' do
46
+ "58 E".parse_dms.should == 58.0
47
+ end
8
48
  end
9
49
  end
10
50
 
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
51
+ describe Array do
52
+ describe '#to_dms' do
53
+ it 'should convert' do
54
+ end
55
+ end
56
+
57
+ describe '#parse_dms' do
58
+ it 'should raise error if no valid compass direction' do
59
+ lambda { ["15 18 00 X", "53 N"].parse_dms }.should raise_error
60
+ end
61
+
62
+ it 'should parse valid format' do
63
+ ["58 E", "32 N"].parse_dms.should == [58.0, 32.0]
64
+ end
65
+
66
+ it 'should parse valid but reverse format' do
67
+ ["32 N", "58 E"].parse_dms.should == [58.0, 32.0]
68
+ end
14
69
  end
15
70
  end
16
71
  end
17
- end
72
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class Earth
4
+ include GeoUnits::Maps::Earth
5
+ end
6
+
7
+ def earth
8
+ Earth.new
9
+ end
10
+
11
+ # - www.movable-type.co.uk/scripts/latlong.html
12
+ describe GeoUnits::Maps::Earth do
13
+ subject { earth }
14
+
15
+ specify { subject.distance_per_latitude_degree[:miles].should be_between(69, 69.5) }
16
+
17
+ specify { subject.radius[:miles].should be_between(3963, 3964) }
18
+
19
+ specify { subject.major_axis_radius[:miles].should be_between(3963, 3964) }
20
+
21
+ specify { subject.minor_axis_radius[:miles].should be_between(3949, 3950) }
22
+
23
+ specify { subject.latitude_degrees(:miles).should be_between(57, 58) }
24
+ end
25
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ class Meters
4
+ include GeoUnits::Maps::Meters
5
+ end
6
+
7
+ def meters
8
+ Meters.new
9
+ end
10
+
11
+ # - www.movable-type.co.uk/scripts/latlong.html
12
+ describe GeoUnits::Maps::Meters do
13
+ subject { meters }
14
+
15
+ specify { subject.from_unit[:feet].should be_between(0.3045, 0.305) }
16
+
17
+ specify { subject.from_unit[:miles].should be_between(1609, 1610) }
18
+
19
+ specify { subject.to_unit[:feet].should be_between(3.28, 3.29) }
20
+
21
+ specify { subject.to_unit[:miles].should be_between(0.0006, 0.0007) }
22
+ end
23
+
24
+
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ class Map
4
+ include GeoUnits::Maps
5
+ end
6
+
7
+ def map
8
+ Map.new
9
+ end
10
+
11
+ # - www.movable-type.co.uk/scripts/latlong.html
12
+ describe GeoUnits::Maps::Earth do
13
+ subject { map }
14
+
15
+ # earth
16
+ specify { subject.distance_per_latitude_degree[:miles].should be_between(69, 69.5) }
17
+
18
+ # meters
19
+ specify { subject.from_unit[:feet].should be_between(0.3045, 0.305) }
20
+
21
+ # maps
22
+ specify { subject.precision[:miles].should == 4 }
23
+ end
24
+
25
+
File without changes
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ # - www.movable-type.co.uk/scripts/latlong.html
4
+ describe Array do
5
+ # deg, format, dp
6
+ describe '#to_dms' do
7
+ let (:dms_arr) { [58.3, 4].to_dms }
8
+
9
+ it 'should convert [58.3, 4] to string of dms format' do
10
+ dms_arr.should match /58.*18.*, 04.*00/
11
+ end
12
+ end
13
+
14
+ describe '#to_dms :reverse' do
15
+ let (:dms_arr) { [58.3, 4].to_dms :lat_lng}
16
+
17
+ it 'should convert [58.3, 4] to string of dms format' do
18
+ dms_arr.should match /04.*00.*N, 058.*18.*E/
19
+ end
20
+ end
21
+ end
@@ -14,7 +14,7 @@ describe GeoUnits do
14
14
 
15
15
  describe '#radians_to' do
16
16
  it 'should convert radians to kms' do
17
- GeoUnits.radians_to(:kms, 2).should be_within(0.5).of 111.17 * 2
17
+ GeoUnits.radians_to(:kms, 2, 30).should be_within(0.5).of 111.17 * 2
18
18
  end
19
19
  end
20
20
 
@@ -23,4 +23,10 @@ describe GeoUnits do
23
23
  GeoUnits.miles_to(:kms, 2).should be_within(0.2).of 3.21
24
24
  end
25
25
  end
26
+
27
+ describe '#kms_to' do
28
+ it 'should convert miles to kms' do
29
+ GeoUnits.kilometers_to(:meters, 2).should == 2000
30
+ end
31
+ end
26
32
  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.6
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.6.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: sweetloader
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: i18n
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -126,15 +142,28 @@ files:
126
142
  - lib/geo_units.rb
127
143
  - lib/geo_units/constants.rb
128
144
  - lib/geo_units/converter.rb
145
+ - lib/geo_units/converter/dms.rb
146
+ - lib/geo_units/converter/normalizer.rb
147
+ - lib/geo_units/converter/units.rb
129
148
  - lib/geo_units/core_ext.rb
130
- - lib/geo_units/dms_converter.rb
131
149
  - lib/geo_units/maps.rb
132
- - lib/geo_units/numeric_ext.rb
150
+ - lib/geo_units/maps/earth.rb
151
+ - lib/geo_units/maps/meters.rb
152
+ - lib/geo_units/numeric.rb
153
+ - lib/geo_units/numeric/dms.rb
154
+ - lib/geo_units/numeric/normalizer.rb
133
155
  - lib/geo_units/unit_conversions.rb
156
+ - spec/geo_units/converter/dms_spec.rb
157
+ - spec/geo_units/converter/normalizer_spec.rb
158
+ - spec/geo_units/converter/units_spec.rb
134
159
  - spec/geo_units/converter_spec.rb
135
160
  - spec/geo_units/core_ext_spec.rb
136
- - spec/geo_units/dms_converter_spec.rb
137
- - spec/geo_units/numeric_ext_spec.rb
161
+ - spec/geo_units/maps/earth_spec.rb
162
+ - spec/geo_units/maps/meters_spec.rb
163
+ - spec/geo_units/maps_spec.rb
164
+ - spec/geo_units/numeric/dms_spec.rb
165
+ - spec/geo_units/numeric/normalizer_spec.rb
166
+ - spec/geo_units/numeric_spec.rb
138
167
  - spec/geo_units_spec.rb
139
168
  - spec/spec_helper.rb
140
169
  homepage: http://github.com/kristianmandrup/geo_units
@@ -152,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
181
  version: '0'
153
182
  segments:
154
183
  - 0
155
- hash: 2047484457319492713
184
+ hash: 4502680806644763495
156
185
  required_rubygems_version: !ruby/object:Gem::Requirement
157
186
  none: false
158
187
  requirements:
@@ -1,107 +0,0 @@
1
- require 'sugar-high/numeric'
2
- require 'sugar-high/string'
3
-
4
- module GeoUnits
5
- module DmsConverter
6
- include NumericCheckExt
7
-
8
- def parse_dms dms_str
9
- # check for signed decimal degrees without NSEW, if so return it directly
10
- return dms_str if is_numeric?(dms_str)
11
-
12
- # strip off any sign or compass dir'n & split out separate d/m/s
13
- dms = dms_str.strip.gsub(/^-/,'').gsub(/[NSEW]$/i,'').split(/[^0-9.,]+/).map(&:strip).map(&:to_f)
14
- return nil if dms.empty?
15
-
16
- # and convert to decimal degrees...
17
- deg = case dms.length
18
- when 3 # interpret 3-part result as d/m/s
19
- dms[0]/1 + dms[1]/60 + dms[2]/3600
20
- when 2 # interpret 2-part result as d/m
21
- dms[0]/1 + dms[1]/60
22
- when 1 # just d (possibly decimal) or non-separated dddmmss
23
- d = dms[0];
24
- # check for fixed-width unseparated format eg 0033709W
25
- d = "0#{d}" if (/[NS]/i.match(dms_str)) # - normalise N/S to 3-digit degrees
26
- d = "#{d.slice(0,3)/1}#{deg.slice(3,5)/60}#{deg.slice(5)/3600}" if (/[0-9]{7}/.match(deg))
27
- d
28
- else
29
- nil
30
- end
31
- return nil if !deg
32
- deg = (deg * -1) if (/^-|[WS]$/i.match(dms_str.strip)) # take '-', west and south as -ve
33
- deg.to_f
34
- end
35
-
36
- # Convert decimal degrees to deg/min/sec format
37
- # - degree, prime, double-prime symbols are added, but sign is discarded, though no compass
38
- # direction is added
39
- #
40
- #
41
- # @param {Number} deg: Degrees
42
- # @param {String} [format=dms]: Return value as 'd', 'dm', 'dms'
43
- # @param {Number} [dp=0|2|4]: No of decimal places to use - default 0 for dms, 2 for dm, 4 for d
44
- # @returns {String} deg formatted as deg/min/secs according to specified format
45
- # @throws {TypeError} deg is an object, perhaps DOM object without .value?
46
-
47
- def to_dms deg, format = :dms, dp = nil
48
- deg = begin
49
- deg.to_f
50
- rescue
51
- nil
52
- end
53
- return nil if !deg # give up here if we can't make a number from deg
54
-
55
- # default values
56
- format ||= :dms
57
- dp = if dp.nil?
58
- case format.to_sym
59
- when :d
60
- 4
61
- when :dm
62
- 2
63
- else
64
- 0 # default
65
- end
66
- end
67
- dp ||= 0
68
-
69
- deg = deg.abs # (unsigned result ready for appending compass dir'n)
70
-
71
- case format
72
- when :d
73
- d = deg.round(dp) # round degrees
74
- ds = "0#{d}" if (d <100) # pad with leading zeros
75
- ds = "0#{ds}" if (d <10)
76
- dms = ds.to_s.concat("\u00B0") # add º symbol
77
- when :dm
78
- min = (deg*60).round(dp) # convert degrees to minutes & round
79
- d = d.to_i
80
- d = (min / 60).floor # get component deg/min
81
- m = (min % 60).round(dp) # pad with trailing zeros
82
- ds = d
83
- ms = m
84
- ds = "0#{d}" if (d<100) # pad with leading zeros
85
- ds = "0#{d}" if (d<10)
86
- ms = "0#{m}" if (m<10)
87
- dms = ds.to_s.concat("\u00B0", ms, "\u2032") # add º, ' symbols
88
- when :dms
89
- sec = (deg * 3600).round # convert degrees to seconds & round
90
- d = (sec / 3600).floor # get component deg/min/sec
91
- m = ((sec / 60) % 60).floor
92
- s = (sec % 60).round(dp) # pad with trailing zeros
93
- ds = d
94
- ms = m
95
- ss = s
96
- ds = "0#{d}" if (d < 100) # pad with leading zeros
97
- ds = "0#{ds}" if (d < 10)
98
- ms = "0#{m}" if (m < 10)
99
- ss = "0#{s}" if (s < 10)
100
- dms = ds.to_s.concat("\u00B0", ms, "\u2032", ss, "\u2033") # add º, ', " symbols
101
- end
102
- return dms
103
- end
104
-
105
- extend self
106
- end
107
- end