KML for trips/routes/stops fixed
KML for trips/routes/stops fixed

<?php <?php
   
header('Content-Type: application/vnd.google-earth.kml+xml'); header('Content-Type: application/vnd.google-earth.kml+xml');
include ('../include/common.inc.php'); include ('../include/common.inc.php');
echo '<?xml version="1.0" encoding="UTF-8"?> echo '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>'; <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>';
echo ' echo '
<Style id="yellowLineGreenPoly"> <Style id="yellowLineGreenPoly">
<LineStyle> <LineStyle>
<color>7f00ff00</color> <color>7f00ff00</color>
<width>4</width> <width>4</width>
</LineStyle> </LineStyle>
<PolyStyle> <PolyStyle>
<color>7f00ffff</color> <color>7f00ffff</color>
</PolyStyle> </PolyStyle>
</Style>'; </Style>';
$route = getRoute($routeid); $route = getRoute($routeid);
echo "\n<Placemark>\n"; echo "\n<Placemark>\n";
$link = curPageURL()."/../trip.php?routeid=".htmlspecialchars ($route["route_id"]);  
echo "<name>".$route['route_short_name']."</name>"; $link = curPageURL() . "/../trip.php?routeid=" . htmlspecialchars($route["route_id"]);
echo '<atom:link href="'.$link.'"/>'; echo "<name>" . $route['route_short_name'] . "</name>";
echo '<description><![CDATA[ <a href="'.$link.'">'.$route['route_short_name']." ".$route['route_long_name']."</a>]]> </description>"; echo '<atom:link href="' . $link . '"/>';
  echo '<description><![CDATA[ <a href="' . $link . '">' . $route['route_short_name'] . " " . $route['route_long_name'] . "</a>]]> </description>";
echo "<styleUrl>#yellowLineGreenPoly</styleUrl>"; echo "<styleUrl>#yellowLineGreenPoly</styleUrl>";
  $trip = getRouteNextTrip($routeid);
$trip = getRouteNextTrip($routeid); echo getTripShape($trip['trip_id']);
echo getTripShape($trip['trip_id']);  
   
echo "</Placemark>\n</Document></kml>\n"; echo "</Placemark>\n</Document></kml>\n";
?> ?>
   
   
<?php <?php
  header('Content-type: application/vnd.google-earth.kml+xml');
include ('../include/common.inc.php'); include ('../include/common.inc.php');
header('Content-type: application/vnd.google-earth.kml+xml');  
//http://wiki.openstreetmap.org/wiki/OpenLayers_Dynamic_KML //http://wiki.openstreetmap.org/wiki/OpenLayers_Dynamic_KML
// Creates the KML/XML Document. // Creates the KML/XML Document.
$dom = new DOMDocument('1.0', 'UTF-8'); $dom = new DOMDocument('1.0', 'UTF-8');
// Creates the root KML element and appends it to the root document. // Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml'); $node = $dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml');
$parNode = $dom->appendChild($node); $parNode = $dom->appendChild($node);
// Creates a KML Document element and append it to the KML element. // Creates a KML Document element and append it to the KML element.
$dnode = $dom->createElement('Document'); $dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode); $docNode = $parNode->appendChild($dnode);
if ($suburb != "") $result_stops = getStopsBySuburb($suburb); if ($suburb != "") $result_stops = getStopsBySuburb($suburb);
else $result_stops = getStops(); else $result_stops = getStops();
foreach ($result_stops as $stop) { foreach ($result_stops as $stop) {
$description = 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $stop['stop_id'] . " <br>"; $description = baseURL() . 'stop.php?stopid=' . $stop['stop_id'] . " <br>";
// Creates a Placemark and append it to the Document. // Creates a Placemark and append it to the Document.
$node = $dom->createElement('Placemark'); $node = $dom->createElement('Placemark');
$placeNode = $docNode->appendChild($node); $placeNode = $docNode->appendChild($node);
// Creates an id attribute and assign it the value of id column. // Creates an id attribute and assign it the value of id column.
$placeNode->setAttribute('id', 'placemark' . $stop['stop_id']); $placeNode->setAttribute('id', 'placemark' . $stop['stop_id']);
// Create name, and description elements and assigns them the values of the name and address columns from the results. // Create name, and description elements and assigns them the values of the name and address columns from the results.
$nameNode = $dom->createElement('name', htmlentities($stop['stop_name'])); $nameNode = $dom->createElement('name', htmlentities($stop['stop_name']));
$descriptionNode = $dom->createElement('description', $description); $descriptionNode = $dom->createElement('description', $description);
$placeNode->appendChild($nameNode); $placeNode->appendChild($nameNode);
$placeNode->appendChild($descriptionNode); $placeNode->appendChild($descriptionNode);
// Creates a Point element. // Creates a Point element.
$pointNode = $dom->createElement('Point'); $pointNode = $dom->createElement('Point');
$placeNode->appendChild($pointNode); $placeNode->appendChild($pointNode);
// Creates a coordinates element and gives it the value of the lng and lat columns from the results. // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
$coorStr = $stop['stop_lon'] . ',' . $stop['stop_lat']; $coorStr = $stop['stop_lon'] . ',' . $stop['stop_lat'];
$coorNode = $dom->createElement('coordinates', $coorStr); $coorNode = $dom->createElement('coordinates', $coorStr);
$pointNode->appendChild($coorNode); $pointNode->appendChild($coorNode);
} }
$kmlOutput = $dom->saveXML(); $kmlOutput = $dom->saveXML();
echo $kmlOutput; echo $kmlOutput;
?> ?>
   
