-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting.py
246 lines (212 loc) · 8.3 KB
/
plotting.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
import copy
import os
import re
import json
import pandas as pd
import numpy as np
import plotly.graph_objects as go
def insert_variables_as_properties_in_geodata(geodata, dataframe, key_property, key_column, variable_names):
output_geodata = copy.deepcopy(geodata)
for feature in output_geodata['features']:
key = feature['properties'][key_property]
row = dataframe[dataframe[key_column] == key]
if not row.empty:
variables_values = row[variable_names].values[0]
for name, value in zip(variable_names, variables_values):
feature['properties'][name] = str(value)
return output_geodata
def format_template(template, variable_names):
replaced = copy.deepcopy(template)
for name in variable_names:
replaced = re.sub(r'\{\}', '%{properties.' + name + '}', replaced, count=1)
return replaced
def load_geodata(geodata_path, key_property):
with open(os.path.join(geodata_path)) as geojson_file:
geodata = json.loads(geojson_file.read())
# add id feature for choroplethmapbox
for feature in geodata['features']:
feature['id'] = feature['properties'][key_property]
return geodata
def plot_map(title, geodata_path, dataframe, key_property, key_column, value_column, template, mapbox_access_token, options):
# unpack options
colorscale = options['colorscale'] if 'colorscale' in options else 'Viridis'
colorbar = options['colorbar'] if 'colorbar' in options else {}
showscale = options['showscale'] if 'showscale' in options else True
# load geodata
geodata = load_geodata(geodata_path, key_property)
# prepare data
columns = dataframe.columns.values
dropped_columns = set([key_column, value_column])
variable_names = [column for column in columns if column not in dropped_columns]
extended_geodata = insert_variables_as_properties_in_geodata(geodata, dataframe, key_property, key_column, variable_names)
formatted_template = format_template(template, variable_names) + '<extra></extra>'
# plot
figure = go.Figure(go.Choroplethmapbox(
geojson=extended_geodata,
locations=dataframe[key_column],
z=dataframe[value_column],
hovertemplate=formatted_template,
showscale=showscale,
colorscale=colorscale,
colorbar=colorbar
))
figure.update_layout(
title=title,
font={'family': 'Arial Black'},
mapbox_accesstoken=mapbox_access_token,
mapbox_zoom=9,
mapbox_pitch=0,
mapbox_bearing=0,
mapbox_center={'lat': 41.86, 'lon': -87.63},
margin={'r': 0, 'l': 50, 't': 50, 'b': 0}
)
return figure
class MapLayer():
def __init__(self, name, dataframe, key_column, value_column, options):
self.name = name
self.dataframe = dataframe
self.key_column = key_column
self.value_column = value_column
self.options = options
# Implementation based on: https://plot.ly/~empet/15237/choroplethmapbox-with-dropdown-menu/#/
def plot_maps(title, geodata_path, key_property, layers, template, mapbox_access_token, options):
# load geodata
geodata = load_geodata(geodata_path, key_property)
data = []
for layer in layers:
# extract variables
dataframe = layer.dataframe
key_column = layer.key_column
value_column = layer.value_column
# unpack options
colorscale = layer.options['colorscale'] if 'colorscale' in options else 'Viridis'
colorbar = layer.options['colorbar'] if 'colorbar' in options else {}
showscale = layer.options['showscale'] if 'showscale' in options else True
# prepare data
columns = dataframe.columns.values
dropped_columns = set([key_column, value_column])
variable_names = [column for column in columns if column not in dropped_columns]
extended_geodata = insert_variables_as_properties_in_geodata(geodata, dataframe, key_property, key_column, variable_names)
formatted_template = format_template(template, variable_names) + '<extra></extra>'
# plot
figure = go.Figure()
data.append(go.Choroplethmapbox(
geojson=extended_geodata,
locations=dataframe[key_column],
z=dataframe[value_column],
hovertemplate=formatted_template,
showscale=showscale,
colorscale=colorscale,
colorbar=colorbar,
visible=False
))
# mark first layer as visible
data[0]['visible'] = True
# create layout
layout = go.Layout(
title=title,
font={'family': 'Arial Black'},
mapbox_accesstoken=mapbox_access_token,
mapbox_zoom=9,
mapbox_pitch=0,
mapbox_bearing=0,
mapbox_center={'lat': 41.86, 'lon': -87.63},
margin={'r': 0, 'l': 50, 't': 50, 'b': 0}
)
# create layers visibility lists
visibilities = []
template_visibility = np.zeros((len(layers),)).astype(bool)
for i in range(len(layers)):
visibility = list(np.copy(template_visibility))
visibility[i] = True
visibilities.append(visibility)
# create menu buttons
buttons = []
for i, layer in enumerate(layers):
buttons.append({
'args': ['visible', visibilities[i]],
'label': layer.name,
'method': 'restyle'
})
# update layout adding menu
layout.update(
updatemenus=[{
'x': 0.95,
'y': 0.95,
'yanchor': 'top',
'buttons': buttons
}]
)
figure = go.Figure(data=data, layout=layout)
return figure
def plot_maps_sliders(title, geodata_path, key_property, layers, template, mapbox_access_token, options):
# load geodata
geodata = load_geodata(geodata_path, key_property)
data = []
for layer in layers:
# extract variables
dataframe = layer.dataframe
key_column = layer.key_column
value_column = layer.value_column
# unpack options
colorscale = layer.options['colorscale'] if 'colorscale' in options else 'Viridis'
colorbar = layer.options['colorbar'] if 'colorbar' in options else {}
showscale = layer.options['showscale'] if 'showscale' in options else True
# prepare data
columns = dataframe.columns.values
dropped_columns = set([key_column, value_column])
variable_names = [column for column in columns if column not in dropped_columns]
extended_geodata = insert_variables_as_properties_in_geodata(geodata, dataframe, key_property, key_column, variable_names)
formatted_template = format_template(template, variable_names) + '<extra></extra>'
# plot
figure = go.Figure()
data.append(go.Choroplethmapbox(
geojson=extended_geodata,
locations=dataframe[key_column],
z=dataframe[value_column],
hovertemplate=formatted_template,
showscale=showscale,
colorscale=colorscale,
colorbar=colorbar,
visible=False
))
# mark first layer as visible
data[0]['visible'] = True
# create layout
layout = go.Layout(
title=title,
font={'family': 'Arial Black'},
mapbox_accesstoken=mapbox_access_token,
mapbox_zoom=9,
mapbox_pitch=0,
mapbox_bearing=0,
mapbox_center={'lat': 41.86, 'lon': -87.63},
margin={'r': 0, 'l': 50, 't': 50, 'b': 0}
)
# create layers visibility lists
visibilities = []
template_visibility = np.zeros((len(layers),)).astype(bool)
for i in range(len(layers)):
visibility = list(np.copy(template_visibility))
visibility[i] = True
visibilities.append(visibility)
# Create and add slider
steps = []
for i, layer in enumerate(layers):
step = {
'method': 'restyle',
'args': ['visible', visibilities[i]],
'label': layer.name
}
steps.append(step)
sliders = [{
'active': 0,
'currentvalue': {'prefix': 'Year: '},
'pad': {'t': 50},
'steps': steps
}]
layout.update(
sliders=sliders
)
figure = go.Figure(data=data, layout=layout)
return figure