Skip to content

Commit

Permalink
Created Chennai rail demo; Updated nonsc code
Browse files Browse the repository at this point in the history
  • Loading branch information
justjkk committed Aug 21, 2011
1 parent 730120b commit a0c2cae
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
7 changes: 7 additions & 0 deletions chennai_rail_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def find_nearest_chennai_rail_stop(cur, lonlat):
point = "POINT(" + lonlat.replace(',', ' ') + ")"
cur.execute("select stop_id from chennai_rail.stops where ST_DWithin(the_geom,st_geomfromtext(%s,4326),0.036) order by ST_Distance(the_geom, st_geomfromtext(%s,4326)) limit 1", (point, point))
row = cur.fetchone()
if row != None:
return row[0]
return None
25 changes: 14 additions & 11 deletions mtc_nonsc_dataloader/loader.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ drop schema MTC_NonSc cascade;

create schema MTC_NonSc;

CREATE DOMAIN MTC_NonSc.wgs84_lat AS double precision CHECK(VALUE >= -90 AND VALUE <= 90);
CREATE DOMAIN MTC_NonSc.wgs84_lon AS double precision CHECK(VALUE >= -180 AND VALUE <= 180);

create table MTC_NonSc.Stops
(
stop_id text primary key,
stop_name text,
stop_lat wgs84_lat not null,
stop_lon wgs84_lon not null
stop_id text primary key,
stop_name text,
stop_lat MTC_NonSc.wgs84_lat not null,
stop_lon MTC_NonSc.wgs84_lon not null
);

create table MTC_NonSc.Trips
Expand All @@ -18,18 +21,18 @@ create table MTC_NonSc.Trips

create table MTC_NonSc.Stop_Times
(
trip_id text not null references MTC_NonSc.Trips,
stop_id text not null references MTC_NonSc.Stops,
arrival_time gtfstime not null,
departure_time gtfstime not null,
trip_id text not null references MTC_NonSc.Trips,
stop_id text not null references MTC_NonSc.Stops,
arrival_time interval not null,
departure_time interval not null,
stop_sequence integer not null
);

create table MTC_NonSc.Frequencies
(
trip_id text not null,
start_time gtfstime not null,
end_time gtfstime not null,
start_time interval not null,
end_time interval not null,
headway_secs integer not null
);

Expand Down Expand Up @@ -57,7 +60,7 @@ delete from MTC_Nonsc.Frequencies where trip_id not in (select trip_id from MTC_

-- Adding Stop_Times from StopTimes
insert into MTC_NonSc.Stop_Times(trip_id, stop_id, arrival_time, departure_time, stop_sequence)
select trip_id, stop_id, secs_to_gtfstime(stop_time), secs_to_gtfstime(stop_time), sequence from StopTimes;
select trip_id, stop_id, stop_time * '1 second'::INTERVAL, stop_time * '1 second'::INTERVAL, sequence from StopTimes;

-- Adding GIS column to Stops
select AddGeometryColumn('mtc_nonsc', 'stops', 'the_geom', 4326, 'POINT', 2);
Expand Down
60 changes: 59 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from dbhelper import connect_to_DB, find_nearest_node
from mtchelper import find_nearest_mtc_stage
from mtc_nonsc_helper import find_nearest_mtc_nonsc_stop
from chennai_rail_helper import find_nearest_chennai_rail_stop
from datetime import datetime, timedelta

app = Flask(__name__)
app.config.from_object('configuration')
Expand Down Expand Up @@ -89,7 +91,7 @@ def mtc_nonsc_route_kml():
finish_stage = find_nearest_mtc_nonsc_stop(g.cur, finish_location)
g.cur.execute(" \
SELECT changeover_id FROM non_scheduled_route( \
'%s', '%s', '%s')" % ('MTC_NonSc', start_stage, finish_stage))
'%s', '%s', '%s')" % ('mtc_nonsc', start_stage, finish_stage))
changeovers = [str(x[0]) for x in g.cur.fetchall()]
if len(changeovers) == 0:
return render_template('route.kml', kml_data=None)
Expand Down Expand Up @@ -139,6 +141,58 @@ def mtc_nonsc_route_kml():

