--- a/include/db/trip-dao.inc.php +++ b/include/db/trip-dao.inc.php @@ -49,7 +49,7 @@ points.append((stop.stop_lat, stop.stop_lon)) return points*/ } -function getTimeInterpolatedTrip($tripID) +function getTimeInterpolatedTrip($tripID, $range = "") { global $conn; $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_lat,stop_lon,stop_name,stop_code, @@ -58,7 +58,7 @@ join trips on trips.trip_id = stop_times.trip_id join routes on trips.route_id = routes.route_id join stops on stops.stop_id = stop_times.stop_id -WHERE trips.trip_id = '$tripID' ORDER BY stop_sequence"; +WHERE trips.trip_id = '$tripID' $range ORDER BY stop_sequence"; debug($query, "database"); $result = pg_query($conn, $query); if (!$result) { @@ -109,11 +109,21 @@ } return $rv; } -function getTimeInterpolatedTripAtStop($tripID, $stop_sequence, $stop_id = "") +function getTimeInterpolatedTripAtStop($tripID, $stop_sequence) { - foreach (getTimeInterpolatedTrip($tripID) as $tripStop) { + global $conn; + // limit interpolation to between nearest actual points. + $prevTimePoint = pg_fetch_assoc(pg_query($conn," SELECT trip_id,stop_id, + stop_sequence +FROM stop_times +WHERE trip_id = '$tripID' and stop_sequence < $stop_sequence and stop_times.arrival_time IS NOT NULL ORDER BY stop_sequence DESC LIMIT 1")); + $nextTimePoint = pg_fetch_assoc(pg_query($conn," SELECT trip_id,stop_id, + stop_sequence +FROM stop_times +WHERE trip_id = '$tripID' and stop_sequence > $stop_sequence and stop_times.arrival_time IS NOT NULL ORDER BY stop_sequence LIMIT 1")); + $range = "AND stop_sequence >= '{$prevTimePoint['stop_sequence']}' AND stop_sequence <= '{$nextTimePoint['stop_sequence']}'"; + foreach (getTimeInterpolatedTrip($tripID,$range) as $tripStop) { if ($tripStop['stop_sequence'] == $stop_sequence) return $tripStop; - if ($tripStop['stop_id'] == $stop_id) return $tripStop; } return Array(); } @@ -133,10 +143,26 @@ $r = pg_fetch_assoc($result); return $r['arrival_time']; } -function viaPointNames($tripid, $stop_sequence = "") +function getActiveTrips($time) +{ + global $conn; + if ($time == "") $time = current_time(); + $query = "Select distinct stop_times.trip_id, start_times.arrival_time as start_time, end_times.arrival_time as end_time from stop_times, (SELECT trip_id,arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL +AND stop_sequence = '1') as start_times, (SELECT trip_id,max(arrival_time) as arrival_time from stop_times WHERE stop_times.arrival_time IS NOT NULL group by trip_id) as end_times +WHERE start_times.trip_id = end_times.trip_id AND stop_times.trip_id = end_times.trip_id AND $time > start_times.arrival_time AND $time < end_times.arrival_time"; + debug($query, "database"); + $result = pg_query($conn, $query); + if (!$result) { + databaseError(pg_result_error($result)); + return Array(); + } + return pg_fetch_all($result); +} + +function viaPoints($tripid, $stop_sequence = "") { global $conn; - $query = "SELECT stop_name + $query = "SELECT stops.stop_id, stop_name, arrival_time FROM stop_times join stops on stops.stop_id = stop_times.stop_id WHERE stop_times.trip_id = '$tripid' ".($stop_sequence != "" ? "AND stop_sequence > '$stop_sequence'" : ""). @@ -147,7 +173,14 @@ databaseError(pg_result_error($result)); return Array(); } - $pointNames = pg_fetch_all($result); - return r_implode(", ", $pointNames); + return pg_fetch_all($result); +} +function viaPointNames($tripid, $stop_sequence = "") +{ + $viaPointNames = Array(); + foreach(viaPoints($tripid, $stop_sequence) as $point) { + $viaPointNames[] = $point['stop_name']; + } + return r_implode(", ", $viaPointNames); } ?>