Improve myway timeliness based on new GTFS feed accuracy
Improve myway timeliness based on new GTFS feed accuracy

<?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.
*/ */
$service_periods = Array( $service_periods = Array(
'sunday', 'sunday',
'saturday', 'saturday',
'weekday' 'weekday'
); );
   
function service_period($date = "") { function service_period($date = "") {
   
if (isset($_SESSION['service_period'])) if (isset($_SESSION['service_period']))
return $_SESSION['service_period']; return $_SESSION['service_period'];
$override = getServiceOverride($date); $override = getServiceOverride($date);
if ($override['service_id']) { if ($override['service_id']) {
return $override['service_id']; $idParts = explode("-",$override['service_id']);
  return strtolower($idParts[2]);
} }
   
switch (date('w', ($date != "" ? $date : time()))) { switch (date('w', ($date != "" ? $date : time()))) {
case 0: case 0:
return 'sunday'; return 'sunday';
case 6: case 6:
return 'saturday'; return 'saturday';
default: default:
return 'weekday'; return 'weekday';
} }
} }
function service_ids($service_period) { function service_ids($service_period) {
switch ($service_period) { switch ($service_period) {
case 'sunday': case 'sunday':
return Array("2010-TUGGSUN-Sunday-20","2010-BELCSUN-Sunday-19"); return Array("2010-TUGGSUN-Sunday-20","2010-BELCSUN-Sunday-19");
case 'saturday': case 'saturday':
return Array("2010-BELCSAT-Saturday-19","2010-TUGGSAT-Saturday-19"); return Array("2010-BELCSAT-Saturday-19","2010-TUGGSAT-Saturday-19");
default: default:
//return 'weekday'; //return 'weekday';
return Array("2010-BELCMAST-Weekday-15","2010-TUGGMAST-Weekday-14"); return Array("2010-BELCMAST-Weekday-15","2010-TUGGMAST-Weekday-14");
} }
} }
   
function midnight_seconds($time = "") { function midnight_seconds($time = "") {
// from http://www.perturb.org/display/Perlfunc__Seconds_Since_Midnight.html // from http://www.perturb.org/display/Perlfunc__Seconds_Since_Midnight.html
if ($time != "") { if ($time != "") {
return (date("G", $time) * 3600) + (date("i", $time) * 60) + date("s", $time); return (date("G", $time) * 3600) + (date("i", $time) * 60) + date("s", $time);
} }
if (isset($_SESSION['time'])) { if (isset($_SESSION['time'])) {
$time = strtotime($_SESSION['time']); $time = strtotime($_SESSION['time']);
return (date("G", $time) * 3600) + (date("i", $time) * 60) + date("s", $time); return (date("G", $time) * 3600) + (date("i", $time) * 60) + date("s", $time);
} }
return (date("G") * 3600) + (date("i") * 60) + date("s"); return (date("G") * 3600) + (date("i") * 60) + date("s");
} }
   
function midnight_seconds_to_time($seconds) { function midnight_seconds_to_time($seconds) {
if ($seconds > 0) { if ($seconds > 0) {
$midnight = mktime(0, 0, 0, date("n"), date("j"), date("Y")); $midnight = mktime(0, 0, 0, date("n"), date("j"), date("Y"));
return date("h:ia", $midnight + $seconds); return date("h:ia", $midnight + $seconds);
} else { } else {
return ""; return "";
} }
} }
   
