Reference: https://projects.raspberrypi.org/en/projects/mapping-the-weather/9
Using data from KDD CUP of Fresh Air.
import numpy as np
import pandas as pd
import folium
bj_aq = pd.read_csv("../data/bj_201803_renamed_aq.csv")
stations = pd.read_csv("../data/Beijing_AirQuality_Stations.csv")
stations.head()
station_id | Longitude | Latitude | type | |
---|---|---|---|---|
0 | dongsi_aq | 116.417 | 39.929 | 城区 |
1 | tiantan_aq | 116.407 | 39.886 | 城区 |
2 | guanyuan_aq | 116.339 | 39.929 | 城区 |
3 | wanshouxigong_aq | 116.352 | 39.878 | 城区 |
4 | aotizhongxin_aq | 116.397 | 39.982 | 城区 |
map_hooray = folium.Map(
location=[39.929, 116.417],
tiles = "Stamen Terrain",
zoom_start = 10) # Uses lat then lon. The bigger the zoom number, the closer in you get
map_hooray # Calls the map to display
data = pd.merge(bj_aq[bj_aq["time"]=="2017-01-01 14:00:00"], stations, on="station_id")
data.head()
station_id | time | PM25_Concentration | PM10_Concentration | NO2_Concentration | CO_Concentration | O3_Concentration | SO2_Concentration | Longitude | Latitude | type | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | aotizhongxin_aq | 2017-01-01 14:00:00 | 453.0 | 467.0 | 156.0 | 7.2 | 3.0 | 9.0 | 116.397 | 39.982 | 城区 |
1 | badaling_aq | 2017-01-01 14:00:00 | 87.0 | 115.0 | 84.0 | 1.5 | 2.0 | 49.0 | 115.988 | 40.365 | 对照点及区域点 |
2 | beibuxinqu_aq | 2017-01-01 14:00:00 | 479.0 | 487.0 | 166.0 | 7.4 | 4.0 | 9.0 | 116.174 | 40.090 | 城区 |
3 | daxing_aq | 2017-01-01 14:00:00 | 352.0 | 488.0 | 125.0 | 5.8 | 5.0 | 8.0 | 116.404 | 39.718 | 郊区 |
4 | dingling_aq | 2017-01-01 14:00:00 | 339.0 | 372.0 | 137.0 | 5.9 | 6.0 | 18.0 | 116.220 | 40.292 | 对照点及区域点 |
map_hooray = folium.Map(
location=[39.929, 116.8],
tiles = "Stamen Terrain",
zoom_start = 9)
for _, row in data.iterrows():
folium.Marker([
row["Latitude"],row["Longitude"]],
popup=row["station_id"]).add_to(map_hooray)
map_hooray
def colourgrad(minimum, maximum, value):
minimum, maximum = float(minimum), float(maximum)
ratio = (value-minimum) / (maximum - minimum)
g = int(max(0, 128*(1 - ratio)))
r = int(max(0, 128*(1 - ratio) + 127))
b = 0
hexcolour = '#%02x%02x%02x' % (r,g,b)
return hexcolour
data = pd.merge(bj_aq[bj_aq["time"]=="2017-01-01 15:00:00"], stations, on="station_id")
map_hooray = folium.Map(
location=[39.929, 116.8],
tiles = "Stamen Terrain",
zoom_start = 9)
for _, row in data.iterrows():
color = colourgrad(0, 500, min(row["PM25_Concentration"], 500))
folium.CircleMarker([
row["Latitude"],row["Longitude"]],
color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,
popup=row["station_id"] + ":" + str(row["PM25_Concentration"])).add_to(map_hooray)
# CWD = os.getcwd()
# map_ws.save('osm.html')
# webbrowser.open_new('file://'+CWD+'/'+'osm.html')
map_hooray
data = pd.merge(bj_aq[bj_aq["time"]=="2017-01-01 18:00:00"], stations, on="station_id")
map_hooray = folium.Map(
location=[39.929, 116.8],
tiles = "Stamen Terrain",
zoom_start = 9)
for _, row in data.iterrows():
color = colourgrad(0, 500, min(row["PM25_Concentration"], 500))
folium.CircleMarker([
row["Latitude"],row["Longitude"]],
color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,
popup=row["station_id"] + ":" + str(row["PM25_Concentration"])).add_to(map_hooray)
map_hooray
data = pd.merge(bj_aq[bj_aq["time"]=="2017-01-01 20:00:00"], stations, on="station_id")
map_hooray = folium.Map(
location=[39.929, 116.8],
tiles = "Stamen Terrain",
zoom_start = 9)
for _, row in data.iterrows():
color = colourgrad(0, 500, min(row["PM25_Concentration"], 500))
folium.CircleMarker([
row["Latitude"],row["Longitude"]],
color=color, radius=9, fill_opacity=1, fill=True, fill_color=color,
popup=row["station_id"] + ":" + str(row["PM25_Concentration"])).add_to(map_hooray)
map_hooray