initial commit
|
1 |
//views/paris.jade |
2 |
html |
|
3 |
head |
|
4 |
link(rel='stylesheet', href="#{url}/stylesheets/leaflet.css") |
|
5 |
#map(style='height: 100%;') |
|
6 |
style. |
|
7 |
.info { |
|
8 |
padding: 6px 8px; |
|
9 |
font: 14px/16px Arial, Helvetica, sans-serif; |
|
10 |
background: white; |
|
11 |
background: rgba(255,255,255,0.8); |
|
12 |
box-shadow: 0 0 15px rgba(0,0,0,0.2); |
|
13 |
border-radius: 5px; |
|
14 |
} |
|
15 |
.info h4 { |
|
16 |
margin: 0 0 5px; |
|
17 |
color: #777; |
|
18 |
} |
|
19 |
.legend { |
|
20 |
text-align: left; |
|
21 |
line-height: 18px; |
|
22 |
color: #555; |
|
23 |
} |
|
24 |
.legend i { |
|
25 |
width: 18px; |
|
26 |
height: 18px; |
|
27 |
float: left; |
|
28 |
margin-right: 8px; |
|
29 |
opacity: 0.7; |
|
30 |
} |
|
31 |
body |
|
32 |
script(src="#{url}/javascripts/leaflet.js") |
|
33 |
script. |
|
34 |
var zones = !{JSON.stringify(zones)}; |
|
35 |
var gradient = !{JSON.stringify(gradient)}; |
|
36 |
var latlon = { lat : { max : 0, min : 90 }, lon : { max : 0, min : 90 } }; |
|
37 | ||
38 |
zones.features.forEach( |
|
39 |
function(feature) { |
|
40 |
feature.geometry.coordinates.forEach( |
|
41 |
function(polygon) { |
|
42 |
polygon.forEach( |
|
43 |
function(point) { |
|
44 |
latlon.lat.max = (latlon.lat.max > point[1]) ? latlon.lat.max : point[1]; |
|
45 |
latlon.lon.max = (latlon.lon.max > point[0]) ? latlon.lon.max : point[0]; |
|
46 |
latlon.lat.min = (latlon.lat.min < point[1]) ? latlon.lat.min : point[1]; |
|
47 |
latlon.lon.min = (latlon.lon.min < point[0]) ? latlon.lon.min : point[0]; |
|
48 |
}); |
|
49 |
}); |
|
50 |
}); |
|
51 |
var center = [latlon.lat.min + (latlon.lat.max - latlon.lat.min)/2, latlon.lon.min + (latlon.lon.max - latlon.lon.min)/2]; |
|
52 |
var zoom = 15; |
|
53 |
var map = L.map("map").setView(center, zoom); |
|
54 | ||
55 |
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '<a href="#{url}/moteur.js">Code source moteur (GPL)</a><br>Découpage par bureau 2014: <a href="https://www.data.gouv.fr/fr/datasets/zones-de-rattachement-des-bureaux-de-vote-en-2014-prs/">Mairie de Paris</a><br>Données électorales: <a href="#{url}/elections.db">Section PCF Paris 12</a> (SQLite, 12Mo)<br>© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(map); |
|
56 | ||
57 |
var info = L.control(); |
|
58 | ||
59 |
info.onAdd = function (map) { |
|
60 |
this._div = L.DomUtil.create('div', 'info'); |
|
61 |
this.update(); |
|
62 |
return this._div; |
|
63 |
}; |
|
64 | ||
65 |
function makePopup(f) { |
|
66 |
var texte = "<p><b><u>Bureau " + f.properties.num_bv + " :</u></b>"; |
|
67 |
for (var hsla in gradient) { |
|
68 |
if (gradient[hsla].intitule) { |
|
69 |
texte += '<br>' + gradient[hsla].intitule + " : "; |
|
70 |
if (gradient[hsla].type === '%') { |
|
71 |
texte += (gradient[hsla][f.id] * 100).toFixed(2) + '%'; |
|
72 |
} |
|
73 |
else { |
|
74 |
texte += (gradient[hsla][f.id]).toFixed(0); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
return texte + "</p>"; |
|
79 |
}; |
|
80 | ||
81 |
info.update = function (props) { |
|
82 |
this._div.innerHTML = '<h4>' + gradient.titre + '</h4>'; |
|
83 |
if (props) { |
|
84 |
this._div.innerHTML += '<b><u>Bureau ' + props.num_bv + '</u></b>'; |
|
85 |
for (var hsla in gradient) { |
|
86 |
if (hsla != 'titre') { |
|
87 |
if (gradient[hsla].intitule) { |
|
88 |
this._div.innerHTML += '<br>' + gradient[hsla].intitule + " : "; |
|
89 |
if (gradient[hsla].type === '%') { |
|
90 |
this._div.innerHTML += (gradient[hsla][props.num_bv] * 100).toFixed(2) + '%'; |
|
91 |
} |
|
92 |
else { |
|
93 |
this._div.innerHTML += (gradient[hsla][props.num_bv]).toFixed(0); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
else { |
|
100 |
this._div.innerHTML += 'Passe le curseur sur une zone pour le détail'; |
|
101 |
} |
|
102 |
}; |
|
103 | ||
104 |
info.addTo(map); |
|
105 |
|
|
106 |
var colormodel = { |
|
107 |
hue : { min : 0, max : 350, default : 0 }, |
|
108 |
saturation : { min : 25, max : 100, default : 100 }, |
|
109 |
lightness : { min : 65, max : 25, default : 50 }, |
|
110 |
alpha : { min : 50, max : 95, default : 70 } |
|
111 |
}; |
|
112 |
|
|
113 |
function style(feature) { |
|
114 |
function get_gradient (component) { |
|
115 |
if (!gradient[component]) { |
|
116 |
return colormodel[component].default; |
|
117 |
} |
|
118 |
else { |
|
119 |
var pente = (colormodel[component].max - colormodel[component].min)/(gradient[component].max - gradient[component].min); |
|
120 |
var origine = colormodel[component].min - (pente * gradient[component].min); |
|
121 |
var y = origine + (pente * gradient[component][feature.id]); |
|
122 |
return y.toFixed(0); |
|
123 |
} |
|
124 |
} |
|
125 |
return { |
|
126 |
fillColor: "hsla(" + get_gradient('hue') + "," + get_gradient('saturation') + '%,' + get_gradient('lightness') + '%,' + get_gradient('alpha') / 100 + ")", |
|
127 |
color : this.fillColor, |
|
128 |
opacity : 1, |
|
129 |
fillOpacity : 1 |
|
130 |
}; |
|
131 |
}; |
|
132 |
|
|
133 |
var geojson; |
|
134 |
|
|
135 |
function highlightFeature(e) { |
|
136 |
var layer = e.target; |
|
137 |
layer.setStyle({ |
|
138 |
weight: 5, |
|
139 |
color: '#666', |
|
140 |
dashArray: '', |
|
141 |
fillOpacity: 0.7 |
|
142 |
}); |
|
143 |
if (!L.Browser.ie && !L.Browser.opera) { |
|
144 |
layer.bringToFront(); |
|
145 |
} |
|
146 |
info.update(layer.feature.properties); |
|
147 |
} |
|
148 | ||
149 |
function resetHighlight(e) { |
|
150 |
geojson.resetStyle(e.target); |
|
151 |
info.update(); |
|
152 |
} |
|
153 | ||
154 |
function onEachFeature(feature, layer) { |
|
155 |
layer.on({ |
|
156 |
mouseover: highlightFeature, |
|
157 |
mouseout: resetHighlight, |
|
158 |
click: function () { info.update(layer.feature.properties); } |
|
159 |
}); |
|
160 |
} |
|
161 | ||
162 |
geojson = L.geoJson(zones, { |
|
163 |
style : style, |
|
164 |
onEachFeature: onEachFeature |
|
165 |
}).addTo(map); |