Module: Tillless::Location::InstanceMethods

Defined in:
lib/tillless-core/location.rb

Instance Method Summary collapse

Instance Method Details

#calibrateObject

Pre-calculate the various terms of the Great Circle Distance Formula

Tillless::Location::RADIUS_OF_EARTH *                            # Radius of Earth in KM
  acos[
     sin(lat1/57.2958) *                     # term1
     sin(lat2/57.2958) +                     # term2
     cos(lat1/57.2958) *                     # term3
     cos(lat2/57.2958) *                     # term4
     cos(
         lon2/57.2958  -                     # term5
         lon1/57.2958)                       # term6
  ]


186
187
188
189
190
191
192
193
194
# File 'lib/tillless-core/location.rb', line 186

def calibrate
  self.term1 = Math.sin(self.lat/Tillless::Location::GREAT_CIRCLE_FACTOR)
  self.term2 = Math.sin(self.lat/Tillless::Location::GREAT_CIRCLE_FACTOR)
  self.term3 = Math.cos(self.lat/Tillless::Location::GREAT_CIRCLE_FACTOR)
  self.term4 = Math.cos(self.lat/Tillless::Location::GREAT_CIRCLE_FACTOR)
  self.term5 = self.lon/Tillless::Location::GREAT_CIRCLE_FACTOR
  self.term6 = self.lon/Tillless::Location::GREAT_CIRCLE_FACTOR
  self.calibrated = true
end

#great_circle_distance_from_here_to(otherloc) ⇒ Object

Calculate the great circle distance between this GeoLocation and the specified other location. This uses the formula specified here: www.meridianworlddata.com/Distance-Calculation.asp

The formula used to calculate the Great Circle Distance is:

RADIUS_OF_EARTH * acos[sin(lat1/57.2958) * sin(lat2/57.2958) + cos(lat1/57.2958) * cos(lat2/57.2958) * cos(lon2/57.2958 - lon1/57.2958)]

Where: lat1/lon1 is the lat and lon of this Location, and lat2/lon2 is the lat and lon of the other location.

:otherloc location to compare with return great circle distance from here to otherloc raises LocationCalibrationException if either self or otherloc are not calibrated



210
211
212
213
214
215
216
217
218
# File 'lib/tillless-core/location.rb', line 210

def great_circle_distance_from_here_to(otherloc)
  raise Tillless::Location::LocationCalibrationException if ! (self.calibrated and otherloc.calibrated)
  stage1 = self.term1 * otherloc.term2 +
           self.term3 * otherloc.term4 *
           Math.cos(self.term5 - otherloc.term6)
  # fix floating point roudning error that manifests in ActiveRecord and Postgres
  stage1 = stage1.round if stage1 < -1 or stage1 > 1
  Tillless::Location::RADIUS_OF_EARTH * Math.acos(stage1)
end

#nearby?(otherloc, min_distance = Tillless::Location::MIN_DISTANCE_FOR_NEARBY_IN_KM) ⇒ Boolean

Test to see if the specified location is near this one, where ‘near’ means within 100 meters.

:otherloc location to check :min_distance minimum distance for which to be considered ‘nearby’, default = Tillless::Location::MIN_DISTANCE_FOR_NEARBY_IN_KM return true if the specified location is near this one raises LocationCalibrationException if either self or otherloc are not calibrated



228
229
230
231
# File 'lib/tillless-core/location.rb', line 228

def nearby?(otherloc, min_distance = Tillless::Location::MIN_DISTANCE_FOR_NEARBY_IN_KM)
  raise Tillless::Location::LocationCalibrationException if ! (self.calibrated and otherloc.calibrated)
  ( (great_circle_distance_from_here_to(otherloc) - min_distance) <= 0.0)
end

#set_and_calibrate(_lat, _lon) ⇒ Object

Set both lat and lon and recalibrate in the one go



168
169
170
171
172
# File 'lib/tillless-core/location.rb', line 168

def set_and_calibrate(_lat, _lon)
  self.lat = _lat
  self.lon = _lon
  calibrate
end