Source: cercalia.heatmap.js

/**
 * @class
 * @constructor
 * @param {cercaliax.HeatmapOptions} name Name of layer
 * 
 */
cercalia.Heatmap = function(options) {

  /**
   * @private
   * @type {string}
   */
  this.CLASS_NAME_ = "cercalia.Heatmap";

  /* OPTIONS */

  /**
   * kml or geojson
   * @type {string|undefined}
   * @private
   */
  this.format_ = options.format ? options.format : "kml";

  /**
   * @type {cercalia.Map}
   * @private
   */
  this.map_ = options.map ? options.map : null;

  /**
   * @type {string|undefined}
   * @private
   */
  this.name_ = options.name ? options.name : "HeatMap_" + new Date().getTime();

  /**
   * @type {string}
   * @private
   */
  this.url_ = options.url ? options.url : null;

  /**
   * @type {string|undefined}
   * @private
   */
  this.projection_ = options.projection ? options.projection : 'EPSG:3857';

  // Opciones del heat map

  this.initialOptions_ = {};

  /**
   * radius
   * @type {number|undefined}
   * @private
   */
  this.radius_ = options.radius ? parseInt(options.radius) : 5;

  /**
   * The color gradient of the heatmap, specified as an array of CSS color strings. Default is ['#00f', '#0ff', '#0f0', '#ff0', '#f00'].
   * @type {Array.<string>|undefined}
   * @private
   */
  this.initialOptions_.gradient = options.gradient ? options.gradient : ['#00f', '#0ff', '#0f0', '#ff0', '#f00'];

  /**
   * Opacity. 0-1. Default is 1.
   * @type {number|undefined}
   * @private
   */
  this.initialOptions_.opacity = options.opacity ? options.opacity : 1;

  /**
   * Blur size in pixels. Default is 15.
   * @type {number|undefined}
   * @private
   */
  this.initialOptions_.blur = options.blur ? options.blur : 15;

  /**
   * Saturation. Default is undefined.
   * @type {number|undefined}
   * @private
   */
  this.initialOptions_.saturation = options.saturation;

  /* Variables privadas */

  /**
   * @private
   * @type {ol.source.Vector}
   */
  //TODO OL3.5 https://github.com/openlayers/ol3/blob/master/changelog/upgrade-notes.md#new-vector-api
  this.source_ = null;

  /**
   * @private
   * @type {ol.layer.Heatmap}
   */
  this.layer_ = null

  // Iniciamos.
  this.inicialize_();
};

/**
 * Inicializa la classe.
 * @private
 */
cercalia.Heatmap.prototype.inicialize_ = function() {

  if (this.url_) {

    if (this.format_ == "kml") {

      // change 3.4.0 -> 3.6.0
      //this.source_ = new ol.source.KML({
      //    extractStyles: false,
      //    projection: this.projection_,
      //    url: this.url_
      //});

      this.source_ = new ol.source.Vector({
        format: new ol.format.KML({
          extractStyles: false
        }),
        projection: this.projection_,
        url: this.url_
      });



    } else if (this.format_ == "geojson") {

      // change 3.4.0 -> 3.6.0
      //this.source_ = new ol.source.GeoJSON({
      //    projection: this.projection_,
      //    url: this.url_
      //});

      this.source_ = new ol.source.Vector({
        projection: this.projection_,
        url: this.url_,
        format: new ol.format.GeoJSON()
      });

    } else {
      console.error("Unknow format");
      return null;
    }

    // Creamos el layer.
    var options = {};
    cercalia.jQuery.extend(options, this.initialOptions_, {
      source: this.source_,
      radius: this.radius_
    });
    this.layer_ = new ol.layer.Heatmap(options);

    // Set map si no es null.
    if (this.map_ != null) {
      this.setMap(this.map_);
    }

  } else {
    console.error("file can't not be null.");
    return null;
  }

};

/**
 * Le asignamos un mapa al objeto. En caso que exista se elimina en el mapa
 * existente y se pinta la capa WMS en el mapa pasado por parametro. Si el
 * parametro es 'null' se borra del mapa del que esta pintado
 * 
 * @param {cercalia.Map|null} map Mapa.
 * @param {number} position Posición zIndex en los layers.
 */
