Correctly identify direction of each trip by it's final stop
[busui.git] / labs / stopBrowser.kml.php
blob:a/labs/stopBrowser.kml.php -> blob:b/labs/stopBrowser.kml.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.
*/ */
header('Content-type: application/vnd.google-earth.kml+xml'); 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://earth.google.com/kml/2.1', '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);
   
   
$bbox = $_GET['bbox']; // get the bbox param from google earth $bbox = $_GET['bbox']; // get the bbox param from google earth
list($bbox_south, $bbox_west, $bbox_north, $bbox_east) = explode(",", $bbox); // west, south, east, north list($bbox_south, $bbox_west, $bbox_north, $bbox_east) = explode(",", $bbox); // west, south, east, north
   
include ('../include/common.inc.php'); include ('../include/common.inc.php');
$debugOkay = Array(); $debugOkay = Array();
$contents = getNearbyStops((($bbox_west + $bbox_east) / 2), ($bbox_south + $bbox_north) / 2, 50, 3000); $contents = getNearbyStops((($bbox_west + $bbox_east) / 2), ($bbox_south + $bbox_north) / 2, 50, 3000);
foreach ($contents as $stop) { foreach ($contents as $stop) {
$description = 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $stop['stop_id'] . " <br>"; $description = 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $stop['stop_id'] . " <br>";
$trips = getStopTripsWithTimes($stop['stop_id'], "", "", "", 3); $trips = getStopTripsWithTimes($stop['stop_id'], "", "", "", 3);
if ($trips) { if ($trips) {
foreach ($trips as $key => $row) { foreach ($trips as $key => $row) {
if ($key < 3) { if ($key < 3) {
$description .= $row['route_short_name'] . ' ' . $row['route_long_name'] . ' @ ' . $row['arrival_time'] . "<br>"; $destination = getTripDestination($row['trip_id']);
  $description .= $row['route_short_name'] . ' ' . $destination['stop_name'] . ' @ ' . $row['arrival_time'] . "<br>";
} }
} }
} else { } else {
$description .= "No more trips today"; $description .= "No more trips today";
} }
// 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;
?> ?>