-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_covid_data.py
119 lines (92 loc) · 4.43 KB
/
plot_covid_data.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
115
116
117
118
119
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 17:01:50 2020
@author: sheldon
"""
import os
import pandas as pd
import folium
from folium import plugins
from folium import IFrame
import rasterio as rio
from rasterio.warp import calculate_default_transform, reproject, Resampling
import earthpy as et
import pdb
import flask
from flask import Flask
class CovidDF:
def __init__(self, url):
self.url = url
self.raw = None
self.aggregated = None
def reload(self, date):
self.raw = pd.read_csv(self.url)
self.group_by_regions(date)
def group_by_regions(self,date):
df=self.raw[['Province/State','Country/Region','Lat','Long',date]]
self.aggregated=df.groupby(['Country/Region']).agg({'Lat':'mean',
'Long':'mean',
date: 'sum'})
self.aggregated.at['France','Lat']=46.2276
self.aggregated.at['France','Long']=2.2137
self.aggregated.at['United Kingdom','Lat']=53.5
self.aggregated.at['United Kingdom','Long']=-2.5
self.aggregated.at['Denmark','Lat']=56
self.aggregated.at['Denmark','Long']=9.2
class CovidData(object):
def __init__(self):
self.confirmed_cases = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv')
self.deaths = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv')
self.recoveries = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv')
self.loaded = False
self.map = folium.Map(location=[10,20],
tiles = 'Stamen Terrain',
zoom_start=3)
def populate(self, date):
if not self.loaded:
self.confirmed_cases.reload(date)
self.deaths.reload(date)
self.recoveries.reload(date)
self.loaded=True
def plot_number_of_cases(self,df,date,custom_color,label):
dc=df.iloc[df[date].to_numpy().nonzero()]
latitude = dc.Lat.values.astype('float')
longitude = dc.Long.values.astype('float')
radius = dc[date].values.astype('float')
number_of_cases = dc[date].values.astype(str)
for la,lo,ra,nc in zip(latitude,longitude,radius,number_of_cases):
folium.Circle(
location=[la,lo],
radius=ra*10,
fill=True,
color=custom_color,
fill_color=custom_color,
fill_opacity=0.5
).add_child(folium.Popup(label+': '+nc)).add_to(self.map)
def plot_number_of_cases_for_all_dataframes(self,date):
self.plot_number_of_cases(self.confirmed_cases.aggregated,date,'blue','Confirmed cases')
self.plot_number_of_cases(self.deaths.aggregated,date,'red','Deaths')
self.plot_number_of_cases(self.recoveries.aggregated,date,'green','Recoveries')
my_date=pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv').columns[-1]
covid_data=CovidData()
covid_data.populate(my_date)
covid_data.plot_number_of_cases_for_all_dataframes(my_date)
legend_html = '''
<div style="position: fixed;
bottom: 50px; left: 50px; width: 300px; height: 100px;
border:2px solid grey; z-index:9999; font-size:14px;
"> Occurences of covid-19 cases by country <br>
Confirmed cases <i class="fa fa-circle" style="color:blue"></i><br>
Deaths <i class="fa fa-circle" style="color:red"></i><br>
Recoveries <i class="fa fa-circle" style="color:green"></i>
</div>
'''
covid_data.map.get_root().html.add_child(folium.Element(legend_html))
#covid_data.map.save("./mytest.html")
app = Flask(__name__)
@app.route("/")
def display_map():
return covid_data.map._repr_html_()
if __name__ == "__main__":
app.run(host='0.0.0.0', port=os.environ.get('PORT', 80))