Trip planner reliablity fixes
Trip planner reliablity fixes

#!/bin/bash #!/bin/bash
#this script should be run from a fresh git checkout from github #this script should be run from a fresh git checkout from github
#ami base must have yum install lighttpd-fastcgi, git, tomcat6 #ami base must have yum install lighttpd-fastcgi, git, tomcat6
#screen php-cli php-gd tomcat6-webapps tomcat6-admin-webapps svn maven2 #php-cli php-gd tomcat6-webapps tomcat6-admin-webapps svn maven2
#postgrtes postgres-server php-pg #postgres postgres-server php-pg
#http://www.how2forge.org/installing-lighttpd-with-php5-and-mysql-support-on-fedora-12 #http://www.how2forge.org/installing-lighttpd-with-php5-and-mysql-support-on-fedora-12
   
cp /root/aws.php /tmp/ cp /root/aws.php /tmp/
mkdir /var/www/lib/staticmaplite/cache mkdir /var/www/lib/staticmaplite/cache
chcon -h system_u:object_r:httpd_sys_content_t /var/www chcon -h system_u:object_r:httpd_sys_content_t /var/www
chcon -R -h root:object_r:httpd_sys_content_t /var/www/* chcon -R -h root:object_r:httpd_sys_content_t /var/www/*
chcon -R -t httpd_sys_content_rw_t /var/www/lib/staticmaplite/cache chcon -R -t httpd_sys_content_rw_t /var/www/lib/staticmaplite/cache
chmod -R 777 /var/www/lib/staticmaplite/cache chmod -R 777 /var/www/lib/staticmaplite/cache
wget http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip \ wget http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip \
-O /var/www/cbrfeed.zip -O /var/www/cbrfeed.zip
   
createdb transitdata createdb transitdata
createlang -d transitdata plpgsql createlang -d transitdata plpgsql
psql -d transitdata -f /var/www/lib/postgis.sql psql -d transitdata -f /var/www/lib/postgis.sql
# curl https://github.com/maxious/ACTBus-ui/raw/master/transitdata.cbrfeed.sql.gz -o transitdata.cbrfeed.sql.gz # curl https://github.com/maxious/ACTBus-ui/raw/master/transitdata.cbrfeed.sql.gz -o transitdata.cbrfeed.sql.gz
#made with pg_dump transitdata | gzip -c > transitdata.cbrfeed.sql.gz #made with pg_dump transitdata | gzip -c > transitdata.cbrfeed.sql.gz
gunzip /var/www/transitdata.cbrfeed.sql.gz gunzip /var/www/transitdata.cbrfeed.sql.gz
psql -d transitdata -f /var/www/transitdata.cbrfeed.sql psql -d transitdata -f /var/www/transitdata.cbrfeed.sql
#createuser transitdata -SDRP #createuser transitdata -SDRP
#password transitdata #password transitdata
#psql -d transitdata -c \"GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips TO transitdata;\" #psql -d transitdata -c \"GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips TO transitdata;\"
php /var/www/updatedb.php php /var/www/updatedb.php
   
wget http://s3-ap-southeast-1.amazonaws.com/busresources/Graph.obj \ wget http://s3-ap-southeast-1.amazonaws.com/busresources/Graph.obj \
-O /tmp/Graph.obj -O /tmp/Graph.obj
rm -rfv /usr/share/tomcat6/webapps/opentripplanner* rm -rfv /usr/share/tomcat6/webapps/opentripplanner*
wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-webapp.war \ wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-webapp.war \
-O /usr/share/tomcat6/webapps/opentripplanner-webapp.war -O /usr/share/tomcat6/webapps/opentripplanner-webapp.war
wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-api-webapp.war \ wget http://s3-ap-southeast-1.amazonaws.com/busresources/opentripplanner-api-webapp.war \
-O /usr/share/tomcat6/webapps/opentripplanner-api-webapp.war -O /usr/share/tomcat6/webapps/opentripplanner-api-webapp.war
/etc/init.d/tomcat6 restart /etc/init.d/tomcat6 restart
   
file:a/readme.txt -> file:b/readme.txt
Busness Time - An ACT bus timetable webapp Busness Time - An ACT bus timetable webapp
Based on the maxious-canberra-transit-feed @ http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip Based on the maxious-canberra-transit-feed @ http://s3-ap-southeast-1.amazonaws.com/busresources/cbrfeed.zip
Source code for the https://github.com/maxious/ACTBus-data transit Source code for the https://github.com/maxious/ACTBus-data transit
feed and https://github.com/maxious/ACTBus-ui this site available from github. feed and https://github.com/maxious/ACTBus-ui this site available from github.
Uses jQuery Mobile, PHP, Ruby, Python, Google Transit Feed Specification Uses jQuery Mobile, PHP, PostgreSQL, OpenTripPlanner, OpenLayers, OpenStreetMap, Cloudmade Geocoder
tools, OpenTripPlanner, OpenLayers, OpenStreetMap, Cloudmade Geocoder  
and Tile Service and Tile Service
   
Must have view.sh running on port 8765 for this webapp to work See aws/awsStartup.sh for example startup steps
   
For static maps, may have to do For static maps, may have to do
/usr/sbin/setsebool -P httpd_can_network_connect=1 /usr/sbin/setsebool -P httpd_can_network_connect=1
on fedora on fedora
   
   
<?php <?php
include ('include/common.inc.php'); include ('include/common.inc.php');
include_header("Trip Planner", "tripPlanner", true, true, true); include_header("Trip Planner", "tripPlanner", true, true, true);
$from = (isset($_REQUEST['from']) ? filter_var($_REQUEST['from'], FILTER_SANITIZE_STRING) : "Brigalow"); $from = (isset($_REQUEST['from']) ? filter_var($_REQUEST['from'], FILTER_SANITIZE_STRING) : "");
$to = (isset($_REQUEST['to']) ? filter_var($_REQUEST['to'], FILTER_SANITIZE_STRING) : "Barry"); $to = (isset($_REQUEST['to']) ? filter_var($_REQUEST['to'], FILTER_SANITIZE_STRING) : "");
$date = (isset($_REQUEST['date']) ? filter_var($_REQUEST['date'], FILTER_SANITIZE_STRING) : date("m/d/Y")); $date = (isset($_REQUEST['date']) ? filter_var($_REQUEST['date'], FILTER_SANITIZE_STRING) : date("m/d/Y"));
$time = (isset($_REQUEST['time']) ? filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING) : date("H:i")); $time = (isset($_REQUEST['time']) ? filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING) : date("H:i"));
function formatTime($timeString) { function formatTime($timeString) {
$timeParts = explode("T",$timeString); $timeParts = explode("T",$timeString);
return str_replace("Z","",$timeParts[1]); return str_replace("Z","",$timeParts[1]);
} }
function tripPlanForm($errorMessage = "") function tripPlanForm($errorMessage = "")
{ {
   
global $date, $time, $from, $to; global $date, $time, $from, $to;
echo "<font color=red>$errorMessage</font>"; echo "<font color=red>$errorMessage</font>";
echo '<form action="tripPlanner.php" method="post"> echo '<form action="tripPlanner.php" method="post">
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="from">I would like to go from</label> <label for="from">I would like to go from</label>
<input type="text" name="from" id="from" value="' . $from . '" /> <input type="text" name="from" id="from" value="' . $from . '" />
<a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a> <a href="#" style="display:none" name="fromHere" id="fromHere"/>Here?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="to"> to </label> <label for="to"> to </label>
<input type="text" name="to" id="to" value="' . $to . '" /> <input type="text" name="to" id="to" value="' . $to . '" />
<a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a> <a href="#" style="display:none" name="toHere" id="toHere"/>Here?</a>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="date"> on </label> <label for="date"> on </label>
<input type="text" name="date" id="date" value="' . $date . '" /> <input type="text" name="date" id="date" value="' . $date . '" />
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label for="time"> at </label> <label for="time"> at </label>
<input type="time" name="time" id="time" value="' . $time . '" /> <input type="time" name="time" id="time" value="' . $time . '" />
</div> </div>
<input type="submit" value="Go!"></form>'; <input type="submit" value="Go!"></form>';
echo "<script> echo "<script>
$('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;}); $('#toHere').click(function(event) { $('#to').val(getCookie('geolocate')); return false;});
$('#toHere').show(); $('#toHere').show();
$('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;}); $('#fromHere').click(function(event) { $('#from').val(getCookie('geolocate')); return false;});
$('#fromHere').show(); $('#fromHere').show();
   
</script>"; </script>";
} }
function processItinerary($itineraryNumber, $itinerary) function processItinerary($itineraryNumber, $itinerary)
{ {
echo '<div data-role="collapsible" ' . ($itineraryNumber > 0 ? 'data-collapsed="true"' : "") . '> <h3> Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes (".formatTime($itinerary->startTime)." to ".formatTime($itinerary->endTime).")</h3><p>"; echo '<div data-role="collapsible" ' . ($itineraryNumber > 0 ? 'data-collapsed="true"' : "") . '> <h3> Option #' . ($itineraryNumber + 1) . ": " . floor($itinerary->duration / 60000) . " minutes (".formatTime($itinerary->startTime)." to ".formatTime($itinerary->endTime).")</h3><p>";
echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)<br>\n"; echo "Walking time: " . floor($itinerary->walkTime / 60000) . " minutes (" . floor($itinerary->walkDistance) . " meters)<br>\n";
echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes<br>\n"; echo "Transit time: " . floor($itinerary->transitTime / 60000) . " minutes<br>\n";
echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes<br>\n"; echo "Waiting time: " . floor($itinerary->waitingTime / 60000) . " minutes<br>\n";
if (is_array($itinerary->legs->leg)) { if (is_array($itinerary->legs->leg)) {
$legMarkers = array(); $legMarkers = array();
foreach ($itinerary->legs->leg as $legNumber => $leg) { foreach ($itinerary->legs->leg as $legNumber => $leg) {
$legMarkers[] = array( $legMarkers[] = array(
$leg->from->lat, $leg->from->lat,
$leg->from->lon $leg->from->lon
); );
} }
echo '' . staticmap($legMarkers, 0, "iconb", false) . "<br>\n"; echo '' . staticmap($legMarkers, 0, "iconb", false) . "<br>\n";
echo '<ul>'; echo '<ul>';
foreach ($itinerary->legs->leg as $legNumber => $leg) { foreach ($itinerary->legs->leg as $legNumber => $leg) {
echo '<li>'; echo '<li>';
processLeg($legNumber, $leg); processLeg($legNumber, $leg);
echo "</li>"; echo "</li>";
flush(); @ob_flush(); flush(); @ob_flush();
} }
echo "</ul>"; echo "</ul>";
} }
else { else {
echo '' . staticmap(array( echo '' . staticmap(array(
array( array(
$itinerary->legs->leg->from->lat, $itinerary->legs->leg->from->lat,
$itinerary->legs->leg->from->lon $itinerary->legs->leg->from->lon
) )
) , 0, "iconb", false) . "<br>\n"; ) , 0, "iconb", false) . "<br>\n";
processLeg(0, $itinerary->legs->leg); processLeg(0, $itinerary->legs->leg);
} }
echo "</p></div>"; echo "</p></div>";
} }
function processLeg($legNumber, $leg) function processLeg($legNumber, $leg)
{ {
$legArray = object2array($leg); $legArray = object2array($leg);
echo '<h3>Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes) </h3>\n"; echo '<h3>Leg #' . ($legNumber + 1) . " ( {$legArray['@mode']} from: {$leg->from->name} to {$leg->to->name}, " . floor($leg->duration / 60000) . " minutes) </h3>\n";
if ($legArray["@mode"] === "BUS") { if ($legArray["@mode"] === "BUS") {
echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "<br>"; echo "Take bus {$legArray['@route']} " . str_replace("To", "towards", $legArray['@headsign']) . "<br>";
} }
else { else {
$walkStepMarkers = array(); $walkStepMarkers = array();
foreach ($leg->steps->walkSteps as $stepNumber => $step) { foreach ($leg->steps->walkSteps as $stepNumber => $step) {
$walkStepMarkers[] = array( $walkStepMarkers[] = array(
$step->lat, $step->lat,
$step->lon $step->lon
); );
} }
echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "<br>\n"; echo "" . staticmap($walkStepMarkers, 0, "icong", false) . "<br>\n";
foreach ($leg->steps->walkSteps as $stepNumber => $step) { foreach ($leg->steps->walkSteps as $stepNumber => $step) {
echo "Walking step " . ($stepNumber + 1) . ": "; echo "Walking step " . ($stepNumber + 1) . ": ";
if ($step->relativeDirection == "CONTINUE") { if ($step->relativeDirection == "CONTINUE") {
echo "Continue, "; echo "Continue, ";
} else if ($step->relativeDirection) echo "Turn ".ucwords(strtolower(str_replace("_"," ",$step->relativeDirection))).", "; } else if ($step->relativeDirection) echo "Turn ".ucwords(strtolower(str_replace("_"," ",$step->relativeDirection))).", ";
echo "Go ".ucwords(strtolower($step->absoluteDirection))." on "; echo "Go ".ucwords(strtolower($step->absoluteDirection))." on ";
if (strpos($step->streetName,"from") !== false && strpos($step->streetName,"way") !== false) { if (strpos($step->streetName,"from") !== false && strpos($step->streetName,"way") !== false) {
echo "footpath"; echo "footpath";
} else { } else {
echo $step->streetName; echo $step->streetName;
} }
echo " for " . floor($step->distance) . " meters<br>\n"; echo " for " . floor($step->distance) . " meters<br>\n";
} }
} }
} }
if ($_REQUEST['time']) { if ($_REQUEST['time']) {
$toPlace = (startsWith($to, "-") ? $to : geocode($to, false)); $toPlace = (startsWith($to, "-") ? $to : geocode($to, false));
$fromPlace = (startsWith($from, "-") ? $from : geocode($from, false)); $fromPlace = (startsWith($from, "-") ? $from : geocode($from, false));
if ($toPlace == "" || $fromPlace == "") { if ($toPlace == "" || $fromPlace == "") {
$errorMessage = ""; $errorMessage = "";
if ($toPlace === "") { if ($toPlace === "") {
$errorMessage.= urlencode($to) . " not found.<br>\n"; $errorMessage.= urlencode($to) . " not found.<br>\n";
trackEvent("Trip Planner","Geocoder Failed", $to); trackEvent("Trip Planner","Geocoder Failed", $to);
} }
if ($fromPlace === "") { if ($fromPlace === "") {
$errorMessage.= urlencode($from) . " not found.<br>\n"; $errorMessage.= urlencode($from) . " not found.<br>\n";
trackEvent("Trip Planner","Geocoder Failed", $from); trackEvent("Trip Planner","Geocoder Failed", $from);
} }
tripPlanForm($errorMessage); tripPlanForm($errorMessage);
} }
else { else {
$url = $otpAPIurl . "ws/plan?date=" . urlencode($_REQUEST['date']) . "&time=" . urlencode($_REQUEST['time']) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces="; $url = $otpAPIurl . "ws/plan?date=" . urlencode($_REQUEST['date']) . "&time=" . urlencode($_REQUEST['time']) . "&mode=TRANSIT%2CWALK&optimize=QUICK&maxWalkDistance=840&wheelchair=false&toPlace=$toPlace&fromPlace=$fromPlace&intermediatePlaces=";
$ch = curl_init($url); debug($url);
  $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json" "Accept: application/json"
)); ));
curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$page = curl_exec($ch); $page = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch) || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) .(isDebug() ? $url : "")); tripPlanForm("Trip planner temporarily unavailable: " . curl_errno($ch) . " " . curl_error($ch) . " ". curl_getinfo($ch, CURLINFO_HTTP_CODE) .(isDebug() ? "<br>".$url : ""));
trackEvent("Trip Planner","Trip Planner Failed", $url); trackEvent("Trip Planner","Trip Planner Failed", $url);
} }
else { else {
trackEvent("Trip Planner","Plan Trip From", $from); trackEvent("Trip Planner","Plan Trip From", $from);
trackEvent("Trip Planner","Plan Trip To", $to); trackEvent("Trip Planner","Plan Trip To", $to);
$tripplan = json_decode($page); $tripplan = json_decode($page);
debug(print_r($triplan, true)); debug(print_r($tripplan, true));
echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>"; echo "<h1> From: {$tripplan->plan->from->name} To: {$tripplan->plan->to->name} </h1>";
echo "<h1> At: ".formatTime($tripplan->plan->date)." </h1>"; echo "<h1> At: ".formatTime($tripplan->plan->date)." </h1>";
if (is_array($tripplan->plan->itineraries->itinerary)) { if (is_array($tripplan->plan->itineraries->itinerary)) {
echo '<div data-role="collapsible-set">'; echo '<div data-role="collapsible-set">';
foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) { foreach ($tripplan->plan->itineraries->itinerary as $itineraryNumber => $itinerary) {
processItinerary($itineraryNumber, $itinerary); processItinerary($itineraryNumber, $itinerary);
} }
echo "</div>"; echo "</div>";
} }
else { else {
processItinerary(0, $tripplan->plan->itineraries->itinerary); processItinerary(0, $tripplan->plan->itineraries->itinerary);
} }
} }
curl_close($ch); curl_close($ch);
} }
} }
else { else {
tripPlanForm(); tripPlanForm();
} }
include_footer(); include_footer();
?> ?>