if ($GTFSREnabled) { if ($GTFSREnabled) {
$serviceAlertCause = Array( $serviceAlertCause = Array(
"UNKNOWN_CAUSE" => "Unknown cause", "UNKNOWN_CAUSE" => "Unknown cause",
"OTHER_CAUSE" => "Other cause", "OTHER_CAUSE" => "Other cause",
"TECHNICAL_PROBLEM" => "Technical problem", "TECHNICAL_PROBLEM" => "Technical problem",
"STRIKE" => "Strike", "STRIKE" => "Strike",
"DEMONSTRATION" => "Demonstration", "DEMONSTRATION" => "Demonstration",
"ACCIDENT" => "Accident", "ACCIDENT" => "Accident",
"HOLIDAY" => "Holiday", "HOLIDAY" => "Holiday",
"WEATHER" => "Weather", "WEATHER" => "Weather",
"MAINTENANCE" => "Maintenance", "MAINTENANCE" => "Maintenance",
"CONSTRUCTION" => "Construction", "CONSTRUCTION" => "Construction",
"POLICE_ACTIVITY" => "Police activity", "POLICE_ACTIVITY" => "Police activity",
"MEDICAL_EMERGENCY" => "Medical emergency" "MEDICAL_EMERGENCY" => "Medical emergency"
); );
$serviceAlertEffect = Array( $serviceAlertEffect = Array(
"NO_SERVICE" => "No service", "NO_SERVICE" => "No service",
"REDUCED_SERVICE" => "Reduced service", "REDUCED_SERVICE" => "Reduced service",
"SIGNIFICANT_DELAYS" => "Significant delays", "SIGNIFICANT_DELAYS" => "Significant delays",
"DETOUR" => "Detour", "DETOUR" => "Detour",
"ADDITIONAL_SERVICE" => "Additional service", "ADDITIONAL_SERVICE" => "Additional service",
"MODIFIED_SERVICE" => "Modified service", "MODIFIED_SERVICE" => "Modified service",
"OTHER_EFFECT" => "Other effect", "OTHER_EFFECT" => "Other effect",
"UNKNOWN_EFFECT" => "Unknown effect", "UNKNOWN_EFFECT" => "Unknown effect",
"STOP_MOVED" => "Stop moved"); "STOP_MOVED" => "Stop moved");
   
set_include_path(get_include_path() . PATH_SEPARATOR . ($basePath . "lib/Protobuf-PHP/library/DrSlump/")); set_include_path(get_include_path() . PATH_SEPARATOR . ($basePath . "lib/Protobuf-PHP/library/DrSlump/"));
   
include_once("Protobuf.php"); include_once("Protobuf.php");
include_once("Protobuf/Message.php"); include_once("Protobuf/Message.php");
include_once("Protobuf/Registry.php"); include_once("Protobuf/Registry.php");
include_once("Protobuf/Descriptor.php"); include_once("Protobuf/Descriptor.php");
include_once("Protobuf/Field.php"); include_once("Protobuf/Field.php");
   
include_once($basePath . "lib/Protobuf-PHP/gtfs-realtime.php"); include_once($basePath . "lib/Protobuf-PHP/gtfs-realtime.php");
include_once("Protobuf/CodecInterface.php"); include_once("Protobuf/CodecInterface.php");
include_once("Protobuf/Codec/PhpArray.php"); include_once("Protobuf/Codec/PhpArray.php");
include_once("Protobuf/Codec/Binary.php"); include_once("Protobuf/Codec/Binary.php");
include_once("Protobuf/Codec/Binary/Writer.php"); include_once("Protobuf/Codec/Binary/Writer.php");
include_once("Protobuf/Codec/Json.php"); include_once("Protobuf/Codec/Json.php");
   
function getServiceAlerts($filter_class = "", $filter_id = "") { function getServiceAlerts($filter_class = "", $filter_id = "") {
/* /*
   
also need last modified epoch of client gtfs also need last modified epoch of client gtfs
   
- add,remove,patch,inform (null) - add,remove,patch,inform (null)
- stop - stop
- trip - trip
- network - network
- classes (WHERE=) - classes (WHERE=)
- route (short_name or route_id) - route (short_name or route_id)
- street - street
- stop - stop
- trip - trip
Currently support: Currently support:
network inform network inform
trip patch: stop remove trip patch: stop remove
street inform: route inform, trip inform, stop inform street inform: route inform, trip inform, stop inform
route patch: trip remove route patch: trip remove
*/ */
$fm = new transit_realtime\FeedMessage(); $fm = new transit_realtime\FeedMessage();
$fh = new transit_realtime\FeedHeader(); $fh = new transit_realtime\FeedHeader();
$fh->setGtfsRealtimeVersion(1); $fh->setGtfsRealtimeVersion(1);
$fh->setTimestamp(time()); $fh->setTimestamp(time());
$fm->setHeader($fh); $fm->setHeader($fh);
foreach (getCurrentAlerts() as $alert) { foreach (getCurrentAlerts() as $alert) {
$fe = new transit_realtime\FeedEntity(); $fe = new transit_realtime\FeedEntity();
$fe->setId($alert['id']); $fe->setId($alert['id']);
$fe->setIsDeleted(false); $fe->setIsDeleted(false);
$alert = new transit_realtime\Alert(); $alert = new transit_realtime\Alert();
$tr = new transit_realtime\TimeRange(); $tr = new transit_realtime\TimeRange();
$tr->setStart($alert['start']); $tr->setStart($alert['start']);
$tr->setEnd($alert['end']); $tr->setEnd($alert['end']);
$alert->addActivePeriod($tr); $alert->addActivePeriod($tr);
$informedEntities = getInformedAlerts($alert['id'], $_REQUEST['filter_class'], $_REQUEST['filter_id']); $informedEntities = getInformedAlerts($alert['id'], $_REQUEST['filter_class'], $_REQUEST['filter_id']);
if (sizeof($informedEntities) > 0) { if (sizeof($informedEntities) > 0) {
$informed = Array(); $informed = Array();
$es = new transit_realtime\EntitySelector(); $es = new transit_realtime\EntitySelector();
if ($informedEntity['informed_class'] == "agency") { if ($informedEntity['informed_class'] == "agency") {
$es->setAgencyId($informedEntity['informed_id']); $es->setAgencyId($informedEntity['informed_id']);
} }
if ($informedEntity['informed_class'] == "stop") { if ($informedEntity['informed_class'] == "stop") {
$es->setStopId($informedEntity['informed_id']); $es->setStopId($informedEntity['informed_id']);
} }
if ($informedEntity['informed_class'] == "route") { if ($informedEntity['informed_class'] == "route") {
$es->setRouteId($informedEntity['informed_id']); $es->setRouteId($informedEntity['informed_id']);
} }
if ($informedEntity['informed_class'] == "trip") { if ($informedEntity['informed_class'] == "trip") {
$td = new transit_realtime\TripDescriptor(); $td = new transit_realtime\TripDescriptor();
$td->setTripId($informedEntity['informed_id']); $td->setTripId($informedEntity['informed_id']);
$es->setTrip($td); $es->setTrip($td);
} }
$alert->addInformedEntity($es); $alert->addInformedEntity($es);
} }
$alert->setCause(constant("transit_realtime\Alert\Cause::" . $alert['cause'])); $alert->setCause(constant("transit_realtime\Alert\Cause::" . $alert['cause']));
$alert->setEffect(constant("transit_realtime\Alert\Effect::" . $alert['effect'])); $alert->setEffect(constant("transit_realtime\Alert\Effect::" . $alert['effect']));
$tsUrl = new transit_realtime\TranslatedString(); $tsUrl = new transit_realtime\TranslatedString();
$tUrl = new transit_realtime\TranslatedString\Translation(); $tUrl = new transit_realtime\TranslatedString\Translation();
$tUrl->setText($alert['url']); $tUrl->setText($alert['url']);
$tUrl->setLanguage("en"); $tUrl->setLanguage("en");
$tsUrl->addTranslation($tUrl); $tsUrl->addTranslation($tUrl);
$alert->setUrl($tsUrl); $alert->setUrl($tsUrl);
$tsHeaderText = new transit_realtime\TranslatedString(); $tsHeaderText = new transit_realtime\TranslatedString();
$tHeaderText = new transit_realtime\TranslatedString\Translation(); $tHeaderText = new transit_realtime\TranslatedString\Translation();
$tHeaderText->setText($alert['header']); $tHeaderText->setText($alert['header']);
$tHeaderText->setLanguage("en"); $tHeaderText->setLanguage("en");
$tsHeaderText->addTranslation($tHeaderText); $tsHeaderText->addTranslation($tHeaderText);
$alert->setHeaderText($tsHeaderText); $alert->setHeaderText($tsHeaderText);
$tsDescriptionText = new transit_realtime\TranslatedString(); $tsDescriptionText = new transit_realtime\TranslatedString();
$tDescriptionText = new transit_realtime\TranslatedString\Translation(); $tDescriptionText = new transit_realtime\TranslatedString\Translation();
$tDescriptionText->setText($alert['description']); $tDescriptionText->setText($alert['description']);
$tDescriptionText->setLanguage("en"); $tDescriptionText->setLanguage("en");
$tsDescriptionText->addTranslation($tDescriptionText); $tsDescriptionText->addTranslation($tDescriptionText);
$alert->setDescriptionText($tsDescriptionText); $alert->setDescriptionText($tsDescriptionText);
$fe->setAlert($alert); $fe->setAlert($alert);
$fm->addEntity($fe); $fm->addEntity($fe);
} }
return $fm; return $fm;
} }
   
function getServiceAlertsAsArray($filter_class = "", $filter_id = "") { function getServiceAlertsAsArray($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\PhpArray(); $codec = new DrSlump\Protobuf\Codec\PhpArray();
return $codec->encode(getServiceAlerts($filter_class, $filter_id)); return $codec->encode(getServiceAlerts($filter_class, $filter_id));
} }
   
function getServiceAlertsAsBinary($filter_class = "", $filter_id = "") { function getServiceAlertsAsBinary($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\Binary(); $codec = new DrSlump\Protobuf\Codec\Binary();
return $codec->encode(getServiceAlerts($filter_class, $filter_id)); return $codec->encode(getServiceAlerts($filter_class, $filter_id));
} }
   
function getServiceAlertsAsJSON($filter_class = "", $filter_id = "") { function getServiceAlertsAsJSON($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\Json(); $codec = new DrSlump\Protobuf\Codec\Json();
return $codec->encode(getServiceAlerts($filter_class, $filter_id)); return $codec->encode(getServiceAlerts($filter_class, $filter_id));
} }
   
function getServiceAlertsByClass() { function getServiceAlertsByClass() {
$return = Array(); $return = Array();
$alerts = getServiceAlertsAsArray("", ""); $alerts = getServiceAlertsAsArray("", "");
foreach ($alerts['entities'] as $entity) { foreach ($alerts['entities'] as $entity) {
foreach ($entity['informed'] as $informed) { foreach ($entity['informed'] as $informed) {
foreach ($informed as $key => $value) { foreach ($informed as $key => $value) {
if (strpos("_id", $key) > 0) { if (strpos("_id", $key) > 0) {
$parts = explode($key); $parts = explode($key);
$class = $parts[0]; $class = $parts[0];
$id = $value; $id = $value;
} }
} }
$return[$class][$id][] = $entity; $return[$class][$id][] = $entity;
} }
} }
} }
   
function getTripUpdates($filter_class = "", $filter_id = "") { function getTripUpdates($filter_class = "", $filter_id = "") {
$fm = new transit_realtime\FeedMessage(); $fm = new transit_realtime\FeedMessage();
$fh = new transit_realtime\FeedHeader(); $fh = new transit_realtime\FeedHeader();
$fh->setGtfsRealtimeVersion(1); $fh->setGtfsRealtimeVersion(1);
$fh->setTimestamp(time()); $fh->setTimestamp(time());
$fm->setHeader($fh); $fm->setHeader($fh);
foreach (getCurrentAlerts() as $alert) { foreach (getCurrentAlerts() as $alert) {
$informedEntities = getInformedAlerts($alert['id'], $_REQUEST['filter_class'], $_REQUEST['filter_id']); $informedEntities = getInformedAlerts($alert['id'], $_REQUEST['filter_class'], $_REQUEST['filter_id']);
$stops = Array(); $stops = Array();
$routestrips = Array(); $routestrips = Array();
if (sizeof($informedEntities) > 0) { if (sizeof($informedEntities) > 0) {
if ($informedEntity['informed_class'] == "stop" && $informed["x-action"] == "remove") { if ($informedEntity['informed_class'] == "stop" && $informed["x-action"] == "remove") {
$stops[] = $informedEntity['informed_id']; $stops[] = $informedEntity['informed_id'];
} }
if (($informedEntity['informed_class'] == "route" || $informedEntity['informed_class'] == "trip") && $informed["x-action"] == "patch") { if (($informedEntity['informed_class'] == "route" || $informedEntity['informed_class'] == "trip") && $informed["x-action"] == "patch") {
$routestrips[] = Array("id" => $informedEntity['informed_id'], $routestrips[] = Array("id" => $informedEntity['informed_id'],
"type" => $informedEntity['informed_class']); "type" => $informedEntity['informed_class']);
} }
} }
foreach ($routestrips as $routetrip) { foreach ($routestrips as $routetrip) {
$fe = new transit_realtime\FeedEntity(); $fe = new transit_realtime\FeedEntity();
$fe->setId($alert['id'] . $routetrip['id']); $fe->setId($alert['id'] . $routetrip['id']);
$fe->setIsDeleted(false); $fe->setIsDeleted(false);
$tu = new transit_realtime\TripUpdate(); $tu = new transit_realtime\TripUpdate();
$td = new transit_realtime\TripDescriptor(); $td = new transit_realtime\TripDescriptor();
if ($routetrip['type'] == "route") { if ($routetrip['type'] == "route") {
$td->setRouteId($routetrip['id']); $td->setRouteId($routetrip['id']);
} else if ($routetrip['type'] == "trip") { } else if ($routetrip['type'] == "trip") {
$td->setTripId($routetrip['id']); $td->setTripId($routetrip['id']);
} }
$tu->setTrip($td); $tu->setTrip($td);
foreach ($stops as $stop) { foreach ($stops as $stop) {
$stu = new transit_realtime\TripUpdate\StopTimeUpdate(); $stu = new transit_realtime\TripUpdate\StopTimeUpdate();
$stu->setStopId($stop); $stu->setStopId($stop);
$stu->setScheduleRelationship(transit_realtime\TripUpdate\StopTimeUpdate\ScheduleRelationship::SKIPPED); $stu->setScheduleRelationship(transit_realtime\TripUpdate\StopTimeUpdate\ScheduleRelationship::SKIPPED);
$tu->addStopTimeUpdate($stu); $tu->addStopTimeUpdate($stu);
} }
$fe->setTripUpdate($tu); $fe->setTripUpdate($tu);
$fm->addEntity($fe); $fm->addEntity($fe);
} }
} }
return $fm; return $fm;
} }
   
function getTripUpdatesAsArray($filter_class = "", $filter_id = "") { function getTripUpdatesAsArray($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\PhpArray(); $codec = new DrSlump\Protobuf\Codec\PhpArray();
return $codec->encode(getTripUpdates($filter_class, $filter_id)); return $codec->encode(getTripUpdates($filter_class, $filter_id));
} }
   
function getTripUpdatesAsBinary($filter_class = "", $filter_id = "") { function getTripUpdatesAsBinary($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\Binary(); $codec = new DrSlump\Protobuf\Codec\Binary();
return $codec->encode(getTripUpdates($filter_class, $filter_id)); return $codec->encode(getTripUpdates($filter_class, $filter_id));
} }
   
function getTripUpdatesAsJSON($filter_class = "", $filter_id = "") { function getTripUpdatesAsJSON($filter_class = "", $filter_id = "") {
$codec = new DrSlump\Protobuf\Codec\Json(); $codec = new DrSlump\Protobuf\Codec\Json();
return $codec->encode(getTripUpdates($filter_class, $filter_id)); return $codec->encode(getTripUpdates($filter_class, $filter_id));
} }
   
} }
?> ?>
   
