Module: GeoHex

Defined in:
lib/geo_hex.rb,
lib/geo_hex/ll.rb,
lib/geo_hex/pp.rb,
lib/geo_hex/unit.rb,
lib/geo_hex/zone.rb,
lib/geo_hex/polygon.rb,
lib/geo_hex/version.rb

Defined Under Namespace

Classes: LL, PP, Polygon, Unit, Zone

Constant Summary collapse

H_KEY =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".freeze
H_BASE =
20037508.34
H_D2R =
Math::PI / 180.0
H_K =
Math.tan(H_D2R * 30)
H_ER =
6_371_007.2
VERSION =
"3.1.4".freeze

Class Method Summary collapse

Class Method Details

.decode(code) ⇒ GeoHex::Zone

Returns the decoded zone.

Parameters:

  • code (String)

    the GeoHex code

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/geo_hex.rb', line 26

def self.decode(code)
  x, y   = 0, 0
  chars  = code.size
  string = "#{H_KEY.index(code[0]) * 30 + H_KEY.index(code[1])}#{code[2..-1]}"
  string = string.rjust(chars+1, "0")
  nums   = string.chars.map {|c| c.to_i }
  nums.each_with_index do |num, i|
    pow = 3**(chars-i)
    num = num.to_s(3).to_i

    case (num / 10) when 0 then x -= pow when 2 then x += pow end
    case (num % 10) when 0 then y -= pow when 2 then y += pow end
  end

  Zone.new(x, y, chars-2).send(:with_code, code)
end

.encode(lat, lon, level = 7) ⇒ GeoHex::Zone

Returns the encoded zone.

Parameters:

  • lat (Float)

    the latitude

  • lon (Float)

    the longitude

  • level (Integer) (defaults to: 7)

    the level

Returns:



20
21
22
# File 'lib/geo_hex.rb', line 20

def self.encode(lat, lon, level = 7)
  LL.new(lat, lon).to_zone(level)
end