osmSpeedtestMapper/index.html
2021-04-27 20:52:18 +02:00

241 lines
5.4 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>FileListTest</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css" type="text/css">
<style>
.error {
color: red;
}
.success {
color: darkgreen;
}
.map {
height: 800px;
width: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
</head>
<body>
<h1>SpeedTest.net file mapper</h1>
<hr />
<form action="" method="post" onsubmit="return trigger(this);">
<input type="file" name="file" id="files" multiple /><br />
<input type="reset" value="clear"/><input type="submit" id="ok" value="ok"/>
</form>
<h3>Status:</h3>
<div id="statusbox" style="color: red; display: none;">
</div>
<div id="overlaytest">
</div>
<div id="map" class="map"></div>
<script>
//class defs
class Entry {
constructor(data) {
this.date = data[0];
this.conntype = data[1];
this.loc = [parseFloat(data[3]), parseFloat(data[2])]; // long, lat
this.speed = [parseFloat(data[4]), parseFloat(data[6])]; // dl, up
this.size = [parseInt(data[5]), parseInt(data[7])]; // dl, up
this.ping = parseInt(data[8]);
this.server = data[9];
this.intIP = data[10];
this.extIP = data[11];
this.url = data[12];
}
toHTML() {
return "Date: " + this.date + " type: " + this.conntype + " loc: " + this.loc + " speed: " + this.speed + " Ping: " + this.ping + " url: <a target=\"_blank\"href=\"" + this.url + "\">" + this.url + "</a><br />\n";
}
};
var statusBox = document.getElementById("statusbox");
var overlaytest = document.getElementById("overlaytest");
var test = "";
var zoomstart = 15;
var isInit = false;
var entryList = [];
//init map
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([0, 0]),
zoom: zoomstart
})
});
var highlightStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({color: 'green'})
})
});
var defaultStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({color: 'red'})
})
});
var highlighted = [];
map.on("pointermove", e => {
var newhighlighted = [];
//gather overlay information
var fids = [];
map.forEachFeatureAtPixel(e.pixel, feature => {
var fid = feature.getProperties().id;
fids.push(fid);
feature.setStyle(highlightStyle);
newhighlighted.push(feature);
})
if(newhighlighted.length > 0) {
//remove highlight style
highlighted.filter(h => newhighlighted.indexOf(h) == -1).forEach(h => h.setStyle(defaultStyle));
highlighted = newhighlighted;
}
// display information
if(fids.length > 0)
overlaytest.innerHTML = "";
for(var i = 0; i < fids.length; ++i) {
var entry = entryList[fids[i]];
console.log(entry);
//print entry
overlaytest.innerHTML += entry.toHTML();
}
});
function showError(error) {
statusBox.innerHTML += "<span class=\"error\"> Error: " + error + "</span><br />\n";
statusBox.style.display = "";
}
function showSuccess(success) {
statusBox.innerHTML += "<span class=\"success\"> Success: " + success + "</span><br />\n";
statusBox.style.display = "";
}
function clearStatus() {
statusBox.innerHTML = "";
statusBox.style.display = "none";
}
function addElement(data, features) {
var elem = new Entry(data);
var id = entryList.length;
entryList.push(elem);
var dot = ol.proj.fromLonLat(elem.loc);
if(!isInit) {
isInit = true;
map.setView(new ol.View({center: dot, zoom: zoomstart}));
console.log(dot); //console.log(data);
}
var feat = new ol.Feature({
geometry: new ol.geom.Point(dot),
type: 'point',
id: id
});
features.push(feat);
}
function loadedFile(text) {
var lines = text.split("\n");
var features = [];
for(var i = 1; i < lines.length; ++i) {
var line = lines[i];
if(line.length < 10) continue;
var date = line.substr(1, 17);
line = line.substr(20);
var lineSplit = line.split(",");
lineSplit = [date ,... lineSplit];
addElement(lineSplit, features);
}
updateMap(features);
}
function updateMap(features) {
var sourceLayer = new ol.source.Vector({
features
});
var vectorLayer = new ol.layer.Vector({
source: sourceLayer,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({color: 'red'})
})
})
});
map.addLayer(vectorLayer);
}
function loadFile(file) {
console.log("loadFile: " + file.name);
console.log(file);
var text = file.text();
text.then(t => loadedFile(t));
}
function trigger(form) {
console.log("OK");
clearStatus();
isInit = false;
//try to read the files
var filelist = document.getElementById("files").files;
if(filelist.length == 0) {
showError("No file selected");
return false;
}
var successC = 0;
for(var i = 0; i < filelist.length; ++i) {
var f = filelist[i];
if(f.type == "text/csv") {
loadFile(f);
successC ++;
} else {
showError("The file <i>" + f.name + "</i> is not in the csv format");
}
}
showSuccess("Loaded " + successC + " files");
return false;
}
</script>
</body>
</html>