lib/gpsutils.rb in gpsutils-0.1.2 vs lib/gpsutils.rb in gpsutils-0.2.0
- old
+ new
@@ -1,7 +1,12 @@
module GpsUtils
+ R = 6371e3
+ def self.to_radians(degrees)
+ degrees * Math::PI / 180
+ end
+
class Point
# @!attribute [r] lat
# @return [Float, Integer] Latitude
attr_reader :lat
@@ -33,9 +38,32 @@
end
def to_s
"#{@lat},#{@lng}"
end
+
+ # Measure the distance between this point and another.
+ #
+ # Distance is calculated using equirectangular projection.
+ # @see https://en.wikipedia.org/wiki/Equirectangular_projection
+ #
+ # @param other [Point]
+ # @return [Float]
+ # @raise [ArgumentError] if other is not a Point
+ def distance(other)
+ unless other.is_a? Point
+ raise ArgumentError.new 'other must be a Point.'
+ end
+
+ dlng = GpsUtils::to_radians(other.lng - @lng)
+ dlat = GpsUtils::to_radians(other.lat - @lat)
+
+ x = dlng * Math.cos(dlat / 2)
+ y = GpsUtils::to_radians(other.lat - @lat)
+
+ Math.sqrt(x**2 + y**2) * GpsUtils::R
+ end
+
end
class BoundingBox
# @!attribute [r] nw
# @return [Point] North-West corner