<commit_before>goog.provide('lux.PrintManager');

goog.require('ol');
goog.require('ol.color');
goog.require('ol.format.GeoJSON');
goog.require('ol.geom.GeometryType');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.math');
goog.require('ol.size');
goog.require('ol.source.ImageWMS');
goog.require('ol.source.TileWMS');
goog.require('ol.source.Vector');
goog.require('ol.source.WMTS');
goog.require('ol.style.Circle');
goog.require('ol.style.RegularShape');
goog.require('ol.style.Style');
goog.require('ol.tilegrid.WMTS');
goog.require('ol.obj');
goog.require('ol.Feature');
goog.require('ol.geom.Point');
goog.require('ol.style.Icon');
goog.require('ol.layer.Group');
goog.require('lux.MapBoxLayer');


/**
 * @classdesc
 * @param {string} url URL to MapFish print web service.
 * @param {lux.Map} map Map object to print.
 * @constructor
 * @export
 */
lux.PrintManager = function(url, map) {
  /**
   * @type {lux.Map}
   * @private
   */
  this.map_ = map;

  /**
   * @type {string}
   * @private
   */
  this.url_ = url;

  /**
   * @type {number}
   * @private
   */
  this.dpi_ = 127;

  /**
   * @type {number}
   * @private
   */
  this.scale_ = 1500;
};

/**
 * @enum {string}
 */
lux.PrintStyleType = {
  LINE_STRING: 'LineString',
  POINT: 'Point',
  POLYGON: 'Polygon'
};


/**
 * @type {Object.<ol.geom.GeometryType, lux.PrintStyleType>}
 * @private
 */
lux.PrintStyleTypes_ = {};

lux.PrintStyleTypes_[ol.geom.GeometryType.LINE_STRING] =
    lux.PrintStyleType.LINE_STRING;
lux.PrintStyleTypes_[ol.geom.GeometryType.POINT] =
    lux.PrintStyleType.POINT;
lux.PrintStyleTypes_[ol.geom.GeometryType.POLYGON] =
    lux.PrintStyleType.POLYGON;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_LINE_STRING] =
    lux.PrintStyleType.LINE_STRING;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_POINT] =
    lux.PrintStyleType.POINT;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_POLYGON] =
    lux.PrintStyleType.POLYGON;

/**
 * @private
 */
lux.PrintManager.FEAT_STYLE_PROP_PREFIX_ = '_ngeo_style_';

/**
 * @const
 * @type {Array.<string>}
*/
lux.PrintManager.LAYOUTS = [
  'A4 landscape',
  'A4 portrait',
  'A3 landscape',
  'A3 portrait',
  'A2 landscape',
  'A2 portrait',
  'A1 landscape',
  'A1 portrait',
  'A0 landscape',
  'A0 portrait'
];

/**
 * Cancel a report.
 * @param {string} ref Print report reference.
 * @return {Promise} HTTP promise.
 * @export
 */
lux.PrintManager.prototype.cancel = function(ref) {
  return fetch(this.url_ + '/cancel/' + ref, {
    method: 'DELTE'
  });
};

/**
 * Create a report specification.
 * @param {number} scale Scale.
 * @param {number} dpi DPI.
 * @param {string} layout Layout.
 * @param {string} format Formats.
 * @param {Object.<string, *>} customAttributes Custom attributes.
 * @return {MapFishPrintSpec} The print spec.
 * @export
 */
lux.PrintManager.prototype.createSpec = function(
    scale, dpi, layout, format, customAttributes) {
  this.dpi_ = dpi;
  this.scale_ = scale;
  var specMap = /** @type {MapFishPrintMap} */ ({
    dpi: dpi,
    rotation: /** number */ (customAttributes['rotation'])
  });

  this.encodeMap_(scale, specMap);

  var attributes = /** @type {!MapFishPrintAttributes} */ ({
    map: specMap
  });
  ol.obj.assign(attributes, customAttributes);

  var spec = /** @type {MapFishPrintSpec} */ ({
    attributes: attributes,
    format: format,
    layout: layout
  });

  return spec;
};

lux.PrintManager.prototype.encodeXYZLayer_ = function(arr, url) {
  // https://vectortiles.geoportail.lu/styles/roadmap_jsapi/{z}/{x}/{y}.png
  const i = url.indexOf('/{z}/{x}/{y}');
  const j = url.lastIndexOf('.');
  if (i === -1 || j === -1) {
    return;
  }
  const baseURL = url.substr(0, i);
  const imageExtension = url.substr(j + 1);
  const object = {
    baseURL: baseURL,
    type: "OSM",
    'imageExtension': imageExtension
  };
  arr.push(object);
}

/**
 * @param {number} scale Scale.
 * @param {MapFishPrintMap} object Object.
 * @private
 */
