Source: formats/kml/KmzFile.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 KmzFile
  30. */
  31. define([
  32. '../../util/jszip',
  33. '../../util/Promise',
  34. '../../util/WWUtil'
  35. ], function (JsZip,
  36. Promise,
  37. WWUtil) {
  38. "use strict";
  39. /**
  40. * Constructs KmzFile. It expects binary representation of the file and the Cache to be used.
  41. * @constructor
  42. * @alias KmzFile
  43. * @param binary {Object} Binary representation of the file.
  44. * @param fileCache {KmlFileCache} Cache for the contents of the file.
  45. */
  46. var KmzFile = function (binary, fileCache) {
  47. this._binary = binary;
  48. this._fileCache = fileCache;
  49. };
  50. /**
  51. * It loads the whole file and register all its contents. Version 0.1 uses file cache mechanism.
  52. * @returns Promise
  53. */
  54. KmzFile.prototype.load = function () {
  55. var self = this;
  56. var rootDocument = null;
  57. var rootFound = false;
  58. return new JsZip().loadAsync(this._binary).then(function (zipFile) {
  59. return Promise.all(Object.keys(zipFile.files).map(function (key) {
  60. return zipFile.files[key];
  61. }).map(function (file) {
  62. if (self.hasExtension("kml", file.name) && !rootFound) {
  63. rootFound = true;
  64. return file.async("text")
  65. .then(function (kmlTextRepresentation) {
  66. rootDocument = kmlTextRepresentation;
  67. });
  68. } else if(self.hasExtension("kml", file.name)) {
  69. return file.async("base64")
  70. .then(function (kmlTextRepresentation) {
  71. return new WorldWind.KmlFile(kmlTextRepresentation).then(function(kmlFile){
  72. self._fileCache.add('href;' + file.name, kmlFile);
  73. });
  74. });
  75. } else {
  76. return file.async("base64")
  77. .then(function (mediaFile) {
  78. var dataURI = "data:image/jpeg;base64," + mediaFile;
  79. self._fileCache.add('href;' + file.name, dataURI);
  80. });
  81. }
  82. }));
  83. }).then(function () {
  84. return rootDocument;
  85. });
  86. };
  87. /**
  88. * FOR INTERNAL USE ONLY.
  89. * Returns a value indicating whether the URL ends with the given extension.
  90. * @param url {String} Url to a file
  91. * @param extension {String} Extension of the file.
  92. * @returns {boolean} true if the extension matches otherwise false
  93. * @private
  94. */
  95. KmzFile.prototype.hasExtension = function (extension, url) {
  96. return WWUtil.endsWith(url, "." + extension);
  97. };
  98. return KmzFile;
  99. });