Module: Geokit::Mappable::ClassMethods

Defined in:
lib/geokit/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, :kms, :nms (Geokit::default_units is the default) :formula - valid values are :flat or :sphere (Geokit::default_formula is the default)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/geokit/mappable.rb', line 34

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
    begin
      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)))
    rescue Errno::EDOM
      0.0
    end
  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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/geokit/mappable.rb', line 74

def endpoint(start,heading, distance, options={})
  units = options[:units] || Geokit::default_units
  radius = case units
           when :kms; EARTH_RADIUS_IN_KMS
           when :nms; EARTH_RADIUS_IN_NMS
           else EARTH_RADIUS_IN_MILES
           end
  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, options = {}) ⇒ Object

Geocodes a location using the multi geocoder.

Raises:

  • (Geokit::Geocoders::GeocodeError)


111
112
113
114
115
# File 'lib/geokit/mappable.rb', line 111

def geocode(location, options = {})
  res = Geocoders::MultiGeocoder.geocode(location, options)
  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.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geokit/mappable.rb', line 59

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


100
101
102
103
104
105
106
107
108
# File 'lib/geokit/mappable.rb', line 100

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