-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
281 lines (251 loc) · 8.72 KB
/
analysis.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# General libraries
import pandas as pd
import numpy as np
import json
import pprint
import regex as re
# Import dash
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
# Plotting libraries
import geopandas as gpd
import matplotlib.pyplot as plt
import folium
from branca.element import Template, MacroElement
def calc_cases():
central_data = gpd.read_file("data/dengue-cases-central-geojson.geojson")
ne_data = gpd.read_file("data/dengue-cases-north-east-geojson.geojson")
se_data = gpd.read_file("data/dengue-cases-south-east-geojson.geojson")
sw_data = gpd.read_file("data/dengue-cases-south-west-geojson.geojson")
central_cases = sum(
central_data.Description.map(
lambda x: int(re.search(r"(<td>.*?</td>)", x).group(0)[4:-5])
)
)
ne_cases = sum(
ne_data.Description.map(
lambda x: int(re.search(r"(<td>.*?</td>)", x).group(0)[4:-5])
)
)
se_cases = sum(
se_data.Description.map(
lambda x: int(re.search(r"(<td>.*?</td>)", x).group(0)[4:-5])
)
)
sw_cases = sum(
sw_data.Description.map(
lambda x: int(re.search(r"(<td>.*?</td>)", x).group(0)[4:-5])
)
)
total_cases = sum([central_cases, ne_cases, se_cases, sw_cases])
return [total_cases, central_cases, ne_cases, se_cases, sw_cases]
def map_graph():
# GeoJson Data
cluster_gj = gpd.read_file("data/dengue-clusters-geojson.geojson")
area_high_ades_gj = gpd.read_file(
"data/areas-with-high-aedes-population-geojson.geojson"
)
# Creation of map
kw = {"location": [1.3521, 103.8198], "zoom_start": 12}
m = folium.Map(**kw)
# Styles
area_high_ades_style = {"fillColor": "#228B22", "color": "#FF0000"}
# Modifying tooltip
cluster_gj.Description = cluster_gj.Description.map(
lambda x: re.search(r"(<td>.*?</td>)", x).group(0)[4:-5]
)
area_high_ades_gj.Description = area_high_ades_gj.Description.map(
lambda x: re.search(r"(<td>.*?</td>)", x).group(0)[4:-5]
)
# Addition of layers to map
folium.GeoJson(
cluster_gj,
tooltip=folium.GeoJsonTooltip(
fields=["Description"], aliases=["Location"], localize=True
),
).add_to(m)
folium.GeoJson(
area_high_ades_gj,
style_function=lambda x: area_high_ades_style,
tooltip=folium.GeoJsonTooltip(
fields=["Description"], aliases=["Location"], localize=True
),
).add_to(m)
# Legend
# Template from: https://nbviewer.jupyter.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd
template = """
{% macro html(this, kwargs) %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#maplegend" ).draggable({
start: function (event, ui) {
$(this).css({
right: "auto",
top: "auto",
bottom: "auto"
});
}
});
});
</script>
</head>
<body>
<div id='maplegend' class='maplegend'
style='position: absolute; z-index:9999; border:2px solid grey; background-color:rgba(255, 255, 255, 0.8);
border-radius:6px; padding: 10px; font-size:14px; right: 20px; bottom: 20px;'>
<div class='legend-title'>Legend</div>
<div class='legend-scale'>
<ul class='legend-labels'>
<li><span style='background:#73adff;opacity:0.9;'></span>Dengue clusters</li>
<li><span style='background:#FF0000;opacity:0.7;'></span>Areas with high ades population</li>
</ul>
</div>
</div>
</body>
</html>
<style type='text/css'>
.maplegend .legend-title {
text-align: left;
margin-bottom: 5px;
font-weight: bold;
font-size: 90%;
}
.maplegend .legend-scale ul {
margin: 0;
margin-bottom: 5px;
padding: 0;
float: left;
list-style: none;
}
.maplegend .legend-scale ul li {
font-size: 80%;
list-style: none;
margin-left: 0;
line-height: 18px;
margin-bottom: 2px;
}
.maplegend ul.legend-labels li span {
display: block;
float: left;
height: 16px;
width: 30px;
margin-right: 5px;
margin-left: 0;
border: 1px solid #999;
}
.maplegend .legend-source {
font-size: 80%;
color: #777;
clear: both;
}
.maplegend a {
color: #777;
}
</style>
{% endmacro %}"""
macro = MacroElement()
macro._template = Template(template)
m.get_root().add_child(macro)
m.save("dengue-cluster.html")
return m
# map_graph()
analysisTab = dbc.Card(
[
dbc.CardBody(
[
# Overview
html.P("Accurate as of: 02 Aug 2020"),
dbc.Row(
[
dbc.Col(
dbc.Card(
[
dbc.CardHeader(
html.H6(id="case_1", children=["Total Cases"],)
),
dbc.CardBody(html.H4(calc_cases()[0])),
],
color="danger",
)
),
dbc.Col(
dbc.Card(
[
dbc.CardHeader(
html.H6(
id="case_2", children=["Central Cases"],
)
),
dbc.CardBody(html.H4(calc_cases()[1])),
],
color="secondary",
)
),
dbc.Col(
dbc.Card(
[
dbc.CardHeader(
html.H6(
id="case_3", children=["North-East Cases"],
)
),
dbc.CardBody(html.H4(calc_cases()[2])),
],
color="success",
)
),
dbc.Col(
dbc.Card(
[
dbc.CardHeader(
html.H6(
id="case_4", children=["South-East Cases"],
)
),
dbc.CardBody(html.H4(calc_cases()[3])),
],
color="warning",
)
),
dbc.Col(
dbc.Card(
[
dbc.CardHeader(
html.H6(
id="case_5", children=["South-West Cases"],
)
),
dbc.CardBody(html.H4(calc_cases()[4])),
],
color="info",
)
),
]
),
html.Br(),
## Graphs
# Dengue cluster
html.P("Accurate as of: 02 Aug 2020"),
html.Iframe(
id="dengue-map",
srcDoc=open("dengue-cluster.html", "r").read(),
width="100%",
height="500",
),
html.Br(),
]
)
]
)