/**
* @class
* @constructor
* @param {string} name Name of WMS layer to paint
* @param {string|Array.<string>} urls URL o URLs (array) from WMS service
* @param {Object} params Object with key / value pairs with the required parameters
* to launch a GETMAP query to WMS server. If not specified some have associated
* default value: (name options in capital letters)
* `SERVICE: "WMS"`, `VERSION: "1.1.1"` `REQUEST:
* "GetMap"`, `STYLES: ""`, `EXCEPTIONS: "application/vnd.ogc.se_inimage"`
* `FORMAT: "image/jpeg"` The following are mandatory: `SRS`, `LAYERS`
* @param {Object} options Hash table with extra options to configure the WMS layer
*/
cercalia.WMS = function(name, urls, params, options) {
/**
* Class name
*
* @private
* @type {string}
*/
this.CLASS_NAME_ = "cercalia.WMS";
/**
* Property: name
*
* @type {string}
* @private
*/
this.name_ = name;
/**
* Property: map
*
* @type {cercalia.Map}
* @private
*/
this.map_ = null;
/**
* Property: urls
*
* @type {Array.<string>}
* @private
*/
this.urls_ = urls instanceof Array ? urls : [ urls ];
/**
* WMS request properties Property: params
*
* @type {Object.<String>}
* @private
*/
this.params_ = params ? params : {};
/**
* Hash table with extra options to configure the WMS layer Property:
* params
*
* @type {Object.<String>}
* @private
*/
this.options_ = options ? options : {};
// Comprobamos los valores para asignar los de defecto
if (!this.params_.SERVICE)
this.params_.SERVICE = "WMS";
if (!this.params_.VERSION)
this.params_.VERSION = "1.1.1";
if (!this.params_.REQUEST)
this.params_.REQUEST = "GetMap";
if (!this.params_.STYLES)
this.params_.STYLES = "";
if (!this.params_.EXCEPTIONS)
this.params_.EXCEPTIONS = "application/vnd.ogc.se_inimage";
if (!this.params_.FORMAT)
this.params_.FORMAT = "image/jpeg";
if (!this.params_.WIDTH)
this.params_.WIDTH = 256;
if (!this.params_.HEIGHT)
this.params_.HEIGHT = 256;
if (!this.params_.LAYERS) {
alert('Layer no especificada');
return null;
}
// Generamos el objeto TileWMSOptions
var tileWMSoptions = {
urls : this.urls_,
params : this.params_
};
if( this.params_.SRS ){
tileWMSoptions.projection = this.params_.SRS;
} else if ( this.params_.CRS ) {
tileWMSoptions.projection = this.params_.CRS;
} else {
tileWMSoptions.projection = 'EPSG:3857';
}
tileWMSoptions.extent = this.options_.extent ? this.options_.extent : undefined;
tileWMSoptions.gutter = this.options_.gutter ? this.options_.gutter : undefined;
tileWMSoptions.maxZoom = this.options_.maxZoom ? this.options_.maxZoom : undefined;
tileWMSoptions.tileLoadFunction = this.options_.tileLoadFunction ? this.options_.tileLoadFunction : undefined;
/**
* @private
* @type {ol.source.TileWMS}
*/
this.wmsSource_ = new ol.source.TileWMS(tileWMSoptions);
/**
* @private
* @type {ol.layer.Tile}
*/
this.layer_ = new ol.layer.Tile({
source : this.wmsSource_,
name : this.name_,
visible: this.options_.visible ? this.options_.visible : true
});
};
/**
* We assign a map to the object. If it's already exist, is deleted in the existing
* map and WMS layer is painted on the map passed as parameter.
* If parameter is "null", it's removed from the map.
*
* @param {cercalia.Map|null} map Map.
* @param {number} position ZIndex position from layers.
*/
cercalia.WMS.prototype.setMap = function(map, position) {
if (!this.map_) {
if (map) {
this.map_ = map;
this.map_.addLayer(this.layer_, position);
} else {
this.map_.getMap().removeLayer(this.layer_);
}
} else {
if (map == null) {
this.map_.getMap().removeLayer(this.layer_);
} else if (this.map_ != map) {
this.map_.getMap().removeLayer(this.layer_);
this.map_ = map;
this.map_.addLayer(this.layer_, position);
}
}
};
/**
* Updates the parameters
* @param {Object} params
*/
cercalia.WMS.prototype.updateParams = function(params) {
this.wmsSource_.updateParams(params);
};
/**
* @return {cercalia.Map|null} map Map, in case of WMS layer is painted in a map. If not, returns null
*/
cercalia.WMS.prototype.getMap = function() {
return this.map_;
};
/**
* @return {string} WMS layer name
*/
cercalia.WMS.prototype.getName = function() {
return this.name_;
};
/**
* @return {Object} params Returns WMS 'params'
*/
cercalia.WMS.prototype.getParams = function() {
return this.params_;
};
/**
* Removes the object with all its attributes
*/
cercalia.WMS.prototype.destroy = function() {
if (this.map_) {
this.map_.getMap().removeLayer(this.layer_);
this.map_ = null;
this.name_ = null;
this.options_ = null;
this.params_ = null;
this.layer_ = null;
this.wmsSource_ = null;
}
};
/**
* @return {ol.source.TileWMS} WMS Source. OL3 Object <a href="http://openlayers.org/en/v3.6.0/apidoc/ol.source.TileWMS.html">ol.source.TileWMS</a>
*/
cercalia.WMS.prototype.getSource = function() {
return this.wmsSource_;
};
/**
* @return {ol.layer.Tile} WMS Source. OL3 Object <a href="http://openlayers.org/en/v3.6.0/apidoc/ol.layer.Tile.html">ol.layer.Tile</a>
*/
cercalia.WMS.prototype.getLayer = function() {
return this.layer_;
};
/**
* @return {string} ClassName from `cercalia.WMS` object.
*/
cercalia.WMS.prototype.getClass = function() {
return this.CLASS_NAME_;
};