Convert myway timeliness calculation to new GTFS feed format
Convert myway timeliness calculation to new GTFS feed format

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -57,7 +57,7 @@
 function isDebugServer() {
     
     return php_sapi_name() == "cli" || isset($_SERVER['SERVER_NAME']) && ( $_SERVER['SERVER_NAME'] == "azusa" || $_SERVER['SERVER_NAME'] == "vanille"
-            || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1");
+            || $_SERVER['SERVER_NAME'] == "localhost" || $_SERVER['SERVER_NAME'] == "127.0.0.1" ||  $_SERVER['SERVER_NAME'] == "192.168.1.8");
 }
 
 include_once ("common-geo.inc.php");

--- a/include/db/route-dao.inc.php
+++ b/include/db/route-dao.inc.php
@@ -29,7 +29,19 @@
     }
     return $query->fetch(PDO :: FETCH_ASSOC);
 }
-
+function getRoutesByShortName($routeShortName) {
+    global $conn;
+    $query = "Select * from routes where route_short_name = :routeShortName";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":routeShortName", $routeShortName);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    return $query->fetchAll();
+}
 function getRouteByFullName($routeFullName) {
     global $conn;
     $query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1";

--- a/include/db/stop-dao.inc.php
+++ b/include/db/stop-dao.inc.php
@@ -139,9 +139,9 @@
 }
 
 function getStopRoutes($stopID, $service_period) {
-    if ($service_period == "")
+    if ($service_period == "") {
         $service_period = service_period();
-    
+    }
     $service_ids = service_ids($service_period);
     $sidA = $service_ids[0];
     $sidB = $service_ids[1];
@@ -164,9 +164,11 @@
 }
 
 function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "") {
-    if ($service_period == "")
+    if ($service_period == "") {
         $service_period = service_period();
+    }
         $service_ids = service_ids($service_period);
+
     $sidA = $service_ids[0];
     $sidB = $service_ids[1];
     if ($limit != "")

--- a/myway/myway_timeliness_calculate.php
+++ b/myway/myway_timeliness_calculate.php
@@ -29,8 +29,7 @@
 
 //collect all observation not in delta
 $query = "select * from myway_observations INNER JOIN myway_stops
-ON myway_observations.myway_stop=myway_stops.myway_stop INNER JOIN myway_routes
-ON myway_observations.myway_route=myway_routes.myway_route
+ON myway_observations.myway_stop=myway_stops.myway_stop 
  WHERE observation_id NOT IN
 (
 SELECT  observation_id
@@ -51,12 +50,8 @@
     //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_code"] == "") {
+    if ($obsv["stop_id"] == "") {
         echo "error, stop '{$obsv['myway_stop']}' unknown";
-        continue;
-    }
-    if ($obsv["route_full_name"] == "") {
-        echo "error, route '{$obsv['myway_route']}' unknown";
         continue;
     }
     // convert timestamp into time of day and date
@@ -66,80 +61,86 @@
     $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));
-    $potentialStops = getStopsByStopCode($obsv["stop_code"], $obsv["stop_street"]);
+    $potentialStops = Array(getStop($obsv["stop_id"]));
     //:get myway_stops records
     //:search by starts with stopcode and starts with street if street is not null
     //no result, skip and display error
     if (sizeof($potentialStops) < 1) {
-        echo "error, potential stops for stopcode {$obsv["stop_code"]} street {$obsv["stop_street"]} unknown";
+        echo "error, potential stops for stopid {$obsv["stop_id"]} unknown";
         continue;
     }
     //print out stops
     echo "Matched stops: ";
-    foreach ($potentialStops as $potentialStop)
-        echo $potentialStop['stop_code'] . " ";
+    foreach ($potentialStops as $potentialStop) {
+        echo $potentialStop['stop_id'] . " " . $potentialStop['stop_name'] . " ";
+    }
     echo "<br>";
     //:get myway_route record
     //no result, skip and display error
     //print out route
