Class: GeoRb::Distance
- Inherits:
-
Object
- Object
- GeoRb::Distance
- Defined in:
- lib/geo_rb/distance.rb
Constant Summary collapse
- EARTH_RADIUS =
6_371.0088
Instance Attribute Summary collapse
-
#kilometers ⇒ Object
(also: #km)
readonly
Returns the value of attribute kilometers.
-
#meters ⇒ Object
(also: #m)
readonly
Returns the value of attribute meters.
Class Method Summary collapse
Instance Method Summary collapse
- #ensure_same_altitude(a, b) ⇒ Object
-
#initialize(*locations) ⇒ Distance
constructor
A new instance of Distance.
- #measure(a, b) ⇒ Object
Constructor Details
#initialize(*locations) ⇒ Distance
Returns a new instance of Distance.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/geo_rb/distance.rb', line 25 def initialize(*locations) GeoRb.logger.debug locations @meters = case locations.size when 0..1 0 else locations.each_cons(2).reduce(0) do |distance, pair| a, b = sanitize_location(pair.first), sanitize_location(pair.last) distance + measure(a, b) end end @kilometers = @meters.to_d / 1_000 end |
Instance Attribute Details
#kilometers ⇒ Object (readonly) Also known as: km
Returns the value of attribute kilometers.
10 11 12 |
# File 'lib/geo_rb/distance.rb', line 10 def kilometers @kilometers end |
#meters ⇒ Object (readonly) Also known as: m
Returns the value of attribute meters.
10 11 12 |
# File 'lib/geo_rb/distance.rb', line 10 def meters @meters end |
Class Method Details
.between(*addresses, adapter: GeoRb::GeoCoders::Nominatim) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/geo_rb/distance.rb', line 14 def self.between(*addresses, adapter: GeoRb::GeoCoders::Nominatim) return new if addresses.size == 1 requests = addresses.map do |address| Concurrent::Promises.future(address) { |a| adapter.new.geocode(a) } end locations = Concurrent::Promises.zip(*requests).value! new(*locations) end |
Instance Method Details
#ensure_same_altitude(a, b) ⇒ Object
39 40 41 |
# File 'lib/geo_rb/distance.rb', line 39 def ensure_same_altitude(a, b) raise LatitudeMismatch if (a.altitude - b.altitude).abs > 1e-6 end |
#measure(a, b) ⇒ Object
43 44 45 46 47 |
# File 'lib/geo_rb/distance.rb', line 43 def measure(a, b) ensure_same_altitude(a, b) r = Wgs84.new.distance(a.latitude, a.longitude, b.latitude, b.longitude).first r.to_d end |