Convert myway timeliness visualisations to new GTFS feed
[busui.git] / myway / myway_timeliness_calculate.php
blob:a/myway/myway_timeliness_calculate.php -> blob:b/myway/myway_timeliness_calculate.php
--- a/myway/myway_timeliness_calculate.php
+++ b/myway/myway_timeliness_calculate.php
@@ -29,7 +29,8 @@
 
 //collect all observation not in delta
 $query = "select * from myway_observations INNER JOIN myway_stops
-ON myway_observations.myway_stop=myway_stops.myway_stop 
+ON myway_observations.myway_stop=myway_stops.myway_stop INNER JOIN myway_routes
+ON myway_observations.myway_route=myway_routes.myway_route 
  WHERE observation_id NOT IN
 (
 SELECT  observation_id
@@ -50,18 +51,27 @@
     //var_dump($obsv);
     echo "<h3>Observation {$obsv['observation_id']}:</h1>
 <small>{$obsv['myway_stop']} @ {$obsv['time']} on {$obsv['myway_route']}</small><br>";
-    if ($obsv["stop_id"] == "") {
-        echo "error, stop '{$obsv['myway_stop']}' unknown";
-        continue;
-    }
     // convert timestamp into time of day and date
 // timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html
     $time = date("H:i:s", strtotime($obsv['time']));
     $time_tz = date("H:i:s", strtotime($obsv['time'])) . " AESST";
-    $search_time = date("H:i:s", strtotime($obsv['time']) - (30 * 60)); // 30 minutes margin
+    $search_time = date("H:i:s", strtotime($obsv['time']) - (60 * 60)); // 30 minutes margin
     $date = date("c", strtotime($obsv['time']));
     $timing_period = service_period(strtotime($date));
+    
+    // little hack for public holidays nolonger active; weekdays and 900+ route numbers don't make sense
+    if ($timing_period == "weekday" && preg_match('/9../',$obsv["route_short_name"]))  {
+        echo "Potential public holiday detected, trying Sunday timetable.<br>";
+        
+        $timing_period = "sunday";
+    }
+    
+    if (isset($obsv["stop_id"]) && $obsv["stop_id"] != "" && $obsv["stop_id"] != "*") {
     $potentialStops = Array(getStop($obsv["stop_id"]));
+    } else {
+        echo "No stop_id recorded for this stop_name, potential stops are a bus station<br>";
+        $potentialStops = getStops("",  trim(str_replace(Array("Arrival","Arrivals","Arrive Platform 3 Set down only.","Arrive","Set Down Only"), "", $obsv["myway_stop"])));
+    }
     //:get myway_stops records
     //:search by starts with stopcode and starts with street if street is not null
     //no result, skip and display error
@@ -78,7 +88,7 @@
     //:get myway_route record
     //no result, skip and display error
     //print out route
-    $potentialRoutes = getRoutesByShortName(preg_replace("/[A-Z]/", "", $obsv["myway_route"]));
+    $potentialRoutes = getRoutesByShortName($obsv["route_short_name"]);
     if (sizeof($potentialRoutes) < 1) {
         echo "error, route '{$obsv["myway_route"]}' unknown";
         continue;
@@ -95,7 +105,7 @@
                     echo "Matching route {$stopRoute['route_id']} found at stop #{$potentialStop['stop_id']}<br>";
                     $foundRoute = $stopRoute;
                     //if does get tripstoptimes for this route
-                    $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time);
+                    $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time, 10, $potentialRoute['route_short_name']);
                     foreach ($trips as $trip) {
                         //echo $trip['route_id']." ".$stopRoute['route_id'].";";
                         if ($trip['route_id'] == $stopRoute['route_id']) {
@@ -108,13 +118,16 @@
                                 "timeDiff" => $timeDiff,
                                 "stop_id" => $potentialStop['stop_id'],
                                 "stop_sequence" => $trip['stop_sequence'],
-                                "route_name" => "{$potentialRoute['route_short_name']} {$potentialRoute['route_long_name']}",
+                                "route_name" => "{$trip['route_short_name']} {$trip['trip_headsign']}",
                                 "route_id" => $trip['route_id']
                             );
                             echo "Found trip {$trip['trip_id']} at stop {$potentialStop['stop_id']} (#{$potentialStop['stop_name']}, sequence #{$trip['stop_sequence']})<br>";
                             echo "Arriving at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
+                        } else {
+                            echo "{$trip['route_id']} != {$stopRoute['route_id']}<br>";
                         }
                     }
+                    if (sizeof($timeDeltas) == 0) echo "Error, no trips found.<bR>";
                     break; // because have found route
                 }
             }
@@ -162,6 +175,7 @@
         }
         var_dump($conn->errorInfo());
         flush();
+       
         }
     }
     flush();