file:b/geo/trip.kml.php (new)
  <?php
 
  header('Content-Type: application/vnd.google-earth.kml+xml');
  include ('../include/common.inc.php');
  echo '<?xml version="1.0" encoding="UTF-8"?>
  <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>';
  echo '
  <Style id="yellowLineGreenPoly">
  <LineStyle>
  <color>7f00ff00</color>
  <width>4</width>
  </LineStyle>
  <PolyStyle>
  <color>7f00ffff</color>
  </PolyStyle>
  </Style>';
  $trip = getTrip($tripid);
  echo "\n<Placemark>\n";
  $link = curPageURL() . "/../trip.php?tripid=" . htmlspecialchars($$tripid);
  echo "<name>" . $tripid . "</name>";
  echo '<atom:link href="' . $link . '"/>';
  echo '<description><![CDATA[ <a href="' . $link . '">' . $tripid . "</a>]]> </description>";
  echo "<styleUrl>#yellowLineGreenPoly</styleUrl>";
 
 
  echo getTripShape($tripid);
 
  echo "</Placemark>\n</Document></kml>\n";
  ?>
 
 
<?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 getPage($url) { function getPage($url) {
debug($url, "json"); debug($url, "json");
$ch = curl_init($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_TIMEOUT, 45); curl_setopt($ch, CURLOPT_TIMEOUT, 45);
$page = curl_exec($ch); $page = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
echo "<font color=red> Database temporarily unavailable: "; echo "<font color=red> Database temporarily unavailable: ";
echo curl_errno($ch) . " " . curl_error($ch); echo curl_errno($ch) . " " . curl_error($ch);
if (isDebug()) { if (isDebug()) {
echo $url; echo $url;
} }
echo "</font><br>"; echo "</font><br>";
} }
curl_close($ch); curl_close($ch);
debug(print_r($page, true), "json"); debug(print_r($page, true), "json");
return $page; return $page;
} }
  function baseURL() {
  $protocol = $_SERVER['HTTPS'] ? "https" : "http";
  return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   
  }
