Convert myway timeliness calculation to new GTFS feed format
[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
@@ -50,10 +50,6 @@
     //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']));
@@ -61,7 +57,12 @@
     $search_time = date("H:i:s", strtotime($obsv['time']) - (30 * 60)); // 30 minutes margin
     $date = date("c", strtotime($obsv['time']));
     $timing_period = service_period(strtotime($date));
+    if (isset($obsv["stop_id"]) && $obsv["stop_id"] != "" ) {
     $potentialStops = Array(getStop($obsv["stop_id"]));
+    } else {
+        echo "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
@@ -108,6 +109,7 @@
                                 "timeDiff" => $timeDiff,
                                 "stop_id" => $potentialStop['stop_id'],
                                 "stop_sequence" => $trip['stop_sequence'],
+                                "route_name" => "{$potentialRoute['route_short_name']} {$potentialRoute['route_long_name']} {$trip['direction']}",
                                 "route_id" => $trip['route_id']
                             );
                             echo "Found trip {$trip['trip_id']} at stop {$potentialStop['stop_id']} (#{$potentialStop['stop_name']}, sequence #{$trip['stop_sequence']})<br>";
@@ -130,17 +132,25 @@
     usort($timeDeltas, "abssort");
     $lowestDelta = $timeDeltas[0]["timeDiff"];
     if (sizeof($timeDeltas) != 0) {
+        if (abs($lowestDelta) > 9999) {
+             echo "Difference of " . round($lowestDelta / 60, 2) . " minutes is too high. Will not record this observation<br>";
+        } else {
         echo "Lowest difference of " . round($lowestDelta / 60, 2) . " minutes will be recorded for this observation<br>";
         
         $observation_id = $obsv['observation_id'];
+        
+        $route_name = $timeDeltas[0]["route_name"];
         $route_id = $timeDeltas[0]["route_id"];
         $stop_id = $timeDeltas[0]["stop_id"];
+        $myway_stop = $obsv["myway_stop"];
         $stop_sequence = $timeDeltas[0]["stop_sequence"];
-        $stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_id, stop_id, timing_delta, time, date, timing_period, stop_sequence)
-				      values (:observation_id, :route_id, :stop_id, :timing_delta, :time, :date, :timing_period, :stop_sequence)");
+        $stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_id, stop_id, timing_delta, time, date, timing_period, stop_sequence,myway_stop,route_name)
+				      values (:observation_id, :route_id, :stop_id, :timing_delta, :time, :date, :timing_period, :stop_sequence,:myway_stop,:route_name)");
         $stmt->bindParam(':observation_id', $observation_id);
         $stmt->bindParam(':route_id', $route_id);
+        $stmt->bindParam(':route_name', $route_name);
         $stmt->bindParam(':stop_id', $stop_id);
+        $stmt->bindParam(':myway_stop', $myway_stop);
         $stmt->bindParam(':timing_delta', $lowestDelta);
         $stmt->bindParam(':time', $time_tz);
         $stmt->bindParam(':date', $date);
@@ -153,6 +163,7 @@
         }
         var_dump($conn->errorInfo());
         flush();
+        }
     }
     flush();
 }