{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": "# Bonus: Working with APIs" }, { "cell_type": "markdown", "metadata": {}, "source": "Apparently the best Pizza place in New York Lombardi's pizza. Let's assume you want to find a hotel with is as close as possible to this place. " }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "lombardi_pizza_coords = (40.7215577, -73.9956097)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3.1**: Create an interactive map showing all hotels and the Lombardi's pizza. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3.2:** Calculate the distance in meters between Lomabardi's pizza and each hotel. Don't forget to reproject the dataframe into a suitable coordinate references system. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3.3:** Create an interactive map which shows the points colored by the distance to the pizza place. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculate distance using openrouteservice\n", "\n", "The euclidean distance is not always a good estimate for the real distance. So we will use the openrouteservice API to calculate the real routes. \n", "\n", "#### Preparation\n", "\n", "1. Install openrouteservice-py in your environment using `pip install openrouteservice`.\n", "2. Create an account at [https://openrouteservice.org/dev/#/login](https://openrouteservice.org/dev/#/login) and generate a token/API key. Paste the API key below" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "api_key = ''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Example\n", "Here's an example of how to generate a route by sending a request to the openrouteservice API." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "from openrouteservice import Client" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "client = Client(key=api_key)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "start_coords = (-73.9684581756592, 40.75580720140376)\n", "destination_coords = (-73.9956097, 40.7215577)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "response = client.directions([start_coords, destination_coords], \n", " profile='foot-walking', \n", " instructions=False, \n", " format='geojson')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert response to geodataframe" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "route_df = gpd.GeoDataFrame.from_features(response, crs='epsg:4326')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot route in interactive map" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "