Class: Maidenhead

Inherits:
Object
  • Object
show all
Defined in:
lib/maidenhead.rb

Overview

Easily convert between latitude and longitude coordinates the the Maidenhead Locator System coordinates.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_latlon(location) ⇒ Object

Convert from a Maidenhead locator string to latitude and longitude. Location may be between 1 and 5 grids in size (2 to 10 characters). Longer values may work, but accuracy is not guaranteed as latitude and longitude values returned are rounded ot 6 decimal places.

For each grid, an arbitrary but repeatable latitude and longitude is returned.



37
38
39
40
41
# File 'lib/maidenhead.rb', line 37

def self.to_latlon(location)
  maidenhead = Maidenhead.new
  maidenhead.locator = location
  [ maidenhead.lat, maidenhead.lon ]
end

.to_maidenhead(lat, lon, precision = 5) ⇒ Object

Convert from latitude and longitude to a Maidenhead locator string. Latitude should be between -90 and 90, and longitude should be between -180 and 180. Precision defaults to 5 blocks, which returns 10 characters. More precise coordinates may work, but accuracy is not guaranteed.



72
73
74
75
76
77
78
# File 'lib/maidenhead.rb', line 72

def self.to_maidenhead(lat, lon, precision = 5)
  maidenhead = Maidenhead.new
  maidenhead.lat = lat
  maidenhead.lon = lon
  maidenhead.precision = precision
  maidenhead.locator
end

.valid_maidenhead?(location) ⇒ Boolean

Verify that the provided Maidenhead locator string is valid.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/maidenhead.rb', line 8

def self.valid_maidenhead?(location)
  return false unless location.is_a?String
  return false unless location.length >= 2
  return false unless (location.length % 2) == 0

  length = location.length / 2
  length.times do |counter|
    grid = location[counter * 2, 2]
    if (counter == 0)
      return false unless grid =~ /[a-rA-R]{2}/
    elsif (counter % 2) == 0
      return false unless grid =~ /[a-xA-X]{2}/
    else
      return false unless grid =~ /[0-9]{2}/
    end
  end

  true
end

Instance Method Details

#latObject

Retrieve the latitude, usually post-conversion from a locator string. The result is rounded to 6 decimal places.



92
93
94
# File 'lib/maidenhead.rb', line 92

def lat
  @lat.round(6)
end

#lat=(pos) ⇒ Object

Set the latitude. Values must be between -90.0 and +90.0 or an ArgumentError will be raised.



84
85
86
# File 'lib/maidenhead.rb', line 84

def lat=(pos)
  @lat = range_check("lat", 90.0, pos)
end

#locatorObject

Convert from a latitude / longitude position, which must have been set via #lat= and #lon=, to a locator.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/maidenhead.rb', line 129

def locator
  @locator = ''

  @lat_tmp = @lat + 90.0
  @lon_tmp = @lon + 180.0
  @precision_tmp = @precision

  calculate_field
  calculate_values

  @locator
end

#locator=(location) ⇒ Object

Set the locator string. It must be a valid string, or an ArgumentError will be raised. This also directly computes the latitude and longitude values for this locator, so they will be valid after caling this method.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/maidenhead.rb', line 48

def locator=(location)
  unless Maidenhead.valid_maidenhead?(location)
    raise ArgumentError.new("Location is not a valid Maidenhead Locator System string")
  end

  @locator = location
  @lat = -90.0
  @lon = -180.0

  pad_locator

  convert_part_to_latlon(0, 1)
  convert_part_to_latlon(1, 10)
  convert_part_to_latlon(2, 10 * 24)
  convert_part_to_latlon(3, 10 * 24 * 10)
  convert_part_to_latlon(4, 10 * 24 * 10 * 24)
end

#lonObject

Retrieve the longitude, usually post-conversion from a locator string. The result is rounded to 6 decimal places.



108
109
110
# File 'lib/maidenhead.rb', line 108

def lon
  @lon.round(6)
end

#lon=(pos) ⇒ Object

Set the longitude. Values must be between -180.0 and +180.0 or an ArgumentError will be raised.



100
101
102
# File 'lib/maidenhead.rb', line 100

def lon=(pos)
  @lon = range_check("lon", 180.0, pos)
end

#precisionObject



121
122
123
# File 'lib/maidenhead.rb', line 121

def precision
  @precision
end

#precision=(value) ⇒ Object

Set the desired precision when converting from a latitude / longitude to a maidenhead locator. This specifies the number of groups to use, usually 2 through 5, which results in 4 through 10 characters.



117
118
119
# File 'lib/maidenhead.rb', line 117

def precision=(value)
  @precision = value
end