/** * For the given trip ID, fetch all the stop times in order of increasing stop_sequence. * This is an efficient iteration over a tree map. */ public Iterable<StopTime> getOrderedStopTimesForTrip (String trip_id) { Map<Fun.Tuple2, StopTime> tripStopTimes = stop_times.subMap( Fun.t2(trip_id, null), Fun.t2(trip_id, Fun.HI) ); return tripStopTimes.values(); }
@Override public void loadOneRow() throws IOException { Frequency f = new Frequency(); Trip trip = getRefField("trip_id", true, feed.trips); f.sourceFileLine = row + 1; // offset line number by 1 to account for 0-based row index f.trip_id = trip.trip_id; f.start_time = getTimeField("start_time", true); f.end_time = getTimeField("end_time", true); f.headway_secs = getIntField("headway_secs", true, 1, 24 * 60 * 60); f.exact_times = getIntField("exact_times", false, 0, 1); f.feed = feed; feed.frequencies.add(Fun.t2(f.trip_id, f)); } }
public void add(K key,V value){ entries.add(Fun.t2(key, value)); } public Iterator<Tuple2<K,V>> batchInsertIterator(){
public void add(K key,V value){ entries.add(Fun.t2(key, value)); } public Iterator<Tuple2<K,V>> batchInsertIterator(){
public void add(K key,V value){ entries.add(Fun.t2(key, value)); } public Iterator<Tuple2<K,V>> batchInsertIterator(){
/** * For the given trip ID, fetch all the stop times in order of increasing stop_sequence. * This is an efficient iteration over a tree map. */ public Iterable<StopTime> getOrderedStopTimesForTrip (String trip_id) { Map<Fun.Tuple2, StopTime> tripStopTimes = stop_times.subMap( Fun.t2(trip_id, null), Fun.t2(trip_id, Fun.HI) ); return tripStopTimes.values(); }
/** Get trip speed in meters per second. */ public double getTripSpeed (String trip_id, boolean straightLine) { StopTime firstStopTime = this.stop_times.ceilingEntry(Fun.t2(trip_id, null)).getValue(); StopTime lastStopTime = this.stop_times.floorEntry(Fun.t2(trip_id, Fun.HI)).getValue(); // ensure that stopTime returned matches trip id (i.e., that the trip has stoptimes) if (!firstStopTime.trip_id.equals(trip_id) || !lastStopTime.trip_id.equals(trip_id)) { return Double.NaN; } double distance = getTripDistance(trip_id, straightLine); // trip time (in seconds) int time = lastStopTime.arrival_time - firstStopTime.departure_time; return distance / time; // meters per second }
@Override public void add(long entityId, Map<String, Object> properties) { for (Map.Entry<String, Object> entry : properties.entrySet()) { getSet(entry.getKey()).add(Fun.t2(entry.getValue(), entityId)); } index.add(entityId,properties); }
@Override public Object[] deserialize( DataInput in, int start, int end, int size ) throws IOException { Object[] ret = new Object[size]; A a = null; int acount = 0; for (int i = start; i < end; i++) { if (acount == 0) { // read new A a = aSerializer.deserialize(in, -1); acount = DataInput2.unpackInt(in); } B b = bSerializer.deserialize(in, -1); ret[i] = Fun.t2(a, b); acount--; } assert (acount == 0); return ret; }
@Override public Object[] deserialize( DataInput in, int start, int end, int size ) throws IOException { Object[] ret = new Object[size]; A a = null; int acount = 0; for (int i = start; i < end; i++) { if (acount == 0) { // read new A a = aSerializer.deserialize(in, -1); acount = DataInput2.unpackInt(in); } B b = bSerializer.deserialize(in, -1); ret[i] = Fun.t2(a, b); acount--; } assert (acount == 0); return ret; }
@Override public void loadOneRow() throws IOException { Frequency f = new Frequency(); Trip trip = getRefField("trip_id", true, feed.trips); f.id = row + 1; // offset line number by 1 to account for 0-based row index f.trip_id = trip.trip_id; f.start_time = getTimeField("start_time", true); f.end_time = getTimeField("end_time", true); f.headway_secs = getIntField("headway_secs", true, 1, 24 * 60 * 60); f.exact_times = getIntField("exact_times", false, 0, 1); f.feed = feed; feed.frequencies.add(Fun.t2(f.trip_id, f)); } }
/** * Get average speed for set of trips that begin within the time window in meters per second. * @param trips * @param from * @param to * @return avg. speed (meters per second) */ public double getAverageSpeedForTrips (Collection<Trip> trips, LocalTime from, LocalTime to) { TDoubleList speeds = new TDoubleArrayList(); for (Trip trip : trips) { StopTime firstStopTime = feed.stop_times.ceilingEntry(Fun.t2(trip.trip_id, null)).getValue(); LocalTime tripBeginTime = LocalTime.ofSecondOfDay(firstStopTime.departure_time % 86399); // convert 24hr+ seconds to 0 - 86399 // skip trip if begin time is before or after specified time period if (tripBeginTime.isAfter(to) || tripBeginTime.isBefore(from)) { continue; } // TODO: swap straight lines for actual geometry? double speed = feed.getTripSpeed(trip.trip_id, true); if (!Double.isNaN(speed)) { speeds.add(speed); } } if (speeds.isEmpty()) return -1; return speeds.sum() / speeds.size(); }