function curPageURL() { function curPageURL() {
$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"); $isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443"))); $port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
$port = ($port) ? ':' . $_SERVER["SERVER_PORT"] : ''; $port = ($port) ? ':' . $_SERVER["SERVER_PORT"] : '';
$url = ($isHTTPS ? 'https://' : 'http://') . $_SERVER["SERVER_NAME"] . $port . htmlentities(dirname($_SERVER['PHP_SELF']), ENT_QUOTES); $url = ($isHTTPS ? 'https://' : 'http://') . $_SERVER["SERVER_NAME"] . $port . htmlentities(dirname($_SERVER['PHP_SELF']), ENT_QUOTES);
return $url; return $url;
} }
   
?> ?>
   
file:a/trip.php -> file:b/trip.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');
$routetrips = Array(); $routetrips = Array();
if (isset($routeid) && !isset($tripid)) { if (isset($routeid) && !isset($tripid)) {
$trip = getRouteNextTrip($routeid,$directionid); $trip = getRouteNextTrip($routeid,$directionid);
if (!($trip)) { if (!($trip)) {
$trip = getRouteFirstTrip($routeid,$directionid); $trip = getRouteFirstTrip($routeid,$directionid);
} }
$tripid = $trip['trip_id']; $tripid = $trip['trip_id'];
} else { } else {
$trip = getTrip($tripid); $trip = getTrip($tripid);
$routeid = $trip['route_id']; $routeid = $trip['route_id'];
} }
$directionid = $trip['direction_id']; $directionid = $trip['direction_id'];
$service_period = strtolower($trip["service_id"]); $service_period = strtolower($trip["service_id"]);
$destination = getTripDestination($trip['trip_id']); $destination = getTripDestination($trip['trip_id']);
include_header("Stops on " . $trip['route_short_name'] . ' ' . $destination['stop_name'], "trip"); include_header("Stops on " . $trip['route_short_name'] . ' ' . $destination['stop_name'], "trip");
trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $destination['stop_name'], $routeid); trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $destination['stop_name'], $routeid);
echo '<span class="content-secondary">'; echo '<span class="content-secondary">';
echo '<a href="' . $trip['route_url'] . '">View Original Timetable/Map</a>'; echo '<a href="' . $trip['route_url'] . '">View Original Timetable/Map</a> ';
  echo '<a href="geo/trip.kml.php?tripid='.$tripid.'">View Trip in Google Earth</a> ';
  echo '<a href="geo/route.kml.php?routeid='.$tripid.'">View Route in Google Earth</a>';
echo '<h2>Via:</h2> <small>' . viaPointNames($tripid) . '</small>'; echo '<h2>Via:</h2> <small>' . viaPointNames($tripid) . '</small>';
echo '<h2>Other Trips:</h2> '; echo '<h2>Other Trips:</h2> ';
$routeTrips = getRouteTrips($routeid, $trip['direction_id'], $service_period); $routeTrips = getRouteTrips($routeid, $trip['direction_id'], $service_period);
foreach ($routeTrips as $key => $othertrip) { foreach ($routeTrips as $key => $othertrip) {
// if ($othertrip['trip_id'] != $tripid) { // if ($othertrip['trip_id'] != $tripid) {
echo '<a href="trip.php?tripid=' . $othertrip['trip_id'] . "&amp;routeid=" . $routeid . '">' . str_replace(" ", ":00", str_replace(":00", " ", $othertrip['arrival_time'])) . '</a> '; echo '<a href="trip.php?tripid=' . $othertrip['trip_id'] . "&amp;routeid=" . $routeid . '">' . str_replace(" ", ":00", str_replace(":00", " ", $othertrip['arrival_time'])) . '</a> ';
// } else { // } else {
// skip this trip but look forward/back // skip this trip but look forward/back
if ($key - 1 > 0) if ($key - 1 > 0)
$prevTrip = $routeTrips[$key - 1]['trip_id']; $prevTrip = $routeTrips[$key - 1]['trip_id'];
if ($key + 1 < sizeof($routeTrips)) if ($key + 1 < sizeof($routeTrips))
$nextTrip = $routeTrips[$key + 1]['trip_id']; $nextTrip = $routeTrips[$key + 1]['trip_id'];
// } // }
} }
flush(); flush();
@ob_flush(); @ob_flush();
echo '<h2>Other directions/timing periods:</h2> '; echo '<h2>Other directions/timing periods:</h2> ';
$otherDir = 0; $otherDir = 0;
   
