forked from AAK-Tele-Science/data_engineer_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
151 lines (135 loc) · 5.79 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
<!DOCTYPE html>
<html>
<head>
<title>World Bank Country Data Map</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<style>
#map { height: calc(100vh - 115px); }
.legend {
background: white;
padding: 10px;
line-height: 1.5;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
.search-container {
padding: 10px;
background: white;
position: relative;
z-index: 1000;
display: flex;
align-items: center;
}
.info-container {
padding: 10px;
background: white;
position: relative;
z-index: 1000;
margin-left: 20px;
}
</style>
</head>
<body>
<div class="search-container">
<input type="text" id="countrySearch" placeholder="Enter country name" />
<button onclick="searchCountry()">Search</button>
<div class="info-container">
<strong>HDI Growth Definition</strong><br>
The Human Development Index (HDI) is a summary measure of average achievement in key dimensions of human development: a long and healthy life, being knowledgeable, and having a decent standard of living. The higher the HDI, means the better standard of living.<br>
</div>
</div>
<div id="map"></div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script>
// Initialize the map
var map = L.map('map').setView([20, 0], 2);
// Add a tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// HDI color function based on the provided map
function getColor(d) {
return d > 0.8 ? '#08306b' :
d > 0.75 ? '#08519c' :
d > 0.5 ? '#3182bd' :
d > 0.25 ? '#6baed6' :
'#c6dbef';
}
// Store country data globally for search functionality
var countryData = [];
// Fetch country data from the API
fetch('/countries/')
.then(response => response.json())
.then(data => {
countryData = data;
data.forEach(country => {
if (country.latitude && country.longitude) {
var hdi = country['HDI'] ? country['HDI'].toFixed(2) : 'N/A';
var color = getColor(country['HDI']);
var circle = L.circleMarker([country.latitude, country.longitude], {
radius: 8,
fillColor: color,
color: color,
weight: 1,
opacity: 1,
fillOpacity: 0.8
}).addTo(map);
circle.bindPopup(`
<b>${country.name}</b><br>
Population: ${country['Population, total']}<br>
Population growth: ${country['Population growth']}%<br>
Net migration: ${country['Net migration']}<br>
Estimated Human Development Index (HDI): ${hdi}
`);
}
});
})
.catch(error => {
console.error('Error fetching country data:', error);
});
// Add a legend
var legend = L.control({ position: 'bottomright' });
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'legend'),
grades = [0, 0.25, 0.5, 0.75, 0.8],
labels = [];
div.innerHTML += '<strong>HDI Legend</strong><br>';
// Loop through our HDI intervals and generate a label with a colored square for each interval
for (var i = 0; i < grades.length; i++) {
div.innerHTML +=
'<i style="background:' + getColor(grades[i] + 0.01) + '"></i> ' +
grades[i] + (grades[i + 1] ? '–' + grades[i + 1] + '<br>' : '+');
}
return div;
};
legend.addTo(map);
// Search function to locate and zoom to a country
function searchCountry() {
var searchInput = document.getElementById('countrySearch').value.toLowerCase();
var foundCountry = countryData.find(country => country.name.toLowerCase() === searchInput);
if (foundCountry && foundCountry.latitude && foundCountry.longitude) {
map.setView([foundCountry.latitude, foundCountry.longitude], 5); // Adjust zoom level as needed
L.popup()
.setLatLng([foundCountry.latitude, foundCountry.longitude])
.setContent(`
<b>${foundCountry.name}</b><br>
Population: ${foundCountry['Population, total']}<br>
Population growth: ${foundCountry['Population growth']}%<br>
Net migration: ${foundCountry['Net migration']}<br>
Human Development Index (HDI): ${foundCountry['HDI'] ? foundCountry['HDI'].toFixed(2) : 'N/A'}
`)
.openOn(map);
} else {
alert('Country not found or no geo-location available.');
}
}
</script>
</body>
</html>