Class: Loc::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/loc/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lng) ⇒ Location

Returns a new instance of Location.

Raises:

  • (TypeError)


6
7
8
9
10
11
# File 'lib/loc/location.rb', line 6

def initialize(lat, lng)
  raise(TypeError, "lat should be a Numeric.") unless lat.is_a?(Numeric)
  raise(TypeError, "lng should be a Numeric.") unless lng.is_a?(Numeric)
  @lat = lat
  @lng = lng
end

Instance Attribute Details

#latObject (readonly)

Returns the value of attribute lat.



4
5
6
# File 'lib/loc/location.rb', line 4

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



4
5
6
# File 'lib/loc/location.rb', line 4

def lng
  @lng
end

Class Method Details

.[](*args) ⇒ Object



18
19
20
# File 'lib/loc/location.rb', line 18

def [](*args)
  from_array(args)
end

.from_array(a) ⇒ Object Also known as: from_a



14
15
16
# File 'lib/loc/location.rb', line 14

def from_array(a)
  new(a[0], a[1])
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
# File 'lib/loc/location.rb', line 61

def ==(other)
  return true if other.equal?(self)
  return false unless other.instance_of?(self.class)
  lat == other.lat && lng == other.lng
end

#[](*args) ⇒ Object



67
68
69
# File 'lib/loc/location.rb', line 67

def [](*args)
  to_array[*args]
end

#bearing_to(other) ⇒ Object

Give the bearing from the object to the other. Params :

other

Location



42
43
44
45
46
47
# File 'lib/loc/location.rb', line 42

def bearing_to(other)
  dlat = to_radians(other.lat - lat)
  dlng = to_radians(other.lng - lng)
  bearing = Math.atan2(dlat, dlng)
  (90 - to_degrees(bearing) + 360) % 360
end

#distance_to(other) ⇒ Object

Calculate the distance in meters betwen the object and another location using the ‘Haversine’ formula. Params :

other

Location



29
30
31
32
33
34
35
36
37
# File 'lib/loc/location.rb', line 29

def distance_to(other)
  dlat = to_radians(other.lat - lat)
  dlon = to_radians(other.lng - lng)
  a = Math.sin(dlat / 2) * Math.sin(dlat / 2) +
      Math.cos(to_radians(lat)) * Math.cos(to_radians(other.lat)) *
      Math.sin(dlon / 2) * Math.sin(dlon / 2)
  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
  6371 * c * 1000
end

#inspectObject



83
84
85
# File 'lib/loc/location.rb', line 83

def inspect
  "<#{self.class} #{{ lat: lat, lng: lng }}>"
end

#lat_degrees_per_kmObject

Give the number of latitude degrees for a kilometer distance at location



51
52
53
# File 'lib/loc/location.rb', line 51

def lat_degrees_per_km
  1 / 111.195
end

#lng_degrees_per_kmObject

Calculate the number of longitude degrees for kilometer distance at location



57
58
59
# File 'lib/loc/location.rb', line 57

def lng_degrees_per_km
  1 / (distance_to(Location[lat, lng + 1]) / 1000)
end

#to_arrayObject Also known as: to_a



71
72
73
# File 'lib/loc/location.rb', line 71

def to_array
  [lat, lng]
end

#to_hashObject Also known as: to_h



75
76
77
# File 'lib/loc/location.rb', line 75

def to_hash
  { lat: lat, lng: lng }
end

#to_sObject



79
80
81
# File 'lib/loc/location.rb', line 79

def to_s
  "#{lat},#{lng}"
end