Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (75 loc) · 2.49 KB
/
Copy pathscript.js
File metadata and controls
90 lines (75 loc) · 2.49 KB
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
var map = new L.map('map')
map.fitBounds([[50.3276,5.8664],[52.5325,9.4623]])
L.tileLayer('http://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Load Lines
var re10FetchPromise = fetch('lines/re10.geojson')
.then(fetchToJson)
var re1FetchPromise = fetch('lines/re1.geojson')
.then(fetchToJson)
var verspaetungPromise = fetch('data/verspaetung.csv')
.then(function (response) {
return response.text()
})
.then(function (verspaetungenText) {
return Papa.parse(verspaetungenText, {
header: true
}).data
})
var lines = ['RE1', 'RE2', 'RE3', 'RE4', 'RE5', 'RE6', 'RE10']
var linesPromises = Promise.all(
lines.map(function (line) {
var filename = 'lines/' + line.toLowerCase() + '.geojson'
return fetch(filename)
.then(fetchToJson)
})
)
Promise.all([verspaetungPromise, linesPromises])
.then(function (fetchs) {
var verspaetungen = fetchs[0]
var linesData = fetchs[1]
var verspaetungenValues = verspaetungen.map(function (item) { return Number(item.Verspaetung) })
var minVerspaetung = Math.min.apply(null, verspaetungenValues)
var maxVerspaetung = Math.max.apply(null, verspaetungenValues)
var scale = chroma.scale(['green', 'yellow', 'red']).domain([minVerspaetung, maxVerspaetung])
for (var i = 0; i < lines.length; i++) {
console.log(lines[i])
addLine(lines[i], linesData[i], 2015)
}
function addLine (ref, geojson, year) {
var verspaetung = findRow(ref, verspaetungen, year || 2015)
var color = scale(Number(verspaetung.Verspaetung)).hex()
L.geoJson(geojson, {
style: {
color: color,
weight: 5,
opacity: 1
},
filter: function (feature) {
return feature.geometry.type === 'LineString'
}
}).addTo(map)
}
}, function (e) {
window.alert('Daten konnten nicht geladen werden. Fehlermeldung: \n\n\n' + JSON.stringify(e, null, 4))
console.error(e)
})
.catch(function (e) {
window.alert('Daten konnten nicht geladen werden. Fehlermeldung: \n\n\n' + JSON.stringify(e, null, 4))
console.error(e)
})
function findRow (line, rows, year) {
for(var i = 0; i < rows.length; i++) {
var row = rows[i]
if (row.Linie === line) {
if (year !== undefined || year === row.Jahr) {
return row
}
return row
}
}
}
function fetchToJson (response) {
return response.json()
}