/
Trajectory.java
81 lines (72 loc) · 2.27 KB
/
Trajectory.java
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
import java.util.SortedMap;
import java.util.Map;
// this class describes the complete path a vehicle takes through time and space
public class Trajectory {
private String tripId;
private String serviceId;
private String routeId;
private SortedMap<Long, Stop> trajectory;
// the contructor for the Trajectory class
public Trajectory(String tripId, String serviceId, String routeId, SortedMap<Long, Stop> trajectory) {
this.tripId = tripId;
this.serviceId = serviceId;
this.routeId = routeId;
this.trajectory = trajectory;
}
// get the trip_id
public String getTripId() {
return this.tripId;
}
// get the service_id
public String getServiceId() {
return this.serviceId;
}
// get the route_id
public String getRouteId() {
return this.routeId;
}
// returns the position of the vehicle at timestamp time
public Coordinate getPosition(long time) {
Stop nextStop = null;
Stop prevStop = null;
long prevTime = 0;
long nextTime = 0;
if (this.isActive(time)) {
for (Map.Entry<Long, Stop> entry : this.trajectory.entrySet()) {
nextTime = entry.getKey();
if (nextTime == time) { // return that cord if time is in the entries
return entry.getValue().getCord();
} else if (nextTime > time) { // wait to calculate cord if time is in between
nextStop = entry.getValue();
break;
}
prevStop = entry.getValue();
prevTime = nextTime;
}
} else { // doesn't show the stop if not active
return new Coordinate(0, 0);
}
double prevLat = prevStop.getLat();
double prevLon = prevStop.getLon();
double nextLat = nextStop.getLat();
double nextLon = nextStop.getLon();
double lat = prevLat + (nextLat - prevLat) * (time - prevTime) / (nextTime - prevTime);
double lon = prevLon + (nextLon - prevLon) * (time - prevTime) / (nextTime - prevTime);
return new Coordinate(lat, lon);
}
// returns whether the vehicle is active at timestamp time
public boolean isActive(long time) {
return time <= this.trajectory.lastKey() && time >= this.trajectory.firstKey();
}
// get the trajectory
public SortedMap<Long, Stop> getTrajectory() {
return this.trajectory;
}
// convert to string
public String toString() {
return "Trajectory {\n"
+ "tripId: "+this.tripId+",\n"
+ "serviceId: "+this.serviceId+",\n"
+ "}\n";
}
}