Merge pull request #1497 from elemoine/vector-api-d3

[vector-api] d3 integration and vector image
This commit is contained in:
Éric Lemoine
2014-01-10 05:48:42 -08:00
21 changed files with 833 additions and 190 deletions

59
examples/d3.html Normal file
View File

@@ -0,0 +1,59 @@
<!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="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>d3 integration example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./">OpenLayers 3 Examples</a>
<ul class="nav pull-right">
<li><iframe class="github-watch-button" src="http://ghbtns.com/github-btn.html?user=openlayers&repo=ol3&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" height="20" width="90"></iframe></li>
<li><a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-hashtags="openlayers">&nbsp;</a></li>
<li><div class="g-plusone-wrapper"><div class="g-plusone" data-size="medium" data-annotation="none"></div></div></li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">d3 integration example</h4>
<p id="shortdesc">Example of using ol3 and d3 together.</p>
<div id="docs">
<p>The example loads TopoJSON geometries and uses d3 (<code>d3.geo.path</code>) to render these geometries to a canvas element that is then used as the image of an ol3 image layer.</p>
<p>See the <a href="d3.js" target="_blank">d3.js source</a> to see how this is done.</p>
</div>
<div id="tags">d3</div>
</div>
</div>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="loader.js?id=d3" type="text/javascript"></script>
<script src="../resources/social-links.js" type="text/javascript"></script>
</body>
</html>

97
examples/d3.js vendored Normal file
View File

@@ -0,0 +1,97 @@
// NOCOMPILE
// this example uses d3 for which we don't have an externs file.
goog.require('ol');
goog.require('ol.Map');
goog.require('ol.View2D');
goog.require('ol.extent');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.proj');
goog.require('ol.source.ImageCanvas');
goog.require('ol.source.Stamen');
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.Stamen({
layer: 'watercolor'
})
})
],
renderers: ['canvas'],
target: 'map',
view: new ol.View2D({
center: ol.proj.transform([-97, 38], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
/**
* Load the topojson data and create an ol.layer.Image for that data.
*/
d3.json('data/us.json', function(error, us) {
var features = topojson.feature(us, us.objects.counties);
/**
* This function uses d3 to render the topojson features to a canvas.
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.Size} size Size.
* @param {ol.proj.Projection} projection Projection.
* @return {HTMLCanvasElement}
*/
var canvasFunction = function(extent, resolution, pixelRatio,
size, projection) {
var canvasWidth = size[0];
var canvasHeight = size[1];
var canvas = d3.select(document.createElement('canvas'));
canvas.attr('width', canvasWidth).attr('height', canvasHeight);
var context = canvas.node().getContext('2d');
var d3Projection = d3.geo.mercator().scale(1).translate([0, 0]);
var d3Path = d3.geo.path().projection(d3Projection);
var pixelBounds = d3Path.bounds(features);
var pixelBoundsWidth = pixelBounds[1][0] - pixelBounds[0][0];
var pixelBoundsHeight = pixelBounds[1][1] - pixelBounds[0][1];
var geoBounds = d3.geo.bounds(features);
var geoBoundsLeftBottom = ol.proj.transform(
geoBounds[0], 'EPSG:4326', projection);
var geoBoundsRightTop = ol.proj.transform(
geoBounds[1], 'EPSG:4326', projection);
var geoBoundsWidth = geoBoundsRightTop[0] - geoBoundsLeftBottom[0];
if (geoBoundsWidth < 0) {
geoBoundsWidth += ol.extent.getWidth(projection.getExtent());
}
var geoBoundsHeight = geoBoundsRightTop[1] - geoBoundsLeftBottom[1];
var widthResolution = geoBoundsWidth / pixelBoundsWidth;
var heightResolution = geoBoundsHeight / pixelBoundsHeight;
var r = Math.max(widthResolution, heightResolution);
var scale = r / (resolution / pixelRatio);
var center = ol.proj.transform(ol.extent.getCenter(extent),
projection, 'EPSG:4326');
d3Projection.scale(scale).center(center)
.translate([canvasWidth / 2, canvasHeight / 2]);
d3Path = d3Path.projection(d3Projection).context(context);
d3Path(features);
context.stroke();
return canvas[0][0];
};
var layer = new ol.layer.Image({
source: new ol.source.ImageCanvas({
canvasFunction: canvasFunction,
projection: 'EPSG:3857'
})
});
map.addLayer(layer);
});

1
examples/data/us.json Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,53 @@
<!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="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>Image vector layer example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Image vector example</h4>
<p id="shortdesc">Example of an image vector layer.</p>
<div id="docs">
<p>This example uses a <code>ol.source.ImageVector</code> source. That source gets vector features from the
<code>ol.source.Vector</code> it's configured with and draw these features to an HTML5 canvas element that
is then used as the image of an image layer.</p>
<p>See the <a href="image-vector-layer.js" target="_blank">image-vector-layer.js source</a> to see how this is done.</p>
</div>
<div id="tags">vector, image</div>
</div>
</div>
</div>
<script src="loader.js?id=image-vector-layer" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
</body>
</html>

View File

@@ -0,0 +1,46 @@
goog.require('ol.Map');
goog.require('ol.RendererHints');
goog.require('ol.View2D');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.ImageVector');
goog.require('ol.source.MapQuest');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
var styleArray = [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.6)'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
})
})];
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
}),
new ol.layer.Image({
source: new ol.source.ImageVector({
source: new ol.source.GeoJSON({
url: 'data/countries.geojson'
}),
styleFunction: function(feature, resolution) {
return styleArray;
}
})
})
],
renderers: ol.RendererHints.createFromQueryData(),
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 1
})
});

