Choropleth3DMap.html 3.32 KB
Newer Older
Boris Mericskay's avatar
Boris Mericskay committed
1
2
3
<!DOCTYPE html>
<html>
<head>
Boris Mericskay's avatar
Boris Mericskay committed
4
5
    <<meta charset='utf-8' />
    <title>MapLibreGL / 3D Choropleth Map</title>
Boris Mericskay's avatar
Boris Mericskay committed
6
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
Boris Mericskay's avatar
Boris Mericskay committed
7
8
9
	
<script src='https://unpkg.com/maplibre-gl@2.1.1/dist/maplibre-gl.js'></script>
<link href='https://unpkg.com/maplibre-gl@2.1.1/dist/maplibre-gl.css' rel='stylesheet' />
Boris Mericskay's avatar
Boris Mericskay committed
10
11

    <style>
Boris Mericskay's avatar
Boris Mericskay committed
12
13
14
15
16
17
18
        #map {position: absolute; top: 0; right: 0; bottom: 0; left: 0;}  
    
    .Mypopup .maplibregl-popup-content {
      background-color: white;
      color : black;
      opacity : 0.8;
}
Boris Mericskay's avatar
Boris Mericskay committed
19

Boris Mericskay's avatar
Boris Mericskay committed
20
    </style>
Boris Mericskay's avatar
Boris Mericskay committed
21
22
23
24
25
26
27
</head>
<body>
 
<div id='map'></div>

<script>

Boris Mericskay's avatar
Boris Mericskay committed
28
29
30
//Configuration de la carte
var map = new maplibregl.Map({
    container: 'map', // container id
Boris Mericskay's avatar
Boris Mericskay committed
31
32
33
34
35
36
37
38
39
40
  style: 'https://geoserveis.icgc.cat/contextmaps/positron.json', //Fond de carte 
	center: [-2.802, 48.1],
	zoom :7.7,
	pitch : 40,
	bearing : 1
});
map.on('style.load', function() {

  
    // Add data
Boris Mericskay's avatar
Boris Mericskay committed
41

Boris Mericskay's avatar
Boris Mericskay committed
42
43
44
45
46
47
48
49
      
 map.addSource('Municipalities', {
            type: 'geojson',
            data: 'https://raw.githubusercontent.com/mastersigat/data/main/communesBZH.geojson' 
        });
 
  
  
Boris Mericskay's avatar
Boris Mericskay committed
50
	// Municipalities layer
Boris Mericskay's avatar
Boris Mericskay committed
51
52
53
54
55
56
	
map.addLayer({
        'id': 'Municipalities',
        'type': 'fill-extrusion',
        'source': 'Municipalities',
        'layout': {'visibility': 'visible'},
Boris Mericskay's avatar
Boris Mericskay committed
57
58
59
		'paint': {'fill-extrusion-color': {
                  property: 'density',
                  stops: [
Boris Mericskay's avatar
Boris Mericskay committed
60
61
62
63
64
                    [20, '#4d9221'],
                    [50, '#a1d76a'],
                    [100, '#e6f5d0'],
                    [200, '#fde0ef'],
                    [500, '#e9a3c9'],
Boris Mericskay's avatar
Boris Mericskay committed
65
66
67
68
69
70
					[1000, '#c51b7d'],]},
					'fill-extrusion-height': {
        'property': 'POPULATION',
		    'stops': [[100, 10],
					  [100, 100],
		              [200000, 70000]] },
Boris Mericskay's avatar
Boris Mericskay committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
      'fill-extrusion-opacity': 0.95,
      'fill-extrusion-base': 0
    }
}); 
	
	
	       // Add Departments

       map.addSource('lignes', {
            type: 'geojson',
            data: 'https://raw.githubusercontent.com/mastersigat/data/main/Departements.geojson' 
        });

     map.addLayer({
            'id': 'Lignes',
            'type': 'line',
            'source': 'lignes',
            'paint': {'line-width': 2.5,
                     'line-color': '#111111'}
     });   
    
Boris Mericskay's avatar
Boris Mericskay committed
92
		 
Boris Mericskay's avatar
Boris Mericskay committed
93
94
95

		});	 

Boris Mericskay's avatar
Boris Mericskay committed
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
		
		
		// Interactivity configuration (popup)

var popup = new maplibregl.Popup({
    className: "Mypopup",
  closeButton: false,
    closeOnClick: false });

map.on('mousemove', 'Municipalities', function(e) {
        // Change the cursor style as a UI indicator.
        map.getCanvas().style.cursor = 'pointer';

        // Single out the first found feature.
        var feature = e.features[0];

        // Display a popup with the name of the county
        popup.setLngLat(e.lngLat)
		.setHTML('<h3>'+ feature.properties.NOM_COM_M + '</h3><hr><h4>'  
		               + feature.properties.POPULATION + ' habitants <br>' 
					   + feature.properties.density + ' hab./km2 </h4>')
        .addTo(map);
    });

    map.on('mouseleave', 'Municipalities', function() {
        map.getCanvas().style.cursor = '';
        popup.remove();
    });





    // Add navigation control and scale
Boris Mericskay's avatar
Boris Mericskay committed
130
  
Boris Mericskay's avatar
Boris Mericskay committed
131
132
133
134
135
136
137
138
139
var nav = new maplibregl.NavigationControl();
map.addControl(nav, 'top-left');

var scale = new maplibregl.ScaleControl({
maxWidth: 80,
unit: 'metric'
});
map.addControl(scale);

Boris Mericskay's avatar
Boris Mericskay committed
140
141
142
143
  
</script>

</body>
Boris Mericskay's avatar
Boris Mericskay committed
144
</html>