Beginnings of new route KML
Beginnings of new route KML

--- a/aws/busuidb.sh
+++ b/aws/busuidb.sh
@@ -11,4 +11,7 @@
 #psql -d transitdata -c "GRANT SELECT,INSERT ON	TABLE myway_observations,myway_routes,myway_stops,myway_timingdeltas TO transitdata;"
 #psql -d transitdata -c	"GRANT SELECT,INSERT,UPDATE ON TABLE myway_routes,myway_stops TO transitdata;"
 ##psql -d transitdata -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO transitdata;"
+## INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
+##SELECT '', 'public', 'shapes', 'shape_pt', ST_CoordDim(shape_pt), ST_SRID(shape_pt), GeometryType(shape_pt)
+##FROM shapes LIMIT 1;
 php /var/www/updatedb.php

--- 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
@@ -33,17 +33,31 @@
     }
     return $query->fetch(PDO :: FETCH_ASSOC);
 }
-
-function getTripShape($tripID) {
-    // todo, use shapes table if shape_id specified
+function getTripStops($tripID) {
     global $conn;
-    $query = "SELECT ST_AsKML(ST_MakeLine(geometry(a.position))) as the_route
-FROM (SELECT position,
+    $query = "SELECT stop_id, stop_name, ST_AsKML(position) as positionkml,
 	stop_sequence, trips.trip_id
 FROM stop_times
 join trips on trips.trip_id = stop_times.trip_id
 join stops on stops.stop_id = stop_times.stop_id
-WHERE trips.trip_id = :tripID ORDER BY stop_sequence) as a group by a.trip_id";
+WHERE trips.trip_id = :tripID ORDER BY stop_sequence";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":tripID", $tripID);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    return $query->fetchColumn(0);
+}
+function getTripShape($tripID) {
+    // todo, use shapes table if shape_id specified
+    global $conn;
+    $query = "SELECT ST_AsKML(ST_MakeLine(geometry(a.shape_pt))) as the_route
+FROM (SELECT shapes.shape_id,shape_pt from shapes
+inner join trips on shapes.shape_id = trips.shape_id
+WHERE trips.trip_id = :tripID ORDER BY shape_pt_sequence) as a group by a.shape_id";
     debug($query, "database");
     $query = $conn->prepare($query);
     $query->bindParam(":tripID", $tripID);

--- 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.= "');";
                     }