View File

@@ -623,6 +623,35 @@
* @todo stability experimental * @todo stability experimental
*/ */
/**
* @typedef {Object} olx.source.ImageCanvasOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {ol.CanvasFunctionType} canvasFunction Canvas function.
* @property {ol.Extent|undefined} extent Extent.
* @property {string|undefined} logo Logo.
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {number|undefined} ratio Ratio. 1 means canvases are the size
* of the map viewport, 2 means twice the size of the map viewport, and so
* on.
* @property {Array.<number>|undefined} resolutions Resolutions. If specified,
* new canvases will be created for these resolutions only.
* @property {ol.source.State|undefined} state Source state.
*/
/**
* @typedef {Object} olx.source.ImageVectorOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {ol.Extent|undefined} extent Extent.
* @property {string|undefined} logo Logo.
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {number|undefined} ratio Ratio. 1 means canvases are the size
* of the map viewport, 2 means twice the size of the map viewport, and so
* on.
* @property {Array.<number>|undefined} resolutions Resolutions. If specified,
* new canvases will be created for these resolutions only.
* @property {ol.source.Vector} source Vector source.
*/
/** /**
* @typedef {Object} olx.source.ImageWMSOptions * @typedef {Object} olx.source.ImageWMSOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions. * @property {Array.<ol.Attribution>|undefined} attributions Attributions.
@@ -634,6 +663,7 @@
* @property {ol.source.wms.ServerType|undefined} serverType The type of the remote WMS * @property {ol.source.wms.ServerType|undefined} serverType The type of the remote WMS
* server: `mapserver`, `geoserver` or `qgis`. Only needed if `hidpi` is `true`. * server: `mapserver`, `geoserver` or `qgis`. Only needed if `hidpi` is `true`.
* Default is `undefined`. * Default is `undefined`.
* @property {string|undefined} logo Logo.
* @property {Object.<string,*>} params WMS request parameters. At least a * @property {Object.<string,*>} params WMS request parameters. At least a
* `LAYERS` param is required. `STYLES` is `` by default. `VERSION` is * `LAYERS` param is required. `STYLES` is `` by default. `VERSION` is
* `1.3.0` by default. `WIDTH`, `HEIGHT`, `BBOX` and `CRS` (`SRS` for WMS * `1.3.0` by default. `WIDTH`, `HEIGHT`, `BBOX` and `CRS` (`SRS` for WMS
@@ -669,9 +699,9 @@
* @property {ol.Extent|undefined} extent Extent. * @property {ol.Extent|undefined} extent Extent.
* @property {ol.Extent|undefined} imageExtent Extent of the image. * @property {ol.Extent|undefined} imageExtent Extent of the image.
* @property {ol.Size|undefined} imageSize Size of the image. * @property {ol.Size|undefined} imageSize Size of the image.
* @property {string|undefined} logo Logo.
* @property {ol.proj.ProjectionLike} projection Projection. * @property {ol.proj.ProjectionLike} projection Projection.
* @property {string|undefined} url URL. * @property {string} url Url.
* @todo stability experimental
*/ */
/** /**

8
src/ol/canvasfunction.js Normal file
View File

@@ -0,0 +1,8 @@
goog.provide('ol.CanvasFunctionType');
/**
* @typedef {function(this:ol.source.ImageCanvas, ol.Extent, number,
* number, ol.Size, ol.proj.Projection): HTMLCanvasElement}
*/
ol.CanvasFunctionType;

View File

@@ -1,54 +1,30 @@
goog.provide('ol.Image'); goog.provide('ol.Image');
goog.provide('ol.ImageState');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.Attribution'); goog.require('ol.ImageBase');
goog.require('ol.Extent'); goog.require('ol.ImageState');
/**
* @enum {number}
*/
ol.ImageState = {
IDLE: 0,
LOADING: 1,
LOADED: 2,
ERROR: 3
};
/** /**
* @constructor * @constructor
* @extends {goog.events.EventTarget} * @extends {ol.ImageBase}
* @param {ol.Extent} extent Extent. * @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio. * @param {number} pixelRatio Pixel ratio.
* @param {Array.<ol.Attribution>} attributions Attributions.
* @param {string} src Image source URI. * @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin. * @param {?string} crossOrigin Cross origin.
* @param {Array.<ol.Attribution>} attributions Attributions.
*/ */
ol.Image = ol.Image =
function(extent, resolution, pixelRatio, src, crossOrigin, attributions) { function(extent, resolution, pixelRatio, attributions, src, crossOrigin) {
goog.base(this); goog.base(this, extent, resolution, pixelRatio, ol.ImageState.IDLE,
attributions);
/**
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = attributions;
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = extent;
/** /**
* @private * @private
@@ -56,18 +32,6 @@ ol.Image =
*/ */
this.src_ = src; this.src_ = src;
/**
* @private
* @type {number}
*/
this.pixelRatio_ = pixelRatio;
/**
* @private
* @type {number}
*/
this.resolution_ = resolution;
/** /**
* @private * @private
* @type {Image} * @type {Image}
@@ -95,31 +59,7 @@ ol.Image =
*/ */
this.state = ol.ImageState.IDLE; this.state = ol.ImageState.IDLE;
}; };
goog.inherits(ol.Image, goog.events.EventTarget); goog.inherits(ol.Image, ol.ImageBase);
/**
* @protected
*/
ol.Image.prototype.dispatchChangeEvent = function() {
this.dispatchEvent(goog.events.EventType.CHANGE);
};
/**
* @return {Array.<ol.Attribution>} Attributions.
*/
ol.Image.prototype.getAttributions = function() {
return this.attributions_;
};
/**
* @return {ol.Extent} Extent.
*/
ol.Image.prototype.getExtent = function() {
return this.extent_;
};
/** /**
@@ -145,30 +85,6 @@ ol.Image.prototype.getImageElement = function(opt_context) {
}; };
/**
* @return {number} PixelRatio.
*/
ol.Image.prototype.getPixelRatio = function() {
return this.pixelRatio_;
};
/**
* @return {number} Resolution.
*/
ol.Image.prototype.getResolution = function() {
return this.resolution_;
};
/**
* @return {ol.ImageState} State.
*/
ol.Image.prototype.getState = function() {
return this.state;
};
/** /**
* Tracks loading or read errors. * Tracks loading or read errors.
* *

127
src/ol/imagebase.js Normal file
View File

@@ -0,0 +1,127 @@
goog.provide('ol.ImageBase');
goog.provide('ol.ImageState');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
goog.require('ol.Attribution');
goog.require('ol.Extent');
/**
* @enum {number}
*/
ol.ImageState = {
IDLE: 0,
LOADING: 1,
LOADED: 2,
ERROR: 3
};
/**
* @constructor
* @extends {goog.events.EventTarget}
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.ImageState} state State.
* @param {Array.<ol.Attribution>} attributions Attributions.
*/
ol.ImageBase = function(extent, resolution, pixelRatio, state, attributions) {
goog.base(this);
/**
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = attributions;
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = extent;
/**
* @private
* @type {number}
*/
this.pixelRatio_ = pixelRatio;
/**
* @private
* @type {number}
*/
this.resolution_ = resolution;
/**
* @protected
* @type {ol.ImageState}
*/
this.state = state;
};
goog.inherits(ol.ImageBase, goog.events.EventTarget);
/**
* @protected
*/
ol.ImageBase.prototype.dispatchChangeEvent = function() {
this.dispatchEvent(goog.events.EventType.CHANGE);
};
/**
* @return {Array.<ol.Attribution>} Attributions.
*/
ol.ImageBase.prototype.getAttributions = function() {
return this.attributions_;
};
/**
* @return {ol.Extent} Extent.
*/
ol.ImageBase.prototype.getExtent = function() {
return this.extent_;
};
/**
* @param {Object=} opt_context Object.
* @return {HTMLCanvasElement|Image|HTMLVideoElement} Image.
*/
ol.ImageBase.prototype.getImageElement = goog.abstractMethod;
/**
* @return {number} PixelRatio.
*/
ol.ImageBase.prototype.getPixelRatio = function() {
return this.pixelRatio_;
};
/**
* @return {number} Resolution.
*/
ol.ImageBase.prototype.getResolution = function() {
return this.resolution_;
};
/**
* @return {ol.ImageState} State.
*/
ol.ImageBase.prototype.getState = function() {
return this.state;
};
/**
* Load not yet loaded URI.
*/
ol.ImageBase.prototype.load = goog.abstractMethod;

