-
Notifications
You must be signed in to change notification settings - Fork 135
/
utils.py
392 lines (298 loc) · 13.4 KB
/
utils.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from .colors import color_ramps, common_html_colors
from chroma import Color, Scale
import geojson
import json
import base64
from io import BytesIO
import re
from matplotlib.image import imsave
from colour import Color as Colour
def row_to_geojson(row, lon, lat):
"""Convert a pandas dataframe row to a geojson format object. Converts all datetimes to epoch seconds.
"""
# Let pandas handle json serialization
row_json = json.loads(row.to_json(date_format='epoch', date_unit='s'))
return geojson.Feature(geometry=geojson.Point((row_json[lon], row_json[lat])),
properties={key: row_json[key] for key in row_json.keys() if key not in [lon, lat]})
def df_to_geojson(df, properties=None, lat='lat', lon='lon', precision=None, filename=None):
"""Serialize a Pandas dataframe to a geojson format Python dictionary
"""
if precision:
df[lon] = df[lon].round(precision)
df[lat] = df[lat].round(precision)
if not properties:
# if no properties are selected, use all properties in dataframe
properties = [c for c in df.columns if c not in [lon, lat]]
for prop in properties:
# Check if list of properties exists in dataframe columns
if prop not in list(df.columns):
raise ValueError(
'properties must be a valid list of column names from dataframe')
if prop in [lon, lat]:
raise ValueError(
'properties cannot be the geometry longitude or latitude column')
if filename:
with open(filename, 'w+') as f:
# Overwrite file if it already exists
pass
with open(filename, 'a+') as f:
# Write out file to line
f.write('{"type": "FeatureCollection", "features": [\n')
for idx, row in df[[lon, lat] + properties].iterrows():
if idx == 0:
f.write(geojson.dumps(row_to_geojson(row, lon, lat)) + '\n')
else:
f.write(',' + geojson.dumps(row_to_geojson(row, lon, lat)) + '\n')
f.write(']}')
return {
"type": "file",
"filename": filename,
"feature_count": df.shape[0]
}
else:
features = []
df[[lon, lat] + properties].apply(lambda x: features.append(
row_to_geojson(x, lon, lat)), axis=1)
return geojson.FeatureCollection(features)
def scale_between(minval, maxval, numStops):
""" Scale a min and max value to equal interval domain with
numStops discrete values
"""
scale = []
if numStops < 2:
return [minval, maxval]
elif maxval < minval:
raise ValueError()
else:
domain = maxval - minval
interval = float(domain) / float(numStops)
for i in range(numStops):
scale.append(round(minval + interval * i, 2))
return scale
def create_radius_stops(breaks, min_radius, max_radius):
"""Convert a data breaks into a radius ramp
"""
num_breaks = len(breaks)
radius_breaks = scale_between(min_radius, max_radius, num_breaks)
stops = []
for i, b in enumerate(breaks):
stops.append([b, radius_breaks[i]])
return stops
def create_weight_stops(breaks):
"""Convert data breaks into a heatmap-weight ramp
"""
num_breaks = len(breaks)
weight_breaks = scale_between(0, 1, num_breaks)
stops = []
for i, b in enumerate(breaks):
stops.append([b, weight_breaks[i]])
return stops
def create_numeric_stops(breaks, min_value, max_value):
"""Convert data breaks into a general numeric ramp (height, radius, weight, etc.)
"""
weight_breaks = scale_between(min_value, max_value, len(breaks))
return [list(x) for x in zip(breaks, weight_breaks)]
def create_color_stops(breaks, colors='RdYlGn', color_ramps=color_ramps):
"""Convert a list of breaks into color stops using colors from colorBrewer
or a custom list of color values in RGB, RGBA, HSL, CSS text, or HEX format.
See www.colorbrewer2.org for a list of color options to pass
"""
num_breaks = len(breaks)
stops = []
if isinstance(colors, list):
# Check if colors contain a list of color values
if len(colors) == 0 or len(colors) != num_breaks:
raise ValueError(
'custom color list must be of same length as breaks list')
for color in colors:
# Check if color is valid string
try:
Colour(color)
except:
raise ValueError(
'The color code {color} is in the wrong format'.format(color=color))
for i, b in enumerate(breaks):
stops.append([b, colors[i]])
else:
if colors not in color_ramps.keys():
raise ValueError('color does not exist in colorBrewer!')
else:
try:
ramp = color_ramps[colors][num_breaks]
except KeyError:
raise ValueError("Color ramp {} does not have a {} breaks".format(
colors, num_breaks))
for i, b in enumerate(breaks):
stops.append([b, ramp[i]])
return stops
def rgb_tuple_from_str(color_string):
"""Convert color in format 'rgb(RRR,GGG,BBB)', 'rgba(RRR,GGG,BBB,alpha)',
'#RRGGBB', or limited English color name (eg 'red') to tuple (RRR, GGG, BBB)
"""
try:
# English color names (limited)
rgb_string = common_html_colors[color_string]
return tuple([float(x) for x in re.findall(r'\d{1,3}', rgb_string)])
except KeyError:
try:
# HEX color code
hex_string = color_string.lstrip('#')
return tuple(int(hex_string[i:i+2], 16) for i in (0, 2 ,4))
except ValueError:
# RGB or RGBA formatted strings
return tuple([int(x) if float(x) > 1 else float(x)
for x in re.findall(r"[-+]?\d*\.*\d+", color_string)])
def color_map(lookup, color_stops, default_color='rgb(122,122,122)'):
"""Return an rgb color value interpolated from given color_stops;
assumes colors in color_stops provided as strings of form 'rgb(RRR,GGG,BBB)'
or in hex: '#RRGGBB'
"""
# if no color_stops, use default color
if len(color_stops) == 0:
return default_color
# dictionary to lookup color from match-type color_stops
match_map = dict((x, y) for (x, y) in color_stops)
# if lookup matches stop exactly, return corresponding color (first priority)
# (includes non-numeric color_stop "keys" for finding color by match)
if lookup in match_map.keys():
return match_map.get(lookup)
# if lookup value numeric, map color by interpolating from color scale
if isinstance(lookup, (int, float, complex)):
# try ordering stops
try:
stops, colors = zip(*sorted(color_stops))
# if not all stops are numeric, attempt looking up as if categorical stops
except TypeError:
return match_map.get(lookup, default_color)
# for interpolation, all stops must be numeric
if not all(isinstance(x, (int, float, complex)) for x in stops):
return default_color
# check if lookup value in stops bounds
if float(lookup) <= stops[0]:
return colors[0]
elif float(lookup) >= stops[-1]:
return colors[-1]
# check if lookup value matches any stop value
elif float(lookup) in stops:
return colors[stops.index(lookup)]
# interpolation required
else:
rgb_tuples = [Color(rgb_tuple_from_str(x)) for x in colors]
# identify bounding color stop values
lower = max([stops[0]] + [x for x in stops if x < lookup])
upper = min([stops[-1]] + [x for x in stops if x > lookup])
# colors from bounding stops
lower_color = rgb_tuples[stops.index(lower)]
upper_color = rgb_tuples[stops.index(upper)]
# generate color scale for mapping lookup value to interpolated color
scale = Scale(Color(lower_color), Color(upper_color))
# compute linear "relative distance" from lower bound color to upper bound color
distance = (lookup - lower) / (upper - lower)
# return string representing rgb color value
return scale(distance).to_string().replace(', ', ',')
# default color value catch-all
return default_color
def numeric_map(lookup, numeric_stops, default=0.0):
"""Return a number value interpolated from given numeric_stops
"""
# if no numeric_stops, use default
if len(numeric_stops) == 0:
return default
# dictionary to lookup value from match-type numeric_stops
match_map = dict((x, y) for (x, y) in numeric_stops)
# if lookup matches stop exactly, return corresponding stop (first priority)
# (includes non-numeric numeric_stop "keys" for finding value by match)
if lookup in match_map.keys():
return match_map.get(lookup)
# if lookup value numeric, map value by interpolating from scale
if isinstance(lookup, (int, float, complex)):
# try ordering stops
try:
stops, values = zip(*sorted(numeric_stops))
# if not all stops are numeric, attempt looking up as if categorical stops
except TypeError:
return match_map.get(lookup, default)
# for interpolation, all stops must be numeric
if not all(isinstance(x, (int, float, complex)) for x in stops):
return default
# check if lookup value in stops bounds
if float(lookup) <= stops[0]:
return values[0]
elif float(lookup) >= stops[-1]:
return values[-1]
# check if lookup value matches any stop value
elif float(lookup) in stops:
return values[stops.index(lookup)]
# interpolation required
else:
# identify bounding stop values
lower = max([stops[0]] + [x for x in stops if x < lookup])
upper = min([stops[-1]] + [x for x in stops if x > lookup])
# values from bounding stops
lower_value = values[stops.index(lower)]
upper_value = values[stops.index(upper)]
# compute linear "relative distance" from lower bound to upper bound
distance = (lookup - lower) / (upper - lower)
# return interpolated value
return lower_value + distance * (upper_value - lower_value)
# default value catch-all
return default
def img_encode(arr, **kwargs):
"""Encode ndarray to base64 string image data
Parameters
----------
arr: ndarray (rows, cols, depth)
kwargs: passed directly to matplotlib.image.imsave
"""
sio = BytesIO()
imsave(sio, arr, **kwargs)
sio.seek(0)
img_format = kwargs['format'] if kwargs.get('format') else 'png'
img_str = base64.b64encode(sio.getvalue()).decode()
return 'data:image/{};base64,{}'.format(img_format, img_str)
def height_map(lookup, height_stops, default_height=0.0):
"""Return a height value (in meters) interpolated from given height_stops;
for use with vector-based visualizations using fill-extrusion layers
"""
# if no height_stops, use default height
if len(height_stops) == 0:
return default_height
# dictionary to lookup height from match-type height_stops
match_map = dict((x, y) for (x, y) in height_stops)
# if lookup matches stop exactly, return corresponding height (first priority)
# (includes non-numeric height_stop "keys" for finding height by match)
if lookup in match_map.keys():
return match_map.get(lookup)
# if lookup value numeric, map height by interpolating from height scale
if isinstance(lookup, (int, float, complex)):
# try ordering stops
try:
stops, heights = zip(*sorted(height_stops))
# if not all stops are numeric, attempt looking up as if categorical stops
except TypeError:
return match_map.get(lookup, default_height)
# for interpolation, all stops must be numeric
if not all(isinstance(x, (int, float, complex)) for x in stops):
return default_height
# check if lookup value in stops bounds
if float(lookup) <= stops[0]:
return heights[0]
elif float(lookup) >= stops[-1]:
return heights[-1]
# check if lookup value matches any stop value
elif float(lookup) in stops:
return heights[stops.index(lookup)]
# interpolation required
else:
# identify bounding height stop values
lower = max([stops[0]] + [x for x in stops if x < lookup])
upper = min([stops[-1]] + [x for x in stops if x > lookup])
# heights from bounding stops
lower_height = heights[stops.index(lower)]
upper_height = heights[stops.index(upper)]
# compute linear "relative distance" from lower bound height to upper bound height
distance = (lookup - lower) / (upper - lower)
# return string representing rgb height value
return lower_height + distance * (upper_height - lower_height)
# default height value catch-all
return default_height