In this capstone stone project, I will be Analysing Hotels in Lagos, Nigeria. In recent years, Business Relationship between Nigeria and China has been growwing with Chinese companies and expertries taking over majority of infrastructural works in Africa's biggest economy. This has led to the influx of Chinese in high proportions to Nigeria leading to demands in Chinese foods and restaurants. In this work I intend to use the foursquare location data to explore chinese restaurants in Lagos which is the commercial center of Nigeria so as to provide recommendation. This work will demostrate in details how to make calls to the Foursquare API, construct a URL to send a request to the API to search for a specific type of venues and to explore a particular venue, geographical location, and to get trending venues around a location. I will also, show how to use the visualization library, Folium, to visualize the results.
import requests # library to handle requests
import pandas as pd # library for data analsysis
import numpy as np # library to handle data in a vectorized manner
import random # library for random number generation
!conda install -c conda-forge geopy --yes
from geopy.geocoders import Nominatim # module to convert an address into latitude and longitude values
# libraries for displaying images
from IPython.display import Image
from IPython.core.display import HTML
# tranforming json file into a pandas dataframe library
from pandas.io.json import json_normalize
!conda install -c conda-forge folium=0.5.0 --yes
import folium # plotting library
print('Folium installed')
print('Libraries imported.')
CLIENT_ID = 'HUIXUMHELPDOY0XYRWPT3XMBZLIM54IC1TUCM4RUWKFIKMZL' # your Foursquare ID
CLIENT_SECRET = '1QYPDRIYN41TVXGFBMFIMDUG1WLFW2SEDMPOYCJCP1NOCBJ1' # your Foursquare Secret
VERSION = '20200426'
LIMIT = 30
print('Your credentails:')
print('CLIENT_ID: ' + CLIENT_ID)
print('CLIENT_SECRET:' + CLIENT_SECRET)
In order to define an instance of the geocoder, we need to define a user_agent. We will name our agent foursquare_agent, as shown below.
address = 'Oshodi Rd, Oshodi-Isolo, Lagos'
geolocator = Nominatim(user_agent="foursquare_agent")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print(latitude, longitude)
https://api.foursquare.com/v2/venues/
search?client_id=
CLIENT_ID&client_secret=
CLIENT_SECRET&ll=
LATITUDE,
LONGITUDE&v=
VERSION&query=
QUERY&radius=
RADIUS&limit=
LIMIT
search_query = 'Market'
radius = 1000
print(search_query + ' .... OK!')
url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION, search_query, radius, LIMIT)
url
results = requests.get(url).json()
results
# assign relevant part of JSON to venues
venues = results['response']['venues']
# tranform venues into a dataframe
dataframe = json_normalize(venues)
dataframe.head()
# keep only columns that include venue name, and anything that is associated with location
filtered_columns = ['name', 'categories'] + [col for col in dataframe.columns if col.startswith('location.')] + ['id']
dataframe_filtered = dataframe.loc[:, filtered_columns]
# function that extracts the category of the venue
def get_category_type(row):
try:
categories_list = row['categories']
except:
categories_list = row['venue.categories']
if len(categories_list) == 0:
return None
else:
return categories_list[0]['name']
# filter the category for each row
dataframe_filtered['categories'] = dataframe_filtered.apply(get_category_type, axis=1)
# clean column names by keeping only last term
dataframe_filtered.columns = [column.split('.')[-1] for column in dataframe_filtered.columns]
dataframe_filtered
dataframe_filtered.name
venues_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map of chinese restaurants aroung Federal Palace Hotel
# add a red circle marker to represent the Federal Palce Hotel
folium.features.CircleMarker(
[latitude, longitude],
radius=10,
color='red',
popup='Federal Palace Hotel',
fill = True,
fill_color = 'red',
fill_opacity = 0.6
).add_to(venues_map)
# add the Chinese restaurants as blue circle markers
for lat, lng, label in zip(dataframe_filtered.lat, dataframe_filtered.lng, dataframe_filtered.categories):
folium.features.CircleMarker(
[lat, lng],
radius=5,
color='blue',
popup=label,
fill = True,
fill_color='blue',
fill_opacity=0.6
).add_to(venues_map)
# display map
venues_map
https://api.foursquare.com/v2/venues/
VENUE_ID?client_id=
CLIENT_ID&client_secret=
CLIENT_SECRET&v=
VERSION
venue_id = '4bd060aeb221c9b62b84d3d0' # ID of Airforce Baase Restaurant
url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)
url
result = requests.get(url).json()
print(result['response']['venue'].keys())
result['response']['venue']
try:
print(result['response']['venue']['rating'])
except:
print('This venue has not been rated yet.')
venue_id = '51741aa0498efc18be344c96' # ID of New China Restaurant
url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)
result = requests.get(url).json()
try:
print(result['response']['venue']['rating'])
except:
print('This venue has no rating yet.')
venue_id = '4e778af91f6e072f1497d5e' # ID of Saffron Restaurant
url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)
result = requests.get(url).json()
try:
print(result['response']['venue']['rating'])
except:
print('This venue has no rating yet.')
https://api.foursquare.com/v2/venues/
explore?client_id=
CLIENT_ID&client_secret=
CLIENT_SECRET&ll=
LATITUDE,
LONGITUDE&v=
VERSION&limit=
LIMIT
We first define the Longitude and Latitude of the Hotel
latitude = 6.5581735
longitude = 3.3466245
url = 'https://api.foursquare.com/v2/venues/explore?client_id={}&client_secret={}&ll={},{}&v={}&radius={}&limit={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION, radius, LIMIT)
url
import requests
results = requests.get(url).json()
'There are {} around The Federal Palace Hotel.'.format(len(results['response']['groups'][0]['items']))
items = results['response']['groups'][0]['items']
items[0]
dataframe = json_normalize(items) # flatten JSON
# filter columns
filtered_columns = ['venue.name', 'venue.categories'] + [col for col in dataframe.columns if col.startswith('venue.location.')] + ['venue.id']
dataframe_filtered = dataframe.loc[:, filtered_columns]
# filter the category for each row
dataframe_filtered['venue.categories'] = dataframe_filtered.apply(get_category_type, axis=1)
# clean columns
dataframe_filtered.columns = [col.split('.')[-1] for col in dataframe_filtered.columns]
dataframe_filtered.head()
venues_map = folium.Map(location=[latitude, longitude], zoom_start=15) # generate map centred around the federal palace hotel
# add Ecco as a red circle mark
folium.features.CircleMarker(
[latitude, longitude],
radius=10,
popup='Ecco',
fill=True,
color='red',
fill_color='red',
fill_opacity=0.6
).add_to(venues_map)
# add popular spots to the map as blue circle markers
for lat, lng, label in zip(dataframe_filtered.lat, dataframe_filtered.lng, dataframe_filtered.categories):
folium.features.CircleMarker(
[lat, lng],
radius=5,
popup=label,
fill=True,
color='blue',
fill_color='blue',
fill_opacity=0.6
).add_to(venues_map)
# display map
venues_map
https://api.foursquare.com/v2/venues/
trending?client_id=
CLIENT_ID&client_secret=
CLIENT_SECRET&ll=
LATITUDE,
LONGITUDE&v=
VERSION
# define URL
url = 'https://api.foursquare.com/v2/venues/trending?client_id={}&client_secret={}&ll={},{}&v={}'.format(CLIENT_ID, CLIENT_SECRET, latitude, longitude, VERSION)
# send GET request and get trending venues
results = requests.get(url).json()
results
if len(results['response']['venues']) == 0:
trending_venues_df = 'No trending venues are available at the moment!'
else:
trending_venues = results['response']['venues']
trending_venues_df = json_normalize(trending_venues)
# filter columns
columns_filtered = ['name', 'categories'] + ['location.distance', 'location.city', 'location.postalCode', 'location.state', 'location.country', 'location.lat', 'location.lng']
trending_venues_df = trending_venues_df.loc[:, columns_filtered]
# filter the category for each row
trending_venues_df['categories'] = trending_venues_df.apply(get_category_type, axis=1)
# display trending venues
trending_venues_df
# display map
venues_map