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

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

input_path = "result.csv"
df = pd.read_csv(input_path)
del df["Unnamed: 0"]
# df = df.dropna()
df.head()


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

# +
locations = [(i,j) for i,j in zip(df["Lat"],df["Lng"])]
weight = df["Humidity"]

# Customize the size of the figure

figure_layout = {
    'width': '800px',
    'height': '500px',
    'border': '1px solid black',
    'padding': '1px',
    'margin': '0 auto 0 auto'
}


fig = gmaps.figure(layout=figure_layout)
max_intensity = max(weight)
# Create heat layer
heat_layer = gmaps.heatmap_layer(locations, weights=weight, 
                                 dissipating=False, max_intensity=max_intensity,
                                 point_radius=1.5)


# Add layer
fig.add_layer(heat_layer)

# Display figure
fig


# +
figure_layout = {
    'width': '800px',
    'height': '500px',
    'border': '1px solid black',
    'padding': '1px',
    'margin': '0 auto 0 auto'
}

fig = gmaps.figure(map_type="HYBRID", layout=figure_layout)

# Create heat layer
heat_layer = gmaps.heatmap_layer(locations, weights=weight, 
                                 dissipating=False, max_intensity=max_intensity,
                                 point_radius=1.5)

fig.add_layer(heat_layer)
fig
# -

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

# +
ideal_weather_df = df.loc[(df["Max Temp"]<80)&(df["Max Temp"]>70)&
                          (df["Wind Speeds"]<10)& (df["Cloudiness"]==0),:]

ideal_weather_df = ideal_weather_df.dropna()
ideal_weather_df = ideal_weather_df.reset_index(drop = True)
ideal_weather_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.

ideal_weather_df['Hotel Name'] = ""
ideal_weather_df

# +
hotel_df = []

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

params = {       
          "radius": 5000,
          "types" : "hotel",
          "key": g_key,
         }

# use iterrows to iterate through pandas dataframe

for index, row in ideal_weather_df.iterrows():
     
    lat = row['Lat']
    lng = row['Lng']

    # add location to params dict
    params["location"] = f"{lat},{lng}"

    # assemble url and make API request
    response = requests.get(base_url, params=params).json()
    try: 
        ideal_weather_df.loc[index, 'Hotel Name'] = response['results'][0]['name']
        hotel_df.append(response['results'][0]['name'])        
    except:
        ideal_weather_df.loc[index, 'Hotel Name'] = float("NaN")
        print("skip the city")       



# -

ideal_weather_df = ideal_weather_df.dropna()
ideal_weather_df

# ## Unfortunately the below codes are not working

# +
# 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
#narrowed_city_df=pd.DataFrame()
hotel_info = [info_box_template.format(**row) for index, row in ideal_weather_df.iterrows()]
locations = ideal_weather_df[["Lat", "Lng"]]
city = ideal_weather_df["City"]
# -

ideal_weather_df.shape

# +
# Add marker layer ontop of heat map

locations = ideal_weather_df[["Lat", "Lng"]]
# markers = gmaps.marker_layer(locations)
markers = gmaps.marker_layer(locations, info_box_content= [f"HOTEL NAME:  \n{hotel}"  for hotel in hotel_df])
fig.add_layer(markers)
fig


# Display Map
# -



StarcoderdataJupyterScriptsDedupFiltered