# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # VacationPy
# ----
#
# #### Note
# * Keep an eye on your API usage. Use https://developers.google.com/maps/reporting/gmp-reporting as reference for how to monitor your usage and billing.
#
# * Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.

# +
# 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
# -

# ### Store Part I results into DataFrame
# * Load the csv exported in Part I to a DataFrame

# +
# To get File Path
file_path = "../WeatherPy/output_data/cities.csv"

cities_df = pd.read_csv(file_path)

cities_df['Date'] = cities_df['Date'].astype(int)
cities_df
# -

# ### Humidity Heatmap
# * Configure gmaps.
# * Use the Lat and Lng as locations and Humidity as the weight.
# * Add Heatmap layer to map.

gmaps.configure(api_key=g_key)

locations = cities_df[['Lat','Lng']]
locations

humidity_cities = cities_df['Humidity']
humidity_cities
# cities_df['Humidity'].max()

# +
max_humidity = humidity_cities.max()


# Plot Heatmap
fig = gmaps.figure(center=(0,0),zoom_level=2)

# Create heat layer
heat_layer = gmaps.heatmap_layer(locations, weights=humidity_cities, 
                                 dissipating=False, max_intensity=max_humidity,
                                 point_radius=3, opacity=0.6)


# Add layer
fig.add_layer(heat_layer)

# Display figure
fig
# -

# ### Create new DataFrame fitting weather criteria
# * Narrow down the cities to fit weather conditions.
# * Drop any rows will null values.

# +
# A max temperature lower than 80 degrees but higher than 70.
# Wind speed less than 10 mph.
# Zero cloudiness.
# Drop any rows that don’t contain all three conditions. You want to be sure the weather is ideal.

weather_fit_df = cities_df.loc[(cities_df['Max Temp'] < 80) & (cities_df['Max Temp'] > 70) & (cities_df['Wind Speed'] < 10) & (cities_df['Cloudiness'] == 0)]

weather_fit_df
# -

# ### Hotel Map
# * Store into variable named `hotel_df`.
# * Add a "Hotel Name" column to the DataFrame.
# * Set parameters to search for hotels with 5000 meters.
# * Hit the Google Places API for each city's coordinates.
# * Store the first Hotel result into the DataFrame.
# * Plot markers on top of the heatmap.

hotel_df = weather_fit_df[['City','Lat','Lng','Country']].copy()
# weather_fit_df
hotel_df["Hotel Name"]=""
hotel_df

# +
from pprint import pprint

# To get Parameters for gmap
params={
    "radius":5000,
    "types":'lodging',
    "key": g_key
}

base_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"

# To get first Hotel Name nearby each city and add it to dataFrame
for index, row in weather_fit_df.iterrows():
    
    params["location"] = f"{row['Lat']},{row['Lng']}"
    
    response = requests.get(base_url, params=params)
    json_response = response.json()
    
    try:
        hotel_df.loc[index, "Hotel Name"] = json_response["results"][0]["name"]
    except:
        print("Missing field/result... skipping.")
        pass
    
hotel_df

# +
# NOTE: Do not change any of the code in this cell

# 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()]
locations = hotel_df[["Lat", "Lng"]]

# +
# Add marker layer ontop of heat map

markers = gmaps.marker_layer(locations)

hotel_layer = gmaps.symbol_layer(
    locations, fill_color='rgba(0, 150, 0, 0.4)',
    stroke_color='rgba(0, 0, 150, 0.4)', scale=4,
    info_box_content=hotel_info
)

# Display figure
fig.add_layer(markers)
fig.add_layer(hotel_layer)

fig
# -



StarcoderdataJupyterScriptsDedupFiltered