{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Usage"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"[](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",
" ZIP_lat | \n",
" ZIP_lon | \n",
" AHA_ID_lat | \n",
" AHA_ID_lon | \n",
"
\n",
" \n",
" \n",
" \n",
" 6010 | \n",
" 42.376239 | \n",
" -72.605400 | \n",
" 42.819978 | \n",
" -73.916518 | \n",
"
\n",
" \n",
" 5333 | \n",
" 42.293923 | \n",
" -72.967189 | \n",
" 41.753841 | \n",
" -72.682788 | \n",
"
\n",
" \n",
" 7678 | \n",
" 42.158520 | \n",
" -72.585325 | \n",
" 40.709320 | \n",
" -74.212500 | \n",
"
\n",
" \n",
"
\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",
" geometry | \n",
" duration (s) | \n",
" distance (m) | \n",
" speed (m/s) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" LINESTRING (-72.58532 42.15852, -72.58523 42.1... | \n",
" 22.6 | \n",
" 279.5 | \n",
" 12.367257 | \n",
"
\n",
" \n",
" 1 | \n",
" LINESTRING (-72.58194 42.15850, -72.58194 42.1... | \n",
" 12.3 | \n",
" 163.2 | \n",
" 13.268293 | \n",
"
\n",
" \n",
" 2 | \n",
" LINESTRING (-72.58090 42.15734, -72.58092 42.1... | \n",
" 128.2 | \n",
" 1929.9 | \n",
" 15.053822 | \n",
"
\n",
" \n",
" 3 | \n",
" LINESTRING (-72.58196 42.14028, -72.58205 42.1... | \n",
" 111.8 | \n",
" 1703.5 | \n",
" 15.237030 | \n",
"
\n",
" \n",
" 4 | \n",
" LINESTRING (-72.58437 42.12533, -72.58492 42.1... | \n",
" 98.1 | \n",
" 1783.3 | \n",
" 18.178389 | \n",
"
\n",
" \n",
"
\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",
" orgin_lat | \n",
" orgin_lon | \n",
" destination_lat | \n",
" destination_lon | \n",
" distance (m) | \n",
" duration (s) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 42.376239 | \n",
" -72.605400 | \n",
" 42.819978 | \n",
" -73.916518 | \n",
" 185141.8 | \n",
" 8639.8 | \n",
"
\n",
" \n",
" 1 | \n",
" 42.376239 | \n",
" -72.605400 | \n",
" 41.753841 | \n",
" -72.682788 | \n",
" 82634.6 | \n",
" 4058.1 | \n",
"
\n",
" \n",
" 2 | \n",
" 42.376239 | \n",
" -72.605400 | \n",
" 40.709320 | \n",
" -74.212500 | \n",
" 300008.0 | \n",
" 13819.9 | \n",
"
\n",
" \n",
" 3 | \n",
" 42.293923 | \n",
" -72.967189 | \n",
" 42.819978 | \n",
" -73.916518 | \n",
" 126934.5 | \n",
" 6829.4 | \n",
"
\n",
" \n",
" 4 | \n",
" 42.293923 | \n",
" -72.967189 | \n",
" 41.753841 | \n",
" -72.682788 | \n",
" 90821.8 | \n",
" 5550.6 | \n",
"
\n",
" \n",
"
\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",
" origin_lat | \n",
" origin_lon | \n",
" destination_lat | \n",
" destination_lon | \n",
" distance (m) | \n",
" duration (s) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 42.376239 | \n",
" -72.605400 | \n",
" 42.819978 | \n",
" -73.916518 | \n",
" 185141.8 | \n",
" 8639.8 | \n",
"
\n",
" \n",
" 0 | \n",
" 42.293923 | \n",
" -72.967189 | \n",
" 41.753841 | \n",
" -72.682788 | \n",
" 90821.8 | \n",
" 5550.6 | \n",
"
\n",
" \n",
" 0 | \n",
" 42.158520 | \n",
" -72.585325 | \n",
" 40.709320 | \n",
" -74.212500 | \n",
" 268234.5 | \n",
" 12313.4 | \n",
"
\n",
" \n",
"
\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
}