lux.PrintManager.prototype.encodeMap_ = function(scale, object) {
  var view = this.map_.getView();
  var viewCenter = view.getCenter();
  var viewProjection = view.getProjection();
  var viewResolution = view.getResolution();
  var viewRotation = object.rotation || ol.math.toDegrees(view.getRotation());

  goog.asserts.assert(viewCenter !== undefined);
  goog.asserts.assert(viewProjection !== undefined);<commit_msg>Use wms<commit_after>goog.provide('lux.PrintManager');

goog.require('ol');
goog.require('ol.color');
goog.require('ol.format.GeoJSON');
goog.require('ol.geom.GeometryType');
goog.require('ol.layer.Image');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.math');
goog.require('ol.size');
goog.require('ol.source.ImageWMS');
goog.require('ol.source.TileWMS');
goog.require('ol.source.Vector');
goog.require('ol.source.WMTS');
goog.require('ol.style.Circle');
goog.require('ol.style.RegularShape');
goog.require('ol.style.Style');
goog.require('ol.tilegrid.WMTS');
goog.require('ol.obj');
goog.require('ol.Feature');
goog.require('ol.geom.Point');
goog.require('ol.style.Icon');
goog.require('ol.layer.Group');
goog.require('lux.MapBoxLayer');


/**
 * @classdesc
 * @param {string} url URL to MapFish print web service.
 * @param {lux.Map} map Map object to print.
 * @constructor
 * @export
 */
lux.PrintManager = function(url, map) {
  /**
   * @type {lux.Map}
   * @private
   */
  this.map_ = map;

  /**
   * @type {string}
   * @private
   */
  this.url_ = url;

  /**
   * @type {number}
   * @private
   */
  this.dpi_ = 127;

  /**
   * @type {number}
   * @private
   */
  this.scale_ = 1500;
};

/**
 * @enum {string}
 */
lux.PrintStyleType = {
  LINE_STRING: 'LineString',
  POINT: 'Point',
  POLYGON: 'Polygon'
};


/**
 * @type {Object.<ol.geom.GeometryType, lux.PrintStyleType>}
 * @private
 */
lux.PrintStyleTypes_ = {};

lux.PrintStyleTypes_[ol.geom.GeometryType.LINE_STRING] =
    lux.PrintStyleType.LINE_STRING;
lux.PrintStyleTypes_[ol.geom.GeometryType.POINT] =
    lux.PrintStyleType.POINT;
lux.PrintStyleTypes_[ol.geom.GeometryType.POLYGON] =
    lux.PrintStyleType.POLYGON;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_LINE_STRING] =
    lux.PrintStyleType.LINE_STRING;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_POINT] =
    lux.PrintStyleType.POINT;
lux.PrintStyleTypes_[ol.geom.GeometryType.MULTI_POLYGON] =
    lux.PrintStyleType.POLYGON;

/**
 * @private
 */
lux.PrintManager.FEAT_STYLE_PROP_PREFIX_ = '_ngeo_style_';

/**
 * @const
 * @type {Array.<string>}
*/
lux.PrintManager.LAYOUTS = [
  'A4 landscape',
  'A4 portrait',
  'A3 landscape',
  'A3 portrait',
  'A2 landscape',
  'A2 portrait',
  'A1 landscape',
  'A1 portrait',
  'A0 landscape',
  'A0 portrait'
];

/**
 * Cancel a report.
 * @param {string} ref Print report reference.
 * @return {Promise} HTTP promise.
 * @export
 */
lux.PrintManager.prototype.cancel = function(ref) {
  return fetch(this.url_ + '/cancel/' + ref, {
    method: 'DELTE'
  });
};

/**
 * Create a report specification.
 * @param {number} scale Scale.
 * @param {number} dpi DPI.
 * @param {string} layout Layout.
 * @param {string} format Formats.
 * @param {Object.<string, *>} customAttributes Custom attributes.
 * @return {MapFishPrintSpec} The print spec.
 * @export
 */
lux.PrintManager.prototype.createSpec = function(
    scale, dpi, layout, format, customAttributes) {
  this.dpi_ = dpi;
  this.scale_ = scale;
  var specMap = /** @type {MapFishPrintMap} */ ({
    dpi: dpi,
    rotation: /** number */ (customAttributes['rotation'])
  });

  this.encodeMap_(scale, specMap);

  var attributes = /** @type {!MapFishPrintAttributes} */ ({
    map: specMap
  });
  ol.obj.assign(attributes, customAttributes);

  var spec = /** @type {MapFishPrintSpec} */ ({
    attributes: attributes,
    format: format,
    layout: layout
  });

  return spec;
};

lux.PrintManager.prototype.encodeXYZLayer_ = function(arr, url) {
  // https://vectortiles.geoportail.lu/styles/roadmap_jsapi/{z}/{x}/{y}.png
  const i = url.indexOf('/{z}/{x}/{y}');
  const j = url.lastIndexOf('.');
  if (i === -1 || j === -1) {
    return;
  }
  const baseURL = url.substr(0, i);
  const imageExtension = url.substr(j + 1);
  const styleName = baseURL.substr(baseURL.lastIndexOf('/styles/') + '/styles/'.length);
  if (styleName === 'topomap' || styleName === 'roadmap' ||
      styleName === 'topomap_gray' || styleName === 'roadmap_jsapi') {
    const object = {
      'baseURL': 'https://wms.geoportail.lu/opendata/service',
      'imageFormat': 'image/' + imageExtension,
      'layers': [styleName === 'roadmap_jsapi'?'roadmap_luxonly':styleName],
      'customParams': {
        'TRANSPARENT': true,
        'MAP_RESOLUTION': 127
      },
      'type': 'wms',
      'opacity': 1,
      'version': '1.1.1',
      'useNativeAngle': true
    }
    arr.push(object);
  } else {
    const object = {
      baseURL,
      type: "OSM",
      'imageExtension': imageExtension
    };
    arr.push(object);
  }
};

/**
 * @param {number} scale Scale.
 * @param {MapFishPrintMap} object Object.
 * @private
 */
lux.PrintManager.prototype.encodeMap_ = function(scale, object) {
  var view = this.map_.getView();
  var viewCenter = view.getCenter();
  var viewProjection = view.getProjection();
  var viewResolution = view.getResolution();
  var viewRotation = object.rotation || ol.math.toDegrees(view.getRotation());

  goog.asserts.assert(viewCenter !== undefined);
  goog.asserts.assert(viewProjection !== undefined);
{'fraction_non_alphanumeric': 0.18001677500524219, 'fraction_numerical': 0.005137345355420424, 'mean_word_length': 3.8128153380423813, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2629972', 'n_tokens_mistral': 3513, 'n_tokens_neox': 3356, 'n_words': 595}