Skip to content

Commit

Permalink
add transit reachability
Browse files Browse the repository at this point in the history
  • Loading branch information
Margaret Li committed Aug 7, 2024
1 parent aa4bbdb commit a1e327a
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 370 deletions.
70 changes: 35 additions & 35 deletions locs/db.csv
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
id,name,lat,long,transit,region
Alki,Alki Beach,,,,
Bainbridge,Bainbridge Ferry Terminal,,,,
Beacon,Beacon Hill Station,,,,
BellDtwn,Bellevue Downtown Station,,,,
BlythPark,Blyth Park,,,,
CapHill,Capitol Hill Station,,,,
CID,Chinatown International District Station,,,,
ColCity,Columbia City Station,,,,
ColmanPark,Colman Park,,,,
Cougar,Cougar Mountain Park,,,,
CSE,UW CSE,,,,
GasWorks,Gas Works Park,,,,
GreenLake,Green Lake Community Center,,,,
JaysCafe,Jay's Cafe,,,,
JRidgeTrail,Johnson Ridge Trail,,,,
Locks,Ballard Locks,,,,
LordHill,Lord Hill Park,,,,
MadronaPark,Madrona Park,,,,
Magnuson,Magnusun Park,,,,
Marymoor,Marymoor Park,,,,
Miller,Miller Playfield,,,,
MtBaker,Mount Baker Station,,,,
Northgate,Northgate Station,,,,
PacPav,Pacar Pavilion,,,,
RedTech,Redmond Technology Center Station,,,,
RedWater,Redmond Watershed Preserve,,,,
Roosevelt,Roosevelt Station,,,,
Seward,Seward Park,,,,
SoBell,South Bellevue Station,,,,
SoDisco,South Discovery Park,,,,
SODO,SODO Station,,,,
WashPark,Washington Park (South Arboretum),,,,
Westlake,Westlake Station,,,,
WoodCafe,Woodinville Cafe,,,,
id,name,lat,lon,region,maps_url,meeting_point_desc,transit,reachability
Alki,Alki Beach,47.5801187,-122.4158763,,,,Bus or Drive,2
Bainbridge,Bainbridge Ferry Terminal,47.6025285,-122.3381589,,https://maps.app.goo.gl/RRLWmbmhDebw7AMq9,Inside ferry building,Ferry to Seattle stop,1
Beacon,Beacon Hill Station,47.5792725,-122.3142812,,https://maps.app.goo.gl/PzogvMhTuyUsYRc87,"""Common Threads-Community Patterns"" poles sculpture",Light Rail to Beacon Hill stop,3
BellDtwn,Bellevue Downtown Station,47.6150957,-122.1942621,,https://maps.app.goo.gl/H38DLL1dhhPAvets9,,Light Rail to Bellevue Downtown stop,2
BlythPark,Blyth Park,47.7505304,-122.211542,,,,Bus or Drive,3
CapHill,Capitol Hill Station,47.6196778,-122.320729,,https://maps.app.goo.gl/d9JjJchMbFj3Dngg9,"North Entrance, SE corner of E John St & Broadway E",Light Rail to Capitol Hill stop,1
CID,Chinatown International District Station,47.5961435,-122.3253719,,https://maps.app.goo.gl/bXUrsZ5M1SpfSRrHA,,Light Rail to Int'l Dist/Chinatown stop,1
ColCity,Columbia City Station,47.5597486,-122.2952164,,https://maps.app.goo.gl/3goc7v3rd1FEqSrP7,,Light Rail to Columbia City stop,1
ColmanPark,Colman Park,47.5863252,-122.2929693,,,,Bus or Drive,1
Cougar,Cougar Mountain Park,47.5306464,-122.1009792,,,,Bus or Drive,3
CSE,UW CSE,47.6232155,-122.347724,,https://maps.app.goo.gl/4ih5UWgn8SpfrZo1A,,Bus or Drive,2
GasWorks,Gas Works Park,47.6459839,-122.3359135,,https://maps.app.goo.gl/3NFyYHvPvtdW5ZhA6,"Parking lot, east side",Bus or Drive,2
GreenLake,Green Lake Community Center,47.6196778,-122.320729,,https://maps.app.goo.gl/iQq4hdSYjUakMWQZ7,,Light Rail to Capitol Hill stop,1
JaysCafe,Jay's Cafe,47.7569854,-122.242083,,,,Bus or Drive,3
JRidgeTrail,Johnson Ridge Trail,47.7966051,-121.2652243,,,,Bus or Drive,3
Locks,Ballard Locks,47.6674708,-122.3962056,,https://maps.app.goo.gl/YKQN6PwsuTsPgHej6,"Outside North Gate, diagonally across Lockspot Cafe",Bus or Drive,2
LordHill,Lord Hill Park,47.8491374,-122.0516655,,,,Bus or Drive,3
MadronaPark,Madrona Park,47.6104128,-122.2849576,,,,Bus or Drive,2
Magnuson,Magnuson Park,47.6797723,-122.256177,,,,Bus or Drive,3
Marymoor,Marymoor Park,47.6626405,-122.10871,,,,Bus or Drive,3
Miller,Miller Playfield,47.6210058,-122.3083135,,,,Bus or Drive,2
MtBaker,Mount Baker Station,47.5765999,-122.2977231,,https://maps.app.goo.gl/zzMgnxYtKJVj6mds6,"""Sky Within"" sculpture at street level, under platform",Light Rail to Mount Baker stop,1
Northgate,Northgate Station,47.7025575,-122.3283229,,https://maps.app.goo.gl/XD2CSQLaHzrQ5oX58,"At the elevated exit to John Lewis Memorial Bridge, one level down from platform, one level above ground",Light Rail to Northgate stop,2
PacPav,Paccar Pavilion,47.6164028,-122.356678,,,,Bus or Drive,2
RedTech,Redmond Technology Center Station,47.6444336,-122.1358824,,https://maps.app.goo.gl/dRRzfUcmvML1TzMr8,,Light Rail to Redmond Technology stop,2
RedWater,Redmond Watershed Preserve,47.7065401,-122.0493104,,,,Bus or Drive,3
Roosevelt,Roosevelt Station,47.675987,-122.3158799,,https://maps.app.goo.gl/hR1C3bR6t4echwML6,"Outside Main Entrance, NW Corner of NE 65th & 12th Ave NE",Light Rail to Roosevelt stop,1
Seward,Seward Park,47.5496082,-122.2599323,,,,Bus or Drive,1
SoBell,South Bellevue Station,47.5865506,-122.1923785,,https://maps.app.goo.gl/uKEqhcKWBedCYdJv6,,Light Rail to South Bellevue stop,2
SoDisco,South Discovery Park,47.6537734,-122.4132117,,,,Bus or Drive,3
SODO,SODO Station,47.5800441,-122.3273371,,https://maps.app.goo.gl/WXSgA2UBwWzwv7MRA,"""Made in USA"" arch sculpture, NE corner of S Lander & SODO busway",Light Rail to SODO stop,1
WashPark,Washington Park (South Arboretum),47.6280587,-122.29417,,https://maps.app.goo.gl/YBRvLZLaKhNDAjok8,East End of Parking Lot,Bus or Drive,2
Westlake,Westlake Station,47.6109627,-122.3382169,,https://maps.app.goo.gl/amdkLVihVvqJMW936,"C1 entrance, street level, North side of Pine b/t 3rd and 4th",Light Rail to Westlake stop,1
WoodCafe,Woodinville Cafe,47.7550348,-122.1534919,,https://maps.app.goo.gl/VZSWE6Xah12iJ3is6,,Bus or Drive,3
35 changes: 35 additions & 0 deletions locs/db.csv_old
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
id,name,lat,lon,region,maps_url,meeting_point_desc
Alki,Alki Beach,47.5801187,-122.4158763,,,
Bainbridge,Bainbridge Ferry Terminal,47.6025285,-122.3381589,,https://maps.app.goo.gl/RRLWmbmhDebw7AMq9,"Inside ferry building"
Beacon,Beacon Hill Station,47.5792725,-122.3142812,,https://maps.app.goo.gl/PzogvMhTuyUsYRc87,"""Common Threads-Community Patterns"" poles sculpture"
BellDtwn,Bellevue Downtown Station,47.6150957,-122.1942621,,https://maps.app.goo.gl/H38DLL1dhhPAvets9,
BlythPark,Blyth Park,47.7505304,-122.211542,,,
CapHill,Capitol Hill Station,47.6196778,-122.320729,,https://maps.app.goo.gl/d9JjJchMbFj3Dngg9,"North Entrance, SE corner of E John St & Broadway E"
CID,Chinatown International District Station,47.5961435,-122.3253719,,https://maps.app.goo.gl/bXUrsZ5M1SpfSRrHA
ColCity,Columbia City Station,47.5597486,-122.2952164,,https://maps.app.goo.gl/3goc7v3rd1FEqSrP7
ColmanPark,Colman Park,47.5863252,-122.2929693,,,
Cougar,Cougar Mountain Park,47.5306464,-122.1009792,,,
CSE,UW CSE,47.6232155,-122.347724,,https://maps.app.goo.gl/4ih5UWgn8SpfrZo1A
GasWorks,Gas Works Park,47.6459839,-122.3359135,,https://maps.app.goo.gl/3NFyYHvPvtdW5ZhA6,"Parking lot, east side"
GreenLake,Green Lake Community Center,47.6196778,-122.320729,,https://maps.app.goo.gl/iQq4hdSYjUakMWQZ7,
JaysCafe,Jay's Cafe,47.7569854,-122.242083,,,
JRidgeTrail,Johnson Ridge Trail,47.7966051,-121.2652243,,,
Locks,Ballard Locks,47.6674708,-122.3962056,,https://maps.app.goo.gl/YKQN6PwsuTsPgHej6,"Outside North Gate, diagonally across Lockspot Cafe"
LordHill,Lord Hill Park,47.8491374,-122.0516655,,,
MadronaPark,Madrona Park,47.6104128,-122.2849576,,,
Magnuson,Magnuson Park,47.6797723,-122.256177,,,
Marymoor,Marymoor Park,47.6626405,-122.10871,,,
Miller,Miller Playfield,47.6210058,-122.3083135,,,
MtBaker,Mount Baker Station,47.5765999,-122.2977231,,https://maps.app.goo.gl/zzMgnxYtKJVj6mds6,"""Sky Within"" sculpture at street level, under platform"
Northgate,Northgate Station,47.7025575,-122.3283229,,https://maps.app.goo.gl/XD2CSQLaHzrQ5oX58,"At the elevated exit to John Lewis Memorial Bridge, one level down from platform, one level above ground"
PacPav,Paccar Pavilion,47.6164028,-122.356678,,,
RedTech,Redmond Technology Center Station,47.6444336,-122.1358824,,https://maps.app.goo.gl/dRRzfUcmvML1TzMr8
RedWater,Redmond Watershed Preserve,47.7065401,-122.0493104,,,
Roosevelt,Roosevelt Station,47.675987,-122.3158799,,https://maps.app.goo.gl/hR1C3bR6t4echwML6,"Outside Main Entrance, NW Corner of NE 65th & 12th Ave NE"
Seward,Seward Park,47.5496082,-122.2599323,,,
SoBell,South Bellevue Station,47.5865506,-122.1923785,,https://maps.app.goo.gl/uKEqhcKWBedCYdJv6
SoDisco,South Discovery Park,47.6537734,-122.4132117,,,
SODO,SODO Station,47.5800441,-122.3273371,,https://maps.app.goo.gl/WXSgA2UBwWzwv7MRA,"""Made in USA"" arch sculpture, NE corner of S Lander & SODO busway"
WashPark,Washington Park (South Arboretum),47.6280587,-122.29417,,https://maps.app.goo.gl/YBRvLZLaKhNDAjok8,"East End of Parking Lot"
Westlake,Westlake Station,47.6109627,-122.3382169,,https://maps.app.goo.gl/amdkLVihVvqJMW936,"C1 entrance, street level, North side of Pine b/t 3rd and 4th"
WoodCafe,Woodinville Cafe,47.7550348,-122.1534919,,https://maps.app.goo.gl/VZSWE6Xah12iJ3is6,
53 changes: 41 additions & 12 deletions locs/extract_locs_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,84 @@
import geojson
import gpxpy
import os
import requests
from shapely import distance
from shapely.geometry import shape, Point
from statistics import mean

ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))

