-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_vector.py
198 lines (167 loc) · 6.13 KB
/
feature_vector.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from os import listdir
from os.path import isfile, join
import haversine as h
import math
from scipy.spatial import ConvexHull
import numpy as np
import matplotlib.pyplot as plt
import min_bounding_box as mbd
def Interpret(line):
# id,time(date hh:mm:ss),x,y
params=line.split(',')
tid=params[0]
cal=params[1]
date=cal.split(' ')[0]
ymd=date.split('-')
yy=int(ymd[0])
mo=int(ymd[1])
dd=int(ymd[2])
time=cal.split(' ')[1]
hms=time.split(':')
hh=int(hms[0])
mm=int(hms[1])
ss=int(hms[2])
x=float(params[2])
y=float(params[3])
record={'id':tid,'date':date,'time':time,\
'dt':{'yy':yy,'mo':mo,'dd':dd,'hh':hh,'mm':mm,'ss':ss},\
'x':x,'y':y}
return record
def calcTime(start,end):
# return seconds
# time={'yy':yy,'mo':mo,'dd':dd,'hh':hh,'mm':mm,'ss':ss}
import datetime
d1=datetime.datetime(int(start['yy']), int(start['mo']), int(start['dd']), int(start['hh']), int(start['mm']), int(start['ss']))
d2=datetime.datetime(end['yy'], end['mo'], end['dd'],end['hh'],end['mm'],end['ss'])
# datetime.timedelta(seconds=1)
return (d2-d1).seconds
def calculate_initial_compass_bearing(pointA, pointB):
if (type(pointA) != tuple) or (type(pointB) != tuple):
raise TypeError("Only tuples are supported as arguments")
lat1 = math.radians(pointA[0])
lat2 = math.radians(pointB[0])
diffLong = math.radians(pointB[1] - pointA[1])
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1)
* math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
initial_bearing = math.degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
#directories = [f for f in listdir('data/')]
#onlyfiles = [f for f in listdir(file_path) if isfile(join(file_path, f))]
#print directories
#print onlyfiles
# 99494
#a = '8717,2008-02-02 13:30:47,116.39465,39.84351'
#b = '8717,2008-02-02 13:31:53,116.39439,39.84353'
#a = Interpret(a)
#b = Interpret(b)
#print a
#print b
#c = calcTime(a['dt'], b['dt'])
#print c
##############################################################
# clean trajectories of 01 directory
onlyfiles = [f for f in listdir('data/01/')]
onlyfiles_ = [f for f in listdir('data/02/')]
#print onlyfiles
#lgtlat_threshold = 0.002
#for file_n in onlyfiles:
# with open('data/'+file_n,'w') as w:
# with open('data/01/'+file_n, 'r') as f:
# while True:
# line1 = f.readline()
# line2 = f.readline()
# if not line2: break # EOF
# tmp1 = Interpret(line1)
# tmp2 = Interpret(line2)
# if abs(tmp1['x'] - tmp2['x']) <= lgtlat_threshold and abs(tmp1['y'] - tmp2['y']) <= lgtlat_threshold :
#
# tmp = line2
# w.write(line1)
# w.write(line2)
# else:
# print line1
# print line2
#################################################
#feature_vector
#for file_n in onlyfiles:
# with open('data/vectors','a') as w:
# my_list = [line for line in open('data/01/'+file_n)]
# sum_dist = .0
# sum_angles = .0
# a = Interpret(my_list[0])
# b = Interpret(my_list[len(my_list)-1])
# dist_total = h.Haversine((float(a['x']), float(a['y'])), (float(b['x']),float(b['y']))).meters
# time = calcTime(a['dt'],b['dt'])
# for i in range(0, len(my_list)-2):
# m = Interpret(my_list[i])
# s = Interpret(my_list[i+1])
# sum_dist += h.Haversine([m['x'],m['y']],[s['x'],s['y']]).meters
# sum_angles += calculate_initial_compass_bearing((m['x'],m['y']),(s['x'],s['y']))
# move_hability = float(dist_total / sum_dist)
# w.write( str(a['id'])+','+str(dist_total)+','+str(time)+','+str(sum_dist)+','+str(sum_angles)+','+str(move_hability)+'\n')
######################## Just 1 hour #################################
#for file_n in onlyfiles:
# with open('data/vectors_hour','a') as w:
# my_list = [line for line in open('data/01/'+file_n)]
# sum_dist = .0
# sum_angles = .0
# a = Interpret(my_list[0])
# b = Interpret(my_list[650])
# dist_total = h.Haversine((float(a['x']), float(a['y'])), (float(b['x']),float(b['y']))).meters
# time = calcTime(a['dt'],b['dt'])
# points = []
# for i in range(0, 651):
# m = Interpret(my_list[i])
# s = Interpret(my_list[i+1])
# points.append([m['x'],m['y']])
# sum_dist += h.Haversine([m['x'],m['y']],[s['x'],s['y']]).meters
# sum_angles += calculate_initial_compass_bearing((m['x'],m['y']),(s['x'],s['y']))
# if(sum_dist == 0):
# move_hability = .0
# else:
# move_hability = float(dist_total / sum_dist)
# hull = ConvexHull(points)
# perimeter = hull.area
# area = hull.volume
# minxy = np.min(points, axis=0)
# maxxy = np.max(points, axis=0)
# width = maxxy[0] - minxy[0]
# height = maxxy[1] - minxy[1]
# aspect_ratio = (maxxy[0] - minxy[0])/ (maxxy[1] - minxy[1])
# w.write( str(a['id'])+','+str(dist_total)+','+str(time)+','+str(sum_dist)+','+str(sum_angles)+','+str(move_hability)+\
# ','+str(perimeter)+','+str(area)+','+str(width)+','+str(height)+','+str(width/height)+'\n')
################################# DATA 02 300 POINTS ########################
for file_n in onlyfiles_:
with open('data/vectors_hour_2','a') as w:
my_list = [line for line in open('data/02/'+file_n)]
print(file_n)
sum_dist = .0
sum_angles = .0
a = Interpret(my_list[0])
b = Interpret(my_list[299])
dist_total = h.Haversine((float(a['x']), float(a['y'])), (float(b['x']),float(b['y']))).meters
time = calcTime(a['dt'],b['dt'])
points = []
for i in range(0, 301):
m = Interpret(my_list[i])
s = Interpret(my_list[i+1])
points.append([m['x'],m['y']])
sum_dist += h.Haversine([m['x'],m['y']],[s['x'],s['y']]).meters
sum_angles += calculate_initial_compass_bearing((m['x'],m['y']),(s['x'],s['y']))
if(sum_dist == 0):
move_hability = .0
else:
move_hability = float(dist_total / sum_dist)
hull = ConvexHull(points)
perimeter = hull.area
area = hull.volume
minxy = np.min(points, axis=0)
maxxy = np.max(points, axis=0)
width = maxxy[0] - minxy[0]
height = maxxy[1] - minxy[1]
aspect_ratio = (maxxy[0] - minxy[0])/ (maxxy[1] - minxy[1])
w.write( str(a['id'])+','+str(dist_total)+','+str(time)+','+str(sum_dist)+','+str(sum_angles)+','+str(move_hability)+\
','+str(perimeter)+','+str(area)+','+str(width)+','+str(height)+','+str(width/height)+'\n')