38
src/ol/imagecanvas.js Normal file
View File

@@ -0,0 +1,38 @@
goog.provide('ol.ImageCanvas');
goog.require('ol.ImageBase');
goog.require('ol.ImageState');
/**
* @constructor
* @extends {ol.ImageBase}
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {Array.<ol.Attribution>} attributions Attributions.
* @param {HTMLCanvasElement} canvas Canvas.
*/
ol.ImageCanvas = function(extent, resolution, pixelRatio, attributions,
canvas) {
goog.base(this, extent, resolution, pixelRatio, ol.ImageState.LOADED,
attributions);
/**
* @private
* @type {HTMLCanvasElement}
*/
this.canvas_ = canvas;
};
goog.inherits(ol.ImageCanvas, ol.ImageBase);
/**
* @inheritDoc
*/
ol.ImageCanvas.prototype.getImageElement = function(opt_context) {
return this.canvas_;
};

View File

@@ -4,7 +4,7 @@ goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.vec.Mat4'); goog.require('goog.vec.Mat4');
goog.require('ol.Image'); goog.require('ol.ImageBase');
goog.require('ol.ImageState'); goog.require('ol.ImageState');
goog.require('ol.ViewHint'); goog.require('ol.ViewHint');
goog.require('ol.layer.Image'); goog.require('ol.layer.Image');
@@ -27,7 +27,7 @@ ol.renderer.canvas.ImageLayer = function(mapRenderer, imageLayer) {
/** /**
* @private * @private
* @type {?ol.Image} * @type {?ol.ImageBase}
*/ */
this.image_ = null; this.image_ = null;

View File

@@ -6,7 +6,7 @@ goog.require('goog.dom.TagName');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.vec.Mat4'); goog.require('goog.vec.Mat4');
goog.require('ol.Image'); goog.require('ol.ImageBase');
goog.require('ol.ImageState'); goog.require('ol.ImageState');
goog.require('ol.ViewHint'); goog.require('ol.ViewHint');
goog.require('ol.dom'); goog.require('ol.dom');
@@ -32,7 +32,7 @@ ol.renderer.dom.ImageLayer = function(mapRenderer, imageLayer) {
/** /**
* The last rendered image. * The last rendered image.
* @private * @private
* @type {?ol.Image} * @type {?ol.ImageBase}
*/ */
this.image_ = null; this.image_ = null;

View File

@@ -7,7 +7,7 @@ goog.require('goog.vec.Mat4');
goog.require('goog.webgl'); goog.require('goog.webgl');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.Extent'); goog.require('ol.Extent');
goog.require('ol.Image'); goog.require('ol.ImageBase');
goog.require('ol.ImageState'); goog.require('ol.ImageState');
goog.require('ol.ViewHint'); goog.require('ol.ViewHint');
goog.require('ol.layer.Image'); goog.require('ol.layer.Image');
@@ -29,7 +29,7 @@ ol.renderer.webgl.ImageLayer = function(mapRenderer, imageLayer) {
/** /**
* The last rendered image. * The last rendered image.
* @private * @private
* @type {?ol.Image} * @type {?ol.ImageBase}
*/ */
this.image_ = null; this.image_ = null;
@@ -38,7 +38,7 @@ goog.inherits(ol.renderer.webgl.ImageLayer, ol.renderer.webgl.Layer);
/** /**
* @param {ol.Image} image Image. * @param {ol.ImageBase} image Image.
* @private * @private
* @return {WebGLTexture} Texture. * @return {WebGLTexture} Texture.
*/ */

View File

@@ -0,0 +1 @@
@exportSymbol ol.source.ImageCanvas

View File

@@ -0,0 +1,79 @@
goog.provide('ol.source.ImageCanvas');
goog.require('ol.CanvasFunctionType');
goog.require('ol.ImageCanvas');
goog.require('ol.extent');
goog.require('ol.source.Image');
/**
* @constructor
* @extends {ol.source.Image}
* @param {olx.source.ImageCanvasOptions} options
*/
ol.source.ImageCanvas = function(options) {
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
logo: options.logo,
projection: options.projection,
resolutions: options.resolutions,
state: options.state
});
/**
* @private
* @type {ol.CanvasFunctionType}
*/
this.canvasFunction_ = options.canvasFunction;
/**
* @private
* @type {ol.ImageCanvas}
*/
this.canvas_ = null;
/**
* @private
* @type {number}
*/
this.ratio_ = goog.isDef(options.ratio) ?
options.ratio : 1.5;
};
goog.inherits(ol.source.ImageCanvas, ol.source.Image);
/**
* @inheritDoc
*/
ol.source.ImageCanvas.prototype.getImage =
function(extent, resolution, pixelRatio, projection) {
resolution = this.findNearestResolution(resolution);
var canvas = this.canvas_;
if (!goog.isNull(canvas) &&
canvas.getResolution() == resolution &&
canvas.getPixelRatio() == pixelRatio &&
ol.extent.containsExtent(canvas.getExtent(), extent)) {
return canvas;
}
extent = extent.slice();
ol.extent.scaleFromCenter(extent, this.ratio_);
var width = (extent[2] - extent[0]) / resolution;
var height = (extent[3] - extent[1]) / resolution;
var size = [width * pixelRatio, height * pixelRatio];
var canvasElement = this.canvasFunction_(
extent, resolution, pixelRatio, size, projection);
if (!goog.isNull(canvasElement)) {
canvas = new ol.ImageCanvas(extent, resolution, pixelRatio,
this.getAttributions(), canvasElement);
}
this.canvas_ = canvas;
return canvas;
};

View File

@@ -5,23 +5,17 @@ goog.require('goog.asserts');
goog.require('ol.Attribution'); goog.require('ol.Attribution');
goog.require('ol.Extent'); goog.require('ol.Extent');
goog.require('ol.Image'); goog.require('ol.Image');
goog.require('ol.ImageUrlFunction');
goog.require('ol.ImageUrlFunctionType');
goog.require('ol.Size');
goog.require('ol.array'); goog.require('ol.array');
goog.require('ol.source.Source'); goog.require('ol.source.Source');
/** /**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined), * @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* crossOrigin: (null|string|undefined),
* extent: (null|ol.Extent|undefined), * extent: (null|ol.Extent|undefined),
* logo: (string|undefined), * logo: (string|undefined),
* projection: ol.proj.ProjectionLike, * projection: ol.proj.ProjectionLike,
* resolutions: (Array.<number>|undefined), * resolutions: (Array.<number>|undefined),
* imageUrlFunction: (ol.ImageUrlFunctionType| * state: (ol.source.State|undefined)}}
* undefined)}}
* @todo stability experimental
*/ */
ol.source.ImageOptions; ol.source.ImageOptions;
@@ -39,25 +33,10 @@ ol.source.Image = function(options) {
attributions: options.attributions, attributions: options.attributions,
extent: options.extent, extent: options.extent,
logo: options.logo, logo: options.logo,
projection: options.projection projection: options.projection,
state: options.state
}); });
/**
* @protected
* @type {ol.ImageUrlFunctionType}
*/
this.imageUrlFunction =
goog.isDef(options.imageUrlFunction) ?
options.imageUrlFunction :
ol.ImageUrlFunction.nullImageUrlFunction;
/**
* @protected
* @type {?string}
*/
this.crossOrigin =
goog.isDef(options.crossOrigin) ? options.crossOrigin : null;
/** /**
* @private * @private
* @type {Array.<number>} * @type {Array.<number>}
@@ -75,24 +54,10 @@ goog.inherits(ol.source.Image, ol.source.Source);
/** /**
* @protected * @return {Array.<number>} Resolutions.
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.Size} size Size.
* @param {ol.proj.Projection} projection Projection.
* @return {ol.Image} Single image.
*/ */
ol.source.Image.prototype.createImage = ol.source.Image.prototype.getResolutions = function() {
function(extent, resolution, pixelRatio, size, projection) { return this.resolutions_;
var image = null;
var imageUrl = this.imageUrlFunction(extent, size, projection);
if (goog.isDef(imageUrl)) {
image = new ol.Image(
extent, resolution, pixelRatio, imageUrl, this.crossOrigin,
this.getAttributions());
}
return image;
}; };
@@ -116,6 +81,6 @@ ol.source.Image.prototype.findNearestResolution =
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio. * @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection. * @param {ol.proj.Projection} projection Projection.
* @return {ol.Image} Single image. * @return {ol.ImageBase} Single image.
*/ */
ol.source.Image.prototype.getImage = goog.abstractMethod; ol.source.Image.prototype.getImage = goog.abstractMethod;

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.ImageStatic'); goog.provide('ol.source.ImageStatic');
goog.require('ol.Image'); goog.require('ol.Image');
goog.require('ol.ImageUrlFunctionType');
goog.require('ol.extent'); goog.require('ol.extent');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.Image'); goog.require('ol.source.Image');
@@ -16,20 +15,21 @@ goog.require('ol.source.Image');
*/ */
ol.source.ImageStatic = function(options) { ol.source.ImageStatic = function(options) {
var imageFunction = ol.source.ImageStatic.createImageFunction( var attributions = goog.isDef(options.attributions) ?
options.url); options.attributions : null;
var crossOrigin = goog.isDef(options.crossOrigin) ?
options.crossOrigin : null;
var imageExtent = options.imageExtent; var imageExtent = options.imageExtent;
var imageSize = options.imageSize; var imageSize = options.imageSize;
var imageResolution = (imageExtent[3] - imageExtent[1]) / imageSize[1]; var imageResolution = (imageExtent[3] - imageExtent[1]) / imageSize[1];
var imageUrl = options.url;
var projection = ol.proj.get(options.projection); var projection = ol.proj.get(options.projection);
goog.base(this, { goog.base(this, {
attributions: options.attributions, attributions: attributions,
crossOrigin: options.crossOrigin,
extent: options.extent, extent: options.extent,
projection: options.projection, logo: options.logo,
imageUrlFunction: imageFunction, projection: projection,
resolutions: [imageResolution] resolutions: [imageResolution]
}); });
@@ -37,8 +37,8 @@ ol.source.ImageStatic = function(options) {
* @private * @private
* @type {ol.Image} * @type {ol.Image}
*/ */
this.image_ = this.createImage( this.image_ = new ol.Image(imageExtent, imageResolution, 1, attributions,
imageExtent, imageResolution, 1, imageSize, projection); imageUrl, crossOrigin);
}; };
goog.inherits(ol.source.ImageStatic, ol.source.Image); goog.inherits(ol.source.ImageStatic, ol.source.Image);
@@ -54,21 +54,3 @@ ol.source.ImageStatic.prototype.getImage =
} }
return null; return null;
}; };
/**
* @param {string|undefined} url URL.
* @return {ol.ImageUrlFunctionType} Function.
*/
ol.source.ImageStatic.createImageFunction = function(url) {
return (
/**
* @param {ol.Extent} extent Extent.
* @param {ol.Size} size Size.
* @param {ol.proj.Projection} projection Projection.
* @return {string|undefined} URL.
*/
function(extent, size, projection) {
return url;
});
};

