# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import gmaps
import os
# Import API key
from api_keys import g_key
weather_csv_file = "../WeatherPy/output_data/city_weather_data.csv"
weather_df = pd.read_csv(weather_csv_file)
weather_df.head()
City | Lat | lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | |
---|---|---|---|---|---|---|---|---|---|
0 | kapaa | 22.08 | -159.32 | 78.01 | 94.0 | 75.0 | 23.04 | US | 1.592599e+09 |
1 | hilo | 19.73 | -155.09 | 80.60 | 61.0 | 75.0 | 7.25 | US | 1.592599e+09 |
2 | bredasdorp | -34.53 | 20.04 | 50.00 | 87.0 | 0.0 | 5.23 | ZA | 1.592599e+09 |
3 | albany | 42.60 | -73.97 | 87.01 | 52.0 | 56.0 | 4.65 | US | 1.592600e+09 |
4 | itoman | 26.12 | 127.67 | 82.40 | 94.0 | 75.0 | 11.41 | JP | 1.592600e+09 |
# Configure gmaps with API key.
gmaps.configure(api_key=g_key)
# Convert Humidity to float and store
# Also, handle NaN values
weather_df = weather_df.dropna()
humidity = weather_df["Humidity"].astype(float)
# Store 'Latitude' and 'Longitude' into locations.
locations = weather_df[["Lat", "lng"]].astype(float)
# Create a humidity Heatmap layer
# Plot Heatmap
fig = gmaps.figure(center = [0,0] ,zoom_level = 2)
# Create and add heat layer
heat_layer = gmaps.heatmap_layer(locations, weights=humidity,
dissipating=False, max_intensity=100,
point_radius = 4)
fig.add_layer(heat_layer)
#Display figure
fig
Figure(layout=FigureLayout(height='420px'))
# Narrow down the DataFrame to find your ideal weather condition.
filtered_weather_df = weather_df
# Drop any rows that don't contain all three conditions. Want to be sure the weather is ideal.
# A max temperature lower than 80 degrees but higher than 70.
filtered_weather_df = filtered_weather_df.loc[(filtered_weather_df["Max Temp"] < 80) & (filtered_weather_df["Max Temp"] > 70)]
# Wind speed less than 10 mph.
filtered_weather_df = filtered_weather_df.loc[filtered_weather_df["Wind Speed"] < 10]
# Zero cloudiness.
filtered_weather_df = filtered_weather_df.loc[filtered_weather_df["Cloudiness"] == 0]
# Drop any rows with null values
filtered_weather_df = filtered_weather_df.dropna()
filtered_weather_df
City | Lat | lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | |
---|---|---|---|---|---|---|---|---|---|
54 | sao felix do xingu | -6.64 | -51.99 | 78.24 | 70.0 | 0.0 | 3.18 | BR | 1.592599e+09 |
99 | deputatskiy | 69.30 | 139.90 | 73.22 | 33.0 | 0.0 | 3.33 | RU | 1.592599e+09 |
174 | ilebo | -4.32 | 20.58 | 70.11 | 51.0 | 0.0 | 1.45 | CD | 1.592600e+09 |
293 | ilhabela | -23.78 | -45.36 | 72.99 | 71.0 | 0.0 | 4.74 | BR | 1.592600e+09 |
337 | astara | 38.50 | 48.67 | 71.60 | 60.0 | 0.0 | 4.70 | AZ | 1.592600e+09 |
385 | bisceglie | 41.24 | 16.50 | 72.00 | 77.0 | 0.0 | 5.82 | IT | 1.592600e+09 |
386 | darnah | 32.77 | 22.64 | 73.83 | 57.0 | 0.0 | 8.32 | LY | 1.592600e+09 |
395 | san nicolas | 18.17 | 120.60 | 79.74 | 78.0 | 0.0 | 4.45 | PH | 1.592600e+09 |
412 | soyo | -6.13 | 12.37 | 73.40 | 89.0 | 0.0 | 5.82 | AO | 1.592600e+09 |
427 | ios | 36.73 | 25.28 | 73.00 | 60.0 | 0.0 | 4.70 | GR | 1.592600e+09 |
429 | kitob | 39.08 | 66.83 | 72.97 | 21.0 | 0.0 | 5.57 | UZ | 1.592600e+09 |
441 | santa isabel | -23.32 | -46.22 | 78.80 | 44.0 | 0.0 | 8.05 | BR | 1.592600e+09 |
hotel_df
.hotel_df = filtered_weather_df
# params dictionary to update each iteration
params = {
"radius": 5000,
"types": "lodging",
"key": g_key
}
for index, row in hotel_df.iterrows():
# get lat, lng from df
lat = row["Lat"]
lng = row["lng"]
# change location each iteration while leaving original params in place
params["location"] = f"{lat},{lng}"
base_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
# assemble url and make API request
print(f"Retrieving Results for Index {index}: {row['City']}.")
response = requests.get(base_url, params=params).json()
# print(json.dumps(response, indent=4, sort_keys=True))
# extract results
results = response['results']
try:
print(f"Closest hotel is {results[0]['name']}.")
hotel_df.loc[index, 'Hotel Name'] = results[0]['name']
except (KeyError, IndexError):
print("Missing field/result... skipping.")
print("------------")
Retrieving Results for Index 54: sao felix do xingu. Missing field/result... skipping. ------------ Retrieving Results for Index 99: deputatskiy. Closest hotel is Baza Otdykha. ------------ Retrieving Results for Index 174: ilebo. Closest hotel is Hôtel des palmes. ------------ Retrieving Results for Index 293: ilhabela. Missing field/result... skipping. ------------ Retrieving Results for Index 337: astara. Missing field/result... skipping. ------------ Retrieving Results for Index 385: bisceglie. Missing field/result... skipping. ------------ Retrieving Results for Index 386: darnah. Missing field/result... skipping. ------------ Retrieving Results for Index 395: san nicolas. Closest hotel is NORTHVIEW Hotel. ------------ Retrieving Results for Index 412: soyo. Missing field/result... skipping. ------------ Retrieving Results for Index 427: ios. Closest hotel is Ios Palace Hotel & Spa. ------------ Retrieving Results for Index 429: kitob. Closest hotel is ULUG'BEK Hotel&Spa. ------------ Retrieving Results for Index 441: santa isabel. Closest hotel is Chácara Sombra do Altíssimo. ------------
hotel_df
City | Lat | lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | Hotel Name | |
---|---|---|---|---|---|---|---|---|---|---|
54 | sao felix do xingu | -6.64 | -51.99 | 78.24 | 70.0 | 0.0 | 3.18 | BR | 1.592599e+09 | NaN |
99 | deputatskiy | 69.30 | 139.90 | 73.22 | 33.0 | 0.0 | 3.33 | RU | 1.592599e+09 | Baza Otdykha |
174 | ilebo | -4.32 | 20.58 | 70.11 | 51.0 | 0.0 | 1.45 | CD | 1.592600e+09 | Hôtel des palmes |
293 | ilhabela | -23.78 | -45.36 | 72.99 | 71.0 | 0.0 | 4.74 | BR | 1.592600e+09 | NaN |
337 | astara | 38.50 | 48.67 | 71.60 | 60.0 | 0.0 | 4.70 | AZ | 1.592600e+09 | NaN |
385 | bisceglie | 41.24 | 16.50 | 72.00 | 77.0 | 0.0 | 5.82 | IT | 1.592600e+09 | NaN |
386 | darnah | 32.77 | 22.64 | 73.83 | 57.0 | 0.0 | 8.32 | LY | 1.592600e+09 | NaN |
395 | san nicolas | 18.17 | 120.60 | 79.74 | 78.0 | 0.0 | 4.45 | PH | 1.592600e+09 | NORTHVIEW Hotel |
412 | soyo | -6.13 | 12.37 | 73.40 | 89.0 | 0.0 | 5.82 | AO | 1.592600e+09 | NaN |
427 | ios | 36.73 | 25.28 | 73.00 | 60.0 | 0.0 | 4.70 | GR | 1.592600e+09 | Ios Palace Hotel & Spa |
429 | kitob | 39.08 | 66.83 | 72.97 | 21.0 | 0.0 | 5.57 | UZ | 1.592600e+09 | ULUG'BEK Hotel&Spa |
441 | santa isabel | -23.32 | -46.22 | 78.80 | 44.0 | 0.0 | 8.05 | BR | 1.592600e+09 | Chácara Sombra do Altíssimo |
# NOTE: Do not change any of the code in this cell
locations = hotel_df[["Lat", "lng"]]
markers = gmaps.marker_layer(locations)
# Using the template add the hotel marks to the heatmap
info_box_template = """
<dl>
<dt>Name</dt><dd>{Hotel Name}</dd>
<dt>City</dt><dd>{City}</dd>
<dt>Country</dt><dd>{Country}</dd>
</dl>
"""
# Store the DataFrame Row
# NOTE: be sure to update with your DataFrame name
hotel_info = [info_box_template.format(**row) for index, row in hotel_df.iterrows()]
# Add marker layer ontop of heat map
hotel_layer = gmaps.symbol_layer(
locations, fill_color='rgba(0, 150, 0, 0.4)',
stroke_color='rgba(0, 0, 150, 0.4)', scale=2,
info_box_content=hotel_info
)
# Display figure
fig.add_layer(markers)
fig.add_layer(hotel_layer)
fig
Figure(layout=FigureLayout(height='420px'))