{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Created by: [SmirkyGraphs](https://smirkygraphs.github.io/). Code: [Github](https://github.com/SmirkyGraphs/Python-Notebooks). Source: [Dunkin](https://www.dunkindonuts.com/en/locations).\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Rhode Island Dunkin' Location Routes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is to get the closest path to every dunkin in the state from 3 specific locations. From the center of the state, the state airport and the state house in Providence. Through using pandas and requests to query GraphHopper Routing API from localhost and parse the gpx results using gpxpy.\n", "\n", "requirements: [QGIS](https://qgis.org/en/site/forusers/download.html), [GraphHopper](https://github.com/graphhopper/graphhopper) \n", "inspiration/tutorial: [Topi Tjukanov](https://medium.com/@tjukanov/animated-routes-with-qgis-9377c1f16021)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import requests\n", "import datetime\n", "import gpxpy\n", "import glob" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('./data/input/dunkin_locations.csv')\n", "df = df[df['state'] == 'RI']\n", "df = df['shapePoints'].str.strip('[]')\n", "df = df.str.split(', ', expand=True)\n", "df.columns = ['Y', 'X']\n", "\n", "dunkins = df.reset_index(drop=True)\n", "\n", "# The starting point for each query \n", "start_x = '-71.435256' \n", "start_y = '41.725856'\n", "\n", "url = 'http://localhost:8989/route?'\n", "url_end = '&type=gpx&instructions=false&vehicle=car'\n", "\n", "for i, row in dunkins.iterrows():\n", " lookup_x = str(row['X'])\n", " lookup_y = str(row['Y'])\n", " req = url + f'point={start_y}%2C{start_x}' + '&' + f'point={lookup_y}%2C{lookup_x}' + url_end\n", " r = requests.get(req)\n", " gpx_data = (r.content).decode('utf-8')\n", "\n", " file = f'data/raw/state_airport/ri_dunkin_{i}.gpx'\n", " with open(file, 'w') as f:\n", " f.write(gpx_data)\n", " f.close() " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "files = glob.glob('./data/raw/state_airport/*.gpx')\n", "\n", "tracks_frame = []\n", "for file in files:\n", " \n", " with open(file, 'r') as f:\n", " gpx = gpxpy.parse(f)\n", " \n", " tracks = [track for track in gpx.tracks][0]\n", " segments = [segment for segment in tracks.segments][0]\n", " \n", " for point in segments.points:\n", " data = {}\n", " data['Y'] = point.latitude\n", " data['X'] = point.longitude\n", " data['T'] = point.time\n", " tracks_frame.append(data)\n", " \n", "df = pd.DataFrame(tracks_frame)\n", "\n", "# convert time\n", "df['T'] = df['T'].dt.strftime('%Y-%m-%d %H:%M:%S.%f')\n", "\n", "# save file\n", "df.to_csv('./data/clean/state_airport_merged.csv')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "gpx = gpxpy.parse(f)\n", " \n", " tracks = [track for track in gpx.tracks][0]\n", " segments = [segment for segment in tracks.segments][0]\n", " \n", " for point in segments.points:\n", " data = {}\n", " data['Y'] = point.latitude\n", " data['X'] = point.longitude\n", " data['T'] = point.time\n", " tracks_frame.append(data)\n", " \n", "df = pd.DataFrame(tracks_frame)\n", "\n", "# convert time\n", "df['T'] = df['T'].dt.strftime('%Y-%m-%d %H:%M:%S.%f')\n", "\n", "# save file\n", "df.to_csv('./data/clean/state_airport_merged.csv')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }