In [1]:
import numpy as np
import gmaps
import gmaps.datasets
import os
import requests
from ipywidgets.embed import embed_minimal_html
from scipy.interpolate import Rbf
from time import sleep
In [2]:
f = open('id-mappings/city-ids.txt', 'r')
cities = f.readlines()
cities = map(lambda elem: elem.split(","), cities)[1:]
cities = map(lambda elem: [elem[0], elem[1], elem[2], elem[3][:-1]], cities)
cities = filter(lambda elem: elem[2].isdigit(), cities)
In [3]:
india_data = {}
for elem in cities:
_, stateName, _, cityName = elem
f = open('data/{}_{}.txt'.format(stateName, cityName), 'r')
city_data = f.readlines()
city_data = filter(lambda elem: elem != "\n", city_data)
city_data = map(lambda elem: elem.split(","), city_data)
city_data = filter(lambda elem: elem[0] == "2016", city_data)
city_data = map(lambda elem: float(elem[1].rstrip("\n")), city_data)
if len(city_data) > 0:
print city_data
india_data[(stateName, cityName)] = np.mean(city_data)
In [4]:
india_data = [[k, v] for (k, v) in india_data.iteritems()]
india_data = sorted(india_data, key=lambda x: x[1])
In [5]:
gmaps.configure(api_key=os.environ["GOOGLE_API_KEY"])
In [6]:
def decode_address_to_coordinates(address):
params = {
'address' : address,
'sensor' : 'false',
}
url = 'http://maps.google.com/maps/api/geocode/json'
r = requests.get(url, params = params)
return r.json()['results'][0]['geometry']['location']
In [8]:
locations = []
for (state, city), val in india_data:
locations.append([(state, city), decode_address_to_coordinates("{}, {}".format(city, state)).values()])
In [9]:
india_coordinates = decode_address_to_coordinates("India").values()
fig = gmaps.figure(center=india_coordinates, zoom_level=4)
weights = map(lambda x: x[1], india_data)
coordinates = map(lambda x: x[1], locations)
heatmap_layer = gmaps.heatmap_layer(coordinates, weights=weights)
heatmap_layer.max_intensity = 200
heatmap_layer.point_radius = 2.0
heatmap_layer.dissipating = False
fig.add_layer(heatmap_layer)
info_box_template = """
<div>
<p><b>City:</b> {0}, {1}</p>
<p><b>PM2.5:</b> {2:.2f}</p>
</div>
"""
city_info = [info_box_template.format(city_data[0][1], city_data[0][0], city_data[1]) for city_data in india_data]
marker_layer = gmaps.marker_layer(coordinates, info_box_content=city_info)
fig.add_layer(marker_layer)
fig
In [10]:
embed_minimal_html('national-aq.html', views=[fig])
In [11]:
for d in india_data[:5]:
print "{0}: {1:.2f} ug/m3".format(d[0][1], d[1])
for d in india_data[-5:]:
print "{0}: {1:.2f} ug/m3".format(d[0][1], d[1])
In [13]:
coordinates = np.array(coordinates)
In [14]:
x = coordinates[:, 0]
y = coordinates[:, 1]
In [15]:
# Interpolating and plotting again
rbfi = Rbf(x, y, weights, function = "inverse")
In [16]:
# sleep(0.05)
data = open('indian-cities.csv', 'r').readlines()
data = map(lambda x: x.split("\r"), data)[0]
In [17]:
cities = []
for city in data:
cities.append(decode_address_to_coordinates("{}, India".format(city)).values())
sleep(1)
In [18]:
cities = np.array(cities)
print cities
In [19]:
aq = rbfi(cities[:, 0], cities[:, 1])
aq = map(lambda x: x if x > 0.0 else 0.0, aq)
print aq
In [20]:
fig = gmaps.figure(center=india_coordinates, zoom_level=4)
info_box_template = """
<div>
<p><b>City:</b> {0}</p>
<p><b>PM2.5:</b> {1:.2f}</p>
</div>
"""
city_info = [info_box_template.format(data[i], aq[i]) for i in range(0, len(data))]
marker_layer = gmaps.marker_layer(cities, info_box_content=city_info)
fig.add_layer(marker_layer)
fig
In [21]:
embed_minimal_html('national-aq-interp.html', views=[fig])
In [22]:
delhi_coordinates = decode_address_to_coordinates("New Delhi").values()
In [7]:
f = open('data/Delhi_Delhi.txt', 'r')
delhi_data = f.readlines()
delhi_data = filter(lambda elem: elem != "\n", delhi_data)
delhi_data = map(lambda elem: elem.split(","), delhi_data)
filtered_delhi_data = []
station = -1
for line in delhi_data:
if len(line) > 2 and "station" in line[2]:
station = line[2].split(":")[1].rstrip("\n")
if line[0] == "2016":
filtered_delhi_data.append([station, line[1].rstrip("\n")])
delhi_data = filtered_delhi_data
f = open('id-mappings/station-ids.txt', 'r')
stations = f.readlines()
stations = map(lambda elem: elem.split(","), stations)
stations = filter(lambda elem: elem[2] == "85" and elem[4].isdigit(), stations)
stations = {station[4]:station[5].rstrip("\n") for station in stations}
print stations, delhi_data
In [8]:
delhi_station_coordinates = []
for station in delhi_data:
delhi_station_coordinates.append(decode_address_to_coordinates("{}, Delhi".format(stations[station[0]])).values())
sleep(1)
print delhi_station_coordinates
In [9]:
delhi_coordinates = decode_address_to_coordinates("New Delhi, Delhi").values()
fig = gmaps.figure(center=delhi_coordinates, zoom_level=11)
weights = np.array(delhi_data)[:, 1]
heatmap_layer = gmaps.heatmap_layer(delhi_station_coordinates, weights=weights)
heatmap_layer.max_intensity = 200
heatmap_layer.point_radius = 35.0
fig.add_layer(heatmap_layer)
info_box_template = """
<div>
<p><b>Station:</b> {0}</p>
<p><b>PM2.5:</b> {1}</p>
</div>
"""
print delhi_data
station_info = [info_box_template.format(stations[d[0]], d[1]) for d in delhi_data]
marker_layer = gmaps.marker_layer(delhi_station_coordinates, info_box_content=station_info)
fig.add_layer(marker_layer)
fig
In [10]:
embed_minimal_html('delhi-aq-pm25.html', views=[fig])
In [ ]: