Module: Geokit::Mappable

Included in:
LatLng
Defined in:
lib/geokit/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, :kms, and :nms.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PI_DIV_RAD =
0.0174
KMS_PER_MILE =
1.609
NMS_PER_MILE =
0.868976242
EARTH_RADIUS_IN_MILES =
3963.19
EARTH_RADIUS_IN_KMS =
EARTH_RADIUS_IN_MILES * KMS_PER_MILE
EARTH_RADIUS_IN_NMS =
EARTH_RADIUS_IN_MILES * NMS_PER_MILE
MILES_PER_LATITUDE_DEGREE =
69.1
KMS_PER_LATITUDE_DEGREE =
MILES_PER_LATITUDE_DEGREE * KMS_PER_MILE
NMS_PER_LATITUDE_DEGREE =
MILES_PER_LATITUDE_DEGREE * NMS_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.



27
28
29
# File 'lib/geokit/mappable.rb', line 27

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, :kms, :or :nms (:miles is the default) :formula - valid values are :flat or :sphere (:sphere is the default)



183
184
185
# File 'lib/geokit/mappable.rb', line 183

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, :kms, or :nms (:miles is the default)



203
204
205
# File 'lib/geokit/mappable.rb', line 203

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



196
197
198
# File 'lib/geokit/mappable.rb', line 196

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



190
191
192
# File 'lib/geokit/mappable.rb', line 190

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, :kms, or :nms (:miles is the default)



210
211
212
# File 'lib/geokit/mappable.rb', line 210

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



173
174
175
176
177
# File 'lib/geokit/mappable.rb', line 173

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)
  nil
end