maiden 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/maiden/version.rb +1 -1
  3. data/lib/maiden.rb +130 -112
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9acfaf45e5e82aed00079683f07d19a5b43864710cca57e15ef00e19ba9c0ac1
4
- data.tar.gz: f479c46b68cabb91817b4e2572c5893f75de93a6d708de80c280e29a09f2f8b4
3
+ metadata.gz: b8a9f7f8685733fd601bc8fccf54eeb7cec409e60a3465177ea6fd93f9723154
4
+ data.tar.gz: 6c6e93b90ed1f03d642a125b450ad7c745c3baa65b1d6ef46116c880e4676b61
5
5
  SHA512:
6
- metadata.gz: aa476a47ba90f5ef8d3588e01b31962fc070bb76289152b499a4920ae885b70fd3ec5637bb4773cb3b4fa07fbe0a5cd1a856b443ec7f55ae0ef3fbc352876503
7
- data.tar.gz: eb88ed4d15490fe00efe5ebac97fad0134e9d9db7ade58d66e95b15b7d6f1ba11743d7c553479d227a25be70f80baaed5e0c6f9980ddc63c17485ff35901cb20
6
+ metadata.gz: 9ce16ea64db3272d184ef49a9eb3b425e9882cb947391445fed5e0f2d90c62ab3c74c7c25dfe8ea2323c67abd17fe33248804ea3301d5cbabb9011f95fee40ab
7
+ data.tar.gz: 0aafb774efe434774c5dbf42911c8be8b8814c0f8cd59c9962c5188b6213b8a3328291390bf3f31e0029e409e7d484f3eeaf61c9a2526e9aef997680a33b8e25
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Maiden
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/maiden.rb CHANGED
@@ -2,136 +2,153 @@
2
2
 
3
3
  require_relative "maiden/version"
4
4
 
5
- module Maidenhead
5
+ module Maiden
6
6
  class Error < StandardError; end
7
+
8
+ @@GRID_SIZE = 4
7
9
 
8
- def self.valid_maidenhead?(location)
9
- return false unless location.is_a?String
10
- return false unless location.length >= 2
11
- return false unless (location.length % 2) == 0
12
-
13
- length = location.length / 2
14
- length.times do |counter|
15
- grid = location[counter * 2, 2]
16
- if (counter == 0)
17
- return false unless grid =~ /[a-rA-R]{2}/
18
- elsif (counter % 2) == 0
19
- return false unless grid =~ /[a-xA-X]{2}/
20
- else
21
- return false unless grid =~ /[0-9]{2}/
10
+ ##
11
+ # gridsquare size
12
+ def self.precision= p
13
+ @@GRID_SIZE = p.to_i
14
+ end
15
+
16
+ class Maidenhead
17
+ def self.valid_maidenhead?(location)
18
+ return false unless location.is_a?String
19
+ return false unless location.length >= 2
20
+ return false unless (location.length % 2) == 0
21
+
22
+ length = location.length / 2
23
+ length.times do |counter|
24
+ grid = location[counter * 2, 2]
25
+ if (counter == 0)
26
+ return false unless grid =~ /[a-rA-R]{2}/
27
+ elsif (counter % 2) == 0
28
+ return false unless grid =~ /[a-xA-X]{2}/
29
+ else
30
+ return false unless grid =~ /[0-9]{2}/
31
+ end
22
32
  end
33
+ true
23
34
  end
24
- true
25
- end
26
- def self.to_latlon(location);
27
- maidenhead = Maidenhead.new; maidenhead.locator = location; return [ maidenhead.lat, maidenhead.lon ]
28
- end
29
- def locator=(location)
30
- unless Maidenhead.valid_maidenhead?(location)
31
- raise ArgumentError.new("Location is not a valid Maidenhead Locator System string")
35
+ def self.to_latlon(location);
36
+ maidenhead = Maidenhead.new; maidenhead.locator = location; return [ maidenhead.lat, maidenhead.lon ]
32
37
  end
