-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse_area_scanning
executable file
·136 lines (117 loc) · 4.42 KB
/
analyse_area_scanning
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import csv
import sys
import pandas as pd
import datetime
from pyproj import Proj
import numpy as np
# Your UTM zone depends on your longitude; see http://www.dmap.co.uk/utmworld.htm
utm_zone = 51
projection = Proj(proj='utm', zone=utm_zone, ellps='WGS84')
def latlon_to_utm(lat, lon):
"""Returns (x, y) coordinates in metres"""
return projection(lon, lat)
def utm_to_latlon(coord):
"""Returns (lat, lon) in degrees"""
lon, lat = projection(coord[0], coord[1], inverse=True)
return lat, lon
# C D
# +-+-+-+-+-+
# +-+-+-+-+-+
# +-+-+-+-+-+
# A B
#
# y^
# |
# +-> x
#
subY = 24
subX = 24
cell_size = 2.5
wpA = [29.86719457094162, 121.5382643168712]
wpB = [29.86702872486077, 121.5388580404463]
wpC = [29.86769032147261, 121.5385162033053]
wpA_utm = latlon_to_utm(wpA[0], wpA[1])
wpB_utm = latlon_to_utm(wpB[0], wpB[1])
wpC_utm = latlon_to_utm(wpC[0], wpC[1])
# Unit vector AB
vAB = np.array([wpB_utm[0] - wpA_utm[0], wpB_utm[1] - wpA_utm[1]])
AB = np.linalg.norm(vAB)
vAB = vAB/AB
# Unit vector AC
vAC = np.array([wpC_utm[0] - wpA_utm[0], wpC_utm[1] - wpA_utm[1]])
AC = np.linalg.norm(vAC)
vAC = vAC/AC
vAB_orth = vAC
def cell_ij_to_coord(i,j):
"""Get the (lat, lon) of the 4 corners of a grid cell"""
A = wpA_utm + (vAB * cell_size * i) + (vAB_orth * cell_size * j)
B = A + (vAB_orth * cell_size)
C = B + (vAB * cell_size)
D = A + (vAB * cell_size)
return (utm_to_latlon(A),
utm_to_latlon(B),
utm_to_latlon(C),
utm_to_latlon(D))
def coord_to_cell_ij(point):
x, y = latlon_to_utm(point[0], point[1])
point_base = [x - wpA_utm[0], y - wpA_utm[1]]
point_i = np.dot(point_base, vAB)*subX/AB
point_j = np.dot(point_base, vAB_orth)*subY/AC
if (0 <= point_i < subX) and (0 <= point_j < subY):
return[int(point_i), int(point_j)]
def combine_attempts(master_file_name):
group_attempts = pd.read_csv(master_file_name, names=['team', 'attempt'])
group_df = pd.DataFrame()
total_attempt_time = {}
for index, row in group_attempts.iterrows():
attempt_csv = row.attempt
print(attempt_csv)
team = row.team
print(team)
attempt_df = pd.read_csv(attempt_csv, names=['time','latitude','longitude','speed','course'])
attempt_df['time'] = attempt_df['time'].apply(pd.to_datetime)
attempt_df.set_index('time', inplace=True)
if team not in total_attempt_time:
total_attempt_time[team] = datetime.timedelta(0, 0)
attempt_df['team'] = row.team
attempt_time = attempt_df.index[-1] - attempt_df.index[0]
#print((attempt_time + total_attempt_time[team]) < datetime.timedelta(seconds=15*60))
if (attempt_time + total_attempt_time[team]) < datetime.timedelta(seconds=15*60):
group_df = pd.concat([group_df, attempt_df])
total_attempt_time[team] += attempt_time
print(team + str(total_attempt_time[team]))
# print(team + ' less than 15 mins')
else:
remaining_attempt_time = datetime.timedelta(seconds=15*60) - total_attempt_time[team]
last_valid_point = attempt_df.index[0] + remaining_attempt_time
group_df = pd.concat([group_df, attempt_df.loc[:last_valid_point]])
total_attempt_time[team] = datetime.timedelta(seconds=15*60)
print(team + str(total_attempt_time[team]))
# print(team + ' reaches 15 mins ')
# Only maintain the first 15 mins
#print(total_attempt_time)
group_df = group_df.sort_index()
return group_df
def team_score(group_df):
df = group_df
team_visited_grids = {}
team_scores = {}
for index, row in df.iterrows():
grid_index = str(coord_to_cell_ij((row.latitude, row.longitude)))
team = row.team
if grid_index not in team_visited_grids:
team_visited_grids[grid_index] = [team]
elif team not in team_visited_grids[grid_index]:
team_visited_grids[grid_index].append(team)
del team_visited_grids['None']
for grid_index, teams in team_visited_grids.items():
for team in teams:
if team not in team_scores:
team_scores[team] = 0
else:
#print((1/(teams.index(team)+1)))
team_scores[team] += (1/(teams.index(team)+1))
return team_scores
if __name__ == "__main__":
group_df = combine_attempts(sys.argv[1])
print(team_score(group_df))