Skip to content
Permalink
Browse files

improve log file parsing performance

it should be almost twice as fast now: expect around -45% less time
from dropping the file to rendered map. e.g. from ~45 seconds down
to 25 seconds on my machine.
  • Loading branch information...
tyrasd committed Sep 2, 2016
1 parent 16bc693 commit 5ff3bce2a1c68084a527ead3917f16c6413c88dc
Showing with 20 additions and 8 deletions.
  1. +2 −1 index.html
  2. +18 −7 index.js
@@ -113,13 +113,14 @@
reader = new FileReader();
reader.onload = function (event) {
console.log(event.target);
processing.className = 'done';
legend.className = 'done';
console.timeEnd("read file")
L.tileLayer.osmTileAccessLogLayer(event.target.result).addTo(map);
};
console.log(file);
console.time("read file")
reader.readAsText(file);
return false;
@@ -5,24 +5,35 @@ L.TileLayer.OsmTileAccessLogLayer = L.TileLayer.Canvas.extend({
data: null
},
initialize: function (data) {
console.log("Loaded data!")
console.time("parse data")
data = data.split("\n")
data.pop() // newline at the end
data = data.map(function(line) {
return line.split(/[ \/]/).map(Number)
line = line.split(' ')
var coords = line[0].split('/')
return {
x: +coords[1],
y: +coords[2],
zoom: +coords[0],
count: +line[1]
}
})
var tree = rbush(9, ['[1]', '[2]', '[1]', '[2]'])
console.timeEnd("parse data")
console.time("build indices")
var tree = rbush(9, ['.x', '.y', '.x', '.y'])
tree.load(data)
this.options.data = tree
console.log("Indices created!")
console.timeEnd("build indices")
},
drawTile: function(canvas, tilePoint, zoom) {
console.time("search data")
fData = this.options.data.search({
minX: tilePoint.x*256,
minY: tilePoint.y*256,
maxX: (tilePoint.x+1)*256-1,
maxY: (tilePoint.y+1)*256-1
}).filter(function(d) { return d[0] === zoom+8 })
}).filter(function(d) { return d.zoom === zoom+8 })
console.timeEnd("search data")

var ctx = canvas.getContext('2d');
// draw something on the tile canvas
@@ -34,12 +45,12 @@ L.TileLayer.OsmTileAccessLogLayer = L.TileLayer.Canvas.extend({
pixeldata[3] = 255

fData.forEach(function(d) {
var cat = Math.max(Math.floor(2*Math.log(d[3])/Math.log(10))-1,0)
var cat = Math.max(Math.floor(2*Math.log(d.count)/Math.log(10))-1,0)
cat = Math.min(cat, colorbrewer.length-1)
pixeldata[0] = colorbrewer[cat][0]
pixeldata[1] = colorbrewer[cat][1]
pixeldata[2] = colorbrewer[cat][2]
ctx.putImageData( pixel, d[1]%256, d[2]%256 )
ctx.putImageData( pixel, d.x%256, d.y%256 )
})
}
});

0 comments on commit 5ff3bce

Please sign in to comment.
You can’t perform that action at this time.