class RouteBoxer::LatLng

Attributes

lat[RW]
latitude[RW]
latitude=[RW]
lng[RW]
longitude[RW]
longitude=[RW]

Public Class Methods

new(lat = nil, lng = nil) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 12
def initialize(lat = nil, lng = nil)
  lat = lat.to_f if lat && !lat.is_a?(Numeric)
  lng = lng.to_f if lng && !lng.is_a?(Numeric)
  @lat = lat
  @lng = lng
end

Public Instance Methods

equals(other) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 35
def equals(other)
  lat == other.lat && lng == other.lng
end
lat=(lat) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 19
def lat=(lat)
  @lat = lat.to_f if lat
end
ll() click to toggle source
# File lib/route_boxer/lat_lng.rb, line 27
def ll
  "#{lat},#{lng}"
end
lng=(lng) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 23
def lng=(lng)
  @lng = lng.to_f if lng
end
rhumb_bearing_to(dest) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 65
def rhumb_bearing_to(dest)
  dLon = deg2rad(dest.lng - lng)
  dPhi = Math.log(Math.tan(deg2rad(dest.lat) / 2 + Math::PI / 4) / Math.tan(deg2rad(lat) / 2 + Math::PI / 4))

  if dLon.abs > Math::PI
    dLon = dLon > 0 ? - (2 * Math::PI - dLon) : (2 * Math::PI + dLon)
  end

  to_brng(Math.atan2(dLon, dPhi))
end
rhumb_destination_point(brng, dist, r = 6378137) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 39
def rhumb_destination_point(brng, dist, r = 6378137)
  d = dist / r.to_f

  brng = deg2rad(brng)
  lat1 = deg2rad(lat)
  lng1 = deg2rad(lng)

  dLat = d * Math.cos(brng)
  dLat = 0 if dLat.abs < 1e-10

  lat2 = lat1 + dLat
  dPhi = Math.log(Math.tan(lat2 / 2.0 + Math::PI / 4.0) / Math.tan(lat1 / 2.0 + Math::PI / 4.0))
  q = (dPhi != 0) ? dLat / dPhi.to_f : Math.cos(lat1)
  dLon = d * Math.sin(brng) / q

  if lat2.abs > (Math::PI / 2.0)
    lat2 = lat2 > 0 ? Math::PI - lat2 : -Math::PI - lat2
  end

  lng2 = (lng1 + dLon + 3 * Math::PI) % (2 * Math::PI) - Math::PI
  lat2 = rad2deg(lat2)
  lng2 = rad2deg(lng2)

  LatLng.new(lat2, lng2)
end
to_a() click to toggle source
# File lib/route_boxer/lat_lng.rb, line 31
def to_a
  [lat, lng]
end
to_brng(number) click to toggle source
# File lib/route_boxer/lat_lng.rb, line 76
def to_brng(number)
  (rad2deg(number) + 360) % 360
end