-
Notifications
You must be signed in to change notification settings - Fork 135
/
viz.py
774 lines (664 loc) · 30.5 KB
/
viz.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import json
import os
from IPython.core.display import HTML, display
import numpy
from mapboxgl.errors import TokenError
from mapboxgl.utils import color_map, height_map
from mapboxgl import templates
from mapboxgl.utils import img_encode, numeric_map
GL_JS_VERSION = 'v0.42.2'
class MapViz(object):
def __init__(self,
data,
access_token=None,
center=(0, 0),
below_layer='',
opacity=1,
div_id='map',
height='500px',
style='mapbox://styles/mapbox/light-v9?optimize=true',
width='100%',
zoom=0,
min_zoom=0,
max_zoom=24,
pitch=0,
bearing=0):
"""Construct a MapViz object
:param data: GeoJSON Feature Collection
:param access_token: Mapbox GL JS access token.
:param center: map center point
:param style: url to mapbox style or stylesheet as a Python dictionary in JSON format
:param div_id: The HTML div id of the map container in the viz
:param width: The CSS width of the HTML div id in % or pixels.
:param height: The CSS height of the HTML map div in % or pixels.
:param zoom: starting zoom level for map
:param opacity: opacity of map data layer
:param pitch: starting pitch (in degrees) for map
:param bearing: starting bearing (in degrees) for map
"""
if access_token is None:
access_token = os.environ.get('MAPBOX_ACCESS_TOKEN', '')
if access_token.startswith('sk'):
raise TokenError('Mapbox access token must be public (pk), not secret (sk). ' \
'Please sign up at https://www.mapbox.com/signup/ to get a public token. ' \
'If you already have an account, you can retreive your token at https://www.mapbox.com/account/.')
self.access_token = access_token
self.template = 'map'
self.data = data
self.div_id = div_id
self.width = width
self.height = height
self.style = style
self.center = center
self.zoom = zoom
self.below_layer = below_layer
self.opacity = opacity
self.label_property = None
self.min_zoom = min_zoom
self.max_zoom = max_zoom
self.pitch = pitch
self.bearing = bearing
def as_iframe(self, html_data):
"""Build the HTML representation for the mapviz."""
srcdoc = html_data.replace('"', "'")
return ('<iframe id="{div_id}", srcdoc="{srcdoc}" style="width: {width}; '
'height: {height};"></iframe>'.format(
div_id=self.div_id,
srcdoc=srcdoc,
width=self.width,
height=self.height))
def show(self, **kwargs):
# Load the HTML iframe
html = self.create_html(**kwargs)
map_html = self.as_iframe(html)
# Display the iframe in the current jupyter notebook view
display(HTML(map_html))
def add_unique_template_variables(self, options):
pass
def create_html(self):
"""Create a circle visual from a geojson data source"""
if isinstance(self.style, str):
style = "'{}'".format(self.style)
else:
style = self.style
options = dict(
gl_js_version=GL_JS_VERSION,
accessToken=self.access_token,
div_id=self.div_id,
style=style,
center=list(self.center),
zoom=self.zoom,
geojson_data=json.dumps(self.data, ensure_ascii=False),
belowLayer=self.below_layer,
opacity=self.opacity,
minzoom=self.min_zoom,
maxzoom=self.max_zoom,
pitch=self.pitch,
bearing=self.bearing)
if self.label_property is None:
options.update(labelProperty=None)
else:
options.update(labelProperty='{' + self.label_property + '}')
self.add_unique_template_variables(options)
return templates.format(self.template, **options)
class CircleViz(MapViz):
"""Create a circle map"""
def __init__(self,
data,
label_property=None,
label_size=8,
label_color='#131516',
label_halo_color='white',
label_halo_width=1,
radius=1,
color_property=None,
color_stops=None,
color_default='grey',
color_function_type='interpolate',
stroke_color='grey',
stroke_width=0.1,
*args,
**kwargs):
"""Construct a Mapviz object
:param label_property: property to use for marker label
:param label_size: size of label text
:param label_color: color of label text
:param label_halo_color: color of label text halo
:param label_halo_width: width of label text halo
:param color_property: property to determine circle color
:param color_stops: property to determine circle color
:param color_default: property to determine default circle color if match lookup fails
:param color_function_type: property to determine `type` used by Mapbox to assign color
:param radius: radius of circle
:param stroke_color: color of circle stroke outline
:param stroke_width: with of circle stroke outline
"""
super(CircleViz, self).__init__(data, *args, **kwargs)
self.template = 'circle'
self.label_property = label_property
self.label_color = label_color
self.label_size = label_size
self.label_halo_color = label_halo_color
self.label_halo_width = label_halo_width
self.color_property = color_property
self.color_stops = color_stops
self.radius = radius
self.stroke_color = stroke_color
self.stroke_width = stroke_width
self.color_function_type = color_function_type
self.color_default = color_default
def add_unique_template_variables(self, options):
"""Update map template variables specific to circle visual"""
options.update(dict(
geojson_data=json.dumps(self.data, ensure_ascii=False),
colorProperty=self.color_property,
colorType=self.color_function_type,
colorStops=self.color_stops,
strokeWidth=self.stroke_width,
strokeColor=self.stroke_color,
radius=self.radius,
defaultColor=self.color_default,
labelColor=self.label_color,
labelSize=self.label_size,
labelHaloColor=self.label_halo_color,
labelHaloWidth=self.label_halo_width
))
class GraduatedCircleViz(MapViz):
"""Create a graduated circle map"""
def __init__(self,
data,
label_property=None,
label_size=8,
label_color='#131516',
label_halo_color='white',
label_halo_width=1,
color_property=None,
color_stops=None,
color_default='grey',
color_function_type='interpolate',
stroke_color='grey',
stroke_width=0.1,
radius_property=None,
radius_stops=None,
radius_default=2,
radius_function_type='interpolate',
*args,
**kwargs):
"""Construct a Mapviz object
:param label_property: property to use for marker label
:param color_property: property to determine circle color
:param color_stops: property to determine circle color
:param color_default: property to determine default circle color if match lookup fails
:param color_function_type: property to determine `type` used by Mapbox to assign color
:param radius_property: property to determine circle radius
:param radius_stops: property to determine circle radius
:param radius_default: property to determine default circle radius if match lookup fails
:param radius_function_type: property to determine `type` used by Mapbox to assign radius size
:param stroke_color: color of circle stroke outline
:param stroke_width: with of circle stroke outline
"""
super(GraduatedCircleViz, self).__init__(data, *args, **kwargs)
self.template = 'graduated_circle'
self.label_property = label_property
self.label_color = label_color
self.label_size = label_size
self.label_halo_color = label_halo_color
self.label_halo_width = label_halo_width
self.color_property = color_property
self.color_stops = color_stops
self.radius_property = radius_property
self.radius_stops = radius_stops
self.color_function_type = color_function_type
self.color_default = color_default
self.radius_function_type = radius_function_type
self.radius_default = radius_default
self.stroke_color = stroke_color
self.stroke_width = stroke_width
def add_unique_template_variables(self, options):
"""Update map template variables specific to graduated circle visual"""
options.update(dict(
colorProperty=self.color_property,
colorStops=self.color_stops,
colorType=self.color_function_type,
radiusType=self.radius_function_type,
defaultColor=self.color_default,
defaultRadius=self.radius_default,
radiusProperty=self.radius_property,
radiusStops=self.radius_stops,
strokeWidth=self.stroke_width,
strokeColor=self.stroke_color,
labelColor=self.label_color,
labelSize=self.label_size,
labelHaloColor=self.label_halo_color,
labelHaloWidth=self.label_halo_width
))
class HeatmapViz(MapViz):
"""Create a heatmap viz"""
def __init__(self,
data,
weight_property=None,
weight_stops=None,
color_stops=None,
radius_stops=None,
intensity_stops=None,
*args,
**kwargs):
"""Construct a Mapviz object
:param weight_property: property to determine heatmap weight. EX. "population"
:param weight_stops: stops to determine heatmap weight. EX. [[10, 0], [100, 1]]
:param color_stops: stops to determine heatmap color. EX. [[0, "red"], [0.5, "blue"], [1, "green"]]
:param radius_stops: stops to determine heatmap radius based on zoom. EX: [[0, 1], [12, 30]]
:param intensity_stops: stops to determine the heatmap intensity based on zoom. EX: [[0, 0.1], [20, 5]]
"""
super(HeatmapViz, self).__init__(data, *args, **kwargs)
self.template = 'heatmap'
self.weight_property = weight_property
self.weight_stops = weight_stops
if color_stops:
# Make the first color stop in a heatmap have opacity 0 for good visual effect
self.color_stops = [[0.00001, 'rgba(0,0,0,0)']] + color_stops
self.radius_stops = radius_stops
self.intensity_stops = intensity_stops
def add_unique_template_variables(self, options):
"""Update map template variables specific to heatmap visual"""
options.update(dict(
colorStops=self.color_stops,
radiusStops=self.radius_stops,
weightProperty=self.weight_property,
weightStops=self.weight_stops,
intensityStops=self.intensity_stops
))
class ClusteredCircleViz(MapViz):
"""Create a clustered circle map"""
def __init__(self,
data,
label_size=8,
label_color='#131516',
label_halo_color='white',
label_halo_width=1,
color_stops=None,
radius_stops=None,
cluster_radius=30,
cluster_maxzoom=14,
radius_default=2,
color_default='black',
stroke_color='grey',
stroke_width=0.1,
*args,
**kwargs):
"""Construct a Mapviz object
:param color_property: property to determine circle color
:param color_stops: property to determine circle color
:param radius_property: property to determine circle radius
:param radius_stops: property to determine circle radius
:param stroke_color: color of circle stroke outline
:param stroke_width: with of circle stroke outline
:param radius_default: radius of circles not contained in a cluster
:param color_default: color of circles not contained in a cluster
"""
super(ClusteredCircleViz, self).__init__(data, *args, **kwargs)
self.template = 'clustered_circle'
self.label_color = label_color
self.label_size = label_size
self.label_halo_color = label_halo_color
self.label_halo_width = label_halo_width
self.color_stops = color_stops
self.radius_stops = radius_stops
self.clusterRadius = cluster_radius
self.clusterMaxZoom = cluster_maxzoom
self.radius_default = radius_default
self.color_default = color_default
self.stroke_color = stroke_color
self.stroke_width = stroke_width
self.color_default = color_default
def add_unique_template_variables(self, options):
"""Update map template variables specific to a clustered circle visual"""
options.update(dict(
colorStops=self.color_stops,
colorDefault=self.color_default,
radiusStops=self.radius_stops,
clusterRadius=self.clusterRadius,
clusterMaxZoom=self.clusterMaxZoom,
strokeWidth=self.stroke_width,
strokeColor=self.stroke_color,
radiusDefault=self.radius_default,
labelColor=self.label_color,
labelSize=self.label_size,
labelHaloColor=self.label_halo_color,
labelHaloWidth=self.label_halo_width
))
class ChoroplethViz(MapViz):
"""Create a choropleth viz"""
def __init__(self,
data,
vector_url=None,
vector_layer_name=None,
vector_join_property=None,
data_join_property=None, # vector only
label_property=None,
color_property=None,
color_stops=None,
color_default='grey',
color_function_type='interpolate',
line_color='white',
line_stroke='solid',
line_width=1,
height_property=None,
height_stops=None,
height_default=0.0,
height_function_type='interpolate',
*args,
**kwargs):
"""Construct a Mapviz object
:param data: can be either GeoJSON (containing polygon features) or JSON for data-join technique with vector polygons
:param vector_url: optional property to define vector polygon source
:param vector_layer_name: property to define target layer of vector source
:param vector_join_property: property to aid in determining color for styling vector polygons
:param data_join_property: property to join json data to vector features
:param label_property: property to use for marker label
:param color_property: property to determine polygon color
:param color_stops: property to determine polygon color
:param color_default: property to determine default polygon color if match lookup fails
:param color_function_type: property to determine `type` used by Mapbox to assign color
:param line_color: property to determine choropleth line color
:param line_stroke: property to determine choropleth line stroke (solid, dashed, dotted, dash dot)
:param line_width: property to determine choropleth line width
:param height_property: feature property for determining polygon height in 3D extruded choropleth map
:param height_stops: property for determining 3D extrusion height
:param height_default: default height for 3D extruded polygons
:param height_function_type: roperty to determine `type` used by Mapbox to assign height
"""
super(ChoroplethViz, self).__init__(data, *args, **kwargs)
self.vector_url = vector_url
self.vector_layer_name = vector_layer_name
self.vector_join_property = vector_join_property
self.data_join_property = data_join_property
if self.vector_url is not None and self.vector_layer_name is not None:
self.template = 'vector_choropleth'
self.vector_source = True
else:
self.vector_source = False
self.template = 'choropleth'
self.label_property = label_property
self.color_property = color_property
self.color_stops = color_stops
self.color_default = color_default
self.color_function_type = color_function_type
self.line_color = line_color
self.line_stroke = line_stroke
self.line_width = line_width
self.height_property = height_property
self.height_stops = height_stops
self.height_default = height_default
self.height_function_type = height_function_type
def generate_vector_color_map(self):
"""Generate color stops array for use with match expression in mapbox template"""
vector_stops = []
for row in self.data:
# map color to JSON feature using color_property
color = color_map(row[self.color_property], self.color_stops, self.color_default)
# link to vector feature using data_join_property (from JSON object)
vector_stops.append([row[self.data_join_property], color])
return vector_stops
def generate_vector_height_map(self):
"""Generate height stops array for use with match expression in mapbox template"""
vector_stops = []
if self.height_function_type == 'match':
match_height = self.height_stops
for row in self.data:
# map height to JSON feature using height_property
height = height_map(row[self.height_property], self.height_stops, self.height_default)
# link to vector feature using data_join_property (from JSON object)
vector_stops.append([row[self.data_join_property], height])
return vector_stops
def add_unique_template_variables(self, options):
"""Update map template variables specific to heatmap visual"""
# set line stroke dash interval based on line_stroke property
if self.line_stroke in ["dashed", "--"]:
self.line_dash_array = [6, 4]
elif self.line_stroke in ["dotted", ":"]:
self.line_dash_array = [0.5, 4]
elif self.line_stroke in ["dash dot", "-."]:
self.line_dash_array = [6, 4, 0.5, 4]
elif self.line_stroke in ["solid", "-"]:
self.line_dash_array = [1, 0]
else:
# default to solid line
self.line_dash_array = [1, 0]
# check if choropleth map should include 3-D extrusion
self.extrude = all([bool(self.height_property), bool(self.height_stops)])
# common variables for vector and geojson-based choropleths
options.update(dict(
colorStops=self.color_stops,
colorProperty=self.color_property,
colorType=self.color_function_type,
defaultColor=self.color_default,
lineColor=self.line_color,
lineDashArray=self.line_dash_array,
lineStroke=self.line_stroke,
lineWidth=self.line_width,
extrudeChoropleth=self.extrude,
))
if self.extrude:
options.update(dict(
heightType=self.height_function_type,
heightProperty=self.height_property,
heightStops=self.height_stops,
defaultHeight=self.height_default,
))
# vector-based choropleth map variables
if self.vector_source:
options.update(dict(
vectorUrl=self.vector_url,
vectorLayer=self.vector_layer_name,
vectorColorStops=self.generate_vector_color_map(),
vectorJoinDataProperty=self.vector_join_property,
joinData=json.dumps(self.data, ensure_ascii=False),
dataJoinProperty=self.data_join_property,
))
if self.extrude:
options.update(dict(
vectorHeightStops=self.generate_vector_height_map(),
))
# geojson-based choropleth map variables
else:
options.update(dict(
geojson_data=json.dumps(self.data, ensure_ascii=False),
))
class ImageViz(MapViz):
"""Create a image viz"""
def __init__(self,
image,
coordinates,
*args,
**kwargs):
"""Construct a Mapviz object
:param coordinates: property to determine image coordinates (UL, UR, LR, LL).
EX. [[-80.425, 46.437], [-71.516, 46.437], [-71.516, 37.936], [-80.425, 37.936]]
:param image: url, local path or a numpy ndarray
"""
super(ImageViz, self).__init__(None, *args, **kwargs)
if type(image) is numpy.ndarray:
image = img_encode(image)
self.template = 'image'
self.image = image
self.coordinates = coordinates
def add_unique_template_variables(self, options):
"""Update map template variables specific to image visual"""
options.update(dict(
image=self.image,
coordinates=self.coordinates))
class RasterTilesViz(MapViz):
"""Create a rastertiles map"""
def __init__(self,
tiles_url,
tiles_size=256,
tiles_bounds=None,
tiles_minzoom=0,
tiles_maxzoom=22,
*args,
**kwargs):
"""Construct a Mapviz object
:param tiles_url: property to determine tiles url endpoint
:param tiles_size: property to determine displayed tiles size
:param tiles_bounds: property to determine the tiles endpoint bounds
:param tiles_minzoom: property to determine the tiles endpoint min zoom
:param tiles_max: property to determine the tiles endpoint max zoom
"""
super(RasterTilesViz, self).__init__(None, *args, **kwargs)
self.template = 'raster'
self.tiles_url = tiles_url
self.tiles_size = tiles_size
self.tiles_bounds = tiles_bounds
self.tiles_minzoom = tiles_minzoom
self.tiles_maxzoom = tiles_maxzoom
def add_unique_template_variables(self, options):
"""Update map template variables specific to a raster visual"""
options.update(dict(
tiles_url=self.tiles_url,
tiles_size=self.tiles_size,
tiles_minzoom=self.tiles_minzoom,
tiles_maxzoom=self.tiles_maxzoom,
tiles_bounds=self.tiles_bounds if self.tiles_bounds else 'undefined'))
class LinestringViz(MapViz):
"""Create a linestring viz"""
def __init__(self,
data,
vector_url=None,
vector_layer_name=None,
vector_join_property=None,
data_join_property=None,
label_property=None,
label_size=8,
label_color='#131516',
label_halo_color='white',
label_halo_width=1,
color_property=None,
color_stops=None,
color_default='grey',
color_function_type='interpolate',
line_stroke='solid',
line_width_property=None,
line_width_stops=None,
line_width_default=1,
line_width_function_type='interpolate',
*args,
**kwargs):
"""Construct a Mapviz object
:param data: can be either GeoJSON (containing polygon features) or JSON for data-join technique with vector polygons
:param vector_url: optional property to define vector linestring source
:param vector_layer_name: property to define target layer of vector source
:param vector_join_property: property to aid in determining color for styling vector lines
:param data_join_property: property to join json data to vector features
:param label_property: property to use for marker label
:param label_size: size of label text
:param label_color: color of label text
:param label_halo_color: color of label text halo
:param label_halo_width: width of label text halo
:param color_property: property to determine line color
:param color_stops: property to determine line color
:param color_default: property to determine default line color if match lookup fails
:param color_function_type: property to determine `type` used by Mapbox to assign color
:param line_stroke: property to determine line stroke (solid, dashed, dotted, dash dot)
:param line_width_property: property to determine line width
:param line_width_stops: property to determine line width
:param line_width_default: property to determine default line width if match lookup fails
:param line_width_function_type: property to determine `type` used by Mapbox to assign line width
"""
super(LinestringViz, self).__init__(data, *args, **kwargs)
self.vector_url = vector_url
self.vector_layer_name = vector_layer_name
self.vector_join_property = vector_join_property
self.data_join_property = data_join_property
if self.vector_url is not None and self.vector_layer_name is not None:
self.template = 'vector_linestring'
self.vector_source = True
else:
self.vector_source = False
self.template = 'linestring'
self.label_property = label_property
self.label_color = label_color
self.label_size = label_size
self.label_halo_color = label_halo_color
self.label_halo_width = label_halo_width
self.color_property = color_property
self.color_stops = color_stops
self.color_default = color_default
self.color_function_type = color_function_type
self.line_stroke = line_stroke
self.line_width_property = line_width_property
self.line_width_stops = line_width_stops
self.line_width_default = line_width_default
self.line_width_function_type = line_width_function_type
def generate_vector_color_map(self):
"""Generate color stops array for use with match expression in mapbox template"""
vector_stops = []
for row in self.data:
# map color to JSON feature using color_property
color = color_map(row[self.color_property], self.color_stops, self.color_default)
# link to vector feature using data_join_property (from JSON object)
vector_stops.append([row[self.data_join_property], color])
return vector_stops
def generate_vector_width_map(self):
"""Generate width stops array for use with match expression in mapbox template"""
vector_stops = []
if self.line_width_function_type == 'match':
match_width = self.line_width_stops
for row in self.data:
# map width to JSON feature using width_property
width = numeric_map(row[self.line_width_property], self.line_width_stops, self.line_width_default)
# link to vector feature using data_join_property (from JSON object)
vector_stops.append([row[self.data_join_property], width])
return vector_stops
def add_unique_template_variables(self, options):
"""Update map template variables specific to linestring visual"""
# set line stroke dash interval based on line_stroke property
if self.line_stroke in ["dashed", "--"]:
self.line_dash_array = [6, 4]
elif self.line_stroke in ["dotted", ":"]:
self.line_dash_array = [0.5, 4]
elif self.line_stroke in ["dash dot", "-."]:
self.line_dash_array = [6, 4, 0.5, 4]
elif self.line_stroke in ["solid", "-"]:
self.line_dash_array = [1, 0]
else:
# default to solid line
self.line_dash_array = [1, 0]
# common variables for vector and geojson-based linestring maps
options.update(dict(
colorStops=self.color_stops,
colorProperty=self.color_property,
colorType=self.color_function_type,
defaultColor=self.color_default,
lineColor=self.color_default,
lineDashArray=self.line_dash_array,
lineStroke=self.line_stroke,
widthStops=self.line_width_stops,
widthProperty=self.line_width_property,
widthType=self.line_width_function_type,
defaultWidth=self.line_width_default,
labelColor=self.label_color,
labelSize=self.label_size,
labelHaloColor=self.label_halo_color,
labelHaloWidth=self.label_halo_width
))
# vector-based linestring map variables
if self.vector_source:
options.update(dict(
vectorUrl=self.vector_url,
vectorLayer=self.vector_layer_name,
vectorJoinDataProperty=self.vector_join_property,
vectorColorStops=[[0,self.color_default]],
vectorWidthStops=[[0,self.line_width_default]],
joinData=json.dumps(self.data, ensure_ascii=False),
dataJoinProperty=self.data_join_property,
))
if self.color_property:
options.update(dict(vectorColorStops=self.generate_vector_color_map()))
if self.line_width_property:
options.update(dict(vectorWidthStops=self.generate_vector_width_map()))
# geojson-based linestring map variables
else:
options.update(dict(
geojson_data=json.dumps(self.data, ensure_ascii=False),
))