cercalia.Heatmap.prototype.setMap = function(map, position) {

  // Si ya habia mapa lo quitamos
  if (this.map_ != null) {

    this.map_.removeLayer(this.layer_);

  }

  if (map != null) {

    this.map_ = map;
    this.map_.addLayer(this.layer_);

  }

};


/**
 * @return {cercalia.Map|null} map Mapa si es que la capa WMS esta pintada en un mapa. Sino, devuelve null 
 */
cercalia.Heatmap.prototype.getMap = function() {
  return this.map_;
};


/**
 * Elimina el objeto junto a todos sus atributos
 */
cercalia.Heatmap.prototype.destroy = function() {
  if (this.map_) {
    this.map_.getMap().removeLayer(this.layer_);
    this.map_ = null;
    this.format_ = null;
    this.layer_ = null;
    this.name_ = null;
    this.source_ = null;
  }
};


/**
 * @return {ol.source.Vector} WMS Source. Objeto OL3 <a href="http://openlayers.org/en/v3.0.0/apidoc/ol.source.TileWMS.html">ol.source.TileWMS</a>
 */
cercalia.Heatmap.prototype.getSource = function() {
  return this.source_;
};

/**
 * @return {ol.layer.Tile} WMS Source. Objeto OL3 <a href="http://openlayers.org/en/v3.0.0/apidoc/ol.layer.Tile.html">ol.layer.Tile</a>
 */
cercalia.Heatmap.prototype.getLayer = function() {
  return this.layer_;
};

/**
 * @return {string} ClassName del objeto `cercalia.WMS`.
 */
cercalia.Heatmap.prototype.getClass = function() {
  return this.CLASS_NAME_;
};

/**
 * Get the radius of the layer.
 * @returns {number}
 */
cercalia.Heatmap.prototype.getRadius = function() {
  return this.radius_;
};

/**
 * Set the radius of the layer.
 * @param {number} radius The radius of the layer.
 */
cercalia.Heatmap.prototype.setRadius = function(radius) {
  if (this.radius_ != radius) {
    this.radius_ = radius;
    this.layer_.setRadius(radius);
  }
};

/**
 * Get the visibility of the layer.
 * @returns {boolean}
 */
cercalia.Heatmap.prototype.getVisible = function() {
  return this.layer_.getVisible();
};

/**
 * Set the visibility of the layer.
 * @param {boolean} visibility The visibility of the layer.
 */
cercalia.Heatmap.prototype.setVisible = function(visibility) {
  this.layer_.setVisible(visibility);
};

/**
 * Get the gradient of the layer.
 * @returns {Array.<string>}
 */
cercalia.Heatmap.prototype.getGradient = function() {
  this.layer_.getGradient();
};

/**
 * Set the gradient of the layer.
 * @param {Array.<string>} gradient The gradient of the layer.
 */
cercalia.Heatmap.prototype.setGradient = function(gradient) {
  this.initialOptions_.gradient = gradient;
  this.layer_.setGradient(gradient);
};

/**
 * Get the opacity of the layer.
 * @return {number}
 */
cercalia.Heatmap.prototype.getOpacity = function() {
  this.layer_.getOpacity();
};

/**
 * Set the opacity of the layer.
 * @param {number} opacity The opacity of the layer.
 */
cercalia.Heatmap.prototype.setOpacity = function(opacity) {
  this.initialOptions_.opacity = opacity;
  this.layer_.setOpacity(opacity);
};

/**
 * Get the saturation of the layer.
 * @return {number}
 */
cercalia.Heatmap.prototype.getSaturation = function() {
  this.layer_.getSaturation();
};

/**
 * Set the saturation of the layer.
 * @param {number} saturation The saturation of the layer.
 */
cercalia.Heatmap.prototype.setSaturation = function(saturation) {
  this.layer_.setSaturation(saturation);
};

/**
 * Get the blur of the layer.
 * @returns {number}
 */
cercalia.Heatmap.prototype.getBlur = function() {
  this.layer_.getBlur();
};

/**
 * Set the blur of the layer.
 * @param {number} blur The blur of the layer.
 */
cercalia.Heatmap.prototype.setBlur = function(blur) {
  this.initialOptions_.blur = blur;
  this.layer_.setBlur(blur);
};