Source: src/data/serializer/XYZ.js

  1. /* Copyright (c) 2015-present The Open Source Geospatial Foundation
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * A serializer for layers that have an `ol.source.XYZ` source.
  18. * Sources with an tileUrlFunction are currently not supported.
  19. *
  20. * @class GeoExt.data.serializer.XYZ
  21. */
  22. Ext.define(
  23. 'GeoExt.data.serializer.XYZ',
  24. {
  25. extend: 'GeoExt.data.serializer.Base',
  26. mixins: ['GeoExt.mixin.SymbolCheck'],
  27. symbols: [
  28. 'ol.layer.Base#getOpacity',
  29. 'ol.size.toSize',
  30. 'ol.source.XYZ',
  31. 'ol.source.XYZ#getTileGrid',
  32. 'ol.source.XYZ#getUrls',
  33. 'ol.tilegrid.TileGrid#getResolutions',
  34. 'ol.tilegrid.TileGrid#getTileSize',
  35. ],
  36. inheritableStatics: {
  37. /**
  38. *
  39. */
  40. allowedImageExtensions: ['png', 'jpg', 'gif'],
  41. /**
  42. * @inheritdoc
  43. */
  44. sourceCls: ol.source.XYZ,
  45. /**
  46. * @inheritdoc
  47. */
  48. validateSource: function (source) {
  49. if (!(source instanceof this.sourceCls)) {
  50. Ext.raise('Cannot serialize this source with this serializer');
  51. }
  52. if (source.getUrls() === null) {
  53. Ext.raise(
  54. 'Cannot serialize this source without an URL. ' +
  55. 'Usage of tileUrlFunction is not yet supported',
  56. );
  57. }
  58. },
  59. /**
  60. * @inheritdoc
  61. */
  62. serialize: function (layer, source) {
  63. this.validateSource(source);
  64. const tileGrid = source.getTileGrid();
  65. const serialized = {
  66. baseURL: source.getUrls()[0],
  67. opacity: layer.getOpacity(),
  68. imageExtension: this.getImageExtensionFromSource(source) || 'png',
  69. resolutions: tileGrid.getResolutions(),
  70. tileSize: ol.size.toSize(tileGrid.getTileSize()),
  71. type: 'OSM',
  72. };
  73. return serialized;
  74. },
  75. /**
  76. * Returns the file extension from the url and compares it to whitelist.
  77. * Sources with an tileUrlFunction are currently not supported.
  78. *
  79. * @private
  80. * @param {ol.source.XYZ} source An ol.source.XYZ.
  81. * @return {string} The fileExtension or `false` if none is found.
  82. */
  83. getImageExtensionFromSource: function (source) {
  84. const urls = source.getUrls();
  85. const url = urls ? urls[0] : '';
  86. const extension = url.substr(url.length - 3);
  87. if (
  88. Ext.isDefined(url) &&
  89. Ext.Array.contains(this.allowedImageExtensions, extension)
  90. ) {
  91. return extension;
  92. }
  93. Ext.raise('No url(s) supplied for ', source);
  94. return false;
  95. },
  96. },
  97. },
  98. function (cls) {
  99. // Register this serializer via the inherited method `register`.
  100. cls.register(cls);
  101. },
  102. );