33
- @locator = location
34
- @lat = -90.0
35
- @lon = -180.0
36
- pad_locator
37
- convert_part_to_latlon(0, 1)
38
- convert_part_to_latlon(1, 10)
39
- convert_part_to_latlon(2, 10 * 24)
40
- convert_part_to_latlon(3, 10 * 24 * 10)
41
- convert_part_to_latlon(4, 10 * 24 * 10 * 24)
42
- end
43
- def self.to_maidenhead(lat, lon, precision = 5)
44
- maidenhead = Maidenhead.new
45
- maidenhead.lat = lat
46
- maidenhead.lon = lon
47
- maidenhead.precision = precision
48
- maidenhead.locator
49
- end
50
- def lat=(pos); @lat = range_check("lat", 90.0, pos); end
51
- def lat; @lat.round(6); end
52
- def lon=(pos); @lon = range_check("lon", 180.0, pos); end
53
- def lon; @lon.round(6); end
54
- def precision=(value); @precision = value; end
55
- def precision; @precision; end
56
- def locator
57
- @locator = ''
58
- @lat_tmp = @lat + 90.0
59
- @lon_tmp = @lon + 180.0
60
- @precision_tmp = @precision
61
- calculate_field
62
- calculate_values
63
- @locator
64
- end
65
-
66
- private
67
-
68
- def pad_locator
69
- length = @locator.length / 2
70
- while (length < 5)
71
- if (length % 2) == 1
72
- @locator += '55'
73
- else
74
- @locator += 'LL'
38
+ def locator=(location)
39
+ unless Maidenhead.valid_maidenhead?(location)
40
+ raise ArgumentError.new("Location is not a valid Maidenhead Locator System string")
75
41
  end
42
+ @locator = location
43
+ @lat = -90.0
44
+ @lon = -180.0
45
+ pad_locator
46
+ convert_part_to_latlon(0, 1)
47
+ convert_part_to_latlon(1, 10)
48
+ convert_part_to_latlon(2, 10 * 24)
49
+ convert_part_to_latlon(3, 10 * 24 * 10)
50
+ convert_part_to_latlon(4, 10 * 24 * 10 * 24)
51
+ end
52
+ def self.to_maidenhead(lat, lon, precision = 5)
53
+ maidenhead = Maidenhead.new
54
+ maidenhead.lat = lat
55
+ maidenhead.lon = lon
56
+ maidenhead.precision = precision
57
+ maidenhead.locator
58
+ end
59
+ def lat=(pos); @lat = range_check("lat", 90.0, pos); end
60
+ def lat; @lat.round(6); end
61
+ def lon=(pos); @lon = range_check("lon", 180.0, pos); end
62
+ def lon; @lon.round(6); end
63
+ def precision=(value); @precision = value; end
64
+ def precision; @precision; end
65
+ def locator
66
+ @locator = ''
67
+ @lat_tmp = @lat + 90.0
68
+ @lon_tmp = @lon + 180.0
69
+ @precision_tmp = @precision
70
+ calculate_field
71
+ calculate_values
72
+ @locator
73
+ end
74
+
75
+ private
76
+
77
+ def pad_locator
76
78
  length = @locator.length / 2
79
+ while (length < 5)
80
+ if (length % 2) == 1
81
+ @locator += '55'
82
+ else
83
+ @locator += 'LL'
84
+ end
85
+ length = @locator.length / 2
86
+ end
77
87
  end
78
- end
79
- def convert_part_to_latlon(counter, divisor)
80
- grid_lon = @locator[counter * 2, 1]
81
- grid_lat = @locator[counter * 2 + 1, 1]
82
- @lat += l2n(grid_lat) * 10.0 / divisor
83
- @lon += l2n(grid_lon) * 20.0 / divisor
84
- end
85
- def calculate_field
86
- @lat_tmp = (@lat_tmp / 10) + 0.0000001
87
- @lon_tmp = (@lon_tmp / 20) + 0.0000001
88
- @locator += n2l(@lon_tmp.floor).upcase + n2l(@lat_tmp.floor).upcase
89
- @precision_tmp -= 1
90
- end
91
- def compute_locator(counter, divisor)
92
- @lat_tmp = (@lat_tmp - @lat_tmp.floor) * divisor
93
- @lon_tmp = (@lon_tmp - @lon_tmp.floor) * divisor
94
- if (counter % 2) == 0
95
- @locator += "#{@lon_tmp.floor}#{@lat_tmp.floor}"
96
- else
97
- @locator += n2l(@lon_tmp.floor) + n2l(@lat_tmp.floor)
88
+ def convert_part_to_latlon(counter, divisor)
89
+ grid_lon = @locator[counter * 2, 1]
90
+ grid_lat = @locator[counter * 2 + 1, 1]
91
+ @lat += l2n(grid_lat) * 10.0 / divisor
92
+ @lon += l2n(grid_lon) * 20.0 / divisor
98
93
  end
