Skip to content

Instantly share code, notes, and snippets.

@marks
Last active February 28, 2016 18:32
Show Gist options
  • Select an option

  • Save marks/79517bbd95fb159174e7 to your computer and use it in GitHub Desktop.

Select an option

Save marks/79517bbd95fb159174e7 to your computer and use it in GitHub Desktop.
Thomas/Darron/Mark Celebrating Cities Hack The Last Mile work
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# iPython stuff\n",
"from __future__ import print_function\n",
"from ipywidgets import interact, interactive, fixed\n",
"from IPython.display import display\n",
"from IPython.display import IFrame\n",
"import ipywidgets as widgets\n",
"\n",
"# Geopy stuff\n",
"from geopy.geocoders import Nominatim\n",
"from geopy.distance import vincenty\n",
"geolocator = Nominatim()\n",
"\n",
"# Darren Initialization Stuff\n",
"from googlemaps import distance_matrix as dm\n",
"import googlemaps\n",
"import cPickle\n",
"import pandas as pd\n",
"\n",
"google_api_key = 'SECRET'\n",
"gmaps = googlemaps.Client(google_api_key)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Darren Code Stuff\n",
"\n",
"# def travel_options(start_lat,)\n",
"\n",
"lr_dests = pd.read_csv('data/Maryland_Transit__Light_Rail_Stops.csv')\n",
"pd_dest_list = lr_dests.values.tolist()\n",
"\n",
"dest_list = []\n",
"for stop in pd_dest_list:\n",
" dest_list.append((float(stop[0]), float(stop[1])))\n",
"\n",
"import uber_rides\n",
"from uber_rides.client import UberRidesClient\n",
"uber_server_token = 'SECRET'\n",
"from uber_rides.session import Session\n",
"session = Session(server_token=uber_server_token)\n",
"client = UberRidesClient(session)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def get_travel_options(start_lat, start_lon):\n",
"\n",
" # set origin list to just the start location\n",
" pd_origin_list = [[start_lat,start_lon]]\n",
" origin_list = []\n",
" for orig in pd_origin_list:\n",
" print(orig)\n",
" origin_list.append((float(orig[0]), float(orig[1])))\n",
"\n",
" # calculate matrix\n",
" walk_matrix = dm.distance_matrix(gmaps, origin_list[0:1], dest_list, mode='walking')\n",
" # rail_matrix = dm.distance_matrix(gmaps, origin_list[0:1], dest_list, mode='transit', transit_mode='rail')\n",
" bus_matrix = dm.distance_matrix(gmaps, origin_list[0:1], dest_list, mode='transit', transit_mode='bus')\n",
"\n",
" travel_options = []\n",
" \n",
" # UberX\n",
" for s in origin_list:\n",
" for e in dest_list[0:1]:\n",
" ride = client.get_price_estimates(start_latitude=s[0], start_longitude=s[1], end_latitude=e[0], end_longitude=e[1])\n",
" for i, rd in enumerate(ride.json['prices']):\n",
" if rd['display_name'] == 'uberX':\n",
" travel_options.append((s[0],s[1], e[0],e[1], 'uberX', rd['distance'], rd['high_estimate'], rd['duration']/60))\n",
"\n",
" # Walk, Train, Bus\n",
" travel_modes = ['Walk', 'Bus']\n",
" travel_mx = [walk_matrix, bus_matrix]\n",
" for mx, mode in enumerate(travel_modes):\n",
" for i, s in enumerate(origin_list):\n",
" for j, e in enumerate(dest_list[0:1]):\n",
" travel_options.append(([s[0],s[1], e[0],e[1], mode, travel_mx[mx]['rows'][i]['elements'][j]['distance']['value']/1000*0.621371, 0, travel_mx[mx]['rows'][i]['elements'][j]['duration']['value']/60]))\n",
"\n",
" return travel_options"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lat/lon of the address #1 is: (39.376362, -76.664136)\n",
"[39.376362, -76.664136]\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>distance</th>\n",
" <th>end</th>\n",
" <th>high_cost_est</th>\n",
" <th>mode</th>\n",
" <th>start</th>\n",
" <th>time_in_mins</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2.100000</td>\n",
" <td>(39.376072, -76.650423)</td>\n",
" <td>6.000000</td>\n",
" <td>uberX</td>\n",
" <td>(39.376362, -76.664136)</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.242742</td>\n",
" <td>(39.376072, -76.650423)</td>\n",
" <td>5.666667</td>\n",
" <td>Walk</td>\n",
" <td>(39.376362, -76.664136)</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.864113</td>\n",
" <td>(39.376072, -76.650423)</td>\n",
" <td>3.000000</td>\n",
" <td>Bus</td>\n",
" <td>(39.376362, -76.664136)</td>\n",
" <td>18</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" distance end high_cost_est mode \\\n",
"0 2.100000 (39.376072, -76.650423) 6.000000 uberX \n",
"1 1.242742 (39.376072, -76.650423) 5.666667 Walk \n",
"2 1.864113 (39.376072, -76.650423) 3.000000 Bus \n",
"\n",
" start time_in_mins \n",
"0 (39.376362, -76.664136) 6 \n",
"1 (39.376362, -76.664136) 34 \n",
"2 (39.376362, -76.664136) 18 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# This function gets called everytime an input changes\n",
"def input_function(address1, hourly_rate):\n",
" # Location 1\n",
" location1 = geolocator.geocode(address1)\n",
" print(\"Lat/lon of the address #1 is: ({}, {})\".format(location1.latitude,location1.longitude))\n",
" travel_options = get_travel_options(location1.latitude, location1.longitude)\n",
" travel_options_data = []\n",
" for option in travel_options:\n",
" try: \n",
" data = {\n",
" 'start': (option[0],option[1]),\n",
" 'end': (option[2],option[3]),\n",
" 'mode': option[4],\n",
" 'distance': option[5],\n",
" 'high_cost_est': option[6],\n",
" 'time_in_mins': option[7]\n",
" }\n",
" if(data['mode'] != \"uberX\"):\n",
" data['high_cost_est'] = (float(data['time_in_mins'])/60)*int(hourly_rate)\n",
" travel_options_data.append(data)\n",
" except: \n",
" print()\n",
" # nothing, it's a hack.\n",
" \n",
" df = pd.DataFrame(travel_options_data)\n",
" display(df)\n",
"\n",
" # return {'address': address1, 'hourly_rate': hourly_rate}\n",
"\n",
"w = interactive(input_function, address1='2006 Smith Ave, Baltimore, MD 21209', hourly_rate=widgets.IntSlider(min=0,max=100,step=5,value=10))\n",
"\n",
"display(w)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"700\"\n",
" height=\"700\"\n",
" src=\"https://www.google.com/maps/embed/v1/directions?key=SECRET&origin=Oslo+Norway&destination=Telemark+Norway&avoid=tolls|highways\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x10877bfd0>"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Display Map\n",
"from IPython.display import IFrame\n",
"IFrame('https://www.google.com/maps/embed/v1/directions?key=SECRET&origin=Oslo+Norway&destination=Telemark+Norway&avoid=tolls|highways', width=700, height=700)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
LAT LON OBJECTID stop_id stop_name Mode Shelter CountyCode Rider_On Rider_Off Rider_Total Stop_Ridership_Rank Distribution_Policy
39.376072 -76.650423 160 7654 FALLS RD & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.172005 -76.632984 135 7625 CROMWELL STATION / GLEN BURNIE nb Light Rail No E1 - Public Domain - Internal Use Only
39.18341 -76.640024 136 7626 FERNDALE LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.181298 -76.668468 137 7627 BWI AIRPORT LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.190678 -76.674466 138 7628 BWI BUSINESS PARK STATION Light Rail No E1 - Public Domain - Internal Use Only
39.202261 -76.655047 139 7630 LINTHICUM LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.214389 -76.645586 140 7631 NORTH LINTHICUM LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.222291 -76.640943 141 7632 NURSERY RD & LIGHT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.234256 -76.633123 142 7634 BATIMORE HIGHLANDS & LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.242013 -76.629838 143 7635 PATAPSCO & LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.256034 -76.634892 144 7637 CHERRY HILL LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.262107 -76.631345 145 7638 WESTPORT LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.278425 -76.620326 146 7639 HAMBURG & LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.282038 -76.619426 147 7640 CAMDEN STA/LT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.286263 -76.619375 148 7641 PRATT ST LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.289155 -76.619511 149 7642 BALTIMORE ST & LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.291975 -76.619686 150 7643 LEXINGTON ST & LIGHT RAIL STAT nb Light Rail Yes E1 - Public Domain - Internal Use Only
39.296501 -76.619951 151 7644 CENTRE ST & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.303022 -76.620324 152 7645 CULTURAL CTR & LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.307181 -76.619973 153 7646 MT ROYAL & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.307791 -76.616141 154 7647 PENN STATION LIGHT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.311871 -76.622574 155 7648 NORTH AVE LIGHT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.312388 -76.622754 156 7649 RELIEF PT/DIVISION Light Rail No E1 - Public Domain - Internal Use Only
39.330979 -76.643242 157 7650 WOODBERRY/LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.344154 -76.647801 158 7651 COLDSPRING & LIGHT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.367908 -76.651504 159 7652 MT WASHINGTON & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.429551 -76.627089 161 7655 LUTHERVILLE & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.434124 -76.628174 162 7656 TIMONIUM BUSINESS PARK Light Rail No E1 - Public Domain - Internal Use Only
39.44888 -76.635128 163 7657 FAIRGROUNDS & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.475424 -76.653683 164 7658 WARREN RD LIT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.481591 -76.661789 165 7659 GILROY RD LIGHT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.48995 -76.658265 166 7660 McCORMICK RD LIGHT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.492211 -76.654874 167 7661 PEPPER RD & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.496267 -76.653746 168 7662 HUNT VALLEY & LT RAIL STAT nb Light Rail No E1 - Public Domain - Internal Use Only
39.496233 -76.654095 169 7663 HUNT VALLEY & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.492158 -76.655177 170 7664 PEPPER RD & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.490058 -76.65831 171 7665 McCORMICK RD LIGHT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.481844 -76.661915 172 7666 GILROY RD LIGHT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.475471 -76.652311 173 7667 WARREN RD LIGHT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.448529 -76.635212 174 7668 FAIRGROUNDS & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.434422 -76.628334 175 7669 TIMONIUM BUSINESS PARK sb Light Rail No E1 - Public Domain - Internal Use Only
39.428688 -76.627341 176 7670 LUTHERVILLE & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.375948 -76.650749 177 7671 FALLS RD & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.367728 -76.651459 178 7673 MT WASHINGTON & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.343712 -76.64777 179 7674 COLDSPRING & LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.331258 -76.643251 180 7675 WOODBERRY/LT RAIL nb Light Rail No E1 - Public Domain - Internal Use Only
39.311853 -76.62262 181 7676 NORTH AVE LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.307805 -76.61611 182 7677 PENN STATION LIGHT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.307038 -76.620067 183 7678 MT ROYAL AVE & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.301896 -76.620253 184 7679 CULTURAL CTR & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.296265 -76.620072 185 7680 CENTRE ST & LT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.291678 -76.619743 186 7681 LEXINGTON ST & LIGHT RAIL STAT sb Light Rail Yes E1 - Public Domain - Internal Use Only
39.288885 -76.619595 187 7682 BALTIMORE ST & LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.286137 -76.619446 188 7683 PRATT ST LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.282038 -76.619379 189 7684 CAMDEN/LT RAIL Light Rail No E1 - Public Domain - Internal Use Only
39.277641 -76.62081 190 7685 HAMBURG ST & LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.261919 -76.631578 191 7686 WESTPORT LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.256041 -76.635003 192 7687 CHERRY HILL LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.240925 -76.630388 193 7689 PATAPSCO & LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.233799 -76.63344 194 7690 BALTIMORE HIGHLANDS & LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.221181 -76.641516 195 7692 NURSERY RD & LIGHT RAIL STAT sb Light Rail No E1 - Public Domain - Internal Use Only
39.214444 -76.645655 196 7693 NORTH LINTHICUM LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.202306 -76.655139 197 7694 LINTHICUM LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.19077 -76.67494 198 7696 BWI BUSINESS PARK STATION Light Rail No E1 - Public Domain - Internal Use Only
39.181234 -76.668364 199 7697 BWI AIRPORT LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.183392 -76.640128 200 7698 FERNDALE LT RAIL sb Light Rail No E1 - Public Domain - Internal Use Only
39.171932 -76.63295 201 7699 CROMWELL STATION / GLEN BURNIE sb Light Rail No E1 - Public Domain - Internal Use Only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment