568 lines
17 KiB
HTML
568 lines
17 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta http-equiv="X-UA-Compatible" content="chrome=1">
|
|
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" type="text/css">
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" type="text/css">
|
|
<link rel="stylesheet" href="./resources/prism/prism.css" type="text/css">
|
|
<link rel="stylesheet" href="../css/ol.css" type="text/css">
|
|
<link rel="stylesheet" href="./resources/layout.css" type="text/css">
|
|
<script src="http://d3js.org/d3.v3.min.js"></script>
|
|
<link rel="stylesheet" href="raster.css">
|
|
<link rel="stylesheet" href="raster.css">
|
|
<script src="./resources/zeroclipboard/ZeroClipboard.min.js"></script>
|
|
<title>Raster Source</title>
|
|
</head>
|
|
<body>
|
|
|
|
<header class="navbar" role="navigation">
|
|
<div class="container" id="navbar-inner-container">
|
|
<a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png"> OpenLayers 3 Examples</a>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="container-fluid">
|
|
|
|
<div class="row-fluid">
|
|
<div class="span12 rel">
|
|
<div id="map" class="map"></div>
|
|
<div id="plot"></div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="row-fluid">
|
|
<div class="span12">
|
|
<h4 id="title">Raster Source</h4>
|
|
<p id="shortdesc">Demonstrates pixelwise operations with a raster source.</p>
|
|
<div id="docs"><p>
|
|
This example uses a <code>ol.source.Raster</code> to generate data
|
|
based on another source. The raster source accepts any number of
|
|
input sources (tile or image based) and runs a pipeline of
|
|
operations on the input pixels. The return from the final
|
|
operation is used as the data for the output source.
|
|
</p> <p>
|
|
In this case, a single tiled source of imagery is used as input.
|
|
For each pixel, the Vegetation Greenness Index
|
|
(<a href="http://www.tandfonline.com/doi/abs/10.1080/10106040108542184#.Vb90ITBViko">VGI</a>)
|
|
is calculated from the input pixels. A second operation colors
|
|
those pixels based on a threshold value (values above the
|
|
threshold are green and those below are transparent).
|
|
</p>
|
|
</div>
|
|
<div id="tags">raster, pixel</div>
|
|
<div id="api-links">Related API documentation: <ul class="inline"><li><a href="../apidoc/ol.Map.html" title="API documentation for ol.Map">ol.Map</a></li>,<li><a href="../apidoc/ol.View.html" title="API documentation for ol.View">ol.View</a></li>,<li><a href="../apidoc/ol.layer.Image.html" title="API documentation for ol.layer.Image">ol.layer.Image</a></li>,<li><a href="../apidoc/ol.layer.Tile.html" title="API documentation for ol.layer.Tile">ol.layer.Tile</a></li>,<li><a href="../apidoc/ol.source.BingMaps.html" title="API documentation for ol.source.BingMaps">ol.source.BingMaps</a></li>,<li><a href="../apidoc/ol.source.Raster.html" title="API documentation for ol.source.Raster">ol.source.Raster</a></li></ul></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row-fluid">
|
|
<div id="source-controls">
|
|
<a id="copy-button"><i class="fa fa-clipboard"></i> Copy</a>
|
|
<a id="jsfiddle-button"><i class="fa fa-jsfiddle"></i> Edit</a>
|
|
</div>
|
|
<form method="POST" id="jsfiddle-form" target="_blank" action="http://jsfiddle.net/api/post/jquery/1.11.0/">
|
|
<textarea class="hidden" name="js">// NOCOMPILE
|
|
// this example uses d3 for which we don't have an externs file.
|
|
var minVgi = 0;
|
|
var maxVgi = 0.25;
|
|
var bins = 10;
|
|
|
|
|
|
/**
|
|
* 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 g = pixel[1] / 255;
|
|
var b = pixel[2] / 255;
|
|
return (2 * g - r - b) / (2 * g + r + b);
|
|
}
|
|
|
|
|
|
/**
|
|
* Summarize values for a histogram.
|
|
* @param {numver} value A VGI value.
|
|
* @param {Object} counts An object for keeping track of VGI counts.
|
|
*/
|
|
function summarize(value, counts) {
|
|
var min = counts.min;
|
|
var max = counts.max;
|
|
var num = counts.values.length;
|
|
if (value < min) {
|
|
// do nothing
|
|
} else if (value >= max) {
|
|
counts.values[num - 1] += 1;
|
|
} else {
|
|
var index = Math.floor((value - min) / counts.delta);
|
|
counts.values[index] += 1;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Use aerial imagery as the input data for the raster source.
|
|
*/
|
|
var bing = new ol.source.BingMaps({
|
|
key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3',
|
|
imagerySet: 'Aerial'
|
|
});
|
|
|
|
|
|
/**
|
|
* Create a raster source where pixels with VGI values above a threshold will
|
|
* be colored green.
|
|
*/
|
|
var raster = new ol.source.Raster({
|
|
sources: [bing],
|
|
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', 0.1);
|
|
|
|
function createCounts(min, max, num) {
|
|
var values = new Array(num);
|
|
for (var i = 0; i < num; ++i) {
|
|
values[i] = 0;
|
|
}
|
|
return {
|
|
min: min,
|
|
max: max,
|
|
values: values,
|
|
delta: (max - min) / num
|
|
};
|
|
}
|
|
|
|
raster.on('beforeoperations', function(event) {
|
|
event.data.counts = createCounts(minVgi, maxVgi, bins);
|
|
event.data.threshold = raster.get('threshold');
|
|
});
|
|
|
|
raster.on('afteroperations', function(event) {
|
|
schedulePlot(event.resolution, event.data.counts, event.data.threshold);
|
|
});
|
|
|
|
var map = new ol.Map({
|
|
layers: [
|
|
new ol.layer.Tile({
|
|
source: bing
|
|
}),
|
|
new ol.layer.Image({
|
|
source: raster
|
|
})
|
|
],
|
|
target: 'map',
|
|
view: new ol.View({
|
|
center: [-9651695, 4937351],
|
|
zoom: 13,
|
|
minZoom: 12,
|
|
maxZoom: 19
|
|
})
|
|
});
|
|
|
|
|
|
var timer = null;
|
|
function schedulePlot(resolution, counts, threshold) {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
timer = null;
|
|
}
|
|
timer = setTimeout(plot.bind(null, resolution, counts, threshold), 1000 / 60);
|
|
}
|
|
|
|
var barWidth = 15;
|
|
var plotHeight = 150;
|
|
var chart = d3.select('#plot').append('svg')
|
|
.attr('width', barWidth * bins)
|
|
.attr('height', plotHeight);
|
|
|
|
var chartRect = chart[0][0].getBoundingClientRect();
|
|
|
|
var tip = d3.select(document.body).append('div')
|
|
.attr('class', 'tip');
|
|
|
|
function plot(resolution, counts, threshold) {
|
|
var yScale = d3.scale.linear()
|
|
.domain([0, d3.max(counts.values)])
|
|
.range([0, plotHeight]);
|
|
|
|
var bar = chart.selectAll('rect').data(counts.values);
|
|
|
|
bar.enter().append('rect');
|
|
|
|
bar.attr('class', function(count, index) {
|
|
var value = counts.min + (index * counts.delta);
|
|
return 'bar' + (value >= threshold ? ' selected' : '');
|
|
})
|
|
.attr('width', barWidth - 2);
|
|
|
|
bar.transition().attr('transform', function(value, index) {
|
|
return 'translate(' + (index * barWidth) + ', ' +
|
|
(plotHeight - yScale(value)) + ')';
|
|
})
|
|
.attr('height', yScale);
|
|
|
|
bar.on('mousemove', function(count, index) {
|
|
var threshold = counts.min + (index * counts.delta);
|
|
if (raster.get('threshold') !== threshold) {
|
|
raster.set('threshold', threshold);
|
|
raster.changed();
|
|
}
|
|
});
|
|
|
|
bar.on('mouseover', function(count, index) {
|
|
var area = 0;
|
|
for (var i = counts.values.length - 1; i >= index; --i) {
|
|
area += resolution * resolution * counts.values[i];
|
|
}
|
|
tip.html(message(counts.min + (index * counts.delta), area));
|
|
tip.style('display', 'block');
|
|
tip.transition().style({
|
|
left: (chartRect.left + (index * barWidth) + (barWidth / 2)) + 'px',
|
|
top: (d3.event.y - 60) + 'px',
|
|
opacity: 1
|
|
});
|
|
});
|
|
|
|
bar.on('mouseout', function() {
|
|
tip.transition().style('opacity', 0).each('end', function() {
|
|
tip.style('display', 'none');
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
function message(value, area) {
|
|
var acres = (area / 4046.86).toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
return acres + ' acres at<br>' + value.toFixed(2) + ' VGI or above';
|
|
}
|
|
</textarea>
|
|
<textarea class="hidden" name="css">.rel {
|
|
position: relative
|
|
}
|
|
|
|
#plot {
|
|
pointer-events: none;
|
|
position: absolute;
|
|
bottom: 10px;
|
|
left: 10px;
|
|
}
|
|
|
|
.bar {
|
|
pointer-events: auto;
|
|
fill: #AFAFB9;
|
|
}
|
|
|
|
.bar.selected {
|
|
fill: green;
|
|
}
|
|
|
|
.tip {
|
|
position: absolute;
|
|
background: black;
|
|
color: white;
|
|
padding: 6px;
|
|
font-size: 12px;
|
|
border-radius: 4px;
|
|
margin-bottom: 10px;
|
|
display: none;
|
|
opacity: 0;
|
|
}
|
|
</textarea>
|
|
<textarea class="hidden" name="html"><div class="row-fluid">
|
|
<div class="span12 rel">
|
|
<div id="map" class="map"></div>
|
|
<div id="plot"></div>
|
|
</div>
|
|
</div>
|
|
</textarea>
|
|
<input type="hidden" name="wrap" value="l">
|
|
<input type="hidden" name="resources" value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css,https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js,http://openlayers.org/en/v3.9.0/css/ol.css,http://openlayers.org/en/v3.9.0/build/ol.js">
|
|
</form>
|
|
<pre><code id="example-source" class="language-markup"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Raster Source</title>
|
|
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
|
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
|
<link rel="stylesheet" href="http://openlayers.org/en/v3.9.0/css/ol.css" type="text/css">
|
|
<script src="http://openlayers.org/en/v3.9.0/build/ol.js"></script>
|
|
<script src="http://d3js.org/d3.v3.min.js"></script>
|
|
<link rel="stylesheet" href="raster.css">
|
|
<style>
|
|
.rel {
|
|
position: relative
|
|
}
|
|
|
|
#plot {
|
|
pointer-events: none;
|
|
position: absolute;
|
|
bottom: 10px;
|
|
left: 10px;
|
|
}
|
|
|
|
.bar {
|
|
pointer-events: auto;
|
|
fill: #AFAFB9;
|
|
}
|
|
|
|
.bar.selected {
|
|
fill: green;
|
|
}
|
|
|
|
.tip {
|
|
position: absolute;
|
|
background: black;
|
|
color: white;
|
|
padding: 6px;
|
|
font-size: 12px;
|
|
border-radius: 4px;
|
|
margin-bottom: 10px;
|
|
display: none;
|
|
opacity: 0;
|
|
}
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container-fluid">
|
|
|
|
<div class="row-fluid">
|
|
<div class="span12 rel">
|
|
<div id="map" class="map"></div>
|
|
<div id="plot"></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
<script>
|
|
// NOCOMPILE
|
|
// this example uses d3 for which we don't have an externs file.
|
|
var minVgi = 0;
|
|
var maxVgi = 0.25;
|
|
var bins = 10;
|
|
|
|
|
|
/**
|
|
* 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 g = pixel[1] / 255;
|
|
var b = pixel[2] / 255;
|
|
return (2 * g - r - b) / (2 * g + r + b);
|
|
}
|
|
|
|
|
|
/**
|
|
* Summarize values for a histogram.
|
|
* @param {numver} value A VGI value.
|
|
* @param {Object} counts An object for keeping track of VGI counts.
|
|
*/
|
|
function summarize(value, counts) {
|
|
var min = counts.min;
|
|
var max = counts.max;
|
|
var num = counts.values.length;
|
|
if (value < min) {
|
|
// do nothing
|
|
} else if (value >= max) {
|
|
counts.values[num - 1] += 1;
|
|
} else {
|
|
var index = Math.floor((value - min) / counts.delta);
|
|
counts.values[index] += 1;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Use aerial imagery as the input data for the raster source.
|
|
*/
|
|
var bing = new ol.source.BingMaps({
|
|
key: 'Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3',
|
|
imagerySet: 'Aerial'
|
|
});
|
|
|
|
|
|
/**
|
|
* Create a raster source where pixels with VGI values above a threshold will
|
|
* be colored green.
|
|
*/
|
|
var raster = new ol.source.Raster({
|
|
sources: [bing],
|
|
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', 0.1);
|
|
|
|
function createCounts(min, max, num) {
|
|
var values = new Array(num);
|
|
for (var i = 0; i < num; ++i) {
|
|
values[i] = 0;
|
|
}
|
|
return {
|
|
min: min,
|
|
max: max,
|
|
values: values,
|
|
delta: (max - min) / num
|
|
};
|
|
}
|
|
|
|
raster.on('beforeoperations', function(event) {
|
|
event.data.counts = createCounts(minVgi, maxVgi, bins);
|
|
event.data.threshold = raster.get('threshold');
|
|
});
|
|
|
|
raster.on('afteroperations', function(event) {
|
|
schedulePlot(event.resolution, event.data.counts, event.data.threshold);
|
|
});
|
|
|
|
var map = new ol.Map({
|
|
layers: [
|
|
new ol.layer.Tile({
|
|
source: bing
|
|
}),
|
|
new ol.layer.Image({
|
|
source: raster
|
|
})
|
|
],
|
|
target: 'map',
|
|
view: new ol.View({
|
|
center: [-9651695, 4937351],
|
|
zoom: 13,
|
|
minZoom: 12,
|
|
maxZoom: 19
|
|
})
|
|
});
|
|
|
|
|
|
var timer = null;
|
|
function schedulePlot(resolution, counts, threshold) {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
timer = null;
|
|
}
|
|
timer = setTimeout(plot.bind(null, resolution, counts, threshold), 1000 / 60);
|
|
}
|
|
|
|
var barWidth = 15;
|
|
var plotHeight = 150;
|
|
var chart = d3.select('#plot').append('svg')
|
|
.attr('width', barWidth * bins)
|
|
.attr('height', plotHeight);
|
|
|
|
var chartRect = chart[0][0].getBoundingClientRect();
|
|
|
|
var tip = d3.select(document.body).append('div')
|
|
.attr('class', 'tip');
|
|
|
|
function plot(resolution, counts, threshold) {
|
|
var yScale = d3.scale.linear()
|
|
.domain([0, d3.max(counts.values)])
|
|
.range([0, plotHeight]);
|
|
|
|
var bar = chart.selectAll('rect').data(counts.values);
|
|
|
|
bar.enter().append('rect');
|
|
|
|
bar.attr('class', function(count, index) {
|
|
var value = counts.min + (index * counts.delta);
|
|
return 'bar' + (value >= threshold ? ' selected' : '');
|
|
})
|
|
.attr('width', barWidth - 2);
|
|
|
|
bar.transition().attr('transform', function(value, index) {
|
|
return 'translate(' + (index * barWidth) + ', ' +
|
|
(plotHeight - yScale(value)) + ')';
|
|
})
|
|
.attr('height', yScale);
|
|
|
|
bar.on('mousemove', function(count, index) {
|
|
var threshold = counts.min + (index * counts.delta);
|
|
if (raster.get('threshold') !== threshold) {
|
|
raster.set('threshold', threshold);
|
|
raster.changed();
|
|
}
|
|
});
|
|
|
|
bar.on('mouseover', function(count, index) {
|
|
var area = 0;
|
|
for (var i = counts.values.length - 1; i >= index; --i) {
|
|
area += resolution * resolution * counts.values[i];
|
|
}
|
|
tip.html(message(counts.min + (index * counts.delta), area));
|
|
tip.style('display', 'block');
|
|
tip.transition().style({
|
|
left: (chartRect.left + (index * barWidth) + (barWidth / 2)) + 'px',
|
|
top: (d3.event.y - 60) + 'px',
|
|
opacity: 1
|
|
});
|
|
});
|
|
|
|
bar.on('mouseout', function() {
|
|
tip.transition().style('opacity', 0).each('end', function() {
|
|
tip.style('display', 'none');
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
function message(value, area) {
|
|
var acres = (area / 4046.86).toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
return acres + ' acres at<br>' + value.toFixed(2) + ' VGI or above';
|
|
}
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
|
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
|
<script src="./resources/common.js"></script>
|
|
<script src="./resources/prism/prism.min.js"></script>
|
|
<script src="loader.js?id=raster"></script>
|
|
|
|
</body>
|
|
</html>
|