geovisualisierung/projects/project-2/aufgabe3.js

161 lines
5.6 KiB
JavaScript
Raw Normal View History

const aufgabe3 = async () => {
const loadData = async () => {
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")
}
const apiData = await Promise.all([
loadSeries("SH_H2O_SAFE", 5000),
loadSeries("SH_SAN_SAFE", 5000)
])
return [...apiData[0], ...apiData[1]]
}
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()
])
// Attach api data to geojson
for (let elem of apiData) {
const find = geojsonData.features.find(value => {
return value.properties.name_long.includes(elem.geoAreaName) || elem.geoAreaName.includes(value.properties.name_long)
})
if (find) find.properties[elem.series + "_" + elem.timePeriodStart] = elem
}
// 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] ? '&ndash;' + 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
}
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} &percnt;`
} else {
info._div.innerHTML += 'Hover over a country'
}
}
info.addTo(map)
let layers = {}
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"},
]
for (let currSeries of wantedSeries) {
let geoJsonLayer = L.geoJson(geojsonData, {
style: feature => ({
stroke: true,
fill: true,
fillColor: getColor(feature.properties[currSeries.name].value),
fillOpacity: 1,
dashArray: '3',
weight: 2,
color: 'black'
}),
filter: feature => {
return feature.properties.hasOwnProperty(currSeries.name)
},
onEachFeature: (feature, layer) => {
layer.on({
mouseover: event => {
event.target.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
})
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) event.target.bringToFront()
info.update(feature.properties, currSeries.name)
},
mouseout: event => {
geoJsonLayer.resetStyle(event.target)
info.update()
},
click: event => map.fitBounds(event.target.getBounds())
})
}
})
layers[currSeries.displayName] = geoJsonLayer
}
L.control.layers(layers, null).addTo(map)
}
aufgabe3()