--- a/include/db/trip-dao.inc.php +++ b/include/db/trip-dao.inc.php @@ -2,7 +2,10 @@ function getTrip($tripID) { global $conn; - $query = "Select * from trips where trip_id = '$tripID' join routes on trips.route_id = routes.route_id LIMIT 1"; + $query = "Select * from trips + join routes on trips.route_id = routes.route_id + where trip_id = '$tripID' + LIMIT 1"; debug($query, "database"); $result = pg_query($conn, $query); if (!$result) { @@ -46,16 +49,16 @@ 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, + $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_lat,stop_lon,stop_name,stop_code, stop_sequence,service_id,trips.route_id,route_short_name,route_long_name FROM stop_times 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) { @@ -108,7 +111,18 @@ } 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; } return Array(); @@ -129,14 +143,30 @@ $r = pg_fetch_assoc($result); return $r['arrival_time']; } -function viaPointNames($tripid, $stopid) +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 viaPointNames($tripid, $stop_sequence = "") { global $conn; $query = "SELECT stop_name FROM stop_times join stops on stops.stop_id = stop_times.stop_id WHERE stop_times.trip_id = '$tripid' -AND stop_sequence > '$stop_sequence' -AND substr(stop_code,1,2) != 'Wj' ORDER BY stop_sequence"; +".($stop_sequence != "" ? "AND stop_sequence > '$stop_sequence'" : ""). +"AND substr(stop_code,1,2) != 'Wj' ORDER BY stop_sequence"; debug($query, "database"); $result = pg_query($conn, $query); if (!$result) {