2020-11-21 22:29:35 +01:00
|
|
|
const aufgabe3 = async () => {
|
|
|
|
|
|
|
|
const loadData = async () => {
|
2020-11-21 23:30:42 +01:00
|
|
|
const loadSeries = async (seriesCode, pageSize = 20) => {
|
|
|
|
const values = (await axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", {
|
|
|
|
params: {
|
|
|
|
seriesCode: seriesCode,
|
|
|
|
pageSize: pageSize
|
|
|
|
}
|
|
|
|
})).data // getting axios data object
|
|
|
|
return values.data // getting api data object (remove paging information)
|
|
|
|
.filter(elem => elem.timePeriodStart >= 2015 && elem.timePeriodStart <= 2017)
|
|
|
|
.filter(elem => elem.dimensions.Location === "ALLAREA")
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:29:35 +01:00
|
|
|
const apiData = await Promise.all([
|
|
|
|
loadSeries("SH_H2O_SAFE", 5000),
|
|
|
|
loadSeries("SH_SAN_SAFE", 5000)
|
|
|
|
])
|
2020-11-21 23:30:42 +01:00
|
|
|
return [...apiData[0], ...apiData[1]]
|
2020-11-21 22:29:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const levels = [0, 50, 80, 85, 90, 95, 99, 100]
|
|
|
|
|
|
|
|
const getColor = percentage => {
|
|
|
|
switch (true) {
|
|
|
|
case percentage > levels[7]:
|
|
|
|
return '#800026'
|
|
|
|
case percentage > levels[6]:
|
|
|
|
return '#BD0026'
|
|
|
|
case percentage > levels[5]:
|
|
|
|
return '#E31A1C'
|
|
|
|
case percentage > levels[4]:
|
|
|
|
return '#FC4E2A'
|
|
|
|
case percentage > levels[3]:
|
|
|
|
return '#FD8D3C'
|
|
|
|
case percentage > levels[2]:
|
|
|
|
return '#FEB24C'
|
|
|
|
case percentage > levels[1]:
|
|
|
|
return '#FED976'
|
|
|
|
case percentage > levels[0]:
|
|
|
|
return '#FFEDA0'
|
|
|
|
default:
|
|
|
|
return '#ffdea1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const map = L.map('suiMap').setView([53.0, 14.0], 4)
|
|
|
|
map.addLayer(new L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
|
|
attribution: 'Map data <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
|
|
|
|
minZoom: 1,
|
|
|
|
maxZoom: 18
|
|
|
|
}))
|
|
|
|
let [geojsonData, apiData] = await Promise.all([
|
|
|
|
$.getJSON('europe.geo.json'),
|
|
|
|
loadData()
|
|
|
|
])
|
|
|
|
|
2020-11-21 23:30:42 +01:00
|
|
|
// Attach api data to geojson
|
2020-11-21 22:29:35 +01:00
|
|
|
for (let elem of apiData) {
|
|
|
|
const find = geojsonData.features.find(value => {
|
2020-11-21 23:30:42 +01:00
|
|
|
return value.properties.name_long.includes(elem.geoAreaName) || elem.geoAreaName.includes(value.properties.name_long)
|
2020-11-21 22:29:35 +01:00
|
|
|
})
|
2020-11-21 23:30:42 +01:00
|
|
|
if (find) find.properties[elem.series + "_" + elem.timePeriodStart] = elem
|
2020-11-21 22:29:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the legend to the map
|
|
|
|
const legend = L.control({position: 'bottomright'})
|
|
|
|
|
|
|
|
legend.onAdd = () => {
|
|
|
|
const legend = L.DomUtil.create('div', 'info legend')
|
|
|
|
const labels = []
|
|
|
|
|
|
|
|
for (let i = 0; i < levels.length; i++) {
|
|
|
|
labels.push(
|
|
|
|
'<i style="background:' + getColor(levels[i] + 1) + '"></i> '
|
|
|
|
+ levels[i] + (levels[i + 1] ? '–' + levels[i + 1] : '+')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
legend.innerHTML = labels.join('<br>')
|
|
|
|
return legend
|
|
|
|
}
|
|
|
|
|
|
|
|
legend.addTo(map)
|
|
|
|
|
|
|
|
// Control that shows state info on hover
|
|
|
|
const info = L.control()
|
|
|
|
|
|
|
|
info.onAdd = () => {
|
|
|
|
info._div = L.DomUtil.create('div', 'info')
|
|
|
|
info.update()
|
|
|
|
return info._div
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:30:42 +01:00
|
|
|
info.update = (props = {}, currSeries) => {
|
|
|
|
const localProps = props[currSeries]
|
|
|
|
info._div.innerHTML = localProps ? `<h5>${localProps.seriesDescription}</h5>` : ''
|
|
|
|
if (localProps) {
|
|
|
|
let formattedPercentage = new Intl.NumberFormat('de-DE').format(localProps.value)
|
|
|
|
info._div.innerHTML += `<b>${props.formal_en}</b><br>${formattedPercentage} %`
|
2020-11-21 22:29:35 +01:00
|
|
|
} else {
|
|
|
|
info._div.innerHTML += 'Hover over a country'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.addTo(map)
|
|
|
|
|
|
|
|
let layers = {}
|
2020-11-21 23:30:42 +01:00
|
|
|
let wantedSeries = [
|
|
|
|
{name: "SH_H2O_SAFE_2015", displayName: "drinking water services 2015"},
|
|
|
|
{name: "SH_H2O_SAFE_2016", displayName: "drinking water services 2016"},
|
|
|
|
{name: "SH_H2O_SAFE_2017", displayName: "drinking water services 2017"},
|
|
|
|
{name: "SH_SAN_SAFE_2015", displayName: "sanitation services 2015"},
|
|
|
|
{name: "SH_SAN_SAFE_2016", displayName: "sanitation services 2016"},
|
|
|
|
{name: "SH_SAN_SAFE_2017", displayName: "sanitation services 2017"},
|
2020-11-21 22:29:35 +01:00
|
|
|
]
|
|
|
|
|
2020-11-21 23:30:42 +01:00
|
|
|
for (let currSeries of wantedSeries) {
|
2020-11-21 22:29:35 +01:00
|
|
|
let geoJsonLayer = L.geoJson(geojsonData, {
|
|
|
|
style: feature => ({
|
|
|
|
stroke: true,
|
|
|
|
fill: true,
|
2020-11-21 23:30:42 +01:00
|
|
|
fillColor: getColor(feature.properties[currSeries.name].value),
|
2020-11-21 22:29:35 +01:00
|
|
|
fillOpacity: 1,
|
|
|
|
dashArray: '3',
|
|
|
|
weight: 2,
|
|
|
|
color: 'black'
|
|
|
|
}),
|
|
|
|
filter: feature => {
|
2020-11-21 23:30:42 +01:00
|
|
|
return feature.properties.hasOwnProperty(currSeries.name)
|
2020-11-21 22:29:35 +01:00
|
|
|
},
|
|
|
|
onEachFeature: (feature, layer) => {
|
|
|
|
layer.on({
|
|
|
|
mouseover: event => {
|
|
|
|
event.target.setStyle({
|
|
|
|
weight: 5,
|
|
|
|
color: '#666',
|
|
|
|
dashArray: '',
|
|
|
|
fillOpacity: 0.7
|
|
|
|
})
|
2020-11-21 23:30:42 +01:00
|
|
|
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) event.target.bringToFront()
|
|
|
|
info.update(feature.properties, currSeries.name)
|
2020-11-21 22:29:35 +01:00
|
|
|
},
|
|
|
|
mouseout: event => {
|
|
|
|
geoJsonLayer.resetStyle(event.target)
|
|
|
|
info.update()
|
|
|
|
},
|
|
|
|
click: event => map.fitBounds(event.target.getBounds())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2020-11-21 23:30:42 +01:00
|
|
|
layers[currSeries.displayName] = geoJsonLayer
|
2020-11-21 22:29:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
L.control.layers(layers, null).addTo(map)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
aufgabe3()
|