-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
338 lines (330 loc) · 11.2 KB
/
map.js
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
var mymap = L.map('mapid').setView([19.380142, -99.142966], 11);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1Ijoib21hci1uYXZhcnJvIiwiYSI6ImNpanN2ZWZxZzBoa291eWx4ZWdsajl1OGIifQ.SH4OG9811nirTGJ3rE4DHw'
}).addTo(mymap);
// COLORES
function choroplethizeHomicidios2015(d) {
return d > 245 ? '#b30000' :
d > 108 ? '#e34a33' :
d > 58 ? '#fc8d59' :
d > 14 ? '#fdcc8a' :
'#fef0d9';
}
function choroplethizeHomicidios2016(d) {
return d > 238 ? '#b30000' :
d > 95 ? '#e34a33' :
d > 66 ? '#fc8d59' :
d > 22 ? '#fdcc8a' :
'#fef0d9';
}
function choroplethizeHomicidios2017(d) {
return d > 192 ? '#b30000' :
d > 123 ? '#e34a33' :
d > 72 ? '#fc8d59' :
d > 31 ? '#fdcc8a' :
'#fef0d9';
}
function choroplethizeRobos2015(d) {
return d > 8783 ? '#7a0177' :
d > 4726 ? '#c51b8a' :
d > 3001 ? '#f768a1' :
d > 1435 ? '#fbb4b9' :
'#feebe2';
}
function choroplethizeRobos2016(d) {
return d > 8303 ? '#7a0177' :
d > 6032 ? '#c51b8a' :
d > 3305 ? '#f768a1' :
d > 1423 ? '#fbb4b9' :
'#feebe2';
}
function choroplethizeRobos2017(d) {
return d > 10447 ? '#7a0177' :
d > 8097 ? '#c51b8a' :
d > 2726 ? '#f768a1' :
d > 1026 ? '#fbb4b9' :
'#feebe2';
}
// PINTAR LAS FIGURAS CON LOS COLORES
function styleHomicidios2015(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeHomicidios2015(feature.properties.homicidios2015_Grand_Total)
}
}
function styleHomicidios2016(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeHomicidios2016(feature.properties.homicidios2016_Grand_Total)
}
}
function styleHomicidios2017(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeHomicidios2017(feature.properties.homicidios2017_Grand_Total)
}
}
function styleRobos2015(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeRobos2015(feature.properties.robbery2015_Total)
}
}
function styleRobos2016(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeRobos2016(feature.properties.robbery2016_Total)
}
}
function styleRobos2017(feature) {
return {
weight: .75,
opacity: 0.5,
color: 'grey',
dashArray: '0',
fillOpacity: 0.9,
fillColor: choroplethizeRobos2017(feature.properties.robbery2017_Total)
}
}
// CREAR CAJAS AL MOMENTO DE HACER CLIC
function geojsonPopupHomicidios2015(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Homicidios en 2015: ' + feature.properties.homicidios2015_Grand_Total)
}
}
function geojsonPopupHomicidios2016(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Homicidios en 2016: ' + feature.properties.homicidios2016_Grand_Total)
}
}
function geojsonPopupHomicidios2017(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Homicidios en 2017: ' + feature.properties.homicidios2017_Grand_Total)
}
}
function geojsonPopupRobos2015(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Robos en 2015: ' + feature.properties.robbery2015_Total)
}
}
function geojsonPopupRobos2016(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Robos en 2016: ' + feature.properties.robbery2016_Total)
}
}
function geojsonPopupRobos2017(feature, layer) {
if (feature.properties.NOMLOC) {
return layer.bindPopup('alcaldía: ' + feature.properties.NOMLOC + '<br>Robos en 2017: ' + feature.properties.robbery2017_Total)
}
}
// CREAR VARIABLES PARA LAS CAPAS
var Homicidios2015Layer = L.geoJSON(homicidios2015, {
style: styleHomicidios2015,
onEachFeature: geojsonPopupHomicidios2015,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
var Homicidios2016Layer = L.geoJSON(homicidios2016, {
style: styleHomicidios2016,
onEachFeature: geojsonPopupHomicidios2016,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
var Homicidios2017Layer = L.geoJSON(homicidios2017, {
style: styleHomicidios2017,
onEachFeature: geojsonPopupHomicidios2017,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
var Robos2015Layer = L.geoJSON(robos2015, {
style: styleRobos2015,
onEachFeature: geojsonPopupRobos2015,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
var Robos2016Layer = L.geoJSON(robos2016, {
style: styleRobos2016,
onEachFeature: geojsonPopupRobos2016,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
var Robos2017Layer = L.geoJSON(robos2017, {
style: styleRobos2017,
onEachFeature: geojsonPopupRobos2017,
pointToLayer: function (feature, latlng) {
return L.marker(latlng);
}
});
// dibujar al mapa
Homicidios2015Layer.addTo(mymap);
var featureLayers = {
"Homicidios 2015": Homicidios2015Layer,
"Homicidios 2016": Homicidios2016Layer,
"Homicidios 2017": Homicidios2017Layer,
"Robos 2015": Robos2015Layer,
"Robos 2016": Robos2016Layer,
"Robos 2017": Robos2017Layer
};
var geojson = L.control.layers(featureLayers, null, {
collapsed: false
}).addTo(mymap);
// LEGEND STARTS HERE
var Homicidios2015Legend = L.control({ position: 'bottomright' });
var Homicidios2016Legend = L.control({ position: 'bottomright' });
var Homicidios2017Legend = L.control({ position: 'bottomright' });
var Robos2015Legend = L.control({ position: 'bottomright' });
var Robos2016Legend = L.control({ position: 'bottomright' });
var Robos2017Legend = L.control({ position: 'bottomright' });
Homicidios2015Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 14, 58, 108, 245],
labels = ['Homicidios por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeHomicidios2015(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 312'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Homicidios2016Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 22, 66, 95, 238],
labels = ['Homicidios por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeHomicidios2016(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 349'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Homicidios2017Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 31, 72, 123, 192],
labels = ['Homicidios por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeHomicidios2017(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 347'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Robos2015Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 1435, 3001, 4726, 8783],
labels = ['Robos por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeRobos2015(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 12646'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Robos2016Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 1423, 3305, 6032, 8303],
labels = ['Robos por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeRobos2016(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 12811'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Robos2017Legend.onAdd = function (mymap) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 1026, 2726, 8097, 10447],
labels = ['Robos por alcadía'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + choroplethizeRobos2017(from + 1) + '"></i> ' +
from + (to ? ' - ' + to : ' - 16633'));
}
div.innerHTML = labels.join('<br>');
return div;
};
Homicidios2015Legend.addTo(mymap);
let currentLegend = Homicidios2015Legend;
// LEGEND Box
mymap.on('baselayerchange', function (eventLayer) {
if (eventLayer.name === 'Homicidios 2015') {
mymap.removeControl(currentLegend);
currentLegend = Homicidios2015Legend;
Homicidios2015Legend.addTo(mymap);
}
else if (eventLayer.name === 'Homicidios 2016') {
mymap.removeControl(currentLegend);
currentLegend = Homicidios2016Legend;
Homicidios2016Legend.addTo(mymap);
}
else if (eventLayer.name === 'Homicidios 2017') {
mymap.removeControl(currentLegend);
currentLegend = Homicidios2017Legend;
Homicidios2017Legend.addTo(mymap);
}
else if (eventLayer.name === 'Robos 2015') {
mymap.removeControl(currentLegend);
currentLegend = Robos2015Legend;
Robos2015Legend.addTo(mymap);
}
else if (eventLayer.name === 'Robos 2016') {
mymap.removeControl(currentLegend);
currentLegend = Robos2016Legend;
Robos2016Legend.addTo(mymap);
}
else if (eventLayer.name === 'Robos 2017') {
mymap.removeControl(currentLegend);
currentLegend = Robos2017Legend;
Robos2017Legend.addTo(mymap);
}
});