Created
October 4, 2016 07:33
-
-
Save mrdmnd/797496b7ed18655312d8afa66f262264 to your computer and use it in GitHub Desktop.
A short script to output ordered bus-route stop data for two routes in San Clemente, CA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
# Rough bounding box for San Clemente, CA | |
southwest = [33.351665, -117.779208] | |
northeast = [33.566387, -117.537489] | |
bbox_string = "%s,%s,%s,%s" % tuple(southwest[::-1]+northeast[::-1]) | |
stops = [] | |
routes = [] | |
stops_struct = {} | |
routes_struct = {} | |
# Populate general stop data from transit.land API. | |
stop_download_done = False | |
stops_url = "https://transit.land/api/v1/stops?offset=0&per_page=100&bbox=%s&served_by=o-9mu-orangecountytransportationauthority&sort_key=id&sort_order=asc" % bbox_string | |
while not stop_download_done: | |
page_content = requests.get(stops_url).json() | |
stops.extend(page_content['stops']) | |
try: | |
stops_url = page_content['meta']['next'] | |
except KeyError: | |
stop_download_done = True | |
# Populate route list: | |
route_download_done = False | |
routes_url = "https://transit.land/api/v1/routes?offset=0&per_page=100&bbox=%s&operated_by=o-9mu-orangecountytransportationauthority&sort_key=id&sort_order=asc" % bbox_string | |
while not route_download_done: | |
page_content = requests.get(routes_url).json() | |
routes.extend(page_content['routes']) | |
try: | |
routes_url = page_content['meta']['next'] | |
except KeyError: | |
route_download_done = True | |
# Prune the returned set of stops to only those on routes 191 or 193. | |
for stop in stops: | |
routes_serving_stop = stop['routes_serving_stop'] | |
for rss in routes_serving_stop: | |
if rss['route_onestop_id'] in ('r-9muq7-193', 'r-9muq-191'): | |
stops_struct[stop['onestop_id']] = (stop['name'], stop['geometry']['coordinates']) | |
break | |
# Prune the returned set of routes to only routes 191 or 193. | |
for route in routes: | |
if route['name'] in ('191', '193'): | |
stop_contents = [] | |
for stop in route['stops_served_by_route']: | |
stop_contents += (stops_struct[stop['stop_onestop_id']],) | |
routes_struct[route['name']] = (route['tags']['route_long_name'], stop_contents) | |
# Print formatted route data as tab-separated values (lat lng name) | |
print 'ROUTE DATA' | |
for route_name in routes_struct: | |
print "Route name/id: %s" % route_name | |
contents = routes_struct[route_name] | |
print "Route long name: %s" % contents[0] | |
for stop_content in stop_contents: | |
print str(stop_content[1][1]).ljust(8) + "\t" + str(stop_content[1][0]).ljust(8) + "\t" + str(stop_content[0]) | |
print "\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ROUTE DATA | |
Route name/id: 191 | |
Route long name: Mission Viejo - San Clemente ** | |
33.45707 -117.64726 CAMINO DE LOS MARES-MARBELLA | |
33.458 -117.65016 CAMINO DE LOS MARES-ENT 647 SAN CLEMENTE | |
33.45939 -117.65151 CAMINO DE LOS MARES-CALLE AGUA | |
33.46014 -117.6538 CAMINO DE LOS MARES-CAMINO EL MOLINO | |
33.45989 -117.65372 CAMINO DE LOS MARES-CAMINO EL MOLINA | |
33.4582 -117.65081 CAMINO DE LOS MARES-ENT 654 OCEAN VIEW PLAZA | |
33.45656 -117.64553 CAMINO DE LOS MARES-CALLE CAMPANA | |
33.43343 -117.63116 AVD PICO-EL CAMINO REAL | |
33.43583 -117.62731 AVD PICO-AVD VISTA HERMOSA | |
33.43643 -117.62532 AVD PICO-CALLE DE LOS MOLINOS | |
33.43264 -117.63327 AVD ESTACION-CALLE DESHECHA | |
33.43717 -117.62381 AVD PICO-VIA PICO PLAZA | |
33.44049 -117.61812 AVD PICO-ENT SAN CLEMENTE HIGH | |
33.4427 -117.61541 AVD PICO-AVD PRESIDIO | |
33.45781 -117.65717 CAMINO DE ESTRELLA-AVD LAS PALMAS | |
33.4548 -117.6593 CAMINO DE ESTRELLA-CALLE HERMOSA | |
33.45682 -117.65762 CAMINO DE ESTRELLA-CAMINO MIRA COSTA | |
33.44209 -117.6167 AVD PICO-CALLE FRONTERA | |
33.43797 -117.62364 AVD PICO-AVD NAVARRO | |
33.43657 -117.62574 AVD PICO-CALLE DE LOS MOLINOS | |
33.43577 -117.62832 AVD PICO-AVD VISTA HERMOSA | |
33.46565 -117.63965 CAMINO DE LOS MARES-CALLE NUEVO | |
33.4688 -117.63338 CAMINO VERA CRUZ-CAMINO DE LOS MARES | |
33.46564 -117.62766 CAMINO VERA CRUZ-SARMENTOSO | |
33.46 -117.61886 CAMINO VERA CRUZ-COLINA RODANTE | |
33.45771 -117.61839 CAMINO VERA CRUZ-CALLE DE LOS ARBOLES | |
33.45598 -117.61698 AVD VISTA HERMOSA-CALLE VERA CRUZ | |
33.46198 -117.60725 AVD VISTA HERMOSA-SPORTS PARK | |
33.45608 -117.60136 AVD PICO-AVD LA PATA | |
33.45452 -117.60436 AVD PICO-PLAZA PACIFICA | |
33.44975 -117.60944 AVENIDA PICO-CALLE AMANECER | |
33.45016 -117.60893 AVD PICO-CALLE AMANECER | |
33.45461 -117.60349 AVD PICO-PLAZA PACIFICA | |
33.45682 -117.6011 AVD LA PATA-AVD PICO | |
33.46185 -117.60813 AVD VISTA HERMOSA-TARGET | |
33.45641 -117.61764 CAMINO VERA CRUZ-AVD VISTA HERMOSA | |
33.4585 -117.61833 CAMINO VERA CRUZ-CAMINO DE LA LADERA | |
33.46078 -117.6189 CAMINO VERA CRUZ-MONTANA DEL SOL | |
33.46593 -117.62789 CAMINO VERA CRUZ-SARMENTOSO | |
33.46906 -117.63367 CAMINO DE LOS MARES-CAMINO VERA CRUZ | |
33.46567 -117.64 CAMINO DE LOS MARES-CALLE NUEVO | |
Route name/id: 193 | |
Route long name: San Clemente Local ** | |
33.45707 -117.64726 CAMINO DE LOS MARES-MARBELLA | |
33.458 -117.65016 CAMINO DE LOS MARES-ENT 647 SAN CLEMENTE | |
33.45939 -117.65151 CAMINO DE LOS MARES-CALLE AGUA | |
33.46014 -117.6538 CAMINO DE LOS MARES-CAMINO EL MOLINO | |
33.45989 -117.65372 CAMINO DE LOS MARES-CAMINO EL MOLINA | |
33.4582 -117.65081 CAMINO DE LOS MARES-ENT 654 OCEAN VIEW PLAZA | |
33.45656 -117.64553 CAMINO DE LOS MARES-CALLE CAMPANA | |
33.43343 -117.63116 AVD PICO-EL CAMINO REAL | |
33.43583 -117.62731 AVD PICO-AVD VISTA HERMOSA | |
33.43643 -117.62532 AVD PICO-CALLE DE LOS MOLINOS | |
33.43264 -117.63327 AVD ESTACION-CALLE DESHECHA | |
33.43717 -117.62381 AVD PICO-VIA PICO PLAZA | |
33.44049 -117.61812 AVD PICO-ENT SAN CLEMENTE HIGH | |
33.4427 -117.61541 AVD PICO-AVD PRESIDIO | |
33.45781 -117.65717 CAMINO DE ESTRELLA-AVD LAS PALMAS | |
33.4548 -117.6593 CAMINO DE ESTRELLA-CALLE HERMOSA | |
33.45682 -117.65762 CAMINO DE ESTRELLA-CAMINO MIRA COSTA | |
33.44209 -117.6167 AVD PICO-CALLE FRONTERA | |
33.43797 -117.62364 AVD PICO-AVD NAVARRO | |
33.43657 -117.62574 AVD PICO-CALLE DE LOS MOLINOS | |
33.43577 -117.62832 AVD PICO-AVD VISTA HERMOSA | |
33.46565 -117.63965 CAMINO DE LOS MARES-CALLE NUEVO | |
33.4688 -117.63338 CAMINO VERA CRUZ-CAMINO DE LOS MARES | |
33.46564 -117.62766 CAMINO VERA CRUZ-SARMENTOSO | |
33.46 -117.61886 CAMINO VERA CRUZ-COLINA RODANTE | |
33.45771 -117.61839 CAMINO VERA CRUZ-CALLE DE LOS ARBOLES | |
33.45598 -117.61698 AVD VISTA HERMOSA-CALLE VERA CRUZ | |
33.46198 -117.60725 AVD VISTA HERMOSA-SPORTS PARK | |
33.45608 -117.60136 AVD PICO-AVD LA PATA | |
33.45452 -117.60436 AVD PICO-PLAZA PACIFICA | |
33.44975 -117.60944 AVENIDA PICO-CALLE AMANECER | |
33.45016 -117.60893 AVD PICO-CALLE AMANECER | |
33.45461 -117.60349 AVD PICO-PLAZA PACIFICA | |
33.45682 -117.6011 AVD LA PATA-AVD PICO | |
33.46185 -117.60813 AVD VISTA HERMOSA-TARGET | |
33.45641 -117.61764 CAMINO VERA CRUZ-AVD VISTA HERMOSA | |
33.4585 -117.61833 CAMINO VERA CRUZ-CAMINO DE LA LADERA | |
33.46078 -117.6189 CAMINO VERA CRUZ-MONTANA DEL SOL | |
33.46593 -117.62789 CAMINO VERA CRUZ-SARMENTOSO | |
33.46906 -117.63367 CAMINO DE LOS MARES-CAMINO VERA CRUZ | |
33.46567 -117.64 CAMINO DE LOS MARES-CALLE NUEVO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment