<commit_before>goog.provide('app.LayerFactory');

goog.require('app');
goog.require('ngeo.DecorateLayer');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.source.ImageWMS');
goog.require('ol.source.WMTS');
goog.require('ol.tilegrid.WMTS');


/**
 * @typedef {function(Object):ol.layer.Layer}
 */
app.LayerFactory;


/**
 * @type {Object.<string, ol.layer.Layer>}
 */
app.layerCache = {};


/**
 * @private
 * @param {ngeo.DecorateLayer} ngeoDecorateLayer ngeo decorate layer service.
 * @return {Function}
 */
app.layerFactory_ = function(ngeoDecorateLayer) {
  /**
   * @param {Object} node Catalog tree node.
   * @return {ol.layer.Layer} OpenLayers layer.
   */
  return function(node) {
    if (!('type' in node)) {
      return null;
    }
    var type = node['type'];
    if ([type, node['name']].join('_') in app.layerCache) {
      return app.layerCache[type];
    }

    var layer;

    if (type.indexOf('WMS') != -1) {
      layer = wmsLayerFactory_(node);
    } else if (type == 'WMTS') {
      layer = wmtsLayerFactory_(node);
    } else {
      return null;
    }
    app.layerCache[[type, node['name']].join('_')] = layer;
    layer.set('label', node['name']);
    ngeoDecorateLayer(layer);
    return layer;
  };


  /**
   * @private
   * @param {Object} node Catalog tree node.
   * @return {ol.layer.Layer} OpenLayers layer.
   */
  function wmtsLayerFactory_(node) {

    var projection = ol.proj.get('EPSG:3857');

    return new ol.layer.Tile({
      source: new ol.source.WMTS({
        url: 'http://wmts.geoportail.lu/mapproxy_4_v3/' +
            'wmts/{Layer}/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}.png',
        layer: node['name'],
        // FIXME should be replaced by node.matrixSet
        // but there's a typo in the database for this attribute
        matrixSet: 'GLOBAL_WEBMERCATOR',
        requestEncoding: /** @type {ol.source.WMTSRequestEncoding} */ ('REST'),
        format: 'image/png',
        projection: projection,
        tileGrid: new ol.tilegrid.WMTS({
          origin: [-20037508.3428, 20037508.3428],
          resolutions: [156543.033928, 78271.516964,
            39135.758482, 19567.879241, 9783.9396205,
            4891.96981025, 2445.98490513, 1222.99245256,
            611.496226281, 305.748113141, 152.87405657,
            76.4370282852, 38.2185141426, 19.1092570713,
            9.55462853565, 4.77731426782, 2.38865713391,
            1.19432856696, 0.597164283478, 0.298582141739],
          matrixIds: [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19
          ]
        }),
        style: 'default'
      })
    });
  }


  /**
   * @private
   * @param {Object} node Catalog tree node.
   * @return {ol.layer.Layer} OpenLayers layer.
   */
  function wmsLayerFactory_(node) {
    var type = node['type'];
    var layer;
    var source;
    if (type == 'internal WMS') {
      source = new ol.source.ImageWMS({
        // FIXME use the internalWmsUrl constant instead
        url: 'http://devv3.geoportail.lu/main/wsgi/wms',
        params: {
          'LAYERS': node['name']
        }
      });
    } else if (type == 'external WMS') {
      source = new ol.source.ImageWMS({
        url: node.url,
        params: {
          'LAYERS': node['name']
        }
      });
    }

    layer = new ol.layer.Image({
      source: /** @type {ol.source.Image} */ (source)
    });
    return layer;
  }
};



app.module.factory('ngeoLayertreeLayerFactory', app.layerFactory_);<commit_msg>Refactoring of the layer factory services<commit_after>goog.provide('app.GetWmsLayer');
goog.provide('app.GetWmtsLayer');
goog.provide('app.LayerFactory');

goog.require('app');
goog.require('ngeo.DecorateLayer');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.source.ImageWMS');
goog.require('ol.source.WMTS');
goog.require('ol.tilegrid.WMTS');


/**
 * @typedef {function(string):ol.layer.Tile}
 */
app.GetWmtsLayer;


/**
 * @typedef {function(string, string=):ol.layer.Image}
 */
app.GetWmsLayer;


/**
 * @typedef {function(Object):ol.layer.Layer}
 */
app.LayerFactory;


/**
 * @const
 * @type {Object.<string, ol.layer.Layer>}
 * @private
 */
app.layerCache_ = {};


/**
 * @param {ngeo.DecorateLayer} ngeoDecorateLayer ngeo decorate layer service.
 * @return {app.GetWmtsLayer} The getWmtsLayer function.
 * @private
 * @ngInject
 */
app.getWmtsLayer_ = function(ngeoDecorateLayer) {
  return getWmtsLayer;

  /**
   * @param {string} name WMTS layer name.
   * @return {ol.layer.Tile} The layer.
   */
  function getWmtsLayer(name) {

    var layer = new ol.layer.Tile({
      source: new ol.source.WMTS({
        url: 'http://wmts.geoportail.lu/mapproxy_4_v3/' +
            'wmts/{Layer}/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}.png',
        layer: name,
        matrixSet: 'GLOBAL_WEBMERCATOR',
        requestEncoding: ol.source.WMTSRequestEncoding.REST,
        format: 'image/png',
        projection: ol.proj.get('EPSG:3857'),
        tileGrid: new ol.tilegrid.WMTS({
          origin: [-20037508.3428, 20037508.3428],
          resolutions: [156543.033928, 78271.516964,
            39135.758482, 19567.879241, 9783.9396205,
            4891.96981025, 2445.98490513, 1222.99245256,
            611.496226281, 305.748113141, 152.87405657,
            76.4370282852, 38.2185141426, 19.1092570713,
            9.55462853565, 4.77731426782, 2.38865713391,
            1.19432856696, 0.597164283478, 0.298582141739],
          matrixIds: [
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19
          ]
        }),
        style: 'default'
      })
    });

    layer.set('label', name);
    ngeoDecorateLayer(layer);

    return layer;
  }
};


app.module.factory('appGetWmtsLayer', app.getWmtsLayer_);


/**
 * @param {ngeo.DecorateLayer} ngeoDecorateLayer ngeo decorate layer service.
 * @return {app.GetWmsLayer} The getWmsLayer function.
 * @private
 * @ngInject
 */
app.getWmsLayer_ = function(ngeoDecorateLayer) {
  return getWmsLayer;

  /**
   * @param {string} name WMS layer name.
   * @param {string=} opt_url WMS URL.
   * @return {ol.layer.Image} The layer.
   */
  function getWmsLayer(name, opt_url) {
    var url = goog.isDef(opt_url) ?
        opt_url : 'http://devv3.geoportail.lu/main/wsgi/wms';
    var layer = new ol.layer.Image({
      source: new ol.source.ImageWMS({
        url: url,
        params: {
          'LAYERS': name
        }
      })
    });

    layer.set('label', name);
    ngeoDecorateLayer(layer);

    return layer;
  }
};


app.module.factory('appGetWmsLayer', app.getWmsLayer_);


/**
 * The function returning the function used as the ngeoLayertreeLayerFactory,
 * which should be defined for the ngeoLayertreenode directive.
 *
 * @param {app.GetWmtsLayer} appGetWmtsLayer The getWmtsLayer function.
 * @param {app.GetWmsLayer} appGetWmsLayer The getWmsLayer function.
 * @return {app.LayerFactory} The layer factory.
 * @private
 * @ngInject
 */
app.createLayerFactory_ = function(appGetWmtsLayer, appGetWmsLayer) {
  return layerFactory_;

  /**
   * @param {Object} node Catalog tree node.
   * @return {ol.layer.Layer} OpenLayers layer.
   */
  function layerFactory_(node) {
    var layer, layerCacheKey, type;
    if (!('type' in node)) {
      return null;
    }
    type = node['type'];
    layerCacheKey = type + '_' + node['name'];
    if (layerCacheKey in app.layerCache_) {
      return app.layerCache_[layerCacheKey];
    }
    if (type.indexOf('WMS') != -1) {
      layer = appGetWmsLayer(node['name'], node['url']);
    } else if (type == 'WMTS') {
      layer = appGetWmtsLayer(node['name']);
    } else {
      return null;
    }
    goog.asserts.assert(goog.isDefAndNotNull(layer));
    app.layerCache_[layerCacheKey] = layer;
    return layer;
  }
};



app.module.factory('ngeoLayertreeLayerFactory', app.createLayerFactory_);
{'fraction_non_alphanumeric': 0.15585562847046946, 'fraction_numerical': 0.07546693589096416, 'mean_word_length': 2.7382075471698113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7049929', 'n_tokens_mistral': 3250, 'n_tokens_neox': 2791, 'n_words': 537}