99
- end
100
- def calculate_values
101
- @precision_tmp.times do |counter|
94
+ def calculate_field
95
+ @lat_tmp = (@lat_tmp / 10) + 0.0000001
96
+ @lon_tmp = (@lon_tmp / 20) + 0.0000001
97
+ @locator += n2l(@lon_tmp.floor).upcase + n2l(@lat_tmp.floor).upcase
98
+ @precision_tmp -= 1
99
+ end
100
+ def compute_locator(counter, divisor)
101
+ @lat_tmp = (@lat_tmp - @lat_tmp.floor) * divisor
102
+ @lon_tmp = (@lon_tmp - @lon_tmp.floor) * divisor
102
103
  if (counter % 2) == 0
103
- compute_locator(counter, 10)
104
+ @locator += "#{@lon_tmp.floor}#{@lat_tmp.floor}"
104
105
  else
105
- compute_locator(counter, 24)
106
+ @locator += n2l(@lon_tmp.floor) + n2l(@lat_tmp.floor)
106
107
  end
107
108
  end
108
- end
109
- def l2n(letter)
110
- if letter =~ /[0-9]+/
111
- letter.to_i
112
- else
113
- letter.downcase.ord - 97
109
+ def calculate_values
110
+ @precision_tmp.times do |counter|
111
+ if (counter % 2) == 0
112
+ compute_locator(counter, 10)
113
+ else
114
+ compute_locator(counter, 24)
115
+ end
116
+ end
117
+ end
118
+ def l2n(letter)
119
+ if letter =~ /[0-9]+/
120
+ letter.to_i
121
+ else
122
+ letter.downcase.ord - 97
123
+ end
124
+ end
125
+ def n2l(number)
126
+ (number + 97).chr
127
+ end
128
+ def range_check(target, range, pos)
129
+ pos = pos.to_f
130
+ if pos < -range or pos > range
131
+ raise ArgumentError.new("#{target} must be between -#{range} and +#{range}")
132
+ end
133
+ pos
114
134
  end
115
135
  end
116
- def n2l(number)
117
- (number + 97).chr
136
+ def self.to_grid lat,lon
137
+ Maidenhead.to_maidenhead(lat,lon,@@GRID_SIZE)
118
138
  end
119
- def range_check(target, range, pos)
120
- pos = pos.to_f
121
- if pos < -range or pos > range
122
- raise ArgumentError.new("#{target} must be between -#{range} and +#{range}")
123
- end
124
- pos
139
+ def self.to_gps g
140
+ Maidenhead.to_latlon(g)
125
141
  end
126
142
  end
127
143
 
144
+
128
145
  module GRID
129
- @@GRID_SIZE = 4
130
146
 
131
- ##
132
- # gridsquare size
147
+ @@GRID_SIZE = 4
148
+ ##
149
+ # gridsquare size
133
150
  def self.precision= p
134
- @@GRID_SIZE = p.to_i
151
+ Maiden.precision = p.to_i
135
152
  end
136
153
 
137
154
  @@G = Hash.new { |h,k| h[k] = [] }
@@ -139,7 +156,9 @@ module GRID
139
156
  ##
140
157
  # known place stack
141
158
  def self.[] k
142
- @@G[k]
159
+ if k.class == String && "#{k}".length > 0
160
+ return @@G[k]
161
+ end
143
162
  end
144
163
 
145
164
  ##
@@ -151,7 +170,7 @@ module GRID
151
170
  ##
152
171
  # convert latitude / longitude to gridsquare
153
172
  def self.to_grid lat,lon
154
- g = Maidenhead.to_maidenhead(lat,lon,@@GRID_SIZE)
173
+ g = Maiden.to_grid(lat,lon)
155
174
  @@G[g]
156
175
  return g
157
176
  end
@@ -160,7 +179,6 @@ module GRID
160
179
  # convert gridsquare to latitude / longitude
161
180
  def self.to_gps g
162
181
  @@G[g]
163
- return Maidenhead.to_latlon(g)
182
+ return Maiden.to_gps(g)
164
183
  end
165
184
  end
166
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maiden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Olson