View File

@@ -0,0 +1 @@
@exportSymbol ol.source.ImageVector

View File

@@ -0,0 +1,213 @@
goog.provide('ol.source.ImageVector');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.vec.Mat4');
goog.require('ol.extent');
goog.require('ol.render.canvas.ReplayGroup');
goog.require('ol.renderer.vector');
goog.require('ol.source.ImageCanvas');
goog.require('ol.source.Vector');
goog.require('ol.style.ImageState');
goog.require('ol.vec.Mat4');
/**
* @constructor
* @extends {ol.source.ImageCanvas}
* @param {olx.source.ImageVectorOptions} options Options.
*/
ol.source.ImageVector = function(options) {
/**
* @private
* @type {ol.source.Vector}
*/
this.source_ = options.source;
/**
* @private
* @type {ol.feature.StyleFunction}
*/
this.styleFunction_ = options.styleFunction;
/**
* @private
* @type {!goog.vec.Mat4.Number}
*/
this.transform_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {HTMLCanvasElement}
*/
this.canvasElement_ = /** @type {HTMLCanvasElement} */
(goog.dom.createElement(goog.dom.TagName.CANVAS));
/**
* @private
* @type {CanvasRenderingContext2D}
*/
this.canvasContext_ = /** @type {CanvasRenderingContext2D} */
(this.canvasElement_.getContext('2d'));
/**
* @private
* @type {ol.Size}
*/
this.canvasSize_ = [0, 0];
goog.base(this, {
attributions: options.attributions,
canvasFunction: goog.bind(this.canvasFunctionInternal_, this),
extent: options.extent,
logo: options.logo,
projection: options.projection,
ratio: options.ratio,
resolutions: options.resolutions,
state: this.source_.getState()
});
goog.events.listen(this.source_, goog.events.EventType.CHANGE,
this.handleSourceChange_, undefined, this);
};
goog.inherits(ol.source.ImageVector, ol.source.ImageCanvas);
/**
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.Size} size Size.
* @param {ol.proj.Projection} projection Projection;
* @return {HTMLCanvasElement} Canvas element.
* @private
*/
ol.source.ImageVector.prototype.canvasFunctionInternal_ =
function(extent, resolution, pixelRatio, size, projection) {
var tolerance = resolution / (2 * pixelRatio);
var replayGroup = new ol.render.canvas.ReplayGroup(
pixelRatio, tolerance);
var loading = false;
this.source_.forEachFeatureInExtent(extent,
/**
* @param {ol.Feature} feature Feature.
*/
function(feature) {
loading = loading ||
this.renderFeature_(feature, resolution, pixelRatio, replayGroup);
}, this);
replayGroup.finish();
if (loading) {
return null;
}
if (this.canvasSize_[0] != size[0] || this.canvasSize_[1] != size[1]) {
this.canvasElement_.width = size[0];
this.canvasElement_.height = size[1];
this.canvasSize_[0] = size[0];
this.canvasSize_[1] = size[1];
} else {
this.canvasContext_.clearRect(0, 0, size[0], size[1]);
}
var transform = this.getTransform_(ol.extent.getCenter(extent),
resolution, pixelRatio, size);
replayGroup.replay(this.canvasContext_, extent, transform,
goog.functions.TRUE);
return this.canvasElement_;
};
/**
* @param {ol.Coordinate} center Center.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.Size} size Size.
* @return {!goog.vec.Mat4.Number} Transform.
* @private
*/
ol.source.ImageVector.prototype.getTransform_ =
function(center, resolution, pixelRatio, size) {
return ol.vec.Mat4.makeTransform2D(this.transform_,
size[0] / 2, size[1] / 2,
pixelRatio / resolution, -pixelRatio / resolution,
0,
-center[0], -center[1]);
};
/**
* Handle changes in image style state.
* @param {goog.events.Event} event Image style change event.
* @private
*/
ol.source.ImageVector.prototype.handleImageStyleChange_ =
function(event) {
var imageStyle = /** @type {ol.style.Image} */ (event.target);
if (imageStyle.getImageState() == ol.style.ImageState.LOADED) {
this.dispatchChangeEvent();
}
};
/**
* @private
*/
ol.source.ImageVector.prototype.handleSourceChange_ = function() {
// setState will trigger a CHANGE event, so we always rely
// change events by calling setState.
this.setState(this.source_.getState());
};
/**
* @param {ol.Feature} feature Feature.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.render.canvas.ReplayGroup} replayGroup Replay group.
* @return {boolean} `true` if an image is loading.
* @private
*/
ol.source.ImageVector.prototype.renderFeature_ =
function(feature, resolution, pixelRatio, replayGroup) {
var loading = false;
var styles = this.styleFunction_(feature, resolution);
if (!goog.isDefAndNotNull(styles)) {
return false;
}
// simplify to a tolerance of half a device pixel
var squaredTolerance =
resolution * resolution / (4 * pixelRatio * pixelRatio);
var i, ii, style, imageStyle, imageState;
for (i = 0, ii = styles.length; i < ii; ++i) {
style = styles[i];
imageStyle = style.getImage();
if (!goog.isNull(imageStyle)) {
if (imageStyle.getImageState() == ol.style.ImageState.IDLE) {
goog.events.listenOnce(imageStyle, goog.events.EventType.CHANGE,
this.handleImageStyleChange_, false, this);
imageStyle.load();
} else if (imageStyle.getImageState() == ol.style.ImageState.LOADED) {
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
}
goog.asserts.assert(
imageStyle.getImageState() != ol.style.ImageState.IDLE);
loading = imageStyle.getImageState() == ol.style.ImageState.LOADING;
} else {
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
}
}
return loading;
};