<?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.
*/ */
   
function getStop($stopID) { function getStop($stopID) {
global $conn; global $conn;
$query = "Select * from stops where stop_id = :stopID LIMIT 1"; $query = "Select * from stops where stop_id = :stopID LIMIT 1";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->bindParam(":stopID", $stopID); $query->bindParam(":stopID", $stopID);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetch(PDO :: FETCH_ASSOC); return $query->fetch(PDO :: FETCH_ASSOC);
} }
   
function getStops($firstLetter = "", $startsWith = "") { function getStops($firstLetter = "", $startsWith = "") {
global $conn; global $conn;
$conditions = Array(); $conditions = Array();
if ($firstLetter != "") if ($firstLetter != "")
$conditions[] = "substr(stop_name,1,1) = :firstLetter"; $conditions[] = "substr(stop_name,1,1) = :firstLetter";
if ($startsWith != "") if ($startsWith != "")
$conditions[] = "stop_name like :startsWith"; $conditions[] = "stop_name like :startsWith";
$query = "Select * from stops"; $query = "Select * from stops";
if (sizeof($conditions) > 0) { if (sizeof($conditions) > 0) {
if (sizeof($conditions) > 1) { if (sizeof($conditions) > 1) {
$query .= " Where " . implode(" AND ", $conditions) . " "; $query .= " Where " . implode(" AND ", $conditions) . " ";
} else { } else {
$query .= " Where " . $conditions[0] . " "; $query .= " Where " . $conditions[0] . " ";
} }
} }
$query .= " order by stop_name;"; $query .= " order by stop_name;";
debug($query,"database"); debug($query,"database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
if ($firstLetter != "") if ($firstLetter != "")
$query->bindParam(":firstLetter", $firstLetter); $query->bindParam(":firstLetter", $firstLetter);
   
if ($startsWith != "") { if ($startsWith != "") {
$startsWith = $startsWith . "%"; $startsWith = $startsWith . "%";
$query->bindParam(":startsWith", $startsWith); $query->bindParam(":startsWith", $startsWith);
} }
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getNearbyStops($lat, $lng, $limit = "", $distance = 1000) { function getNearbyStops($lat, $lng, $limit = "", $distance = 1000) {
if ($lat == null || $lng == null) if ($lat == null || $lng == null)
return Array(); return Array();
if ($limit != "") if ($limit != "")
$limitSQL = " LIMIT :limit "; $limitSQL = " LIMIT :limit ";
global $conn; global $conn;
$query = "Select *, ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE) as distance $query = "Select *, ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE) as distance
from stops WHERE ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), :distance, FALSE) from stops WHERE ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), :distance, FALSE)
order by distance $limitSQL;"; order by distance $limitSQL;";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->bindParam(":distance", $distance); $query->bindParam(":distance", $distance);
$query->bindParam(":limit", $limit); $query->bindParam(":limit", $limit);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopsByName($name) { function getStopsByName($name) {
global $conn; global $conn;
$query = "Select * from stops where stop_name LIKE :name;"; $query = "Select * from stops where stop_name LIKE :name;";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$name = "%" . $name . ";%"; $name = "%" . $name . ";%";
$query->bindParam(":name", $name); $query->bindParam(":name", $name);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopsBySuburb($suburb) { function getStopsBySuburb($suburb) {
global $conn; global $conn;
$query = "Select * from stops where stop_desc LIKE :suburb order by stop_name;"; $query = "Select * from stops where stop_desc LIKE :suburb order by stop_name;";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$suburb = "%<br>Suburb: %" . $suburb . "%"; $suburb = "%<br>Suburb: %" . $suburb . "%";
$query->bindParam(":suburb", $suburb); $query->bindParam(":suburb", $suburb);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopsByStopCode($stop_code, $startsWith = "") { function getStopsByStopCode($stop_code, $startsWith = "") {
global $conn; global $conn;
$query = "Select * from stops where (stop_code = :stop_code OR stop_code LIKE :stop_code2)"; $query = "Select * from stops where (stop_code = :stop_code OR stop_code LIKE :stop_code2)";
if ($startsWith != "") if ($startsWith != "")
$query .= " AND stop_name like :startsWith"; $query .= " AND stop_name like :startsWith";
   
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
   
$query->bindParam(":stop_code", $stop_code); $query->bindParam(":stop_code", $stop_code);
$stop_code2 = $stop_code . "%"; $stop_code2 = $stop_code . "%";
$query->bindParam(":stop_code2", $stop_code2); $query->bindParam(":stop_code2", $stop_code2);
if ($startsWith != "") { if ($startsWith != "") {
$startsWith = $startsWith . "%"; $startsWith = $startsWith . "%";
$query->bindParam(":startsWith", $startsWith); $query->bindParam(":startsWith", $startsWith);
} }
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopRoutes($stopID, $service_period) { function getStopRoutes($stopID, $service_period) {
if ($service_period == "") { if ($service_period == "") {
$service_period = service_period(); $service_period = service_period();
} }
$service_ids = service_ids($service_period); $service_ids = service_ids($service_period);
$sidA = $service_ids[0]; $sidA = $service_ids[0];
$sidB = $service_ids[1]; $sidB = $service_ids[1];
global $conn; global $conn;
$query = "SELECT distinct service_id,trips.route_id,route_short_name,route_long_name $query = "SELECT distinct service_id,trips.route_id,route_short_name,route_long_name
FROM stop_times join trips on trips.trip_id = FROM stop_times join trips on trips.trip_id =
stop_times.trip_id join routes on trips.route_id = routes.route_id WHERE stop_id = :stopID stop_times.trip_id join routes on trips.route_id = routes.route_id WHERE stop_id = :stopID
AND (service_id=:service_periodA OR service_id=:service_periodB)"; AND (service_id=:service_periodA OR service_id=:service_periodB)";
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->bindParam(":service_periodA", $sidA); $query->bindParam(":service_periodA", $sidA);
$query->bindParam(":service_periodB", $sidB); $query->bindParam(":service_periodB", $sidB);
$query->bindParam(":stopID", $stopID); $query->bindParam(":stopID", $stopID);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "") { function getStopTrips($stopID, $service_period = "", $afterTime = "", $limit = "", $route_short_name = "") {
if ($service_period == "") { if ($service_period == "") {
$service_period = service_period(); $service_period = service_period();
} }
$service_ids = service_ids($service_period); $service_ids = service_ids($service_period);
   
$sidA = $service_ids[0]; $sidA = $service_ids[0];
$sidB = $service_ids[1]; $sidB = $service_ids[1];
  $limitSQL = "";
if ($limit != "") if ($limit != "")
$limitSQL = " LIMIT :limit "; $limitSQL .= " LIMIT :limit ";
   
global $conn; global $conn;
if ($afterTime != "") { if ($afterTime != "") {
$query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name, end_times.arrival_time as end_time $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name,end_times.arrival_time as end_time
FROM stop_times FROM stop_times
join trips on trips.trip_id = join trips on trips.trip_id =
stop_times.trip_id stop_times.trip_id
join routes on trips.route_id = routes.route_id , (SELECT trip_id,max(arrival_time) as arrival_time from stop_times join routes on trips.route_id = routes.route_id , (SELECT trip_id,max(arrival_time) as arrival_time from stop_times
WHERE stop_times.arrival_time IS NOT NULL group by trip_id) as end_times WHERE stop_times.arrival_time IS NOT NULL group by trip_id) as end_times
WHERE stop_times.stop_id = :stopID WHERE stop_times.stop_id = :stopID
AND stop_times.trip_id = end_times.trip_id AND stop_times.trip_id = end_times.trip_id
AND (service_id=:service_periodA OR service_id=:service_periodB) AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")."
AND end_times.arrival_time > :afterTime AND end_times.arrival_time > :afterTime
ORDER BY end_time $limitSQL"; ORDER BY end_time $limitSQL";
} else { } else {
$query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name $query = "SELECT stop_times.trip_id,arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name
FROM stop_times FROM stop_times
join trips on trips.trip_id = join trips on trips.trip_id =
stop_times.trip_id stop_times.trip_id
join routes on trips.route_id = routes.route_id join routes on trips.route_id = routes.route_id
WHERE stop_times.stop_id = :stopID WHERE stop_times.stop_id = :stopID
AND (service_id=:service_periodA OR service_id=:service_periodB) AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")."
ORDER BY arrival_time $limitSQL"; ORDER BY arrival_time $limitSQL";
} }
debug($query, "database"); debug($query, "database");
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->bindParam(":service_periodA", $sidA); $query->bindParam(":service_periodA", $sidA);
$query->bindParam(":service_periodB", $sidB); $query->bindParam(":service_periodB", $sidB);
$query->bindParam(":stopID", $stopID); $query->bindParam(":stopID", $stopID);
if ($limit != "") if ($limit != "")
$query->bindParam(":limit", $limit); $query->bindParam(":limit", $limit);
if ($afterTime != "") if ($afterTime != "")
$query->bindParam(":afterTime", $afterTime); $query->bindParam(":afterTime", $afterTime);
  if ($route_short_name != "")
  $query->bindParam(":route_short_name", $route_short_name);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
return $query->fetchAll(); return $query->fetchAll();
} }
   
function getStopTripsWithTimes($stopID, $time = "", $service_period = "", $time_range = "", $limit = "") { function getStopTripsWithTimes($stopID, $time = "", $service_period = "", $time_range = "", $limit = "") {
if ($service_period == "") if ($service_period == "")
$service_period = service_period(); $service_period = service_period();
if ($time_range == "") if ($time_range == "")
$time_range = (24 * 60 * 60); $time_range = (24 * 60 * 60);
if ($time == "") if ($time == "")
$time = current_time(); $time = current_time();
if ($limit == "") if ($limit == "")
$limit = 10; $limit = 10;
$trips = getStopTrips($stopID, $service_period, $time); $trips = getStopTrips($stopID, $service_period, $time);
$timedTrips = Array(); $timedTrips = Array();
if ($trips && sizeof($trips) > 0) { if ($trips && sizeof($trips) > 0) {
foreach ($trips as $trip) { foreach ($trips as $trip) {
if ($trip['arrival_time'] != "") { if ($trip['arrival_time'] != "") {
if (strtotime($trip['arrival_time']) > strtotime($time) and strtotime($trip['arrival_time']) < (strtotime($time) + $time_range)) { if (strtotime($trip['arrival_time']) > strtotime($time) and strtotime($trip['arrival_time']) < (strtotime($time) + $time_range)) {
$timedTrips[] = $trip; $timedTrips[] = $trip;
} }
} else { } else {
$timedTrip = getTripAtStop($trip['trip_id'], $trip['stop_sequence']); $timedTrip = getTripAtStop($trip['trip_id'], $trip['stop_sequence']);
if ($timedTrip['arrival_time'] > $time and strtotime($timedTrip['arrival_time']) < (strtotime($time) + $time_range)) { if ($timedTrip['arrival_time'] > $time and strtotime($timedTrip['arrival_time']) < (strtotime($time) + $time_range)) {
$timedTrips[] = $timedTrip; $timedTrips[] = $timedTrip;
} }
} }
if (sizeof($timedTrips) > $limit) if (sizeof($timedTrips) > $limit)
break; break;
} }
sktimesort($timedTrips, "arrival_time", true); sktimesort($timedTrips, "arrival_time", true);
} }
return $timedTrips; return $timedTrips;
} }
   
?> ?>
<?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 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 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']) - (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'])); $date = date("c", strtotime($obsv['time']));
$timing_period = service_period(strtotime($date)); $timing_period = service_period(strtotime($date));
if (isset($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 "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(preg_replace("/[A-Z]/", "", $obsv["myway_route"])); $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); $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" => "{$potentialRoute['route_short_name']} {$potentialRoute['route_long_name']} {$trip['direction']}", "route_name" => "{$trip['route_short_name']} {$trip['route_long_name']} {$trip['direction']}",
"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 {
  echo "{$trip['route_id']} != {$stopRoute['route_id']}<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();
} }
   
<?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');
header('Content-Type: text/javascript; charset=utf8'); header('Content-Type: text/javascript; charset=utf8');
// header('Access-Control-Allow-Origin: http://bus.lambdacomplex.org/'); // header('Access-Control-Allow-Origin: http://bus.lambdacomplex.org/');
header('Access-Control-Max-Age: 3628800'); header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
?> ?>
{ {
"label": "<?php echo $_REQUEST['routeid']; ?>", "label": "<?php echo $_REQUEST['routeid']; ?>",
"data": <?php "data": <?php
$query = "select * from myway_timingdeltas where route_name = :route_name AND abs(timing_delta) < 2*(select stddev(timing_delta) from myway_timingdeltas) order by stop_sequence;"; $query = "select * from myway_timingdeltas where route_name = :route_name AND abs(timing_delta) < 2*(select stddev(timing_delta) from myway_timingdeltas) order by stop_sequence;";
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->bindParam(':route_name', $_REQUEST['routeid'], PDO::PARAM_STR, 42); $_REQUEST['routeid'].=" ";
  $query->bindParam(':route_name', $_REQUEST['routeid'], PDO::PARAM_STR);
   
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
return Array(); return Array();
} }
foreach ($query->fetchAll() as $delta) { foreach ($query->fetchAll() as $delta) {
$points[] = "[{$delta['stop_sequence']}, {$delta['timing_delta']}]"; $points[] = "[{$delta['stop_sequence']}, {$delta['timing_delta']}]";
}; };
echo "[" . implode(",", $points) . "]"; echo "[" . implode(",", $points) . "]";
?> ?>
} }
<?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 Deltas", "mywayDelta"); include_header("MyWay Deltas", "mywayDelta");
?> ?>
   
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../js/flot/excanvas.min.js"></script><![endif]--> <!--[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> <script language="javascript" type="text/javascript" src="../js/flot/jquery.flot.js"></script>
<form method="get" action=""> <form method="get" action="">
<select id="stopid" name="stopid"> <select id="stopid" name="stopid">
<?php <?php
$query = "select distinct myway_stop from myway_timingdeltas where myway_stop != '' order by myway_stop"; $query = "select distinct myway_stop from myway_timingdeltas where myway_stop != '' order by myway_stop";
$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();
} }
foreach ($query->fetchAll() as $stop) { foreach ($query->fetchAll() as $stop) {
echo "<option value=\"{$stop['myway_stop']}\">{$stop['myway_stop']}</option>"; echo "<option value=\"{$stop['myway_stop']}\">{$stop['myway_stop']}</option>";
}; };
?> </select> <center><div id="placeholder" style="width:900px;height:550px"></div></center> ?> </select> <center><div id="placeholder" style="width:900px;height:550px"></div></center>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
var d = new Date(); var d = new Date();
d.setUTCMinutes(0); d.setUTCMinutes(0);
d.setUTCHours(0); d.setUTCHours(0);
var midnight = d.getTime(); var midnight = d.getTime();
   
