master
Gast 2021-02-17 19:24:56 +01:00
parent 4debddfd2d
commit 5ecfbb937f
8 changed files with 3969 additions and 41 deletions

Binary file not shown.

Binary file not shown.

1
countries.js Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -6,8 +6,26 @@ import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from bs4 import element from bs4 import element
def writejson(filename, aobj):
with open(filename, 'w') as outfile:
json.dump(aobj, outfile, sort_keys=True, indent=4, separators=(', ', ': '))
def writejsoncompact(filename, aobj):
with open(filename, 'w') as outfile:
json.dump(aobj, outfile, sort_keys=True, separators=(',', ':'))
def readjson(file):
print('reading json from %s ...' % file)
data = json.loads(open(file).read())
if not data:
print('error loading data file')
return False
return data
def area_fb2000(): def area_fb2000():
html = open('factbook2000/fields/area.html').read() html = open('data/factbook/2000/area.html').read()
if not html: if not html:
print('error reading area.html') print('error reading area.html')
return False return False
@ -48,7 +66,7 @@ def area_fb2000():
return states return states
def population_fb2000(): def population_fb2000():
file = 'factbook2000/fields/population.html' file = 'data/factbook/2000/population.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -70,7 +88,7 @@ def population_fb2000():
return states return states
def deathrate_fb2000(): def deathrate_fb2000():
file = 'factbook2000/fields/death_rate.html' file = 'data/factbook/2000/death_rate.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -89,7 +107,7 @@ def deathrate_fb2000():
return states return states
def birthrate_fb2000(): def birthrate_fb2000():
html = open('factbook2000/fields/birth_rate.html').read() html = open('data/factbook/2000/birth_rate.html').read()
if not html: if not html:
print('error reading birth_rate.html') print('error reading birth_rate.html')
return False return False
@ -106,7 +124,7 @@ def birthrate_fb2000():
return states return states
def unemploymentrate_fb2000(): def unemploymentrate_fb2000():
file = 'factbook2000/fields/unemployment_rate.html' file = 'data/factbook/2000/unemployment_rate.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -126,7 +144,7 @@ def unemploymentrate_fb2000():
return states return states
def total_fertility_rate_fb2000(): def total_fertility_rate_fb2000():
file = 'factbook2000/fields/total_fertility_rate.html' file = 'data/factbook/2000/total_fertility_rate.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -146,7 +164,7 @@ def total_fertility_rate_fb2000():
return states return states
def population_below_poverty_line_fb2000(): def population_below_poverty_line_fb2000():
file = 'factbook2000/fields/population_below_poverty_line.html' file = 'data/factbook/2000/population_below_poverty_line.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -166,7 +184,7 @@ def population_below_poverty_line_fb2000():
return states return states
def life_expectancy_at_birth_fb2000(): def life_expectancy_at_birth_fb2000():
file = 'factbook2000/fields/life_expectancy_at_birth.html' file = 'data/factbook/2000/life_expectancy_at_birth.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -190,7 +208,7 @@ def life_expectancy_at_birth_fb2000():
return states return states
def infant_mortality_rate_fb2000(): def infant_mortality_rate_fb2000():
file = 'factbook2000/fields/infant_mortality_rate.html' file = 'data/factbook/2000/infant_mortality_rate.html'
html = open(file).read() html = open(file).read()
if not html: if not html:
print('error reading file at <%s>' % file) print('error reading file at <%s>' % file)
@ -279,31 +297,18 @@ def gen_fb2000():
print('fb2000', len(merged), 'items') print('fb2000', len(merged), 'items')
return merged return merged
def writejson(filename, aobj):
with open(filename, 'w') as outfile:
json.dump(aobj, outfile, sort_keys=True, indent=4, separators=(', ', ': '))
def writejsoncompact(filename, aobj):
with open(filename, 'w') as outfile:
json.dump(aobj, outfile, sort_keys=True, separators=(',', ':'))
def readjson(file):
print('reading json from %s ...' % file)
data = json.loads(open(file).read())
if not data:
print('error loading data file')
return False
return data
def add_fb2000_data_to_countries_geojson(): def add_fb2000_data_to_countries_geojson():
datafile = 'fb2000.json' datafile = 'fb2000.json'
data = json.loads(open(datafile).read()) data = json.loads(open(datafile).read())
if not data: if not data:
print('error loading data file') print('error loading data file')
return False return False
countriesfile = 'countries.geo.json' countriesfile = 'data/world/countries.geo.json'
# {"type":"Feature","id":"BHS","properties":{"name":"The Bahamas"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-77.53466,23.75975],[-77.78,23.71],[-78.03405,24.28615],[-78.40848,24.57564],[-78.19087,25.2103],[-77.89,25.17],[-77.54,24.34],[-77.53466,23.75975]]],[[[-77.82,26.58],[-78.91,26.42],[-78.98,26.79],[-78.51,26.87],[-77.85,26.84],[-77.82,26.58]]],[[[-77,26.59],[-77.17255,25.87918],[-77.35641,26.00735],[-77.34,26.53],[-77.78802,26.92516],[-77.79,27.04],[-77,26.59]]]]}},
countries = json.loads(open(countriesfile).read()) countries = json.loads(open(countriesfile).read())
if not countries: if not countries:
print('error loading countries file') print('error loading countries file')
@ -343,16 +348,19 @@ def add_fb2000_data_to_countries_geojson():
density = 'NA' density = 'NA'
country['properties']['density'] = density country['properties']['density'] = density
country['properties']['population'] = int(item['population'])
country['properties']['area_total_km2'] = int(item['area_total_km2'])
found = True found = True
if not found: if not found:
print('no match found for <%s>' % state) print('no match found for <%s>' % state)
writejsoncompact('countries.js', countries) writejsoncompact('data/world/countries-dev.js', countries)
c = open('countries.js').read() c = open('data/world/countries-dev.js').read()
cc = 'var statesData = ' + c cc = 'var statesData = ' + c
with open('countries.js', 'w') as js: with open('data/world/countries-dev.js', 'w') as js:
js.write(cc) js.write(cc)
def json_to_js(filein, fileout): def json_to_js(filein, fileout):
@ -361,7 +369,8 @@ def json_to_js(filein, fileout):
with open(fileout, 'w') as js: with open(fileout, 'w') as js:
js.write(cc) js.write(cc)
def main():
def eng():
geoitems = readjson('eng/regions-ons.json') geoitems = readjson('eng/regions-ons.json')
dataitems = readjson('eng/regions-wiki.json') dataitems = readjson('eng/regions-wiki.json')
@ -385,20 +394,25 @@ def main():
density = round(int(dataitem['population']) / float(dataitem['area_km2']), 1) density = round(int(dataitem['population']) / float(dataitem['area_km2']), 1)
geoitem['properties']['name'] = geoitemname geoitem['properties']['name'] = geoitemname
geoitem['properties']['density'] = density geoitem['properties']['density'] = density
geoitem['properties']['population'] = dataitem['population']
geoitem['properties']['area_km2'] = dataitem['area_km2']
if not match: if not match:
print('no match for <%s>' % geoitemname) print('no match for <%s>' % geoitemname)
writejsoncompact('eng/regions.geojson', geoitems) writejsoncompact('eng/regions.geojson', geoitems)
json_to_js('eng/regions.geojson', 'eng/regions.js') json_to_js('eng/regions.geojson', 'eng/regions.js')
return def nld():
provinces = readjson('nld/provinces.json') provinces = readjson('nld/provinces.json')
geojson = readjson('nld/provinces.geojson') geojson = readjson('nld/provinces.geojson')
for geoitem in geojson['features']: for geoitem in geojson['features']:
name = geoitem['properties']['name'].split(' ')[0] name = geoitem['properties']['name'].split(' ')[0]
match = False match = False
for item in provinces: for item in provinces:
if name == item['name_nl']: if name == item['name_nl']:
@ -410,13 +424,18 @@ def main():
else: else:
density = round(int(item['population']) / float(item['area_km2']), 1) density = round(int(item['population']) / float(item['area_km2']), 1)
geoitem['properties']['density'] = density geoitem['properties']['density'] = density
if not match: if not match:
print('no match for <%s>' % name) print('no match for <%s>' % name)
writejsoncompact('netherlands/provinces-edit.geojson', geojson) writejsoncompact('netherlands/provinces-edit.geojson', geojson)
json_to_js('netherlands/provinces-edit.geojson', 'netherlands/provinces-edit.js') json_to_js('netherlands/provinces-edit.geojson', 'netherlands/provinces-edit.js')
return def main():
# eng()
# nld()
writejson('fb2000.json', gen_fb2000()) writejson('fb2000.json', gen_fb2000())

168
dev.html
View File

@ -20,17 +20,84 @@
#map { width: 100%; height: 100%; } #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; } .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; }</style> .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> </head>
<body> <body>
<div id='foo'>
</div>
<div id='map'></div> <div id='map'></div>
<script type="text/javascript" src="maps/world/countries-dev.js"></script> <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"> <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() { String.prototype.format = function() {
// Usage: 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP'); // Usage: 'The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP');
var formatted = this; var formatted = this;
@ -50,7 +117,7 @@
} }
var map = L.map('map').setView([10, 30], 2); var map = L.map('map').setView([20, 20], 3);
var currentZoom = map.getZoom(); var currentZoom = map.getZoom();
var currentDataset = 'world'; var currentDataset = 'world';
@ -89,9 +156,16 @@
}; };
info.update = function (props) { info.update = function (props) {
this._div.innerHTML = '<h4>Population Density</h4>' + (props ? // console.log(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>');
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); info.addTo(map);
@ -271,10 +345,88 @@
} }
} }
} }
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>
<!--
<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> </body>
</html> </html>

3
dev.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/sh
python3 -m http.server

3753
fb2000.json Normal file

File diff suppressed because it is too large Load Diff