{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Usage" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/wybert/georouting/blob/main/docs/usage.ipynb)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install georouting" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare some data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ZIP_latZIP_lonAHA_ID_latAHA_ID_lon
601042.376239-72.60540042.819978-73.916518
533342.293923-72.96718941.753841-72.682788
767842.158520-72.58532540.709320-74.212500
\n", "
" ], "text/plain": [ " ZIP_lat ZIP_lon AHA_ID_lat AHA_ID_lon\n", "6010 42.376239 -72.605400 42.819978 -73.916518\n", "5333 42.293923 -72.967189 41.753841 -72.682788\n", "7678 42.158520 -72.585325 40.709320 -74.212500" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "data = pd.read_csv(\n", " \"https://raw.githubusercontent.com/wybert/georouting/main/docs/data/sample_3.csv\",\n", " index_col=0)\n", "data = data[['ZIP_lat', 'ZIP_lon', 'AHA_ID_lat', 'AHA_ID_lon']]\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "origin = [data.iloc[2][\"ZIP_lat\"],data.iloc[2][\"ZIP_lon\"]]\n", "destination = [data.iloc[2][\"AHA_ID_lat\"],data.iloc[2][\"AHA_ID_lon\"]]\n", "\n", "origins = data[['ZIP_lat', 'ZIP_lon']].values.tolist()\n", "destinations = data[['AHA_ID_lat', 'AHA_ID_lon']].values.tolist()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Get a route use OSRM " ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Get a route between two points is easy, " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Distance: 268234.5 meters\n", "Duration: 12313.4 seconds\n" ] } ], "source": [ "from georouting.routers import OSRMRouter\n", "# create a router object \n", "router = OSRMRouter(mode=\"driving\")\n", "# get the route between the origin and destination, this will return a Route object\n", "# this will call the OSRM API\n", "route = router.get_route(origin, destination)\n", "# Now you can get the distance and duration of the route in meters and seconds\n", "print(\"Distance: {} meters\".format(route.get_distance()))\n", "print(\"Duration: {} seconds\".format(route.get_duration()))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "You can easily get the distance, duration.\n", "\n", "You can also return the routing results in a GeoDataFrame, It will return the distance, duration, speed and the route geometry," ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometryduration (s)distance (m)speed (m/s)
0LINESTRING (-72.58532 42.15852, -72.58523 42.1...22.6279.512.367257
1LINESTRING (-72.58194 42.15850, -72.58194 42.1...12.3163.213.268293
2LINESTRING (-72.58090 42.15734, -72.58092 42.1...128.21929.915.053822
3LINESTRING (-72.58196 42.14028, -72.58205 42.1...111.81703.515.237030
4LINESTRING (-72.58437 42.12533, -72.58492 42.1...98.11783.318.178389
\n", "
" ], "text/plain": [ " geometry ... speed (m/s)\n", "0 LINESTRING (-72.58532 42.15852, -72.58523 42.1... ... 12.367257\n", "1 LINESTRING (-72.58194 42.15850, -72.58194 42.1... ... 13.268293\n", "2 LINESTRING (-72.58090 42.15734, -72.58092 42.1... ... 15.053822\n", "3 LINESTRING (-72.58196 42.14028, -72.58205 42.1... ... 15.237030\n", "4 LINESTRING (-72.58437 42.12533, -72.58492 42.1... ... 18.178389\n", "\n", "[5 rows x 4 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df= route.get_route_geopandas()\n", "df.head()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "You can visualize the route in a map," ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "route.plot_route()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Get a distance matrix" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
orgin_latorgin_londestination_latdestination_londistance (m)duration (s)
042.376239-72.60540042.819978-73.916518185141.88639.8
142.376239-72.60540041.753841-72.68278882634.64058.1
242.376239-72.60540040.709320-74.212500300008.013819.9
342.293923-72.96718942.819978-73.916518126934.56829.4
442.293923-72.96718941.753841-72.68278890821.85550.6
\n", "
" ], "text/plain": [ " orgin_lat orgin_lon ... distance (m) duration (s)\n", "0 42.376239 -72.605400 ... 185141.8 8639.8\n", "1 42.376239 -72.605400 ... 82634.6 4058.1\n", "2 42.376239 -72.605400 ... 300008.0 13819.9\n", "3 42.293923 -72.967189 ... 126934.5 6829.4\n", "4 42.293923 -72.967189 ... 90821.8 5550.6\n", "\n", "[5 rows x 6 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distance_matrix = router.get_distance_matrix(origins, destinations, append_od=True)\n", "distance_matrix.head()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Get distances according OD pairs" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you may want to get the durations and distances for some specific origin-destination pairs not for all possible combinations between them. you can use the `get_distances_batch` function. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
origin_latorigin_londestination_latdestination_londistance (m)duration (s)
042.376239-72.60540042.819978-73.916518185141.88639.8
042.293923-72.96718941.753841-72.68278890821.85550.6
042.158520-72.58532540.709320-74.212500268234.512313.4
\n", "
" ], "text/plain": [ " origin_lat origin_lon ... distance (m) duration (s)\n", "0 42.376239 -72.605400 ... 185141.8 8639.8\n", "0 42.293923 -72.967189 ... 90821.8 5550.6\n", "0 42.158520 -72.585325 ... 268234.5 12313.4\n", "\n", "[3 rows x 6 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distances = router.get_distances_batch(origins, destinations, append_od=True)\n", "distances" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "It will automatically split the OD pairs into batches and get the distance matrix for each batch to avoid the API limit." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## What's more" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "`georouting` provides a unified API for routing services, you can use the similar code to get the routing results from different routing services like Google Maps, Bing Maps, OSRM, etc." ] } ], "metadata": { "kernelspec": { "display_name": "georouting", "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.11.0" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "37401de40115f5bc41f8ac2879c9b417df31798bc0973b18c8e59529def509f0" } } }, "nbformat": 4, "nbformat_minor": 2 }