Public holiday consideration in myway timeliness
Public holiday consideration in myway timeliness

<?php <?php
   
/* /*
* Copyright 2010,2011 Alexander Sadleir * Copyright 2010,2011 Alexander Sadleir
   
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
   
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
   
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
include ('../include/common.inc.php'); include ('../include/common.inc.php');
include_header("MyWay Delta Calculate", "mywayDeltaCalc"); include_header("MyWay Delta Calculate", "mywayDeltaCalc");
flush(); flush();
ob_flush(); ob_flush();
   
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>";
// convert timestamp into time of day and date // convert timestamp into time of day and date
// timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html // timezones from http://www.postgresql.org/docs/8.0/static/datetime-keywords.html
$time = date("H:i:s", strtotime($obsv['time'])); $time = date("H:i:s", strtotime($obsv['time']));
$time_tz = date("H:i:s", strtotime($obsv['time'])) . " AESST"; $time_tz = date("H:i:s", strtotime($obsv['time'])) . " AESST";
$search_time = date("H:i:s", strtotime($obsv['time']) - (60 * 60)); // 30 minutes margin $search_time = date("H:i:s", strtotime($obsv['time']) - (60 * 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));
   
  // 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"] != "*") { if (isset($obsv["stop_id"]) && $obsv["stop_id"] != "" && $obsv["stop_id"] != "*") {
$potentialStops = Array(getStop($obsv["stop_id"])); $potentialStops = Array(getStop($obsv["stop_id"]));
} else { } else {
echo "Potential stops are a bus station<br>"; 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"]))); $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 //: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 stopid {$obsv["stop_id"]} unknown"; echo "error, potential stops for stopid {$obsv["stop_id"]} unknown";
continue; continue;
} }
//print out stops //print out stops
echo "Matched stops: "; echo "Matched stops: ";
foreach ($potentialStops as $potentialStop) { foreach ($potentialStops as $potentialStop) {
echo $potentialStop['stop_id'] . " " . $potentialStop['stop_name'] . " "; echo $potentialStop['stop_id'] . " " . $potentialStop['stop_name'] . " ";
} }
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
$potentialRoutes = getRoutesByShortName($obsv["route_short_name"]); $potentialRoutes = getRoutesByShortName($obsv["route_short_name"]);
if (sizeof($potentialRoutes) < 1) { if (sizeof($potentialRoutes) < 1) {
echo "error, route '{$obsv["myway_route"]}' unknown"; echo "error, route '{$obsv["myway_route"]}' unknown";
continue; continue;
} }
$timeDeltas = Array(); $timeDeltas = Array();
foreach ($potentialRoutes as $potentialRoute) { foreach ($potentialRoutes as $potentialRoute) {
echo "Matched route: {$potentialRoute['route_id']} {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>"; echo "Matched route: {$potentialRoute['route_id']} {$potentialRoute['route_short_name']}{$potentialRoute['route_long_name']} {$timing_period}<br>";
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_id'] == $potentialRoute['route_id']) { if ($stopRoute['route_id'] == $potentialRoute['route_id']) {
echo "Matching route {$stopRoute['route_id']} found at stop #{$potentialStop['stop_id']}<br>"; echo "Matching route {$stopRoute['route_id']} found at stop #{$potentialStop['stop_id']}<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, 10, $potentialRoute['route_short_name']); $trips = getStopTrips($potentialStop['stop_id'], $timing_period, $search_time, 10, $potentialRoute['route_short_name']);
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 = getTripAtStop($trip['trip_id'], $trip['stop_sequence']); $timedTrip = getTripAtStop($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_id" => $potentialStop['stop_id'], "stop_id" => $potentialStop['stop_id'],
"stop_sequence" => $trip['stop_sequence'], "stop_sequence" => $trip['stop_sequence'],
"route_name" => "{$trip['route_short_name']} {$trip['trip_headsign']}", "route_name" => "{$trip['route_short_name']} {$trip['trip_headsign']}",
"route_id" => $trip['route_id'] "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 "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>"; echo "Arriving at {$timedTrip['arrival_time']}, difference of " . round($timeDiff / 60, 2) . " minutes<br>";
} else { } else {
echo "{$trip['route_id']} != {$stopRoute['route_id']}<br>"; echo "{$trip['route_id']} != {$stopRoute['route_id']}<br>";
} }
} }
if (sizeof($timeDeltas) == 0) echo "Error, no trips found.<bR>"; if (sizeof($timeDeltas) == 0) echo "Error, no trips found.<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_id']}<br>"; echo "No matching routes found at {$potentialStop['stop_id']}<br>";
//var_dump($stopRoutes); //var_dump($stopRoutes);
flush(); flush();
} }
} }
} }
// 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) {
if (abs($lowestDelta) > 9999) { if (abs($lowestDelta) > 9999) {
echo "Difference of " . round($lowestDelta / 60, 2) . " minutes is too high. Will not record this observation<br>"; echo "Difference of " . round($lowestDelta / 60, 2) . " minutes is too high. Will not record this observation<br>";
} else { } else {
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_name = $timeDeltas[0]["route_name"]; $route_name = $timeDeltas[0]["route_name"];
$route_id = $timeDeltas[0]["route_id"]; $route_id = $timeDeltas[0]["route_id"];
$stop_id = $timeDeltas[0]["stop_id"]; $stop_id = $timeDeltas[0]["stop_id"];
$myway_stop = $obsv["myway_stop"]; $myway_stop = $obsv["myway_stop"];
$stop_sequence = $timeDeltas[0]["stop_sequence"]; $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,myway_stop,route_name) $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)"); 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(':observation_id', $observation_id);
$stmt->bindParam(':route_id', $route_id); $stmt->bindParam(':route_id', $route_id);
$stmt->bindParam(':route_name', $route_name); $stmt->bindParam(':route_name', $route_name);
$stmt->bindParam(':stop_id', $stop_id); $stmt->bindParam(':stop_id', $stop_id);
$stmt->bindParam(':myway_stop', $myway_stop); $stmt->bindParam(':myway_stop', $myway_stop);
$stmt->bindParam(':timing_delta', $lowestDelta); $stmt->bindParam(':timing_delta', $lowestDelta);
$stmt->bindParam(':time', $time_tz); $stmt->bindParam(':time', $time_tz);
$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); $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();
} }
} }
flush(); flush();
} }
   
file:a/stop.php -> file:b/stop.php
<?php <?php
   
/* /*
* Copyright 2010,2011 Alexander Sadleir * Copyright 2010,2011 Alexander Sadleir
   
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
   
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
   
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
include ('include/common.inc.php'); include ('include/common.inc.php');
if ($stopid) if ($stopid)
$stop = getStop($stopid); $stop = getStop($stopid);
/* if ($stopcode != "" && $stop[5] != $stopcode) { /* if ($stopcode != "" && $stop[5] != $stopcode) {
$url = $APIurl . "/json/stopcodesearch?q=" . $stopcode; $url = $APIurl . "/json/stopcodesearch?q=" . $stopcode;
$stopsearch = json_decode(getPage($url)); $stopsearch = json_decode(getPage($url));
$stopid = $stopsearch[0][0]; $stopid = $stopsearch[0][0];
$url = $APIurl . "/json/stop?stop_id=" . $stopid; $url = $APIurl . "/json/stop?stop_id=" . $stopid;
$stop = json_decode(getPage($url)); $stop = json_decode(getPage($url));
} }
if (!startsWith($stop[5], "Wj") && strpos($stop[1], "Platform") === false) { if (!startsWith($stop[5], "Wj") && strpos($stop[1], "Platform") === false) {
// expand out to all platforms // expand out to all platforms
   
} */ } */
   
