Pass data object to operations

This commit is contained in:
Tim Schaub
2015-06-25 17:32:34 -06:00
parent 5267776627
commit 65fee5b7ac
4 changed files with 47 additions and 24 deletions
+15 -13
View File
@@ -7,7 +7,7 @@ goog.require('ol.layer.Tile');
goog.require('ol.source.BingMaps'); goog.require('ol.source.BingMaps');
goog.require('ol.source.Raster'); goog.require('ol.source.Raster');
function tgi(pixels) { function tgi(pixels, data) {
var pixel = pixels[0]; var pixel = pixels[0];
var r = pixel[0] / 255; var r = pixel[0] / 255;
var g = pixel[1] / 255; var g = pixel[1] / 255;
@@ -17,20 +17,17 @@ function tgi(pixels) {
return pixels; return pixels;
} }
var counts = new Counts(0, 25);
function summarize(pixels) { function summarize(pixels, data) {
var value = pixels[0][0]; var value = pixels[0][0];
counts.increment(value); data.counts.increment(value);
return pixels; return pixels;
} }
var threshold = 10; function color(pixels, data) {
function color(pixels) {
var pixel = pixels[0]; var pixel = pixels[0];
var value = pixel[0]; var value = pixel[0];
if (value > threshold) { if (value > data.threshold) {
pixel[0] = 0; pixel[0] = 0;
pixel[1] = 255; pixel[1] = 255;
pixel[2] = 0; pixel[2] = 0;
@@ -51,12 +48,17 @@ var raster = new ol.source.Raster({
operations: [tgi, summarize, color] operations: [tgi, summarize, color]
}); });
raster.on('beforeoperations', function() { var counts = new Counts(0, 25);
var threshold = 10;
raster.on('beforeoperations', function(event) {
counts.clear(); counts.clear();
event.data.counts = counts;
event.data.threshold = threshold;
}); });
raster.on('afteroperations', function(event) { raster.on('afteroperations', function(event) {
schedulePlot(event.resolution); schedulePlot(event.resolution, event.data.counts);
}); });
var map = new ol.Map({ var map = new ol.Map({
@@ -114,12 +116,12 @@ Counts.prototype.increment = function(value) {
}; };
var timer = null; var timer = null;
function schedulePlot(resolution) { function schedulePlot(resolution, counts) {
if (timer) { if (timer) {
clearTimeout(timer); clearTimeout(timer);
timer = null; timer = null;
} }
timer = setTimeout(plot.bind(null, resolution), 1000 / 60); timer = setTimeout(plot.bind(null, resolution, counts), 1000 / 60);
} }
var barWidth = 15; var barWidth = 15;
@@ -133,7 +135,7 @@ var chartRect = chart[0][0].getBoundingClientRect();
var tip = d3.select(document.body).append('div') var tip = d3.select(document.body).append('div')
.attr('class', 'tip'); .attr('class', 'tip');
function plot(resolution) { function plot(resolution, counts) {
var yScale = d3.scale.linear() var yScale = d3.scale.linear()
.domain([0, d3.max(counts.values)]) .domain([0, d3.max(counts.values)])
.range([0, plotHeight]); .range([0, plotHeight]);
+6
View File
@@ -284,6 +284,12 @@ oli.source.RasterEvent.prototype.extent;
oli.source.RasterEvent.prototype.resolution; oli.source.RasterEvent.prototype.resolution;
/**
* @type {Object}
*/
oli.source.RasterEvent.prototype.data;
/** /**
* @interface * @interface
*/ */
+5 -2
View File
@@ -21,9 +21,12 @@ ol.raster.OperationType = {
* return an array of the same. For `'image'` type operations, functions will * return an array of the same. For `'image'` type operations, functions will
* be called with an array of {@link ImageData * be called with an array of {@link ImageData
* https://developer.mozilla.org/en-US/docs/Web/API/ImageData} and should return * https://developer.mozilla.org/en-US/docs/Web/API/ImageData} and should return
* an array of the same. * an array of the same. The operations are called with a second "data"
* argument, which can be used for storage. The data object is accessible
* from raster events, where it can be initialized in "beforeoperations" and
* accessed again in "afteroperations."
* *
* @typedef {function((Array.<ol.raster.Pixel>|Array.<ImageData>)): * @typedef {function((Array.<ol.raster.Pixel>|Array.<ImageData>), Object):
* (Array.<ol.raster.Pixel>|Array.<ImageData>)} * (Array.<ol.raster.Pixel>|Array.<ImageData>)}
* @api * @api
*/ */
+21 -9
View File
@@ -223,8 +223,9 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
this.renderers_[i], frameState, frameState.layerStatesArray[i]); this.renderers_[i], frameState, frameState.layerStatesArray[i]);
} }
var data = {};
this.dispatchEvent(new ol.source.RasterEvent( this.dispatchEvent(new ol.source.RasterEvent(
ol.source.RasterEventType.BEFOREOPERATIONS, frameState)); ol.source.RasterEventType.BEFOREOPERATIONS, frameState, data));
var targetImageData = null; var targetImageData = null;
if (this.operationType_ === ol.raster.OperationType.PIXEL) { if (this.operationType_ === ol.raster.OperationType.PIXEL) {
@@ -242,20 +243,20 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
pixel[2] = source[j + 2]; pixel[2] = source[j + 2];
pixel[3] = source[j + 3]; pixel[3] = source[j + 3];
} }
pixel = this.runPixelOperations_(pixels)[0]; pixel = this.runPixelOperations_(pixels, data)[0];
target[j] = pixel[0]; target[j] = pixel[0];
target[j + 1] = pixel[1]; target[j + 1] = pixel[1];
target[j + 2] = pixel[2]; target[j + 2] = pixel[2];
target[j + 3] = pixel[3]; target[j + 3] = pixel[3];
} }
} else if (this.operationType_ === ol.raster.OperationType.IMAGE) { } else if (this.operationType_ === ol.raster.OperationType.IMAGE) {
targetImageData = this.runImageOperations_(imageDatas)[0]; targetImageData = this.runImageOperations_(imageDatas, data)[0];
} else { } else {
goog.asserts.fail('unsupported operation type: ' + this.operationType_); goog.asserts.fail('unsupported operation type: ' + this.operationType_);
} }
this.dispatchEvent(new ol.source.RasterEvent( this.dispatchEvent(new ol.source.RasterEvent(
ol.source.RasterEventType.AFTEROPERATIONS, frameState)); ol.source.RasterEventType.AFTEROPERATIONS, frameState, data));
context.putImageData(targetImageData, 0, 0); context.putImageData(targetImageData, 0, 0);
@@ -266,12 +267,13 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
/** /**
* Run pixel-wise operations to transform pixels. * Run pixel-wise operations to transform pixels.
* @param {Array.<ol.raster.Pixel>} pixels The input pixels. * @param {Array.<ol.raster.Pixel>} pixels The input pixels.
* @param {Object} data User storage.
* @return {Array.<ol.raster.Pixel>} The modified pixels. * @return {Array.<ol.raster.Pixel>} The modified pixels.
* @private * @private
*/ */
ol.source.Raster.prototype.runPixelOperations_ = function(pixels) { ol.source.Raster.prototype.runPixelOperations_ = function(pixels, data) {
for (var i = 0, ii = this.operations_.length; i < ii; ++i) { for (var i = 0, ii = this.operations_.length; i < ii; ++i) {
pixels = this.operations_[i](pixels); pixels = this.operations_[i](pixels, data);
} }
return pixels; return pixels;
}; };
@@ -280,12 +282,13 @@ ol.source.Raster.prototype.runPixelOperations_ = function(pixels) {
/** /**
* Run image operations. * Run image operations.
* @param {Array.<ImageData>} imageDatas The input image data. * @param {Array.<ImageData>} imageDatas The input image data.
* @param {Object} data User storage.
* @return {Array.<ImageData>} The output image data. * @return {Array.<ImageData>} The output image data.
* @private * @private
*/ */
ol.source.Raster.prototype.runImageOperations_ = function(imageDatas) { ol.source.Raster.prototype.runImageOperations_ = function(imageDatas, data) {
for (var i = 0, ii = this.operations_.length; i < ii; ++i) { for (var i = 0, ii = this.operations_.length; i < ii; ++i) {
imageDatas = this.operations_[i](imageDatas); imageDatas = this.operations_[i](imageDatas, data);
} }
return imageDatas; return imageDatas;
}; };
@@ -396,8 +399,9 @@ ol.source.Raster.createTileRenderer_ = function(source) {
* @implements {oli.source.RasterEvent} * @implements {oli.source.RasterEvent}
* @param {string} type Type. * @param {string} type Type.
* @param {olx.FrameState} frameState The frame state. * @param {olx.FrameState} frameState The frame state.
* @param {Object} data An object made available to operations.
*/ */
ol.source.RasterEvent = function(type, frameState) { ol.source.RasterEvent = function(type, frameState, data) {
goog.base(this, type); goog.base(this, type);
/** /**
@@ -414,6 +418,14 @@ ol.source.RasterEvent = function(type, frameState) {
*/ */
this.resolution = frameState.viewState.resolution / frameState.pixelRatio; this.resolution = frameState.viewState.resolution / frameState.pixelRatio;
/**
* An object made available to all operations. This can be used by operations
* as a storage object (e.g. for calculating statistics).
* @type {Object}
* @api
*/
this.data = data;
}; };
goog.inherits(ol.source.RasterEvent, goog.events.Event); goog.inherits(ol.source.RasterEvent, goog.events.Event);