Created
October 17, 2012 19:18
-
-
Save werebus/3907523 to your computer and use it in GitHub Desktop.
Haversine distance (http://www.movable-type.co.uk/scripts/latlong.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env ruby | |
# | |
class GeoDist | |
def self.haversine(lat1, lon1, lat2, lon2) | |
r = 6371 ## km | |
dLat = self.toRadians(lat2-lat1) | |
dLon = self.toRadians(lon2-lon1) | |
lat1 = self.toRadians(lat1) | |
lat2 = self.toRadians(lat2) | |
a = Math.sin(dLat/2) * Math.sin(dLat/2) + | |
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2) | |
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)) | |
d = r * c | |
return d | |
end | |
def self.toRadians(degrees) | |
return degrees*(Math::PI/180) | |
end | |
end | |
puts GeoDist.haversine(42.1234, 72.1234, 42.1230, 72.1200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment