Source: layer/OpenStreetMapImageLayer.js

  1. /*
  2. * Copyright 2003-2006, 2009, 2017, 2020 United States Government, as represented
  3. * by the Administrator of the National Aeronautics and Space Administration.
  4. * All rights reserved.
  5. *
  6. * The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
  7. * Version 2.0 (the "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License
  9. * at http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. *
  16. * NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
  17. * software:
  18. *
  19. * ES6-Promise – under MIT License
  20. * libtess.js – SGI Free Software License B
  21. * Proj4 – under MIT License
  22. * JSZip – under MIT License
  23. *
  24. * A complete listing of 3rd Party software notices and licenses included in
  25. * WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
  26. * PDF found in code directory.
  27. */
  28. /**
  29. * @exports OpenStreetMapImageLayer
  30. */
  31. define([
  32. '../util/Color',
  33. '../layer/Layer',
  34. '../util/Logger',
  35. '../ogc/wmts/WmtsCapabilities',
  36. '../layer/WmtsLayer'
  37. ],
  38. function (Color,
  39. Layer,
  40. Logger,
  41. WmtsCapabilities,
  42. WmtsLayer) {
  43. "use strict";
  44. /**
  45. * Constructs an Open Street Map layer.
  46. * @alias OpenStreetMapImageLayer
  47. * @constructor
  48. * @augments WmtsLayer
  49. * @classdesc Provides a layer that shows Open Street Map imagery.
  50. *
  51. * @param {String} displayName This layer's display name. "Open Street Map" if this parameter is
  52. * null or undefined.
  53. */
  54. var OpenStreetMapImageLayer = function (displayName) {
  55. Layer.call(this, this.displayName);
  56. this.displayName = displayName || "Open Street Map";
  57. this.layer = null;
  58. this.xhr = null;
  59. // TODO: Picking is enabled as a temporary measure for screen credit hyperlinks to work (see Layer.render)
  60. this.pickEnabled = true;
  61. };
  62. OpenStreetMapImageLayer.prototype = Object.create(Layer.prototype);
  63. OpenStreetMapImageLayer.prototype.doRender = function (dc) {
  64. this.configureLayer(dc);
  65. if (this.layer) {
  66. this.layer.opacity = this.opacity;
  67. this.layer.doRender(dc);
  68. this.inCurrentFrame = this.layer.inCurrentFrame;
  69. // Add a screen credit to attribute the data source to OSM and EOX
  70. // The pattern for this attribute is described in the WMTS Capabilities document and demonstrated at EOX
  71. // Maps site: http://maps.eox.at/
  72. if (this.inCurrentFrame) {
  73. dc.screenCreditController.addCredit("OpenStreetMap ©", Color.DARK_GRAY);
  74. dc.screenCreditController.addCredit("EOX.at ©", Color.DARK_GRAY);
  75. }
  76. }
  77. };
  78. OpenStreetMapImageLayer.prototype.configureLayer = function (dc) {
  79. if (!this.xhr) {
  80. var self = this;
  81. var canvas = dc.currentGlContext.canvas;
  82. this.xhr = new XMLHttpRequest();
  83. this.xhr.open("GET", "https://tiles.maps.eox.at/wmts/1.0.0/WMTSCapabilities.xml", true);
  84. this.xhr.onreadystatechange = function () {
  85. if (self.xhr.readyState === 4) {
  86. if (self.xhr.status === 200) {
  87. // Create a layer from the WMTS capabilities.
  88. var wmtsCapabilities = new WmtsCapabilities(self.xhr.responseXML);
  89. var wmtsLayerCapabilities = wmtsCapabilities.getLayer("osm");
  90. var wmtsConfig = WmtsLayer.formLayerConfiguration(wmtsLayerCapabilities);
  91. wmtsConfig.title = self.displayName;
  92. self.layer = new WmtsLayer(wmtsConfig);
  93. // Send an event to request a redraw.
  94. var e = document.createEvent('Event');
  95. e.initEvent(WorldWind.REDRAW_EVENT_TYPE, true, true);
  96. canvas.dispatchEvent(e);
  97. } else {
  98. Logger.log(Logger.LEVEL_WARNING,
  99. "OSM retrieval failed (" + xhr.statusText + "): " + url);
  100. }
  101. }
  102. };
  103. this.xhr.onerror = function () {
  104. Logger.log(Logger.LEVEL_WARNING, "OSM retrieval failed: " + url);
  105. };
  106. this.xhr.ontimeout = function () {
  107. Logger.log(Logger.LEVEL_WARNING, "OSM retrieval timed out: " + url);
  108. };
  109. this.xhr.send(null);
  110. }
  111. };
  112. return OpenStreetMapImageLayer;
  113. }
  114. );