Class: GeoHex::LL

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

Overview

Lat/Lon coordinates

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lon) ⇒ LL

Returns a new instance of LL.

Parameters:

  • lat (Float)

    the latitude

  • lon (Float)

    the longitude



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

def initialize(lat, lon)
  @lat, @lon = lat, self.class.normalize(lon)
end

Instance Attribute Details

#latObject (readonly)

Returns the value of attribute lat.



17
18
19
# File 'lib/geo_hex/ll.rb', line 17

def lat
  @lat
end

#lonObject (readonly)

Returns the value of attribute lon.



17
18
19
# File 'lib/geo_hex/ll.rb', line 17

def lon
  @lon
end

Class Method Details

.normalize(lon) ⇒ Float

Returns longitude.

Returns:

  • (Float)

    longitude



7
8
9
10
11
12
13
14
15
# File 'lib/geo_hex/ll.rb', line 7

def self.normalize(lon)
  if lon < -180
    lon += 360
  elsif lon > 180
    lon -= 360
  else
    lon
  end
end

Instance Method Details

#distance_to(other) ⇒ Float

Returns distance in meters.

Parameters:

Returns:

  • (Float)

    distance in meters



49
50
51
52
53
54
55
56
57
58
# File 'lib/geo_hex/ll.rb', line 49

def distance_to(other)
  d_lat, d_lon = (other.lat - lat) * H_D2R / 2.0, (other.lon - lon) * H_D2R / 2.0
  lat1, lat2   = lat * H_D2R, other.lat * H_D2R

  a = Math.sin(d_lat) ** 2 +
      Math.sin(d_lon) ** 2 * Math.cos(lat1) * Math.cos(lat2)
  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))

  H_ER * c
end

#eastingFloat

Returns mercator easting.

Returns:

  • (Float)

    mercator easting



26
27
28
# File 'lib/geo_hex/ll.rb', line 26

def easting
  @easting ||= lon * H_BASE / 180.0
end

#northingFloat

Returns mercator northing.

Returns:

  • (Float)

    mercator northing



31
32
33
# File 'lib/geo_hex/ll.rb', line 31

def northing
  @northing ||= Math.log(Math.tan((90 + lat) * H_D2R / 2)) / Math::PI * H_BASE
end

#to_ppGeoHex::PP

Returns the full projection point coordinates.

Returns:

  • (GeoHex::PP)

    the full projection point coordinates



36
37
38
# File 'lib/geo_hex/ll.rb', line 36

def to_pp
  GeoHex::PP.new(easting, northing)
end

#to_zone(level) ⇒ GeoHex::Zone

Converts coordinates to Zone

Parameters:

  • level (Integer)

    the level

Returns:



43
44
45
# File 'lib/geo_hex/ll.rb', line 43

def to_zone(level)
  to_pp.to_zone(level)
end