Module: HrLite::Geo

Defined in:
lib/hr_lite/geo.rb

Overview

Pure-Ruby haversine — no geocoding gem needed to answer "how far is this punch from the office?" at office-radius precision.

Constant Summary collapse

EARTH_RADIUS_M =
6_371_000.0

Class Method Summary collapse

Class Method Details

.deg2rad(deg) ⇒ Object



18
19
20
# File 'lib/hr_lite/geo.rb', line 18

def self.deg2rad(deg)
  deg.to_f * Math::PI / 180
end

.distance_m(lat1, lng1, lat2, lng2) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/hr_lite/geo.rb', line 7

def self.distance_m(lat1, lng1, lat2, lng2)
  rlat1 = deg2rad(lat1)
  rlat2 = deg2rad(lat2)
  dlat = deg2rad(lat2.to_f - lat1.to_f)
  dlng = deg2rad(lng2.to_f - lng1.to_f)

  a = Math.sin(dlat / 2)**2 + Math.cos(rlat1) * Math.cos(rlat2) * Math.sin(dlng / 2)**2
  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
  EARTH_RADIUS_M * c
end