var placeholder = $("#placeholder"); var placeholder = $("#placeholder");
var data = []; var data = [];
var options = { var options = {
xaxis: { xaxis: {
mode: "time", mode: "time"
min: midnight + (1000*60*60*8),  
max: midnight + (1000*60*60*23.5)  
}, },
yaxis: { yaxis: {
tickFormatter: yformatter tickFormatter: yformatter
}, },
grid: { hoverable: true, clickable: true, labelMargin: 32 }, grid: { hoverable: true, clickable: true, labelMargin: 32 },
series: { series: {
lines: { show: false }, lines: { show: false },
points: { show: true } points: { show: true }
} }
}; };
   
var plot = $.plot(placeholder, data, options); var plot = $.plot(placeholder, data, options);
var o; var o;
o = plot.pointOffset({ x: midnight+ (9*60*60*1000), y: -1.2}); o = plot.pointOffset({ x: midnight+ (9*60*60*1000), y: -1.2});
placeholder.append('<div style="position:absolute;left:' + (o.left + 4) + 'px;top:' + o.top + 'px;color:#666;font-size:smaller">9am</div>'); placeholder.append('<div style="position:absolute;left:' + (o.left + 4) + 'px;top:' + o.top + 'px;color:#666;font-size:smaller">9am</div>');
o = plot.pointOffset({ x: midnight+ (16*60*60*1000), y: -1.2}); o = plot.pointOffset({ x: midnight+ (16*60*60*1000), y: -1.2});
placeholder.append('<div style="position:absolute;left:' + (o.left + 4) + 'px;top:' + o.top + 'px;color:#666;font-size:smaller">4pm</div>'); placeholder.append('<div style="position:absolute;left:' + (o.left + 4) + 'px;top:' + o.top + 'px;color:#666;font-size:smaller">4pm</div>');
// fetch one series, adding to what we got // fetch one series, adding to what we got
var alreadyFetched = {}; var alreadyFetched = {};
$("#stopid").change(function () { $("#stopid").change(function () {
var select = $(this); var select = $(this);
// find the URL in the link right next to us // find the URL in the link right next to us
// var dataurl = button.siblings('a').attr('href'); // var dataurl = button.siblings('a').attr('href');
var dataurl = "myway_timeliness_stop.json.php?stopid=" + select.val(); var dataurl = "myway_timeliness_stop.json.php?stopid=" + select.val();
// then fetch the data with jQuery // then fetch the data with jQuery
function onDataReceived(series) { function onDataReceived(series) {
// extract the first coordinate pair so you can see that // extract the first coordinate pair so you can see that
// data is now an ordinary Javascript object // data is now an ordinary Javascript object
var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')'; var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')';
// let's add it to our current data // let's add it to our current data
if (!alreadyFetched[series.label]) { if (!alreadyFetched[series.label]) {
alreadyFetched[series.label] = true; alreadyFetched[series.label] = true;
data.push(series); data.push(series);
} }
// and plot all we got // and plot all we got
$.plot(placeholder, data, options); $.plot(placeholder, data, options);
} }
$.ajax({ $.ajax({
url: dataurl, url: dataurl,
method: 'GET', method: 'GET',
dataType: 'json', dataType: 'json',
success: onDataReceived success: onDataReceived
}); });
}); });
   
}); });
function yformatter(v) { function yformatter(v) {
if (Math.floor(v/60) < -9) return ""; if (Math.floor(v/60) < -9) return "";
return Math.abs(Math.floor(v/60)) + " min " + (v == 0 ? "" : (v >0 ? "early":"late")) return Math.abs(Math.floor(v/60)) + " min " + (v == 0 ? "" : (v >0 ? "early":"late"))
} }
function showTooltip(x, y, contents) { function showTooltip(x, y, contents) {
$('<div id="tooltip">' + contents + '</div>').css( { $('<div id="tooltip">' + contents + '</div>').css( {
position: 'absolute', position: 'absolute',
display: 'none', display: 'none',
top: y + 5, top: y + 5,
left: x + 5, left: x + 5,
border: '1px solid #fdd', border: '1px solid #fdd',
padding: '2px', padding: '2px',
'background-color': '#fee', 'background-color': '#fee',
opacity: 0.80 opacity: 0.80
}).appendTo("body").fadeIn(200); }).appendTo("body").fadeIn(200);
} }
var previousPoint = null; var previousPoint = null;
$("#placeholder").bind("plothover", function (event, pos, item) { $("#placeholder").bind("plothover", function (event, pos, item) {
$("#x").text(pos.x.toFixed(2)); $("#x").text(pos.x.toFixed(2));
$("#y").text(pos.y.toFixed(2)); $("#y").text(pos.y.toFixed(2));
if (item) { if (item) {
if (previousPoint != item.dataIndex) { if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex; previousPoint = item.dataIndex;
$("#tooltip").remove(); $("#tooltip").remove();
var x = item.datapoint[0].toFixed(2), var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2); y = item.datapoint[1].toFixed(2);
var d = new Date(); var d = new Date();
d.setTime(x); d.setTime(x);
var time = d.getUTCHours() +':'+ (d.getUTCMinutes().toString().length == 1 ? '0'+ d.getMinutes(): d.getUTCMinutes()) var time = d.getUTCHours() +':'+ (d.getUTCMinutes().toString().length == 1 ? '0'+ d.getMinutes(): d.getUTCMinutes())
   
showTooltip(item.pageX, item.pageY, showTooltip(item.pageX, item.pageY,
item.series.label + " at "+ time +" = " + Math.abs(new Number(y/60).toFixed(2))+" minutes "+(y >0 ? "early":"late")); item.series.label + " at "+ time +" = " + Math.abs(new Number(y/60).toFixed(2))+" minutes "+(y >0 ? "early":"late"));
} }
} }
else { else {
$("#tooltip").remove(); $("#tooltip").remove();
previousPoint = null; previousPoint = null;
} }
}); });
   
</script> </script>