-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
167 lines (150 loc) · 5.22 KB
/
preprocess.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
from write import write_move,write_sp, auth
thr=float(raw_input("Enter threshold (recommended 0.0001) : "))
auth()
path="/home/ss/Dropbox/Wriju/Codes/10.4.1.72_trajectory_clone/data"
#path=raw_input("Enter full input path (root directory of the files) : ")
'''
##test
thr=0.0001
#path="/home/mehul/1/2"
#path ="/home/ss/Dropbox/Wriju/Codes/10.4.1.72_trajectory_clone/data2/peace"
path = "/home/ss/Dropbox/Wriju/Codes/10.4.1.72_trajectory_clone/data3"
def write_move(taxino,lat,lon,starttime,endtime,count,hy):
print(starttime,endtime,count,lat,lon,"move")
global counter
def write_sp(taxino,lat,lon,starttime,endtime,count,hy):
print(starttime,endtime,count,lat,lon,"sp")
global counter
##test end
'''
import os
import psycopg2
from write import time_difference
os.chdir(path)
counter=0
#from writepos import write
taxi={}
folderlist=os.listdir(os.getcwd())
totnum=0
for folder in folderlist:
os.chdir(path+"/"+folder)
filelist=os.listdir(os.getcwd())
for files in filelist:
totnum+=1
numnow=0; pumbalines=0; pumbanow=0
prec=1
#precision=6
for folder in folderlist:
os.chdir(path+"/"+folder)
filelist=os.listdir(os.getcwd())
for files in filelist:
os.system("clear")
print "Scanned file("+files+") "+str(numnow)+" of "+str(totnum)+"."
print "% complete: "+str(numnow*100/totnum)
try:
print "The last file was compressed by: "+str(pumbanow*100.0/pumbalines)
except: pass
numnow+=1
f=open(files,"r")
pumbalines=0; pumbanow=0
global records
records=[]
flag=1
while(1):
line=f.readline()
pumbalines+=1
if not line: break
data=line.strip('\n').split(",")
dates=data[1].strip(" ").split(" ")
date=dates[0].split("-")
time=dates[1].split(":")
day, month, year =int(date[2]), int(date[1]), int(date[0])
hr, mins, sec = int(time[0]), int(time[1]), int(time[2])
lat=float(data[2])
lon=float(data[3])
#global records
#print([[day,month,year,hr,mins,sec],lat,lon])
#print(len(records))
if prec: precision=len(str(lat).split(".")[1]); prec=0
taxino=int(data[0])
templat=0; templon=0
try:
#count=len(taxi[taxino])-1
pass
#print("count changed : "+str(count))
except:
#print("except")
taxi[taxino]=[]
taxi[taxino]=[[day,month,year,hr,mins,sec],lat,lon]
count=1
try:
i=1
while(1):
try:
templat+=taxi[taxino][i]
templon+=taxi[taxino][i+1]
i+=2
except:
count=(len(taxi[taxino])-1)*0.5
#count = Count + 1
#print(count)
break
#print("try")
#count = float(count)+float(1.0)
dist=(((templat/(count))-lat)**2)+(((templon/(count))-lon)**2)
#print(str(templat/count)+" "+str(templon/count)+" "+str(lat)+" "+str(lon))
#print "dist: "+str(dist)
if(flag):
starpos=[lat,lon]
starTIE=[day,month,year,hr,mins,sec]
flag=0
if (dist<thr**2):
global stemp
stemp=[day,month,year,hr,mins,sec]
stemppos=[lat,lon]
taxi[taxino].append(lat)
taxi[taxino].append(lon)
records.append([[day,month,year,hr,mins,sec],lat,lon])
#print("if")
else:
flag=1
i=len(records)-1
timeELAPSED=time_difference(starTIE,stemp)
precision = 6
#print(timeELAPSED," sec")
if ( count >= 10 or timeELAPSED >= 60 ):
write_move(taxino,starpos[0],starpos[1], starTIE,starTIE,-1,precision)
write_sp(taxino,(templat+lat)/(count+float(1.0)),(templon+lon)/(count+float(1.0)), taxi[taxino][0],stemp,count,precision)
write_move(taxino,stemppos[0],stemppos[1], stemp,stemp,count,precision)
pumbanow+=3
while len(records) > 0 : records.pop()
else:
#records.pop();records.pop();records.pop();
p=len(records)
for i in range(len(records)):
try:
record1=records.pop(0)
#record2=records.pop(0)
write_move(taxino,record1[1],record1[2], record1[0],record1[0],0,precision)
pumbanow+=1
except:
while len(records) > 0 : records.pop()
while len(records) > 0 : records.pop()
#print(len(records),p)
#global records
#records=[]
''' #print(len(records))
else :
write_sp(taxino,(templat+lat)/count,(templon+lon)/count, taxi[taxino][0],stemp,count,precision)
'''
#write_move(taxino,lat,lon, [day,month,year,hr,mins,sec],[day,month,year,hr,mins,sec],0,precision)
#write(taxino,lat,lon, [day,month,year,hr,mins,sec],[day,month,year,hr,mins,sec],count,"move")
counter+=1
taxi[taxino]=[[day,month,year,hr,mins,sec],lat,lon]
#print("else")
except Exception, e:
#print e
taxi[taxino]=[]
taxi[taxino]=[[day,month,year,hr,mins,sec],lat,lon]
#print("oexceptf")
#os.chdir(path)