-    $potentialRoute = getRouteByFullName($obsv["route_full_name"]);
-    if ($potentialRoute["route_short_name"] == "") {
-        echo "error, route '{$obsv["route_full_name"]}' unknown";
+    $potentialRoutes = getRoutesByShortName(preg_replace("/[A-Z]/", "", $obsv["myway_route"]));
+    if (sizeof($potentialRoutes) < 1) {
+        echo "error, route '{$obsv["myway_route"]}' unknown";
         continue;
     }
-    echo "Matched route: {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>";
     $timeDeltas = Array();
-    foreach ($potentialStops as $potentialStop) {
-        $stopRoutes = getStopRoutes($potentialStop['stop_id'], $timing_period);
-        $foundRoute = Array();
-        foreach ($stopRoutes as $stopRoute) {
-            //Check if this route stops at each stop
-            if ($stopRoute['route_short_name'] . $stopRoute['route_long_name'] == $obsv["route_full_name"]) {
-                echo "Matching route {$stopRoute['route_id']} found at {$potentialStop['stop_code']}<br>";
-                $foundRoute = $stopRoute;
-                //if does get tripstoptimes for this route
-                $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time);
-                foreach ($trips as $trip) {
-                    //echo $trip['route_id']." ".$stopRoute['route_id'].";";
-                    if ($trip['route_id'] == $stopRoute['route_id']) {
-                        $timedTrip = getTimeInterpolatedTripAtStop($trip['trip_id'], $trip['stop_sequence']);
-                        $actual_time = strtotime($time);
-                        $trip_time = strtotime($timedTrip['arrival_time']);
-                        $timeDiff = $actual_time - $trip_time;
-                        //work out time delta, put into array with index of delta
-                        $timeDeltas[] = Array(
-                            "timeDiff" => $timeDiff,
-                            "stop_code" => $potentialStop['stop_code'],
-                            "stop_sequence" => $timedTrip['stop_sequence']
-                        );
-                        echo "Found trip {$trip['trip_id']} at stop {$potentialStop['stop_code']} (#{$potentialStop['stop_id']}, sequence #{$trip['stop_sequence']})<br>";
-                        echo "Arriving at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
+    foreach ($potentialRoutes as $potentialRoute) {
+        echo "Matched route: {$potentialRoute['route_id']} {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>";
+        foreach ($potentialStops as $potentialStop) {
+            $stopRoutes = getStopRoutes($potentialStop['stop_id'], $timing_period);
+            $foundRoute = Array();
+            foreach ($stopRoutes as $stopRoute) {
+                //Check if this route stops at each stop
+                if ($stopRoute['route_id'] == $potentialRoute['route_id']) {
+                    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);
+                    foreach ($trips as $trip) {
+                        //echo $trip['route_id']." ".$stopRoute['route_id'].";";
+                        if ($trip['route_id'] == $stopRoute['route_id']) {
+                            $timedTrip = getTripAtStop($trip['trip_id'], $trip['stop_sequence']);
+                            $actual_time = strtotime($time);
+                            $trip_time = strtotime($timedTrip['arrival_time']);
+                            $timeDiff = $actual_time - $trip_time;
+                            //work out time delta, put into array with index of delta
+                            $timeDeltas[] = Array(
+                                "timeDiff" => $timeDiff,
+                                "stop_id" => $potentialStop['stop_id'],
+                                "stop_sequence" => $trip['stop_sequence'],
+                                "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>";
+                        }
                     }
+                    break; // because have found route
                 }
-                break; // because have found route
+            }
+            if (sizeof($foundRoute) < 1) {
+                //print out that stops/does not stop
+                echo "No matching routes found at {$potentialStop['stop_id']}<br>";
+                //var_dump($stopRoutes);
+                flush();
             }
         }
-        if (sizeof($foundRoute) < 1) {
-            //print out that stops/does not stop
-            echo "No matching routes found at {$potentialStop['stop_code']}<br>";
-            var_dump($stopRoutes);
-            flush();
-        }
     }
+    
     //   lowest delta is recorded delta
     usort($timeDeltas, "abssort");
     $lowestDelta = $timeDeltas[0]["timeDiff"];
     if (sizeof($timeDeltas) != 0) {
         echo "Lowest difference of " . round($lowestDelta / 60, 2) . " minutes will be recorded for this observation<br>";
+        
         $observation_id = $obsv['observation_id'];
-        $route_full_name = $obsv['route_full_name'];
-        $stop_code = $timeDeltas[0]["stop_code"];
+        $route_id = $timeDeltas[0]["route_id"];
+        $stop_id = $timeDeltas[0]["stop_id"];
         $stop_sequence = $timeDeltas[0]["stop_sequence"];
-        $stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_full_name, stop_code, timing_delta, time, date, timing_period, stop_sequence)
-				      values (:observation_id, :route_full_name, :stop_code, :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)
+				      values (:observation_id, :route_id, :stop_id, :timing_delta, :time, :date, :timing_period, :stop_sequence)");
         $stmt->bindParam(':observation_id', $observation_id);
-        $stmt->bindParam(':route_full_name', $route_full_name);
-        $stmt->bindParam(':stop_code', $stop_code);
+        $stmt->bindParam(':route_id', $route_id);
+        $stmt->bindParam(':stop_id', $stop_id);
         $stmt->bindParam(':timing_delta', $lowestDelta);
         $stmt->bindParam(':time', $time_tz);
         $stmt->bindParam(':date', $date);

--- a/myway/myway_timeliness_reconcile.php
+++ b/myway/myway_timeliness_reconcile.php
@@ -19,25 +19,23 @@
 foreach ($_REQUEST as $key => $value) {
     if (strstr($key, "route") && !strstr($value, "Select")) {
         $myway_route = str_replace("route", "", $key);
-        $route_full_name = $value;
-        $query = "update myway_routes set route_full_name = :route_full_name where myway_route = :myway_route";
+        $route_id = $value;
+        $query = "update myway_routes set route_id = :route_id where myway_route = :myway_route";
         debug($query, "database");
         $query = $conn->prepare($query);
         $query->bindParam(":myway_route", $myway_route, PDO::PARAM_STR, 5);
-        $query->bindParam(":route_full_name", $route_full_name, PDO::PARAM_STR, 42);
+        $query->bindParam(":route_id", $route_id, PDO::PARAM_STR, 42);
         $query->execute();
         die(print_r($conn->errorInfo(), true));
     }
     if (strstr($key, "myway_stop")) {
         $myway_stop = $value;
-        $stop_code = $_REQUEST['stop_code'];
-        $stop_street = $_REQUEST['stop_street'];
-        $query = "update myway_stops set stop_code = :stop_code, stop_street = :stop_street where myway_stop = :myway_stop";
+        $stop_id = $_REQUEST['stop_id'];
+        $query = "update myway_stops set stop_id = :stop_id where myway_stop = :myway_stop";
         debug($query, "database");
         $query = $conn->prepare($query);
         $query->bindParam(":myway_stop", $myway_stop, PDO::PARAM_STR, 25);
-        $query->bindParam(":stop_code", $stop_code, PDO::PARAM_STR, 32);
-        $query->bindParam(":stop_street", $stop_street);
+        $query->bindParam(":stop_id", $stop_id, PDO::PARAM_STR, 32);
         $query->execute();
         die(print_r($conn->errorInfo(), true));
     }
@@ -68,7 +66,7 @@
 echo "<h2>Stops</h2>";
 /* stops
   search start of name, display map and table nuimbered, two text boxes */
-$query = "Select * from myway_stops where stop_code is NULL and stop_street is NUll;";
+$query = "Select * from myway_stops where stop_id is NUll;";
 debug($query, "database");
 $query = $conn->prepare($query);
 $query->execute();
@@ -78,10 +76,9 @@
 }
 foreach ($query->fetchAll() as $myway_stop) {
     echo "<h3>{$myway_stop[0]}</h3>";
-    $stopNameParts = explode(" ", $myway_stop[0]);
     $markers = array();
     $stopKey = 0;
-    $foundStops = getStops(false, "", $stopNameParts[0] . " " . $stopNameParts[1]);
+    $foundStops = getStops("",$myway_stop[0]);
     if (sizeof($foundStops) > 0) {
         echo "<table>";
         foreach ($foundStops as $stopResult) {
@@ -89,7 +86,7 @@
                 $stopResult['stop_lat'],
                 $stopResult['stop_lon']
             );
-            echo "<tr><td>" . $stopKey++ . "</td><td>" . $stopResult['stop_name'] . "</td><td>" . $stopResult['stop_code'] . "</td></tr>";
+            echo "<tr><td>" . $stopKey++ . "</td><td>" . $stopResult['stop_name'] . "</td><td>" . $stopResult['stop_id'] . "</td></tr>";
         }
         echo '</table>';
         echo "" . staticmap($markers,false,false,false,true) . "<br>\n";
@@ -97,50 +94,10 @@
     echo '<form id="inputform' . md5($myway_stop[0]) . '">
         <input type="hidden" name="myway_stop" value="' . $myway_stop[0] . '">
         <div data-role="fieldcontain">
-        <label for="stop_code">Stop Code</label>
-        <input type="text" name="stop_code" id="stop_code" value="' . $foundStops[0]['stop_code'] . '"  />
-    </div>
-        <div data-role="fieldcontain">
-        <label for="stop_street">Stop Street </label>
-        <input type="text" name="stop_street" id="stop_street" value="' . $foundStops[0]['stop_name'] . '"  />
+        <label for="stop_id">Stop ID</label>
+        <input type="text" name="stop_id" id="stop_id" value="' . $foundStops[0]['stop_id'] . '"  />
     </div>         <input type="button" onclick="$.post(\'myway_timeliness_reconcile.php\', $(\'#inputform' . md5($myway_stop[0]) . '\').serialize())" value="Go!"></form>
 ';
-    echo '<hr>';
-}
-echo '<h2>Routes</h2>';
-/* routes
-  remove alpha char, search present dropdown */
-$query = "Select * from myway_routes where route_full_name is NUll;";
-debug($query, "database");
-$query = $conn->prepare($query);
-$query->execute();
-if (!$query) {
-    databaseError($conn->errorInfo());
-    return Array();
-}
-foreach ($query->fetchAll() as $myway_route) {
-    echo "<h3>{$myway_route[0]}</h3>";
-    $query = "Select * from myway_observations where myway_route = :route order by time";
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    $query->bindParam(":route", $myway_route[0]);
-    $query->execute();
-    if (!$query) {
-        databaseError($conn->errorInfo());
-        return Array();
-    }
-    foreach ($query->fetchAll() as $myway_obvs) {
-        echo $myway_obvs['myway_stop'] . $myway_obvs['time'] . "<br>";
-    }
-    $searchRouteNo = preg_replace("/[A-Z]/", "", $myway_route[0]);
-    echo $searchRouteNo;
-    echo '<form id="inputform' . $myway_route[0] . '">
-<select name="route' . $myway_route[0] . '" onchange=\'$.post("myway_timeliness_reconcile.php", $("#inputform' . $myway_route[0] . '").serialize())\'>
-<option>Select a from/to pair...</option>';
-    foreach (getRoutesByNumber($searchRouteNo) as $routeResult) {
-        echo "<option value=\"{$routeResult['route_short_name']}{$routeResult['route_long_name']}\"> {$routeResult['route_short_name']}{$routeResult['route_long_name']} </option>\n";
-    }
-    echo "</select></form>";
     echo '<hr>';
 }
 include_footer();