Refactor to use PostGIS database instead of gtfs tools
[busui.git] / include / db / route-dao.inc.php
blob:a/include/db/route-dao.inc.php -> blob:b/include/db/route-dao.inc.php
<?php <?php
   
function getRoute($routeID) { function getRoute($routeID) {
/* $query = "Select * from routes where route_id = '$routeID' LIMIT 1";
def handle_json_GET_routerow(self, params): debug($query,"database");
schedule = self.server.schedule $result = pg_query($conn, $query);
route = schedule.GetRoute(params.get('route', None)) if (!$result) {
return [transitfeed.Route._FIELD_NAMES, route.GetFieldValuesTuple()] databaseError(pg_result_error($result));
*/ return Array();
  }
  return pg_fetch_assoc($result);
} }
function getRoutes() { function getRoutes() {
/* def handle_json_GET_routes(self, params): global $conn;
"""Return a list of all routes.""" $query = "Select * from routes order by route_short_name;";
schedule = self.server.schedule debug($query,"database");
result = [] $result = pg_query($conn, $query);
for r in schedule.GetRouteList(): if (!$result) {
servicep = None databaseError(pg_result_error($result));
for t in schedule.GetTripList(): return Array();
if t.route_id == r.route_id: }
servicep = t.service_period return pg_fetch_all($result);
break  
result.append( (r.route_id, r.route_short_name, r.route_long_name, servicep.service_id) )  
result.sort(key = lambda x: x[1:3])  
return result  
*/  
} }
   
function findRouteByNumber($routeNumber) { function getRoutesByNumber($routeNumber = "") {
/* global $conn;
def handle_json_GET_routesearch(self, params): if ($routeNumber != "") {
"""Return a list of routes with matching short name.""" $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes join trips on trips.route_id =
schedule = self.server.schedule routes.route_id join stop_times on stop_times.trip_id = trips.trip_id where route_short_name = '$routeNumber' order by route_short_name;";
routeshortname = params.get('routeshortname', None) } else {
result = [] $query = "SELECT DISTINCT route_short_name from routes order by route_short_name";
for r in schedule.GetRouteList(): }
if r.route_short_name == routeshortname: debug($query,"database");
servicep = None $result = pg_query($conn, $query);
for t in schedule.GetTripList(): if (!$result) {
if t.route_id == r.route_id: databaseError(pg_result_error($result));
servicep = t.service_period return Array();
break }
result.append( (r.route_id, r.route_short_name, r.route_long_name, servicep.service_id) ) return pg_fetch_all($result);
result.sort(key = lambda x: x[1:3])  
return result  
*/  
} }
   
function getRouteNextTrip($routeID) { function getRouteNextTrip($routeID) {
/* global $conn;
def handle_json_GET_routetrips(self, params): $query = "select * from routes join trips on trips.route_id = routes.route_id
""" Get a trip for a route_id (preferablly the next one) """ join stop_times on stop_times.trip_id = trips.trip_id where
schedule = self.server.schedule arrival_time > CURRENT_TIME and routes.route_id = '$routeID' order by
query = params.get('route_id', None).lower() arrival_time limit 1";
result = [] debug($query,"database");
for t in schedule.GetTripList(): $result = pg_query($conn, $query);
if t.route_id == query: if (!$result) {
try: databaseError(pg_result_error($result));
starttime = t.GetStartTime() return Array();
except: }
print "Error for GetStartTime of trip #" + t.trip_id + sys.exc_info()[0] return pg_fetch_assoc($result);
else: }
cursor = t._schedule._connection.cursor() function getRouteTrips($routeID) {
cursor.execute( global $conn;
'SELECT arrival_secs,departure_secs FROM stop_times WHERE ' $query = "select * from routes join trips on trips.route_id = routes.route_id
'trip_id=? ORDER BY stop_sequence DESC LIMIT 1', (t.trip_id,)) join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = '$routeID' order by
(arrival_secs, departure_secs) = cursor.fetchone() arrival_time ";
if arrival_secs != None: debug($query,"database");
endtime = arrival_secs $result = pg_query($conn, $query);
elif departure_secs != None: if (!$result) {
endtime = departure_secs databaseError(pg_result_error($result));
else: return Array();
endtime =0 }
result.append ( (starttime, t.trip_id, endtime) ) return pg_fetch_all($result);
return sorted(result, key=lambda trip: trip[2]) }
*/ function getRoutesByDestination($destination = "", $service_period = "") {
  global $conn;
  if ($service_period == "") $service_period = service_period();
  if ($destination != "") {
  $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
  FROM stop_times join trips on trips.trip_id =
  stop_times.trip_id join routes on trips.route_id = routes.route_id
  WHERE route_long_name = '$destination' AND service_id='$service_period' order by route_short_name";
  } else {
  $query = "SELECT DISTINCT route_long_name
  FROM stop_times join trips on trips.trip_id =
  stop_times.trip_id join routes on trips.route_id = routes.route_id
  WHERE service_id='$service_period' order by route_long_name";
  }
  debug($query,"database");
  $result = pg_query($conn, $query);
  if (!$result) {
  databaseError(pg_result_error($result));
  return Array();
  }
  return pg_fetch_all($result);
} }
   
  function getRoutesBySuburb($suburb, $service_period = "") {
  if ($service_period == "") $service_period = service_period();
  global $conn;
  $query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name
  FROM stop_times join trips on trips.trip_id = stop_times.trip_id
  join routes on trips.route_id = routes.route_id
  join stops on stops.stop_id = stop_times.stop_id
  WHERE zone_id LIKE '%$suburb;%' AND service_id='$service_period' ORDER BY route_short_name";
  debug($query,"database");
  $result = pg_query($conn, $query);
  if (!$result) {
  databaseError(pg_result_error($result));
  return Array();
  }
  return pg_fetch_all($result);
  }
   
  function getRoutesNearby($lat, $lng, $limit = "", $distance = 500) {
   
   
  if ($service_period == "") $service_period = service_period();
  if ($limit != "") $limit = " LIMIT $limit ";
  global $conn;
  $query = "SELECT service_id,trips.route_id,route_short_name,route_long_name,
  min(ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE)) as distance
  FROM stop_times
  join trips on trips.trip_id = stop_times.trip_id
  join routes on trips.route_id = routes.route_id
  join stops on stops.stop_id = stop_times.stop_id
  WHERE service_id='$service_period'
  AND ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), $distance, FALSE)
  group by service_id,trips.route_id,route_short_name,route_long_name
  order by distance $limit";
  debug($query,"database");
  $result = pg_query($conn, $query);
  if (!$result) {
  databaseError(pg_result_error($result));
  return Array();
  }
  return pg_fetch_all($result);
  }
?> ?>