$stops = Array(); $stops = Array();
$stopPositions = Array(); $stopPositions = Array();
$stopNames = Array(); $stopNames = Array();
$tripStopNumbers = Array(); $tripStopNumbers = Array();
$allStopsTrips = Array(); $allStopsTrips = Array();
$fetchedTripSequences = Array(); $fetchedTripSequences = Array();
$stopLinks = ""; $stopLinks = "";
if (isset($stopids)) { if (isset($stopids)) {
foreach ($stopids as $sub_stopid) { foreach ($stopids as $sub_stopid) {
$stops[] = getStop($sub_stopid); $stops[] = getStop($sub_stopid);
} }
$stop = $stops[0]; $stop = $stops[0];
$stopid = $stops[0]["stop_id"]; $stopid = $stops[0]["stop_id"];
$stopLinks.= "Individual stop pages: <br>"; $stopLinks.= "Individual stop pages: <br>";
foreach ($stops as $key => $sub_stop) { foreach ($stops as $key => $sub_stop) {
   
$stopNames[$key] = $sub_stop["stop_name"]; $stopNames[$key] = $sub_stop["stop_name"];
$stopLinks.= '<a href="stop.php?stopid=' . $sub_stop["stop_id"] . '&amp;stopcode=' . $sub_stop["stop_code"] . '">' . $sub_stop["stop_name"] . '</a> '; $stopLinks.= '<a href="stop.php?stopid=' . $sub_stop["stop_id"] . '&amp;stopcode=' . $sub_stop["stop_code"] . '">' . $sub_stop["stop_name"] . '</a> ';
   
$stopPositions[$key] = Array( $stopPositions[$key] = Array(
$sub_stop["stop_lat"], $sub_stop["stop_lat"],
$sub_stop["stop_lon"] $sub_stop["stop_lon"]
); );
$trips = getStopTrips($sub_stop["stop_id"]); $trips = getStopTrips($sub_stop["stop_id"]);
$tripSequence = ""; $tripSequence = "";
foreach ($trips as $trip) { foreach ($trips as $trip) {
$tripSequence.= "{$trip['trip_id']},"; $tripSequence.= "{$trip['trip_id']},";
$tripStopNumbers[$trip['trip_id']][] = $key; $tripStopNumbers[$trip['trip_id']][] = $key;
} }
if (!in_array($tripSequence, $fetchedTripSequences)) { if (!in_array($tripSequence, $fetchedTripSequences)) {
// only fetch new trip sequences // only fetch new trip sequences
$fetchedTripSequences[] = $tripSequence; $fetchedTripSequences[] = $tripSequence;
$trips = getStopTripsWithTimes($sub_stop["stop_id"]); $trips = getStopTripsWithTimes($sub_stop["stop_id"]);
foreach ($trips as $trip) { foreach ($trips as $trip) {
if (!isset($allStopsTrips[$trip["trip_id"]])) if (!isset($allStopsTrips[$trip["trip_id"]]))
$allStopsTrips[$trip["trip_id"]] = $trip; $allStopsTrips[$trip["trip_id"]] = $trip;
} }
} }
//else { //else {
// echo "skipped sequence $tripSequence"; // echo "skipped sequence $tripSequence";
//} //}
} }
} }
  if (sizeof($stops) > 0) {
$stopDescParts = explode("<br>",$stop['stop_desc']); $stopDescParts = explode("<br>",$stop['stop_desc']);
include_header(trim(str_replace("Street: ","",$stopDescParts[0])), "stop"); include_header(trim(str_replace("Street: ","",$stopDescParts[0])), "stop");
  } else {
  include_header($stop['stop_name']);
  }
