added method to aggrerate by key

This commit is contained in:
javi 2013-08-01 12:04:32 +02:00
parent a113a2088f
commit 1add37713a

View File

@ -22,6 +22,35 @@
json.prototype = { json.prototype = {
//
// return the data aggregated by key:
// {
// key0: 12,
// key1: 32
// key2: 25
// }
//
aggregateByKey: function(rows) {
function getKeys(row) {
var HEADER_SIZE = 3;
var valuesCount = row.data[2];
var keys = {};
for (var s = 0; s < valuesCount; ++s) {
keys[row.data[HEADER_SIZE + s]] = row.data[HEADER_SIZE + valuesCount + s];
}
return keys;
}
var keys = {};
for (r = 0; r < rows.length; ++r) {
var rowKeys = getKeys(rows[r]);
for(var k in rowKeys) {
keys[k] = keys[k] || 0;
keys[k] += rowKeys[k];
}
}
return keys;
},
/** /**
* *
*/ */
@ -131,6 +160,26 @@
return this.options.url; return this.options.url;
}, },
getTile: function(coord, zoom, callback) {
var template = this.url();
template = template
.replace('{x}', coord.x)
.replace('{y}', coord.y)
.replace('{z}', zoom);
var self = this;
var fetchTime = Profiler.metric('jsonarray:fetch time');
fetchTime.start();
torque.net.get(template, function (data) {
fetchTime.end();
if(data) {
data = JSON.parse(data.responseText);
}
callback(data);
});
},
/** /**
* `coord` object like {x : tilex, y: tiley } * `coord` object like {x : tilex, y: tiley }
* `zoom` quadtree zoom level * `zoom` quadtree zoom level