Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions lib/torque/provider/tilejson.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,39 @@
* Index: Array index to the properties
* }
*/
proccessTile: function(rows, coord, zoom) {
createProccessTileWorker:function(){
var workerFunction = "var proccessTile ="+ this.proccessTileSerial.toString()
var wrapper = "; self.onmessage = function(e){var data = JSON.parse(e.data); JSON.stringify(self.postMessage(proccessTile(data.rows,data.coord, data.zoom, data.options)))}"
var script = workerFunction + wrapper;
var blob = new Blob([script], {type: "text/javascript"})
var worker = new Worker(window.URL.createObjectURL(blob))
return worker
},
proccessTile:function(rows,coord,zoom,callback){
if(typeof(Worker) === "undefined"){
callback(this.proccessTileSerial(rows,coord,zoom, this.options))
}
else{
var worker = this.createProccessTileWorker()
worker.onmessage = function(e){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this, why the worker is terminated when recieves a message (maybe is because I don't actully know the webworkers api)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worker is only terminated when its done. worker.onmessage is triggered when the worker sends a message back to the main thread.

We could recycle the workers but that requires adding some pool logic which seems overkill just now

callback(e.data)
worker.terminate()
}

var workerSafeOptions= {
x : new this.options.coordinates_data_type(rows.length),
y : new this.options.coordinates_data_type(rows.length),
cumulative: this.options.cumulative,
valueDataType: this.options.valueDataType,
resolution: this.options.resolution,
}
worker.postMessage(JSON.stringify({rows: rows, coord: {x:coord.x,y:coord.y}, zoom:zoom, options: workerSafeOptions}))
}
},
proccessTileSerial: function(rows, coord, zoom,options) {
var r;
var x = new this.options.coordinates_data_type(rows.length);
var y = new this.options.coordinates_data_type(rows.length);
var x = options.x || new options.coordinates_data_type(rows.length);
var y = options.y || new options.coordinates_data_type(rows.length);

// count number of dates
var dates = 0;
Expand All @@ -64,32 +93,32 @@
}
}

if(this.options.cumulative) {
if(options.cumulative) {
dates = (1 + maxDateSlots) * rows.length;
}

var type = this.options.cumulative ? Uint32Array: Uint8ClampedArray;
var type = options.cumulative ? Uint32Array: Uint8ClampedArray;

// reserve memory for all the dates
var timeIndex = new Int32Array(maxDateSlots + 1); //index-size
var timeCount = new Int32Array(maxDateSlots + 1);
var renderData = new (this.options.valueDataType || type)(dates);
var renderData = new (options.valueDataType || type)(dates);
var renderDataPos = new Uint32Array(dates);

var rowsPerSlot = {};

// precache pixel positions
for (var r = 0; r < rows.length; ++r) {
var row = rows[r];
x[r] = row.x__uint8 * this.options.resolution;
y[r] = row.y__uint8 * this.options.resolution;
x[r] = row.x__uint8 * options.resolution;
y[r] = row.y__uint8 * options.resolution;

var dates = row.dates__uint16;
var vals = row.vals__uint8;
if (!this.options.cumulative) {
if (!options.cumulative) {
for (var j = 0, len = dates.length; j < len; ++j) {
var rr = rowsPerSlot[dates[j]] || (rowsPerSlot[dates[j]] = []);
if(this.options.cumulative) {
if(options.cumulative) {
vals[j] += prev_val;
}
prev_val = vals[j];
Expand Down Expand Up @@ -152,7 +181,7 @@
};
},

setSteps: function(steps, opt) {
setSteps: function(steps, opt) {
opt = opt || {};
if (this.options.steps !== steps) {
this.options.steps = steps;
Expand Down Expand Up @@ -257,7 +286,7 @@
torque.net.get( url , function (data) {
if (data && data.responseText) {
var rows = JSON.parse(data.responseText);
callback(self.proccessTile(rows, coord, zoom));
self.proccessTile(rows, coord, zoom,callback.bind(self));
} else {
callback(null);
}
Expand Down Expand Up @@ -314,6 +343,7 @@

_fetchMap: function(callback) {
var self = this;

torque.net.get(this.options.tileJSON, function (data) {
data = JSON.parse(data.response);
if (data) {
Expand All @@ -334,4 +364,4 @@
}
};

module.exports = tileJSON;
module.exports = tileJSON;
16 changes: 9 additions & 7 deletions test/support/point_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ function getTile(jsonRelPath, cartocss, z, x, y, step, callback) {

var canvas = new Canvas(256, 256);
var pointRenderer = new torque.renderer.Point(canvas, rendererOptions);
provider.proccessTile(rows, {x: x, y: y}, z, function(tile){
pointRenderer.renderTile(tile, step, function(err) {
if (err) {
return callback(err, null);
}
pointRenderer.applyFilters();
return callback(null, canvas);
});
}.bind(this));

pointRenderer.renderTile(provider.proccessTile(rows, {x: x, y: y}, z), step, function(err) {
if (err) {
return callback(err, null);
}
pointRenderer.applyFilters();
return callback(null, canvas);
});
}

module.exports = {
Expand Down