Module: Locapoint

Defined in:
lib/locapoint/locapoint.rb,
lib/locapoint/version.rb

Overview

LocaPoint module.

Defined Under Namespace

Modules: VERSION Classes: FormatError

Constant Summary collapse

LOCAPOINT_REGEXP =

Regular expression that matches LocaPoint strings.

/[A-Z][A-Z][0-9]\.[A-Z][A-Z][0-9]\.[A-Z][A-Z][0-9]\.[A-Z][A-Z][0-9]/

Class Method Summary collapse

Class Method Details

.decode(locapoint) ⇒ Object

Will convert a LocaPoint string into a pair of latitude and longitude.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/locapoint/locapoint.rb', line 15

def self.decode(locapoint)
  unless locapoint =~ LOCAPOINT_REGEXP && locapoint.size == 15
    raise FormatError, "Invalid format"
  end
  
  s = locapoint.to_s
  latitude = ((s[0] - ?A) * 1757600 \
              +(s[1] - ?A) * 67600 \
              +(s[2] - ?0) * 6760 \
              +(s[8] - ?A) * 260 \
              +(s[9] - ?A) * 10 \
              +(s[10] - ?0) ) * 180 / 45697600.0 -90;
  longitude = ((s[4] - ?A) * 1757600 \
               +(s[5] - ?A) * 67600 \
               +(s[6] - ?0) * 6760 \
               +(s[12] - ?A) * 260 \
               +(s[13] - ?A) * 10 \
               +(s[14] - ?0) ) * 360 / 45697600.0 -180;
  return latitude, longitude
end

.encode(latitude, longitude) ⇒ Object

Will convert latitude and longitude into LocaPoint string.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/locapoint/locapoint.rb', line 37

def self.encode(latitude, longitude)
  latitude_step = (latitude.to_f + 90) / 180 * 45697600;
  longitude_step = (longitude.to_f + 180) / 360 * 45697600;
  
  s = "AA0.AA0.AA0.AA0"
  s[0] += (latitude_step / 1757600 % 26).to_i
  s[1] += (latitude_step / 67600 % 26).to_i
  s[2] += (latitude_step / 6760 % 10).to_i
  
  s[4] += (longitude_step / 1757600 % 26).to_i
  s[5] += (longitude_step / 67600 % 26).to_i
  s[6] += (longitude_step / 6760 % 10).to_i

  s[8] += (latitude_step / 260 % 26).to_i
  s[9] += (latitude_step / 10 % 26).to_i
  s[10] += (latitude_step / 1 % 10).to_i
  
  s[12] += (longitude_step / 260 % 26).to_i
  s[13] += (longitude_step / 10 % 26).to_i
  s[14] += (longitude_step / 1 % 10).to_i

  return s
end