Module: GeoKit::Mappable

Included in:
LatLng
Defined in:
lib/geo_kit/mappable.rb

Overview

Contains class and instance methods providing distance calcuation services. This module is meant to be mixed into classes containing lat and lng attributes where distance calculation is desired.

At present, two forms of distance calculations are provided:

  • Pythagorean Theory (flat Earth) - which assumes the world is flat and loses accuracy over long distances.

  • Haversine (sphere) - which is fairly accurate, but at a performance cost.

Distance units supported are :miles and :kms.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PI_DIV_RAD =
0.0174
KMS_PER_MILE =
1.609
EARTH_RADIUS_IN_MILES =
3963.19
EARTH_RADIUS_IN_KMS =
EARTH_RADIUS_IN_MILES * KMS_PER_MILE
MILES_PER_LATITUDE_DEGREE =
69.1
KMS_PER_LATITUDE_DEGREE =
MILES_PER_LATITUDE_DEGREE * KMS_PER_MILE
LATITUDE_DEGREES =
EARTH_RADIUS_IN_MILES / MILES_PER_LATITUDE_DEGREE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object

Mix below class methods into the includer.



24
25
26
# File 'lib/geo_kit/mappable.rb', line 24

def self.included(receiver) # :nodoc:
  receiver.extend ClassMethods
end

Instance Method Details

#distance_to(other, options = {}) ⇒ Object Also known as: distance_from

Returns the distance from another point. The other point parameter is required to have lat and lng attributes. Valid options are: :units - valid values are :miles or :kms (:miles is the default) :formula - valid values are :flat or :sphere (:sphere is the default)



154
155
156
# File 'lib/geo_kit/mappable.rb', line 154

def distance_to(other, options={})
  self.class.distance_between(self, other, options)
end

#endpoint(heading, distance, options = {}) ⇒ Object

Returns the endpoint, given a heading (in degrees) and distance.

Valid option: :units - valid values are :miles or :kms (:miles is the default)



174
175
176
# File 'lib/geo_kit/mappable.rb', line 174

def endpoint(heading,distance,options={})
  self.class.endpoint(self,heading,distance,options)  
end

#heading_from(other) ⇒ Object

Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) FROM the given point. The given point can be a LatLng or a string to be Geocoded



167
168
169
# File 'lib/geo_kit/mappable.rb', line 167

def heading_from(other)
  self.class.heading_between(other,self)
end

#heading_to(other) ⇒ Object

Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) to the given point. The given point can be a LatLng or a string to be Geocoded



161
162
163
# File 'lib/geo_kit/mappable.rb', line 161

def heading_to(other)
  self.class.heading_between(self,other)
end

#midpoint_to(other, options = {}) ⇒ Object

Returns the midpoint, given another point on the map.

Valid option: :units - valid values are :miles or :kms (:miles is the default)



181
182
183
# File 'lib/geo_kit/mappable.rb', line 181

def midpoint_to(other, options={})
  self.class.midpoint_between(self,other,options)
end

#to_lat_lngObject

Extracts a LatLng instance. Use with models that are acts_as_mappable



144
145
146
147
148
# File 'lib/geo_kit/mappable.rb', line 144

def to_lat_lng
  return self if instance_of?(GeoKit::LatLng) || instance_of?(GeoKit::GeoLoc)
  return LatLng.new(send(self.class.lat_column_name),send(self.class.lng_column_name)) if self.class.respond_to?(:acts_as_mappable)
  return nil
end