Skip to content

Commit

Permalink
Seems like the earth is not flat after all. Made it use haversine for…
Browse files Browse the repository at this point in the history
…mula to calculate distance between latitude/longitude points.
  • Loading branch information
Janhouse committed Sep 23, 2012
1 parent 642a9aa commit 40437e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
~
old/
*~
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ TODO:
* Store the measurement data and draw graphs.
* Measure the speed for the whole network interface (similar like it was in the
old version of Tespeed).
* Random data generator uses too much resources.


23 changes: 18 additions & 5 deletions tespeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import time
import string
import random
from math import sqrt
from math import radians, cos, sin, asin, sqrt

from StringIO import StringIO

Expand Down Expand Up @@ -81,16 +81,27 @@ def __init__(self, server = ""):


def Distance(self, one, two):
# Calculates distance between two objects in plane
dist = sqrt(pow(two[0] - one[0], 2) + pow(two[1] - one[1], 2))
return dist
#Calculate the great circle distance between two points
#on the earth specified in decimal degrees (haversine formula)
#(http://stackoverflow.com/posts/4913653/revisions)
# convert decimal degrees to radians

lon1, lat1, lon2, lat2 = map(radians, [one[0], one[1], two[0], two[1]])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km


def Closest(self, center, points):
# Returns object that is closest to center
closest={}
for p in range(len(points)):
now = self.Distance(center, [points[p]['lat'], points[p]['lon']])
points[p]['distance']=now
while True:
if now in closest:
now=now+00.1
Expand All @@ -116,7 +127,9 @@ def TestLatency(self, servers):
now=self.TestSingleLatency(server['url']+"latency.txt?x=" + str( time.time() ))*1000
if now == -1:
continue
print "%0.0fms latency for %s (%s, %s, %s)" % (now, server['url'], server['sponsor'], server['name'], server['country'])
print server
print "\n\n\n\n\n\n\n\n"
print "%0.0f ms latency for %s (%s, %s, %s) [%0.2f km]" % (now, server['url'], server['sponsor'], server['name'], server['country'], server['distance'])

if (now < shortest and shortest > 0) or shortest < 0:
shortest = now
Expand Down

0 comments on commit 40437e1

Please sign in to comment.