433 lines
11 KiB
HTML
433 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
|
|
<title>World Population Density</title>
|
|
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>
|
|
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>
|
|
|
|
<style>
|
|
html, body {
|
|
height: 100%;
|
|
margin: 0;
|
|
}
|
|
|
|
#map { width: 100%; height: 100%; }
|
|
|
|
.info {
|
|
padding: 6px 8px;
|
|
font: 14px/16px Arial, Helvetica, sans-serif;
|
|
background: white;
|
|
background: rgba(255,255,255,0.8);
|
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
|
border-radius: 5px;
|
|
}
|
|
|
|
.info h4 {
|
|
margin: 0 0 5px;
|
|
color: #777;
|
|
}
|
|
|
|
.legend {
|
|
text-align: left;
|
|
line-height: 18px;
|
|
color: #555;
|
|
}
|
|
|
|
.legend i {
|
|
width: 18px;
|
|
height: 18px;
|
|
float: left;
|
|
margin-right: 8px;
|
|
opacity: 0.7;
|
|
}
|
|
|
|
.detail-row {
|
|
line-height: 18px;
|
|
vertical-align: bottom;
|
|
}
|
|
|
|
.detail-col-left {
|
|
text-align: right;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id='foo'>
|
|
|
|
</div>
|
|
<div id='map'></div>
|
|
|
|
<script type="text/javascript" src="data/world/countries-dev.js"></script>
|
|
|
|
<script id="bar" type="text/template">
|
|
<b>{name}</b>
|
|
<table>
|
|
<tr class="detail-row">
|
|
<td class="detail-col-left">Fläche:</td><td>{area} km<sup>2</sup></td>
|
|
</tr>
|
|
<tr class="detail-row">
|
|
<td class="detail-col-left">Einwohner:</td><td>{population}</td>
|
|
</tr>
|
|
<tr class="detail-row">
|
|
<td class="detail-col-left">Dichte:</td><td>{density} / km<sup>2</sup></td>
|
|
</tr>
|
|
</table>
|
|
</script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
function intlFormat(num)
|
|
{
|
|
return new Intl.NumberFormat().format(Math.round(num*10)/10).replace(',', '.');
|
|
}
|
|
function makeFriendly(num)
|
|
{
|
|
if(num >= 1000000000)
|
|
return intlFormat(num/1000000000)+' Mrd.';
|
|
if(num >= 1000000)
|
|
return intlFormat(num/1000000)+' Mio.';
|
|
if(num >= 1000)
|
|
return intlFormat(num/1000)+' Tsd.';
|
|
return intlFormat(num);
|
|
}
|
|
|
|
String.prototype.format = function() {
|
|
// Usage: 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP');
|
|
var formatted = this;
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
|
|
formatted = formatted.replace(regexp, arguments[i]);
|
|
}
|
|
return formatted;
|
|
};
|
|
|
|
function setData() {
|
|
geojson.remove();
|
|
geojson = L.geoJson(worldData[currentDataset], {
|
|
style: style,
|
|
onEachFeature: onEachFeature
|
|
}).addTo(map);
|
|
}
|
|
|
|
|
|
var map = L.map('map').setView([20, 20], 3);
|
|
|
|
var currentZoom = map.getZoom();
|
|
var currentDataset = 'world';
|
|
var worldData = { 'world' : countries };
|
|
|
|
map.on('zoomend', function(ev) {
|
|
if(map.getZoom() < currentZoom){
|
|
console.log('zoomed out to ' + map.getZoom());
|
|
}
|
|
currentZoom = map.getZoom();
|
|
|
|
if(currentZoom <= 6 && currentDataset != 'world'){
|
|
console.log('reset to world now');
|
|
currentDataset = 'world';
|
|
setData();
|
|
}
|
|
|
|
});
|
|
|
|
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
|
|
maxZoom: 18,
|
|
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
|
|
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
|
|
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
|
|
id: 'mapbox.light'
|
|
}).addTo(map);
|
|
|
|
|
|
// control that shows state info on hover
|
|
var info = L.control();
|
|
|
|
info.onAdd = function (map) {
|
|
this._div = L.DomUtil.create('div', 'info');
|
|
this.update();
|
|
return this._div;
|
|
};
|
|
|
|
info.update = function (props) {
|
|
// console.log(props);
|
|
|
|
|
|
|
|
this._div.innerHTML = (props ? render(document.getElementById('bar'), {"name": props.name, "area": makeFriendly(props.area_total_km2), "population": makeFriendly(props.population), "density": props.density }) : render(document.getElementById('bar'), {"name": "World", "area": makeFriendly(149400000), "population": makeFriendly(9000000000), "density": Math.round(9000000000/149400000) }));
|
|
|
|
// this._div.innerHTML = '' + (props ? '<b>' + props.name + '</b><br />Fläche: ' + makeFriendly(props.area_total_km2) + ' km<sup>2</sup><br />' + 'Einwohner: ' + makeFriendly(props.population) + ' <br/>Dichte: ' + props.density + ' / km<sup>2</sup><br />': 'Hover over a country.<br />Click for regional data on:<br /><ul><li>Netherlands</li><li>England</li><li>USA</li></ul>');
|
|
// this._div.innerHTML = '<h4>Population Density</h4>' + (props ?
|
|
// '<b>' + props.name + '</b><br />' + props.density + ' humans / km<sup>2</sup>'
|
|
// : 'Hover over a country.<br />Click for regional data on:<br /><ul><li>Netherlands</li><li>England</li><li>USA</li></ul>');
|
|
};
|
|
|
|
info.addTo(map);
|
|
|
|
|
|
// get color depending on population density value
|
|
function getColor(d) {
|
|
return d > 1000 ? '#800026' :
|
|
d > 500 ? '#BD0026' :
|
|
d > 200 ? '#E31A1C' :
|
|
d > 100 ? '#FC4E2A' :
|
|
d > 50 ? '#FD8D3C' :
|
|
d > 20 ? '#FEB24C' :
|
|
d > 10 ? '#FED976' :
|
|
'#FFEDA0';
|
|
}
|
|
|
|
function style(feature) {
|
|
return {
|
|
weight: 2,
|
|
opacity: 1,
|
|
color: 'white',
|
|
dashArray: '3',
|
|
fillOpacity: 0.7,
|
|
fillColor: getColor(feature.properties.density)
|
|
};
|
|
}
|
|
|
|
function highlightFeature(e) {
|
|
var layer = e.target;
|
|
|
|
layer.setStyle({
|
|
weight: 5,
|
|
color: '#666',
|
|
dashArray: '',
|
|
fillOpacity: 0.7
|
|
});
|
|
|
|
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
|
|
layer.bringToFront();
|
|
}
|
|
|
|
info.update(layer.feature.properties);
|
|
|
|
// console.log(map.view);
|
|
}
|
|
|
|
var geojson;
|
|
|
|
function resetHighlight(e) {
|
|
geojson.resetStyle(e.target);
|
|
info.update();
|
|
}
|
|
|
|
function zoomToFeature(e) {
|
|
// console.log(map.getZoom());
|
|
// console.log(map.getCenter());
|
|
// console.log(e);
|
|
console.log(e.target.feature.id);
|
|
|
|
map.fitBounds(e.target.getBounds());
|
|
|
|
var layer = e.target;
|
|
var countryid = layer.feature.id.toLowerCase();
|
|
currentDataset = countryid;
|
|
|
|
if(worldData[countryid] == undefined){
|
|
console.log('new data path', 'maps/' + countryid + '/provinces.geojson');
|
|
request(countryid, layer);
|
|
}else{
|
|
|
|
geojson.resetStyle(layer);
|
|
info.update();
|
|
geojson.remove();
|
|
|
|
geojson = L.geoJson(worldData[countryid], {
|
|
style: style,
|
|
onEachFeature: onEachFeature
|
|
}).addTo(map);
|
|
|
|
|
|
layer.setStyle({
|
|
weight: 5,
|
|
color: '#666',
|
|
dashArray: '',
|
|
fillOpacity: 0.7
|
|
});
|
|
|
|
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
|
|
layer.bringToFront();
|
|
}
|
|
|
|
info.update(layer.feature.properties);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
function onEachFeature(feature, layer) {
|
|
layer.on({
|
|
mouseover: highlightFeature,
|
|
mouseout: resetHighlight,
|
|
click: zoomToFeature
|
|
});
|
|
}
|
|
|
|
|
|
geojson = L.geoJson(worldData[currentDataset], {
|
|
style: style,
|
|
onEachFeature: onEachFeature
|
|
}).addTo(map);
|
|
|
|
map.attributionControl.addAttribution('Population data by <a href="https://www.cia.gov/library/publications/download">CIA World Factbook</a> | <a href="https://data.worldbank.org/country">Worldbank</a> | <a href="https://en.wikipedia.org">Wikipedia</a>');
|
|
|
|
|
|
var legend = L.control({position: 'bottomright'});
|
|
|
|
legend.onAdd = function (map) {
|
|
|
|
var div = L.DomUtil.create('div', 'info legend'),
|
|
grades = [0, 10, 20, 50, 100, 200, 500, 1000],
|
|
labels = [],
|
|
from, to;
|
|
|
|
for (var i = 0; i < grades.length; i++) {
|
|
from = grades[i];
|
|
to = grades[i + 1];
|
|
|
|
labels.push(
|
|
'<i style="background:' + getColor(from + 1) + '"></i> ' +
|
|
from + (to ? '–' + to : '+'));
|
|
}
|
|
|
|
div.innerHTML = labels.join('<br>');
|
|
return div;
|
|
};
|
|
|
|
legend.addTo(map);
|
|
|
|
|
|
function request(countryid, layer){
|
|
const url = 'maps/{0}/provinces.geojson'.format(countryid);
|
|
var http = new XMLHttpRequest();
|
|
http.open('GET', url, true);
|
|
http.setRequestHeader("Accept","application/json");
|
|
http.send();
|
|
http.onreadystatechange = function()
|
|
{
|
|
console.log(http.readyState,http.status,http.responseText);
|
|
|
|
if (http.readyState != 4){
|
|
return;
|
|
}
|
|
if(http.status == 401){
|
|
console.error(http.responseText);
|
|
}
|
|
else if(http.status == 200){
|
|
var resp = JSON.parse(http.responseText);
|
|
console.log('ok');
|
|
worldData[countryid] = resp;
|
|
// statesData = worldData[countryid];
|
|
|
|
// console.log(statesData);
|
|
// console.log(target);
|
|
|
|
geojson.remove();
|
|
geojson = L.geoJson(worldData[countryid], {
|
|
style: style,
|
|
onEachFeature: onEachFeature
|
|
}).addTo(map);
|
|
|
|
geojson.resetStyle(layer);
|
|
info.update(layer.feature.properties);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
function renderList(template, dataList) {
|
|
var templateHtml = template.innerHTML;
|
|
var listHtml = "";
|
|
var keys = Object.keys(dataList[0]);
|
|
for (var i = 0; i < dataList.length; i++) {
|
|
var itemHtml = templateHtml;
|
|
// console.log(itemHtml);
|
|
for(var j = 0; j < keys.length; j++){
|
|
itemHtml = itemHtml.replace(new RegExp("{"+keys[j]+"}", "g"), dataList[i][keys[j]]);
|
|
|
|
}
|
|
|
|
listHtml += itemHtml;
|
|
}
|
|
console.log(listHtml);
|
|
return listHtml;
|
|
}
|
|
|
|
function render(template, data) {
|
|
var templateHtml = template.innerHTML;
|
|
var listHtml = "";
|
|
var keys = Object.keys(data);
|
|
var itemHtml = templateHtml;
|
|
for(var j = 0; j < keys.length; j++){
|
|
itemHtml = itemHtml.replace(new RegExp("{"+keys[j]+"}", "g"), data[keys[j]]);
|
|
}
|
|
listHtml += itemHtml;
|
|
return listHtml;
|
|
}
|
|
|
|
|
|
|
|
// function onLoad(e) {
|
|
// document.getElementById("foo").innerHTML = render(document.getElementById("bar"), { "name": "entenhausen", "area" : "23km", "population" : "22M", "density" : "23.5" });
|
|
|
|
// }
|
|
|
|
// document.addEventListener('DOMContentLoaded',onLoad);
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script id="sipservers-table-row-error" type="text/template">
|
|
<tr class="error-table-row">
|
|
<td colspan="5">No data available.<br/>{msg}</td>
|
|
</tr>
|
|
</script>
|
|
|
|
|
|
function renderList(template, dataList) {
|
|
var templateHtml = template.innerHTML;
|
|
var listHtml = "";
|
|
var keys = Object.keys(dataList[0]);
|
|
for (var i = 0; i < dataList.length; i++) {
|
|
var itemHtml = templateHtml;
|
|
for(var j = 0; j < keys.length; j++){
|
|
itemHtml = itemHtml.replace(new RegExp("{"+keys[j]+"}", "g"), dataList[i][keys[j]]);
|
|
}
|
|
listHtml += itemHtml;
|
|
}
|
|
return listHtml;
|
|
}
|
|
|
|
document.getElementById("devices-table-rows").innerHTML = renderList(
|
|
document.getElementById("devices-table-row-error"), [{ 'msg' : result['data'] }]);
|
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|