Simplify raster sources by working with a single operation

This commit is contained in:
Tim Schaub
2015-08-03 09:31:17 -06:00
parent f2f5cd2630
commit 860fdabd76
9 changed files with 138 additions and 113 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ docs: >
</p> </p>
<p> <p>
In this case, a single tiled source of imagery is used as input. In this case, a single tiled source of imagery is used as input.
For each pixel, the Triangular Greenness Index For each pixel, the Vegetaion Greenness Index
(<a href="http://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=2161&context=usdaarsfacpub">TGI</a>) (<a href="http://www.tandfonline.com/doi/abs/10.1080/10106040108542184#.Vb90ITBViko">VGI</a>)
is calculated from the input pixels. A second operation colors is calculated from the input pixels. A second operation colors
those pixels based on a threshold value (values above the those pixels based on a threshold value (values above the
threshold are green and those below are transparent). threshold are green and those below are transparent).
+76 -51
View File
@@ -7,68 +7,96 @@ 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');
var minTgi = 0; var minVgi = 0;
var maxTgi = 25; var maxVgi = 0.25;
var bins = 10;
function tgi(pixels, data) {
var pixel = pixels[0]; /**
* Calculate the Vegetation Greenness Index (VGI) from an input pixel. This
* is a rough estimate assuming that pixel values correspond to reflectance.
* @param {ol.raster.Pixel} pixel An array of [R, G, B, A] values.
* @return {number} The VGI value for the given pixel.
*/
function vgi(pixel) {
var r = pixel[0] / 255; var r = pixel[0] / 255;
var g = pixel[1] / 255; var g = pixel[1] / 255;
var b = pixel[2] / 255; var b = pixel[2] / 255;
var value = (120 * (r - b) - (190 * (r - g))) / 2; return (2 * g - r - b) / (2 * g + r + b);
pixel[0] = value;
return pixels;
} }
function summarize(pixels, data) {
var value = Math.floor(pixels[0][0]);
var counts = data.counts;
if (value >= counts.min && value < counts.max) {
counts.values[value - counts.min] += 1;
}
return pixels;
}
function color(pixels, data) { /**
var pixel = pixels[0]; * Summarize values for a histogram.
var value = pixel[0]; * @param {numver} value A VGI value.
if (value > data.threshold) { * @param {Object} counts An object for keeping track of VGI counts.
pixel[0] = 0; */
pixel[1] = 255; function summarize(value, counts) {
pixel[2] = 0; var min = counts.min;
pixel[3] = 128; var max = counts.max;
var num = counts.values.length;
if (value < min) {
// do nothing
} else if (value >= max) {
counts.values[num - 1] += 1;
} else { } else {
pixel[3] = 0; var index = Math.floor((value - min) / counts.delta);
counts.values[index] += 1;
} }
return pixels;
} }
/**
* Use aerial imagery as the input data for the raster source.
*/
var bing = new ol.source.BingMaps({ var bing = new ol.source.BingMaps({
key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3', key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3',
imagerySet: 'Aerial' imagerySet: 'Aerial'
}); });
/**
* Create a raster source where pixels with VGI values above a threshold will
* be colored green.
*/
var raster = new ol.source.Raster({ var raster = new ol.source.Raster({
sources: [bing], sources: [bing],
operations: [tgi, summarize, color] operation: function(pixels, data) {
var pixel = pixels[0];
var value = vgi(pixel);
summarize(value, data.counts);
if (value >= data.threshold) {
pixel[0] = 0;
pixel[1] = 255;
pixel[2] = 0;
pixel[3] = 128;
} else {
pixel[3] = 0;
}
return pixel;
},
lib: {
vgi: vgi,
summarize: summarize
}
}); });
raster.set('threshold', 10); raster.set('threshold', 0.1);
function createCounts(min, max) { function createCounts(min, max, num) {
var len = max - min; var values = new Array(num);
var values = new Array(len); for (var i = 0; i < num; ++i) {
for (var i = 0; i < len; ++i) {
values[i] = 0; values[i] = 0;
} }
return { return {
min: min, min: min,
max: max, max: max,
values: values values: values,
delta: (max - min) / num
}; };
} }
raster.on('beforeoperations', function(event) { raster.on('beforeoperations', function(event) {
event.data.counts = createCounts(minTgi, maxTgi); event.data.counts = createCounts(minVgi, maxVgi, bins);
event.data.threshold = raster.get('threshold'); event.data.threshold = raster.get('threshold');
}); });
@@ -107,7 +135,7 @@ function schedulePlot(resolution, counts, threshold) {
var barWidth = 15; var barWidth = 15;
var plotHeight = 150; var plotHeight = 150;
var chart = d3.select('#plot').append('svg') var chart = d3.select('#plot').append('svg')
.attr('width', barWidth * (maxTgi - minTgi)) .attr('width', barWidth * bins)
.attr('height', plotHeight); .attr('height', plotHeight);
var chartRect = chart[0][0].getBoundingClientRect(); var chartRect = chart[0][0].getBoundingClientRect();
@@ -124,35 +152,32 @@ function plot(resolution, counts, threshold) {
bar.enter().append('rect'); bar.enter().append('rect');
bar.attr('class', function(value, index) { bar.attr('class', function(count, index) {
return 'bar' + (index - counts.min >= threshold ? ' selected' : ''); var value = counts.min + (index * counts.delta);
return 'bar' + (value >= threshold ? ' selected' : '');
}) })
.attr('width', barWidth - 2); .attr('width', barWidth - 2);
bar.transition() bar.transition().attr('transform', function(value, index) {
.attr('transform', function(value, index) { return 'translate(' + (index * barWidth) + ', ' +
return 'translate(' + (index * barWidth) + ', ' + (plotHeight - yScale(value)) + ')';
(plotHeight - yScale(value)) + ')'; })
}) .attr('height', yScale);
.attr('height', yScale);
bar.on('mousemove', function() { bar.on('mousemove', function(count, index) {
var old = threshold; var threshold = counts.min + (index * counts.delta);
threshold = counts.min + if (raster.get('threshold') !== threshold) {
Math.floor((d3.event.pageX - chartRect.left) / barWidth);
if (old !== threshold) {
raster.set('threshold', threshold); raster.set('threshold', threshold);
raster.changed(); raster.changed();
} }
}); });
bar.on('mouseover', function() { bar.on('mouseover', function(count, index) {
var index = Math.floor((d3.event.pageX - chartRect.left) / barWidth);
var area = 0; var area = 0;
for (var i = counts.values.length - 1; i >= index; --i) { for (var i = counts.values.length - 1; i >= index; --i) {
area += resolution * resolution * counts.values[i]; area += resolution * resolution * counts.values[i];
} }
tip.html(message(index + counts.min, area)); tip.html(message(counts.min + (index * counts.delta), area));
tip.style('display', 'block'); tip.style('display', 'block');
tip.transition().style({ tip.transition().style({
left: (chartRect.left + (index * barWidth) + (barWidth / 2)) + 'px', left: (chartRect.left + (index * barWidth) + (barWidth / 2)) + 'px',
@@ -171,5 +196,5 @@ function plot(resolution, counts, threshold) {
function message(value, area) { function message(value, area) {
var acres = (area / 4046.86).toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ','); var acres = (area / 4046.86).toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return acres + ' acres at<br>' + value + ' TGI or above'; return acres + ' acres at<br>' + value.toFixed(2) + ' VGI or above';
} }
+1 -1
View File
@@ -27,7 +27,7 @@ tags: "raster, region growing"
--- ---
<div class="row-fluid"> <div class="row-fluid">
<div class="span12"> <div class="span12">
<div id="map" class="map"></div> <div id="map" class="map" style="cursor: pointer"></div>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
+7 -7
View File
@@ -12,7 +12,7 @@ function growRegion(inputs, data) {
var seed = data.pixel; var seed = data.pixel;
var delta = parseInt(data.delta); var delta = parseInt(data.delta);
if (!seed) { if (!seed) {
return [image]; return image;
} }
seed = seed.map(Math.round); seed = seed.map(Math.round);
@@ -59,7 +59,7 @@ function growRegion(inputs, data) {
} }
edge = newedge; edge = newedge;
} }
return [new ImageData(outputData, width, height)]; return new ImageData(outputData, width, height);
} }
function next4Edges(edge) { function next4Edges(edge) {
@@ -81,8 +81,8 @@ var imagery = new ol.layer.Tile({
var raster = new ol.source.Raster({ var raster = new ol.source.Raster({
sources: [imagery.getSource()], sources: [imagery.getSource()],
operationType: 'image', operationType: 'image',
operations: [growRegion], operation: growRegion,
// the contents of `lib` option will be available to the operation(s) run in // Functions in the `lib` object will be available to the operation run in
// the web worker. // the web worker.
lib: { lib: {
nextEdges: next4Edges nextEdges: next4Edges
@@ -90,7 +90,7 @@ var raster = new ol.source.Raster({
}); });
var rasterImage = new ol.layer.Image({ var rasterImage = new ol.layer.Image({
opacity: 0.8, opacity: 0.7,
source: raster source: raster
}); });
@@ -98,8 +98,8 @@ var map = new ol.Map({
layers: [imagery, rasterImage], layers: [imagery, rasterImage],
target: 'map', target: 'map',
view: new ol.View({ view: new ol.View({
center: ol.proj.fromLonLat([-120, 50]), center: ol.proj.fromLonLat([-119.07, 47.65]),
zoom: 6 zoom: 11
}) })
}); });
+3 -3
View File
@@ -81,7 +81,7 @@ function shade(inputs, data) {
aspect = Math.atan2(dzdy, -dzdx); aspect = Math.atan2(dzdy, -dzdx);
if (aspect < 0) { if (aspect < 0) {
aspect = halfPi - aspect; aspect = halfPi - aspect;
} else if (aspect > Math.PI / 2) { } else if (aspect > halfPi) {
aspect = twoPi - aspect + halfPi; aspect = twoPi - aspect + halfPi;
} else { } else {
aspect = halfPi - aspect; aspect = halfPi - aspect;
@@ -99,7 +99,7 @@ function shade(inputs, data) {
} }
} }
return [new ImageData(shadeData, width, height)]; return new ImageData(shadeData, width, height);
} }
var elevation = new ol.source.XYZ({ var elevation = new ol.source.XYZ({
@@ -110,7 +110,7 @@ var elevation = new ol.source.XYZ({
var raster = new ol.source.Raster({ var raster = new ol.source.Raster({
sources: [elevation], sources: [elevation],
operationType: 'image', operationType: 'image',
operations: [shade] operation: shade
}); });
var map = new ol.Map({ var map = new ol.Map({
+5 -5
View File
@@ -4500,7 +4500,7 @@ olx.source.ImageVectorOptions.prototype.style;
/** /**
* @typedef {{sources: Array.<ol.source.Source>, * @typedef {{sources: Array.<ol.source.Source>,
* operations: (Array.<ol.raster.Operation>|undefined), * operation: (ol.raster.Operation|undefined),
* lib: (Object|undefined), * lib: (Object|undefined),
* threads: (number|undefined), * threads: (number|undefined),
* operationType: (ol.raster.OperationType|undefined)}} * operationType: (ol.raster.OperationType|undefined)}}
@@ -4518,12 +4518,12 @@ olx.source.RasterOptions.prototype.sources;
/** /**
* Pixel operations. Operations will be called with data from input sources * Raster operation. The operation will be called with data from input sources
* and the final output will be assigned to the raster source. * and the output will be assigned to the raster source.
* @type {Array.<ol.raster.Operation>|undefined} * @type {ol.raster.Operation|undefined}
* @api * @api
*/ */
olx.source.RasterOptions.prototype.operations; olx.source.RasterOptions.prototype.operation;
/** /**
+1 -1
View File
@@ -38,7 +38,7 @@
"metalsmith": "1.6.0", "metalsmith": "1.6.0",
"metalsmith-templates": "0.7.0", "metalsmith-templates": "0.7.0",
"nomnom": "1.8.0", "nomnom": "1.8.0",
"pixelworks": "^0.11.0", "pixelworks": "1.0.0",
"rbush": "1.3.5", "rbush": "1.3.5",
"temp": "0.8.1", "temp": "0.8.1",
"walk": "2.3.4", "walk": "2.3.4",
+10 -10
View File
@@ -46,8 +46,8 @@ ol.source.Raster = function(options) {
this.operationType_ = goog.isDef(options.operationType) ? this.operationType_ = goog.isDef(options.operationType) ?
options.operationType : ol.raster.OperationType.PIXEL; options.operationType : ol.raster.OperationType.PIXEL;
var operations = goog.isDef(options.operations) ? var operation = goog.isDef(options.operation) ?
options.operations : [ol.raster.IdentityOp]; options.operation : ol.raster.IdentityOp;
/** /**
* @private * @private
@@ -59,7 +59,7 @@ ol.source.Raster = function(options) {
* @private * @private
* @type {*} * @type {*}
*/ */
this.worker_ = this.createWorker_(operations, options.lib, this.threads_); this.worker_ = this.createWorker_(operation, options.lib, this.threads_);
/** /**
* @private * @private
@@ -146,16 +146,16 @@ goog.inherits(ol.source.Raster, ol.source.Image);
/** /**
* Create a worker. * Create a worker.
* @param {Array.<ol.raster.Operation>} operations The operations. * @param {ol.raster.Operation} operation The operation.
* @param {Object=} opt_lib Optional lib functions. * @param {Object=} opt_lib Optional lib functions.
* @param {number=} opt_threads Number of threads. * @param {number=} opt_threads Number of threads.
* @return {*} The worker. * @return {*} The worker.
* @private * @private
*/ */
ol.source.Raster.prototype.createWorker_ = ol.source.Raster.prototype.createWorker_ =
function(operations, opt_lib, opt_threads) { function(operation, opt_lib, opt_threads) {
return new ol.ext.pixelworks.Processor({ return new ol.ext.pixelworks.Processor({
operations: operations, operation: operation,
imageOps: this.operationType_ === ol.raster.OperationType.IMAGE, imageOps: this.operationType_ === ol.raster.OperationType.IMAGE,
queue: 1, queue: 1,
lib: opt_lib, lib: opt_lib,
@@ -165,14 +165,14 @@ ol.source.Raster.prototype.createWorker_ =
/** /**
* Reset the operations. * Reset the operation.
* @param {Array.<ol.raster.Operation>} operations New operations. * @param {ol.raster.Operation} operation New operation.
* @param {Object=} opt_lib Functions that will be available to operations run * @param {Object=} opt_lib Functions that will be available to operations run
* in a worker. * in a worker.
* @api * @api
*/ */
ol.source.Raster.prototype.setOperations = function(operations, opt_lib) { ol.source.Raster.prototype.setOperation = function(operation, opt_lib) {
this.worker_ = this.createWorker_(operations, opt_lib, this.threads_); this.worker_ = this.createWorker_(operation, opt_lib, this.threads_);
this.changed(); this.changed();
}; };
+33 -33
View File
@@ -52,9 +52,9 @@ describe('ol.source.Raster', function() {
raster = new ol.source.Raster({ raster = new ol.source.Raster({
threads: 0, threads: 0,
sources: [redSource, greenSource, blueSource], sources: [redSource, greenSource, blueSource],
operations: [function(inputs) { operation: function(inputs) {
return inputs; return inputs[0];
}] }
}); });
map = new ol.Map({ map = new ol.Map({
@@ -91,17 +91,17 @@ describe('ol.source.Raster', function() {
expect(source).to.be.a(ol.source.Raster); expect(source).to.be.a(ol.source.Raster);
}); });
itNoPhantom('defaults to "pixel" operations', function(done) { itNoPhantom('defaults to "pixel" operation', function(done) {
var log = []; var log = [];
raster = new ol.source.Raster({ raster = new ol.source.Raster({
threads: 0, threads: 0,
sources: [redSource, greenSource, blueSource], sources: [redSource, greenSource, blueSource],
operations: [function(inputs) { operation: function(inputs) {
log.push(inputs); log.push(inputs);
return inputs; return inputs[0];
}] }
}); });
raster.on('afteroperations', function() { raster.on('afteroperations', function() {
@@ -127,10 +127,10 @@ describe('ol.source.Raster', function() {
operationType: ol.raster.OperationType.IMAGE, operationType: ol.raster.OperationType.IMAGE,
threads: 0, threads: 0,
sources: [redSource, greenSource, blueSource], sources: [redSource, greenSource, blueSource],
operations: [function(inputs) { operation: function(inputs) {
log.push(inputs); log.push(inputs);
return inputs; return inputs[0];
}] }
}); });
raster.on('afteroperations', function() { raster.on('afteroperations', function() {
@@ -149,12 +149,12 @@ describe('ol.source.Raster', function() {
}); });
describe('#setOperations()', function() { describe('#setOperation()', function() {
itNoPhantom('allows operations to be set', function(done) { itNoPhantom('allows operation to be set', function(done) {
var count = 0; var count = 0;
raster.setOperations([function(pixels) { raster.setOperation(function(pixels) {
++count; ++count;
var redPixel = pixels[0]; var redPixel = pixels[0];
var greenPixel = pixels[1]; var greenPixel = pixels[1];
@@ -162,8 +162,8 @@ describe('ol.source.Raster', function() {
expect(redPixel).to.eql([255, 0, 0, 255]); expect(redPixel).to.eql([255, 0, 0, 255]);
expect(greenPixel).to.eql([0, 255, 0, 255]); expect(greenPixel).to.eql([0, 255, 0, 255]);
expect(bluePixel).to.eql([0, 0, 255, 255]); expect(bluePixel).to.eql([0, 0, 255, 255]);
return pixels; return pixels[0];
}]); });
var view = map.getView(); var view = map.getView();
view.setCenter([0, 0]); view.setCenter([0, 0]);
@@ -176,7 +176,7 @@ describe('ol.source.Raster', function() {
}); });
itNoPhantom('updates and re-runs the operations', function(done) { itNoPhantom('updates and re-runs the operation', function(done) {
var view = map.getView(); var view = map.getView();
view.setCenter([0, 0]); view.setCenter([0, 0]);
@@ -186,9 +186,9 @@ describe('ol.source.Raster', function() {
raster.on('afteroperations', function(event) { raster.on('afteroperations', function(event) {
++count; ++count;
if (count === 1) { if (count === 1) {
raster.setOperations([function(inputs) { raster.setOperation(function(inputs) {
return inputs; return inputs[0];
}]); });
} else { } else {
done(); done();
} }
@@ -203,10 +203,10 @@ describe('ol.source.Raster', function() {
itNoPhantom('gets called before operations are run', function(done) { itNoPhantom('gets called before operations are run', function(done) {
var count = 0; var count = 0;
raster.setOperations([function(inputs) { raster.setOperation(function(inputs) {
++count; ++count;
return inputs; return inputs[0];
}]); });
raster.on('beforeoperations', function(event) { raster.on('beforeoperations', function(event) {
expect(count).to.equal(0); expect(count).to.equal(0);
@@ -224,12 +224,12 @@ describe('ol.source.Raster', function() {
}); });
itNoPhantom('allows data to be set for the operations', function(done) { itNoPhantom('allows data to be set for the operation', function(done) {
raster.setOperations([function(inputs, data) { raster.setOperation(function(inputs, data) {
++data.count; ++data.count;
return inputs; return inputs[0];
}]); });
raster.on('beforeoperations', function(event) { raster.on('beforeoperations', function(event) {
event.data.count = 0; event.data.count = 0;
@@ -253,10 +253,10 @@ describe('ol.source.Raster', function() {
itNoPhantom('gets called after operations are run', function(done) { itNoPhantom('gets called after operations are run', function(done) {
var count = 0; var count = 0;
raster.setOperations([function(inputs) { raster.setOperation(function(inputs) {
++count; ++count;
return inputs; return inputs[0];
}]); });
raster.on('afteroperations', function(event) { raster.on('afteroperations', function(event) {
expect(count).to.equal(4); expect(count).to.equal(4);
@@ -273,12 +273,12 @@ describe('ol.source.Raster', function() {
}); });
itNoPhantom('receives data set by the operations', function(done) { itNoPhantom('receives data set by the operation', function(done) {
raster.setOperations([function(inputs, data) { raster.setOperation(function(inputs, data) {
data.message = 'hello world'; data.message = 'hello world';
return inputs; return inputs[0];
}]); });
raster.on('afteroperations', function(event) { raster.on('afteroperations', function(event) {
expect(event.data.message).to.equal('hello world'); expect(event.data.message).to.equal('hello world');