This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
343 lines (333 loc) · 11.1 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hurtos a personas 2017 - Bogotá, Colombia</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<style>
html, body {
height: 100%;
width: 100%;
}
</style>
</head>
<body style="text-align: center">
<h2>Characterization and mapping of robberies in Bogotá</h2>
<h4>Proyecto de Geostatistics (GEOC3303) por Diego Rodríguez Baquero</h4>
<table style="width: 100%; height: 70%">
<tr>
<td style="width: 50%; text-align: center">
<div id="map" class="map" style="width: 400px; height: 600px; margin: 0 auto;"></div>
<form>
<label>Factor</label>
<input id="factor" type="range" min="0.5" max="3" step="0.1" value="1.5"/>
<label id="factorVal">1.5</label><br>
<label>Radio</label>
<input id="radius" type="range" min="1" max="50" step="1" value="5"/><br>
<label>Difuminado</label>
<input id="blur" type="range" min="1" max="50" step="1" value="15"/><br>
<label>Filtro</label>
<select id="property" type="text" value="total">
<option value="total">Todos</option>
<option value="sexo.MASCULINO">Sexo Masculino</option>
<option value="sexo.FEMENINO">Sexo Femenino</option>
<option value="armas.SIN EMPLEO DE ARMAS">Sin armas</option>
<option value="armas.ARMA DE FUEGO">Arma de fuego</option>
<option value="armas.CONTUNDENTES">Contundente</option>
<option value="armas.ARMA BLANCA / CORTOPUNZANTE">Arma blanca</option>
<option value="armas.ESCOPOLAMINA">Escopolamina</option>
<option value="sexoArmas.MASCULINO.SIN EMPLEO DE ARMAS">Sexo Masculino - Sin armas</option>
<option value="sexoArmas.MASCULINO.ARMA DE FUEGO">Sexo Masculino - Arma de fuego</option>
<option value="sexoArmas.MASCULINO.CONTUNDENTES">Sexo Masculino - Contundente</option>
<option value="sexoArmas.MASCULINO.ARMA BLANCA / CORTOPUNZANTE">Sexo Masculino - Arma blanca</option>
<option value="sexoArmas.MASCULINO.ESCOPOLAMINA">Sexo Masculino - Escopolamina</option>
<option value="sexoArmas.FEMENINO.SIN EMPLEO DE ARMAS">Sexo Femenino - Sin armas</option>
<option value="sexoArmas.FEMENINO.ARMA DE FUEGO">Sexo Femenino - Arma de fuego</option>
<option value="sexoArmas.FEMENINO.CONTUNDENTES">Sexo Femenino - Contundente</option>
<option value="sexoArmas.FEMENINO.ARMA BLANCA / CORTOPUNZANTE">Sexo Femenino - Arma blanca</option>
<option value="sexoArmas.FEMENINO.ESCOPOLAMINA">Sexo Femenino - Escopolamina</option>
</select>
</form>
</td>
<td style="width: 50%;">
<table>
<tr>
<td><canvas id="sexo" style="margin: 0 auto; padding-bottom: 5%" width="400" height="300"></canvas></td>
<td><canvas id="sexo2" style="margin: 0 auto; padding-bottom: 5%" width="400" height="300"></canvas></td>
</tr>
<tr>
<td><canvas id="armas" style="margin: 0 auto" width="400" height="350"></canvas></td>
<td><canvas id="armas2" style="margin: 0 auto" width="400" height="350"></canvas></td>
</tr>
</table>
</td>
</tr>
</table>
<p>Datos: Hurto a personas 2017 (Filtrado a Bogotá) - Obtenidos de <a href="https://www.datos.gov.co/Seguridad-y-Defensa/Hurto-a-personas-2017/cwdd-8tiq" target="_blank">Datos Abiertos</a></span>
<script>
const factor = document.getElementById('factor')
const factorVal = document.getElementById('factorVal')
const blur = document.getElementById('blur')
const radius = document.getElementById('radius')
const property = document.getElementById('property')
const ctx = document.getElementById("sexo").getContext('2d')
const ctx2 = document.getElementById("armas").getContext('2d')
const ctx3 = document.getElementById("sexo2").getContext('2d')
const ctx4 = document.getElementById("armas2").getContext('2d')
const vectorSource = new ol.source.Vector()
const heatmap = new ol.layer.Heatmap({
source: vectorSource,
blur: 5,
radius: 10,
opacity: 0.7
})
let data = []
let prop = property.value
fetch('data2.json').then(res => res.json()).then(json => {
data = json
updateMap()
updateCharts()
})
let raster = new ol.layer.Tile({
source: new ol.source.Stamen({
layer: 'toner'
})
})
let map = new ol.Map({
layers: [
raster,
heatmap
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([-74.0982061, 4.6503369]),
zoom: 11
})
})
blur.addEventListener('input', function() {
heatmap.setBlur(parseInt(blur.value, 10))
})
radius.addEventListener('input', function() {
heatmap.setRadius(parseInt(radius.value, 10))
})
factor.addEventListener('input', () =>{
updateMap()
factorVal.innerHTML = Number(factor.value).toFixed(1)
})
property.addEventListener('change', () => {
updateMap()
updateCharts()
})
const sexos = ['MASCULINO', 'FEMENINO']
const armas = ['SIN EMPLEO DE ARMAS', 'ARMA DE FUEGO', 'CONTUNDENTES', 'ARMA BLANCA / CORTOPUNZANTE', 'ESCOPOLAMINA']
let charts = []
let contSexos = [0, 0]
let contArmas = [0, 0, 0, 0, 0]
function updateMap () {
prop = property.value
vectorSource.clear()
let max = 0
contSexos = [0, 0]
contArmas = [0, 0, 0, 0, 0]
for (let p of data) {
if (p[prop] && p[prop] > max) {
max = p[prop]
}
if (prop === 'total') {
for (const sexo of sexos) {
if (p[`sexo.${sexo}`]) contSexos[sexos.indexOf(sexo)] += p[`sexo.${sexo}`]
}
for (let arma of armas) {
if (p[`armas.${arma}`]) contArmas[armas.indexOf(arma)] += p[`armas.${arma}`]
}
} else if (prop.startsWith('sexoArmas')) {
const sexo = prop.split('.')[1]
const arma = prop.split('.')[2]
if (p[`sexo.${sexo}`]) contSexos[sexos.indexOf(sexo)] += p[`sexo.${sexo}`]
if (p[`armas.${arma}`]) contArmas[armas.indexOf(arma)] += p[`armas.${arma}`]
} else if (prop.startsWith('armas')) {
const arma = prop.split('.')[1]
if (p[`armas.${arma}`]) contArmas[armas.indexOf(arma)] += p[`armas.${arma}`]
for (let sexo of sexos) {
if (p[`sexoArmas.${sexo}.${arma}`]) contSexos[sexos.indexOf(sexo)] += p[`sexoArmas.${sexo}.${arma}`]
}
} else if (prop.startsWith('sexo')) {
const sexo = prop.split('.')[1]
if (p[`sexo.${sexo}`]) contSexos[sexos.indexOf(sexo)] += p[`sexo.${sexo}`]
for (let arma of armas) {
if (p[`sexoArmas.${sexo}.${arma}`]) contArmas[armas.indexOf(arma)] += p[`sexoArmas.${sexo}.${arma}`]
}
}
}
max = Math.pow(max, 1 / factor.value)
console.log('Max', max)
console.log('contSexos', contSexos)
console.log('contArmas', contArmas)
for (let p of data) {
p[prop] = p[prop] || 0
const ft = new ol.Feature({
name: p.name,
geometry: new ol.geom.Point(ol.proj.fromLonLat([Number(p.lng), Number(p.lat)])),
weight: Math.pow(p[prop], 1 / factor.value) / max
})
vectorSource.addFeature(ft)
}
}
function updateCharts () {
charts.forEach(chart => chart.destroy())
charts = []
charts.push(new Chart(ctx, {
type: 'bar',
data: {
labels: sexos.map(sexo => sexo.charAt(0).toUpperCase() + sexo.slice(1).toLowerCase()),
datasets: [{
label: false,
data: contSexos,
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(255,99,132,1)',
],
borderWidth: 1
}]
},
options: {
legend: {
display: false
},
title: {
display: true,
text: '# robos por sexo'
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
},
responsive: false
}
}))
const sexSum = contSexos[0] + contSexos[1]
charts.push(new Chart(ctx3, {
type: 'pie',
data: {
labels: sexos.map(sexo => sexo.charAt(0).toUpperCase() + sexo.slice(1).toLowerCase()),
datasets: [{
label: false,
data: contSexos.map(sexo => (sexo/sexSum*100).toFixed(0)),
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(255,99,132,1)',
],
borderWidth: 1
}]
},
options: {
legend: {
display: true
},
title: {
display: true,
text: '% sexo'
},
responsive: false
}
}))
charts.push(new Chart(ctx2, {
type: 'bar',
data: {
labels: armas.map(arma => arma.charAt(0).toUpperCase() + arma.slice(1).toLowerCase()),
datasets: [{
label: '',
data: contArmas,
backgroundColor: [
'rgba(100, 100, 100, 0.2)',
'rgba(200, 20, 20, 0.2)',
'rgba(240, 150, 0, 0.2)',
'rgba(240, 240, 0, 0.2)',
'rgba(50, 150, 255, 0.2)'
],
borderColor: [
'rgba(150, 150, 150, 1)',
'rgba(200, 20, 20, 1)',
'rgba(240, 150, 0, 1)',
'rgba(240, 240, 0, 1)',
'rgba(50, 150, 255, 1)'
],
borderWidth: 1
}]
},
options: {
legend: {
display: false,
},
title: {
display: true,
text: '# robos por tipo de arma'
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}],
xAxes: [{
ticks: {
autoSkip: false
}
}]
},
responsive: false
}
}))
const sumArmas = contArmas.reduce((sum, v) => sum + v)
charts.push(new Chart(ctx4, {
type: 'pie',
data: {
labels: armas.map(arma => arma.charAt(0).toUpperCase() + arma.slice(1).toLowerCase()),
datasets: [{
label: '',
data: contArmas.map(arma => (arma/sumArmas*100).toFixed(0)),
backgroundColor: [
'rgba(100, 100, 100, 0.2)',
'rgba(200, 20, 20, 0.2)',
'rgba(240, 150, 0, 0.2)',
'rgba(240, 240, 0, 0.2)',
'rgba(50, 150, 255, 0.2)'
],
borderColor: [
'rgba(150, 150, 150, 1)',
'rgba(200, 20, 20, 1)',
'rgba(240, 150, 0, 1)',
'rgba(240, 240, 0, 1)',
'rgba(50, 150, 255, 1)'
],
borderWidth: 1
}]
},
options: {
title: {
display: true,
text: '% tipo de arma'
},
legend: {
position: 'bottom'
},
responsive: false
}
}))
}
</script>
</body>
</html>