import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
In this notebook, I’ll show a quick example of how to use Folium (which internally uses LeafletJS) for visualising the location of air quality monitors in India. The purpose of this notebook is eductional in nature.
Standard Imports
Downloading data from OpenAQ for 2018-04-06
!wget --no-check-certificate https://openaq-data.s3.amazonaws.com/2018-04-06.csv -P /Users/nipun/Downloads/
--2020-02-29 17:52:50-- https://openaq-data.s3.amazonaws.com/2018-04-06.csv
Resolving openaq-data.s3.amazonaws.com (openaq-data.s3.amazonaws.com)... 52.216.99.123
Connecting to openaq-data.s3.amazonaws.com (openaq-data.s3.amazonaws.com)|52.216.99.123|:443... connected.
WARNING: cannot verify openaq-data.s3.amazonaws.com's certificate, issued by ‘CN=DigiCert Baltimore CA-2 G2,OU=www.digicert.com,O=DigiCert Inc,C=US’:
Unable to locally verify the issuer's authority.
HTTP request sent, awaiting response... 200 OK
Length: 133839107 (128M) [text/csv]
Saving to: ‘/Users/nipun/Downloads/2018-04-06.csv.1’
2018-04-06.csv.1 37%[======> ] 47.37M 3.79MB/s eta 40s ^C
import pandas as pd
= pd.read_csv("/Users/nipun/Downloads/2018-04-06.csv")
df = df[(df.country=='IN')&(df.parameter=='pm25')].dropna().groupby("location").mean() df
df
value | latitude | longitude | |
---|---|---|---|
location | |||
Adarsh Nagar, Jaipur - RSPCB | 79.916667 | 26.902909 | 75.836853 |
Anand Kala Kshetram, Rajamahendravaram - APPCB | 42.750000 | 16.987287 | 81.736318 |
Ardhali Bazar, Varanasi - UPPCB | 103.666667 | 25.350599 | 82.908307 |
Asanol Court Area, Asanol - WBPCB | 56.833333 | 23.685297 | 86.945968 |
Ashok Nagar, Udaipur - RSPCB | 114.750000 | 24.588617 | 73.632140 |
... | ... | ... | ... |
Vasundhara, Ghaziabad, UP - UPPCB | 223.333333 | 28.660335 | 77.357256 |
Vikas Sadan, Gurgaon, Haryana - HSPCB | 280.250000 | 28.450124 | 77.026305 |
Vindhyachal STPS, Singrauli - MPPCB | 144.000000 | 24.108970 | 82.645580 |
Ward-32 Bapupara, Siliguri - WBPCB | 195.000000 | 26.688305 | 88.412668 |
Zoo Park, Hyderabad - TSPCB | 82.500000 | 17.349694 | 78.451437 |
79 rows × 3 columns
Downloading World GeoJson file
!wget --no-check-certificate https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/world-countries.json
--2020-02-29 17:53:17-- https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/world-countries.json
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.8.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.8.133|:443... connected.
WARNING: cannot verify raw.githubusercontent.com's certificate, issued by ‘CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US’:
Unable to locally verify the issuer's authority.
HTTP request sent, awaiting response... 200 OK
Length: 252515 (247K) [text/plain]
Saving to: ‘world-countries.json’
world-countries.jso 100%[===================>] 246.60K 376KB/s in 0.7s
2020-02-29 17:53:19 (376 KB/s) - ‘world-countries.json’ saved [252515/252515]
Creating india.json correspdonding to Indian data
import json
= json.load(open('world-countries.json','r'))
e 'features'][73], open('india.json','w')) json.dump(e[
import folium
= folium.Map(width = '60%',height=800,location=[20, 77],
folium_map =5,
zoom_start="Stamen Terrain",min_lat=7, max_lat=35, min_lon=73, max_lon=90)
tilesfor x in df.iterrows():
= x[0]
name = x[1]['latitude'], x[1]['longitude']
lat, lon =5, color='#000000',fill_color='#D3D3D3' , fill_opacity=1).add_to(folium_map)
folium.CircleMarker([lat, lon], radius
'india.json').add_to(folium_map) folium.GeoJson(
<folium.features.GeoJson at 0x11e497bd0>
"map.html") folium_map.save(
There you go!Till next time.