-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
364 lines (311 loc) · 13.3 KB
/
app.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import dash
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash import dcc
from dash import html
# import inspect
import pandas as pd
import plotly.figure_factory as ff
import numpy as np
import math
# import plotly.express as px # (version 4.7.0)
# import plotly.graph_objects as go
# import json
import geopandas as gpd
LOGO = "https://raw.githubusercontent.com/lperozzi/personale_web/preview/data/logo_white.png"
cos = pd.read_csv('static/cos.csv')
# define earth radius (in meters) for hexagone resolution calculation)
earth_radius = 6.371e6
zoom=11
opacity=0.5
###################################################
############# Start of the Dash app #############
###################################################
app = dash.Dash(__name__,
external_stylesheets=[dbc.themes.LUX],
title='GEOMAAP.io | Hexbin Map example',
meta_tags=[{'name': 'viewport',
'content': 'width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.2'}]
)
###################################################
############# Header /Navigation bar #############
###################################################
navbar = dbc.Navbar(
[
html.A(
# Use row and col to control vertical alignment of logo / brand
dbc.Row(
[ dbc.Col(html.Img(src=LOGO, height="30px")),
dbc.Col(dbc.NavbarBrand("| Hexbin Map example", className="ml-2")),
],
align="center",
no_gutters=True,
),
href="https://www.geomaap.io/about",
),
],
color="dark",
dark=True,
className="mb-4",
)
###################################################
############# Controls ##########################
###################################################
# Range Slider
targetdepth_tab = dbc.Card(
dbc.CardBody(
[
html.H6("Target depth"),
dcc.RangeSlider(
id='TargetDepth',
marks={
0: {'label': '0', 'style': {'fontSize': "0.6rem"}},
300: {'label': '300 m', 'style': {'fontSize': "0.6rem"}}, # key=position, value=what you see
500: {'label': '', 'style': {'fontSize': "0.6rem"}},
1000: {'label': '1000 m', 'style': {'fontSize': "0.6rem"}},
1500: {'label': '', 'style': {'fontSize': "0.6rem"}},
2000: {'label': '2000 m', 'style': {'fontSize': "0.6rem"}},
3000: {'label': '', 'style': {'fontSize': "0.6rem"}},
4000: {'label': '4000 m', 'style': {'fontSize': "0.6rem"}},
},
step=20, # number of steps between values
min=0,
max=4000,
value=[0,4000], # default value initially chosen4
dots=True, # True, False - insert dots, only when step>1
allowCross=False, # True,False - Manage handle crossover
disabled=False, # True,False - disable handle
pushable=2, # any number, or True with multiple handles
updatemode='mouseup', # 'mouseup', 'drag' - update value method
included=True, # True, False - highlight handle
vertical=False, # True, False - vertical, horizontal slider
verticalHeight=900, # hight of slider (pixels) when vertical=True
className='None',
tooltip={'always_visible':False, # show current slider values
'placement':'bottom'},
),
]
)
)
# Colormap choice and Hexbin resolution
colorscale_names = ['Greys','RdBu','Viridis','Magma','Jet','IceFire']
control_tab = dbc.CardGroup(
[
dbc.Card(
dbc.CardBody(
[
html.H6("Colormap"),
dcc.Dropdown(
id='colorscale',
options=[{"value": x, "label": x}
for x in colorscale_names],
value='IceFire',
style={'margin-bottom':'10px'}
),
]
),
),
dbc.Card(
dbc.CardBody(
[
html.H6("Hexbin resolution"),
dcc.Dropdown(
id="resolution",
options=[
{'label': '300 m', 'value': 300},
{'label': '500 m', 'value': 500},
{'label': '750 m', 'value': 750},
{'label': '1000 m', 'value': 1000},
{'label': '2000 m', 'value': 2000},
],
value=500,
clearable=False
)
]
)
),
]
)
credits_tab = dbc.Card(
dbc.CardBody(
dcc.Markdown(
"""
Made with love with Dash
Realization: [geomaap.io](https://www.geomaap.io/about)
"""
),
),
className="mt-0",
)
###################################################
############# Right layout (=Maps) ###############
###################################################
data_density_map_component = dbc.Card(
[
dbc.CardHeader(
html.H3("Data density map")),
dbc.CardBody(
[
dcc.Markdown(
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut nec ultrices risus, eleifend aliquam dolor. Curabitur quis libero quis dui volutpat iaculis. Phasellus pellentesque mi vitae mauris scelerisque, id aliquam lacus mattis. Vestibulum dolor est, consequat sed elit ut, convallis euismod risus. Curabitur id ex diam. Etiam in augue id.
"""
),
dcc.Graph(
id='data_density' ,
style={'height': 800},
),
],
style={'padding':'0.5'}
)
],
className="my-2",
style={'height': 980}
)
data_cos_component = dbc.Card(
[
dbc.CardHeader(
html.H3("Data density map")),
dbc.CardBody(
[
dcc.Markdown(
"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut nec ultrices risus, eleifend aliquam dolor. Curabitur quis libero quis dui volutpat iaculis. Phasellus pellentesque mi vitae mauris scelerisque, id aliquam lacus mattis. Vestibulum dolor est, consequat sed elit ut, convallis euismod risus. Curabitur id ex diam. Etiam in augue id.
"""
),
dcc.Graph(
id='data_cos' ,
style={'height': 800},
# responsive=True
),
],
style={'padding':'0.5'}
)
],
className="my-2",
style={'height': 980}
)
fig_tab = dbc.Tabs(
[
dbc.Tab(data_density_map_component, label="Data density map",active_label_style={"font-weight":"800","color": "#00AEF9"}),
dbc.Tab(data_cos_component, label="COS map",active_label_style={"font-weight":"800","color": "#00AEF9"}),
]
)
###################################
###### Layout of Dash app ########
###################################
app.layout = dbc.Container(
[
navbar,
dbc.Row(
[
dbc.Col( # left layout
[
dbc.CardHeader("Data control"),
targetdepth_tab,
control_tab,
dbc.CardHeader("Credits"),
credits_tab,
],
width=4),
dbc.Col( # right layout
[
fig_tab,
],
width=8,
),
]
),
],
fluid=True,
)
###################################
###### Data density Map ########
###################################
@app.callback(
Output('data_density', 'figure'),
[
Input('resolution', 'value'),
Input('TargetDepth', 'value'),
Input("colorscale", "value"),
]
)
def update_data_density(resolution, depth, scale):
dff= cos.copy()
dff = dff[(dff["TARGET DEP"] >= depth[0]) & (dff["TARGET DEP"] <= depth[1])]
heg_size = -1* ((dff.X.max() - dff.X.min()) / resolution * np.pi / 180 * earth_radius * np.cos(dff.Y.mean()))
heg_size = math.floor(heg_size) # define hegsize dimension
fig_data_density = ff.create_hexbin_mapbox(data_frame=dff,
lat="Y", lon="X",
nx_hexagon=heg_size,
opacity=opacity,
labels={"color": "Density index"},
color="SCORE",
agg_func=np.sum,
mapbox_style='carto-positron',
color_continuous_scale=scale,
show_original_data=False,
original_data_marker=dict(opacity=0.6, size=4, color="black"),
min_count=1,
zoom=10.5,
center= {"lon": 6.13, "lat": 46.22}
)
fig_data_density.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0},
showlegend=False,
coloraxis_showscale=False,
hoverlabel=dict(
bgcolor="#3a3a3b",
font_color='white',
# color='white',
font_size=16,
font_family="Nunito Sans"
)
)
fig_data_density.data[0].hovertemplate = "<span style='font-size:1.2rem; font-weight=400'>Data density index = %{z:,.0f}</span><br><br>"
return fig_data_density
###################################
###### Cos Map ########
###################################
@app.callback(
Output('data_cos', 'figure'),
[
Input('resolution', 'value'),
Input('TargetDepth', 'value'),
Input("colorscale", "value"),
]
)
def update_data_cos(resolution, depth, scale):
dff= cos.copy()
dff = dff[(dff["TARGET DEP"] >= depth[0]) & (dff["TARGET DEP"] <= depth[1])]
heg_size = -1* ((dff.X.max() - dff.X.min()) / resolution * np.pi / 180 * earth_radius * np.cos(dff.Y.mean()))
heg_size = math.floor(heg_size) # define hegsize dimension
fig_cos = ff.create_hexbin_mapbox(data_frame=dff,
lat="Y", lon="X",
nx_hexagon=heg_size,
opacity=opacity,
labels={"color": "Density index"},
color="COS",
agg_func=np.sum,
mapbox_style='carto-positron',
color_continuous_scale=scale,
show_original_data=False,
original_data_marker=dict(opacity=0.6, size=4, color="black"),
min_count=1,
zoom=10.5,
center= {"lon": 6.13, "lat": 46.22}
)
fig_cos.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0},
showlegend=False,
coloraxis_showscale=False,
hoverlabel=dict(
bgcolor="#3a3a3b",
font_color='white',
# color='white',
font_size=16,
font_family="Nunito Sans"
)
)
fig_cos.data[0].hovertemplate = "<span style='font-size:1.2rem; font-weight=400'>COS index = %{z:,.0f}</span><br><br>"
return fig_cos
if __name__ == "__main__":
app.run_server(debug=True)