Merge pull request #225 from CartoDB/get_values

getValues function added
pull/224/merge
javi santana 9 years ago
commit 9fe8d9b7f3

@ -35,7 +35,7 @@ add-header:
node lib/header.js
prepare-test-suite:
browserify test/suite.js > test/suite-bundle.js
$(BROWSERIFY) test/suite.js > test/suite-bundle.js
test: prepare-test-suite
@echo "***tests***"
@ -43,7 +43,7 @@ test: prepare-test-suite
test-acceptance: clean-results
@echo "***acceptance***"
./node_modules/.bin/qunit -c lib/torque/ -t `find test/acceptance/ -name *.js`
./node_modules/.bin/qunit -c lib/torque/ -t `find test/acceptance/ -name "*.js"`
test-all: test test-acceptance

@ -105,6 +105,7 @@ torqueLayer.setSQL("SELECT * FROM table LIMIT 100");
| ```getValueForPos(x, y[, step])```| | an object like { bbox:[], value: VALUE } if there is value for the pos, null otherwise | allows to get the value for the coordinate (in map reference system) for a concrete step. If step is not specified the animation one is used. This method is expensive in terms of CPU so be careful. It returns the value from the raster data not the rendered data |
| ```getValueForBBox(xstart, ystart, xend, yend)```| | a number | returns an accumulated numerical value from all the torque areas within the specified bounds |
| ```getActivePointsBBox(step)```| | list of bbox | returns the list of bounding boxes active for ``step``
| ```getValues(step)```| | list of values| returns the list of values for the pixels active in ``step``
| ```invalidate()```| | | forces a reload of the layer data.
### Interaction methods (only available for Leaflet)

@ -178,6 +178,7 @@ GMapsTorqueLayer.prototype = torque.extend({},
// don't load tiles that are not being shown
if (t.zoom !== self.options.map.getZoom()) return;
self._tileLoaded(t, tileData);
self.fire('tileLoaded');
if (tileData) {
self.redraw();
}
@ -319,6 +320,20 @@ GMapsTorqueLayer.prototype = torque.extend({},
google.maps.event.removeListener(this._cacheListener);
},
/**
* return an array with the values for all the pixels active for the step
*/
getValues: function(step) {
var values = [];
step = step === undefined ? this.key: step;
var t, tile;
for(t in this._tiles) {
tile = this._tiles[t];
this.renderer.getValues(tile, step, values);
}
return values;
},
getValueForPos: function(x, y, step) {
step = step === undefined ? this.key: step;
var t, tile, pos, value = null, xx, yy;

@ -97,6 +97,7 @@ L.TorqueLayer = L.CanvasLayer.extend({
if (tileData) {
self.redraw();
}
self.fire('tileLoaded');
});
}, this);
@ -372,6 +373,20 @@ L.TorqueLayer = L.CanvasLayer.extend({
return positions;
},
/**
* return an array with the values for all the pixels active for the step
*/
getValues: function(step) {
var values = [];
step = step === undefined ? this.key: step;
var t, tile;
for(t in this._tiles) {
tile = this._tiles[t];
this.renderer.getValues(tile, step, values);
}
return values;
},
/**
* return the value for position relative to map coordinates. null for no value
*/

@ -313,6 +313,23 @@ var Filters = require('./torque_filters');
return positions;
},
/**
* returns an array with all the values for the active pixels
* @tile tile object
* @step integer with the step
* @values (optional) an array where the values will be placed
*/
getValues: function(tile, step, values) {
values = values || [];
var activePixels = tile.timeCount[step];
var pixelIndex = tile.timeIndex[step];
for(var p = 0; p < activePixels; ++p) {
var posIdx = tile.renderDataPos[pixelIndex + p];
values.push(tile.renderData[pixelIndex + p]);
}
return values;
},
// return the value for x, y (tile coordinates)
// null for no value
getValueFor: function(tile, step, px, py) {

@ -115,3 +115,21 @@ test('get value for position', function() {
v = renderer.getValueFor(tile, 0, 99, 255 - 3);
equal(v, null);
});
test('get values for tile', function() {
tile = {
timeCount: [2],
timeIndex: [0],
renderDataPos: [0, 0],
renderData: [5, 7],
x: [100],
y: [3],
coord: { x: 0, y: 0, z: 0 }
};
renderer.options = {
resolution: 1
};
v = renderer.getValues(tile, 0);
equal(v[0], 5);
equal(v[1], 7);
});

Loading…
Cancel
Save