Files
openlayers/src/ol/source/Image.js
2018-01-10 09:36:28 -07:00

232 lines
5.5 KiB
JavaScript

/**
* @module ol/source/Image
*/
import {ENABLE_RASTER_REPROJECTION} from '../reproj/common.js';
import {inherits} from '../index.js';
import ImageState from '../ImageState.js';
import {linearFindNearest} from '../array.js';
import Event from '../events/Event.js';
import {equals} from '../extent.js';
import {equivalent} from '../proj.js';
import _ol_reproj_Image_ from '../reproj/Image.js';
import Source from '../source/Source.js';
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
* instantiated in apps.
* Base class for sources providing a single image.
*
* @constructor
* @abstract
* @extends {ol.source.Source}
* @param {ol.SourceImageOptions} options Single image source options.
* @api
*/
var ImageSource = function(options) {
Source.call(this, {
attributions: options.attributions,
extent: options.extent,
projection: options.projection,
state: options.state
});
/**
* @private
* @type {Array.<number>}
*/
this.resolutions_ = options.resolutions !== undefined ?
options.resolutions : null;
/**
* @private
* @type {ol.reproj.Image}
*/
this.reprojectedImage_ = null;
/**
* @private
* @type {number}
*/
this.reprojectedRevision_ = 0;
};
inherits(ImageSource, Source);
/**
* @return {Array.<number>} Resolutions.
* @override
*/
ImageSource.prototype.getResolutions = function() {
return this.resolutions_;
};
/**
* @protected
* @param {number} resolution Resolution.
* @return {number} Resolution.
*/
ImageSource.prototype.findNearestResolution = function(resolution) {
if (this.resolutions_) {
var idx = linearFindNearest(this.resolutions_, resolution, 0);
resolution = this.resolutions_[idx];
}
return resolution;
};
/**
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection.
* @return {ol.ImageBase} Single image.
*/
ImageSource.prototype.getImage = function(extent, resolution, pixelRatio, projection) {
var sourceProjection = this.getProjection();
if (!ENABLE_RASTER_REPROJECTION ||
!sourceProjection ||
!projection ||
equivalent(sourceProjection, projection)) {
if (sourceProjection) {
projection = sourceProjection;
}
return this.getImageInternal(extent, resolution, pixelRatio, projection);
} else {
if (this.reprojectedImage_) {
if (this.reprojectedRevision_ == this.getRevision() &&
equivalent(
this.reprojectedImage_.getProjection(), projection) &&
this.reprojectedImage_.getResolution() == resolution &&
equals(this.reprojectedImage_.getExtent(), extent)) {
return this.reprojectedImage_;
}
this.reprojectedImage_.dispose();
this.reprojectedImage_ = null;
}
this.reprojectedImage_ = new _ol_reproj_Image_(
sourceProjection, projection, extent, resolution, pixelRatio,
function(extent, resolution, pixelRatio) {
return this.getImageInternal(extent, resolution,
pixelRatio, sourceProjection);
}.bind(this));
this.reprojectedRevision_ = this.getRevision();
return this.reprojectedImage_;
}
};
/**
* @abstract
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection.
* @return {ol.ImageBase} Single image.
* @protected
*/
ImageSource.prototype.getImageInternal = function(extent, resolution, pixelRatio, projection) {};
/**
* Handle image change events.
* @param {ol.events.Event} event Event.
* @protected
*/
ImageSource.prototype.handleImageChange = function(event) {
var image = /** @type {ol.Image} */ (event.target);
switch (image.getState()) {
case ImageState.LOADING:
this.dispatchEvent(
new ImageSource.Event(ImageSource.EventType_.IMAGELOADSTART,
image));
break;
case ImageState.LOADED:
this.dispatchEvent(
new ImageSource.Event(ImageSource.EventType_.IMAGELOADEND,
image));
break;
case ImageState.ERROR:
this.dispatchEvent(
new ImageSource.Event(ImageSource.EventType_.IMAGELOADERROR,
image));
break;
default:
// pass
}
};
/**
* Default image load function for image sources that use ol.Image image
* instances.
* @param {ol.Image} image Image.
* @param {string} src Source.
*/
ImageSource.defaultImageLoadFunction = function(image, src) {
image.getImage().src = src;
};
/**
* @classdesc
* Events emitted by {@link ol.source.Image} instances are instances of this
* type.
*
* @constructor
* @extends {ol.events.Event}
* @implements {oli.source.ImageEvent}
* @param {string} type Type.
* @param {ol.Image} image The image.
*/
ImageSource.Event = function(type, image) {
Event.call(this, type);
/**
* The image related to the event.
* @type {ol.Image}
* @api
*/
this.image = image;
};
inherits(ImageSource.Event, Event);
/**
* @enum {string}
* @private
*/
ImageSource.EventType_ = {
/**
* Triggered when an image starts loading.
* @event ol.source.Image.Event#imageloadstart
* @api
*/
IMAGELOADSTART: 'imageloadstart',
/**
* Triggered when an image finishes loading.
* @event ol.source.Image.Event#imageloadend
* @api
*/
IMAGELOADEND: 'imageloadend',
/**
* Triggered if image loading results in an error.
* @event ol.source.Image.Event#imageloaderror
* @api
*/
IMAGELOADERROR: 'imageloaderror'
};
export default ImageSource;