rgeissert / http-redirector

Debian mirrors HTTP redirector
http://httpredir.debian.org/
89 stars 8 forks source link

Distance calculation is wrong #50

Open sesse opened 11 years ago

sesse commented 11 years ago

Hi,

The distance calculation assumes the Earth is a flat rectangle that doesn't wrap, which is obviously not the case. For small distances, the “flat rectangle” assumption is not so bad, but not properly wrapping, be it at the zero-meridian of between -180/+180 (I don't know offhand which one is used), can give completely bogus results.

You probably want http://en.wikipedia.org/wiki/Haversine_formula instead.

sesse commented 11 years ago

FWIW, it seems Math::Trig already has this. I've used something like

my $pihalf = Math::Trig::pi / 2;
my $lon1 = Math::Trig::deg2rad($geoloc->{'lon'});
my $lat1 = $pihalf - Math::Trig::deg2rad($geoloc->{'lat'});
my $lon2 = Math::Trig::deg2rad($client_geoloc->{'lon'});
my $lat2 = $pihalf - Math::Trig::deg2rad($client_geoloc->{'lat'});

$hosts{$ip} = Math::Trig::great_circle_distance($lon1, $lat1, $lon2, $lat2);