Module: Geokit::Mappable::ClassMethods
- Defined in:
- lib/geokit/mappable.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#distance_between(from, to, options = {}) ⇒ Object
Returns the distance between two points.
- #distance_between_flat(from, to, units) ⇒ Object
- #distance_between_sphere(from, to, units) ⇒ Object
-
#endpoint(start, heading, distance, options = {}) ⇒ Object
Given a start point, distance, and heading (in degrees), provides an endpoint.
-
#geocode(location, options = {}) ⇒ Object
Geocodes a location using the multi geocoder.
-
#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.
- #math_error_classes ⇒ Object
-
#midpoint_between(from, to, options = {}) ⇒ Object
Returns the midpoint, given two points.
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 |
# File 'lib/geokit/mappable.rb', line 36 def distance_between(from, to, ={}) from=Geokit::LatLng.normalize(from) to=Geokit::LatLng.normalize(to) return 0.0 if from == to # fixes a "zero-distance" bug units = [:units] || Geokit::default_units formula = [:formula] || Geokit::default_formula case formula when :sphere then distance_between_sphere(from, to, units) when :flat then distance_between_flat(from, to, units) end end |
#distance_between_flat(from, to, units) ⇒ Object
57 58 59 60 61 |
# File 'lib/geokit/mappable.rb', line 57 def distance_between_flat(from, to, units) lat_length = units_per_latitude_degree(units) * (from.lat - to.lat) lng_length = units_per_longitude_degree(from.lat, units) * (from.lng - to.lng) Math.sqrt(lat_length ** 2 + lng_length ** 2) end |
#distance_between_sphere(from, to, units) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/geokit/mappable.rb', line 48 def distance_between_sphere(from, to, units) lat_sin = Math.sin(deg2rad(from.lat)) * Math.sin(deg2rad(to.lat)) lat_cos = Math.cos(deg2rad(from.lat)) * Math.cos(deg2rad(to.lat)) lng_cos = Math.cos(deg2rad(to.lng) - deg2rad(from.lng)) units_sphere_multiplier(units) * Math.acos(lat_sin + lat_cos * lng_cos) rescue *math_error_classes 0.0 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/geokit/mappable.rb', line 89 def endpoint(start,heading, distance, ={}) units = [:units] || Geokit::default_units ratio = distance.to_f / units_sphere_multiplier(units) start = Geokit::LatLng.normalize(start) lat = deg2rad(start.lat) lng = deg2rad(start.lng) heading = deg2rad(heading) sin_ratio = Math.sin(ratio) cos_ratio = Math.cos(ratio) sin_lat = Math.sin(lat) cos_lat = Math.cos(lat) end_lat = Math.asin(sin_lat * cos_ratio + cos_lat * sin_ratio * Math.cos(heading)) end_lng = lng + Math.atan2(Math.sin(heading) * sin_ratio * cos_lat, cos_ratio - 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.
126 127 128 129 130 |
# File 'lib/geokit/mappable.rb', line 126 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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/geokit/mappable.rb', line 74 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 |
#math_error_classes ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/geokit/mappable.rb', line 63 def math_error_classes 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) 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)
115 116 117 118 119 120 121 122 123 |
# File 'lib/geokit/mappable.rb', line 115 def midpoint_between(from,to,={}) from=Geokit::LatLng.normalize(from) units = [:units] || Geokit::default_units heading=from.heading_to(to) distance=from.distance_to(to,) midpoint=from.endpoint(heading,distance/2,) end |