Correctly identify direction of each trip by it's final stop
Correctly identify direction of each trip by it's final stop

--- a/aws/transitdata.sql
+++ b/aws/transitdata.sql
@@ -13382,7 +13382,8 @@
     shape_pt_lat double precision,
     shape_pt_lon double precision,
     shape_pt_sequence integer NOT NULL,
-    shape_dist_traveled integer
+    shape_dist_traveled integer,
+    shape_pt geography,
 );
 
 

--- a/include/db/trip-dao.inc.php
+++ b/include/db/trip-dao.inc.php
@@ -120,6 +120,23 @@
     return $r['arrival_time'];
 }
 
+function getTripDestination($tripID) {
+    global $conn;
+    $query = "SELECT stops.stop_id, stops.stop_name, stops.stop_desc 
+        from stop_times inner join stops on stop_times.stop_id =  stops.stop_id
+	WHERE trip_id = :tripID order by stop_sequence desc limit 1";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":tripID", $tripID);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    $r = $query->fetch(PDO :: FETCH_ASSOC);
+    return $r;
+}
+
 function getActiveTrips($time) {
     global $conn;
     if ($time == "")

--- a/labs/stopBrowser.kml.php
+++ b/labs/stopBrowser.kml.php
@@ -41,7 +41,8 @@
     if ($trips) {
         foreach ($trips as $key => $row) {
             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 {

--- a/layar_api.php
+++ b/layar_api.php
@@ -45,7 +45,8 @@
         $trips = getStopTripsWithTimes($stop['stop_id'], "", "", "", 3);
         foreach ($trips as $key => $row) {
             if ($key < 3) {
-                $hotspot['line' . strval($key + 2)] = $row['route_short_name'] . ' ' . $row['route_long_name'] . ' @ ' . $row['arrival_time'];
+                $destination = getTripDestination($row['trip_id']);
+                $hotspot['line' . strval($key + 2)] = $row['route_short_name'] . ' ' . $destination['stop_name'] . ' @ ' . $row['arrival_time'];
             }
         }
         if (sizeof($trips) == 0)

file:a/stop.php -> file:b/stop.php
--- a/stop.php
+++ b/stop.php
@@ -133,7 +133,9 @@
 } else {
     foreach ($trips as $trip) {
         echo '<li>';
-        echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " " . $trip['route_long_name'] . "</h3><p>";
+        
+                $destination = getTripDestination($trip['trip_id']);
+        echo '<a href="trip.php?stopid=' . $stopid . '&amp;tripid=' . $trip['trip_id'] . '"><h3>' . $trip['route_short_name'] . " " . $destination['stop_name'] . "</h3><p>";
         $viaPoints = viaPointNames($trip['trip_id'], $trip['stop_sequence']);
         if ($viaPoints != "")
             echo '<br><span class="viaPoints">Via: ' . $viaPoints . '</span>';

file:a/trip.php -> file:b/trip.php
--- a/trip.php
+++ b/trip.php
@@ -24,8 +24,10 @@
     $trip = getTrip($tripid);
     $routeid = $trip["route_id"];
 }
-include_header("Stops on " . $trip['route_short_name'] . ' ' . $trip['route_long_name'], "trip");
-trackEvent("Route/Trip View", "View Route", $trip['route_short_name'] . ' ' . $trip['route_long_name'], $routeid);
+
+                $destination = getTripDestination($trip['trip_id']);
+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);
 echo '<span class="content-secondary">';
 echo '<a href="' . $trip['route_url'] . '">View Original Timetable/Map</a>';
 echo '<h2>Via:</h2> <small>' . viaPointNames($tripid) . '</small>';
@@ -66,7 +68,7 @@
 echo '  <ul data-role="listview"  data-inset="true">';
 $stopsGrouped = Array();
 $tripStopTimes = getTripStopTimes($tripid);
-echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' ' . $trip['route_long_name'] . ' (' . ucwords($tripStopTimes[0]['service_id']) . ')</li>';
+echo '<li data-role="list-divider">' . $tripStopTimes[0]['arrival_time'] . ' to ' . $tripStopTimes[sizeof($tripStopTimes) - 1]['arrival_time'] . ' ' . $destination['stop_name'] . ' (' . ucwords($tripStopTimes[0]['service_id']) . ')</li>';
 foreach ($tripStopTimes as $key => $tripStopTime) {
     if ($key + 1 > sizeof($tripStopTimes) || stopCompare($tripStopTimes[$key]["stop_name"]) != stopCompare($tripStopTimes[$key + 1]["stop_name"])) {
         echo '<li>';

--- a/updatedb.php
+++ b/updatedb.php
@@ -30,10 +30,11 @@
       delete from stops;
       delete from trips;
      */
+
 // Unzip cbrfeed.zip, import all csv files to database
-    $unzip = true;
+    $unzip = false;
     $zip = zip_open(dirname(__FILE__) . "/cbrfeed.zip");
-    $tmpdir = "/tmp/cbrfeed/";
+    $tmpdir = "c:/tmp/";
     mkdir($tmpdir);
     if ($unzip) {
         if (is_resource($zip)) {
@@ -68,7 +69,10 @@
                 $stmt->bindParam(':departure_time', $time);
             }
 
-
+            $distance = 0;
+            $lastshape = 0;
+            $lastlat = 0;
+            $lastlon = 0;
             while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 if ($line == 0) {
                     
@@ -79,8 +83,19 @@
                         $query.=($valueCount > 0 ? "','" : "'") . pg_escape_string($value);
                         $valueCount++;
                     }
+
                     if ($tablename == "stops") {
                         $query.= "', ST_GeographyFromText('SRID=4326;POINT({$data[2]} {$data[0]})'));";
+                    } else if ($tablename == "shapes") {
+                        if ($data[0] != $lastshape) {
+                            $distance = 0;
+                            $lastshape = $data[0];
+                        } else {
+                            $distance += distance($lastlat, $lastlon, $data[1], $data[2]);
+                        }
+                        $lastlat = $data[1];
+                        $lastlon = $data[2];
+                        $query.= "', $distance,  ST_GeographyFromText('SRID=4326;POINT({$data[2]} {$data[1]})'));";
                     } else {
                         $query.= "');";
                     }