If image size is not provided, determine resolution on load
This commit is contained in:
+5
-1
@@ -7,6 +7,7 @@ goog.require('goog.events.EventType');
|
|||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('ol.ImageBase');
|
goog.require('ol.ImageBase');
|
||||||
goog.require('ol.ImageState');
|
goog.require('ol.ImageState');
|
||||||
|
goog.require('ol.extent');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ goog.require('ol.ImageState');
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @extends {ol.ImageBase}
|
* @extends {ol.ImageBase}
|
||||||
* @param {ol.Extent} extent Extent.
|
* @param {ol.Extent} extent Extent.
|
||||||
* @param {number} resolution Resolution.
|
* @param {number|undefined} resolution Resolution.
|
||||||
* @param {number} pixelRatio Pixel ratio.
|
* @param {number} pixelRatio Pixel ratio.
|
||||||
* @param {Array.<ol.Attribution>} attributions Attributions.
|
* @param {Array.<ol.Attribution>} attributions Attributions.
|
||||||
* @param {string} src Image source URI.
|
* @param {string} src Image source URI.
|
||||||
@@ -103,6 +104,9 @@ ol.Image.prototype.handleImageError_ = function() {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.Image.prototype.handleImageLoad_ = function() {
|
ol.Image.prototype.handleImageLoad_ = function() {
|
||||||
|
if (!goog.isDef(this.resolution)) {
|
||||||
|
this.resolution = ol.extent.getHeight(this.extent) / this.image_.height;
|
||||||
|
}
|
||||||
this.state = ol.ImageState.LOADED;
|
this.state = ol.ImageState.LOADED;
|
||||||
this.unlistenImage_();
|
this.unlistenImage_();
|
||||||
this.changed();
|
this.changed();
|
||||||
|
|||||||
+10
-8
@@ -1,6 +1,7 @@
|
|||||||
goog.provide('ol.ImageBase');
|
goog.provide('ol.ImageBase');
|
||||||
goog.provide('ol.ImageState');
|
goog.provide('ol.ImageState');
|
||||||
|
|
||||||
|
goog.require('goog.asserts');
|
||||||
goog.require('goog.events.EventTarget');
|
goog.require('goog.events.EventTarget');
|
||||||
goog.require('goog.events.EventType');
|
goog.require('goog.events.EventType');
|
||||||
goog.require('ol.Attribution');
|
goog.require('ol.Attribution');
|
||||||
@@ -23,7 +24,7 @@ ol.ImageState = {
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @extends {goog.events.EventTarget}
|
* @extends {goog.events.EventTarget}
|
||||||
* @param {ol.Extent} extent Extent.
|
* @param {ol.Extent} extent Extent.
|
||||||
* @param {number} resolution Resolution.
|
* @param {number|undefined} resolution Resolution.
|
||||||
* @param {number} pixelRatio Pixel ratio.
|
* @param {number} pixelRatio Pixel ratio.
|
||||||
* @param {ol.ImageState} state State.
|
* @param {ol.ImageState} state State.
|
||||||
* @param {Array.<ol.Attribution>} attributions Attributions.
|
* @param {Array.<ol.Attribution>} attributions Attributions.
|
||||||
@@ -39,10 +40,10 @@ ol.ImageBase = function(extent, resolution, pixelRatio, state, attributions) {
|
|||||||
this.attributions_ = attributions;
|
this.attributions_ = attributions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @protected
|
||||||
* @type {ol.Extent}
|
* @type {ol.Extent}
|
||||||
*/
|
*/
|
||||||
this.extent_ = extent;
|
this.extent = extent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
@@ -51,10 +52,10 @@ ol.ImageBase = function(extent, resolution, pixelRatio, state, attributions) {
|
|||||||
this.pixelRatio_ = pixelRatio;
|
this.pixelRatio_ = pixelRatio;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @protected
|
||||||
* @type {number}
|
* @type {number|undefined}
|
||||||
*/
|
*/
|
||||||
this.resolution_ = resolution;
|
this.resolution = resolution;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @protected
|
* @protected
|
||||||
@@ -86,7 +87,7 @@ ol.ImageBase.prototype.getAttributions = function() {
|
|||||||
* @return {ol.Extent} Extent.
|
* @return {ol.Extent} Extent.
|
||||||
*/
|
*/
|
||||||
ol.ImageBase.prototype.getExtent = function() {
|
ol.ImageBase.prototype.getExtent = function() {
|
||||||
return this.extent_;
|
return this.extent;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -109,7 +110,8 @@ ol.ImageBase.prototype.getPixelRatio = function() {
|
|||||||
* @return {number} Resolution.
|
* @return {number} Resolution.
|
||||||
*/
|
*/
|
||||||
ol.ImageBase.prototype.getResolution = function() {
|
ol.ImageBase.prototype.getResolution = function() {
|
||||||
return this.resolution_;
|
goog.asserts.assert(goog.isDef(this.resolution), 'resolution not yet set');
|
||||||
|
return this.resolution;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
goog.provide('ol.source.ImageStatic');
|
goog.provide('ol.source.ImageStatic');
|
||||||
|
|
||||||
|
goog.require('goog.events');
|
||||||
|
goog.require('goog.events.EventType');
|
||||||
goog.require('ol.Image');
|
goog.require('ol.Image');
|
||||||
|
goog.require('ol.ImageState');
|
||||||
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');
|
||||||
|
goog.require('ol.source.State');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -21,27 +25,41 @@ ol.source.ImageStatic = function(options) {
|
|||||||
|
|
||||||
var attributions = goog.isDef(options.attributions) ?
|
var attributions = goog.isDef(options.attributions) ?
|
||||||
options.attributions : null;
|
options.attributions : null;
|
||||||
|
|
||||||
|
var imageExtent = options.imageExtent;
|
||||||
|
|
||||||
|
var state, resolution, resolutions;
|
||||||
|
if (goog.isDef(options.imageSize)) {
|
||||||
|
state = ol.source.State.READY;
|
||||||
|
resolution = ol.extent.getHeight(imageExtent) / options.imageSize[1];
|
||||||
|
resolutions = [resolution];
|
||||||
|
} else {
|
||||||
|
state = ol.source.State.LOADING;
|
||||||
|
}
|
||||||
|
|
||||||
var crossOrigin = goog.isDef(options.crossOrigin) ?
|
var crossOrigin = goog.isDef(options.crossOrigin) ?
|
||||||
options.crossOrigin : null;
|
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, {
|
goog.base(this, {
|
||||||
attributions: attributions,
|
attributions: attributions,
|
||||||
logo: options.logo,
|
logo: options.logo,
|
||||||
projection: projection,
|
projection: ol.proj.get(options.projection),
|
||||||
resolutions: [imageResolution]
|
resolutions: resolutions,
|
||||||
|
state: state
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {ol.Image}
|
* @type {ol.Image}
|
||||||
*/
|
*/
|
||||||
this.image_ = new ol.Image(imageExtent, imageResolution, 1, attributions,
|
this.image_ = new ol.Image(imageExtent, resolution, 1, attributions,
|
||||||
imageUrl, crossOrigin);
|
options.url, crossOrigin);
|
||||||
|
|
||||||
|
if (state !== ol.source.State.READY) {
|
||||||
|
goog.events.listen(this.image_, goog.events.EventType.CHANGE,
|
||||||
|
this.handleImageChange_, false, this);
|
||||||
|
this.image_.load();
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
goog.inherits(ol.source.ImageStatic, ol.source.Image);
|
goog.inherits(ol.source.ImageStatic, ol.source.Image);
|
||||||
@@ -57,3 +75,17 @@ ol.source.ImageStatic.prototype.getImage =
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle image change events.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.source.ImageStatic.prototype.handleImageChange_ = function() {
|
||||||
|
var imageState = this.image_.getState();
|
||||||
|
if (imageState === ol.ImageState.LOADED) {
|
||||||
|
this.setState(ol.source.State.READY);
|
||||||
|
} else if (imageState === ol.ImageState.ERROR) {
|
||||||
|
this.setState(ol.source.State.ERROR);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user