Module: GeoKit::Mappable::ClassMethods

Defined in:
lib/geo_kit/mappable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#distance_between(from, to, options = {}) ⇒ Object

Returns the distance between two points. The from and to parameters are required to have lat and lng attributes. Valid options are: :units - valid values are :miles or :kms (GeoKit::default_units is the default) :formula - valid values are :flat or :sphere (GeoKit::default_formula is the default)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/geo_kit/mappable.rb', line 31

def distance_between(from, to, options={})
  from=GeoKit::LatLng.normalize(from)
  to=GeoKit::LatLng.normalize(to)
  return 0.0 if from == to # fixes a "zero-distance" bug
  units = options[:units] || GeoKit::default_units
  formula = options[:formula] || GeoKit::default_formula
  case formula
  when :sphere          
    units_sphere_multiplier(units) * 
        Math.acos( Math.sin(deg2rad(from.lat)) * Math.sin(deg2rad(to.lat)) + 
        Math.cos(deg2rad(from.lat)) * Math.cos(deg2rad(to.lat)) * 
        Math.cos(deg2rad(to.lng) - deg2rad(from.lng)))   
  when :flat
    Math.sqrt((units_per_latitude_degree(units)*(from.lat-to.lat))**2 + 
        (units_per_longitude_degree(from.lat, units)*(from.lng-to.lng))**2)
  end
end

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

Given a start point, distance, and heading (in degrees), provides an endpoint. Returns a LatLng instance. Typically, the instance method will be used instead of this method.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/geo_kit/mappable.rb', line 67

def endpoint(start,heading, distance, options={})
  units = options[:units] || GeoKit::default_units
  radius = units == :miles ? EARTH_RADIUS_IN_MILES : EARTH_RADIUS_IN_KMS
  start=GeoKit::LatLng.normalize(start)        
  lat=deg2rad(start.lat)
  lng=deg2rad(start.lng)
  heading=deg2rad(heading)
  distance=distance.to_f
  
  end_lat=Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
                    Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))

  end_lng=lng+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
                         Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))

  LatLng.new(rad2deg(end_lat),rad2deg(end_lng))
end

#geocode(location) ⇒ Object

Geocodes a location using the multi geocoder.



100
101
102
103
104
# File 'lib/geo_kit/mappable.rb', line 100

def geocode(location)
  res = Geocoders::MultiGeocoder.geocode(location)
  return res if res.success
  raise GeoKit::Geocoders::GeocodeError      
end

#heading_between(from, to) ⇒ Object

Returns heading in degrees (0 is north, 90 is east, 180 is south, etc) from the first point to the second point. Typicaly, the instance methods will be used instead of this method.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geo_kit/mappable.rb', line 52

def heading_between(from,to)
  from=GeoKit::LatLng.normalize(from)
  to=GeoKit::LatLng.normalize(to)

  d_lng=deg2rad(to.lng-from.lng)
  from_lat=deg2rad(from.lat)
  to_lat=deg2rad(to.lat) 
  y=Math.sin(d_lng) * Math.cos(to_lat)
  x=Math.cos(from_lat)*Math.sin(to_lat)-Math.sin(from_lat)*Math.cos(to_lat)*Math.cos(d_lng)
  heading=to_heading(Math.atan2(y,x))
end

#midpoint_between(from, to, options = {}) ⇒ Object

Returns the midpoint, given two points. Returns a LatLng. Typically, the instance method will be used instead of this method. Valid option:

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


89
90
91
92
93
94
95
96
97
# File 'lib/geo_kit/mappable.rb', line 89

def midpoint_between(from,to,options={})
  from=GeoKit::LatLng.normalize(from)

  units = options[:units] || GeoKit::default_units
  
  heading=from.heading_to(to)
  distance=from.distance_to(to,options)
  midpoint=from.endpoint(heading,distance/2,options)
end