/* $serviceAlerts = json_decode(getPage(curPageURL() . "/servicealerts_api.php?filter_class=stop&filter_id=".$stopid) , true); /* $serviceAlerts = json_decode(getPage(curPageURL() . "/servicealerts_api.php?filter_class=stop&filter_id=".$stopid) , true);
   
foreach($serviceAlerts['entities'] as $serviceAlert) { foreach($serviceAlerts['entities'] as $serviceAlert) {
echo '<div id="servicewarning">'.$serviceAlert['alert']['description']['translation'].'</div>'; echo '<div id="servicewarning">'.$serviceAlert['alert']['description']['translation'].'</div>';
} */ } */
   
echo '<span class="content-secondary">'; echo '<span class="content-secondary">';
echo $stopLinks; echo $stopLinks;
if (sizeof($stops) > 0) { if (sizeof($stops) > 0) {
trackEvent("View Stops", "View Combined Stops", $stop["stop_name"], $stop["stop_id"]); trackEvent("View Stops", "View Combined Stops", $stop["stop_name"], $stop["stop_id"]);
echo staticmap($stopPositions); echo staticmap($stopPositions);
} else { } else {
trackEvent("View Stops", "View Single Stop", $stop["stop_name"], $stop["stop_id"]); trackEvent("View Stops", "View Single Stop", $stop["stop_name"], $stop["stop_id"]);
echo staticmap(Array( echo staticmap(Array(
0 => Array( 0 => Array(
$stop["stop_lat"], $stop["stop_lat"],
$stop["stop_lon"] $stop["stop_lon"]
) )
)); ));
} }
   
timeSettings(); timeSettings();
   
echo '</span><span class="content-primary">'; echo '</span><span class="content-primary">';
echo ' <ul data-role="listview" data-inset="true">'; echo ' <ul data-role="listview" data-inset="true">';
if (sizeof($allStopsTrips) > 0) { if (sizeof($allStopsTrips) > 0) {
sktimesort($allStopsTrips, "arrival_time", true); sktimesort($allStopsTrips, "arrival_time", true);
$trips = $allStopsTrips; $trips = $allStopsTrips;
} else { } else {
$trips = getStopTripsWithTimes($stopid); $trips = getStopTripsWithTimes($stopid);
} }
   
echo "<div class='ui-header' style='overflow: visible; height: 2.5em'>"; echo "<div class='ui-header' style='overflow: visible; height: 2.5em'>";
// if we have too many trips, cut down to size. // if we have too many trips, cut down to size.
if (sizeof($trips) > 10) { if (sizeof($trips) > 10) {
$trips = array_splice($trips, 0,10); $trips = array_splice($trips, 0,10);
} }
// later/earlier button setup // later/earlier button setup
if (sizeof($trips) == 0) { if (sizeof($trips) == 0) {
$time = isset($_REQUEST['time']) ? strtotime($_REQUEST['time']) : time(); $time = isset($_REQUEST['time']) ? strtotime($_REQUEST['time']) : time();
$earlierTime = $time - (90 * 60); $earlierTime = $time - (90 * 60);
$laterTime = $time + (90 * 60); $laterTime = $time + (90 * 60);
} else { } else {