foreach (getRouteHeadsigns($routeid) as $headsign) { foreach (getRouteHeadsigns($routeid) as $headsign) {
if ($headsign['direction_id'] != $directionid || strtolower($headsign['service_id']) != $service_period) { if ($headsign['direction_id'] != $directionid || strtolower($headsign['service_id']) != $service_period) {
   
echo '<a href="trip.php?routeid=' . $routeid . '&directionid=' . $headsign['direction_id'] . '&service_period=' . $headsign['service_id'] . '"> Starting at ' . $headsign['stop_name'] . ' (' . $headsign['service_id'] . ')</a> '; echo '<a href="trip.php?routeid=' . $routeid . '&directionid=' . $headsign['direction_id'] . '&service_period=' . $headsign['service_id'] . '"> Starting at ' . $headsign['stop_name'] . ' (' . $headsign['service_id'] . ')</a> ';
$otherDir++; $otherDir++;
} }
} }
   
if ($otherDir == 0) { if ($otherDir == 0) {
echo "None"; echo "None";
} }
echo '</span><span class="content-primary">'; echo '</span><span class="content-primary">';
flush(); flush();
@ob_flush(); @ob_flush();
echo "<div class='ui-header' style='overflow: visible; height: 1.5em'>"; echo "<div class='ui-header' style='overflow: visible; height: 1.5em'>";
if ($nextTrip) if ($nextTrip)
echo '<a href="trip.php?tripid=' . $nextTrip . "&amp;routeid=" . $routeid . '" data-icon="arrow-r" class="ui-btn-right">Next Trip</a>'; echo '<a href="trip.php?tripid=' . $nextTrip . "&amp;routeid=" . $routeid . '" data-icon="arrow-r" class="ui-btn-right">Next Trip</a>';
if ($prevTrip) if ($prevTrip)
echo '<a href="trip.php?tripid=' . $prevTrip . "&amp;routeid=" . $routeid . '" data-icon="arrow-l" class="ui-btn-left">Previous Trip</a>'; echo '<a href="trip.php?tripid=' . $prevTrip . "&amp;routeid=" . $routeid . '" data-icon="arrow-l" class="ui-btn-left">Previous Trip</a>';
echo "</div>"; echo "</div>";
echo ' <ul data-role="listview" data-inset="true">'; echo ' <ul data-role="listview" data-inset="true">';
$stopsGrouped = Array(); $stopsGrouped = Array();
$tripStopTimes = getTripStopTimes($tripid); $tripStopTimes = getTripStopTimes($tripid);
echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' towards ' . $destination['stop_name'] . ' (' . ucwords(strtolower($tripStopTimes[0]['service_id'])) . ')</li>'; echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' towards ' . $destination['stop_name'] . ' (' . ucwords(strtolower($tripStopTimes[0]['service_id'])) . ')</li>';
foreach ($tripStopTimes as $key => $tripStopTime) { foreach ($tripStopTimes as $key => $tripStopTime) {
if ($key + 1 > sizeof($tripStopTimes) || stopCompare($tripStopTimes[$key]["stop_name"]) != stopCompare($tripStopTimes[$key + 1]["stop_name"])) { if ($key + 1 > sizeof($tripStopTimes) || stopCompare($tripStopTimes[$key]["stop_name"]) != stopCompare($tripStopTimes[$key + 1]["stop_name"])) {
echo '<li>'; echo '<li>';
   
if (sizeof($stopsGrouped) > 0) { if (sizeof($stopsGrouped) > 0) {
// print and empty grouped stops // print and empty grouped stops
// subsequent duplicates // subsequent duplicates
$stopsGrouped["stop_ids"][] = $tripStopTime['stop_id']; $stopsGrouped["stop_ids"][] = $tripStopTime['stop_id'];
$stopsGrouped["endTime"] = $tripStopTime['arrival_time']; $stopsGrouped["endTime"] = $tripStopTime['arrival_time'];
echo '<a href="stop.php?stopids=' . implode(",", $stopsGrouped['stop_ids']) . '">'; echo '<a href="stop.php?stopids=' . implode(",", $stopsGrouped['stop_ids']) . '">';
echo '<p class="ui-li-aside">' . $stopsGrouped['startTime'] . ' to ' . $stopsGrouped['endTime']; echo '<p class="ui-li-aside">' . $stopsGrouped['startTime'] . ' to ' . $stopsGrouped['endTime'];
if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) { if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) {
echo '<br>' . distance($tripStopTime['stop_lat'], $tripStopTime['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away'; echo '<br>' . distance($tripStopTime['stop_lat'], $tripStopTime['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away';
} }
echo '</p>'; echo '</p>';
echo stopGroupTitle($tripStopTime['stop_name'], $tripStopTime['stop_desc']) . '<br><small>' . sizeof($stopsGrouped["stop_ids"]) . ' stops</small>'; echo stopGroupTitle($tripStopTime['stop_name'], $tripStopTime['stop_desc']) . '<br><small>' . sizeof($stopsGrouped["stop_ids"]) . ' stops</small>';
   
echo '</a></li>'; echo '</a></li>';
flush(); flush();
@ob_flush(); @ob_flush();
$stopsGrouped = Array(); $stopsGrouped = Array();
} else { } else {
// just a normal stop // just a normal stop
echo '<a href="stop.php?stopid=' . $tripStopTime['stop_id'] . (startsWith($tripStopTime['stop_code'], "Wj") ? '&amp;stopcode=' . $tripStopTime['stop_code'] : "") . '">'; echo '<a href="stop.php?stopid=' . $tripStopTime['stop_id'] . (startsWith($tripStopTime['stop_code'], "Wj") ? '&amp;stopcode=' . $tripStopTime['stop_code'] : "") . '">';
echo '<p class="ui-li-aside">' . $tripStopTime['arrival_time']; echo '<p class="ui-li-aside">' . $tripStopTime['arrival_time'];
if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) { if (isset($_SESSION['lat']) && isset($_SESSION['lon'])) {
echo '<br>' . distance($tripStopTime['stop_lat'], $tripStopTime['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away'; echo '<br>' . distance($tripStopTime['stop_lat'], $tripStopTime['stop_lon'], $_SESSION['lat'], $_SESSION['lon'], true) . 'm away';
} }
echo '</p>'; echo '</p>';
echo $tripStopTime['stop_name']; echo $tripStopTime['stop_name'];
echo '</a></li>'; echo '</a></li>';
flush(); flush();
@ob_flush(); @ob_flush();
} }
} else { } else {
// this is a duplicated line item // this is a duplicated line item
if ($key - 1 <= 0 || stopCompare($tripStopTimes[$key]['stop_name']) != stopCompare($tripStopTimes[$key - 1]['stop_name'])) { if ($key - 1 <= 0 || stopCompare($tripStopTimes[$key]['stop_name']) != stopCompare($tripStopTimes[$key - 1]['stop_name'])) {
// first duplicate // first duplicate
$stopsGrouped = Array( $stopsGrouped = Array(
"name" => trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])), "name" => trim(preg_replace("/\(Platform.*/", "", $stop['stop_name'])),
"startTime" => $tripStopTime['arrival_time'], "startTime" => $tripStopTime['arrival_time'],
"stop_ids" => Array( "stop_ids" => Array(
$tripStopTime['stop_id'] $tripStopTime['stop_id']
) )
); );
} else { } else {
// subsequent duplicates // subsequent duplicates
$stopsGrouped["stop_ids"][] = $tripStopTime['stop_id']; $stopsGrouped["stop_ids"][] = $tripStopTime['stop_id'];
$stopsGrouped["endTime"] = $tripStopTime['arrival_time