-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug4.py
230 lines (223 loc) · 12.1 KB
/
debug4.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 6 10:11:52 2019
@author: kocak
"""
# trip çıkar bir tane 129-130 to 1-2 var onu düzelt.
import pandas as pd
dff3 = dff2[dff2["Direction"]==1]
for i, group in dff3.groupby("ShiftID"):
listIndexes = []
group = group.sort_values(["F_TS","ST1"])
for index1,row1 in group.iterrows():
listIndexes.append(index1)
firstRow = True
for index,row in group.iterrows():
if firstRow:
tripID = 1
group.loc[index,"TripID"] = tripID
dff3.loc[index,"TripID"] = tripID
fs = row["ST1"]
firstRow = False
continue
if row["ST1"] < fs:
listForSearch = listIndexes[listIndexes.index(index):]
if group.loc[listForSearch[0],"F_IND"] == group.loc[listForSearch[1],"F_IND"] and \
group.loc[listForSearch[0],"L_IND"] == group.loc[listForSearch[1],"L_IND"] and \
group.loc[listForSearch[1],"ST1"] > fs:
tripID +=0
group.loc[index,"TripID"] = tripID + 1
group.loc[listForSearch[1],"TripID"] = tripID
dff3.loc[index,"TripID"] = tripID + 1
dff3.loc[listForSearch[1],"TripID"] = tripID
fs = row["ST1"]
continue
else:
tripID +=1
group.loc[index,"TripID"] = tripID
dff3.loc[index,"TripID"] = tripID
fs = row["ST1"]
sc_segments_w_tripID = dff3
#%%
from shapely.geometry import Point
import geopandas as gpd
sc_segments_w_tripID["#ofPassengers"] = 0
sc_segments_w_tripID["#ofPassenger@FS"] = 0
sc_segments_w_tripID["#ofPassenger@LS"] = 0
sc_segments_w_tripID["#ofPassenger@Route"] = 0
#sc30["ShiftID"] = ""
#sc30["tripID"] = 0
#sc30["AssignedStopID"] = ""
#sc30["Stat"] = 0 # 0 means on route, 1 means at stop
#gpsAracNo = []
sc_segments_FP = gpd.GeoDataFrame(index=sc_segments_w_tripID.index,crs={'init':'epsg:4326'},geometry=[Point(x, y) for x,y in zip(sc_segments_w_tripID.DURAK1_LON, sc_segments_w_tripID.DURAK1_LAT)])
sc_segments_LP = gpd.GeoDataFrame(index=sc_segments_w_tripID.index,geometry=[Point(x, y) for x,y in zip(sc_segments_w_tripID.DURAK2_LON, sc_segments_w_tripID.DURAK2_LAT)])
sc_segments_FP["geometry50_FP"] = sc_segments_FP["geometry"].buffer(0.00045045045)
sc_segments_FP["geometry100_FP"] = sc_segments_FP["geometry"].buffer(0.0009009009)
sc_segments_LP["geometry50_LP"] = sc_segments_LP["geometry"].buffer(0.00045045045)
sc_segments_LP["geometry100_LP"] = sc_segments_LP["geometry"].buffer(0.0009009009)
sc_segments50_FP = gpd.GeoDataFrame(index=sc_segments_FP.index,crs={'init':'epsg:4326'},geometry = sc_segments_FP["geometry50_FP"] )
sc_segments50_LP = gpd.GeoDataFrame(index=sc_segments_LP.index,crs={'init':'epsg:4326'},geometry = sc_segments_LP["geometry50_LP"] )
#%%
sc30_notGPS = gpd.GeoDataFrame(sc30_notGPS,geometry =[Point(x, y) for x,y in zip(sc30_notGPS.BOYLAM, sc30_notGPS.ENLEM)] )
sc30_notGPS["AssignedStopID"] = "null"
sc30_notGPS["Stat"] = 0
for shiftID, group in sc_segments_w_tripID.groupby("ShiftID"):
group = group.sort_values(by=["F_TS"])
scForGroup = sc30_notGPS[(sc30_notGPS["ARAC_NO"]== int(shiftID[4:7]))]
# gpsAracNo.append(int(shiftID[4:7]))
isFirstRow = True
for i, row in group.iterrows():
sc_between_segment = scForGroup[(pd.to_datetime(scForGroup["TIMESTAMP"]) >= pd.to_datetime(row["F_TS"])) & (pd.to_datetime(scForGroup["TIMESTAMP"]) <= pd.to_datetime(row["L_TS"]))]
sc_segments_w_tripID.loc[i,"#ofPassenger"] = sc_between_segment.shape[0]
splitedSegtID = row["SEGTID"].split("_")
if isFirstRow:
indexesForFS = []
indexesForLS = []
indexesForRoute = []
for index, row_sc in sc_between_segment.iterrows():
sc30_notGPS.loc[index,"ShiftID"] = row["ShiftID"]
sc30_notGPS.loc[index,"tripID"] = row["TripID"]
if sc_segments_FP.loc[i,"geometry50_FP"].contains(row_sc["geometry"]):
indexesForFS.append(index)
sc30_notGPS.loc[index,"AssignedStopID"] = splitedSegtID[3]
sc_between_segment.loc[index,"AssignedStopID"] = splitedSegtID[3]
elif sc_segments_LP.loc[i,"geometry50_LP"].contains(row_sc["geometry"]):
indexesForLS.append(index)
sc30_notGPS.loc[index,"AssignedStopID"] = splitedSegtID[4]
sc_between_segment.loc[index,"AssignedStopID"] = splitedSegtID[4]
else:
indexesForRoute.append(index)
distanceToFS = calculateDistance([row_sc["ENLEM"],row_sc["BOYLAM"]],[row["DURAK1_LAT"],row["DURAK1_LON"]])
distanceToLS = calculateDistance([row_sc["ENLEM"],row_sc["BOYLAM"]],[row["DURAK2_LAT"],row["DURAK2_LON"]])
distanceBetweenFS_LS = calculateDistance([row["DURAK1_LAT"],row["DURAK1_LON"]],[row["DURAK2_LAT"],row["DURAK2_LON"]])
if distanceToFS <= 100 and distanceToLS > distanceBetweenFS_LS:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_b_"+str(int(splitedSegtID[3])-1)+"_"+splitedSegtID[3]
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_b_"+str(int(splitedSegtID[3])-1)+"_"+splitedSegtID[3]
elif distanceToLS <= 100 and distanceToFS > distanceBetweenFS_LS:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_a_"+splitedSegtID[4]+"_"+str(int(splitedSegtID[4])+1)
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_a_"+splitedSegtID[4]+"_"+str(int(splitedSegtID[4])+1)
else:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_"+splitedSegtID[3]+"_"+splitedSegtID[4]
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_"+splitedSegtID[3]+"_"+splitedSegtID[4]
isFirstRow = False
sc_segments_w_tripID.loc[i,"#ofPassenger@FS"] = len(indexesForFS)
sc_segments_w_tripID.loc[i,"#ofPassenger@LS"] = len(indexesForLS)
sc_segments_w_tripID.loc[i,"#ofPassenger@Route"] = len(indexesForRoute)
prevIndex = i
continue
indexesForFS = []
indexesForLS = []
indexesForRoute = []
for index, row_sc in sc_between_segment.iterrows():
sc30_notGPS.loc[index,"ShiftID"] = row["ShiftID"]
sc30_notGPS.loc[index,"tripID"] = row["TripID"]
if sc_segments_FP.loc[i,"geometry50_FP"].contains(row_sc["geometry"]):
indexesForFS.append(index)
sc30_notGPS.loc[index,"AssignedStopID"] = splitedSegtID[3]
sc_between_segment.loc[index,"AssignedStopID"] = splitedSegtID[3]
elif sc_segments_LP.loc[i,"geometry50_LP"].contains(row_sc["geometry"]):
indexesForLS.append(index)
sc30_notGPS.loc[index,"AssignedStopID"] = splitedSegtID[4]
sc_between_segment.loc[index,"AssignedStopID"] = splitedSegtID[4]
else:
indexesForRoute.append(index)
distanceToFS = calculateDistance([row_sc["ENLEM"],row_sc["BOYLAM"]],[row["DURAK1_LAT"],row["DURAK1_LON"]])
distanceToLS = calculateDistance([row_sc["ENLEM"],row_sc["BOYLAM"]],[row["DURAK2_LAT"],row["DURAK2_LON"]])
distanceBetweenFS_LS = calculateDistance([row["DURAK1_LAT"],row["DURAK1_LON"]],[row["DURAK2_LAT"],row["DURAK2_LON"]])
if distanceToFS <= 100 and distanceToLS > distanceBetweenFS_LS:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_b_"+str(int(splitedSegtID[3])-1)+"_"+splitedSegtID[3]
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_b_"+str(int(splitedSegtID[3])-1)+"_"+splitedSegtID[3]
elif distanceToLS <= 100 and distanceToFS > distanceBetweenFS_LS:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_a_"+splitedSegtID[4]+"_"+str(int(splitedSegtID[4])+1)
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_a_"+splitedSegtID[4]+"_"+str(int(splitedSegtID[4])+1)
else:
sc30_notGPS.loc[index,"AssignedStopID"] = "en-route_"+splitedSegtID[3]+"_"+splitedSegtID[4]
sc_between_segment.loc[index,"AssignedStopID"] = "en-route_"+splitedSegtID[3]+"_"+splitedSegtID[4]
sc_segments_w_tripID.loc[i,"#ofPassenger@FS"] = len(indexesForFS)
sc_segments_w_tripID.loc[i,"#ofPassenger@LS"] = len(indexesForLS)
sc_segments_w_tripID.loc[i,"#ofPassenger@Route"] = len(indexesForRoute)
prevIndex = i
# =============================================================================
# for i,group in dff2.groupby("ShiftID"):
# group = group.sort_values(by=["ST1"])
# historyF_IND = []
# historyL_IND = []
# historyIndex = []
# directions = []
# controls = [] #shows changes direction
# controlIndex = []
# listIndexes = []
# for index,row in group.iterrows():
# listIndexes.append(index)
# isFirstRow = True
# isUpdated = False
# group["Direction"] = 0
# for index,row in group.iterrows():
# if isFirstRow:
# historyF_IND.append(row["F_IND"])
# historyL_IND.append(row["L_IND"])
# historyIndex.append([index])
# isFirstRow = False
# group.loc[index,"Direction"] = 0
# directions.append(0)
# controls.append(0)
# controlIndex.append(0)
# continue
# isUpdated = False
# for j in range(len(historyF_IND)):
# if historyF_IND[j] <= row["F_IND"] and historyL_IND[j] +1 >= row["F_IND"]:
# historyIndex[j].append(index)
# if historyF_IND[j] < row["F_IND"]:
# if directions[j] == -1:
# controls[j] =-1
# controlIndex[j] = index
# group.loc[index,"Direction"] = 1
# directions[j] = 1
# elif historyL_IND[j] < row["L_IND"]:
# group.loc[index,"Direction"] = 1
# if directions[j] == -1:
# controls[j] =-1
# controlIndex[j] = index
# directions[j] = 1
# historyF_IND[j] = row["F_IND"]
# historyL_IND[j] = row["L_IND"]
# isUpdated = True
# elif historyF_IND[j] -1 <= row["L_IND"] and historyL_IND[j] >= row["L_IND"]:
# historyIndex[j].append(index)
# if historyL_IND[j] > row["L_IND"]:
# if directions[j] == 1:
# controls[j] = 1
# controlIndex[j] = index
# group.loc[index,"Direction"] = -1
# directions[j] = -1
# elif historyF_IND[j] > row["F_IND"]:
# if directions[j] == 1:
# controls[j] =1
# controlIndex[j] = index
# group.loc[index,"Direction"] = -1
# directions[j] = -1
# historyF_IND[j] = row["F_IND"]
# historyL_IND[j] = row["L_IND"]
# isUpdated = True
# if not isUpdated:
# historyF_IND.append(row["F_IND"])
# historyL_IND.append(row["L_IND"])
# historyIndex.append([index])
# directions.append(0)
# controls.append(0)
# controlIndex.append(0)
# group.loc[index,"Direction"] = 0
#
# for ind in range(len(historyIndex)):
# if controls[ind] == 0:
# for jInd in historyIndex[ind]:
# group.loc[jInd,"Direction"] = directions[ind]
# else:
# firstIndex = historyIndex[ind].index(controlIndex[ind])
# for jInd in historyIndex[ind][0:firstIndex]:
# group.loc[jInd,"Direction"] = controls[ind]
# for jInd in historyIndex[ind][firstIndex:]:
# group.loc[jInd,"Direction"] = directions[ind]
#
# =============================================================================