CartoDB is used to create an interactive map showing the nearest RTD transit locations and intersecting routes in Denver based on a user's location and destination. Shapefiles of RTD bus routes and stops are loaded into CartoDB along with Leaflet and other open source software. SQL queries retrieve nearest stops and routes between stops allowing users to plan trips using real-time transit data. While some queries have issues, the demo shows promise in providing an informative, fast tool for transit users.
2. Key Points
• Introduce CartoDB
• Explain about CartoCSS
• SQL via CartoDB API's
• Telling your story
3. CartoDB
• Geospatial mapping
• Data stored in the cloud
• PostGIS engine
• Supports CartoCSS
• SQL API
• Leaflet and Google Maps Libraries
• Graphical Editor for Point & Polygons
• Lots of Examples
• Free 5MB account
• FAST!!!
www.cartodb.com
4. RTD Transit Data Example
Goal: To create an interactive map to get the
nearest location from where I am at and to find
any intersecting routes based on my
destination.
Requirements:
• Use open data
• Use open source software
• Real Time Interaction
• Enjoyable to users
• Make it informative and Fast!!!
13. SQL Queries
# All stops for a single route
SELECT * FROM
(SELECT * FROM
(SELECT replace(regexp_split_to_table(routes, E','), ' ', '') as
new_route, bsid, the_geom FROM busstops) as a
WHERE a.new_route='6') as b
Regexp_split_to_table ref: http://www.postgresql.org/docs/8.3/static/functions-matching.html
Example: “6, 12, 8, 112, 104” will be transformed to individual rows.
14. SQL Queries
#closest bus stops
SELECT * FROM busstops
ORDER BY the_geom <-> st_setsrid(st_makepoint(-106, 39),4326)
LIMIT 5
Indexed Nearest Neighbor Search
• Using the <-> operator, you get the nearest neighbor using the centers of
the bounding boxes to calculate the inter-object distances.
• Using the <#> operator, you get the nearest neighbor using the bounding
boxes themselves to calculate the inter-object distances.
Ref: http://blog.opengeo.org/tag/knn/
WGS 84 (4326) http://spatialreference.org/ref/epsg/4326/
15. SQL Queries
# returns a row if the bus stops are on the same route
SELECT c.* FROM busroutes c JOIN
(SELECT a.new_route, a.bsid, b.bsid FROM
(SELECT replace(regexp_split_to_table(routes, E','), ' ', '') as
new_route, bsid, the_geom FROM busstops WHERE bsid =
'{{{START STOP ID}}}') as a
JOIN (SELECT replace(regexp_split_to_table(routes, E','), ' ', '') as
new_route, bsid, the_geom FROM busstops WHERE bsid = '{{{END
STOP ID}}}') as b ON a.bsid = b.bsid) as d
ON d.new_route = c.route
How to write a query to graph routes for 1st or 2nd degree route. Bus transfers!!!
16. Failed Queries???
#Only show the line between two points that fall on the bus route
SELECT st_transform( ST_Line_Substring(the_geom,
(SELECT ST_Line_Locate_Point(
(SELECT ST_GeometryN(the_geom,5) FROM busroutes WHERE route='6')
,
(SELECT the_geom FROM (SELECT replace(regexp_split_to_table(routes, E','), ' ', '') as new_route,
bsid, the_geom FROM busstops WHERE bsid = 12978) as a WHERE a.new_route='6'
))),
(SELECT ST_Line_Locate_Point(
(SELECT ST_GeometryN(the_geom,5) FROM busroutes WHERE route='6'),
(SELECT the_geom FROM (SELECT replace(regexp_split_to_table(routes, E','), ' ', '') as new_route,
bsid, the_geom FROM busstops WHERE bsid = 10227) as a WHERE a.new_route='6'
)))), 3857) as the_geom_webmercator
FROM busroutes WHERE route = '6‘
17. Better Methods & Missing API’s
• PgRoute http://pgrouting.org/
• API for route time tables
• Preferred Transfer locations