-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaversine.py
More file actions
34 lines (30 loc) · 1.04 KB
/
haversine.py
File metadata and controls
34 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import csv
import math
import time
def calc_distance(point1, point2):
R = 6373000 # earth radius in meters
lat1_r = point1[0] * (math.pi / 180)
lat2_r = point2[0] * (math.pi / 180)
x = (point1[1] - point2[1]) * (math.pi / 180)
x *= math.cos((lat1_r + lat2_r) / 2)
y = (lat1_r - lat2_r)
d = math.sqrt(x * x + y * y) * R
return(round(d, 2))
fname = "points.csv"
outfile = open("results.csv", 'w+', newline = '')
writer = csv.writer(outfile, delimiter = ',')
with open(fname, newline = "") as fp:
reader = csv.reader(fp)
header = next(reader, None)
headerLen = len(header)
if(headerLen == 4):
start = time.time()
header.append('distance')
writer.writerow(header)
for row in reader:
point1 = (float(row[0]), float(row[1]))
point2 = (float(row[-2]), float(row[-1]))
distance = calc_distance(point1,point2)
row.append(distance)
writer.writerow(row)
print("Time Elapsed", round(time.time() - start, 3), "seconds.")