MyWay timeliness by stop/route
MyWay timeliness by stop/route

<?php <?php
include ('../include/common.inc.php'); include ('../include/common.inc.php');
include_header("Busness R&amp;D", "index") include_header("Busness R&amp;D", "index")
?> ?>
<ul data-role="listview" data-theme="e" data-groupingtheme="e"> <ul data-role="listview" data-theme="e" data-groupingtheme="e">
<li data-role="list-divider" > Experimental Features </li> <li data-role="list-divider" > Experimental Features </li>
<li><a href="mywaybalance.php"><h3>MyWay Balance for mobile</h3> <li><a href="mywaybalance.php"><h3>MyWay Balance for mobile</h3>
<p>Mobile viewer for MyWay balance. Warning! No HTTPS security.</p></a></li> <p>Mobile viewer for MyWay balance. Warning! No HTTPS security.</p></a></li>
<li><a href="busstopdensity.php"><h3>Bus Stop Density Map</h3> <li><a href="busstopdensity.php"><h3>Bus Stop Density Map</h3>
<p>Analysis of bus stop coverage</p></a></li> <p>Analysis of bus stop coverage</p></a></li>
<li><a href="stopBrowser.php"><h3>Bus Stop Browser Map</h3> <li><a href="stopBrowser.php"><h3>Bus Stop Browser Map</h3>
<p>Bus stop location/route browser</p></a></li> <p>Bus stop location/route browser</p></a></li>
</ul> </ul>
<ul data-role="listview" data-theme="e" data-groupingtheme="e"> <ul data-role="listview" data-theme="e" data-groupingtheme="e">
   
<li data-role="list-divider" > MyWay Timeliness Graphs </li> <li data-role="list-divider" > MyWay Timeliness Graphs </li>
<li><a href="myway_timeliness.php"><h3>Timeliness over Day</h3> <li><a href="myway_timeliness.php"><h3>Timeliness over Day</h3>
<p>Displays the deviation from the timetable over the day</p></a></li> <p>Displays the deviation from the timetable over the day</p></a></li>
<li><a href="myway_timeliness_freqdist.php"><h3>Frequency Distribution of Time Deviation</h3> <li><a href="myway_timeliness_freqdist.php"><h3>Frequency Distribution of Time Deviation</h3>
<p>Displays spread of time deviations</p></a></li> <p>Displays spread of time deviations</p></a></li>
<!--<li><a href="myway_timeliness_route.php"><h3>Timeliness over Route</h3> <li><a href="myway_timeliness_route.php"><h3>Timeliness over Route</h3>
<p>Displays the deviation from timetable as a specific route progresses</p></a></li> <p>Displays the deviation from timetable as a specific route progresses</p></a></li>
<li><a href="myway_timeliness_stop.php"><h3>Timeliness at Stop</h3> <li><a href="myway_timeliness_stop.php"><h3>Timeliness at Stop</h3>
<p>Displays the deviation from the timetable at a specific stop</p></a></li> <p>Displays the deviation from the timetable at a specific stop</p></a></li>
--></ul> </ul>
</div> </div>
<?php <?php
include_footer() include_footer()
?> ?>
   
