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)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geokit/mappable.rb', line 36

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
    error_classes = [Errno::EDOM]

    # Ruby 1.9 raises {Math::DomainError}, but it is not defined in Ruby
    # 1.8. Backwards-compatibly rescue both errors.
    error_classes << Math::DomainError if defined?(Math::DomainError)

    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 *error_classes
      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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/geokit/mappable.rb', line 82

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.



119
120
121
122
123
# File 'lib/geokit/mappable.rb', line 119

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.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/geokit/mappable.rb', line 67

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)


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

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