View File

@@ -24,12 +24,19 @@ ol.source.ImageWMS = function(opt_options) {
goog.base(this, { goog.base(this, {
attributions: options.attributions, attributions: options.attributions,
crossOrigin: options.crossOrigin,
extent: options.extent, extent: options.extent,
logo: options.logo,
projection: options.projection, projection: options.projection,
resolutions: options.resolutions resolutions: options.resolutions
}); });
/**
* @private
* @type {?string}
*/
this.crossOrigin_ =
goog.isDef(options.crossOrigin) ? options.crossOrigin : null;
/** /**
* @private * @private
* @type {string|undefined} * @type {string|undefined}
@@ -181,8 +188,8 @@ ol.source.ImageWMS.prototype.getImage =
var url = goog.uri.utils.appendParamsFromMap(this.url_, params); var url = goog.uri.utils.appendParamsFromMap(this.url_, params);
this.image_ = new ol.Image(extent, resolution, pixelRatio, url, this.image_ = new ol.Image(extent, resolution, pixelRatio,
this.crossOrigin, this.getAttributions()); this.getAttributions(), url, this.crossOrigin_);
return this.image_; return this.image_;
}; };

View File

@@ -2,6 +2,7 @@ goog.provide('ol.source.MapGuide');
goog.require('goog.object'); goog.require('goog.object');
goog.require('goog.uri.utils'); goog.require('goog.uri.utils');
goog.require('ol.Image');
goog.require('ol.ImageUrlFunction'); goog.require('ol.ImageUrlFunction');
goog.require('ol.extent'); goog.require('ol.extent');
goog.require('ol.source.Image'); goog.require('ol.source.Image');
@@ -15,6 +16,26 @@ goog.require('ol.source.Image');
*/ */
ol.source.MapGuide = function(options) { ol.source.MapGuide = function(options) {
goog.base(this, {
extent: options.extent,
projection: options.projection,
resolutions: options.resolutions
});
/**
* @private
* @type {?string}
*/
this.crossOrigin_ =
goog.isDef(options.crossOrigin) ? options.crossOrigin : null;
/**
* @private
* @type {number}
*/
this.displayDpi_ = goog.isDef(options.displayDpi) ?
options.displayDpi : 96;
var imageUrlFunction; var imageUrlFunction;
if (goog.isDef(options.url)) { if (goog.isDef(options.url)) {
var params = goog.isDef(options.params) ? options.params : {}; var params = goog.isDef(options.params) ? options.params : {};
@@ -24,12 +45,11 @@ ol.source.MapGuide = function(options) {
imageUrlFunction = ol.ImageUrlFunction.nullImageUrlFunction; imageUrlFunction = ol.ImageUrlFunction.nullImageUrlFunction;
} }
goog.base(this, { /**
extent: options.extent, * @private
projection: options.projection, * @type {ol.ImageUrlFunctionType}
resolutions: options.resolutions, */
imageUrlFunction: imageUrlFunction this.imageUrlFunction_ = imageUrlFunction;
});
/** /**
* @private * @private
@@ -37,13 +57,6 @@ ol.source.MapGuide = function(options) {
*/ */
this.hidpi_ = goog.isDef(options.hidpi) ? options.hidpi : true; this.hidpi_ = goog.isDef(options.hidpi) ? options.hidpi : true;
/**
* @private
* @type {number}
*/
this.displayDpi_ = goog.isDef(options.displayDpi) ?
options.displayDpi : 96;
/** /**
* @private * @private
* @type {number} * @type {number}
@@ -98,9 +111,16 @@ ol.source.MapGuide.prototype.getImage =
var height = (extent[3] - extent[1]) / resolution; var height = (extent[3] - extent[1]) / resolution;
var size = [width * pixelRatio, height * pixelRatio]; var size = [width * pixelRatio, height * pixelRatio];
this.image_ = this.createImage( var imageUrl = this.imageUrlFunction_(extent, size, projection);
extent, resolution, pixelRatio, size, projection); if (goog.isDef(imageUrl)) {
return this.image_; image = new ol.Image(extent, resolution, pixelRatio,
this.getAttributions(), imageUrl, this.crossOrigin_);
} else {
image = null;
}
this.image_ = image;
return image;
}; };