Source: DataSources/KmlDataSource.js

  1. /*global define*/
  2. define([
  3. '../Core/AssociativeArray',
  4. '../Core/BoundingRectangle',
  5. '../Core/Cartesian2',
  6. '../Core/Cartesian3',
  7. '../Core/Cartographic',
  8. '../Core/ClockRange',
  9. '../Core/ClockStep',
  10. '../Core/Color',
  11. '../Core/createGuid',
  12. '../Core/defaultValue',
  13. '../Core/defined',
  14. '../Core/defineProperties',
  15. '../Core/DeveloperError',
  16. '../Core/Ellipsoid',
  17. '../Core/Event',
  18. '../Core/getAbsoluteUri',
  19. '../Core/getExtensionFromUri',
  20. '../Core/getFilenameFromUri',
  21. '../Core/Iso8601',
  22. '../Core/joinUrls',
  23. '../Core/JulianDate',
  24. '../Core/loadBlob',
  25. '../Core/loadXML',
  26. '../Core/Math',
  27. '../Core/NearFarScalar',
  28. '../Core/PinBuilder',
  29. '../Core/PolygonHierarchy',
  30. '../Core/Rectangle',
  31. '../Core/RuntimeError',
  32. '../Core/TimeInterval',
  33. '../Core/TimeIntervalCollection',
  34. '../Scene/HeightReference',
  35. '../Scene/HorizontalOrigin',
  36. '../Scene/LabelStyle',
  37. '../Scene/SceneMode',
  38. '../ThirdParty/Autolinker',
  39. '../ThirdParty/Uri',
  40. '../ThirdParty/when',
  41. '../ThirdParty/zip',
  42. './BillboardGraphics',
  43. './CompositePositionProperty',
  44. './ConstantPositionProperty',
  45. './CorridorGraphics',
  46. './DataSource',
  47. './DataSourceClock',
  48. './Entity',
  49. './EntityCluster',
  50. './EntityCollection',
  51. './LabelGraphics',
  52. './PathGraphics',
  53. './PolygonGraphics',
  54. './PolylineGraphics',
  55. './PositionPropertyArray',
  56. './RectangleGraphics',
  57. './ReferenceProperty',
  58. './SampledPositionProperty',
  59. './ScaledPositionProperty',
  60. './TimeIntervalCollectionProperty',
  61. './WallGraphics'
  62. ], function(
  63. AssociativeArray,
  64. BoundingRectangle,
  65. Cartesian2,
  66. Cartesian3,
  67. Cartographic,
  68. ClockRange,
  69. ClockStep,
  70. Color,
  71. createGuid,
  72. defaultValue,
  73. defined,
  74. defineProperties,
  75. DeveloperError,
  76. Ellipsoid,
  77. Event,
  78. getAbsoluteUri,
  79. getExtensionFromUri,
  80. getFilenameFromUri,
  81. Iso8601,
  82. joinUrls,
  83. JulianDate,
  84. loadBlob,
  85. loadXML,
  86. CesiumMath,
  87. NearFarScalar,
  88. PinBuilder,
  89. PolygonHierarchy,
  90. Rectangle,
  91. RuntimeError,
  92. TimeInterval,
  93. TimeIntervalCollection,
  94. HeightReference,
  95. HorizontalOrigin,
  96. LabelStyle,
  97. SceneMode,
  98. Autolinker,
  99. Uri,
  100. when,
  101. zip,
  102. BillboardGraphics,
  103. CompositePositionProperty,
  104. ConstantPositionProperty,
  105. CorridorGraphics,
  106. DataSource,
  107. DataSourceClock,
  108. Entity,
  109. EntityCluster,
  110. EntityCollection,
  111. LabelGraphics,
  112. PathGraphics,
  113. PolygonGraphics,
  114. PolylineGraphics,
  115. PositionPropertyArray,
  116. RectangleGraphics,
  117. ReferenceProperty,
  118. SampledPositionProperty,
  119. ScaledPositionProperty,
  120. TimeIntervalCollectionProperty,
  121. WallGraphics) {
  122. 'use strict';
  123. // IE 8 doesn't have a DOM parser and can't run Cesium anyway, so just bail.
  124. if (typeof DOMParser === 'undefined') {
  125. return {};
  126. }
  127. //This is by no means an exhaustive list of MIME types.
  128. //The purpose of this list is to be able to accurately identify content embedded
  129. //in KMZ files. Eventually, we can make this configurable by the end user so they can add
  130. //there own content types if they have KMZ files that require it.
  131. var MimeTypes = {
  132. avi : "video/x-msvideo",
  133. bmp : "image/bmp",
  134. bz2 : "application/x-bzip2",
  135. chm : "application/vnd.ms-htmlhelp",
  136. css : "text/css",
  137. csv : "text/csv",
  138. doc : "application/msword",
  139. dvi : "application/x-dvi",
  140. eps : "application/postscript",
  141. flv : "video/x-flv",
  142. gif : "image/gif",
  143. gz : "application/x-gzip",
  144. htm : "text/html",
  145. html : "text/html",
  146. ico : "image/vnd.microsoft.icon",
  147. jnlp : "application/x-java-jnlp-file",
  148. jpeg : "image/jpeg",
  149. jpg : "image/jpeg",
  150. m3u : "audio/x-mpegurl",
  151. m4v : "video/mp4",
  152. mathml : "application/mathml+xml",
  153. mid : "audio/midi",
  154. midi : "audio/midi",
  155. mov : "video/quicktime",
  156. mp3 : "audio/mpeg",
  157. mp4 : "video/mp4",
  158. mp4v : "video/mp4",
  159. mpeg : "video/mpeg",
  160. mpg : "video/mpeg",
  161. odp : "application/vnd.oasis.opendocument.presentation",
  162. ods : "application/vnd.oasis.opendocument.spreadsheet",
  163. odt : "application/vnd.oasis.opendocument.text",
  164. ogg : "application/ogg",
  165. pdf : "application/pdf",
  166. png : "image/png",
  167. pps : "application/vnd.ms-powerpoint",
  168. ppt : "application/vnd.ms-powerpoint",
  169. ps : "application/postscript",
  170. qt : "video/quicktime",
  171. rdf : "application/rdf+xml",
  172. rss : "application/rss+xml",
  173. rtf : "application/rtf",
  174. svg : "image/svg+xml",
  175. swf : "application/x-shockwave-flash",
  176. text : "text/plain",
  177. tif : "image/tiff",
  178. tiff : "image/tiff",
  179. txt : "text/plain",
  180. wav : "audio/x-wav",
  181. wma : "audio/x-ms-wma",
  182. wmv : "video/x-ms-wmv",
  183. xml : "application/xml",
  184. zip : "application/zip",
  185. detectFromFilename : function(filename) {
  186. var ext = filename.toLowerCase();
  187. ext = getExtensionFromUri(ext);
  188. return MimeTypes[ext];
  189. }
  190. };
  191. var parser = new DOMParser();
  192. var autolinker = new Autolinker({
  193. stripPrefix : false,
  194. twitter : false,
  195. email : false,
  196. replaceFn : function(linker, match) {
  197. if (!match.protocolUrlMatch) {
  198. //Prevent matching of non-explicit urls.
  199. //i.e. foo.id won't match but http://foo.id will
  200. return false;
  201. }
  202. }
  203. });
  204. var BILLBOARD_SIZE = 32;
  205. var BILLBOARD_NEAR_DISTANCE = 2414016;
  206. var BILLBOARD_NEAR_RATIO = 1.0;
  207. var BILLBOARD_FAR_DISTANCE = 1.6093e+7;
  208. var BILLBOARD_FAR_RATIO = 0.1;
  209. function isZipFile(blob) {
  210. var magicBlob = blob.slice(0, Math.min(4, blob.size));
  211. var deferred = when.defer();
  212. var reader = new FileReader();
  213. reader.addEventListener('load', function() {
  214. deferred.resolve(new DataView(reader.result).getUint32(0, false) === 0x504b0304);
  215. });
  216. reader.addEventListener('error', function() {
  217. deferred.reject(reader.error);
  218. });
  219. reader.readAsArrayBuffer(magicBlob);
  220. return deferred.promise;
  221. }
  222. function readBlobAsText(blob) {
  223. var deferred = when.defer();
  224. var reader = new FileReader();
  225. reader.addEventListener('load', function() {
  226. deferred.resolve(reader.result);
  227. });
  228. reader.addEventListener('error', function() {
  229. deferred.reject(reader.error);
  230. });
  231. reader.readAsText(blob);
  232. return deferred.promise;
  233. }
  234. function loadXmlFromZip(reader, entry, uriResolver, deferred) {
  235. entry.getData(new zip.TextWriter(), function(text) {
  236. uriResolver.kml = parser.parseFromString(text, 'application/xml');
  237. deferred.resolve();
  238. });
  239. }
  240. function loadDataUriFromZip(reader, entry, uriResolver, deferred) {
  241. var mimeType = defaultValue(MimeTypes.detectFromFilename(entry.filename), 'application/octet-stream');
  242. entry.getData(new zip.Data64URIWriter(mimeType), function(dataUri) {
  243. uriResolver[entry.filename] = dataUri;
  244. deferred.resolve();
  245. });
  246. }
  247. function replaceAttributes(div, elementType, attributeName, uriResolver) {
  248. var keys = uriResolver.keys;
  249. var baseUri = new Uri('.');
  250. var elements = div.querySelectorAll(elementType);
  251. for (var i = 0; i < elements.length; i++) {
  252. var element = elements[i];
  253. var value = element.getAttribute(attributeName);
  254. var uri = new Uri(value).resolve(baseUri).toString();
  255. var index = keys.indexOf(uri);
  256. if (index !== -1) {
  257. var key = keys[index];
  258. element.setAttribute(attributeName, uriResolver[key]);
  259. if (elementType === 'a' && element.getAttribute('download') === null) {
  260. element.setAttribute('download', key);
  261. }
  262. }
  263. }
  264. }
  265. function proxyUrl(url, proxy) {
  266. if (defined(proxy)) {
  267. if (new Uri(url).isAbsolute()) {
  268. url = proxy.getURL(url);
  269. }
  270. }
  271. return url;
  272. }
  273. // an optional context is passed to allow for some malformed kmls (those with multiple geometries with same ids) to still parse
  274. // correctly, as they do in Google Earth.
  275. function createEntity(node, entityCollection, context) {
  276. var id = queryStringAttribute(node, 'id');
  277. id = defined(id) && id.length !== 0 ? id : createGuid();
  278. if(defined(context)){
  279. id = context + id;
  280. }
  281. // If we have a duplicate ID just generate one.
  282. // This isn't valid KML but Google Earth handles this case.
  283. var entity = entityCollection.getById(id);
  284. if (defined(entity)) {
  285. id = createGuid();
  286. if(defined(context)){
  287. id = context + id;
  288. }
  289. }
  290. entity = entityCollection.add(new Entity({id : id}));
  291. if (!defined(entity.kml)) {
  292. entity.addProperty('kml');
  293. entity.kml = new KmlFeatureData();
  294. }
  295. return entity;
  296. }
  297. function isExtrudable(altitudeMode, gxAltitudeMode) {
  298. return altitudeMode === 'absolute' || altitudeMode === 'relativeToGround' || gxAltitudeMode === 'relativeToSeaFloor';
  299. }
  300. function readCoordinate(value) {
  301. //Google Earth treats empty or missing coordinates as 0.
  302. if (!defined(value)) {
  303. return Cartesian3.fromDegrees(0, 0, 0);
  304. }
  305. var digits = value.match(/[^\s,\n]+/g);
  306. if (!defined(digits)) {
  307. return Cartesian3.fromDegrees(0, 0, 0);
  308. }
  309. var longitude = parseFloat(digits[0]);
  310. var latitude = parseFloat(digits[1]);
  311. var height = parseFloat(digits[2]);
  312. longitude = isNaN(longitude) ? 0.0 : longitude;
  313. latitude = isNaN(latitude) ? 0.0 : latitude;
  314. height = isNaN(height) ? 0.0 : height;
  315. return Cartesian3.fromDegrees(longitude, latitude, height);
  316. }
  317. function readCoordinates(element) {
  318. if (!defined(element)) {
  319. return undefined;
  320. }
  321. var tuples = element.textContent.match(/[^\s\n]+/g);
  322. if (!defined(tuples)) {
  323. return undefined;
  324. }
  325. var length = tuples.length;
  326. var result = new Array(length);
  327. var resultIndex = 0;
  328. for (var i = 0; i < length; i++) {
  329. result[resultIndex++] = readCoordinate(tuples[i]);
  330. }
  331. return result;
  332. }
  333. var kmlNamespaces = [null, undefined, 'http://www.opengis.net/kml/2.2', 'http://earth.google.com/kml/2.2', 'http://earth.google.com/kml/2.1', 'http://earth.google.com/kml/2.0'];
  334. var gxNamespaces = ['http://www.google.com/kml/ext/2.2'];
  335. var atomNamespaces = ['http://www.w3.org/2005/Atom'];
  336. var namespaces = {
  337. kml : kmlNamespaces,
  338. gx : gxNamespaces,
  339. atom : atomNamespaces,
  340. kmlgx : kmlNamespaces.concat(gxNamespaces)
  341. };
  342. function queryNumericAttribute(node, attributeName) {
  343. if (!defined(node)) {
  344. return undefined;
  345. }
  346. var value = node.getAttribute(attributeName);
  347. if (value !== null) {
  348. var result = parseFloat(value);
  349. return !isNaN(result) ? result : undefined;
  350. }
  351. return undefined;
  352. }
  353. function queryStringAttribute(node, attributeName) {
  354. if (!defined(node)) {
  355. return undefined;
  356. }
  357. var value = node.getAttribute(attributeName);
  358. return value !== null ? value : undefined;
  359. }
  360. function queryFirstNode(node, tagName, namespace) {
  361. if (!defined(node)) {
  362. return undefined;
  363. }
  364. var childNodes = node.childNodes;
  365. var length = childNodes.length;
  366. for (var q = 0; q < length; q++) {
  367. var child = childNodes[q];
  368. if (child.localName === tagName && namespace.indexOf(child.namespaceURI) !== -1) {
  369. return child;
  370. }
  371. }
  372. return undefined;
  373. }
  374. function queryNodes(node, tagName, namespace) {
  375. if (!defined(node)) {
  376. return undefined;
  377. }
  378. var result = [];
  379. var childNodes = node.getElementsByTagNameNS('*', tagName);
  380. var length = childNodes.length;
  381. for (var q = 0; q < length; q++) {
  382. var child = childNodes[q];
  383. if (child.localName === tagName && namespace.indexOf(child.namespaceURI) !== -1) {
  384. result.push(child);
  385. }
  386. }
  387. return result;
  388. }
  389. function queryChildNodes(node, tagName, namespace) {
  390. if (!defined(node)) {
  391. return [];
  392. }
  393. var result = [];
  394. var childNodes = node.childNodes;
  395. var length = childNodes.length;
  396. for (var q = 0; q < length; q++) {
  397. var child = childNodes[q];
  398. if (child.localName === tagName && namespace.indexOf(child.namespaceURI) !== -1) {
  399. result.push(child);
  400. }
  401. }
  402. return result;
  403. }
  404. function queryNumericValue(node, tagName, namespace) {
  405. var resultNode = queryFirstNode(node, tagName, namespace);
  406. if (defined(resultNode)) {
  407. var result = parseFloat(resultNode.textContent);
  408. return !isNaN(result) ? result : undefined;
  409. }
  410. return undefined;
  411. }
  412. function queryStringValue(node, tagName, namespace) {
  413. var result = queryFirstNode(node, tagName, namespace);
  414. if (defined(result)) {
  415. return result.textContent.trim();
  416. }
  417. return undefined;
  418. }
  419. function queryBooleanValue(node, tagName, namespace) {
  420. var result = queryFirstNode(node, tagName, namespace);
  421. if (defined(result)) {
  422. var value = result.textContent.trim();
  423. return value === '1' || /^true$/i.test(value);
  424. }
  425. return undefined;
  426. }
  427. function resolveHref(href, proxy, sourceUri, uriResolver) {
  428. if (!defined(href)) {
  429. return undefined;
  430. }
  431. var hrefResolved = false;
  432. if (defined(uriResolver)) {
  433. var blob = uriResolver[href];
  434. if (defined(blob)) {
  435. hrefResolved = true;
  436. href = blob;
  437. } else {
  438. // Needed for multiple levels of KML files in a KMZ
  439. var tmpHref = getAbsoluteUri(href, sourceUri);
  440. blob = uriResolver[tmpHref];
  441. if (defined(blob)) {
  442. hrefResolved = true;
  443. href = blob;
  444. }
  445. }
  446. }
  447. if (!hrefResolved && defined(sourceUri)) {
  448. href = getAbsoluteUri(href, getAbsoluteUri(sourceUri));
  449. href = proxyUrl(href, proxy);
  450. }
  451. return href;
  452. }
  453. var colorOptions = {};
  454. function parseColorString(value, isRandom) {
  455. if (!defined(value)) {
  456. return undefined;
  457. }
  458. if (value[0] === '#') {
  459. value = value.substring(1);
  460. }
  461. var alpha = parseInt(value.substring(0, 2), 16) / 255.0;
  462. var blue = parseInt(value.substring(2, 4), 16) / 255.0;
  463. var green = parseInt(value.substring(4, 6), 16) / 255.0;
  464. var red = parseInt(value.substring(6, 8), 16) / 255.0;
  465. if (!isRandom) {
  466. return new Color(red, green, blue, alpha);
  467. }
  468. if (red > 0) {
  469. colorOptions.maximumRed = red;
  470. } else {
  471. colorOptions.red = 0;
  472. }
  473. if (green > 0) {
  474. colorOptions.maximumGreen = green;
  475. } else {
  476. colorOptions.green = 0;
  477. }
  478. if (blue > 0) {
  479. colorOptions.maximumBlue = blue;
  480. } else {
  481. colorOptions.blue = 0;
  482. }
  483. colorOptions.alpha = alpha;
  484. return Color.fromRandom(colorOptions);
  485. }
  486. function queryColorValue(node, tagName, namespace) {
  487. var value = queryStringValue(node, tagName, namespace);
  488. if (!defined(value)) {
  489. return undefined;
  490. }
  491. return parseColorString(value, queryStringValue(node, 'colorMode', namespace) === 'random');
  492. }
  493. function processTimeStamp(featureNode) {
  494. var node = queryFirstNode(featureNode, 'TimeStamp', namespaces.kmlgx);
  495. var whenString = queryStringValue(node, 'when', namespaces.kmlgx);
  496. if (!defined(node) || !defined(whenString) || whenString.length === 0) {
  497. return undefined;
  498. }
  499. //According to the KML spec, a TimeStamp represents a "single moment in time"
  500. //However, since Cesium animates much differently than Google Earth, that doesn't
  501. //Make much sense here. Instead, we use the TimeStamp as the moment the feature
  502. //comes into existence. This works much better and gives a similar feel to
  503. //GE's experience.
  504. var when = JulianDate.fromIso8601(whenString);
  505. var result = new TimeIntervalCollection();
  506. result.addInterval(new TimeInterval({
  507. start : when,
  508. stop : Iso8601.MAXIMUM_VALUE
  509. }));
  510. return result;
  511. }
  512. function processTimeSpan(featureNode) {
  513. var node = queryFirstNode(featureNode, 'TimeSpan', namespaces.kmlgx);
  514. if (!defined(node)) {
  515. return undefined;
  516. }
  517. var result;
  518. var beginNode = queryFirstNode(node, 'begin', namespaces.kmlgx);
  519. var beginDate = defined(beginNode) ? JulianDate.fromIso8601(beginNode.textContent) : undefined;
  520. var endNode = queryFirstNode(node, 'end', namespaces.kmlgx);
  521. var endDate = defined(endNode) ? JulianDate.fromIso8601(endNode.textContent) : undefined;
  522. if (defined(beginDate) && defined(endDate)) {
  523. if (JulianDate.lessThan(endDate, beginDate)) {
  524. var tmp = beginDate;
  525. beginDate = endDate;
  526. endDate = tmp;
  527. }
  528. result = new TimeIntervalCollection();
  529. result.addInterval(new TimeInterval({
  530. start : beginDate,
  531. stop : endDate
  532. }));
  533. } else if (defined(beginDate)) {
  534. result = new TimeIntervalCollection();
  535. result.addInterval(new TimeInterval({
  536. start : beginDate,
  537. stop : Iso8601.MAXIMUM_VALUE
  538. }));
  539. } else if (defined(endDate)) {
  540. result = new TimeIntervalCollection();
  541. result.addInterval(new TimeInterval({
  542. start : Iso8601.MINIMUM_VALUE,
  543. stop : endDate
  544. }));
  545. }
  546. return result;
  547. }
  548. function createDefaultBillboard() {
  549. var billboard = new BillboardGraphics();
  550. billboard.width = BILLBOARD_SIZE;
  551. billboard.height = BILLBOARD_SIZE;
  552. billboard.scaleByDistance = new NearFarScalar(BILLBOARD_NEAR_DISTANCE, BILLBOARD_NEAR_RATIO, BILLBOARD_FAR_DISTANCE, BILLBOARD_FAR_RATIO);
  553. billboard.pixelOffsetScaleByDistance = new NearFarScalar(BILLBOARD_NEAR_DISTANCE, BILLBOARD_NEAR_RATIO, BILLBOARD_FAR_DISTANCE, BILLBOARD_FAR_RATIO);
  554. return billboard;
  555. }
  556. function createDefaultPolygon() {
  557. var polygon = new PolygonGraphics();
  558. polygon.outline = true;
  559. polygon.outlineColor = Color.WHITE;
  560. return polygon;
  561. }
  562. function createDefaultLabel() {
  563. var label = new LabelGraphics();
  564. label.translucencyByDistance = new NearFarScalar(3000000, 1.0, 5000000, 0.0);
  565. label.pixelOffset = new Cartesian2(17, 0);
  566. label.horizontalOrigin = HorizontalOrigin.LEFT;
  567. label.font = '16px sans-serif';
  568. label.style = LabelStyle.FILL_AND_OUTLINE;
  569. return label;
  570. }
  571. function getIconHref(iconNode, dataSource, sourceUri, uriResolver, canRefresh) {
  572. var href = queryStringValue(iconNode, 'href', namespaces.kml);
  573. if (!defined(href) || (href.length === 0)) {
  574. return undefined;
  575. }
  576. if (href.indexOf('root://icons/palette-') === 0) {
  577. var palette = href.charAt(21);
  578. // Get the icon number
  579. var x = defaultValue(queryNumericValue(iconNode, 'x', namespaces.gx), 0);
  580. var y = defaultValue(queryNumericValue(iconNode, 'y', namespaces.gx), 0);
  581. x = Math.min(x / 32, 7);
  582. y = 7 - Math.min(y / 32, 7);
  583. var iconNum = (8 * y) + x;
  584. href = 'https://maps.google.com/mapfiles/kml/pal' + palette + '/icon' + iconNum + '.png';
  585. }
  586. href = resolveHref(href, dataSource._proxy, sourceUri, uriResolver);
  587. if (canRefresh) {
  588. var refreshMode = queryStringValue(iconNode, 'refreshMode', namespaces.kml);
  589. var viewRefreshMode = queryStringValue(iconNode, 'viewRefreshMode', namespaces.kml);
  590. if (refreshMode === 'onInterval' || refreshMode === 'onExpire') {
  591. console.log('KML - Unsupported Icon refreshMode: ' + refreshMode);
  592. } else if (viewRefreshMode === 'onStop' || viewRefreshMode === 'onRegion') {
  593. console.log('KML - Unsupported Icon viewRefreshMode: ' + viewRefreshMode);
  594. }
  595. var viewBoundScale = defaultValue(queryStringValue(iconNode, 'viewBoundScale', namespaces.kml), 1.0);
  596. var defaultViewFormat = (viewRefreshMode === 'onStop') ? 'BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]' : '';
  597. var viewFormat = defaultValue(queryStringValue(iconNode, 'viewFormat', namespaces.kml), defaultViewFormat);
  598. var httpQuery = queryStringValue(iconNode, 'httpQuery', namespaces.kml);
  599. var queryString = makeQueryString(viewFormat, httpQuery);
  600. var icon = joinUrls(href, queryString, false);
  601. return processNetworkLinkQueryString(dataSource._camera, dataSource._canvas, icon, viewBoundScale, dataSource._lastCameraView.bbox);
  602. }
  603. return href;
  604. }
  605. function processBillboardIcon(dataSource, node, targetEntity, sourceUri, uriResolver) {
  606. var scale = queryNumericValue(node, 'scale', namespaces.kml);
  607. var heading = queryNumericValue(node, 'heading', namespaces.kml);
  608. var color = queryColorValue(node, 'color', namespaces.kml);
  609. var iconNode = queryFirstNode(node, 'Icon', namespaces.kml);
  610. var icon = getIconHref(iconNode, dataSource, sourceUri, uriResolver, false);
  611. var x = queryNumericValue(iconNode, 'x', namespaces.gx);
  612. var y = queryNumericValue(iconNode, 'y', namespaces.gx);
  613. var w = queryNumericValue(iconNode, 'w', namespaces.gx);
  614. var h = queryNumericValue(iconNode, 'h', namespaces.gx);
  615. var hotSpotNode = queryFirstNode(node, 'hotSpot', namespaces.kml);
  616. var hotSpotX = queryNumericAttribute(hotSpotNode, 'x');
  617. var hotSpotY = queryNumericAttribute(hotSpotNode, 'y');
  618. var hotSpotXUnit = queryStringAttribute(hotSpotNode, 'xunits');
  619. var hotSpotYUnit = queryStringAttribute(hotSpotNode, 'yunits');
  620. var billboard = targetEntity.billboard;
  621. if (!defined(billboard)) {
  622. billboard = createDefaultBillboard();
  623. targetEntity.billboard = billboard;
  624. }
  625. billboard.image = icon;
  626. billboard.scale = scale;
  627. billboard.color = color;
  628. if (defined(x) || defined(y) || defined(w) || defined(h)) {
  629. billboard.imageSubRegion = new BoundingRectangle(x, y, w, h);
  630. }
  631. //GE treats a heading of zero as no heading
  632. //You can still point north using a 360 degree angle (or any multiple of 360)
  633. if (defined(heading) && heading !== 0) {
  634. billboard.rotation = CesiumMath.toRadians(-heading);
  635. billboard.alignedAxis = Cartesian3.UNIT_Z;
  636. }
  637. //Hotpot is the KML equivalent of pixel offset
  638. //The hotspot origin is the lower left, but we leave
  639. //our billboard origin at the center and simply
  640. //modify the pixel offset to take this into account
  641. scale = defaultValue(scale, 1.0);
  642. var xOffset;
  643. var yOffset;
  644. if (defined(hotSpotX)) {
  645. if (hotSpotXUnit === 'pixels') {
  646. xOffset = -hotSpotX * scale;
  647. } else if (hotSpotXUnit === 'insetPixels') {
  648. xOffset = (hotSpotX - BILLBOARD_SIZE) * scale;
  649. } else if (hotSpotXUnit === 'fraction') {
  650. xOffset = -hotSpotX * BILLBOARD_SIZE * scale;
  651. }
  652. xOffset += BILLBOARD_SIZE * 0.5 * scale;
  653. }
  654. if (defined(hotSpotY)) {
  655. if (hotSpotYUnit === 'pixels') {
  656. yOffset = hotSpotY * scale;
  657. } else if (hotSpotYUnit === 'insetPixels') {
  658. yOffset = (-hotSpotY + BILLBOARD_SIZE) * scale;
  659. } else if (hotSpotYUnit === 'fraction') {
  660. yOffset = hotSpotY * BILLBOARD_SIZE * scale;
  661. }
  662. yOffset -= BILLBOARD_SIZE * 0.5 * scale;
  663. }
  664. if (defined(xOffset) || defined(yOffset)) {
  665. billboard.pixelOffset = new Cartesian2(xOffset, yOffset);
  666. }
  667. }
  668. function applyStyle(dataSource, styleNode, targetEntity, sourceUri, uriResolver) {
  669. for (var i = 0, len = styleNode.childNodes.length; i < len; i++) {
  670. var node = styleNode.childNodes.item(i);
  671. if (node.localName === 'IconStyle') {
  672. processBillboardIcon(dataSource, node, targetEntity, sourceUri, uriResolver);
  673. } else if (node.localName === 'LabelStyle') {
  674. var label = targetEntity.label;
  675. if (!defined(label)) {
  676. label = createDefaultLabel();
  677. targetEntity.label = label;
  678. }
  679. label.scale = defaultValue(queryNumericValue(node, 'scale', namespaces.kml), label.scale);
  680. label.fillColor = defaultValue(queryColorValue(node, 'color', namespaces.kml), label.fillColor);
  681. label.text = targetEntity.name;
  682. } else if (node.localName === 'LineStyle') {
  683. var polyline = targetEntity.polyline;
  684. if (!defined(polyline)) {
  685. polyline = new PolylineGraphics();
  686. targetEntity.polyline = polyline;
  687. }
  688. polyline.width = queryNumericValue(node, 'width', namespaces.kml);
  689. polyline.material = queryColorValue(node, 'color', namespaces.kml);
  690. if (defined(queryColorValue(node, 'outerColor', namespaces.gx))) {
  691. console.log('KML - gx:outerColor is not supported in a LineStyle');
  692. }
  693. if (defined(queryNumericValue(node, 'outerWidth', namespaces.gx))) {
  694. console.log('KML - gx:outerWidth is not supported in a LineStyle');
  695. }
  696. if (defined(queryNumericValue(node, 'physicalWidth', namespaces.gx))) {
  697. console.log('KML - gx:physicalWidth is not supported in a LineStyle');
  698. }
  699. if (defined(queryBooleanValue(node, 'labelVisibility', namespaces.gx))) {
  700. console.log('KML - gx:labelVisibility is not supported in a LineStyle');
  701. }
  702. } else if (node.localName === 'PolyStyle') {
  703. var polygon = targetEntity.polygon;
  704. if (!defined(polygon)) {
  705. polygon = createDefaultPolygon();
  706. targetEntity.polygon = polygon;
  707. }
  708. polygon.material = defaultValue(queryColorValue(node, 'color', namespaces.kml), polygon.material);
  709. polygon.fill = defaultValue(queryBooleanValue(node, 'fill', namespaces.kml), polygon.fill);
  710. polygon.outline = defaultValue(queryBooleanValue(node, 'outline', namespaces.kml), polygon.outline);
  711. } else if (node.localName === 'BalloonStyle') {
  712. var bgColor = defaultValue(parseColorString(queryStringValue(node, 'bgColor', namespaces.kml)), Color.WHITE);
  713. var textColor = defaultValue(parseColorString(queryStringValue(node, 'textColor', namespaces.kml)), Color.BLACK);
  714. var text = queryStringValue(node, 'text', namespaces.kml);
  715. //This is purely an internal property used in style processing,
  716. //it never ends up on the final entity.
  717. targetEntity.addProperty('balloonStyle');
  718. targetEntity.balloonStyle = {
  719. bgColor : bgColor,
  720. textColor : textColor,
  721. text : text
  722. };
  723. } else if (node.localName === 'ListStyle') {
  724. var listItemType = queryStringValue(node, 'listItemType', namespaces.kml);
  725. if (listItemType === 'radioFolder' || listItemType === 'checkOffOnly') {
  726. console.log('KML - Unsupported ListStyle with listItemType: ' + listItemType);
  727. }
  728. }
  729. }
  730. }
  731. //Processes and merges any inline styles for the provided node into the provided entity.
  732. function computeFinalStyle(entity, dataSource, placeMark, styleCollection, sourceUri, uriResolver) {
  733. var result = new Entity();
  734. var styleEntity;
  735. //Google earth seems to always use the last inline Style/StyleMap only
  736. var styleIndex = -1;
  737. var childNodes = placeMark.childNodes;
  738. var length = childNodes.length;
  739. for (var q = 0; q < length; q++) {
  740. var child = childNodes[q];
  741. if (child.localName === 'Style' || child.localName === 'StyleMap') {
  742. styleIndex = q;
  743. }
  744. }
  745. if (styleIndex !== -1) {
  746. var inlineStyleNode = childNodes[styleIndex];
  747. if (inlineStyleNode.localName === 'Style') {
  748. applyStyle(dataSource, inlineStyleNode, result, sourceUri, uriResolver);
  749. } else { // StyleMap
  750. var pairs = queryChildNodes(inlineStyleNode, 'Pair', namespaces.kml);
  751. for (var p = 0; p < pairs.length; p++) {
  752. var pair = pairs[p];
  753. var key = queryStringValue(pair, 'key', namespaces.kml);
  754. if (key === 'normal') {
  755. var styleUrl = queryStringValue(pair, 'styleUrl', namespaces.kml);
  756. if (defined(styleUrl)) {
  757. styleEntity = styleCollection.getById(styleUrl);
  758. if (!defined(styleEntity)) {
  759. styleEntity = styleCollection.getById('#' + styleUrl);
  760. }
  761. if (defined(styleEntity)) {
  762. result.merge(styleEntity);
  763. }
  764. } else {
  765. var node = queryFirstNode(pair, 'Style', namespaces.kml);
  766. applyStyle(dataSource, node, result, sourceUri, uriResolver);
  767. }
  768. } else {
  769. console.log('KML - Unsupported StyleMap key: ' + key);
  770. }
  771. }
  772. }
  773. }
  774. //Google earth seems to always use the first external style only.
  775. var externalStyle = queryStringValue(placeMark, 'styleUrl', namespaces.kml);
  776. if (defined(externalStyle)) {
  777. var id = externalStyle;
  778. if (externalStyle[0] !== '#' && externalStyle.indexOf('#') !== -1) {
  779. var tokens = externalStyle.split('#');
  780. var uri = tokens[0];
  781. if (defined(sourceUri)) {
  782. uri = getAbsoluteUri(uri, getAbsoluteUri(sourceUri));
  783. }
  784. id = uri + '#' + tokens[1];
  785. }
  786. styleEntity = styleCollection.getById(id);
  787. if (!defined(styleEntity)) {
  788. styleEntity = styleCollection.getById('#' + id);
  789. }
  790. if (defined(styleEntity)) {
  791. result.merge(styleEntity);
  792. }
  793. }
  794. return result;
  795. }
  796. //Asynchronously processes an external style file.
  797. function processExternalStyles(dataSource, uri, styleCollection) {
  798. return loadXML(proxyUrl(uri, dataSource._proxy)).then(function(styleKml) {
  799. return processStyles(dataSource, styleKml, styleCollection, uri, true);
  800. });
  801. }
  802. //Processes all shared and external styles and stores
  803. //their id into the provided styleCollection.
  804. //Returns an array of promises that will resolve when
  805. //each style is loaded.
  806. function processStyles(dataSource, kml, styleCollection, sourceUri, isExternal, uriResolver) {
  807. var i;
  808. var id;
  809. var styleEntity;
  810. var node;
  811. var styleNodes = queryNodes(kml, 'Style', namespaces.kml);
  812. if (defined(styleNodes)) {
  813. var styleNodesLength = styleNodes.length;
  814. for (i = 0; i < styleNodesLength; i++) {
  815. node = styleNodes[i];
  816. id = queryStringAttribute(node, 'id');
  817. if (defined(id)) {
  818. id = '#' + id;
  819. if (isExternal && defined(sourceUri)) {
  820. id = sourceUri + id;
  821. }
  822. if (!defined(styleCollection.getById(id))) {
  823. styleEntity = new Entity({
  824. id : id
  825. });
  826. styleCollection.add(styleEntity);
  827. applyStyle(dataSource, node, styleEntity, sourceUri, uriResolver);
  828. }
  829. }
  830. }
  831. }
  832. var styleMaps = queryNodes(kml, 'StyleMap', namespaces.kml);
  833. if (defined(styleMaps)) {
  834. var styleMapsLength = styleMaps.length;
  835. for (i = 0; i < styleMapsLength; i++) {
  836. var styleMap = styleMaps[i];
  837. id = queryStringAttribute(styleMap, 'id');
  838. if (defined(id)) {
  839. var pairs = queryChildNodes(styleMap, 'Pair', namespaces.kml);
  840. for (var p = 0; p < pairs.length; p++) {
  841. var pair = pairs[p];
  842. var key = queryStringValue(pair, 'key', namespaces.kml);
  843. if (key === 'normal') {
  844. id = '#' + id;
  845. if (isExternal && defined(sourceUri)) {
  846. id = sourceUri + id;
  847. }
  848. if (!defined(styleCollection.getById(id))) {
  849. styleEntity = styleCollection.getOrCreateEntity(id);
  850. var styleUrl = queryStringValue(pair, 'styleUrl', namespaces.kml);
  851. if (defined(styleUrl)) {
  852. if (styleUrl[0] !== '#') {
  853. styleUrl = '#' + styleUrl;
  854. }
  855. if (isExternal && defined(sourceUri)) {
  856. styleUrl = sourceUri + styleUrl;
  857. }
  858. var base = styleCollection.getById(styleUrl);
  859. if (defined(base)) {
  860. styleEntity.merge(base);
  861. }
  862. } else {
  863. node = queryFirstNode(pair, 'Style', namespaces.kml);
  864. applyStyle(dataSource, node, styleEntity, sourceUri, uriResolver);
  865. }
  866. }
  867. } else {
  868. console.log('KML - Unsupported StyleMap key: ' + key);
  869. }
  870. }
  871. }
  872. }
  873. }
  874. var externalStyleHash = {};
  875. var promises = [];
  876. var styleUrlNodes = kml.getElementsByTagName('styleUrl');
  877. var styleUrlNodesLength = styleUrlNodes.length;
  878. for (i = 0; i < styleUrlNodesLength; i++) {
  879. var styleReference = styleUrlNodes[i].textContent;
  880. if (styleReference[0] !== '#') {
  881. //According to the spec, all local styles should start with a #
  882. //and everything else is an external style that has a # seperating
  883. //the URL of the document and the style. However, Google Earth
  884. //also accepts styleUrls without a # as meaning a local style.
  885. var tokens = styleReference.split('#');
  886. if (tokens.length === 2) {
  887. var uri = tokens[0];
  888. if (!defined(externalStyleHash[uri])) {
  889. if (defined(sourceUri)) {
  890. uri = getAbsoluteUri(uri, getAbsoluteUri(sourceUri));
  891. }
  892. promises.push(processExternalStyles(dataSource, uri, styleCollection, sourceUri));
  893. }
  894. }
  895. }
  896. }
  897. return promises;
  898. }
  899. function createDropLine(entityCollection, entity, styleEntity) {
  900. var entityPosition = new ReferenceProperty(entityCollection, entity.id, ['position']);
  901. var surfacePosition = new ScaledPositionProperty(entity.position);
  902. entity.polyline = defined(styleEntity.polyline) ? styleEntity.polyline.clone() : new PolylineGraphics();
  903. entity.polyline.positions = new PositionPropertyArray([entityPosition, surfacePosition]);
  904. }
  905. function heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode) {
  906. if (!defined(altitudeMode) && !defined(gxAltitudeMode) || altitudeMode === 'clampToGround') {
  907. return HeightReference.CLAMP_TO_GROUND;
  908. }
  909. if (altitudeMode === 'relativeToGround') {
  910. return HeightReference.RELATIVE_TO_GROUND;
  911. }
  912. if (altitudeMode === 'absolute') {
  913. return HeightReference.NONE;
  914. }
  915. if (gxAltitudeMode === 'clampToSeaFloor') {
  916. console.log('KML - <gx:altitudeMode>:clampToSeaFloor is currently not supported, using <kml:altitudeMode>:clampToGround.');
  917. return HeightReference.CLAMP_TO_GROUND;
  918. }
  919. if (gxAltitudeMode === 'relativeToSeaFloor') {
  920. console.log('KML - <gx:altitudeMode>:relativeToSeaFloor is currently not supported, using <kml:altitudeMode>:relativeToGround.');
  921. return HeightReference.RELATIVE_TO_GROUND;
  922. }
  923. if (defined(altitudeMode)) {
  924. console.log('KML - Unknown <kml:altitudeMode>:' + altitudeMode + ', using <kml:altitudeMode>:CLAMP_TO_GROUND.');
  925. } else {
  926. console.log('KML - Unknown <gx:altitudeMode>:' + gxAltitudeMode + ', using <kml:altitudeMode>:CLAMP_TO_GROUND.');
  927. }
  928. // Clamp to ground is the default
  929. return HeightReference.CLAMP_TO_GROUND;
  930. }
  931. function createPositionPropertyFromAltitudeMode(property, altitudeMode, gxAltitudeMode) {
  932. if (gxAltitudeMode === 'relativeToSeaFloor' || altitudeMode === 'absolute' || altitudeMode === 'relativeToGround') {
  933. //Just return the ellipsoid referenced property until we support MSL
  934. return property;
  935. }
  936. if ((defined(altitudeMode) && altitudeMode !== 'clampToGround') || //
  937. (defined(gxAltitudeMode) && gxAltitudeMode !== 'clampToSeaFloor')) {
  938. console.log('KML - Unknown altitudeMode: ' + defaultValue(altitudeMode, gxAltitudeMode));
  939. }
  940. // Clamp to ground is the default
  941. return new ScaledPositionProperty(property);
  942. }
  943. function createPositionPropertyArrayFromAltitudeMode(properties, altitudeMode, gxAltitudeMode) {
  944. if (!defined(properties)) {
  945. return undefined;
  946. }
  947. if (gxAltitudeMode === 'relativeToSeaFloor' || altitudeMode === 'absolute' || altitudeMode === 'relativeToGround') {
  948. //Just return the ellipsoid referenced property until we support MSL
  949. return properties;
  950. }
  951. if ((defined(altitudeMode) && altitudeMode !== 'clampToGround') || //
  952. (defined(gxAltitudeMode) && gxAltitudeMode !== 'clampToSeaFloor')) {
  953. console.log('KML - Unknown altitudeMode: ' + defaultValue(altitudeMode, gxAltitudeMode));
  954. }
  955. // Clamp to ground is the default
  956. var propertiesLength = properties.length;
  957. for (var i = 0; i < propertiesLength; i++) {
  958. var property = properties[i];
  959. Ellipsoid.WGS84.scaleToGeodeticSurface(property, property);
  960. }
  961. return properties;
  962. }
  963. function processPositionGraphics(dataSource, entity, styleEntity, heightReference) {
  964. var label = entity.label;
  965. if (!defined(label)) {
  966. label = defined(styleEntity.label) ? styleEntity.label.clone() : createDefaultLabel();
  967. entity.label = label;
  968. }
  969. label.text = entity.name;
  970. var billboard = entity.billboard;
  971. if (!defined(billboard)) {
  972. billboard = defined(styleEntity.billboard) ? styleEntity.billboard.clone() : createDefaultBillboard();
  973. entity.billboard = billboard;
  974. }
  975. if (!defined(billboard.image)) {
  976. billboard.image = dataSource._pinBuilder.fromColor(Color.YELLOW, 64);
  977. }
  978. var scale = 1.0;
  979. if (defined(billboard.scale)) {
  980. scale = billboard.scale.getValue();
  981. if (scale !== 0) {
  982. label.pixelOffset = new Cartesian2((scale * 16) + 1, 0);
  983. } else {
  984. //Minor tweaks to better match Google Earth.
  985. label.pixelOffset = undefined;
  986. label.horizontalOrigin = undefined;
  987. }
  988. }
  989. if (defined(heightReference) && dataSource._clampToGround) {
  990. billboard.heightReference = heightReference;
  991. label.heightReference = heightReference;
  992. }
  993. }
  994. function processPathGraphics(dataSource, entity, styleEntity) {
  995. var path = entity.path;
  996. if (!defined(path)) {
  997. path = new PathGraphics();
  998. path.leadTime = 0;
  999. entity.path = path;
  1000. }
  1001. var polyline = styleEntity.polyline;
  1002. if (defined(polyline)) {
  1003. path.material = polyline.material;
  1004. path.width = polyline.width;
  1005. }
  1006. }
  1007. function processPoint(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1008. var coordinatesString = queryStringValue(geometryNode, 'coordinates', namespaces.kml);
  1009. var altitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.kml);
  1010. var gxAltitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.gx);
  1011. var extrude = queryBooleanValue(geometryNode, 'extrude', namespaces.kml);
  1012. var position = readCoordinate(coordinatesString);
  1013. entity.position = position;
  1014. processPositionGraphics(dataSource, entity, styleEntity, heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode));
  1015. if (extrude && isExtrudable(altitudeMode, gxAltitudeMode)) {
  1016. createDropLine(entityCollection, entity, styleEntity);
  1017. }
  1018. return true;
  1019. }
  1020. function processLineStringOrLinearRing(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1021. var coordinatesNode = queryFirstNode(geometryNode, 'coordinates', namespaces.kml);
  1022. var altitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.kml);
  1023. var gxAltitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.gx);
  1024. var extrude = queryBooleanValue(geometryNode, 'extrude', namespaces.kml);
  1025. var tessellate = queryBooleanValue(geometryNode, 'tessellate', namespaces.kml);
  1026. var canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1027. if (defined(queryNumericValue(geometryNode, 'drawOrder', namespaces.gx))) {
  1028. console.log('KML - gx:drawOrder is not supported in LineStrings');
  1029. }
  1030. var coordinates = readCoordinates(coordinatesNode);
  1031. var polyline = styleEntity.polyline;
  1032. if (canExtrude && extrude) {
  1033. var wall = new WallGraphics();
  1034. entity.wall = wall;
  1035. wall.positions = coordinates;
  1036. var polygon = styleEntity.polygon;
  1037. if (defined(polygon)) {
  1038. wall.fill = polygon.fill;
  1039. wall.outline = polygon.outline;
  1040. wall.material = polygon.material;
  1041. }
  1042. if (defined(polyline)) {
  1043. wall.outlineColor = defined(polyline.material) ? polyline.material.color : Color.WHITE;
  1044. wall.outlineWidth = polyline.width;
  1045. }
  1046. } else {
  1047. if (dataSource._clampToGround && !canExtrude && tessellate) {
  1048. var corridor = new CorridorGraphics();
  1049. entity.corridor = corridor;
  1050. corridor.positions = coordinates;
  1051. if (defined(polyline)) {
  1052. corridor.material = defined(polyline.material) ? polyline.material.color.getValue(Iso8601.MINIMUM_VALUE) : Color.WHITE;
  1053. corridor.width = defaultValue(polyline.width, 1.0);
  1054. } else {
  1055. corridor.material = Color.WHITE;
  1056. corridor.width = 1.0;
  1057. }
  1058. } else {
  1059. polyline = defined(polyline) ? polyline.clone() : new PolylineGraphics();
  1060. entity.polyline = polyline;
  1061. polyline.positions = createPositionPropertyArrayFromAltitudeMode(coordinates, altitudeMode, gxAltitudeMode);
  1062. if (!tessellate || canExtrude) {
  1063. polyline.followSurface = false;
  1064. }
  1065. }
  1066. }
  1067. return true;
  1068. }
  1069. function processPolygon(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1070. var outerBoundaryIsNode = queryFirstNode(geometryNode, 'outerBoundaryIs', namespaces.kml);
  1071. var linearRingNode = queryFirstNode(outerBoundaryIsNode, 'LinearRing', namespaces.kml);
  1072. var coordinatesNode = queryFirstNode(linearRingNode, 'coordinates', namespaces.kml);
  1073. var coordinates = readCoordinates(coordinatesNode);
  1074. var extrude = queryBooleanValue(geometryNode, 'extrude', namespaces.kml);
  1075. var altitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.kml);
  1076. var gxAltitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.gx);
  1077. var canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1078. var polygon = defined(styleEntity.polygon) ? styleEntity.polygon.clone() : createDefaultPolygon();
  1079. var polyline = styleEntity.polyline;
  1080. if (defined(polyline)) {
  1081. polygon.outlineColor = defined(polyline.material) ? polyline.material.color : Color.WHITE;
  1082. polygon.outlineWidth = polyline.width;
  1083. }
  1084. entity.polygon = polygon;
  1085. if (canExtrude) {
  1086. polygon.perPositionHeight = true;
  1087. polygon.extrudedHeight = extrude ? 0 : undefined;
  1088. } else if (!dataSource._clampToGround) {
  1089. polygon.height = 0;
  1090. }
  1091. if (defined(coordinates)) {
  1092. var hierarchy = new PolygonHierarchy(coordinates);
  1093. var innerBoundaryIsNodes = queryChildNodes(geometryNode, 'innerBoundaryIs', namespaces.kml);
  1094. for (var j = 0; j < innerBoundaryIsNodes.length; j++) {
  1095. linearRingNode = queryChildNodes(innerBoundaryIsNodes[j], 'LinearRing', namespaces.kml);
  1096. for (var k = 0; k < linearRingNode.length; k++) {
  1097. coordinatesNode = queryFirstNode(linearRingNode[k], 'coordinates', namespaces.kml);
  1098. coordinates = readCoordinates(coordinatesNode);
  1099. if (defined(coordinates)) {
  1100. hierarchy.holes.push(new PolygonHierarchy(coordinates));
  1101. }
  1102. }
  1103. }
  1104. polygon.hierarchy = hierarchy;
  1105. }
  1106. return true;
  1107. }
  1108. function processTrack(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1109. var altitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.kml);
  1110. var gxAltitudeMode = queryStringValue(geometryNode, 'altitudeMode', namespaces.gx);
  1111. var coordNodes = queryChildNodes(geometryNode, 'coord', namespaces.gx);
  1112. var angleNodes = queryChildNodes(geometryNode, 'angles', namespaces.gx);
  1113. var timeNodes = queryChildNodes(geometryNode, 'when', namespaces.kml);
  1114. var extrude = queryBooleanValue(geometryNode, 'extrude', namespaces.kml);
  1115. var canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1116. if (angleNodes.length > 0) {
  1117. console.log('KML - gx:angles are not supported in gx:Tracks');
  1118. }
  1119. var length = Math.min(coordNodes.length, timeNodes.length);
  1120. var coordinates = [];
  1121. var times = [];
  1122. for (var i = 0; i < length; i++) {
  1123. var position = readCoordinate(coordNodes[i].textContent);
  1124. coordinates.push(position);
  1125. times.push(JulianDate.fromIso8601(timeNodes[i].textContent));
  1126. }
  1127. var property = new SampledPositionProperty();
  1128. property.addSamples(times, coordinates);
  1129. entity.position = property;
  1130. processPositionGraphics(dataSource, entity, styleEntity, heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode));
  1131. processPathGraphics(dataSource, entity, styleEntity);
  1132. entity.availability = new TimeIntervalCollection();
  1133. if (timeNodes.length > 0) {
  1134. entity.availability.addInterval(new TimeInterval({
  1135. start : times[0],
  1136. stop : times[times.length - 1]
  1137. }));
  1138. }
  1139. if (canExtrude && extrude) {
  1140. createDropLine(entityCollection, entity, styleEntity);
  1141. }
  1142. return true;
  1143. }
  1144. function addToMultiTrack(times, positions, composite, availability, dropShowProperty, extrude, altitudeMode, gxAltitudeMode, includeEndPoints) {
  1145. var start = times[0];
  1146. var stop = times[times.length - 1];
  1147. var data = new SampledPositionProperty();
  1148. data.addSamples(times, positions);
  1149. composite.intervals.addInterval(new TimeInterval({
  1150. start : start,
  1151. stop : stop,
  1152. isStartIncluded : includeEndPoints,
  1153. isStopIncluded : includeEndPoints,
  1154. data : createPositionPropertyFromAltitudeMode(data, altitudeMode, gxAltitudeMode)
  1155. }));
  1156. availability.addInterval(new TimeInterval({
  1157. start : start,
  1158. stop : stop,
  1159. isStartIncluded : includeEndPoints,
  1160. isStopIncluded : includeEndPoints
  1161. }));
  1162. dropShowProperty.intervals.addInterval(new TimeInterval({
  1163. start : start,
  1164. stop : stop,
  1165. isStartIncluded : includeEndPoints,
  1166. isStopIncluded : includeEndPoints,
  1167. data : extrude
  1168. }));
  1169. }
  1170. function processMultiTrack(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1171. // Multitrack options do not work in GE as detailed in the spec,
  1172. // rather than altitudeMode being at the MultiTrack level,
  1173. // GE just defers all settings to the underlying track.
  1174. var interpolate = queryBooleanValue(geometryNode, 'interpolate', namespaces.gx);
  1175. var trackNodes = queryChildNodes(geometryNode, 'Track', namespaces.gx);
  1176. var times;
  1177. var lastStop;
  1178. var lastStopPosition;
  1179. var needDropLine = false;
  1180. var dropShowProperty = new TimeIntervalCollectionProperty();
  1181. var availability = new TimeIntervalCollection();
  1182. var composite = new CompositePositionProperty();
  1183. for (var i = 0, len = trackNodes.length; i < len; i++) {
  1184. var trackNode = trackNodes[i];
  1185. var timeNodes = queryChildNodes(trackNode, 'when', namespaces.kml);
  1186. var coordNodes = queryChildNodes(trackNode, 'coord', namespaces.gx);
  1187. var altitudeMode = queryStringValue(trackNode, 'altitudeMode', namespaces.kml);
  1188. var gxAltitudeMode = queryStringValue(trackNode, 'altitudeMode', namespaces.gx);
  1189. var canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1190. var extrude = queryBooleanValue(trackNode, 'extrude', namespaces.kml);
  1191. var length = Math.min(coordNodes.length, timeNodes.length);
  1192. var positions = [];
  1193. times = [];
  1194. for (var x = 0; x < length; x++) {
  1195. var position = readCoordinate(coordNodes[x].textContent);
  1196. positions.push(position);
  1197. times.push(JulianDate.fromIso8601(timeNodes[x].textContent));
  1198. }
  1199. if (interpolate) {
  1200. //If we are interpolating, then we need to fill in the end of
  1201. //the last track and the beginning of this one with a sampled
  1202. //property. From testing in Google Earth, this property
  1203. //is never extruded and always absolute.
  1204. if (defined(lastStop)) {
  1205. addToMultiTrack([lastStop, times[0]], [lastStopPosition, positions[0]], composite, availability, dropShowProperty, false, 'absolute', undefined, false);
  1206. }
  1207. lastStop = times[length - 1];
  1208. lastStopPosition = positions[positions.length - 1];
  1209. }
  1210. addToMultiTrack(times, positions, composite, availability, dropShowProperty, canExtrude && extrude, altitudeMode, gxAltitudeMode, true);
  1211. needDropLine = needDropLine || (canExtrude && extrude);
  1212. }
  1213. entity.availability = availability;
  1214. entity.position = composite;
  1215. processPositionGraphics(dataSource, entity, styleEntity);
  1216. processPathGraphics(dataSource, entity, styleEntity);
  1217. if (needDropLine) {
  1218. createDropLine(entityCollection, entity, styleEntity);
  1219. entity.polyline.show = dropShowProperty;
  1220. }
  1221. return true;
  1222. }
  1223. function processMultiGeometry(dataSource, entityCollection, geometryNode, entity, styleEntity, context) {
  1224. var childNodes = geometryNode.childNodes;
  1225. var hasGeometry = false;
  1226. for (var i = 0, len = childNodes.length; i < len; i++) {
  1227. var childNode = childNodes.item(i);
  1228. var geometryProcessor = geometryTypes[childNode.localName];
  1229. if (defined(geometryProcessor)) {
  1230. var childEntity = createEntity(childNode, entityCollection, context);
  1231. childEntity.parent = entity;
  1232. childEntity.name = entity.name;
  1233. childEntity.availability = entity.availability;
  1234. childEntity.description = entity.description;
  1235. childEntity.kml = entity.kml;
  1236. if (geometryProcessor(dataSource, entityCollection, childNode, childEntity, styleEntity)) {
  1237. hasGeometry = true;
  1238. }
  1239. }
  1240. }
  1241. return hasGeometry;
  1242. }
  1243. function processUnsupportedGeometry(dataSource, entityCollection, geometryNode, entity, styleEntity) {
  1244. console.log('KML - Unsupported geometry: ' + geometryNode.localName);
  1245. return false;
  1246. }
  1247. function processExtendedData(node, entity) {
  1248. var extendedDataNode = queryFirstNode(node, 'ExtendedData', namespaces.kml);
  1249. if (!defined(extendedDataNode)) {
  1250. return undefined;
  1251. }
  1252. if (defined(queryFirstNode(extendedDataNode, 'SchemaData', namespaces.kml))) {
  1253. console.log('KML - SchemaData is unsupported');
  1254. }
  1255. if (defined(queryStringAttribute(extendedDataNode, 'xmlns:prefix'))) {
  1256. console.log('KML - ExtendedData with xmlns:prefix is unsupported');
  1257. }
  1258. var result = {};
  1259. var dataNodes = queryChildNodes(extendedDataNode, 'Data', namespaces.kml);
  1260. if (defined(dataNodes)) {
  1261. var length = dataNodes.length;
  1262. for (var i = 0; i < length; i++) {
  1263. var dataNode = dataNodes[i];
  1264. var name = queryStringAttribute(dataNode, 'name');
  1265. if (defined(name)) {
  1266. result[name] = {
  1267. displayName : queryStringValue(dataNode, 'displayName', namespaces.kml),
  1268. value : queryStringValue(dataNode, 'value', namespaces.kml)
  1269. };
  1270. }
  1271. }
  1272. }
  1273. entity.kml.extendedData = result;
  1274. }
  1275. var scratchDiv = document.createElement('div');
  1276. function processDescription(node, entity, styleEntity, uriResolver) {
  1277. var i;
  1278. var key;
  1279. var keys;
  1280. var kmlData = entity.kml;
  1281. var extendedData = kmlData.extendedData;
  1282. var description = queryStringValue(node, 'description', namespaces.kml);
  1283. var balloonStyle = defaultValue(entity.balloonStyle, styleEntity.balloonStyle);
  1284. var background = Color.WHITE;
  1285. var foreground = Color.BLACK;
  1286. var text = description;
  1287. if (defined(balloonStyle)) {
  1288. background = defaultValue(balloonStyle.bgColor, Color.WHITE);
  1289. foreground = defaultValue(balloonStyle.textColor, Color.BLACK);
  1290. text = defaultValue(balloonStyle.text, description);
  1291. }
  1292. var value;
  1293. if (defined(text)) {
  1294. text = text.replace('$[name]', defaultValue(entity.name, ''));
  1295. text = text.replace('$[description]', defaultValue(description, ''));
  1296. text = text.replace('$[address]', defaultValue(kmlData.address, ''));
  1297. text = text.replace('$[Snippet]', defaultValue(kmlData.snippet, ''));
  1298. text = text.replace('$[id]', entity.id);
  1299. //While not explicitly defined by the OGC spec, in Google Earth
  1300. //The appearance of geDirections adds the directions to/from links
  1301. //We simply replace this string with nothing.
  1302. text = text.replace('$[geDirections]', '');
  1303. if (defined(extendedData)) {
  1304. var matches = text.match(/\$\[.+?\]/g);
  1305. if (matches !== null) {
  1306. for (i = 0; i < matches.length; i++) {
  1307. var token = matches[i];
  1308. var propertyName = token.substr(2, token.length - 3);
  1309. var isDisplayName = /\/displayName$/.test(propertyName);
  1310. propertyName = propertyName.replace(/\/displayName$/, '');
  1311. value = extendedData[propertyName];
  1312. if (defined(value)) {
  1313. value = isDisplayName ? value.displayName : value.value;
  1314. }
  1315. if (defined(value)) {
  1316. text = text.replace(token, defaultValue(value, ''));
  1317. }
  1318. }
  1319. }
  1320. }
  1321. } else if (defined(extendedData)) {
  1322. //If no description exists, build a table out of the extended data
  1323. keys = Object.keys(extendedData);
  1324. if (keys.length > 0) {
  1325. text = '<table class="cesium-infoBox-defaultTable cesium-infoBox-defaultTable-lighter"><tbody>';
  1326. for (i = 0; i < keys.length; i++) {
  1327. key = keys[i];
  1328. value = extendedData[key];
  1329. text += '<tr><th>' + defaultValue(value.displayName, key) + '</th><td>' + defaultValue(value.value, '') + '</td></tr>';
  1330. }
  1331. text += '</tbody></table>';
  1332. }
  1333. }
  1334. if (!defined(text)) {
  1335. //No description
  1336. return;
  1337. }
  1338. //Turns non-explicit links into clickable links.
  1339. text = autolinker.link(text);
  1340. //Use a temporary div to manipulate the links
  1341. //so that they open in a new window.
  1342. scratchDiv.innerHTML = text;
  1343. var links = scratchDiv.querySelectorAll('a');
  1344. for (i = 0; i < links.length; i++) {
  1345. links[i].setAttribute('target', '_blank');
  1346. }
  1347. //Rewrite any KMZ embedded urls
  1348. if (defined(uriResolver) && uriResolver.keys.length > 1) {
  1349. replaceAttributes(scratchDiv, 'a', 'href', uriResolver);
  1350. replaceAttributes(scratchDiv, 'img', 'src', uriResolver);
  1351. }
  1352. var tmp = '<div class="cesium-infoBox-description-lighter" style="';
  1353. tmp += 'overflow:auto;';
  1354. tmp += 'word-wrap:break-word;';
  1355. tmp += 'background-color:' + background.toCssColorString() + ';';
  1356. tmp += 'color:' + foreground.toCssColorString() + ';';
  1357. tmp += '">';
  1358. tmp += scratchDiv.innerHTML + '</div>';
  1359. scratchDiv.innerHTML = '';
  1360. //Set the final HTML as the description.
  1361. entity.description = tmp;
  1362. }
  1363. function processFeature(dataSource, parent, featureNode, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1364. var entity = createEntity(featureNode, entityCollection, context);
  1365. var kmlData = entity.kml;
  1366. var styleEntity = computeFinalStyle(entity, dataSource, featureNode, styleCollection, sourceUri, uriResolver);
  1367. var name = queryStringValue(featureNode, 'name', namespaces.kml);
  1368. entity.name = name;
  1369. entity.parent = parent;
  1370. var availability = processTimeSpan(featureNode);
  1371. if (!defined(availability)) {
  1372. availability = processTimeStamp(featureNode);
  1373. }
  1374. entity.availability = availability;
  1375. mergeAvailabilityWithParent(entity);
  1376. // Per KML spec "A Feature is visible only if it and all its ancestors are visible."
  1377. function ancestryIsVisible(parentEntity) {
  1378. if (!parentEntity) {
  1379. return true;
  1380. }
  1381. return parentEntity.show && ancestryIsVisible(parentEntity.parent);
  1382. }
  1383. var visibility = queryBooleanValue(featureNode, 'visibility', namespaces.kml);
  1384. entity.show = ancestryIsVisible(parent) && defaultValue(visibility, true);
  1385. //var open = queryBooleanValue(featureNode, 'open', namespaces.kml);
  1386. var authorNode = queryFirstNode(featureNode, 'author', namespaces.atom);
  1387. var author = kmlData.author;
  1388. author.name = queryStringValue(authorNode, 'name', namespaces.atom);
  1389. author.uri = queryStringValue(authorNode, 'uri', namespaces.atom);
  1390. author.email = queryStringValue(authorNode, 'email', namespaces.atom);
  1391. var linkNode = queryFirstNode(featureNode, 'link', namespaces.atom);
  1392. var link = kmlData.link;
  1393. link.href = queryStringAttribute(linkNode, 'href');
  1394. link.hreflang = queryStringAttribute(linkNode, 'hreflang');
  1395. link.rel = queryStringAttribute(linkNode, 'rel');
  1396. link.type = queryStringAttribute(linkNode, 'type');
  1397. link.title = queryStringAttribute(linkNode, 'title');
  1398. link.length = queryStringAttribute(linkNode, 'length');
  1399. kmlData.address = queryStringValue(featureNode, 'address', namespaces.kml);
  1400. kmlData.phoneNumber = queryStringValue(featureNode, 'phoneNumber', namespaces.kml);
  1401. kmlData.snippet = queryStringValue(featureNode, 'Snippet', namespaces.kml);
  1402. processExtendedData(featureNode, entity);
  1403. processDescription(featureNode, entity, styleEntity, uriResolver);
  1404. if (defined(queryFirstNode(featureNode, 'Camera', namespaces.kml))) {
  1405. console.log('KML - Unsupported view: Camera');
  1406. }
  1407. if (defined(queryFirstNode(featureNode, 'LookAt', namespaces.kml))) {
  1408. console.log('KML - Unsupported view: LookAt');
  1409. }
  1410. if (defined(queryFirstNode(featureNode, 'Region', namespaces.kml))) {
  1411. console.log('KML - Placemark Regions are unsupported');
  1412. }
  1413. return {
  1414. entity : entity,
  1415. styleEntity : styleEntity
  1416. };
  1417. }
  1418. var geometryTypes = {
  1419. Point : processPoint,
  1420. LineString : processLineStringOrLinearRing,
  1421. LinearRing : processLineStringOrLinearRing,
  1422. Polygon : processPolygon,
  1423. Track : processTrack,
  1424. MultiTrack : processMultiTrack,
  1425. MultiGeometry : processMultiGeometry,
  1426. Model : processUnsupportedGeometry
  1427. };
  1428. function processDocument(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1429. var featureTypeNames = Object.keys(featureTypes);
  1430. var featureTypeNamesLength = featureTypeNames.length;
  1431. for (var i = 0; i < featureTypeNamesLength; i++) {
  1432. var featureName = featureTypeNames[i];
  1433. var processFeatureNode = featureTypes[featureName];
  1434. var childNodes = node.childNodes;
  1435. var length = childNodes.length;
  1436. for (var q = 0; q < length; q++) {
  1437. var child = childNodes[q];
  1438. if (child.localName === featureName &&
  1439. ((namespaces.kml.indexOf(child.namespaceURI) !== -1) || (namespaces.gx.indexOf(child.namespaceURI) !== -1))) {
  1440. processFeatureNode(dataSource, parent, child, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1441. }
  1442. }
  1443. }
  1444. }
  1445. function processFolder(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1446. var r = processFeature(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1447. processDocument(dataSource, r.entity, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1448. }
  1449. function processPlacemark(dataSource, parent, placemark, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1450. var r = processFeature(dataSource, parent, placemark, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1451. var entity = r.entity;
  1452. var styleEntity = r.styleEntity;
  1453. var hasGeometry = false;
  1454. var childNodes = placemark.childNodes;
  1455. for (var i = 0, len = childNodes.length; i < len && !hasGeometry; i++) {
  1456. var childNode = childNodes.item(i);
  1457. var geometryProcessor = geometryTypes[childNode.localName];
  1458. if (defined(geometryProcessor)) {
  1459. // pass the placemark entity id as a context for case of defining multiple child entities together to handle case
  1460. // where some malformed kmls reuse the same id across placemarks, which works in GE, but is not technically to spec.
  1461. geometryProcessor(dataSource, entityCollection, childNode, entity, styleEntity, entity.id);
  1462. hasGeometry = true;
  1463. }
  1464. }
  1465. if (!hasGeometry) {
  1466. entity.merge(styleEntity);
  1467. processPositionGraphics(dataSource, entity, styleEntity);
  1468. }
  1469. }
  1470. function processGroundOverlay(dataSource, parent, groundOverlay, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1471. var r = processFeature(dataSource, parent, groundOverlay, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1472. var entity = r.entity;
  1473. var geometry;
  1474. var isLatLonQuad = false;
  1475. var positions = readCoordinates(queryFirstNode(groundOverlay, 'LatLonQuad', namespaces.gx));
  1476. if (defined(positions)) {
  1477. geometry = createDefaultPolygon();
  1478. geometry.hierarchy = new PolygonHierarchy(positions);
  1479. entity.polygon = geometry;
  1480. isLatLonQuad = true;
  1481. } else {
  1482. geometry = new RectangleGraphics();
  1483. entity.rectangle = geometry;
  1484. var latLonBox = queryFirstNode(groundOverlay, 'LatLonBox', namespaces.kml);
  1485. if (defined(latLonBox)) {
  1486. var west = queryNumericValue(latLonBox, 'west', namespaces.kml);
  1487. var south = queryNumericValue(latLonBox, 'south', namespaces.kml);
  1488. var east = queryNumericValue(latLonBox, 'east', namespaces.kml);
  1489. var north = queryNumericValue(latLonBox, 'north', namespaces.kml);
  1490. if (defined(west)) {
  1491. west = CesiumMath.negativePiToPi(CesiumMath.toRadians(west));
  1492. }
  1493. if (defined(south)) {
  1494. south = CesiumMath.clampToLatitudeRange(CesiumMath.toRadians(south));
  1495. }
  1496. if (defined(east)) {
  1497. east = CesiumMath.negativePiToPi(CesiumMath.toRadians(east));
  1498. }
  1499. if (defined(north)) {
  1500. north = CesiumMath.clampToLatitudeRange(CesiumMath.toRadians(north));
  1501. }
  1502. geometry.coordinates = new Rectangle(west, south, east, north);
  1503. var rotation = queryNumericValue(latLonBox, 'rotation', namespaces.kml);
  1504. if (defined(rotation)) {
  1505. geometry.rotation = CesiumMath.toRadians(rotation);
  1506. }
  1507. }
  1508. }
  1509. var iconNode = queryFirstNode(groundOverlay, 'Icon', namespaces.kml);
  1510. var href = getIconHref(iconNode, dataSource, sourceUri, uriResolver, true);
  1511. if (defined(href)) {
  1512. if (isLatLonQuad) {
  1513. console.log('KML - gx:LatLonQuad Icon does not support texture projection.');
  1514. }
  1515. var x = queryNumericValue(iconNode, 'x', namespaces.gx);
  1516. var y = queryNumericValue(iconNode, 'y', namespaces.gx);
  1517. var w = queryNumericValue(iconNode, 'w', namespaces.gx);
  1518. var h = queryNumericValue(iconNode, 'h', namespaces.gx);
  1519. if (defined(x) || defined(y) || defined(w) || defined(h)) {
  1520. console.log('KML - gx:x, gx:y, gx:w, gx:h aren\'t supported for GroundOverlays');
  1521. }
  1522. geometry.material = href;
  1523. geometry.material.color = queryColorValue(groundOverlay, 'color', namespaces.kml);
  1524. geometry.material.transparent = true;
  1525. } else {
  1526. geometry.material = queryColorValue(groundOverlay, 'color', namespaces.kml);
  1527. }
  1528. var altitudeMode = queryStringValue(groundOverlay, 'altitudeMode', namespaces.kml);
  1529. if (defined(altitudeMode)) {
  1530. if (altitudeMode === 'absolute') {
  1531. //Use height above ellipsoid until we support MSL.
  1532. geometry.height = queryNumericValue(groundOverlay, 'altitude', namespaces.kml);
  1533. } else if (altitudeMode !== 'clampToGround'){
  1534. console.log('KML - Unknown altitudeMode: ' + altitudeMode);
  1535. }
  1536. // else just use the default of 0 until we support 'clampToGround'
  1537. } else {
  1538. altitudeMode = queryStringValue(groundOverlay, 'altitudeMode', namespaces.gx);
  1539. if (altitudeMode === 'relativeToSeaFloor') {
  1540. console.log('KML - altitudeMode relativeToSeaFloor is currently not supported, treating as absolute.');
  1541. geometry.height = queryNumericValue(groundOverlay, 'altitude', namespaces.kml);
  1542. } else if (altitudeMode === 'clampToSeaFloor') {
  1543. console.log('KML - altitudeMode clampToSeaFloor is currently not supported, treating as clampToGround.');
  1544. } else if (defined(altitudeMode)) {
  1545. console.log('KML - Unknown altitudeMode: ' + altitudeMode);
  1546. }
  1547. }
  1548. }
  1549. function processUnsupportedFeature(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1550. dataSource._unsupportedNode.raiseEvent(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver);
  1551. console.log('KML - Unsupported feature: ' + node.localName);
  1552. }
  1553. var RefreshMode = {
  1554. INTERVAL : 0,
  1555. EXPIRE : 1,
  1556. STOP : 2
  1557. };
  1558. function cleanupString(s) {
  1559. if (!defined(s) || s.length === 0) {
  1560. return '';
  1561. }
  1562. var sFirst = s[0];
  1563. if (sFirst === '&') {
  1564. s.splice(0, 1);
  1565. }
  1566. if(sFirst !== '?') {
  1567. s = '?' + s;
  1568. }
  1569. return s;
  1570. }
  1571. function makeQueryString(string1, string2) {
  1572. var result = '';
  1573. if ((defined(string1) && string1.length > 0) || (defined(string2) && string2.length > 0)) {
  1574. result += joinUrls(cleanupString(string1), cleanupString(string2), false);
  1575. }
  1576. return result;
  1577. }
  1578. var zeroRectangle = new Rectangle();
  1579. var scratchCartographic = new Cartographic();
  1580. var scratchCartesian2 = new Cartesian2();
  1581. var scratchCartesian3 = new Cartesian3();
  1582. function processNetworkLinkQueryString(camera, canvas, queryString, viewBoundScale, bbox) {
  1583. function fixLatitude(value) {
  1584. if (value < -CesiumMath.PI_OVER_TWO) {
  1585. return -CesiumMath.PI_OVER_TWO;
  1586. } else if (value > CesiumMath.PI_OVER_TWO) {
  1587. return CesiumMath.PI_OVER_TWO;
  1588. }
  1589. return value;
  1590. }
  1591. function fixLongitude(value) {
  1592. if (value > CesiumMath.PI) {
  1593. return value - CesiumMath.TWO_PI;
  1594. } else if (value < -CesiumMath.PI) {
  1595. return value + CesiumMath.TWO_PI;
  1596. }
  1597. return value;
  1598. }
  1599. if (defined(camera) && camera._mode !== SceneMode.MORPHING) {
  1600. var wgs84 = Ellipsoid.WGS84;
  1601. var centerCartesian;
  1602. var centerCartographic;
  1603. bbox = defaultValue(bbox, zeroRectangle);
  1604. if (defined(canvas)) {
  1605. scratchCartesian2.x = canvas.clientWidth * 0.5;
  1606. scratchCartesian2.y = canvas.clientHeight * 0.5;
  1607. centerCartesian = camera.pickEllipsoid(scratchCartesian2, wgs84, scratchCartesian3);
  1608. }
  1609. if (defined(centerCartesian)) {
  1610. centerCartographic = wgs84.cartesianToCartographic(centerCartesian, scratchCartographic);
  1611. } else {
  1612. centerCartographic = Rectangle.center(bbox, scratchCartographic);
  1613. centerCartesian = wgs84.cartographicToCartesian(centerCartographic);
  1614. }
  1615. if (defined(viewBoundScale) && !CesiumMath.equalsEpsilon(viewBoundScale, 1.0, CesiumMath.EPSILON9)) {
  1616. var newHalfWidth = bbox.width * viewBoundScale * 0.5;
  1617. var newHalfHeight = bbox.height * viewBoundScale * 0.5;
  1618. bbox = new Rectangle(fixLongitude(centerCartographic.longitude - newHalfWidth),
  1619. fixLatitude(centerCartographic.latitude - newHalfHeight),
  1620. fixLongitude(centerCartographic.longitude + newHalfWidth),
  1621. fixLatitude(centerCartographic.latitude + newHalfHeight)
  1622. );
  1623. }
  1624. queryString = queryString.replace('[bboxWest]', CesiumMath.toDegrees(bbox.west).toString());
  1625. queryString = queryString.replace('[bboxSouth]', CesiumMath.toDegrees(bbox.south).toString());
  1626. queryString = queryString.replace('[bboxEast]', CesiumMath.toDegrees(bbox.east).toString());
  1627. queryString = queryString.replace('[bboxNorth]', CesiumMath.toDegrees(bbox.north).toString());
  1628. var lon = CesiumMath.toDegrees(centerCartographic.longitude).toString();
  1629. var lat = CesiumMath.toDegrees(centerCartographic.latitude).toString();
  1630. queryString = queryString.replace('[lookatLon]', lon);
  1631. queryString = queryString.replace('[lookatLat]', lat);
  1632. queryString = queryString.replace('[lookatTilt]', CesiumMath.toDegrees(camera.pitch).toString());
  1633. queryString = queryString.replace('[lookatHeading]', CesiumMath.toDegrees(camera.heading).toString());
  1634. queryString = queryString.replace('[lookatRange]', Cartesian3.distance(camera.positionWC, centerCartesian));
  1635. queryString = queryString.replace('[lookatTerrainLon]', lon);
  1636. queryString = queryString.replace('[lookatTerrainLat]', lat);
  1637. queryString = queryString.replace('[lookatTerrainAlt]', centerCartographic.height.toString());
  1638. wgs84.cartesianToCartographic(camera.positionWC, scratchCartographic);
  1639. queryString = queryString.replace('[cameraLon]', CesiumMath.toDegrees(scratchCartographic.longitude).toString());
  1640. queryString = queryString.replace('[cameraLat]', CesiumMath.toDegrees(scratchCartographic.latitude).toString());
  1641. queryString = queryString.replace('[cameraAlt]', CesiumMath.toDegrees(scratchCartographic.height).toString());
  1642. var frustum = camera.frustum;
  1643. var aspectRatio = frustum.aspectRatio;
  1644. var horizFov = '';
  1645. var vertFov = '';
  1646. if (defined(aspectRatio)) {
  1647. var fov = CesiumMath.toDegrees(frustum.fov);
  1648. if (aspectRatio > 1.0) {
  1649. horizFov = fov;
  1650. vertFov = fov / aspectRatio;
  1651. } else {
  1652. vertFov = fov;
  1653. horizFov = fov * aspectRatio;
  1654. }
  1655. }
  1656. queryString = queryString.replace('[horizFov]', horizFov.toString());
  1657. queryString = queryString.replace('[vertFov]', vertFov.toString());
  1658. } else {
  1659. queryString = queryString.replace('[bboxWest]', '-180');
  1660. queryString = queryString.replace('[bboxSouth]', '-90');
  1661. queryString = queryString.replace('[bboxEast]', '180');
  1662. queryString = queryString.replace('[bboxNorth]', '90');
  1663. queryString = queryString.replace('[lookatLon]', '');
  1664. queryString = queryString.replace('[lookatLat]', '');
  1665. queryString = queryString.replace('[lookatRange]', '');
  1666. queryString = queryString.replace('[lookatTilt]', '');
  1667. queryString = queryString.replace('[lookatHeading]', '');
  1668. queryString = queryString.replace('[lookatTerrainLon]', '');
  1669. queryString = queryString.replace('[lookatTerrainLat]', '');
  1670. queryString = queryString.replace('[lookatTerrainAlt]', '');
  1671. queryString = queryString.replace('[cameraLon]', '');
  1672. queryString = queryString.replace('[cameraLat]', '');
  1673. queryString = queryString.replace('[cameraAlt]', '');
  1674. queryString = queryString.replace('[horizFov]', '');
  1675. queryString = queryString.replace('[vertFov]', '');
  1676. }
  1677. if (defined(canvas)) {
  1678. queryString = queryString.replace('[horizPixels]', canvas.clientWidth);
  1679. queryString = queryString.replace('[vertPixels]', canvas.clientHeight);
  1680. } else {
  1681. queryString = queryString.replace('[horizPixels]', '');
  1682. queryString = queryString.replace('[vertPixels]', '');
  1683. }
  1684. queryString = queryString.replace('[terrainEnabled]', '1');
  1685. queryString = queryString.replace('[clientVersion]', '1');
  1686. queryString = queryString.replace('[kmlVersion]', '2.2');
  1687. queryString = queryString.replace('[clientName]', 'Cesium');
  1688. queryString = queryString.replace('[language]', 'English');
  1689. return queryString;
  1690. }
  1691. function processNetworkLink(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1692. var r = processFeature(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1693. var networkEntity = r.entity;
  1694. var link = queryFirstNode(node, 'Link', namespaces.kml);
  1695. if(!defined(link)){
  1696. link = queryFirstNode(node, 'Url', namespaces.kml);
  1697. }
  1698. if (defined(link)) {
  1699. var href = queryStringValue(link, 'href', namespaces.kml);
  1700. if (defined(href)) {
  1701. var newSourceUri = href;
  1702. href = resolveHref(href, undefined, sourceUri, uriResolver);
  1703. var linkUrl;
  1704. // We need to pass in the original path if resolveHref returns a data uri because the network link
  1705. // references a document in a KMZ archive
  1706. if (/^data:/.test(href)) {
  1707. // No need to build a query string for a data uri, just use as is
  1708. linkUrl = href;
  1709. // So if sourceUri isn't the kmz file, then its another kml in the archive, so resolve it
  1710. if (!/\.kmz/i.test(sourceUri)) {
  1711. newSourceUri = getAbsoluteUri(newSourceUri, sourceUri);
  1712. }
  1713. } else {
  1714. newSourceUri = href; // Not a data uri so use the fully qualified uri
  1715. var viewRefreshMode = queryStringValue(link, 'viewRefreshMode', namespaces.kml);
  1716. var viewBoundScale = defaultValue(queryStringValue(link, 'viewBoundScale', namespaces.kml), 1.0);
  1717. var defaultViewFormat = (viewRefreshMode === 'onStop') ? 'BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]' : '';
  1718. var viewFormat = defaultValue(queryStringValue(link, 'viewFormat', namespaces.kml), defaultViewFormat);
  1719. var httpQuery = queryStringValue(link, 'httpQuery', namespaces.kml);
  1720. var queryString = makeQueryString(viewFormat, httpQuery);
  1721. linkUrl = processNetworkLinkQueryString(dataSource._camera, dataSource._canvas, joinUrls(href, queryString, false),
  1722. viewBoundScale, dataSource._lastCameraView.bbox);
  1723. }
  1724. var options = {
  1725. sourceUri : newSourceUri,
  1726. uriResolver : uriResolver,
  1727. context : networkEntity.id
  1728. };
  1729. var networkLinkCollection = new EntityCollection();
  1730. var promise = load(dataSource, networkLinkCollection, linkUrl, options).then(function(rootElement) {
  1731. var entities = dataSource._entityCollection;
  1732. var newEntities = networkLinkCollection.values;
  1733. entities.suspendEvents();
  1734. for (var i = 0; i < newEntities.length; i++) {
  1735. var newEntity = newEntities[i];
  1736. if (!defined(newEntity.parent)) {
  1737. newEntity.parent = networkEntity;
  1738. mergeAvailabilityWithParent(newEntity);
  1739. }
  1740. entities.add(newEntity);
  1741. }
  1742. entities.resumeEvents();
  1743. // Add network links to a list if we need they will need to be updated
  1744. var refreshMode = queryStringValue(link, 'refreshMode', namespaces.kml);
  1745. var refreshInterval = defaultValue(queryNumericValue(link, 'refreshInterval', namespaces.kml), 0);
  1746. if ((refreshMode === 'onInterval' && refreshInterval > 0 ) || (refreshMode === 'onExpire') || (viewRefreshMode === 'onStop')) {
  1747. var networkLinkControl = queryFirstNode(rootElement, 'NetworkLinkControl', namespaces.kml);
  1748. var hasNetworkLinkControl = defined(networkLinkControl);
  1749. var now = JulianDate.now();
  1750. var networkLinkInfo = {
  1751. id : createGuid(),
  1752. href : href,
  1753. cookie : '',
  1754. queryString : queryString,
  1755. lastUpdated : now,
  1756. updating : false,
  1757. entity : networkEntity,
  1758. viewBoundScale : viewBoundScale,
  1759. needsUpdate : false,
  1760. cameraUpdateTime : now
  1761. };
  1762. var minRefreshPeriod = 0;
  1763. if (hasNetworkLinkControl) {
  1764. networkLinkInfo.cookie = defaultValue(queryStringValue(networkLinkControl, 'cookie', namespaces.kml), '');
  1765. minRefreshPeriod = defaultValue(queryNumericValue(networkLinkControl, 'minRefreshPeriod', namespaces.kml), 0);
  1766. }
  1767. if (refreshMode === 'onInterval') {
  1768. if (hasNetworkLinkControl) {
  1769. refreshInterval = Math.max(minRefreshPeriod, refreshInterval);
  1770. }
  1771. networkLinkInfo.refreshMode = RefreshMode.INTERVAL;
  1772. networkLinkInfo.time = refreshInterval;
  1773. } else if (refreshMode === 'onExpire') {
  1774. var expires;
  1775. if (hasNetworkLinkControl) {
  1776. expires = queryStringValue(networkLinkControl, 'expires', namespaces.kml);
  1777. }
  1778. if (defined(expires)) {
  1779. try {
  1780. var date = JulianDate.fromIso8601(expires);
  1781. var diff = JulianDate.secondsDifference(date, now);
  1782. if (diff > 0 && diff < minRefreshPeriod) {
  1783. JulianDate.addSeconds(now, minRefreshPeriod, date);
  1784. }
  1785. networkLinkInfo.refreshMode = RefreshMode.EXPIRE;
  1786. networkLinkInfo.time = date;
  1787. } catch (e) {
  1788. console.log('KML - NetworkLinkControl expires is not a valid date');
  1789. }
  1790. } else {
  1791. console.log('KML - refreshMode of onExpire requires the NetworkLinkControl to have an expires element');
  1792. }
  1793. } else {
  1794. if (dataSource._camera) { // Only allow onStop refreshes if we have a camera
  1795. networkLinkInfo.refreshMode = RefreshMode.STOP;
  1796. networkLinkInfo.time = defaultValue(queryNumericValue(link, 'viewRefreshTime', namespaces.kml), 0);
  1797. } else {
  1798. console.log('A NetworkLink with viewRefreshMode=onStop requires a camera be passed in when creating the KmlDataSource');
  1799. }
  1800. }
  1801. if (defined(networkLinkInfo.refreshMode)) {
  1802. dataSource._networkLinks.set(networkLinkInfo.id, networkLinkInfo);
  1803. }
  1804. } else if (viewRefreshMode === 'onRegion'){
  1805. console.log('KML - Unsupported viewRefreshMode: onRegion');
  1806. }
  1807. });
  1808. promises.push(promise);
  1809. }
  1810. }
  1811. }
  1812. // Ensure Specs/Data/KML/unsupported.kml is kept up to date with these supported types
  1813. var featureTypes = {
  1814. Document : processDocument,
  1815. Folder : processFolder,
  1816. Placemark : processPlacemark,
  1817. NetworkLink : processNetworkLink,
  1818. GroundOverlay : processGroundOverlay,
  1819. PhotoOverlay : processUnsupportedFeature,
  1820. ScreenOverlay : processUnsupportedFeature,
  1821. Tour : processUnsupportedFeature
  1822. };
  1823. function processFeatureNode(dataSource, node, parent, entityCollection, styleCollection, sourceUri, uriResolver, promises, context) {
  1824. var featureProcessor = featureTypes[node.localName];
  1825. if (defined(featureProcessor)) {
  1826. featureProcessor(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1827. } else {
  1828. processUnsupportedFeature(dataSource, parent, node, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1829. }
  1830. }
  1831. function loadKml(dataSource, entityCollection, kml, sourceUri, uriResolver, context) {
  1832. entityCollection.removeAll();
  1833. var promises = [];
  1834. var documentElement = kml.documentElement;
  1835. var document = documentElement.localName === 'Document' ? documentElement : queryFirstNode(documentElement, 'Document', namespaces.kml);
  1836. var name = queryStringValue(document, 'name', namespaces.kml);
  1837. if (!defined(name) && defined(sourceUri)) {
  1838. name = getFilenameFromUri(sourceUri);
  1839. }
  1840. // Only set the name from the root document
  1841. if (!defined(dataSource._name)) {
  1842. dataSource._name = name;
  1843. }
  1844. var styleCollection = new EntityCollection(dataSource);
  1845. return when.all(processStyles(dataSource, kml, styleCollection, sourceUri, false, uriResolver, context)).then(function() {
  1846. var element = kml.documentElement;
  1847. if (element.localName === 'kml') {
  1848. var childNodes = element.childNodes;
  1849. for (var i = 0; i < childNodes.length; i++) {
  1850. var tmp = childNodes[i];
  1851. if (defined(featureTypes[tmp.localName])) {
  1852. element = tmp;
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. entityCollection.suspendEvents();
  1858. processFeatureNode(dataSource, element, undefined, entityCollection, styleCollection, sourceUri, uriResolver, promises, context);
  1859. entityCollection.resumeEvents();
  1860. return when.all(promises).then(function() {
  1861. return kml.documentElement;
  1862. });
  1863. });
  1864. }
  1865. function loadKmz(dataSource, entityCollection, blob, sourceUri) {
  1866. var deferred = when.defer();
  1867. zip.createReader(new zip.BlobReader(blob), function(reader) {
  1868. reader.getEntries(function(entries) {
  1869. var promises = [];
  1870. var uriResolver = {};
  1871. var docEntry;
  1872. var docDefer;
  1873. for (var i = 0; i < entries.length; i++) {
  1874. var entry = entries[i];
  1875. if (!entry.directory) {
  1876. var innerDefer = when.defer();
  1877. promises.push(innerDefer.promise);
  1878. if (/\.kml$/i.test(entry.filename)) {
  1879. // We use the first KML document we come across
  1880. // https://developers.google.com/kml/documentation/kmzarchives
  1881. // Unless we come across a .kml file at the root of the archive because GE does this
  1882. if (!defined(docEntry) || !/\//i.test(entry.filename)) {
  1883. if (defined(docEntry)) {
  1884. // We found one at the root so load the initial kml as a data uri
  1885. loadDataUriFromZip(reader, docEntry, uriResolver, docDefer);
  1886. }
  1887. docEntry = entry;
  1888. docDefer = innerDefer;
  1889. } else {
  1890. // Wasn't the first kml and wasn't at the root
  1891. loadDataUriFromZip(reader, entry, uriResolver, innerDefer);
  1892. }
  1893. } else {
  1894. loadDataUriFromZip(reader, entry, uriResolver, innerDefer);
  1895. }
  1896. }
  1897. }
  1898. // Now load the root KML document
  1899. if (defined(docEntry)) {
  1900. loadXmlFromZip(reader, docEntry, uriResolver, docDefer);
  1901. }
  1902. when.all(promises).then(function() {
  1903. reader.close();
  1904. if (!defined(uriResolver.kml)) {
  1905. deferred.reject(new RuntimeError('KMZ file does not contain a KML document.'));
  1906. return;
  1907. }
  1908. uriResolver.keys = Object.keys(uriResolver);
  1909. return loadKml(dataSource, entityCollection, uriResolver.kml, sourceUri, uriResolver);
  1910. }).then(deferred.resolve).otherwise(deferred.reject);
  1911. });
  1912. }, function(e) {
  1913. deferred.reject(e);
  1914. });
  1915. return deferred.promise;
  1916. }
  1917. function load(dataSource, entityCollection, data, options) {
  1918. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  1919. var sourceUri = options.sourceUri;
  1920. var uriResolver = options.uriResolver;
  1921. var context = options.context;
  1922. var promise = data;
  1923. if (typeof data === 'string') {
  1924. promise = loadBlob(proxyUrl(data, dataSource._proxy));
  1925. sourceUri = defaultValue(sourceUri, data);
  1926. }
  1927. return when(promise)
  1928. .then(function(dataToLoad) {
  1929. if (dataToLoad instanceof Blob) {
  1930. return isZipFile(dataToLoad).then(function(isZip) {
  1931. if (isZip) {
  1932. return loadKmz(dataSource, entityCollection, dataToLoad, sourceUri);
  1933. }
  1934. return readBlobAsText(dataToLoad).then(function(text) {
  1935. //There's no official way to validate if a parse was successful.
  1936. //The following check detects the error on various browsers.
  1937. //IE raises an exception
  1938. var kml;
  1939. var error;
  1940. try {
  1941. kml = parser.parseFromString(text, 'application/xml');
  1942. } catch (e) {
  1943. error = e.toString();
  1944. }
  1945. //The parse succeeds on Chrome and Firefox, but the error
  1946. //handling is different in each.
  1947. if (defined(error) || kml.body || kml.documentElement.tagName === 'parsererror') {
  1948. //Firefox has error information as the firstChild nodeValue.
  1949. var msg = defined(error) ? error : kml.documentElement.firstChild.nodeValue;
  1950. //Chrome has it in the body text.
  1951. if (!msg) {
  1952. msg = kml.body.innerText;
  1953. }
  1954. //Return the error
  1955. throw new RuntimeError(msg);
  1956. }
  1957. return loadKml(dataSource, entityCollection, kml, sourceUri, uriResolver, context);
  1958. });
  1959. });
  1960. } else {
  1961. return loadKml(dataSource, entityCollection, dataToLoad, sourceUri, uriResolver, context);
  1962. }
  1963. })
  1964. .otherwise(function(error) {
  1965. dataSource._error.raiseEvent(dataSource, error);
  1966. console.log(error);
  1967. return when.reject(error);
  1968. });
  1969. }
  1970. /**
  1971. * A {@link DataSource} which processes Keyhole Markup Language 2.2 (KML).
  1972. * <p>
  1973. * KML support in Cesium is incomplete, but a large amount of the standard,
  1974. * as well as Google's <code>gx</code> extension namespace, is supported. See Github issue
  1975. * {@link https://github.com/AnalyticalGraphicsInc/cesium/issues/873|#873} for a
  1976. * detailed list of what is and isn't support. Cesium will also write information to the
  1977. * console when it encounters most unsupported features.
  1978. * </p>
  1979. * <p>
  1980. * Non visual feature data, such as <code>atom:author</code> and <code>ExtendedData</code>
  1981. * is exposed via an instance of {@link KmlFeatureData}, which is added to each {@link Entity}
  1982. * under the <code>kml</code> property.
  1983. * </p>
  1984. *
  1985. * @alias KmlDataSource
  1986. * @constructor
  1987. *
  1988. * @param {Camera} options.camera The camera that is used for viewRefreshModes and sending camera properties to network links.
  1989. * @param {Canvas} options.canvas The canvas that is used for sending viewer properties to network links.
  1990. * @param {DefaultProxy} [options.proxy] A proxy to be used for loading external data.
  1991. *
  1992. * @see {@link http://www.opengeospatial.org/standards/kml/|Open Geospatial Consortium KML Standard}
  1993. * @see {@link https://developers.google.com/kml/|Google KML Documentation}
  1994. *
  1995. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=KML.html|Cesium Sandcastle KML Demo}
  1996. *
  1997. * @example
  1998. * var viewer = new Cesium.Viewer('cesiumContainer');
  1999. * viewer.dataSources.add(Cesium.KmlDataSource.load('../../SampleData/facilities.kmz'),
  2000. * {
  2001. * camera: viewer.scene.camera,
  2002. * canvas: viewer.scene.canvas
  2003. * });
  2004. */
  2005. function KmlDataSource(options) {
  2006. options = defaultValue(options, {});
  2007. var camera = options.camera;
  2008. var canvas = options.canvas;
  2009. //>>includeStart('debug', pragmas.debug);
  2010. if (!defined(camera)) {
  2011. throw new DeveloperError('options.camera is required.');
  2012. }
  2013. if (!defined(canvas)) {
  2014. throw new DeveloperError('options.canvas is required.');
  2015. }
  2016. //>>includeEnd('debug');
  2017. this._changed = new Event();
  2018. this._error = new Event();
  2019. this._loading = new Event();
  2020. this._refresh = new Event();
  2021. this._unsupportedNode = new Event();
  2022. this._clock = undefined;
  2023. this._entityCollection = new EntityCollection(this);
  2024. this._name = undefined;
  2025. this._isLoading = false;
  2026. this._proxy = options.proxy;
  2027. this._pinBuilder = new PinBuilder();
  2028. this._networkLinks = new AssociativeArray();
  2029. this._entityCluster = new EntityCluster();
  2030. this._canvas = canvas;
  2031. this._camera = camera;
  2032. this._lastCameraView = {
  2033. position : defined(camera) ? Cartesian3.clone(camera.positionWC) : undefined,
  2034. direction : defined(camera) ? Cartesian3.clone(camera.directionWC) : undefined,
  2035. up : defined(camera) ? Cartesian3.clone(camera.upWC) : undefined,
  2036. bbox : defined(camera) ? camera.computeViewRectangle() : Rectangle.clone(Rectangle.MAX_VALUE)
  2037. };
  2038. }
  2039. /**
  2040. * Creates a Promise to a new instance loaded with the provided KML data.
  2041. *
  2042. * @param {String|Document|Blob} data A url, parsed KML document, or Blob containing binary KMZ data or a parsed KML document.
  2043. * @param {Object} [options] An object with the following properties:
  2044. * @param {Camera} options.camera The camera that is used for viewRefreshModes and sending camera properties to network links.
  2045. * @param {Canvas} options.canvas The canvas that is used for sending viewer properties to network links.
  2046. * @param {DefaultProxy} [options.proxy] A proxy to be used for loading external data.
  2047. * @param {String} [options.sourceUri] Overrides the url to use for resolving relative links and other KML network features.
  2048. * @param {Boolean} [options.clampToGround=false] true if we want the geometry features (Polygons, LineStrings and LinearRings) clamped to the ground. If true, lines will use corridors so use Entity.corridor instead of Entity.polyline.
  2049. *
  2050. * @returns {Promise.<KmlDataSource>} A promise that will resolve to a new KmlDataSource instance once the KML is loaded.
  2051. */
  2052. KmlDataSource.load = function(data, options) {
  2053. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  2054. var dataSource = new KmlDataSource(options);
  2055. return dataSource.load(data, options);
  2056. };
  2057. defineProperties(KmlDataSource.prototype, {
  2058. /**
  2059. * Gets a human-readable name for this instance.
  2060. * This will be automatically be set to the KML document name on load.
  2061. * @memberof KmlDataSource.prototype
  2062. * @type {String}
  2063. */
  2064. name : {
  2065. get : function() {
  2066. return this._name;
  2067. }
  2068. },
  2069. /**
  2070. * Gets the clock settings defined by the loaded KML. This represents the total
  2071. * availability interval for all time-dynamic data. If the KML does not contain
  2072. * time-dynamic data, this value is undefined.
  2073. * @memberof KmlDataSource.prototype
  2074. * @type {DataSourceClock}
  2075. */
  2076. clock : {
  2077. get : function() {
  2078. return this._clock;
  2079. }
  2080. },
  2081. /**
  2082. * Gets the collection of {@link Entity} instances.
  2083. * @memberof KmlDataSource.prototype
  2084. * @type {EntityCollection}
  2085. */
  2086. entities : {
  2087. get : function() {
  2088. return this._entityCollection;
  2089. }
  2090. },
  2091. /**
  2092. * Gets a value indicating if the data source is currently loading data.
  2093. * @memberof KmlDataSource.prototype
  2094. * @type {Boolean}
  2095. */
  2096. isLoading : {
  2097. get : function() {
  2098. return this._isLoading;
  2099. }
  2100. },
  2101. /**
  2102. * Gets an event that will be raised when the underlying data changes.
  2103. * @memberof KmlDataSource.prototype
  2104. * @type {Event}
  2105. */
  2106. changedEvent : {
  2107. get : function() {
  2108. return this._changed;
  2109. }
  2110. },
  2111. /**
  2112. * Gets an event that will be raised if an error is encountered during processing.
  2113. * @memberof KmlDataSource.prototype
  2114. * @type {Event}
  2115. */
  2116. errorEvent : {
  2117. get : function() {
  2118. return this._error;
  2119. }
  2120. },
  2121. /**
  2122. * Gets an event that will be raised when the data source either starts or stops loading.
  2123. * @memberof KmlDataSource.prototype
  2124. * @type {Event}
  2125. */
  2126. loadingEvent : {
  2127. get : function() {
  2128. return this._loading;
  2129. }
  2130. },
  2131. /**
  2132. * Gets an event that will be raised when the data source refreshes a network link.
  2133. * @memberof KmlDataSource.prototype
  2134. * @type {Event}
  2135. */
  2136. refreshEvent : {
  2137. get : function() {
  2138. return this._refresh;
  2139. }
  2140. },
  2141. /**
  2142. * Gets an event that will be raised when the data source finds an unsupported node type.
  2143. * @memberof KmlDataSource.prototype
  2144. * @type {Event}
  2145. */
  2146. unsupportedNodeEvent : {
  2147. get : function() {
  2148. return this._unsupportedNode;
  2149. }
  2150. },
  2151. /**
  2152. * Gets whether or not this data source should be displayed.
  2153. * @memberof KmlDataSource.prototype
  2154. * @type {Boolean}
  2155. */
  2156. show : {
  2157. get : function() {
  2158. return this._entityCollection.show;
  2159. },
  2160. set : function(value) {
  2161. this._entityCollection.show = value;
  2162. }
  2163. },
  2164. /**
  2165. * Gets or sets the clustering options for this data source. This object can be shared between multiple data sources.
  2166. *
  2167. * @memberof KmlDataSource.prototype
  2168. * @type {EntityCluster}
  2169. */
  2170. clustering : {
  2171. get : function() {
  2172. return this._entityCluster;
  2173. },
  2174. set : function(value) {
  2175. //>>includeStart('debug', pragmas.debug);
  2176. if (!defined(value)) {
  2177. throw new DeveloperError('value must be defined.');
  2178. }
  2179. //>>includeEnd('debug');
  2180. this._entityCluster = value;
  2181. }
  2182. }
  2183. });
  2184. /**
  2185. * Asynchronously loads the provided KML data, replacing any existing data.
  2186. *
  2187. * @param {String|Document|Blob} data A url, parsed KML document, or Blob containing binary KMZ data or a parsed KML document.
  2188. * @param {Object} [options] An object with the following properties:
  2189. * @param {Number} [options.sourceUri] Overrides the url to use for resolving relative links and other KML network features.
  2190. * @returns {Promise.<KmlDataSource>} A promise that will resolve to this instances once the KML is loaded.
  2191. * @param {Boolean} [options.clampToGround=false] true if we want the geometry features (Polygons, LineStrings and LinearRings) clamped to the ground. If true, lines will use corridors so use Entity.corridor instead of Entity.polyline.
  2192. */
  2193. KmlDataSource.prototype.load = function(data, options) {
  2194. //>>includeStart('debug', pragmas.debug);
  2195. if (!defined(data)) {
  2196. throw new DeveloperError('data is required.');
  2197. }
  2198. //>>includeEnd('debug');
  2199. options = defaultValue(options, {});
  2200. DataSource.setLoading(this, true);
  2201. var oldName = this._name;
  2202. this._name = undefined;
  2203. this._clampToGround = defaultValue(options.clampToGround, false);
  2204. var that = this;
  2205. return load(this, this._entityCollection, data, options).then(function() {
  2206. var clock;
  2207. var availability = that._entityCollection.computeAvailability();
  2208. var start = availability.start;
  2209. var stop = availability.stop;
  2210. var isMinStart = JulianDate.equals(start, Iso8601.MINIMUM_VALUE);
  2211. var isMaxStop = JulianDate.equals(stop, Iso8601.MAXIMUM_VALUE);
  2212. if (!isMinStart || !isMaxStop) {
  2213. var date;
  2214. //If start is min time just start at midnight this morning, local time
  2215. if (isMinStart) {
  2216. date = new Date();
  2217. date.setHours(0, 0, 0, 0);
  2218. start = JulianDate.fromDate(date);
  2219. }
  2220. //If stop is max value just stop at midnight tonight, local time
  2221. if (isMaxStop) {
  2222. date = new Date();
  2223. date.setHours(24, 0, 0, 0);
  2224. stop = JulianDate.fromDate(date);
  2225. }
  2226. clock = new DataSourceClock();
  2227. clock.startTime = start;
  2228. clock.stopTime = stop;
  2229. clock.currentTime = JulianDate.clone(start);
  2230. clock.clockRange = ClockRange.LOOP_STOP;
  2231. clock.clockStep = ClockStep.SYSTEM_CLOCK_MULTIPLIER;
  2232. clock.multiplier = Math.round(Math.min(Math.max(JulianDate.secondsDifference(stop, start) / 60, 1), 3.15569e7));
  2233. }
  2234. var changed = false;
  2235. if (clock !== that._clock) {
  2236. that._clock = clock;
  2237. changed = true;
  2238. }
  2239. if (oldName !== that._name) {
  2240. changed = true;
  2241. }
  2242. if (changed) {
  2243. that._changed.raiseEvent(that);
  2244. }
  2245. DataSource.setLoading(that, false);
  2246. return that;
  2247. }).otherwise(function(error) {
  2248. DataSource.setLoading(that, false);
  2249. that._error.raiseEvent(that, error);
  2250. console.log(error);
  2251. return when.reject(error);
  2252. });
  2253. };
  2254. function mergeAvailabilityWithParent(child) {
  2255. var parent = child.parent;
  2256. if (defined(parent)) {
  2257. var parentAvailability = parent.availability;
  2258. if (defined(parentAvailability)) {
  2259. var childAvailability = child.availability;
  2260. if (defined(childAvailability)) {
  2261. childAvailability.intersect(parentAvailability);
  2262. } else {
  2263. child.availability = parentAvailability;
  2264. }
  2265. }
  2266. }
  2267. }
  2268. function getNetworkLinkUpdateCallback(dataSource, networkLink, newEntityCollection, networkLinks, processedHref) {
  2269. return function(rootElement) {
  2270. if (!networkLinks.contains(networkLink.id)) {
  2271. // Got into the odd case where a parent network link was updated while a child
  2272. // network link update was in flight, so just throw it away.
  2273. return;
  2274. }
  2275. var remove = false;
  2276. var networkLinkControl = queryFirstNode(rootElement, 'NetworkLinkControl', namespaces.kml);
  2277. var hasNetworkLinkControl = defined(networkLinkControl);
  2278. var minRefreshPeriod = 0;
  2279. if (hasNetworkLinkControl) {
  2280. if (defined(queryFirstNode(networkLinkControl, 'Update', namespaces.kml))) {
  2281. console.log('KML - NetworkLinkControl updates aren\'t supported.');
  2282. networkLink.updating = false;
  2283. networkLinks.remove(networkLink.id);
  2284. return;
  2285. }
  2286. networkLink.cookie = defaultValue(queryStringValue(networkLinkControl, 'cookie', namespaces.kml), '');
  2287. minRefreshPeriod = defaultValue(queryNumericValue(networkLinkControl, 'minRefreshPeriod', namespaces.kml), 0);
  2288. }
  2289. var now = JulianDate.now();
  2290. var refreshMode = networkLink.refreshMode;
  2291. if (refreshMode === RefreshMode.INTERVAL) {
  2292. if (defined(networkLinkControl)) {
  2293. networkLink.time = Math.max(minRefreshPeriod, networkLink.time);
  2294. }
  2295. } else if (refreshMode === RefreshMode.EXPIRE) {
  2296. var expires;
  2297. if (defined(networkLinkControl)) {
  2298. expires = queryStringValue(networkLinkControl, 'expires', namespaces.kml);
  2299. }
  2300. if (defined(expires)) {
  2301. try {
  2302. var date = JulianDate.fromIso8601(expires);
  2303. var diff = JulianDate.secondsDifference(date, now);
  2304. if (diff > 0 && diff < minRefreshPeriod) {
  2305. JulianDate.addSeconds(now, minRefreshPeriod, date);
  2306. }
  2307. networkLink.time = date;
  2308. } catch (e) {
  2309. console.log('KML - NetworkLinkControl expires is not a valid date');
  2310. remove = true;
  2311. }
  2312. } else {
  2313. console.log('KML - refreshMode of onExpire requires the NetworkLinkControl to have an expires element');
  2314. remove = true;
  2315. }
  2316. }
  2317. var networkLinkEntity = networkLink.entity;
  2318. var entityCollection = dataSource._entityCollection;
  2319. var newEntities = newEntityCollection.values;
  2320. function removeChildren(entity) {
  2321. entityCollection.remove(entity);
  2322. var children = entity._children;
  2323. var count = children.length;
  2324. for(var i=0;i<count;++i) {
  2325. removeChildren(children[i]);
  2326. }
  2327. }
  2328. // Remove old entities
  2329. entityCollection.suspendEvents();
  2330. var entitiesCopy = entityCollection.values.slice();
  2331. for (var i=0;i<entitiesCopy.length;++i) {
  2332. var entityToRemove = entitiesCopy[i];
  2333. if (entityToRemove.parent === networkLinkEntity) {
  2334. entityToRemove.parent = undefined;
  2335. removeChildren(entityToRemove);
  2336. }
  2337. }
  2338. entityCollection.resumeEvents();
  2339. // Add new entities
  2340. entityCollection.suspendEvents();
  2341. for (i = 0; i < newEntities.length; i++) {
  2342. var newEntity = newEntities[i];
  2343. if (!defined(newEntity.parent)) {
  2344. newEntity.parent = networkLinkEntity;
  2345. mergeAvailabilityWithParent(newEntity);
  2346. }
  2347. entityCollection.add(newEntity);
  2348. }
  2349. entityCollection.resumeEvents();
  2350. // No refresh information remove it, otherwise update lastUpdate time
  2351. if (remove) {
  2352. networkLinks.remove(networkLink.id);
  2353. } else {
  2354. networkLink.lastUpdated = now;
  2355. }
  2356. var availability = entityCollection.computeAvailability();
  2357. var start = availability.start;
  2358. var stop = availability.stop;
  2359. var isMinStart = JulianDate.equals(start, Iso8601.MINIMUM_VALUE);
  2360. var isMaxStop = JulianDate.equals(stop, Iso8601.MAXIMUM_VALUE);
  2361. if (!isMinStart || !isMaxStop) {
  2362. var clock = dataSource._clock;
  2363. if (clock.startTime !== start || clock.stopTime !== stop) {
  2364. clock.startTime = start;
  2365. clock.stopTime = stop;
  2366. dataSource._changed.raiseEvent(dataSource);
  2367. }
  2368. }
  2369. networkLink.updating = false;
  2370. networkLink.needsUpdate = false;
  2371. dataSource._refresh.raiseEvent(dataSource, processedHref);
  2372. };
  2373. }
  2374. var entitiesToIgnore = new AssociativeArray();
  2375. /**
  2376. * Updates any NetworkLink that require updating
  2377. * @function
  2378. *
  2379. * @param {JulianDate} time The simulation time.
  2380. * @returns {Boolean} True if this data source is ready to be displayed at the provided time, false otherwise.
  2381. */
  2382. KmlDataSource.prototype.update = function(time) {
  2383. var networkLinks = this._networkLinks;
  2384. if (networkLinks.length === 0) {
  2385. return true;
  2386. }
  2387. var now = JulianDate.now();
  2388. var that = this;
  2389. entitiesToIgnore.removeAll();
  2390. function recurseIgnoreEntities(entity) {
  2391. var children = entity._children;
  2392. var count = children.length;
  2393. for (var i=0;i<count;++i) {
  2394. var child = children[i];
  2395. entitiesToIgnore.set(child.id, child);
  2396. recurseIgnoreEntities(child);
  2397. }
  2398. }
  2399. var cameraViewUpdate = false;
  2400. var lastCameraView = this._lastCameraView;
  2401. var camera = this._camera;
  2402. if (defined(camera) &&
  2403. !(camera.positionWC.equalsEpsilon(lastCameraView.position, CesiumMath.EPSILON7) &&
  2404. camera.directionWC.equalsEpsilon(lastCameraView.direction, CesiumMath.EPSILON7) &&
  2405. camera.upWC.equalsEpsilon(lastCameraView.up, CesiumMath.EPSILON7))) {
  2406. // Camera has changed so update the last view
  2407. lastCameraView.position = Cartesian3.clone(camera.positionWC);
  2408. lastCameraView.direction = Cartesian3.clone(camera.directionWC);
  2409. lastCameraView.up = Cartesian3.clone(camera.upWC);
  2410. lastCameraView.bbox = camera.computeViewRectangle();
  2411. cameraViewUpdate = true;
  2412. }
  2413. var newNetworkLinks = new AssociativeArray();
  2414. var changed = false;
  2415. networkLinks.values.forEach(function(networkLink) {
  2416. var entity = networkLink.entity;
  2417. if (entitiesToIgnore.contains(entity.id)) {
  2418. return;
  2419. }
  2420. if (!networkLink.updating) {
  2421. var doUpdate = false;
  2422. if (networkLink.refreshMode === RefreshMode.INTERVAL) {
  2423. if (JulianDate.secondsDifference(now, networkLink.lastUpdated) > networkLink.time) {
  2424. doUpdate = true;
  2425. }
  2426. }
  2427. else if (networkLink.refreshMode === RefreshMode.EXPIRE) {
  2428. if (JulianDate.greaterThan(now, networkLink.time)) {
  2429. doUpdate = true;
  2430. }
  2431. } else if (networkLink.refreshMode === RefreshMode.STOP) {
  2432. if (cameraViewUpdate) {
  2433. networkLink.needsUpdate = true;
  2434. networkLink.cameraUpdateTime = now;
  2435. }
  2436. if (networkLink.needsUpdate && JulianDate.secondsDifference(now, networkLink.cameraUpdateTime) >= networkLink.time) {
  2437. doUpdate = true;
  2438. }
  2439. }
  2440. if (doUpdate) {
  2441. recurseIgnoreEntities(entity);
  2442. networkLink.updating = true;
  2443. var newEntityCollection = new EntityCollection();
  2444. var href = joinUrls(networkLink.href, makeQueryString(networkLink.cookie, networkLink.queryString), false);
  2445. href = processNetworkLinkQueryString(that._camera, that._canvas, href, networkLink.viewBoundScale, lastCameraView.bbox);
  2446. load(that, newEntityCollection, href, {context: entity.id})
  2447. .then(getNetworkLinkUpdateCallback(that, networkLink, newEntityCollection, newNetworkLinks, href))
  2448. .otherwise(function(error) {
  2449. var msg = 'NetworkLink ' + networkLink.href + ' refresh failed: ' + error;
  2450. console.log(msg);
  2451. that._error.raiseEvent(that, msg);
  2452. });
  2453. changed = true;
  2454. }
  2455. }
  2456. newNetworkLinks.set(networkLink.id, networkLink);
  2457. });
  2458. if (changed) {
  2459. this._networkLinks = newNetworkLinks;
  2460. this._changed.raiseEvent(this);
  2461. }
  2462. return true;
  2463. };
  2464. /**
  2465. * Contains KML Feature data loaded into the <code>Entity.kml</code> property by {@link KmlDataSource}.
  2466. * @alias KmlFeatureData
  2467. * @constructor
  2468. */
  2469. function KmlFeatureData() {
  2470. /**
  2471. * Gets the atom syndication format author field.
  2472. * @type Object
  2473. */
  2474. this.author = {
  2475. /**
  2476. * Gets the name.
  2477. * @type String
  2478. * @alias author.name
  2479. * @memberof! KmlFeatureData#
  2480. * @property author.name
  2481. */
  2482. name : undefined,
  2483. /**
  2484. * Gets the URI.
  2485. * @type String
  2486. * @alias author.uri
  2487. * @memberof! KmlFeatureData#
  2488. * @property author.uri
  2489. */
  2490. uri : undefined,
  2491. /**
  2492. * Gets the email.
  2493. * @type String
  2494. * @alias author.email
  2495. * @memberof! KmlFeatureData#
  2496. * @property author.email
  2497. */
  2498. email : undefined
  2499. };
  2500. /**
  2501. * Gets the link.
  2502. * @type Object
  2503. */
  2504. this.link = {
  2505. /**
  2506. * Gets the href.
  2507. * @type String
  2508. * @alias link.href
  2509. * @memberof! KmlFeatureData#
  2510. * @property link.href
  2511. */
  2512. href : undefined,
  2513. /**
  2514. * Gets the language of the linked resource.
  2515. * @type String
  2516. * @alias link.hreflang
  2517. * @memberof! KmlFeatureData#
  2518. * @property link.hreflang
  2519. */
  2520. hreflang : undefined,
  2521. /**
  2522. * Gets the link relation.
  2523. * @type String
  2524. * @alias link.rel
  2525. * @memberof! KmlFeatureData#
  2526. * @property link.rel
  2527. */
  2528. rel : undefined,
  2529. /**
  2530. * Gets the link type.
  2531. * @type String
  2532. * @alias link.type
  2533. * @memberof! KmlFeatureData#
  2534. * @property link.type
  2535. */
  2536. type : undefined,
  2537. /**
  2538. * Gets the link title.
  2539. * @type String
  2540. * @alias link.title
  2541. * @memberof! KmlFeatureData#
  2542. * @property link.title
  2543. */
  2544. title : undefined,
  2545. /**
  2546. * Gets the link length.
  2547. * @type String
  2548. * @alias link.length
  2549. * @memberof! KmlFeatureData#
  2550. * @property link.length
  2551. */
  2552. length : undefined
  2553. };
  2554. /**
  2555. * Gets the unstructured address field.
  2556. * @type String
  2557. */
  2558. this.address = undefined;
  2559. /**
  2560. * Gets the phone number.
  2561. * @type String
  2562. */
  2563. this.phoneNumber = undefined;
  2564. /**
  2565. * Gets the snippet.
  2566. * @type String
  2567. */
  2568. this.snippet = undefined;
  2569. /**
  2570. * Gets the extended data, parsed into a JSON object.
  2571. * Currently only the <code>Data</code> property is supported.
  2572. * <code>SchemaData</code> and custom data are ignored.
  2573. * @type String
  2574. */
  2575. this.extendedData = undefined;
  2576. }
  2577. return KmlDataSource;
  2578. });