<?php <?php
include ('../include/common.inc.php'); include ('../include/common.inc.php');
include_header("MyWay Delta Calculate", "mywayDeltaCalc"); include_header("MyWay Delta Calculate", "mywayDeltaCalc");
function abssort($a, $b) function abssort($a, $b)
{ {
if ($a['timeDiff'] == $b['timeDiff']) { if ($a['timeDiff'] == $b['timeDiff']) {
return 0; return 0;
} }
return (abs($a['timeDiff']) < abs($b['timeDiff'])) ? -1 : 1; return (abs($a['timeDiff']) < abs($b['timeDiff'])) ? -1 : 1;
} }
//collect all observation not in delta //collect all observation not in delta
$query = "select * from myway_observations INNER JOIN myway_stops $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_stop=myway_stops.myway_stop INNER JOIN myway_routes
ON myway_observations.myway_route=myway_routes.myway_route ON myway_observations.myway_route=myway_routes.myway_route
WHERE observation_id NOT IN WHERE observation_id NOT IN
( (
SELECT observation_id SELECT observation_id
FROM myway_timingdeltas FROM myway_timingdeltas
)"; )";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
$uncalcdObservations = $query->fetchAll(); $uncalcdObservations = $query->fetchAll();
//Display count //Display count
echo "<h3>" . sizeof($uncalcdObservations) . " observations not yet processed</h2>"; echo "<h3>" . sizeof($uncalcdObservations) . " observations not yet processed</h2>";
//foreach observation not in delta //foreach observation not in delta
foreach ($uncalcdObservations as $obsv) { foreach ($uncalcdObservations as $obsv) {
//var_dump($obsv); //var_dump($obsv);
echo "<h3>Observation {$obsv['observation_id']}:</h1> echo "<h3>Observation {$obsv['observation_id']}:</h1>
<small>{$obsv['myway_stop']} @ {$obsv['time']} on {$obsv['myway_route']}</small><br>"; <small>{$obsv['myway_stop']} @ {$obsv['time']} on {$obsv['myway_route']}</small><br>";
if ($obsv["stop_code"] == "") { if ($obsv["stop_code"] == "") {
echo "error, stop '{$obsv['myway_stop']}' unknown"; echo "error, stop '{$obsv['myway_stop']}' unknown";
continue; continue;
} }
if ($obsv["route_full_name"] == "") { if ($obsv["route_full_name"] == "") {
echo "error, route '{$obsv['myway_route']}' unknown"; echo "error, route '{$obsv['myway_route']}' unknown";
continue; continue;
} }
// :convert timestamp into time of day and date // :convert timestamp into time of day and date
$time = date("H:i:s", strtotime($obsv['time'])); $time = date("H:i:s", strtotime($obsv['time']));
$search_time = date("H:i:s", strtotime($obsv['time'])-(30*60)); // 30 minutes margin $search_time = date("H:i:s", strtotime($obsv['time'])-(30*60)); // 30 minutes margin
$date = date("c", strtotime($obsv['time'])); $date = date("c", strtotime($obsv['time']));
$timing_period = service_period(strtotime($date)); $timing_period = service_period(strtotime($date));
$potentialStops = getStopsByStopCode($obsv["stop_code"], $obsv["stop_street"]); $potentialStops = getStopsByStopCode($obsv["stop_code"], $obsv["stop_street"]);
//:get myway_stops records //:get myway_stops records
//:search by starts with stopcode and starts with street if street is not null //:search by starts with stopcode and starts with street if street is not null
//no result, skip and display error //no result, skip and display error
if (sizeof($potentialStops) < 1) { if (sizeof($potentialStops) < 1) {
echo "error, potential stops for stopcode {$obsv["stop_code"]} street {$obsv["stop_street"]} unknown"; echo "error, potential stops for stopcode {$obsv["stop_code"]} street {$obsv["stop_street"]} unknown";
continue; continue;
} }
//print out stops //print out stops
echo "Matched stops: "; echo "Matched stops: ";
foreach ($potentialStops as $potentialStop) echo $potentialStop['stop_code'] . " "; foreach ($potentialStops as $potentialStop) echo $potentialStop['stop_code'] . " ";
echo "<br>"; echo "<br>";
//:get myway_route record //:get myway_route record
//no result, skip and display error //no result, skip and display error
//print out route //print out route
$potentialRoute = getRouteByFullName($obsv["route_full_name"]); $potentialRoute = getRouteByFullName($obsv["route_full_name"]);
if ($potentialRoute["route_short_name"] == "") { if ($potentialRoute["route_short_name"] == "") {
echo "error, route '{$obsv["route_full_name"]}' unknown"; echo "error, route '{$obsv["route_full_name"]}' unknown";
continue; continue;
} }
echo "Matched route: {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>"; echo "Matched route: {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>";
$timeDeltas = Array(); $timeDeltas = Array();
foreach ($potentialStops as $potentialStop) { foreach ($potentialStops as $potentialStop) {
$stopRoutes = getStopRoutes($potentialStop['stop_id'], $timing_period); $stopRoutes = getStopRoutes($potentialStop['stop_id'], $timing_period);
$foundRoute = Array(); $foundRoute = Array();
foreach ($stopRoutes as $stopRoute) { foreach ($stopRoutes as $stopRoute) {
//Check if this route stops at each stop //Check if this route stops at each stop
if ($stopRoute['route_short_name'] . $stopRoute['route_long_name'] == $obsv["route_full_name"]) { 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>"; echo "Matching route {$stopRoute['route_id']} found at {$potentialStop['stop_code']}<br>";
$foundRoute = $stopRoute; $foundRoute = $stopRoute;
//if does get tripstoptimes for this route //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);
foreach ($trips as $trip) { foreach ($trips as $trip) {
//echo $trip['route_id']." ".$stopRoute['route_id'].";"; //echo $trip['route_id']." ".$stopRoute['route_id'].";";
if ($trip['route_id'] == $stopRoute['route_id']) { if ($trip['route_id'] == $stopRoute['route_id']) {
$timedTrip = getTimeInterpolatedTripAtStop($trip['trip_id'], $trip['stop_sequence']); $timedTrip = getTimeInterpolatedTripAtStop($trip['trip_id'], $trip['stop_sequence']);
$actual_time = strtotime($time); $actual_time = strtotime($time);
$trip_time = strtotime($timedTrip['arrival_time']); $trip_time = strtotime($timedTrip['arrival_time']);
$timeDiff = $actual_time - $trip_time; $timeDiff = $actual_time - $trip_time;
//work out time delta, put into array with index of delta //work out time delta, put into array with index of delta
$timeDeltas[] = Array( $timeDeltas[] = Array(
"timeDiff" => $timeDiff, "timeDiff" => $timeDiff,
"stop_code" => $potentialStop['stop_code'] "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 "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>"; 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) { if (sizeof($foundRoute) < 1) {
//print out that stops/does not stop //print out that stops/does not stop
echo "No matching routes found at {$potentialStop['stop_code']}<br>"; echo "No matching routes found at {$potentialStop['stop_code']}<br>";
var_dump($stopRoutes); var_dump($stopRoutes);
} }
} }
// lowest delta is recorded delta // lowest delta is recorded delta
usort($timeDeltas, "abssort"); usort($timeDeltas, "abssort");
$lowestDelta = $timeDeltas[0]["timeDiff"]; $lowestDelta = $timeDeltas[0]["timeDiff"];
if (sizeof($timeDeltas) != 0) { if (sizeof($timeDeltas) != 0) {
echo "Lowest difference of " . round($lowestDelta / 60, 2) . " minutes will be recorded for this observation<br>"; echo "Lowest difference of " . round($lowestDelta / 60, 2) . " minutes will be recorded for this observation<br>";
$observation_id = $obsv['observation_id']; $observation_id = $obsv['observation_id'];
$route_full_name = $obsv['route_full_name']; $route_full_name = $obsv['route_full_name'];
$myway_route = $obsv['myway_stop'];  
$stop_code = $timeDeltas[0]["stop_code"]; $stop_code = $timeDeltas[0]["stop_code"];
$stmt = $conn->prepare("insert into myway_timingdeltas (observation_id, route_full_name, myway_route, stop_code, timing_delta, time, date, timing_period) $stop_sequence = $timeDeltas[0]["stop_sequence"];
values (:observation_id, :route_full_name, :myway_route, :stop_code, :timing_delta, :time, :date, :timing_period)"); $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->bindParam(':observation_id', $observation_id); $stmt->bindParam(':observation_id', $observation_id);
$stmt->bindParam(':route_full_name', $route_full_name); $stmt->bindParam(':route_full_name', $route_full_name);
$stmt->bindParam(':myway_route', $myway_route);  
$stmt->bindParam(':stop_code', $stop_code); $stmt->bindParam(':stop_code', $stop_code);
$stmt->bindParam(':timing_delta', $lowestDelta); $stmt->bindParam(':timing_delta', $lowestDelta);
$stmt->bindParam(':time', $time); $stmt->bindParam(':time', $time);
$stmt->bindParam(':date', $date); $stmt->bindParam(':date', $date);
$stmt->bindParam(':timing_period', $timing_period); $stmt->bindParam(':timing_period', $timing_period);
  $stmt->bindParam(':stop_sequence', $stop_sequence);
// insert a record // insert a record
$stmt->execute(); $stmt->execute();
if ($stmt->rowCount() > 0) { if ($stmt->rowCount() > 0) {
echo "Recorded.<br>"; echo "Recorded.<br>";
} }
var_dump($conn->errorInfo()); var_dump($conn->errorInfo());
} }
flush(); flush();
} }
   
  <?php
  include ('../include/common.inc.php');
  header('Content-Type: text/javascript; charset=utf8');
  // header('Access-Control-Allow-Origin: http://bus.lambdacomplex.org/');
  header('Access-Control-Max-Age: 3628800');
  header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
  ?>
  {
  "label": "<?php echo $_REQUEST['routeid']; ?>",
  "data": <?php
  $query = "select * from myway_timingdeltas where route_full_name = :route_full_name AND abs(timing_delta) < 2*(select stddev(timing_delta) from myway_timingdeltas) order by stop_sequence;";
  $query = $conn->prepare($query);
  $query->bindParam(':route_full_name', $_REQUEST['routeid'],PDO::PARAM_STR, 42);
 
  $query->execute();
  if (!$query) {
  databaseError($conn->errorInfo());
  return Array();
  }
  foreach ($query->fetchAll() as $delta) {
  $points[] = "[{$delta['stop_sequence']}, {$delta['timing_delta']}]";
  };
  echo "[".implode(",",$points)."]";
  ?>
  }
  <?php
  include ('../include/common.inc.php');
  include_header("MyWay Deltas", "mywayDelta");
  ?>
 
  <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../js/flot/excanvas.min.js"></script><![endif]-->
 
  <script language="javascript" type="text/javascript" src="../js/flot/jquery.flot.js"></script>
  <form method="get" action="">
  <select id="routeid" name="routeid">
  <?php
  $query = "select distinct route_full_name from myway_routes where myway_route != '' order by route_full_name";
  $query = $conn->prepare($query);
  $query->execute();
  if (!$query) {
  databaseError($conn->errorInfo());
  return Array();
  }
  foreach ($query->fetchAll() as $route) {
  echo "<option value=\"{$route['route_full_name']}\">{$route['route_full_name']}</option>";
 
  };
  ?> </select>
  <center><div id="placeholder" style="width:900px;height:550px"></div></center>
  <script type="text/javascript">
  $(function () {
 
  var placeholder = $("#placeholder");
  var data = [];
  var options = {
  xaxis: {
  },
  yaxis: {
  tickFormatter: yformatter
  },
  grid: { hoverable: true, clickable: true, labelMargin: 32 },
  };
 
  var plot = $.plot(placeholder, data, options);
 
  // fetch one series, adding to what we got
  var alreadyFetched = {};
 
  $("#routeid").change(function () {
  var select = $(this);
 
  // find the URL in the link right next to us
  // var dataurl = button.siblings('a').attr('href');
  var dataurl = "myway_timeliness_route.json.php?routeid=" + select.val();
  // then fetch the data with jQuery
  function onDataReceived(series) {
  // extract the first coordinate pair so you can see that
  // data is now an ordinary Javascript object
  var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')';
 
 
  // let's add it to our current data
  if (!alreadyFetched[series.label]) {
  alreadyFetched[series.label] = true;
  data.push(series);
  }
 
  // and plot all we got
  $.plot(placeholder, data, options);
  }
 
  $.ajax({
  url: dataurl,
  method: 'GET',
  dataType: 'json',
  success: onDataReceived
  });
  });
 
 
  });
 
 
 
  function yformatter(v) {
  if (Math.floor(v/60) < -9) return "";
  return Math.abs(Math.floor(v/60)) + " min " + (v == 0 ? "" : (v >0 ? "early":"late"))
  }
  function showTooltip(x, y, contents) {
  $('<div id="tooltip">' + contents + '</div>').css( {
  position: 'absolute',
  display: 'none',
  top: y + 5,
  left: x + 5,
  border: '1px solid #fdd',
  padding: '2px',
  'background-color': '#fee',
  opacity: 0.80
  }).appendTo("body").fadeIn(200);
  }
 
  var previousPoint = null;
  $("#placeholder").bind("plothover", function (event, pos, item) {
  $("#x").text(pos.x.toFixed(2));
  $("#y").text(pos.y.toFixed(2));
 
  if (item) {
  if (previousPoint != item.dataIndex) {
  previousPoint = item.dataIndex;
 
  $("#tooltip").remove();
  var x = item.datapoint[0],
  y = item.datapoint[1].toFixed(2);
 
  showTooltip(item.pageX, item.pageY,
  item.series.label + " at stop_sequence "+ x +" = " + Math.abs(new Number(y/60).toFixed(2))+" minutes "+(y >0 ? "early":"late"));
  }
  }
  else {
  $("#tooltip").remove();
  previousPoint = null;
  }
  });
 
  </script>
</
  <?php
  include ('../include/common.inc.php');
  header('Content-Type: text/javascript; charset=utf8');
  // header('Access-Control-Allow-Origin: http://bus.lambdacomplex.org/');
  header('Access-Control-Max-Age: 3628800');
  header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
  ?>
  {
  "label": "<?php echo $_REQUEST['stopid']; ?>",
  "data": <?php
  $query = "select * from myway_timingdeltas INNER JOIN myway_observations
  ON myway_observations.observation_id=myway_timingdeltas.observation_id
  where myway_stop = :myway_stop
  AND abs(timing_delta) < 2*(select stddev(timing_delta) from myway_timingdeltas)
  order by myway_timingdeltas.time;";
  $query = $conn->prepare($query);
  $query->bindParam(':myway_stop', $_REQUEST['stopid'],PDO::PARAM_STR, 42);