Image source refactoring

This commit is contained in:
Éric Lemoine
2013-09-22 09:58:05 +01:00
parent 0cc844a169
commit 5af738593e
14 changed files with 343 additions and 190 deletions

View File

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

View File

@@ -0,0 +1,76 @@
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
});
/**
* @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, size,
projection);
canvas = new ol.ImageCanvas(extent, resolution, pixelRatio,
this.getAttributions(), canvasElement);
this.canvas_ = canvas;
return canvas;
};

View File

@@ -5,23 +5,16 @@ goog.require('goog.asserts');
goog.require('ol.Attribution');
goog.require('ol.Extent');
goog.require('ol.Image');
goog.require('ol.ImageUrlFunction');
goog.require('ol.ImageUrlFunctionType');
goog.require('ol.Size');
goog.require('ol.array');
goog.require('ol.source.Source');
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* crossOrigin: (null|string|undefined),
* extent: (null|ol.Extent|undefined),
* logo: (string|undefined),
* projection: ol.proj.ProjectionLike,
* resolutions: (Array.<number>|undefined),
* imageUrlFunction: (ol.ImageUrlFunctionType|
* undefined)}}
* @todo stability experimental
* resolutions: (Array.<number>|undefined)}}
*/
ol.source.ImageOptions;
@@ -42,22 +35,6 @@ ol.source.Image = function(options) {
projection: options.projection
});
/**
* @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
* @type {Array.<number>}
@@ -75,24 +52,10 @@ goog.inherits(ol.source.Image, ol.source.Source);
/**
* @protected
* @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.
* @return {Array.<number>} Resolutions.
*/
ol.source.Image.prototype.createImage =
function(extent, resolution, pixelRatio, size, projection) {
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;
ol.source.Image.prototype.getResolutions = function() {
return this.resolutions_;
};
@@ -116,6 +79,6 @@ ol.source.Image.prototype.findNearestResolution =
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection.
* @return {ol.Image} Single image.
* @return {ol.ImageBase} Single image.
*/
ol.source.Image.prototype.getImage = goog.abstractMethod;

View File

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

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

View File

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