return render_template('route.kml', kml_data=kml_data + kml_data2)

def Placemark(label, kml_text):
return "<Placemark>\n<label>" + escape(label) + "</label>\n" \
+ kml_text + "\n</Placemark>"

@app.route("/chennai-rail_route.kml")
def chennai_rail_route_kml():
if not "start_location" in request.args or not "finish_location" in request.args:
return render_template('route.kml', kml_data=None)
start_location = request.args["start_location"]
finish_location = request.args["finish_location"]
start_stop = find_nearest_chennai_rail_stop(g.cur, start_location)
finish_stop = find_nearest_chennai_rail_stop(g.cur, finish_location)
query_time = datetime.now()
g.cur.execute(" \
SELECT stop_id, trip_id, waiting_time, travel_time, ST_AsKML(the_point) \
FROM gtfs_route_with_schema( 'chennai_rail', '%s', '%s', '%s')" % (
start_stop,
finish_stop,
query_time.isoformat()))
route_data = g.cur.fetchall()
if len(route_data) == 0:
return render_template('route.kml', kml_data=None)
kml_rows = []
kml_data2 = ''
stop_time = query_time
for row_data in route_data[:-1]:
g.cur.execute("""
SELECT
ST_ASKML(ST_Makeline(iq.the_geom))
FROM (SELECT st.trip_id, s.the_geom
FROM
chennai_rail.Stops s,
chennai_rail.Stop_Times st
WHERE s.stop_id = st.stop_id
AND st.trip_id = '%s'
ORDER BY st.stop_sequence) AS iq
GROUP BY iq.trip_id
""" % (row_data[1])
)
trip_geom = g.cur.fetchone()[0]
kml_rows.append(("%s(waiting=%s, travel=%s)" % (row_data[1], row_data[2], row_data[3]), trip_geom))
stop_label = "%s %s" %(row_data[0], stop_time.strftime('%d-%b-%Y \
%H:%M:%S'))
stop_time += row_data[2] + row_data[3]
kml_data2 += Placemark(stop_label, row_data[4])
stop_label = "%s %s" %(route_data[-1][0], stop_time.strftime('%d-%b-%Y \
%H:%M:%S'))
kml_data2 += Placemark(stop_label, route_data[-1][4])
kml_data = '\n'.join([Placemark(x[0], x[1]) for x in kml_rows])

return render_template('route.kml', kml_data=kml_data + kml_data2)

@app.route("/routing.js", methods=["GET"])
def routing_js():
if not "start_location" in request.args or not "finish_location" in request.args:
Expand All @@ -164,5 +218,9 @@ def mtc_dijkstra():
def mtc_nonsc():
return render_template('mtc-nonsc.html')

@app.route("/chennai-rail")
def chennai_rail():
return render_template('chennai-rail.html')

if __name__ == "__main__":
app.run()
Binary file added static/img/thumbs/chennai-rail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function blocklink() {
$("#mtc-nonsc").click(function () {
window.location.href = "/mtc-nonsc";
});
$("#chennai-rail").click(function () {
window.location.href = "/chennai-rail";
});
}

$(document).ready(function () {
Expand Down
2 changes: 2 additions & 0 deletions templates/chennai-rail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends "map.html" %}
{% block routing_url %}"/chennai-rail_route.kml"{% endblock %}
5 changes: 5 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
<header>MTC - nonsc</header>
<img src="/static/img/thumbs/mtc-nonsc.png" alt="MTC - Nonscheduled" width="200" height="150" />
</article>
<article id="chennai-rail">
<header>Chennai rail</header>
<img src="/static/img/thumbs/chennai-rail.png" alt="Chennai Rail" width="200"
height="150" />
</article>
</section>
{% endblock %}

0 comments on commit a0c2cae

Please sign in to comment.