-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
931 lines (850 loc) · 44 KB
/
demo.html
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>gridviz-leaflet demo</title>
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI="
crossorigin=""
/>
<style>
span,
strong {
font-size: 16px;
cursor: pointer;
}
#map {
height: 98vh;
width: 100%;
}
#tooltip_eurostat,
#gvizLegend {
z-index: 99999999999999;
}
.leaflet-control-layers-expanded,
.leaflet-control-layers .leaflet-control-layers-list,
.leaflet-control-layers-expanded {
display: block !important;
}
.leaflet-control-layers {
padding: 6px 10px 6px 6px;
}
.leaflet-control-layers-toggle {
display: none !important;
}
</style>
</head>
<body>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=" crossorigin=""></script>
<script src="https://unpkg.com/gridviz/dist/gridviz.min.js"></script>
<script src="../build/leaflet-gridviz.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-interpolate@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-scale-chromatic@3"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.9.0/proj4.js"
integrity="sha512-lO8f7sIViqr9x5VE6Q72PS6f4FoZcuh5W9YzeSyfNRJ9z/qL3bkweiwG6keGzWS0BQzNDqAWXdBhYzFD6KffIw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/proj4leaflet/1.0.2/proj4leaflet.min.js"
integrity="sha512-GsAYl1wxzWW6azVpXkhyYfjMb2LbaOnlrqWMBdAk9xDcilinFvGMN+48Ajp/10u/9lcnh8YyS2CYNgung7ewHg=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<div id="map"></div>
<script>
// define our projection
proj4.defs('EPSG:3035', '+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs')
async function main() {
// get 3035 basemap capabilities
const response = await fetch(
'https://ec.europa.eu/statistical-atlas/arcgis/rest/services/Basemaps/StatAtlas_Continents_2021_3035/MapServer?f=pjson'
)
const basemap = await response.json()
// define EPSG 3035
// for gisco map-proxy
const GISCOCRS = new L.Proj.CRS('EPSG:3035', '+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs', {
resolutions: [
156543.03392804097, 78271.51696402048, 39135.75848201024, 19567.87924100512, 9783.93962050256, 4891.96981025128, 2445.98490512564,
1222.99245256282, 611.49622628141, 305.748113140705, 152.8740565703525, 76.43702828517625, 38.21851414258813, 19.109257071294063,
9.554628535647032, 4.777314267823516, 2.388657133911758, 1.19432856695,
],
bounds: L.bounds([-1031235.09091, -3364908.9791], [8000000, 11736009]),
origin: [0, 6000000],
})
// for esri map services
let resolutions = []
for (let i = 0; i < basemap.tileInfo.lods.length; i++) {
resolutions.push(basemap.tileInfo.lods[i].resolution)
}
let bounds = L.bounds(L.point(basemap.fullExtent.xmin, basemap.fullExtent.ymin), L.point(basemap.fullExtent.xmax, basemap.fullExtent.ymax))
let origin = [basemap.tileInfo.origin.x, basemap.tileInfo.origin.y]
const ESRICRS = new L.Proj.CRS(
'EPSG:3035',
'+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs',
{
//bounds: L.bounds([-1031235.09091, -3364908.9791], [8000000, 11736009]),
//origin: [0, 6000000],
origin: origin,
bounds: bounds,
resolutions: resolutions,
tileSize: basemap.tileInfo.rows,
}
)
ESRICRS.distance = L.CRS.Earth.distance
ESRICRS.R = 6378137
// create leaflet map
let leafletMap = new L.Map('map', {
crs: GISCOCRS,
center: ['50.00754', '19.98211'],
maxBounds: {
_northEast: {
lat: -38,
lng: 173,
},
_southWest: {
lat: -57,
lng: -152,
},
},
maxBoundsViscosity: 1,
maxZoom: 17,
minZoom: 1,
touchZoom: true,
zoom: 5,
zoomAnimation: false,
zoomControl: false,
})
// add basemap(s)
let osm = L.tileLayer('https://gisco-services.ec.europa.eu/maps/tiles/OSMCartoCompositeEN/EPSG3035/{z}/{x}/{y}.png', {
type: 'tiles',
maxZoom: 19,
minZoom: 0,
attribution:
"<a class='wt-link' href='//openstreetmap.org/copyright'>© OpenStreetMap</a> contributors <a class='wt-link' href='//ec.europa.eu/eurostat/web/gisco'>© GISCO</a>",
noWrap: false,
}).addTo(leafletMap)
let continents = L.tileLayer(
'https://ec.europa.eu/statistical-atlas/arcgis/rest/services/Basemaps/StatAtlas_Continents_2021_3035/MapServer/tile/{z}/{y}/{x}',
{
maxZoom: 19,
minZoom: 0,
errorTileUrl: 'assets/image/error-tile.png',
noWrap: true,
opacity: 1,
tileSize: 256,
tms: false,
}
)
// initialise gridviz layers
let shapeColorSizeStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
//the resolutions
[1000, 2000, 5000, 10000, 20000, 50000, 100000],
//the function returning each dataset from the resolution
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/population2/' + resolution + 'm/'
)
)
//define color for each cell c
const colorFunction = (cell, resolution) => {
const density = (1000000 * cell.TOT_P_2021) / (resolution * resolution)
if (density > 1500) return '#993404'
else if (density > 600) return '#d95f0e'
else if (density > 200) return '#fe9929'
else if (density > 60) return '#fec44f'
else if (density > 15) return '#fee391'
else return '#ffffd4'
}
//define style
const style = new gridviz.ShapeColorSizeStyle({ color: colorFunction })
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 4 })]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let tanakaStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/elevation/5000m/'
)
//define style
const styles = gridviz.TanakaStyle.get(
(cell) => cell.elevation,
[100, 200, 500, 1000, 2000],
['#ffffd4', '#fee391', '#fec44f', '#fe9929', '#d95f0e', '#993404']
)
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, styles)]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let joyplotStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
//define dataset
const dataset = new gridviz.CSVGrid(
gridvizMap,
'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/csv/Europe/pop_2018_10km.csv',
10000
)
//define style
const style = new gridviz.JoyPlotStyle({
height: (c) => Math.sqrt(c.population) * 50,
lineColor: () => 'white',
lineWidth: (y, ys, r, z) => (z < 2000 ? 2 * z : 1 * z),
fillColor: () => 'black',
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style])]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let legoStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[500, 1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/clc/' + resolution + 'm/'
)
)
//define colors for categories
const clcColors = {
1: '#e6004d',
2: '#ff0000',
3: '#cc4df2',
4: '#cc0000',
5: '#e6cccc',
6: '#e6cce6',
7: '#a600cc',
8: '#a64d00',
9: '#ff4dff',
10: '#ffa6ff',
11: '#ffe6ff',
12: '#ffffa8',
13: '#ffff00',
14: '#e6e600',
15: '#e68000',
16: '#f2a64d',
17: '#e6a600',
18: '#e6e64d',
19: '#ffe6a6',
20: '#ffe64d',
21: '#e6cc4d',
22: '#f2cca6',
23: '#80ff00',
24: '#00a600',
25: '#4dff00',
26: '#ccf24d',
27: '#a6ff80',
28: '#a6e64d',
29: '#a6f200',
30: '#e6e6e6',
31: '#cccccc',
32: '#ccffcc',
33: '#000000',
34: '#a6e6cc',
35: '#a6a6ff',
36: '#4d4dff',
37: '#ccccff',
38: '#e6e6ff',
39: '#a6a6e6',
40: '#00ccf2',
41: '#80f2e6',
42: '#00ffa6',
43: '#a6ffe6',
44: '#e6f2ff',
48: 'gray',
}
//define labels for categories
const clcLabels = {
1: 'Continuous urban fabric',
2: 'Discontinuous urban fabric',
3: 'Industrial or commercial units',
4: 'Road and rail networks and associated land',
5: 'Port areas',
6: 'Airports',
7: 'Mineral extraction sites',
8: 'Dump sites',
9: 'Construction sites',
10: 'Green urban areas',
11: 'Sport and leisure facilities',
12: 'Non-irrigated arable land',
13: 'Permanently irrigated land',
14: 'Rice fields',
15: 'Vineyards',
16: 'Fruit trees and berry plantations',
17: 'Olive groves',
18: 'Pastures',
19: 'Annual crops associated with permanent crops',
20: 'Complex cultivation patterns',
21: 'Land principally occupied by agriculture with significant areas of natural vegetation',
22: 'Agro-forestry areas',
23: 'Broad-leaved forest',
24: 'Coniferous forest',
25: 'Mixed forest',
26: 'Natural grasslands',
27: 'Moors and heathland',
28: 'Sclerophyllous vegetation',
29: 'Transitional woodland-shrub',
30: 'sands',
31: 'Bare rocks',
32: 'Sparsely vegetated areas',
33: 'Burnt areas',
34: 'Glaciers and perpetual snow',
35: 'Inland marshes',
36: 'Peat bogs',
37: 'Salt marshes',
38: 'Salines',
39: 'Intertidal flats',
40: 'Water courses',
41: 'Water bodies',
42: 'Coastal lagoons',
43: 'Estuaries',
44: 'Sea and ocean',
48: 'No data',
}
//define lego style
const legoStyle = gridviz.LegoStyle.getCategory((cell) => cell.clc, clcColors)
//add layer to map
gridvizMap.layers = [
new gridviz.GridLayer(dataset, legoStyle, {
minPixelsPerCell: 7,
cellInfoHTML: (cell) => clcLabels[cell.clc],
}),
]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let pillarStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.CSVGrid(
gridvizMap,
'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/csv/Europe/pop_2018_10km.csv',
10000
)
//define style
const style = new gridviz.PillarStyle({
height: (cell, resolution, z) => 0.3 * cell.population,
simple: (resolution, z) => z > 1000,
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style])]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let imageStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.CSVGrid(
gridvizMap,
'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/csv/Europe/pop_2018_10km.csv',
10000
)
//define image style
const style = new gridviz.ImageStyle({
// image URL from cell
image: (cell) => {
if (cell.population > 70000) return 'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/images/kitten_dark.png'
else if (cell.population > 7000)
return 'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/images/kitten_gray.png'
else if (cell.population > 1500)
return 'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/images/kitten_white.png'
},
// cell size
size: (c, r, z, viewScale) => r * 0.9,
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style])]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let textStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[500, 1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/elevation/' + resolution + 'm/'
)
)
//define style
const style = new gridviz.TextStyle({
text: (cell) => cell.elevation,
viewScale: (cells) => d3.extent(cells, (cell) => +cell.elevation),
color: (cell, resolution, z, [min, max]) => d3.interpolateSpectral(1 - (cell.elevation - min) / (max - min)),
fontSize: (cell, r) => 0.4 * r,
})
//define grid layer
const gridLayer = new gridviz.GridLayer(dataset, [style], {
minPixelsPerCell: 20,
cellInfoHTML: (cell) => cell.elevation + ' m',
})
//add layer to map
gridvizMap.layers = [gridLayer]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let trivariateStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.MultiResolutionDataset(
[100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiled-grid-germany-zensus2011/main/out/ALTER_KURZcsv/' + resolution + 'm/'
),
{
preprocess: (c) => {
/*population by age (5 age groups) ALTER_KURZ 1 Under 18 2 18 - 29 3 30 - 49 4 50 - 64 5 65 and over */
//total
c.TOT = 0
for (let i = 1; i <= 5; i++) c.TOT += +c[i]
//define 3 age groups:
//<18
c.yo = +c['1']
//between
c.mi = +c['2'] + +c['3'] + +c['4']
//>65
c.ol = +c['5']
},
}
)
//define ternary classifier
const trivariateClassifier = gridviz.trivariateColorClassifier(
//the three groups
['yo', 'mi', 'ol'],
//the function returning the total of the three
(c) => c.TOT,
//the colors
['#4daf4a', '#377eb8', '#e41a1c'],
{
//the center point of the ternary classification
center: [0.17, 0.63, 0.2],
//the size of the central class
centerCoefficient: 0.15,
}
)
//define style
const classNumberSize = 4
const style = new gridviz.ShapeColorSizeStyle({
color: (c) => trivariateClassifier(c) || 'black',
viewScale: gridviz.viewScaleQuantile({
valueFunction: (c) => +c.TOT,
classNumber: classNumberSize,
minSizePix: 3,
maxSizeFactor: 1.1,
}),
size: (c, r, z, viewScale) => viewScale(c.TOT),
shape: () => 'circle',
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 4 })]
//legend
style.legends = [
new gridviz.TrivariateLegend({
title: 'Age',
classifier: trivariateClassifier,
width: 90,
tooltip: map.tooltip,
texts: {
0: 'Over representation of persons aged <18',
1: 'Over representation of persons aged 18 to 64',
2: 'Over representation of persons aged >=65',
12: 'Under representation of persons aged <18',
'02': 'Under representation of<br>persons aged 18 to 64',
'01': 'Under representation of persons aged >=65',
center: 'Balanced representation of age groups',
},
leftText: '<18',
topText: '18 to 64',
rightText: '>65',
centerCoefficient: 0.5,
}),
]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let mosaicStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.CSVGrid(
gridvizMap,
'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/csv/Europe/pop_2018_10km.csv',
10000
)
//define color for each cell c
const colorFunction = (cell) => {
if (cell.population > 150000) return '#993404'
else if (cell.population > 60000) return '#d95f0e'
else if (cell.population > 20000) return '#fe9929'
else if (cell.population > 6000) return '#fec44f'
else if (cell.population > 1500) return '#fee391'
else return '#ffffd4'
}
//define style
const style = new gridviz.MosaicStyle({ color: colorFunction })
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style])]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let dotDensityStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define dataset
const dataset = new gridviz.CSVGrid(
gridvizMap,
'https://raw.githubusercontent.com/eurostat/gridviz/master/assets/csv/Europe/pop_2018_10km.csv',
10000
)
//define style
const style = new gridviz.DotDensityStyle({
dotNumber: (c, r, z) => c.population / z / 5,
dotSize: (r, z) => 1.2 * z,
color: () => '#FF5733',
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style])]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let compositionStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[200, 400, 600, 1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiled-grid-france-filosofi/main/out/csv/met/men/2019/' + resolution + 'm/'
),
{
preprocess: (c) => {
c.men_2_4ind = +c.men - +c.men_1ind - +c.men_5ind
},
}
)
//define style
const style = new gridviz.CompositionStyle({
color: {
men_1ind: '#7570b3', //blue
men_2_4ind: '#1b9e77', //green
men_5ind: '#d95f02', //orange
},
type: () => 'ring',
stripesOrientation: () => 0,
size: (c, r, z, scale) => scale(+c.men),
viewScale: gridviz.viewScaleQuantile({ valueFunction: (c) => +c.men, classNumber: 5, minSizePix: 8 }),
offsetAngle: () => 90,
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 18 })]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let isometricFenceStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[200, 400, 600, 1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiled-grid-france-filosofi/main/out/csv/met/ind/2019/' + resolution + 'm/'
),
{
preprocess: (c) => {
//aggregate to 3 age groups only
c.ind_0_24 = +c.ind_0_3 + +c.ind_4_5 + +c.ind_6_10 + +c.ind_11_17 + +c.ind_18_24
c.ind_25_64 = +c.ind_25_39 + +c.ind_40_54 + +c.ind_55_64
c.ind_65p = +c.ind_65_79 + +c.ind_80p
},
}
)
//define style
const style = new gridviz.IsoFenceStyle({
color: {
ind_0_24: 'rgb(240, 112, 74)',
ind_25_64: 'rgb(254, 221, 141)',
ind_65p: 'rgb(66, 136, 181)',
},
angle: 50,
viewScale: gridviz.viewScale({ valueFunction: (c) => +c.ind, stretching: gridviz.logarithmicScale(-7) }),
height: (c, r, z, viewScale) => 1.5 * viewScale(c.ind),
})
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [style], { minPixelsPerCell: 25 })]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let smoothingStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(
gridvizMap,
'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/population2/' + resolution + 'm/'
)
)
//define style
const sig = 10
const smoothingStyle = new gridviz_smoothing.KernelSmoothingStyle({
value: (cell) => +cell.TOT_P_2021,
filterSmoothed: (value) => value > 0.001,
sigma: (resolution, z) => (resolution * sig) / 10,
})
let webgl = true
if (webgl) {
smoothingStyle.factor = 3
const scale = gridviz.exponentialScale(-20)
smoothingStyle.styles = [
new gridviz.SquareColorWebGLStyle({
color: (t) => d3.interpolateYlOrRd(t), //d3.interpolateCividis, interpolateSpectral
viewScale: (cells) => d3.max(cells, (cell) => +cell.ksmval),
tFun: (cell, resolution, z, max) => {
const t = scale(cell.ksmval / max)
return t
},
}),
]
} else {
smoothingStyle.factor = 2
smoothingStyle.styles = [
new gridviz.ShapeColorSizeStyle({
color: (c, r, z, viewScale) => viewScale(c.ksmval),
viewScale: gridviz.viewScaleColor({
valueFunction: (c) => +c.ksmval,
colors: d3.schemeYlOrRd[7],
stretching: gridviz.logarithmicScale(-7),
}),
}),
]
}
//add layer to map
gridvizMap.layers = [new gridviz.GridLayer(dataset, [smoothingStyle], { minPixelsPerCell: 3 })]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
let SquareColorCategoryWebGLStyleLayer = new L.GridvizLayer({
proj: 'EPSG:3035',
onLayerDidMountCallback: (gridvizMap) => {
//define multi resolution dataset
const dataset = new gridviz.MultiResolutionDataset(
[500, 1000, 2000, 5000, 10000, 20000, 50000, 100000],
(resolution) =>
new gridviz.TiledGrid(gridvizMap, 'https://raw.githubusercontent.com/jgaffuri/tiledgrids/main/data/europe/clc/' + resolution + 'm/')
)
//define colors for categories
const clcColors = {
1: '#e6004d',
2: '#ff0000',
3: '#cc4df2',
4: '#cc0000',
5: '#e6cccc',
6: '#e6cce6',
7: '#a600cc',
8: '#a64d00',
9: '#ff4dff',
10: '#ffa6ff',
11: '#ffe6ff',
12: '#ffffa8',
13: '#ffff00',
14: '#e6e600',
15: '#e68000',
16: '#f2a64d',
17: '#e6a600',
18: '#e6e64d',
19: '#ffe6a6',
20: '#ffe64d',
21: '#e6cc4d',
22: '#f2cca6',
23: '#80ff00',
24: '#00a600',
25: '#4dff00',
26: '#ccf24d',
27: '#a6ff80',
28: '#a6e64d',
29: '#a6f200',
30: '#e6e6e6',
31: '#cccccc',
32: '#ccffcc',
33: '#000000',
34: '#a6e6cc',
35: '#a6a6ff',
36: '#4d4dff',
37: '#ccccff',
38: '#e6e6ff',
39: '#a6a6e6',
40: '#00ccf2',
41: '#80f2e6',
42: '#00ffa6',
43: '#a6ffe6',
44: '#e6f2ff',
48: 'gray',
}
//define labels for categories
const clcLabels = {
1: 'Continuous urban fabric',
2: 'Discontinuous urban fabric',
3: 'Industrial or commercial units',
4: 'Road and rail networks and associated land',
5: 'Port areas',
6: 'Airports',
7: 'Mineral extraction sites',
8: 'Dump sites',
9: 'Construction sites',
10: 'Green urban areas',
11: 'Sport and leisure facilities',
12: 'Non-irrigated arable land',
13: 'Permanently irrigated land',
14: 'Rice fields',
15: 'Vineyards',
16: 'Fruit trees and berry plantations',
17: 'Olive groves',
18: 'Pastures',
19: 'Annual crops associated with permanent crops',
20: 'Complex cultivation patterns',
21: 'Land principally occupied by agriculture with significant areas of natural vegetation',
22: 'Agro-forestry areas',
23: 'Broad-leaved forest',
24: 'Coniferous forest',
25: 'Mixed forest',
26: 'Natural grasslands',
27: 'Moors and heathland',
28: 'Sclerophyllous vegetation',
29: 'Transitional woodland-shrub',
30: 'sands',
31: 'Bare rocks',
32: 'Sparsely vegetated areas',
33: 'Burnt areas',
34: 'Glaciers and perpetual snow',
35: 'Inland marshes',
36: 'Peat bogs',
37: 'Salt marshes',
38: 'Salines',
39: 'Intertidal flats',
40: 'Water courses',
41: 'Water bodies',
42: 'Coastal lagoons',
43: 'Estuaries',
44: 'Sea and ocean',
48: 'No data',
}
//define style
const style = new gridviz.SquareColorCategoryWebGLStyle({
code: (cell) => cell.clc,
color: clcColors,
})
//add layer to map
gridvizMap.layers = [
new gridviz.GridLayer(dataset, [style], {
minPixelsPerCell: 4,
cellInfoHTML: (cell) => clcLabels[cell.clc],
}),
]
//custom opacity
gridvizMap._canvas.style.opacity = 0.7
},
})
// add first gridviz layer to the map
shapeColorSizeStyleLayer.addTo(leafletMap)
// add layer toggle
let baseMaps = {
OpenStreetMap: osm,
Continents: continents,
}
let gridvizLayers = {
'Shape / Color / Size': shapeColorSizeStyleLayer,
'Joyplot style': joyplotStyleLayer,
'Lego style': legoStyleLayer,
'Pillar style': pillarStyleLayer,
'Image style': imageStyleLayer,
'Text style': textStyleLayer,
'Trivariate style': trivariateStyleLayer,
'Mosaic style': mosaicStyleLayer,
'Dot density style': dotDensityStyleLayer,
'Composition style': compositionStyleLayer,
'Isometric fence style': isometricFenceStyleLayer,
'Kernel smoothing style': smoothingStyleLayer,
'Square Color Category WebGL': SquareColorCategoryWebGLStyleLayer,
'Tanaka style': tanakaStyleLayer,
}
let layerControl = L.control.layers(baseMaps, gridvizLayers).addTo(leafletMap)
// Adding titles to basemap sections
var basemapTitle = L.DomUtil.create('div', 'basemap-title')
basemapTitle.innerHTML = '<strong>Basemaps</strong>'
layerControl._baseLayersList.prepend(basemapTitle)
var overlayTitle = L.DomUtil.create('div', 'overlay-title')
overlayTitle.innerHTML = '<strong>Gridviz styles</strong>'
layerControl._overlaysList.prepend(overlayTitle)
// change basemap might require a change in CRS definition
leafletMap.on('baselayerchange', function (e) {
if (e.name == 'Continents') {
leafletMap.options.crs = ESRICRS
} else {
leafletMap.options.crs = GISCOCRS
}
let center, zoom
try {
center = leafletMap.getCenter()
zoom = leafletMap.getZoom()
} catch {
center = L.latLng(50.5, 19)
zoom = 5
}
//leafletMap.setView(center, zoom)
leafletMap._resetView(center, zoom)
})
}
main()
</script>
</body>
</html>