-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_api.py
114 lines (85 loc) · 3.61 KB
/
data_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import pandas as pd
from pandas.io.json import json_normalize
import requests
from tabulate import tabulate
from sklearn.cluster import KMeans
import random
import numpy as np
import pandas as pd
import folium
#Fetching data form HERE API for IIT Bombay
url = 'https://discover.search.hereapi.com/v1/discover?in=circle:19.1334,72.9133;r=10000&q=apartment&apiKey=uJHMEjeagmFGldXp661-pDMf4R-PxvWIu7I68UjYC5Q'
data = requests.get(url).json()
d=json_normalize(data['items'])
d.to_csv('api-data/apartment.csv')
#Cleaning API data
d2=d[['title','address.label','distance','access','position.lat','position.lng','address.postalCode','contacts','id']]
d2.to_csv('api-data/cleaned_apartment.csv')
#Counting no. of cafes, department stores and gyms near apartments around IIT Bombay
df_final=d2[['position.lat','position.lng']]
CafeList=[]
DepList=[]
GymList=[]
latitudes = list(d2['position.lat'])
longitudes = list( d2['position.lng'])
for lat, lng in zip(latitudes, longitudes):
radius = '1000' #Set the radius to 1000 metres
latitude=lat
longitude=lng
search_query = 'cafe' #Search for any cafes
url = 'https://discover.search.hereapi.com/v1/discover?in=circle:{},{};r={}&q={}&apiKey=uJHMEjeagmFGldXp661-pDMf4R-PxvWIu7I68UjYC5Q'.format(latitude, longitude, radius, search_query)
results = requests.get(url).json()
venues=json_normalize(results['items'])
CafeList.append(venues['title'].count())
search_query = 'gym' #Search for any gyms
url = 'https://discover.search.hereapi.com/v1/discover?in=circle:{},{};r={}&q={}&apiKey=uJHMEjeagmFGldXp661-pDMf4R-PxvWIu7I68UjYC5Q'.format(latitude, longitude, radius, search_query)
results = requests.get(url).json()
venues=json_normalize(results['items'])
GymList.append(venues['title'].count())
search_query = 'department-store' #search for supermarkets
url = 'https://discover.search.hereapi.com/v1/discover?in=circle:{},{};r={}&q={}&apiKey=uJHMEjeagmFGldXp661-pDMf4R-PxvWIu7I68UjYC5Q'.format(latitude, longitude, radius, search_query)
results = requests.get(url).json()
venues=json_normalize(results['items'])
DepList.append(venues['title'].count())
df_final['Cafes'] = CafeList
df_final['Department Stores'] = DepList
df_final['Gyms'] = GymList
print(tabulate(df_final,headers='keys',tablefmt='github'))
#Run K-means clustering on dataframe
kclusters = 3
kmeans = KMeans(n_clusters=kclusters, random_state=0).fit(df_final)
df_final['Cluster']=kmeans.labels_
df_final['Cluster']=df_final['Cluster'].apply(str)
print(tabulate(df_final,headers='keys',tablefmt='github'))
#Plotting clustered locations on map using Folium
#define coordinates of the college
map_bom=folium.Map(location=[19.1334,72.9133],zoom_start=12)
# instantiate a feature group for the incidents in the dataframe
locations = folium.map.FeatureGroup()
# set color scheme for the clusters
def color_producer(cluster):
if cluster=='0':
return 'green'
elif cluster=='1':
return 'orange'
else:
return 'red'
latitudes = list(df_final['position.lat'])
longitudes = list(df_final['position.lng'])
labels = list(df_final['Cluster'])
names=list(d2['title'])
for lat, lng, label,names in zip(latitudes, longitudes, labels,names):
folium.CircleMarker(
[lat,lng],
fill=True,
fill_opacity=1,
popup=folium.Popup(names, max_width = 300),
radius=5,
color=color_producer(label)
).add_to(map_bom)
# add locations to map
map_bom.add_child(locations)
folium.Marker([19.1334,72.9133],popup='IIT Bombay').add_to(map_bom)
#saving the map
map_bom.save("map-IITBombay.html")