{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "ef18e89b-d1c5-43a7-97b8-d3f8ff4de316", "metadata": {}, "outputs": [], "source": [ "import requests\n", "import pandas as pd\n", "\n", "class Location:\n", " \n", " def __init__(self, latitude, longitude):\n", " self.latitude = latitude\n", " self.longitude = longitude\n", "\n", "\n", "class OverpassAPI:\n", " \n", " def __init__(self, base_url=\"http://overpass-api.de/api/interpreter\"):\n", " self.base_url = base_url\n", "\n", " def fetch_restaurants(self, latitude, longitude, radius):\n", " \"\"\"Fetch restaurants from OpenStreetMap using Overpass API within a radius.\"\"\"\n", " # Calculate the bounding box\n", " lat_min = latitude - radius\n", " lat_max = latitude + radius\n", " lon_min = longitude - radius\n", " lon_max = longitude + radius\n", "\n", " # Define the query to get restaurant data\n", " query = f\"\"\"\n", " [out:json];\n", " node[\"amenity\"=\"restaurant\"]({lat_min},{lon_min},{lat_max},{lon_max});\n", " out;\n", " \"\"\"\n", "\n", " # Make the API request\n", " response = requests.get(self.base_url, params={'data': query})\n", " return response.json()\n", "\n", "\n", "class RestaurantDataProcessor:\n", "\n", " @staticmethod\n", " def process_data(data):\n", "\n", " restaurants = []\n", "\n", " # Extract relevant information from each element\n", " for element in data.get('elements', []):\n", " name = element['tags'].get('name', 'Unknown')\n", " cuisine = element['tags'].get('cuisine', 'Unknown')\n", " lat = element['lat']\n", " lon = element['lon']\n", " restaurants.append({\n", " 'Name': name,\n", " 'Cuisine': cuisine,\n", " 'Latitude': lat,\n", " 'Longitude': lon\n", " })\n", "\n", " return pd.DataFrame(restaurants)\n", "\n", "\n", "\n", "# Define the location and radius\n", "location = Location(38.7097, -9.1557) \n", "radius = 0.01 # Radius in degrees (~1 km)\n", "\n", "# Create instances of the API and Data Processor classes\n", "api = OverpassAPI()\n", "processor = RestaurantDataProcessor()\n", "\n", "# Fetch and process the restaurant data\n", "raw_data = api.fetch_restaurants(location.latitude, location.longitude, radius)\n", "restaurants_df = processor.process_data(raw_data)\n", "\n", "# Print the results\n", "print(\"Restaurants Found:\")\n", "print(restaurants_df)" ] }, { "cell_type": "markdown", "id": "49d7f32a-7d0f-4f47-b901-c7342466cc09", "metadata": {}, "source": [ "1. Describe the code, the purpose of each class, and the purpose of each method within each class.\n", "2. Print the JSON file obtained from the API.\n", "3. List all the restaurants that are \"regional.\"\n", "4. List all \"Brazilian\" restaurants.\n", "5. Count all \"Portuguese\" restaurants.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.13.0" } }, "nbformat": 4, "nbformat_minor": 5 }