GOOGLE_MAPS_API_KEY = ""
#"AIzaSyCJdAH_V4n12Ko
# DyNBcsAV86hWl1_49Hs0"

TRANSPORT_FILES = {
"Light Rail": f"{ROOT}/locs/seattle_city_raw_data/sound_transit_light_rail.csv",
"Ferry": f"{ROOT}/locs/seattle_city_raw_data/ferry.csv",
"Bus": f"{ROOT}/locs/seattle_city_raw_data/bus.csv",
# "Bus": f"{ROOT}/locs/seattle_city_raw_data/bus.csv",
}
LOCS_CSV_FILE = f"{ROOT}/locs/db.csv"

df = pd.read_csv(LOCS_CSV_FILE)
assert "id" in list(df), "db.csv does not contain location ids"

def near_enough(p1, p2, threshold=0.005): #0.005 ~= 0.3 miles or 6 minutes of walking
return distance(p1, p2) < threshold
CRITICAL_LOC_NAMES = ["CSE", "GreenLake", "Beacon"]
CRITICAL_LOCS = []
for index, row in df.iterrows():
if row['id'] in CRITICAL_LOC_NAMES:
CRITICAL_LOCS.append((row['lon'], row['lat']))

STOPS = {}

for system_name, file_name in TRANSPORT_FILES.items():
system_df = pd.read_csv(TRANSPORT_FILES[system_name])
for index, row in system_df.iterrows():
STOPS[row['stop_name']] = {
'lat': row['stop_lat'],
'lon': row['stop_lon'],
'lat': row['stop_lat'],
'system': system_name,
}

# construct point based on lon/lat returned by geocoder
MAX_REACHABILITY = 3

# check each polygon to see if it contains the point
def near_enough(p1, p2, threshold=0.005): #0.005 ~= 0.3 miles or 6 minutes of walking
return distance(p1, p2) < threshold

## add new columns if not existing in csv
for col_name in ["transit"]:
for col_name in ["transit", "reachability"]:
if col_name not in df:
df[col_name] = ""

for index, row in df.iterrows():
id = row["id"]
loc_point = Point(row['lon'], row['lat'])

loc_point = Point(row['long'], row['lat'])

# match to nearest stop
df.at[index, "transit"] = f"Bus or Drive"
for stop_name, stop_dict in STOPS.items():
if near_enough(loc_point, Point(stop_dict['lon'], stop_dict['lat'])):
df.at[index, "transit"] = f"{stop_dict["system"]} to {stop_name} stop"
# import pdb; pdb.set_trace()
df.at[index, "transit"] = f"{stop_dict['system']} to {stop_name} stop"
break

dest = "%7C".join([f"{lat}%2C{lon}" for lon, lat in CRITICAL_LOCS])
orig = f"{row['lat']}%2C{row['lon']}"
request = f"https://maps.googleapis.com/maps/api/distancematrix/json?destinations={dest}&origins={orig}&mode=transit&key={GOOGLE_MAPS_API_KEY}"
response = requests.get(request)
response_json = response.json()
# import pdb;pdb.set_trace()
if response.status_code == 200 and response_json.get("status") == "OK":
distance_dicts = [r.get("elements", {})[0].get("duration", {}) for r in response_json.get("rows", {})]
distance_sec = [d["value"] for d in distance_dicts if "value" in d]
distance_text = [d["text"] for d in distance_dicts if "text" in d]
reachability = MAX_REACHABILITY
if distance_sec:
avg_distance_mins = mean(distance_sec)/60
max_distance_mins = max(distance_sec)/60
reachability = min(reachability, 1 + int(max_distance_mins/(30)))
df.at[index, "reachability"] = reachability

os.rename(LOCS_CSV_FILE, LOCS_CSV_FILE + "_old")
df.to_csv(LOCS_CSV_FILE, index=False)

df.to_csv(LOCS_CSV_FILE + "_new")


1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
icalendar==5.0.7
PyYAML==6.0.1
gpxpy==1.6.2
requests
Loading

0 comments on commit a1e327a

Please sign in to comment.