KmlDataSource.js 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324
  1. import ArcType from "../Core/ArcType.js";
  2. import AssociativeArray from "../Core/AssociativeArray.js";
  3. import BoundingRectangle from "../Core/BoundingRectangle.js";
  4. import buildModuleUrl from "../Core/buildModuleUrl.js";
  5. import Cartesian2 from "../Core/Cartesian2.js";
  6. import Cartesian3 from "../Core/Cartesian3.js";
  7. import Cartographic from "../Core/Cartographic.js";
  8. import ClockRange from "../Core/ClockRange.js";
  9. import ClockStep from "../Core/ClockStep.js";
  10. import clone from "../Core/clone.js";
  11. import Color from "../Core/Color.js";
  12. import createGuid from "../Core/createGuid.js";
  13. import Credit from "../Core/Credit.js";
  14. import defaultValue from "../Core/defaultValue.js";
  15. import defer from "../Core/defer.js";
  16. import defined from "../Core/defined.js";
  17. import DeveloperError from "../Core/DeveloperError.js";
  18. import Ellipsoid from "../Core/Ellipsoid.js";
  19. import Event from "../Core/Event.js";
  20. import getExtensionFromUri from "../Core/getExtensionFromUri.js";
  21. import getFilenameFromUri from "../Core/getFilenameFromUri.js";
  22. import getTimestamp from "../Core/getTimestamp.js";
  23. import HeadingPitchRange from "../Core/HeadingPitchRange.js";
  24. import HeadingPitchRoll from "../Core/HeadingPitchRoll.js";
  25. import Iso8601 from "../Core/Iso8601.js";
  26. import JulianDate from "../Core/JulianDate.js";
  27. import CesiumMath from "../Core/Math.js";
  28. import NearFarScalar from "../Core/NearFarScalar.js";
  29. import objectToQuery from "../Core/objectToQuery.js";
  30. import oneTimeWarning from "../Core/oneTimeWarning.js";
  31. import PinBuilder from "../Core/PinBuilder.js";
  32. import PolygonHierarchy from "../Core/PolygonHierarchy.js";
  33. import queryToObject from "../Core/queryToObject.js";
  34. import Rectangle from "../Core/Rectangle.js";
  35. import Resource from "../Core/Resource.js";
  36. import RuntimeError from "../Core/RuntimeError.js";
  37. import TimeInterval from "../Core/TimeInterval.js";
  38. import TimeIntervalCollection from "../Core/TimeIntervalCollection.js";
  39. import HeightReference from "../Scene/HeightReference.js";
  40. import HorizontalOrigin from "../Scene/HorizontalOrigin.js";
  41. import LabelStyle from "../Scene/LabelStyle.js";
  42. import SceneMode from "../Scene/SceneMode.js";
  43. import Autolinker from "autolinker";
  44. import Uri from "urijs";
  45. import * as zip from "@zip.js/zip.js/lib/zip-no-worker.js";
  46. import getElement from "./getElement.js";
  47. import BillboardGraphics from "./BillboardGraphics.js";
  48. import CompositePositionProperty from "./CompositePositionProperty.js";
  49. import DataSource from "./DataSource.js";
  50. import DataSourceClock from "./DataSourceClock.js";
  51. import Entity from "./Entity.js";
  52. import EntityCluster from "./EntityCluster.js";
  53. import EntityCollection from "./EntityCollection.js";
  54. import KmlCamera from "./KmlCamera.js";
  55. import KmlLookAt from "./KmlLookAt.js";
  56. import KmlTour from "./KmlTour.js";
  57. import KmlTourFlyTo from "./KmlTourFlyTo.js";
  58. import KmlTourWait from "./KmlTourWait.js";
  59. import LabelGraphics from "./LabelGraphics.js";
  60. import PathGraphics from "./PathGraphics.js";
  61. import PolygonGraphics from "./PolygonGraphics.js";
  62. import PolylineGraphics from "./PolylineGraphics.js";
  63. import PositionPropertyArray from "./PositionPropertyArray.js";
  64. import RectangleGraphics from "./RectangleGraphics.js";
  65. import ReferenceProperty from "./ReferenceProperty.js";
  66. import SampledPositionProperty from "./SampledPositionProperty.js";
  67. import ScaledPositionProperty from "./ScaledPositionProperty.js";
  68. import TimeIntervalCollectionProperty from "./TimeIntervalCollectionProperty.js";
  69. import WallGraphics from "./WallGraphics.js";
  70. //This is by no means an exhaustive list of MIME types.
  71. //The purpose of this list is to be able to accurately identify content embedded
  72. //in KMZ files. Eventually, we can make this configurable by the end user so they can add
  73. //there own content types if they have KMZ files that require it.
  74. const MimeTypes = {
  75. avi: "video/x-msvideo",
  76. bmp: "image/bmp",
  77. bz2: "application/x-bzip2",
  78. chm: "application/vnd.ms-htmlhelp",
  79. css: "text/css",
  80. csv: "text/csv",
  81. doc: "application/msword",
  82. dvi: "application/x-dvi",
  83. eps: "application/postscript",
  84. flv: "video/x-flv",
  85. gif: "image/gif",
  86. gz: "application/x-gzip",
  87. htm: "text/html",
  88. html: "text/html",
  89. ico: "image/vnd.microsoft.icon",
  90. jnlp: "application/x-java-jnlp-file",
  91. jpeg: "image/jpeg",
  92. jpg: "image/jpeg",
  93. m3u: "audio/x-mpegurl",
  94. m4v: "video/mp4",
  95. mathml: "application/mathml+xml",
  96. mid: "audio/midi",
  97. midi: "audio/midi",
  98. mov: "video/quicktime",
  99. mp3: "audio/mpeg",
  100. mp4: "video/mp4",
  101. mp4v: "video/mp4",
  102. mpeg: "video/mpeg",
  103. mpg: "video/mpeg",
  104. odp: "application/vnd.oasis.opendocument.presentation",
  105. ods: "application/vnd.oasis.opendocument.spreadsheet",
  106. odt: "application/vnd.oasis.opendocument.text",
  107. ogg: "application/ogg",
  108. pdf: "application/pdf",
  109. png: "image/png",
  110. pps: "application/vnd.ms-powerpoint",
  111. ppt: "application/vnd.ms-powerpoint",
  112. ps: "application/postscript",
  113. qt: "video/quicktime",
  114. rdf: "application/rdf+xml",
  115. rss: "application/rss+xml",
  116. rtf: "application/rtf",
  117. svg: "image/svg+xml",
  118. swf: "application/x-shockwave-flash",
  119. text: "text/plain",
  120. tif: "image/tiff",
  121. tiff: "image/tiff",
  122. txt: "text/plain",
  123. wav: "audio/x-wav",
  124. wma: "audio/x-ms-wma",
  125. wmv: "video/x-ms-wmv",
  126. xml: "application/xml",
  127. zip: "application/zip",
  128. detectFromFilename: function (filename) {
  129. let ext = filename.toLowerCase();
  130. ext = getExtensionFromUri(ext);
  131. return MimeTypes[ext];
  132. },
  133. };
  134. let parser;
  135. if (typeof DOMParser !== "undefined") {
  136. parser = new DOMParser();
  137. }
  138. const autolinker = new Autolinker({
  139. stripPrefix: false,
  140. email: false,
  141. replaceFn: function (match) {
  142. //Prevent matching of non-explicit urls.
  143. //i.e. foo.id won't match but http://foo.id will
  144. return match.urlMatchType === "scheme" || match.urlMatchType === "www";
  145. },
  146. });
  147. const BILLBOARD_SIZE = 32;
  148. const BILLBOARD_NEAR_DISTANCE = 2414016;
  149. const BILLBOARD_NEAR_RATIO = 1.0;
  150. const BILLBOARD_FAR_DISTANCE = 1.6093e7;
  151. const BILLBOARD_FAR_RATIO = 0.1;
  152. const kmlNamespaces = [
  153. null,
  154. undefined,
  155. "http://www.opengis.net/kml/2.2",
  156. "http://earth.google.com/kml/2.2",
  157. "http://earth.google.com/kml/2.1",
  158. "http://earth.google.com/kml/2.0",
  159. ];
  160. const gxNamespaces = ["http://www.google.com/kml/ext/2.2"];
  161. const atomNamespaces = ["http://www.w3.org/2005/Atom"];
  162. const namespaces = {
  163. kml: kmlNamespaces,
  164. gx: gxNamespaces,
  165. atom: atomNamespaces,
  166. kmlgx: kmlNamespaces.concat(gxNamespaces),
  167. };
  168. // Ensure Specs/Data/KML/unsupported.kml is kept up to date with these supported types
  169. const featureTypes = {
  170. Document: processDocument,
  171. Folder: processFolder,
  172. Placemark: processPlacemark,
  173. NetworkLink: processNetworkLink,
  174. GroundOverlay: processGroundOverlay,
  175. PhotoOverlay: processUnsupportedFeature,
  176. ScreenOverlay: processScreenOverlay,
  177. Tour: processTour,
  178. };
  179. function DeferredLoading(dataSource) {
  180. this._dataSource = dataSource;
  181. this._deferred = defer();
  182. this._stack = [];
  183. this._promises = [];
  184. this._timeoutSet = false;
  185. this._used = false;
  186. this._started = 0;
  187. this._timeThreshold = 1000; // Initial load is 1 second
  188. }
  189. Object.defineProperties(DeferredLoading.prototype, {
  190. dataSource: {
  191. get: function () {
  192. return this._dataSource;
  193. },
  194. },
  195. });
  196. DeferredLoading.prototype.addNodes = function (nodes, processingData) {
  197. this._stack.push({
  198. nodes: nodes,
  199. index: 0,
  200. processingData: processingData,
  201. });
  202. this._used = true;
  203. };
  204. DeferredLoading.prototype.addPromise = function (promise) {
  205. this._promises.push(promise);
  206. };
  207. DeferredLoading.prototype.wait = function () {
  208. // Case where we had a non-document/folder as the root
  209. const deferred = this._deferred;
  210. if (!this._used) {
  211. deferred.resolve();
  212. }
  213. return Promise.all([deferred.promise, Promise.all(this._promises)]);
  214. };
  215. DeferredLoading.prototype.process = function () {
  216. const isFirstCall = this._stack.length === 1;
  217. if (isFirstCall) {
  218. this._started = KmlDataSource._getTimestamp();
  219. }
  220. return this._process(isFirstCall);
  221. };
  222. DeferredLoading.prototype._giveUpTime = function () {
  223. if (this._timeoutSet) {
  224. // Timeout was already set so just return
  225. return;
  226. }
  227. this._timeoutSet = true;
  228. this._timeThreshold = 50; // After the first load lower threshold to 0.5 seconds
  229. const that = this;
  230. setTimeout(function () {
  231. that._timeoutSet = false;
  232. that._started = KmlDataSource._getTimestamp();
  233. that._process(true);
  234. }, 0);
  235. };
  236. DeferredLoading.prototype._nextNode = function () {
  237. const stack = this._stack;
  238. const top = stack[stack.length - 1];
  239. const index = top.index;
  240. const nodes = top.nodes;
  241. if (index === nodes.length) {
  242. return;
  243. }
  244. ++top.index;
  245. return nodes[index];
  246. };
  247. DeferredLoading.prototype._pop = function () {
  248. const stack = this._stack;
  249. stack.pop();
  250. // Return false if we are done
  251. if (stack.length === 0) {
  252. this._deferred.resolve();
  253. return false;
  254. }
  255. return true;
  256. };
  257. DeferredLoading.prototype._process = function (isFirstCall) {
  258. const dataSource = this.dataSource;
  259. const processingData = this._stack[this._stack.length - 1].processingData;
  260. let child = this._nextNode();
  261. while (defined(child)) {
  262. const featureProcessor = featureTypes[child.localName];
  263. if (
  264. defined(featureProcessor) &&
  265. (namespaces.kml.indexOf(child.namespaceURI) !== -1 ||
  266. namespaces.gx.indexOf(child.namespaceURI) !== -1)
  267. ) {
  268. featureProcessor(dataSource, child, processingData, this);
  269. // Give up time and continue loading later
  270. if (
  271. this._timeoutSet ||
  272. KmlDataSource._getTimestamp() > this._started + this._timeThreshold
  273. ) {
  274. this._giveUpTime();
  275. return;
  276. }
  277. }
  278. child = this._nextNode();
  279. }
  280. // If we are a recursive call from a subfolder, just return so the parent folder can continue processing
  281. // If we aren't then make another call to processNodes because there is stuff still left in the queue
  282. if (this._pop() && isFirstCall) {
  283. this._process(true);
  284. }
  285. };
  286. function isZipFile(blob) {
  287. const magicBlob = blob.slice(0, Math.min(4, blob.size));
  288. const deferred = defer();
  289. const reader = new FileReader();
  290. reader.addEventListener("load", function () {
  291. deferred.resolve(
  292. new DataView(reader.result).getUint32(0, false) === 0x504b0304
  293. );
  294. });
  295. reader.addEventListener("error", function () {
  296. deferred.reject(reader.error);
  297. });
  298. reader.readAsArrayBuffer(magicBlob);
  299. return deferred.promise;
  300. }
  301. function readBlobAsText(blob) {
  302. const deferred = defer();
  303. const reader = new FileReader();
  304. reader.addEventListener("load", function () {
  305. deferred.resolve(reader.result);
  306. });
  307. reader.addEventListener("error", function () {
  308. deferred.reject(reader.error);
  309. });
  310. reader.readAsText(blob);
  311. return deferred.promise;
  312. }
  313. function insertNamespaces(text) {
  314. const namespaceMap = {
  315. xsi: "http://www.w3.org/2001/XMLSchema-instance",
  316. };
  317. let firstPart, lastPart, reg, declaration;
  318. for (const key in namespaceMap) {
  319. if (namespaceMap.hasOwnProperty(key)) {
  320. reg = RegExp(`[< ]${key}:`);
  321. declaration = `xmlns:${key}=`;
  322. if (reg.test(text) && text.indexOf(declaration) === -1) {
  323. if (!defined(firstPart)) {
  324. firstPart = text.substr(0, text.indexOf("<kml") + 4);
  325. lastPart = text.substr(firstPart.length);
  326. }
  327. firstPart += ` ${declaration}"${namespaceMap[key]}"`;
  328. }
  329. }
  330. }
  331. if (defined(firstPart)) {
  332. text = firstPart + lastPart;
  333. }
  334. return text;
  335. }
  336. function removeDuplicateNamespaces(text) {
  337. let index = text.indexOf("xmlns:");
  338. const endDeclaration = text.indexOf(">", index);
  339. let namespace, startIndex, endIndex;
  340. while (index !== -1 && index < endDeclaration) {
  341. namespace = text.slice(index, text.indexOf('"', index));
  342. startIndex = index;
  343. index = text.indexOf(namespace, index + 1);
  344. if (index !== -1) {
  345. endIndex = text.indexOf('"', text.indexOf('"', index) + 1);
  346. text = text.slice(0, index - 1) + text.slice(endIndex + 1, text.length);
  347. index = text.indexOf("xmlns:", startIndex - 1);
  348. } else {
  349. index = text.indexOf("xmlns:", startIndex + 1);
  350. }
  351. }
  352. return text;
  353. }
  354. function loadXmlFromZip(entry, uriResolver) {
  355. return Promise.resolve(entry.getData(new zip.TextWriter())).then(function (
  356. text
  357. ) {
  358. text = insertNamespaces(text);
  359. text = removeDuplicateNamespaces(text);
  360. uriResolver.kml = parser.parseFromString(text, "application/xml");
  361. });
  362. }
  363. function loadDataUriFromZip(entry, uriResolver) {
  364. const mimeType = defaultValue(
  365. MimeTypes.detectFromFilename(entry.filename),
  366. "application/octet-stream"
  367. );
  368. return Promise.resolve(entry.getData(new zip.Data64URIWriter(mimeType))).then(
  369. function (dataUri) {
  370. uriResolver[entry.filename] = dataUri;
  371. }
  372. );
  373. }
  374. function embedDataUris(div, elementType, attributeName, uriResolver) {
  375. const keys = uriResolver.keys;
  376. const baseUri = new Uri(".");
  377. const elements = div.querySelectorAll(elementType);
  378. for (let i = 0; i < elements.length; i++) {
  379. const element = elements[i];
  380. const value = element.getAttribute(attributeName);
  381. if (defined(value)) {
  382. const relativeUri = new Uri(value);
  383. const uri = relativeUri.absoluteTo(baseUri).toString();
  384. const index = keys.indexOf(uri);
  385. if (index !== -1) {
  386. const key = keys[index];
  387. element.setAttribute(attributeName, uriResolver[key]);
  388. if (elementType === "a" && element.getAttribute("download") === null) {
  389. element.setAttribute("download", key);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. function applyBasePath(div, elementType, attributeName, sourceResource) {
  396. const elements = div.querySelectorAll(elementType);
  397. for (let i = 0; i < elements.length; i++) {
  398. const element = elements[i];
  399. const value = element.getAttribute(attributeName);
  400. const resource = resolveHref(value, sourceResource);
  401. if (defined(resource)) {
  402. element.setAttribute(attributeName, resource.url);
  403. }
  404. }
  405. }
  406. // an optional context is passed to allow for some malformed kmls (those with multiple geometries with same ids) to still parse
  407. // correctly, as they do in Google Earth.
  408. function createEntity(node, entityCollection, context) {
  409. let id = queryStringAttribute(node, "id");
  410. id = defined(id) && id.length !== 0 ? id : createGuid();
  411. if (defined(context)) {
  412. id = context + id;
  413. }
  414. // If we have a duplicate ID just generate one.
  415. // This isn't valid KML but Google Earth handles this case.
  416. let entity = entityCollection.getById(id);
  417. if (defined(entity)) {
  418. id = createGuid();
  419. if (defined(context)) {
  420. id = context + id;
  421. }
  422. }
  423. entity = entityCollection.add(new Entity({ id: id }));
  424. if (!defined(entity.kml)) {
  425. entity.addProperty("kml");
  426. entity.kml = new KmlFeatureData();
  427. }
  428. return entity;
  429. }
  430. function isExtrudable(altitudeMode, gxAltitudeMode) {
  431. return (
  432. altitudeMode === "absolute" ||
  433. altitudeMode === "relativeToGround" ||
  434. gxAltitudeMode === "relativeToSeaFloor"
  435. );
  436. }
  437. function readCoordinate(value, ellipsoid) {
  438. //Google Earth treats empty or missing coordinates as 0.
  439. if (!defined(value)) {
  440. return Cartesian3.fromDegrees(0, 0, 0, ellipsoid);
  441. }
  442. const digits = value.match(/[^\s,\n]+/g);
  443. if (!defined(digits)) {
  444. return Cartesian3.fromDegrees(0, 0, 0, ellipsoid);
  445. }
  446. let longitude = parseFloat(digits[0]);
  447. let latitude = parseFloat(digits[1]);
  448. let height = parseFloat(digits[2]);
  449. longitude = isNaN(longitude) ? 0.0 : longitude;
  450. latitude = isNaN(latitude) ? 0.0 : latitude;
  451. height = isNaN(height) ? 0.0 : height;
  452. return Cartesian3.fromDegrees(longitude, latitude, height, ellipsoid);
  453. }
  454. function readCoordinates(element, ellipsoid) {
  455. if (!defined(element)) {
  456. return undefined;
  457. }
  458. const tuples = element.textContent.match(/[^\s\n]+/g);
  459. if (!defined(tuples)) {
  460. return undefined;
  461. }
  462. const length = tuples.length;
  463. const result = new Array(length);
  464. let resultIndex = 0;
  465. for (let i = 0; i < length; i++) {
  466. result[resultIndex++] = readCoordinate(tuples[i], ellipsoid);
  467. }
  468. return result;
  469. }
  470. function queryNumericAttribute(node, attributeName) {
  471. if (!defined(node)) {
  472. return undefined;
  473. }
  474. const value = node.getAttribute(attributeName);
  475. if (value !== null) {
  476. const result = parseFloat(value);
  477. return !isNaN(result) ? result : undefined;
  478. }
  479. return undefined;
  480. }
  481. function queryStringAttribute(node, attributeName) {
  482. if (!defined(node)) {
  483. return undefined;
  484. }
  485. const value = node.getAttribute(attributeName);
  486. return value !== null ? value : undefined;
  487. }
  488. function queryFirstNode(node, tagName, namespace) {
  489. if (!defined(node)) {
  490. return undefined;
  491. }
  492. const childNodes = node.childNodes;
  493. const length = childNodes.length;
  494. for (let q = 0; q < length; q++) {
  495. const child = childNodes[q];
  496. if (
  497. child.localName === tagName &&
  498. namespace.indexOf(child.namespaceURI) !== -1
  499. ) {
  500. return child;
  501. }
  502. }
  503. return undefined;
  504. }
  505. function queryNodes(node, tagName, namespace) {
  506. if (!defined(node)) {
  507. return undefined;
  508. }
  509. const result = [];
  510. const childNodes = node.getElementsByTagNameNS("*", tagName);
  511. const length = childNodes.length;
  512. for (let q = 0; q < length; q++) {
  513. const child = childNodes[q];
  514. if (
  515. child.localName === tagName &&
  516. namespace.indexOf(child.namespaceURI) !== -1
  517. ) {
  518. result.push(child);
  519. }
  520. }
  521. return result;
  522. }
  523. function queryChildNodes(node, tagName, namespace) {
  524. if (!defined(node)) {
  525. return [];
  526. }
  527. const result = [];
  528. const childNodes = node.childNodes;
  529. const length = childNodes.length;
  530. for (let q = 0; q < length; q++) {
  531. const child = childNodes[q];
  532. if (
  533. child.localName === tagName &&
  534. namespace.indexOf(child.namespaceURI) !== -1
  535. ) {
  536. result.push(child);
  537. }
  538. }
  539. return result;
  540. }
  541. function queryNumericValue(node, tagName, namespace) {
  542. const resultNode = queryFirstNode(node, tagName, namespace);
  543. if (defined(resultNode)) {
  544. const result = parseFloat(resultNode.textContent);
  545. return !isNaN(result) ? result : undefined;
  546. }
  547. return undefined;
  548. }
  549. function queryStringValue(node, tagName, namespace) {
  550. const result = queryFirstNode(node, tagName, namespace);
  551. if (defined(result)) {
  552. return result.textContent.trim();
  553. }
  554. return undefined;
  555. }
  556. function queryBooleanValue(node, tagName, namespace) {
  557. const result = queryFirstNode(node, tagName, namespace);
  558. if (defined(result)) {
  559. const value = result.textContent.trim();
  560. return value === "1" || /^true$/i.test(value);
  561. }
  562. return undefined;
  563. }
  564. function resolveHref(href, sourceResource, uriResolver) {
  565. if (!defined(href)) {
  566. return undefined;
  567. }
  568. let resource;
  569. if (defined(uriResolver)) {
  570. // To resolve issues with KML sources defined in Windows style paths.
  571. href = href.replace(/\\/g, "/");
  572. let blob = uriResolver[href];
  573. if (defined(blob)) {
  574. resource = new Resource({
  575. url: blob,
  576. });
  577. } else {
  578. // Needed for multiple levels of KML files in a KMZ
  579. const baseUri = new Uri(sourceResource.getUrlComponent());
  580. const uri = new Uri(href);
  581. blob = uriResolver[uri.absoluteTo(baseUri)];
  582. if (defined(blob)) {
  583. resource = new Resource({
  584. url: blob,
  585. });
  586. }
  587. }
  588. }
  589. if (!defined(resource)) {
  590. resource = sourceResource.getDerivedResource({
  591. url: href,
  592. });
  593. }
  594. return resource;
  595. }
  596. const colorOptions = {
  597. maximumRed: undefined,
  598. red: undefined,
  599. maximumGreen: undefined,
  600. green: undefined,
  601. maximumBlue: undefined,
  602. blue: undefined,
  603. };
  604. function parseColorString(value, isRandom) {
  605. if (!defined(value) || /^\s*$/gm.test(value)) {
  606. return undefined;
  607. }
  608. if (value[0] === "#") {
  609. value = value.substring(1);
  610. }
  611. const alpha = parseInt(value.substring(0, 2), 16) / 255.0;
  612. const blue = parseInt(value.substring(2, 4), 16) / 255.0;
  613. const green = parseInt(value.substring(4, 6), 16) / 255.0;
  614. const red = parseInt(value.substring(6, 8), 16) / 255.0;
  615. if (!isRandom) {
  616. return new Color(red, green, blue, alpha);
  617. }
  618. if (red > 0) {
  619. colorOptions.maximumRed = red;
  620. colorOptions.red = undefined;
  621. } else {
  622. colorOptions.maximumRed = undefined;
  623. colorOptions.red = 0;
  624. }
  625. if (green > 0) {
  626. colorOptions.maximumGreen = green;
  627. colorOptions.green = undefined;
  628. } else {
  629. colorOptions.maximumGreen = undefined;
  630. colorOptions.green = 0;
  631. }
  632. if (blue > 0) {
  633. colorOptions.maximumBlue = blue;
  634. colorOptions.blue = undefined;
  635. } else {
  636. colorOptions.maximumBlue = undefined;
  637. colorOptions.blue = 0;
  638. }
  639. colorOptions.alpha = alpha;
  640. return Color.fromRandom(colorOptions);
  641. }
  642. function queryColorValue(node, tagName, namespace) {
  643. const value = queryStringValue(node, tagName, namespace);
  644. if (!defined(value)) {
  645. return undefined;
  646. }
  647. return parseColorString(
  648. value,
  649. queryStringValue(node, "colorMode", namespace) === "random"
  650. );
  651. }
  652. function processTimeStamp(featureNode) {
  653. const node = queryFirstNode(featureNode, "TimeStamp", namespaces.kmlgx);
  654. const whenString = queryStringValue(node, "when", namespaces.kmlgx);
  655. if (!defined(node) || !defined(whenString) || whenString.length === 0) {
  656. return undefined;
  657. }
  658. //According to the KML spec, a TimeStamp represents a "single moment in time"
  659. //However, since Cesium animates much differently than Google Earth, that doesn't
  660. //Make much sense here. Instead, we use the TimeStamp as the moment the feature
  661. //comes into existence. This works much better and gives a similar feel to
  662. //GE's experience.
  663. const when = JulianDate.fromIso8601(whenString);
  664. const result = new TimeIntervalCollection();
  665. result.addInterval(
  666. new TimeInterval({
  667. start: when,
  668. stop: Iso8601.MAXIMUM_VALUE,
  669. })
  670. );
  671. return result;
  672. }
  673. function processTimeSpan(featureNode) {
  674. const node = queryFirstNode(featureNode, "TimeSpan", namespaces.kmlgx);
  675. if (!defined(node)) {
  676. return undefined;
  677. }
  678. let result;
  679. const beginNode = queryFirstNode(node, "begin", namespaces.kmlgx);
  680. let beginDate = defined(beginNode)
  681. ? JulianDate.fromIso8601(beginNode.textContent)
  682. : undefined;
  683. const endNode = queryFirstNode(node, "end", namespaces.kmlgx);
  684. let endDate = defined(endNode)
  685. ? JulianDate.fromIso8601(endNode.textContent)
  686. : undefined;
  687. if (defined(beginDate) && defined(endDate)) {
  688. if (JulianDate.lessThan(endDate, beginDate)) {
  689. const tmp = beginDate;
  690. beginDate = endDate;
  691. endDate = tmp;
  692. }
  693. result = new TimeIntervalCollection();
  694. result.addInterval(
  695. new TimeInterval({
  696. start: beginDate,
  697. stop: endDate,
  698. })
  699. );
  700. } else if (defined(beginDate)) {
  701. result = new TimeIntervalCollection();
  702. result.addInterval(
  703. new TimeInterval({
  704. start: beginDate,
  705. stop: Iso8601.MAXIMUM_VALUE,
  706. })
  707. );
  708. } else if (defined(endDate)) {
  709. result = new TimeIntervalCollection();
  710. result.addInterval(
  711. new TimeInterval({
  712. start: Iso8601.MINIMUM_VALUE,
  713. stop: endDate,
  714. })
  715. );
  716. }
  717. return result;
  718. }
  719. function createDefaultBillboard() {
  720. const billboard = new BillboardGraphics();
  721. billboard.width = BILLBOARD_SIZE;
  722. billboard.height = BILLBOARD_SIZE;
  723. billboard.scaleByDistance = new NearFarScalar(
  724. BILLBOARD_NEAR_DISTANCE,
  725. BILLBOARD_NEAR_RATIO,
  726. BILLBOARD_FAR_DISTANCE,
  727. BILLBOARD_FAR_RATIO
  728. );
  729. billboard.pixelOffsetScaleByDistance = new NearFarScalar(
  730. BILLBOARD_NEAR_DISTANCE,
  731. BILLBOARD_NEAR_RATIO,
  732. BILLBOARD_FAR_DISTANCE,
  733. BILLBOARD_FAR_RATIO
  734. );
  735. return billboard;
  736. }
  737. function createDefaultPolygon() {
  738. const polygon = new PolygonGraphics();
  739. polygon.outline = true;
  740. polygon.outlineColor = Color.WHITE;
  741. return polygon;
  742. }
  743. function createDefaultLabel() {
  744. const label = new LabelGraphics();
  745. label.translucencyByDistance = new NearFarScalar(3000000, 1.0, 5000000, 0.0);
  746. label.pixelOffset = new Cartesian2(17, 0);
  747. label.horizontalOrigin = HorizontalOrigin.LEFT;
  748. label.font = "16px sans-serif";
  749. label.style = LabelStyle.FILL_AND_OUTLINE;
  750. return label;
  751. }
  752. function getIconHref(
  753. iconNode,
  754. dataSource,
  755. sourceResource,
  756. uriResolver,
  757. canRefresh
  758. ) {
  759. let href = queryStringValue(iconNode, "href", namespaces.kml);
  760. if (!defined(href) || href.length === 0) {
  761. return undefined;
  762. }
  763. if (href.indexOf("root://icons/palette-") === 0) {
  764. const palette = href.charAt(21);
  765. // Get the icon number
  766. let x = defaultValue(queryNumericValue(iconNode, "x", namespaces.gx), 0);
  767. let y = defaultValue(queryNumericValue(iconNode, "y", namespaces.gx), 0);
  768. x = Math.min(x / 32, 7);
  769. y = 7 - Math.min(y / 32, 7);
  770. const iconNum = 8 * y + x;
  771. href = `https://maps.google.com/mapfiles/kml/pal${palette}/icon${iconNum}.png`;
  772. }
  773. const hrefResource = resolveHref(href, sourceResource, uriResolver);
  774. if (canRefresh) {
  775. const refreshMode = queryStringValue(
  776. iconNode,
  777. "refreshMode",
  778. namespaces.kml
  779. );
  780. const viewRefreshMode = queryStringValue(
  781. iconNode,
  782. "viewRefreshMode",
  783. namespaces.kml
  784. );
  785. if (refreshMode === "onInterval" || refreshMode === "onExpire") {
  786. oneTimeWarning(
  787. `kml-refreshMode-${refreshMode}`,
  788. `KML - Unsupported Icon refreshMode: ${refreshMode}`
  789. );
  790. } else if (viewRefreshMode === "onStop" || viewRefreshMode === "onRegion") {
  791. oneTimeWarning(
  792. `kml-refreshMode-${viewRefreshMode}`,
  793. `KML - Unsupported Icon viewRefreshMode: ${viewRefreshMode}`
  794. );
  795. }
  796. const viewBoundScale = defaultValue(
  797. queryStringValue(iconNode, "viewBoundScale", namespaces.kml),
  798. 1.0
  799. );
  800. const defaultViewFormat =
  801. viewRefreshMode === "onStop"
  802. ? "BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]"
  803. : "";
  804. const viewFormat = defaultValue(
  805. queryStringValue(iconNode, "viewFormat", namespaces.kml),
  806. defaultViewFormat
  807. );
  808. const httpQuery = queryStringValue(iconNode, "httpQuery", namespaces.kml);
  809. if (defined(viewFormat)) {
  810. hrefResource.setQueryParameters(queryToObject(cleanupString(viewFormat)));
  811. }
  812. if (defined(httpQuery)) {
  813. hrefResource.setQueryParameters(queryToObject(cleanupString(httpQuery)));
  814. }
  815. const ellipsoid = dataSource._ellipsoid;
  816. processNetworkLinkQueryString(
  817. hrefResource,
  818. dataSource.camera,
  819. dataSource.canvas,
  820. viewBoundScale,
  821. dataSource._lastCameraView.bbox,
  822. ellipsoid
  823. );
  824. return hrefResource;
  825. }
  826. return hrefResource;
  827. }
  828. function processBillboardIcon(
  829. dataSource,
  830. node,
  831. targetEntity,
  832. sourceResource,
  833. uriResolver
  834. ) {
  835. let scale = queryNumericValue(node, "scale", namespaces.kml);
  836. const heading = queryNumericValue(node, "heading", namespaces.kml);
  837. const color = queryColorValue(node, "color", namespaces.kml);
  838. const iconNode = queryFirstNode(node, "Icon", namespaces.kml);
  839. let icon = getIconHref(
  840. iconNode,
  841. dataSource,
  842. sourceResource,
  843. uriResolver,
  844. false
  845. );
  846. // If icon tags are present but blank, we do not want to show an icon
  847. if (defined(iconNode) && !defined(icon)) {
  848. icon = false;
  849. }
  850. const x = queryNumericValue(iconNode, "x", namespaces.gx);
  851. const y = queryNumericValue(iconNode, "y", namespaces.gx);
  852. const w = queryNumericValue(iconNode, "w", namespaces.gx);
  853. const h = queryNumericValue(iconNode, "h", namespaces.gx);
  854. const hotSpotNode = queryFirstNode(node, "hotSpot", namespaces.kml);
  855. const hotSpotX = queryNumericAttribute(hotSpotNode, "x");
  856. const hotSpotY = queryNumericAttribute(hotSpotNode, "y");
  857. const hotSpotXUnit = queryStringAttribute(hotSpotNode, "xunits");
  858. const hotSpotYUnit = queryStringAttribute(hotSpotNode, "yunits");
  859. let billboard = targetEntity.billboard;
  860. if (!defined(billboard)) {
  861. billboard = createDefaultBillboard();
  862. targetEntity.billboard = billboard;
  863. }
  864. billboard.image = icon;
  865. billboard.scale = scale;
  866. billboard.color = color;
  867. if (defined(x) || defined(y) || defined(w) || defined(h)) {
  868. billboard.imageSubRegion = new BoundingRectangle(x, y, w, h);
  869. }
  870. //GE treats a heading of zero as no heading
  871. //You can still point north using a 360 degree angle (or any multiple of 360)
  872. if (defined(heading) && heading !== 0) {
  873. billboard.rotation = CesiumMath.toRadians(-heading);
  874. billboard.alignedAxis = Cartesian3.UNIT_Z;
  875. }
  876. //Hotpot is the KML equivalent of pixel offset
  877. //The hotspot origin is the lower left, but we leave
  878. //our billboard origin at the center and simply
  879. //modify the pixel offset to take this into account
  880. scale = defaultValue(scale, 1.0);
  881. let xOffset;
  882. let yOffset;
  883. if (defined(hotSpotX)) {
  884. if (hotSpotXUnit === "pixels") {
  885. xOffset = -hotSpotX * scale;
  886. } else if (hotSpotXUnit === "insetPixels") {
  887. xOffset = (hotSpotX - BILLBOARD_SIZE) * scale;
  888. } else if (hotSpotXUnit === "fraction") {
  889. xOffset = -hotSpotX * BILLBOARD_SIZE * scale;
  890. }
  891. xOffset += BILLBOARD_SIZE * 0.5 * scale;
  892. }
  893. if (defined(hotSpotY)) {
  894. if (hotSpotYUnit === "pixels") {
  895. yOffset = hotSpotY * scale;
  896. } else if (hotSpotYUnit === "insetPixels") {
  897. yOffset = (-hotSpotY + BILLBOARD_SIZE) * scale;
  898. } else if (hotSpotYUnit === "fraction") {
  899. yOffset = hotSpotY * BILLBOARD_SIZE * scale;
  900. }
  901. yOffset -= BILLBOARD_SIZE * 0.5 * scale;
  902. }
  903. if (defined(xOffset) || defined(yOffset)) {
  904. billboard.pixelOffset = new Cartesian2(xOffset, yOffset);
  905. }
  906. }
  907. function applyStyle(
  908. dataSource,
  909. styleNode,
  910. targetEntity,
  911. sourceResource,
  912. uriResolver
  913. ) {
  914. for (let i = 0, len = styleNode.childNodes.length; i < len; i++) {
  915. const node = styleNode.childNodes.item(i);
  916. if (node.localName === "IconStyle") {
  917. processBillboardIcon(
  918. dataSource,
  919. node,
  920. targetEntity,
  921. sourceResource,
  922. uriResolver
  923. );
  924. } else if (node.localName === "LabelStyle") {
  925. let label = targetEntity.label;
  926. if (!defined(label)) {
  927. label = createDefaultLabel();
  928. targetEntity.label = label;
  929. }
  930. label.scale = defaultValue(
  931. queryNumericValue(node, "scale", namespaces.kml),
  932. label.scale
  933. );
  934. label.fillColor = defaultValue(
  935. queryColorValue(node, "color", namespaces.kml),
  936. label.fillColor
  937. );
  938. label.text = targetEntity.name;
  939. } else if (node.localName === "LineStyle") {
  940. let polyline = targetEntity.polyline;
  941. if (!defined(polyline)) {
  942. polyline = new PolylineGraphics();
  943. targetEntity.polyline = polyline;
  944. }
  945. polyline.width = queryNumericValue(node, "width", namespaces.kml);
  946. polyline.material = queryColorValue(node, "color", namespaces.kml);
  947. if (defined(queryColorValue(node, "outerColor", namespaces.gx))) {
  948. oneTimeWarning(
  949. "kml-gx:outerColor",
  950. "KML - gx:outerColor is not supported in a LineStyle"
  951. );
  952. }
  953. if (defined(queryNumericValue(node, "outerWidth", namespaces.gx))) {
  954. oneTimeWarning(
  955. "kml-gx:outerWidth",
  956. "KML - gx:outerWidth is not supported in a LineStyle"
  957. );
  958. }
  959. if (defined(queryNumericValue(node, "physicalWidth", namespaces.gx))) {
  960. oneTimeWarning(
  961. "kml-gx:physicalWidth",
  962. "KML - gx:physicalWidth is not supported in a LineStyle"
  963. );
  964. }
  965. if (defined(queryBooleanValue(node, "labelVisibility", namespaces.gx))) {
  966. oneTimeWarning(
  967. "kml-gx:labelVisibility",
  968. "KML - gx:labelVisibility is not supported in a LineStyle"
  969. );
  970. }
  971. } else if (node.localName === "PolyStyle") {
  972. let polygon = targetEntity.polygon;
  973. if (!defined(polygon)) {
  974. polygon = createDefaultPolygon();
  975. targetEntity.polygon = polygon;
  976. }
  977. polygon.material = defaultValue(
  978. queryColorValue(node, "color", namespaces.kml),
  979. polygon.material
  980. );
  981. polygon.fill = defaultValue(
  982. queryBooleanValue(node, "fill", namespaces.kml),
  983. polygon.fill
  984. );
  985. polygon.outline = defaultValue(
  986. queryBooleanValue(node, "outline", namespaces.kml),
  987. polygon.outline
  988. );
  989. } else if (node.localName === "BalloonStyle") {
  990. const bgColor = defaultValue(
  991. parseColorString(queryStringValue(node, "bgColor", namespaces.kml)),
  992. Color.WHITE
  993. );
  994. const textColor = defaultValue(
  995. parseColorString(queryStringValue(node, "textColor", namespaces.kml)),
  996. Color.BLACK
  997. );
  998. const text = queryStringValue(node, "text", namespaces.kml);
  999. //This is purely an internal property used in style processing,
  1000. //it never ends up on the final entity.
  1001. targetEntity.addProperty("balloonStyle");
  1002. targetEntity.balloonStyle = {
  1003. bgColor: bgColor,
  1004. textColor: textColor,
  1005. text: text,
  1006. };
  1007. } else if (node.localName === "ListStyle") {
  1008. const listItemType = queryStringValue(
  1009. node,
  1010. "listItemType",
  1011. namespaces.kml
  1012. );
  1013. if (listItemType === "radioFolder" || listItemType === "checkOffOnly") {
  1014. oneTimeWarning(
  1015. `kml-listStyle-${listItemType}`,
  1016. `KML - Unsupported ListStyle with listItemType: ${listItemType}`
  1017. );
  1018. }
  1019. }
  1020. }
  1021. }
  1022. //Processes and merges any inline styles for the provided node into the provided entity.
  1023. function computeFinalStyle(
  1024. dataSource,
  1025. placeMark,
  1026. styleCollection,
  1027. sourceResource,
  1028. uriResolver
  1029. ) {
  1030. const result = new Entity();
  1031. let styleEntity;
  1032. //Google earth seems to always use the last inline Style/StyleMap only
  1033. let styleIndex = -1;
  1034. const childNodes = placeMark.childNodes;
  1035. const length = childNodes.length;
  1036. for (let q = 0; q < length; q++) {
  1037. const child = childNodes[q];
  1038. if (child.localName === "Style" || child.localName === "StyleMap") {
  1039. styleIndex = q;
  1040. }
  1041. }
  1042. if (styleIndex !== -1) {
  1043. const inlineStyleNode = childNodes[styleIndex];
  1044. if (inlineStyleNode.localName === "Style") {
  1045. applyStyle(
  1046. dataSource,
  1047. inlineStyleNode,
  1048. result,
  1049. sourceResource,
  1050. uriResolver
  1051. );
  1052. } else {
  1053. // StyleMap
  1054. const pairs = queryChildNodes(inlineStyleNode, "Pair", namespaces.kml);
  1055. for (let p = 0; p < pairs.length; p++) {
  1056. const pair = pairs[p];
  1057. const key = queryStringValue(pair, "key", namespaces.kml);
  1058. if (key === "normal") {
  1059. const styleUrl = queryStringValue(pair, "styleUrl", namespaces.kml);
  1060. if (defined(styleUrl)) {
  1061. styleEntity = styleCollection.getById(styleUrl);
  1062. if (!defined(styleEntity)) {
  1063. styleEntity = styleCollection.getById(`#${styleUrl}`);
  1064. }
  1065. if (defined(styleEntity)) {
  1066. result.merge(styleEntity);
  1067. }
  1068. } else {
  1069. const node = queryFirstNode(pair, "Style", namespaces.kml);
  1070. applyStyle(dataSource, node, result, sourceResource, uriResolver);
  1071. }
  1072. } else {
  1073. oneTimeWarning(
  1074. `kml-styleMap-${key}`,
  1075. `KML - Unsupported StyleMap key: ${key}`
  1076. );
  1077. }
  1078. }
  1079. }
  1080. }
  1081. //Google earth seems to always use the first external style only.
  1082. const externalStyle = queryStringValue(placeMark, "styleUrl", namespaces.kml);
  1083. if (defined(externalStyle)) {
  1084. let id = externalStyle;
  1085. if (externalStyle[0] !== "#" && externalStyle.indexOf("#") !== -1) {
  1086. const tokens = externalStyle.split("#");
  1087. const uri = tokens[0];
  1088. const resource = sourceResource.getDerivedResource({
  1089. url: uri,
  1090. });
  1091. id = `${resource.getUrlComponent()}#${tokens[1]}`;
  1092. }
  1093. styleEntity = styleCollection.getById(id);
  1094. if (!defined(styleEntity)) {
  1095. styleEntity = styleCollection.getById(`#${id}`);
  1096. }
  1097. if (defined(styleEntity)) {
  1098. result.merge(styleEntity);
  1099. }
  1100. }
  1101. return result;
  1102. }
  1103. //Asynchronously processes an external style file.
  1104. function processExternalStyles(dataSource, resource, styleCollection) {
  1105. return resource.fetchXML().then(function (styleKml) {
  1106. return processStyles(dataSource, styleKml, styleCollection, resource, true);
  1107. });
  1108. }
  1109. //Processes all shared and external styles and stores
  1110. //their id into the provided styleCollection.
  1111. //Returns an array of promises that will resolve when
  1112. //each style is loaded.
  1113. function processStyles(
  1114. dataSource,
  1115. kml,
  1116. styleCollection,
  1117. sourceResource,
  1118. isExternal,
  1119. uriResolver
  1120. ) {
  1121. let i;
  1122. let id;
  1123. let styleEntity;
  1124. let node;
  1125. const styleNodes = queryNodes(kml, "Style", namespaces.kml);
  1126. if (defined(styleNodes)) {
  1127. const styleNodesLength = styleNodes.length;
  1128. for (i = 0; i < styleNodesLength; i++) {
  1129. node = styleNodes[i];
  1130. id = queryStringAttribute(node, "id");
  1131. if (defined(id)) {
  1132. id = `#${id}`;
  1133. if (isExternal && defined(sourceResource)) {
  1134. id = sourceResource.getUrlComponent() + id;
  1135. }
  1136. if (!defined(styleCollection.getById(id))) {
  1137. styleEntity = new Entity({
  1138. id: id,
  1139. });
  1140. styleCollection.add(styleEntity);
  1141. applyStyle(
  1142. dataSource,
  1143. node,
  1144. styleEntity,
  1145. sourceResource,
  1146. uriResolver
  1147. );
  1148. }
  1149. }
  1150. }
  1151. }
  1152. const styleMaps = queryNodes(kml, "StyleMap", namespaces.kml);
  1153. if (defined(styleMaps)) {
  1154. const styleMapsLength = styleMaps.length;
  1155. for (i = 0; i < styleMapsLength; i++) {
  1156. const styleMap = styleMaps[i];
  1157. id = queryStringAttribute(styleMap, "id");
  1158. if (defined(id)) {
  1159. const pairs = queryChildNodes(styleMap, "Pair", namespaces.kml);
  1160. for (let p = 0; p < pairs.length; p++) {
  1161. const pair = pairs[p];
  1162. const key = queryStringValue(pair, "key", namespaces.kml);
  1163. if (key === "normal") {
  1164. id = `#${id}`;
  1165. if (isExternal && defined(sourceResource)) {
  1166. id = sourceResource.getUrlComponent() + id;
  1167. }
  1168. if (!defined(styleCollection.getById(id))) {
  1169. styleEntity = styleCollection.getOrCreateEntity(id);
  1170. let styleUrl = queryStringValue(pair, "styleUrl", namespaces.kml);
  1171. if (defined(styleUrl)) {
  1172. if (styleUrl[0] !== "#") {
  1173. styleUrl = `#${styleUrl}`;
  1174. }
  1175. if (isExternal && defined(sourceResource)) {
  1176. styleUrl = sourceResource.getUrlComponent() + styleUrl;
  1177. }
  1178. const base = styleCollection.getById(styleUrl);
  1179. if (defined(base)) {
  1180. styleEntity.merge(base);
  1181. }
  1182. } else {
  1183. node = queryFirstNode(pair, "Style", namespaces.kml);
  1184. applyStyle(
  1185. dataSource,
  1186. node,
  1187. styleEntity,
  1188. sourceResource,
  1189. uriResolver
  1190. );
  1191. }
  1192. }
  1193. } else {
  1194. oneTimeWarning(
  1195. `kml-styleMap-${key}`,
  1196. `KML - Unsupported StyleMap key: ${key}`
  1197. );
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. const promises = [];
  1204. const styleUrlNodes = kml.getElementsByTagName("styleUrl");
  1205. const styleUrlNodesLength = styleUrlNodes.length;
  1206. for (i = 0; i < styleUrlNodesLength; i++) {
  1207. const styleReference = styleUrlNodes[i].textContent;
  1208. if (styleReference[0] !== "#") {
  1209. //According to the spec, all local styles should start with a #
  1210. //and everything else is an external style that has a # seperating
  1211. //the URL of the document and the style. However, Google Earth
  1212. //also accepts styleUrls without a # as meaning a local style.
  1213. const tokens = styleReference.split("#");
  1214. if (tokens.length === 2) {
  1215. const uri = tokens[0];
  1216. const resource = sourceResource.getDerivedResource({
  1217. url: uri,
  1218. });
  1219. promises.push(
  1220. processExternalStyles(dataSource, resource, styleCollection)
  1221. );
  1222. }
  1223. }
  1224. }
  1225. return promises;
  1226. }
  1227. function createDropLine(entityCollection, entity, styleEntity) {
  1228. const entityPosition = new ReferenceProperty(entityCollection, entity.id, [
  1229. "position",
  1230. ]);
  1231. const surfacePosition = new ScaledPositionProperty(entity.position);
  1232. entity.polyline = defined(styleEntity.polyline)
  1233. ? styleEntity.polyline.clone()
  1234. : new PolylineGraphics();
  1235. entity.polyline.positions = new PositionPropertyArray([
  1236. entityPosition,
  1237. surfacePosition,
  1238. ]);
  1239. }
  1240. function heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode) {
  1241. if (
  1242. (!defined(altitudeMode) && !defined(gxAltitudeMode)) ||
  1243. altitudeMode === "clampToGround"
  1244. ) {
  1245. return HeightReference.CLAMP_TO_GROUND;
  1246. }
  1247. if (altitudeMode === "relativeToGround") {
  1248. return HeightReference.RELATIVE_TO_GROUND;
  1249. }
  1250. if (altitudeMode === "absolute") {
  1251. return HeightReference.NONE;
  1252. }
  1253. if (gxAltitudeMode === "clampToSeaFloor") {
  1254. oneTimeWarning(
  1255. "kml-gx:altitudeMode-clampToSeaFloor",
  1256. "KML - <gx:altitudeMode>:clampToSeaFloor is currently not supported, using <kml:altitudeMode>:clampToGround."
  1257. );
  1258. return HeightReference.CLAMP_TO_GROUND;
  1259. }
  1260. if (gxAltitudeMode === "relativeToSeaFloor") {
  1261. oneTimeWarning(
  1262. "kml-gx:altitudeMode-relativeToSeaFloor",
  1263. "KML - <gx:altitudeMode>:relativeToSeaFloor is currently not supported, using <kml:altitudeMode>:relativeToGround."
  1264. );
  1265. return HeightReference.RELATIVE_TO_GROUND;
  1266. }
  1267. if (defined(altitudeMode)) {
  1268. oneTimeWarning(
  1269. "kml-altitudeMode-unknown",
  1270. `KML - Unknown <kml:altitudeMode>:${altitudeMode}, using <kml:altitudeMode>:CLAMP_TO_GROUND.`
  1271. );
  1272. } else {
  1273. oneTimeWarning(
  1274. "kml-gx:altitudeMode-unknown",
  1275. `KML - Unknown <gx:altitudeMode>:${gxAltitudeMode}, using <kml:altitudeMode>:CLAMP_TO_GROUND.`
  1276. );
  1277. }
  1278. // Clamp to ground is the default
  1279. return HeightReference.CLAMP_TO_GROUND;
  1280. }
  1281. function createPositionPropertyFromAltitudeMode(
  1282. property,
  1283. altitudeMode,
  1284. gxAltitudeMode
  1285. ) {
  1286. if (
  1287. gxAltitudeMode === "relativeToSeaFloor" ||
  1288. altitudeMode === "absolute" ||
  1289. altitudeMode === "relativeToGround"
  1290. ) {
  1291. //Just return the ellipsoid referenced property until we support MSL
  1292. return property;
  1293. }
  1294. if (
  1295. (defined(altitudeMode) && altitudeMode !== "clampToGround") || //
  1296. (defined(gxAltitudeMode) && gxAltitudeMode !== "clampToSeaFloor")
  1297. ) {
  1298. oneTimeWarning(
  1299. "kml-altitudeMode-unknown",
  1300. `KML - Unknown altitudeMode: ${defaultValue(
  1301. altitudeMode,
  1302. gxAltitudeMode
  1303. )}`
  1304. );
  1305. }
  1306. // Clamp to ground is the default
  1307. return new ScaledPositionProperty(property);
  1308. }
  1309. function createPositionPropertyArrayFromAltitudeMode(
  1310. properties,
  1311. altitudeMode,
  1312. gxAltitudeMode,
  1313. ellipsoid
  1314. ) {
  1315. if (!defined(properties)) {
  1316. return undefined;
  1317. }
  1318. if (
  1319. gxAltitudeMode === "relativeToSeaFloor" ||
  1320. altitudeMode === "absolute" ||
  1321. altitudeMode === "relativeToGround"
  1322. ) {
  1323. //Just return the ellipsoid referenced property until we support MSL
  1324. return properties;
  1325. }
  1326. if (
  1327. (defined(altitudeMode) && altitudeMode !== "clampToGround") || //
  1328. (defined(gxAltitudeMode) && gxAltitudeMode !== "clampToSeaFloor")
  1329. ) {
  1330. oneTimeWarning(
  1331. "kml-altitudeMode-unknown",
  1332. `KML - Unknown altitudeMode: ${defaultValue(
  1333. altitudeMode,
  1334. gxAltitudeMode
  1335. )}`
  1336. );
  1337. }
  1338. // Clamp to ground is the default
  1339. const propertiesLength = properties.length;
  1340. for (let i = 0; i < propertiesLength; i++) {
  1341. const property = properties[i];
  1342. ellipsoid.scaleToGeodeticSurface(property, property);
  1343. }
  1344. return properties;
  1345. }
  1346. function processPositionGraphics(
  1347. dataSource,
  1348. entity,
  1349. styleEntity,
  1350. heightReference
  1351. ) {
  1352. let label = entity.label;
  1353. if (!defined(label)) {
  1354. label = defined(styleEntity.label)
  1355. ? styleEntity.label.clone()
  1356. : createDefaultLabel();
  1357. entity.label = label;
  1358. }
  1359. label.text = entity.name;
  1360. let billboard = entity.billboard;
  1361. if (!defined(billboard)) {
  1362. billboard = defined(styleEntity.billboard)
  1363. ? styleEntity.billboard.clone()
  1364. : createDefaultBillboard();
  1365. entity.billboard = billboard;
  1366. }
  1367. if (!defined(billboard.image)) {
  1368. billboard.image = dataSource._pinBuilder.fromColor(Color.YELLOW, 64);
  1369. // If there were empty <Icon> tags in the KML, then billboard.image was set to false above
  1370. // However, in this case, the false value would have been converted to a property afterwards
  1371. // Thus, we check if billboard.image is defined with value of false
  1372. } else if (!billboard.image.getValue()) {
  1373. billboard.image = undefined;
  1374. }
  1375. let scale = 1.0;
  1376. if (defined(billboard.scale)) {
  1377. scale = billboard.scale.getValue();
  1378. if (scale !== 0) {
  1379. label.pixelOffset = new Cartesian2(scale * 16 + 1, 0);
  1380. } else {
  1381. //Minor tweaks to better match Google Earth.
  1382. label.pixelOffset = undefined;
  1383. label.horizontalOrigin = undefined;
  1384. }
  1385. }
  1386. if (defined(heightReference) && dataSource._clampToGround) {
  1387. billboard.heightReference = heightReference;
  1388. label.heightReference = heightReference;
  1389. }
  1390. }
  1391. function processPathGraphics(entity, styleEntity) {
  1392. let path = entity.path;
  1393. if (!defined(path)) {
  1394. path = new PathGraphics();
  1395. path.leadTime = 0;
  1396. entity.path = path;
  1397. }
  1398. const polyline = styleEntity.polyline;
  1399. if (defined(polyline)) {
  1400. path.material = polyline.material;
  1401. path.width = polyline.width;
  1402. }
  1403. }
  1404. function processPoint(
  1405. dataSource,
  1406. entityCollection,
  1407. geometryNode,
  1408. entity,
  1409. styleEntity
  1410. ) {
  1411. const coordinatesString = queryStringValue(
  1412. geometryNode,
  1413. "coordinates",
  1414. namespaces.kml
  1415. );
  1416. const altitudeMode = queryStringValue(
  1417. geometryNode,
  1418. "altitudeMode",
  1419. namespaces.kml
  1420. );
  1421. const gxAltitudeMode = queryStringValue(
  1422. geometryNode,
  1423. "altitudeMode",
  1424. namespaces.gx
  1425. );
  1426. const extrude = queryBooleanValue(geometryNode, "extrude", namespaces.kml);
  1427. const ellipsoid = dataSource._ellipsoid;
  1428. const position = readCoordinate(coordinatesString, ellipsoid);
  1429. entity.position = position;
  1430. processPositionGraphics(
  1431. dataSource,
  1432. entity,
  1433. styleEntity,
  1434. heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode)
  1435. );
  1436. if (extrude && isExtrudable(altitudeMode, gxAltitudeMode)) {
  1437. createDropLine(entityCollection, entity, styleEntity);
  1438. }
  1439. return true;
  1440. }
  1441. function processLineStringOrLinearRing(
  1442. dataSource,
  1443. entityCollection,
  1444. geometryNode,
  1445. entity,
  1446. styleEntity
  1447. ) {
  1448. const coordinatesNode = queryFirstNode(
  1449. geometryNode,
  1450. "coordinates",
  1451. namespaces.kml
  1452. );
  1453. const altitudeMode = queryStringValue(
  1454. geometryNode,
  1455. "altitudeMode",
  1456. namespaces.kml
  1457. );
  1458. const gxAltitudeMode = queryStringValue(
  1459. geometryNode,
  1460. "altitudeMode",
  1461. namespaces.gx
  1462. );
  1463. const extrude = queryBooleanValue(geometryNode, "extrude", namespaces.kml);
  1464. const tessellate = queryBooleanValue(
  1465. geometryNode,
  1466. "tessellate",
  1467. namespaces.kml
  1468. );
  1469. const canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1470. const zIndex = queryNumericValue(geometryNode, "drawOrder", namespaces.gx);
  1471. const ellipsoid = dataSource._ellipsoid;
  1472. const coordinates = readCoordinates(coordinatesNode, ellipsoid);
  1473. let polyline = styleEntity.polyline;
  1474. if (canExtrude && extrude) {
  1475. const wall = new WallGraphics();
  1476. entity.wall = wall;
  1477. wall.positions = coordinates;
  1478. const polygon = styleEntity.polygon;
  1479. if (defined(polygon)) {
  1480. wall.fill = polygon.fill;
  1481. wall.material = polygon.material;
  1482. }
  1483. //Always outline walls so they show up in 2D.
  1484. wall.outline = true;
  1485. if (defined(polyline)) {
  1486. wall.outlineColor = defined(polyline.material)
  1487. ? polyline.material.color
  1488. : Color.WHITE;
  1489. wall.outlineWidth = polyline.width;
  1490. } else if (defined(polygon)) {
  1491. wall.outlineColor = defined(polygon.material)
  1492. ? polygon.material.color
  1493. : Color.WHITE;
  1494. }
  1495. } else if (dataSource._clampToGround && !canExtrude && tessellate) {
  1496. const polylineGraphics = new PolylineGraphics();
  1497. polylineGraphics.clampToGround = true;
  1498. entity.polyline = polylineGraphics;
  1499. polylineGraphics.positions = coordinates;
  1500. if (defined(polyline)) {
  1501. polylineGraphics.material = defined(polyline.material)
  1502. ? polyline.material.color.getValue(Iso8601.MINIMUM_VALUE)
  1503. : Color.WHITE;
  1504. polylineGraphics.width = defaultValue(polyline.width, 1.0);
  1505. } else {
  1506. polylineGraphics.material = Color.WHITE;
  1507. polylineGraphics.width = 1.0;
  1508. }
  1509. polylineGraphics.zIndex = zIndex;
  1510. } else {
  1511. if (defined(zIndex)) {
  1512. oneTimeWarning(
  1513. "kml-gx:drawOrder",
  1514. "KML - gx:drawOrder is not supported in LineStrings when clampToGround is false"
  1515. );
  1516. }
  1517. if (dataSource._clampToGround && !tessellate) {
  1518. oneTimeWarning(
  1519. "kml-line-tesselate",
  1520. "Ignoring clampToGround for KML lines without the tessellate flag."
  1521. );
  1522. }
  1523. polyline = defined(polyline) ? polyline.clone() : new PolylineGraphics();
  1524. entity.polyline = polyline;
  1525. polyline.positions = createPositionPropertyArrayFromAltitudeMode(
  1526. coordinates,
  1527. altitudeMode,
  1528. gxAltitudeMode,
  1529. ellipsoid
  1530. );
  1531. if (!tessellate || canExtrude) {
  1532. polyline.arcType = ArcType.NONE;
  1533. }
  1534. }
  1535. return true;
  1536. }
  1537. function processPolygon(
  1538. dataSource,
  1539. entityCollection,
  1540. geometryNode,
  1541. entity,
  1542. styleEntity
  1543. ) {
  1544. const outerBoundaryIsNode = queryFirstNode(
  1545. geometryNode,
  1546. "outerBoundaryIs",
  1547. namespaces.kml
  1548. );
  1549. let linearRingNode = queryFirstNode(
  1550. outerBoundaryIsNode,
  1551. "LinearRing",
  1552. namespaces.kml
  1553. );
  1554. let coordinatesNode = queryFirstNode(
  1555. linearRingNode,
  1556. "coordinates",
  1557. namespaces.kml
  1558. );
  1559. const ellipsoid = dataSource._ellipsoid;
  1560. let coordinates = readCoordinates(coordinatesNode, ellipsoid);
  1561. const extrude = queryBooleanValue(geometryNode, "extrude", namespaces.kml);
  1562. const altitudeMode = queryStringValue(
  1563. geometryNode,
  1564. "altitudeMode",
  1565. namespaces.kml
  1566. );
  1567. const gxAltitudeMode = queryStringValue(
  1568. geometryNode,
  1569. "altitudeMode",
  1570. namespaces.gx
  1571. );
  1572. const canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1573. const polygon = defined(styleEntity.polygon)
  1574. ? styleEntity.polygon.clone()
  1575. : createDefaultPolygon();
  1576. const polyline = styleEntity.polyline;
  1577. if (defined(polyline)) {
  1578. polygon.outlineColor = defined(polyline.material)
  1579. ? polyline.material.color
  1580. : Color.WHITE;
  1581. polygon.outlineWidth = polyline.width;
  1582. }
  1583. entity.polygon = polygon;
  1584. if (canExtrude) {
  1585. polygon.perPositionHeight = true;
  1586. polygon.extrudedHeight = extrude ? 0 : undefined;
  1587. } else if (!dataSource._clampToGround) {
  1588. polygon.height = 0;
  1589. }
  1590. if (defined(coordinates)) {
  1591. const hierarchy = new PolygonHierarchy(coordinates);
  1592. const innerBoundaryIsNodes = queryChildNodes(
  1593. geometryNode,
  1594. "innerBoundaryIs",
  1595. namespaces.kml
  1596. );
  1597. for (let j = 0; j < innerBoundaryIsNodes.length; j++) {
  1598. linearRingNode = queryChildNodes(
  1599. innerBoundaryIsNodes[j],
  1600. "LinearRing",
  1601. namespaces.kml
  1602. );
  1603. for (let k = 0; k < linearRingNode.length; k++) {
  1604. coordinatesNode = queryFirstNode(
  1605. linearRingNode[k],
  1606. "coordinates",
  1607. namespaces.kml
  1608. );
  1609. coordinates = readCoordinates(coordinatesNode, ellipsoid);
  1610. if (defined(coordinates)) {
  1611. hierarchy.holes.push(new PolygonHierarchy(coordinates));
  1612. }
  1613. }
  1614. }
  1615. polygon.hierarchy = hierarchy;
  1616. }
  1617. return true;
  1618. }
  1619. function processTrack(
  1620. dataSource,
  1621. entityCollection,
  1622. geometryNode,
  1623. entity,
  1624. styleEntity
  1625. ) {
  1626. const altitudeMode = queryStringValue(
  1627. geometryNode,
  1628. "altitudeMode",
  1629. namespaces.kml
  1630. );
  1631. const gxAltitudeMode = queryStringValue(
  1632. geometryNode,
  1633. "altitudeMode",
  1634. namespaces.gx
  1635. );
  1636. const coordNodes = queryChildNodes(geometryNode, "coord", namespaces.gx);
  1637. const angleNodes = queryChildNodes(geometryNode, "angles", namespaces.gx);
  1638. const timeNodes = queryChildNodes(geometryNode, "when", namespaces.kml);
  1639. const extrude = queryBooleanValue(geometryNode, "extrude", namespaces.kml);
  1640. const canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1641. const ellipsoid = dataSource._ellipsoid;
  1642. if (angleNodes.length > 0) {
  1643. oneTimeWarning(
  1644. "kml-gx:angles",
  1645. "KML - gx:angles are not supported in gx:Tracks"
  1646. );
  1647. }
  1648. const length = Math.min(coordNodes.length, timeNodes.length);
  1649. const coordinates = [];
  1650. const times = [];
  1651. for (let i = 0; i < length; i++) {
  1652. const position = readCoordinate(coordNodes[i].textContent, ellipsoid);
  1653. coordinates.push(position);
  1654. times.push(JulianDate.fromIso8601(timeNodes[i].textContent));
  1655. }
  1656. const property = new SampledPositionProperty();
  1657. property.addSamples(times, coordinates);
  1658. entity.position = property;
  1659. processPositionGraphics(
  1660. dataSource,
  1661. entity,
  1662. styleEntity,
  1663. heightReferenceFromAltitudeMode(altitudeMode, gxAltitudeMode)
  1664. );
  1665. processPathGraphics(entity, styleEntity);
  1666. entity.availability = new TimeIntervalCollection();
  1667. if (timeNodes.length > 0) {
  1668. entity.availability.addInterval(
  1669. new TimeInterval({
  1670. start: times[0],
  1671. stop: times[times.length - 1],
  1672. })
  1673. );
  1674. }
  1675. if (canExtrude && extrude) {
  1676. createDropLine(entityCollection, entity, styleEntity);
  1677. }
  1678. return true;
  1679. }
  1680. function addToMultiTrack(
  1681. times,
  1682. positions,
  1683. composite,
  1684. availability,
  1685. dropShowProperty,
  1686. extrude,
  1687. altitudeMode,
  1688. gxAltitudeMode,
  1689. includeEndPoints
  1690. ) {
  1691. const start = times[0];
  1692. const stop = times[times.length - 1];
  1693. const data = new SampledPositionProperty();
  1694. data.addSamples(times, positions);
  1695. composite.intervals.addInterval(
  1696. new TimeInterval({
  1697. start: start,
  1698. stop: stop,
  1699. isStartIncluded: includeEndPoints,
  1700. isStopIncluded: includeEndPoints,
  1701. data: createPositionPropertyFromAltitudeMode(
  1702. data,
  1703. altitudeMode,
  1704. gxAltitudeMode
  1705. ),
  1706. })
  1707. );
  1708. availability.addInterval(
  1709. new TimeInterval({
  1710. start: start,
  1711. stop: stop,
  1712. isStartIncluded: includeEndPoints,
  1713. isStopIncluded: includeEndPoints,
  1714. })
  1715. );
  1716. dropShowProperty.intervals.addInterval(
  1717. new TimeInterval({
  1718. start: start,
  1719. stop: stop,
  1720. isStartIncluded: includeEndPoints,
  1721. isStopIncluded: includeEndPoints,
  1722. data: extrude,
  1723. })
  1724. );
  1725. }
  1726. function processMultiTrack(
  1727. dataSource,
  1728. entityCollection,
  1729. geometryNode,
  1730. entity,
  1731. styleEntity
  1732. ) {
  1733. // Multitrack options do not work in GE as detailed in the spec,
  1734. // rather than altitudeMode being at the MultiTrack level,
  1735. // GE just defers all settings to the underlying track.
  1736. const interpolate = queryBooleanValue(
  1737. geometryNode,
  1738. "interpolate",
  1739. namespaces.gx
  1740. );
  1741. const trackNodes = queryChildNodes(geometryNode, "Track", namespaces.gx);
  1742. let times;
  1743. let lastStop;
  1744. let lastStopPosition;
  1745. let needDropLine = false;
  1746. const dropShowProperty = new TimeIntervalCollectionProperty();
  1747. const availability = new TimeIntervalCollection();
  1748. const composite = new CompositePositionProperty();
  1749. const ellipsoid = dataSource._ellipsoid;
  1750. for (let i = 0, len = trackNodes.length; i < len; i++) {
  1751. const trackNode = trackNodes[i];
  1752. const timeNodes = queryChildNodes(trackNode, "when", namespaces.kml);
  1753. const coordNodes = queryChildNodes(trackNode, "coord", namespaces.gx);
  1754. const altitudeMode = queryStringValue(
  1755. trackNode,
  1756. "altitudeMode",
  1757. namespaces.kml
  1758. );
  1759. const gxAltitudeMode = queryStringValue(
  1760. trackNode,
  1761. "altitudeMode",
  1762. namespaces.gx
  1763. );
  1764. const canExtrude = isExtrudable(altitudeMode, gxAltitudeMode);
  1765. const extrude = queryBooleanValue(trackNode, "extrude", namespaces.kml);
  1766. const length = Math.min(coordNodes.length, timeNodes.length);
  1767. const positions = [];
  1768. times = [];
  1769. for (let x = 0; x < length; x++) {
  1770. const position = readCoordinate(coordNodes[x].textContent, ellipsoid);
  1771. positions.push(position);
  1772. times.push(JulianDate.fromIso8601(timeNodes[x].textContent));
  1773. }
  1774. if (interpolate) {
  1775. //If we are interpolating, then we need to fill in the end of
  1776. //the last track and the beginning of this one with a sampled
  1777. //property. From testing in Google Earth, this property
  1778. //is never extruded and always absolute.
  1779. if (defined(lastStop)) {
  1780. addToMultiTrack(
  1781. [lastStop, times[0]],
  1782. [lastStopPosition, positions[0]],
  1783. composite,
  1784. availability,
  1785. dropShowProperty,
  1786. false,
  1787. "absolute",
  1788. undefined,
  1789. false
  1790. );
  1791. }
  1792. lastStop = times[length - 1];
  1793. lastStopPosition = positions[positions.length - 1];
  1794. }
  1795. addToMultiTrack(
  1796. times,
  1797. positions,
  1798. composite,
  1799. availability,
  1800. dropShowProperty,
  1801. canExtrude && extrude,
  1802. altitudeMode,
  1803. gxAltitudeMode,
  1804. true
  1805. );
  1806. needDropLine = needDropLine || (canExtrude && extrude);
  1807. }
  1808. entity.availability = availability;
  1809. entity.position = composite;
  1810. processPositionGraphics(dataSource, entity, styleEntity);
  1811. processPathGraphics(entity, styleEntity);
  1812. if (needDropLine) {
  1813. createDropLine(entityCollection, entity, styleEntity);
  1814. entity.polyline.show = dropShowProperty;
  1815. }
  1816. return true;
  1817. }
  1818. const geometryTypes = {
  1819. Point: processPoint,
  1820. LineString: processLineStringOrLinearRing,
  1821. LinearRing: processLineStringOrLinearRing,
  1822. Polygon: processPolygon,
  1823. Track: processTrack,
  1824. MultiTrack: processMultiTrack,
  1825. MultiGeometry: processMultiGeometry,
  1826. Model: processUnsupportedGeometry,
  1827. };
  1828. function processMultiGeometry(
  1829. dataSource,
  1830. entityCollection,
  1831. geometryNode,
  1832. entity,
  1833. styleEntity,
  1834. context
  1835. ) {
  1836. const childNodes = geometryNode.childNodes;
  1837. let hasGeometry = false;
  1838. for (let i = 0, len = childNodes.length; i < len; i++) {
  1839. const childNode = childNodes.item(i);
  1840. const geometryProcessor = geometryTypes[childNode.localName];
  1841. if (defined(geometryProcessor)) {
  1842. const childEntity = createEntity(childNode, entityCollection, context);
  1843. childEntity.parent = entity;
  1844. childEntity.name = entity.name;
  1845. childEntity.availability = entity.availability;
  1846. childEntity.description = entity.description;
  1847. childEntity.kml = entity.kml;
  1848. if (
  1849. geometryProcessor(
  1850. dataSource,
  1851. entityCollection,
  1852. childNode,
  1853. childEntity,
  1854. styleEntity
  1855. )
  1856. ) {
  1857. hasGeometry = true;
  1858. }
  1859. }
  1860. }
  1861. return hasGeometry;
  1862. }
  1863. function processUnsupportedGeometry(
  1864. dataSource,
  1865. entityCollection,
  1866. geometryNode,
  1867. entity,
  1868. styleEntity
  1869. ) {
  1870. oneTimeWarning(
  1871. "kml-unsupportedGeometry",
  1872. `KML - Unsupported geometry: ${geometryNode.localName}`
  1873. );
  1874. return false;
  1875. }
  1876. function processExtendedData(node, entity) {
  1877. const extendedDataNode = queryFirstNode(node, "ExtendedData", namespaces.kml);
  1878. if (!defined(extendedDataNode)) {
  1879. return undefined;
  1880. }
  1881. if (defined(queryFirstNode(extendedDataNode, "SchemaData", namespaces.kml))) {
  1882. oneTimeWarning("kml-schemaData", "KML - SchemaData is unsupported");
  1883. }
  1884. if (defined(queryStringAttribute(extendedDataNode, "xmlns:prefix"))) {
  1885. oneTimeWarning(
  1886. "kml-extendedData",
  1887. "KML - ExtendedData with xmlns:prefix is unsupported"
  1888. );
  1889. }
  1890. const result = {};
  1891. const dataNodes = queryChildNodes(extendedDataNode, "Data", namespaces.kml);
  1892. if (defined(dataNodes)) {
  1893. const length = dataNodes.length;
  1894. for (let i = 0; i < length; i++) {
  1895. const dataNode = dataNodes[i];
  1896. const name = queryStringAttribute(dataNode, "name");
  1897. if (defined(name)) {
  1898. result[name] = {
  1899. displayName: queryStringValue(
  1900. dataNode,
  1901. "displayName",
  1902. namespaces.kml
  1903. ),
  1904. value: queryStringValue(dataNode, "value", namespaces.kml),
  1905. };
  1906. }
  1907. }
  1908. }
  1909. entity.kml.extendedData = result;
  1910. }
  1911. let scratchDiv;
  1912. if (typeof document !== "undefined") {
  1913. scratchDiv = document.createElement("div");
  1914. }
  1915. function processDescription(
  1916. node,
  1917. entity,
  1918. styleEntity,
  1919. uriResolver,
  1920. sourceResource
  1921. ) {
  1922. let i;
  1923. let key;
  1924. let keys;
  1925. const kmlData = entity.kml;
  1926. const extendedData = kmlData.extendedData;
  1927. const description = queryStringValue(node, "description", namespaces.kml);
  1928. const balloonStyle = defaultValue(
  1929. entity.balloonStyle,
  1930. styleEntity.balloonStyle
  1931. );
  1932. let background = Color.WHITE;
  1933. let foreground = Color.BLACK;
  1934. let text = description;
  1935. if (defined(balloonStyle)) {
  1936. background = defaultValue(balloonStyle.bgColor, Color.WHITE);
  1937. foreground = defaultValue(balloonStyle.textColor, Color.BLACK);
  1938. text = defaultValue(balloonStyle.text, description);
  1939. }
  1940. let value;
  1941. if (defined(text)) {
  1942. text = text.replace("$[name]", defaultValue(entity.name, ""));
  1943. text = text.replace("$[description]", defaultValue(description, ""));
  1944. text = text.replace("$[address]", defaultValue(kmlData.address, ""));
  1945. text = text.replace("$[Snippet]", defaultValue(kmlData.snippet, ""));
  1946. text = text.replace("$[id]", entity.id);
  1947. //While not explicitly defined by the OGC spec, in Google Earth
  1948. //The appearance of geDirections adds the directions to/from links
  1949. //We simply replace this string with nothing.
  1950. text = text.replace("$[geDirections]", "");
  1951. if (defined(extendedData)) {
  1952. const matches = text.match(/\$\[.+?\]/g);
  1953. if (matches !== null) {
  1954. for (i = 0; i < matches.length; i++) {
  1955. const token = matches[i];
  1956. let propertyName = token.substr(2, token.length - 3);
  1957. const isDisplayName = /\/displayName$/.test(propertyName);
  1958. propertyName = propertyName.replace(/\/displayName$/, "");
  1959. value = extendedData[propertyName];
  1960. if (defined(value)) {
  1961. value = isDisplayName ? value.displayName : value.value;
  1962. }
  1963. if (defined(value)) {
  1964. text = text.replace(token, defaultValue(value, ""));
  1965. }
  1966. }
  1967. }
  1968. }
  1969. } else if (defined(extendedData)) {
  1970. //If no description exists, build a table out of the extended data
  1971. keys = Object.keys(extendedData);
  1972. if (keys.length > 0) {
  1973. text =
  1974. '<table class="cesium-infoBox-defaultTable cesium-infoBox-defaultTable-lighter"><tbody>';
  1975. for (i = 0; i < keys.length; i++) {
  1976. key = keys[i];
  1977. value = extendedData[key];
  1978. text += `<tr><th>${defaultValue(
  1979. value.displayName,
  1980. key
  1981. )}</th><td>${defaultValue(value.value, "")}</td></tr>`;
  1982. }
  1983. text += "</tbody></table>";
  1984. }
  1985. }
  1986. if (!defined(text)) {
  1987. //No description
  1988. return;
  1989. }
  1990. //Turns non-explicit links into clickable links.
  1991. text = autolinker.link(text);
  1992. //Use a temporary div to manipulate the links
  1993. //so that they open in a new window.
  1994. scratchDiv.innerHTML = text;
  1995. const links = scratchDiv.querySelectorAll("a");
  1996. for (i = 0; i < links.length; i++) {
  1997. links[i].setAttribute("target", "_blank");
  1998. }
  1999. //Rewrite any KMZ embedded urls
  2000. if (defined(uriResolver) && uriResolver.keys.length > 1) {
  2001. embedDataUris(scratchDiv, "a", "href", uriResolver);
  2002. embedDataUris(scratchDiv, "link", "href", uriResolver);
  2003. embedDataUris(scratchDiv, "area", "href", uriResolver);
  2004. embedDataUris(scratchDiv, "img", "src", uriResolver);
  2005. embedDataUris(scratchDiv, "iframe", "src", uriResolver);
  2006. embedDataUris(scratchDiv, "video", "src", uriResolver);
  2007. embedDataUris(scratchDiv, "audio", "src", uriResolver);
  2008. embedDataUris(scratchDiv, "source", "src", uriResolver);
  2009. embedDataUris(scratchDiv, "track", "src", uriResolver);
  2010. embedDataUris(scratchDiv, "input", "src", uriResolver);
  2011. embedDataUris(scratchDiv, "embed", "src", uriResolver);
  2012. embedDataUris(scratchDiv, "script", "src", uriResolver);
  2013. embedDataUris(scratchDiv, "video", "poster", uriResolver);
  2014. }
  2015. //Make relative urls absolute using the sourceResource
  2016. applyBasePath(scratchDiv, "a", "href", sourceResource);
  2017. applyBasePath(scratchDiv, "link", "href", sourceResource);
  2018. applyBasePath(scratchDiv, "area", "href", sourceResource);
  2019. applyBasePath(scratchDiv, "img", "src", sourceResource);
  2020. applyBasePath(scratchDiv, "iframe", "src", sourceResource);
  2021. applyBasePath(scratchDiv, "video", "src", sourceResource);
  2022. applyBasePath(scratchDiv, "audio", "src", sourceResource);
  2023. applyBasePath(scratchDiv, "source", "src", sourceResource);
  2024. applyBasePath(scratchDiv, "track", "src", sourceResource);
  2025. applyBasePath(scratchDiv, "input", "src", sourceResource);
  2026. applyBasePath(scratchDiv, "embed", "src", sourceResource);
  2027. applyBasePath(scratchDiv, "script", "src", sourceResource);
  2028. applyBasePath(scratchDiv, "video", "poster", sourceResource);
  2029. let tmp = '<div class="cesium-infoBox-description-lighter" style="';
  2030. tmp += "overflow:auto;";
  2031. tmp += "word-wrap:break-word;";
  2032. tmp += `background-color:${background.toCssColorString()};`;
  2033. tmp += `color:${foreground.toCssColorString()};`;
  2034. tmp += '">';
  2035. tmp += `${scratchDiv.innerHTML}</div>`;
  2036. scratchDiv.innerHTML = "";
  2037. //Set the final HTML as the description.
  2038. entity.description = tmp;
  2039. }
  2040. function processFeature(dataSource, featureNode, processingData) {
  2041. const entityCollection = processingData.entityCollection;
  2042. const parent = processingData.parentEntity;
  2043. const sourceResource = processingData.sourceResource;
  2044. const uriResolver = processingData.uriResolver;
  2045. const entity = createEntity(
  2046. featureNode,
  2047. entityCollection,
  2048. processingData.context
  2049. );
  2050. const kmlData = entity.kml;
  2051. const styleEntity = computeFinalStyle(
  2052. dataSource,
  2053. featureNode,
  2054. processingData.styleCollection,
  2055. sourceResource,
  2056. uriResolver
  2057. );
  2058. const name = queryStringValue(featureNode, "name", namespaces.kml);
  2059. entity.name = name;
  2060. entity.parent = parent;
  2061. let availability = processTimeSpan(featureNode);
  2062. if (!defined(availability)) {
  2063. availability = processTimeStamp(featureNode);
  2064. }
  2065. entity.availability = availability;
  2066. mergeAvailabilityWithParent(entity);
  2067. // Per KML spec "A Feature is visible only if it and all its ancestors are visible."
  2068. function ancestryIsVisible(parentEntity) {
  2069. if (!parentEntity) {
  2070. return true;
  2071. }
  2072. return parentEntity.show && ancestryIsVisible(parentEntity.parent);
  2073. }
  2074. const visibility = queryBooleanValue(
  2075. featureNode,
  2076. "visibility",
  2077. namespaces.kml
  2078. );
  2079. entity.show = ancestryIsVisible(parent) && defaultValue(visibility, true);
  2080. //const open = queryBooleanValue(featureNode, 'open', namespaces.kml);
  2081. const authorNode = queryFirstNode(featureNode, "author", namespaces.atom);
  2082. const author = kmlData.author;
  2083. author.name = queryStringValue(authorNode, "name", namespaces.atom);
  2084. author.uri = queryStringValue(authorNode, "uri", namespaces.atom);
  2085. author.email = queryStringValue(authorNode, "email", namespaces.atom);
  2086. const linkNode = queryFirstNode(featureNode, "link", namespaces.atom);
  2087. const link = kmlData.link;
  2088. link.href = queryStringAttribute(linkNode, "href");
  2089. link.hreflang = queryStringAttribute(linkNode, "hreflang");
  2090. link.rel = queryStringAttribute(linkNode, "rel");
  2091. link.type = queryStringAttribute(linkNode, "type");
  2092. link.title = queryStringAttribute(linkNode, "title");
  2093. link.length = queryStringAttribute(linkNode, "length");
  2094. kmlData.address = queryStringValue(featureNode, "address", namespaces.kml);
  2095. kmlData.phoneNumber = queryStringValue(
  2096. featureNode,
  2097. "phoneNumber",
  2098. namespaces.kml
  2099. );
  2100. kmlData.snippet = queryStringValue(featureNode, "Snippet", namespaces.kml);
  2101. processExtendedData(featureNode, entity);
  2102. processDescription(
  2103. featureNode,
  2104. entity,
  2105. styleEntity,
  2106. uriResolver,
  2107. sourceResource
  2108. );
  2109. const ellipsoid = dataSource._ellipsoid;
  2110. processLookAt(featureNode, entity, ellipsoid);
  2111. processCamera(featureNode, entity, ellipsoid);
  2112. if (defined(queryFirstNode(featureNode, "Region", namespaces.kml))) {
  2113. oneTimeWarning("kml-region", "KML - Placemark Regions are unsupported");
  2114. }
  2115. return {
  2116. entity: entity,
  2117. styleEntity: styleEntity,
  2118. };
  2119. }
  2120. function processDocument(dataSource, node, processingData, deferredLoading) {
  2121. deferredLoading.addNodes(node.childNodes, processingData);
  2122. deferredLoading.process();
  2123. }
  2124. function processFolder(dataSource, node, processingData, deferredLoading) {
  2125. const r = processFeature(dataSource, node, processingData);
  2126. const newProcessingData = clone(processingData);
  2127. newProcessingData.parentEntity = r.entity;
  2128. processDocument(dataSource, node, newProcessingData, deferredLoading);
  2129. }
  2130. function processPlacemark(
  2131. dataSource,
  2132. placemark,
  2133. processingData,
  2134. deferredLoading
  2135. ) {
  2136. const r = processFeature(dataSource, placemark, processingData);
  2137. const entity = r.entity;
  2138. const styleEntity = r.styleEntity;
  2139. let hasGeometry = false;
  2140. const childNodes = placemark.childNodes;
  2141. for (let i = 0, len = childNodes.length; i < len && !hasGeometry; i++) {
  2142. const childNode = childNodes.item(i);
  2143. const geometryProcessor = geometryTypes[childNode.localName];
  2144. if (defined(geometryProcessor)) {
  2145. // pass the placemark entity id as a context for case of defining multiple child entities together to handle case
  2146. // where some malformed kmls reuse the same id across placemarks, which works in GE, but is not technically to spec.
  2147. geometryProcessor(
  2148. dataSource,
  2149. processingData.entityCollection,
  2150. childNode,
  2151. entity,
  2152. styleEntity,
  2153. entity.id
  2154. );
  2155. hasGeometry = true;
  2156. }
  2157. }
  2158. if (!hasGeometry) {
  2159. entity.merge(styleEntity);
  2160. processPositionGraphics(dataSource, entity, styleEntity);
  2161. }
  2162. }
  2163. const playlistNodeProcessors = {
  2164. FlyTo: processTourFlyTo,
  2165. Wait: processTourWait,
  2166. SoundCue: processTourUnsupportedNode,
  2167. AnimatedUpdate: processTourUnsupportedNode,
  2168. TourControl: processTourUnsupportedNode,
  2169. };
  2170. function processTour(dataSource, node, processingData, deferredLoading) {
  2171. const name = queryStringValue(node, "name", namespaces.kml);
  2172. const id = queryStringAttribute(node, "id");
  2173. const tour = new KmlTour(name, id);
  2174. const playlistNode = queryFirstNode(node, "Playlist", namespaces.gx);
  2175. if (playlistNode) {
  2176. const ellipsoid = dataSource._ellipsoid;
  2177. const childNodes = playlistNode.childNodes;
  2178. for (let i = 0; i < childNodes.length; i++) {
  2179. const entryNode = childNodes[i];
  2180. if (entryNode.localName) {
  2181. const playlistNodeProcessor =
  2182. playlistNodeProcessors[entryNode.localName];
  2183. if (playlistNodeProcessor) {
  2184. playlistNodeProcessor(tour, entryNode, ellipsoid);
  2185. } else {
  2186. console.log(
  2187. `Unknown KML Tour playlist entry type ${entryNode.localName}`
  2188. );
  2189. }
  2190. }
  2191. }
  2192. }
  2193. dataSource._kmlTours.push(tour);
  2194. }
  2195. function processTourUnsupportedNode(tour, entryNode) {
  2196. oneTimeWarning(`KML Tour unsupported node ${entryNode.localName}`);
  2197. }
  2198. function processTourWait(tour, entryNode) {
  2199. const duration = queryNumericValue(entryNode, "duration", namespaces.gx);
  2200. tour.addPlaylistEntry(new KmlTourWait(duration));
  2201. }
  2202. function processTourFlyTo(tour, entryNode, ellipsoid) {
  2203. const duration = queryNumericValue(entryNode, "duration", namespaces.gx);
  2204. const flyToMode = queryStringValue(entryNode, "flyToMode", namespaces.gx);
  2205. const t = { kml: {} };
  2206. processLookAt(entryNode, t, ellipsoid);
  2207. processCamera(entryNode, t, ellipsoid);
  2208. const view = t.kml.lookAt || t.kml.camera;
  2209. const flyto = new KmlTourFlyTo(duration, flyToMode, view);
  2210. tour.addPlaylistEntry(flyto);
  2211. }
  2212. function processCamera(featureNode, entity, ellipsoid) {
  2213. const camera = queryFirstNode(featureNode, "Camera", namespaces.kml);
  2214. if (defined(camera)) {
  2215. const lon = defaultValue(
  2216. queryNumericValue(camera, "longitude", namespaces.kml),
  2217. 0.0
  2218. );
  2219. const lat = defaultValue(
  2220. queryNumericValue(camera, "latitude", namespaces.kml),
  2221. 0.0
  2222. );
  2223. const altitude = defaultValue(
  2224. queryNumericValue(camera, "altitude", namespaces.kml),
  2225. 0.0
  2226. );
  2227. const heading = defaultValue(
  2228. queryNumericValue(camera, "heading", namespaces.kml),
  2229. 0.0
  2230. );
  2231. const tilt = defaultValue(
  2232. queryNumericValue(camera, "tilt", namespaces.kml),
  2233. 0.0
  2234. );
  2235. const roll = defaultValue(
  2236. queryNumericValue(camera, "roll", namespaces.kml),
  2237. 0.0
  2238. );
  2239. const position = Cartesian3.fromDegrees(lon, lat, altitude, ellipsoid);
  2240. const hpr = HeadingPitchRoll.fromDegrees(heading, tilt - 90.0, roll);
  2241. entity.kml.camera = new KmlCamera(position, hpr);
  2242. }
  2243. }
  2244. function processLookAt(featureNode, entity, ellipsoid) {
  2245. const lookAt = queryFirstNode(featureNode, "LookAt", namespaces.kml);
  2246. if (defined(lookAt)) {
  2247. const lon = defaultValue(
  2248. queryNumericValue(lookAt, "longitude", namespaces.kml),
  2249. 0.0
  2250. );
  2251. const lat = defaultValue(
  2252. queryNumericValue(lookAt, "latitude", namespaces.kml),
  2253. 0.0
  2254. );
  2255. const altitude = defaultValue(
  2256. queryNumericValue(lookAt, "altitude", namespaces.kml),
  2257. 0.0
  2258. );
  2259. let heading = queryNumericValue(lookAt, "heading", namespaces.kml);
  2260. let tilt = queryNumericValue(lookAt, "tilt", namespaces.kml);
  2261. const range = defaultValue(
  2262. queryNumericValue(lookAt, "range", namespaces.kml),
  2263. 0.0
  2264. );
  2265. tilt = CesiumMath.toRadians(defaultValue(tilt, 0.0));
  2266. heading = CesiumMath.toRadians(defaultValue(heading, 0.0));
  2267. const hpr = new HeadingPitchRange(
  2268. heading,
  2269. tilt - CesiumMath.PI_OVER_TWO,
  2270. range
  2271. );
  2272. const viewPoint = Cartesian3.fromDegrees(lon, lat, altitude, ellipsoid);
  2273. entity.kml.lookAt = new KmlLookAt(viewPoint, hpr);
  2274. }
  2275. }
  2276. function processScreenOverlay(
  2277. dataSource,
  2278. screenOverlayNode,
  2279. processingData,
  2280. deferredLoading
  2281. ) {
  2282. const screenOverlay = processingData.screenOverlayContainer;
  2283. if (!defined(screenOverlay)) {
  2284. return undefined;
  2285. }
  2286. const sourceResource = processingData.sourceResource;
  2287. const uriResolver = processingData.uriResolver;
  2288. const iconNode = queryFirstNode(screenOverlayNode, "Icon", namespaces.kml);
  2289. const icon = getIconHref(
  2290. iconNode,
  2291. dataSource,
  2292. sourceResource,
  2293. uriResolver,
  2294. false
  2295. );
  2296. if (!defined(icon)) {
  2297. return undefined;
  2298. }
  2299. const img = document.createElement("img");
  2300. dataSource._screenOverlays.push(img);
  2301. img.src = icon.url;
  2302. img.onload = function () {
  2303. const styles = ["position: absolute"];
  2304. const screenXY = queryFirstNode(
  2305. screenOverlayNode,
  2306. "screenXY",
  2307. namespaces.kml
  2308. );
  2309. const overlayXY = queryFirstNode(
  2310. screenOverlayNode,
  2311. "overlayXY",
  2312. namespaces.kml
  2313. );
  2314. const size = queryFirstNode(screenOverlayNode, "size", namespaces.kml);
  2315. let x, y;
  2316. let xUnit, yUnit;
  2317. let xStyle, yStyle;
  2318. if (defined(size)) {
  2319. x = queryNumericAttribute(size, "x");
  2320. y = queryNumericAttribute(size, "y");
  2321. xUnit = queryStringAttribute(size, "xunits");
  2322. yUnit = queryStringAttribute(size, "yunits");
  2323. if (defined(x) && x !== -1 && x !== 0) {
  2324. if (xUnit === "fraction") {
  2325. xStyle = `width: ${Math.floor(x * 100)}%`;
  2326. } else if (xUnit === "pixels") {
  2327. xStyle = `width: ${x}px`;
  2328. }
  2329. styles.push(xStyle);
  2330. }
  2331. if (defined(y) && y !== -1 && y !== 0) {
  2332. if (yUnit === "fraction") {
  2333. yStyle = `height: ${Math.floor(y * 100)}%`;
  2334. } else if (yUnit === "pixels") {
  2335. yStyle = `height: ${y}px`;
  2336. }
  2337. styles.push(yStyle);
  2338. }
  2339. }
  2340. // set the interim style so the width/height properties get calculated
  2341. img.style = styles.join(";");
  2342. let xOrigin = 0;
  2343. let yOrigin = img.height;
  2344. if (defined(overlayXY)) {
  2345. x = queryNumericAttribute(overlayXY, "x");
  2346. y = queryNumericAttribute(overlayXY, "y");
  2347. xUnit = queryStringAttribute(overlayXY, "xunits");
  2348. yUnit = queryStringAttribute(overlayXY, "yunits");
  2349. if (defined(x)) {
  2350. if (xUnit === "fraction") {
  2351. xOrigin = x * img.width;
  2352. } else if (xUnit === "pixels") {
  2353. xOrigin = x;
  2354. } else if (xUnit === "insetPixels") {
  2355. xOrigin = x;
  2356. }
  2357. }
  2358. if (defined(y)) {
  2359. if (yUnit === "fraction") {
  2360. yOrigin = y * img.height;
  2361. } else if (yUnit === "pixels") {
  2362. yOrigin = y;
  2363. } else if (yUnit === "insetPixels") {
  2364. yOrigin = y;
  2365. }
  2366. }
  2367. }
  2368. if (defined(screenXY)) {
  2369. x = queryNumericAttribute(screenXY, "x");
  2370. y = queryNumericAttribute(screenXY, "y");
  2371. xUnit = queryStringAttribute(screenXY, "xunits");
  2372. yUnit = queryStringAttribute(screenXY, "yunits");
  2373. if (defined(x)) {
  2374. if (xUnit === "fraction") {
  2375. xStyle = `${"left: " + "calc("}${Math.floor(
  2376. x * 100
  2377. )}% - ${xOrigin}px)`;
  2378. } else if (xUnit === "pixels") {
  2379. xStyle = `left: ${x - xOrigin}px`;
  2380. } else if (xUnit === "insetPixels") {
  2381. xStyle = `right: ${x - xOrigin}px`;
  2382. }
  2383. styles.push(xStyle);
  2384. }
  2385. if (defined(y)) {
  2386. if (yUnit === "fraction") {
  2387. yStyle = `${"bottom: " + "calc("}${Math.floor(
  2388. y * 100
  2389. )}% - ${yOrigin}px)`;
  2390. } else if (yUnit === "pixels") {
  2391. yStyle = `bottom: ${y - yOrigin}px`;
  2392. } else if (yUnit === "insetPixels") {
  2393. yStyle = `top: ${y - yOrigin}px`;
  2394. }
  2395. styles.push(yStyle);
  2396. }
  2397. }
  2398. img.style = styles.join(";");
  2399. };
  2400. screenOverlay.appendChild(img);
  2401. }
  2402. function processGroundOverlay(
  2403. dataSource,
  2404. groundOverlay,
  2405. processingData,
  2406. deferredLoading
  2407. ) {
  2408. const r = processFeature(dataSource, groundOverlay, processingData);
  2409. const entity = r.entity;
  2410. let geometry;
  2411. let isLatLonQuad = false;
  2412. const ellipsoid = dataSource._ellipsoid;
  2413. const positions = readCoordinates(
  2414. queryFirstNode(groundOverlay, "LatLonQuad", namespaces.gx),
  2415. ellipsoid
  2416. );
  2417. const zIndex = queryNumericValue(groundOverlay, "drawOrder", namespaces.kml);
  2418. if (defined(positions)) {
  2419. geometry = createDefaultPolygon();
  2420. geometry.hierarchy = new PolygonHierarchy(positions);
  2421. geometry.zIndex = zIndex;
  2422. entity.polygon = geometry;
  2423. isLatLonQuad = true;
  2424. } else {
  2425. geometry = new RectangleGraphics();
  2426. geometry.zIndex = zIndex;
  2427. entity.rectangle = geometry;
  2428. const latLonBox = queryFirstNode(
  2429. groundOverlay,
  2430. "LatLonBox",
  2431. namespaces.kml
  2432. );
  2433. if (defined(latLonBox)) {
  2434. let west = queryNumericValue(latLonBox, "west", namespaces.kml);
  2435. let south = queryNumericValue(latLonBox, "south", namespaces.kml);
  2436. let east = queryNumericValue(latLonBox, "east", namespaces.kml);
  2437. let north = queryNumericValue(latLonBox, "north", namespaces.kml);
  2438. if (defined(west)) {
  2439. west = CesiumMath.negativePiToPi(CesiumMath.toRadians(west));
  2440. }
  2441. if (defined(south)) {
  2442. south = CesiumMath.clampToLatitudeRange(CesiumMath.toRadians(south));
  2443. }
  2444. if (defined(east)) {
  2445. east = CesiumMath.negativePiToPi(CesiumMath.toRadians(east));
  2446. }
  2447. if (defined(north)) {
  2448. north = CesiumMath.clampToLatitudeRange(CesiumMath.toRadians(north));
  2449. }
  2450. geometry.coordinates = new Rectangle(west, south, east, north);
  2451. const rotation = queryNumericValue(latLonBox, "rotation", namespaces.kml);
  2452. if (defined(rotation)) {
  2453. const rotationRadians = CesiumMath.toRadians(rotation);
  2454. geometry.rotation = rotationRadians;
  2455. geometry.stRotation = rotationRadians;
  2456. }
  2457. }
  2458. }
  2459. const iconNode = queryFirstNode(groundOverlay, "Icon", namespaces.kml);
  2460. const href = getIconHref(
  2461. iconNode,
  2462. dataSource,
  2463. processingData.sourceResource,
  2464. processingData.uriResolver,
  2465. true
  2466. );
  2467. if (defined(href)) {
  2468. if (isLatLonQuad) {
  2469. oneTimeWarning(
  2470. "kml-gx:LatLonQuad",
  2471. "KML - gx:LatLonQuad Icon does not support texture projection."
  2472. );
  2473. }
  2474. const x = queryNumericValue(iconNode, "x", namespaces.gx);
  2475. const y = queryNumericValue(iconNode, "y", namespaces.gx);
  2476. const w = queryNumericValue(iconNode, "w", namespaces.gx);
  2477. const h = queryNumericValue(iconNode, "h", namespaces.gx);
  2478. if (defined(x) || defined(y) || defined(w) || defined(h)) {
  2479. oneTimeWarning(
  2480. "kml-groundOverlay-xywh",
  2481. "KML - gx:x, gx:y, gx:w, gx:h aren't supported for GroundOverlays"
  2482. );
  2483. }
  2484. geometry.material = href;
  2485. geometry.material.color = queryColorValue(
  2486. groundOverlay,
  2487. "color",
  2488. namespaces.kml
  2489. );
  2490. geometry.material.transparent = true;
  2491. } else {
  2492. geometry.material = queryColorValue(groundOverlay, "color", namespaces.kml);
  2493. }
  2494. let altitudeMode = queryStringValue(
  2495. groundOverlay,
  2496. "altitudeMode",
  2497. namespaces.kml
  2498. );
  2499. if (defined(altitudeMode)) {
  2500. if (altitudeMode === "absolute") {
  2501. //Use height above ellipsoid until we support MSL.
  2502. geometry.height = queryNumericValue(
  2503. groundOverlay,
  2504. "altitude",
  2505. namespaces.kml
  2506. );
  2507. geometry.zIndex = undefined;
  2508. } else if (altitudeMode !== "clampToGround") {
  2509. oneTimeWarning(
  2510. "kml-altitudeMode-unknown",
  2511. `KML - Unknown altitudeMode: ${altitudeMode}`
  2512. );
  2513. }
  2514. // else just use the default of 0 until we support 'clampToGround'
  2515. } else {
  2516. altitudeMode = queryStringValue(
  2517. groundOverlay,
  2518. "altitudeMode",
  2519. namespaces.gx
  2520. );
  2521. if (altitudeMode === "relativeToSeaFloor") {
  2522. oneTimeWarning(
  2523. "kml-altitudeMode-relativeToSeaFloor",
  2524. "KML - altitudeMode relativeToSeaFloor is currently not supported, treating as absolute."
  2525. );
  2526. geometry.height = queryNumericValue(
  2527. groundOverlay,
  2528. "altitude",
  2529. namespaces.kml
  2530. );
  2531. geometry.zIndex = undefined;
  2532. } else if (altitudeMode === "clampToSeaFloor") {
  2533. oneTimeWarning(
  2534. "kml-altitudeMode-clampToSeaFloor",
  2535. "KML - altitudeMode clampToSeaFloor is currently not supported, treating as clampToGround."
  2536. );
  2537. } else if (defined(altitudeMode)) {
  2538. oneTimeWarning(
  2539. "kml-altitudeMode-unknown",
  2540. `KML - Unknown altitudeMode: ${altitudeMode}`
  2541. );
  2542. }
  2543. }
  2544. }
  2545. function processUnsupportedFeature(
  2546. dataSource,
  2547. node,
  2548. processingData,
  2549. deferredLoading
  2550. ) {
  2551. dataSource._unsupportedNode.raiseEvent(
  2552. dataSource,
  2553. processingData.parentEntity,
  2554. node,
  2555. processingData.entityCollection,
  2556. processingData.styleCollection,
  2557. processingData.sourceResource,
  2558. processingData.uriResolver
  2559. );
  2560. oneTimeWarning(
  2561. `kml-unsupportedFeature-${node.nodeName}`,
  2562. `KML - Unsupported feature: ${node.nodeName}`
  2563. );
  2564. }
  2565. const RefreshMode = {
  2566. INTERVAL: 0,
  2567. EXPIRE: 1,
  2568. STOP: 2,
  2569. };
  2570. function cleanupString(s) {
  2571. if (!defined(s) || s.length === 0) {
  2572. return "";
  2573. }
  2574. const sFirst = s[0];
  2575. if (sFirst === "&" || sFirst === "?") {
  2576. s = s.substring(1);
  2577. }
  2578. return s;
  2579. }
  2580. const zeroRectangle = new Rectangle();
  2581. const scratchCartographic = new Cartographic();
  2582. const scratchCartesian2 = new Cartesian2();
  2583. const scratchCartesian3 = new Cartesian3();
  2584. function processNetworkLinkQueryString(
  2585. resource,
  2586. camera,
  2587. canvas,
  2588. viewBoundScale,
  2589. bbox,
  2590. ellipsoid
  2591. ) {
  2592. function fixLatitude(value) {
  2593. if (value < -CesiumMath.PI_OVER_TWO) {
  2594. return -CesiumMath.PI_OVER_TWO;
  2595. } else if (value > CesiumMath.PI_OVER_TWO) {
  2596. return CesiumMath.PI_OVER_TWO;
  2597. }
  2598. return value;
  2599. }
  2600. function fixLongitude(value) {
  2601. if (value > CesiumMath.PI) {
  2602. return value - CesiumMath.TWO_PI;
  2603. } else if (value < -CesiumMath.PI) {
  2604. return value + CesiumMath.TWO_PI;
  2605. }
  2606. return value;
  2607. }
  2608. let queryString = objectToQuery(resource.queryParameters);
  2609. // objectToQuery escapes [ and ], so fix that
  2610. queryString = queryString.replace(/%5B/g, "[").replace(/%5D/g, "]");
  2611. if (defined(camera) && camera._mode !== SceneMode.MORPHING) {
  2612. let centerCartesian;
  2613. let centerCartographic;
  2614. bbox = defaultValue(bbox, zeroRectangle);
  2615. if (defined(canvas)) {
  2616. scratchCartesian2.x = canvas.clientWidth * 0.5;
  2617. scratchCartesian2.y = canvas.clientHeight * 0.5;
  2618. centerCartesian = camera.pickEllipsoid(
  2619. scratchCartesian2,
  2620. ellipsoid,
  2621. scratchCartesian3
  2622. );
  2623. }
  2624. if (defined(centerCartesian)) {
  2625. centerCartographic = ellipsoid.cartesianToCartographic(
  2626. centerCartesian,
  2627. scratchCartographic
  2628. );
  2629. } else {
  2630. centerCartographic = Rectangle.center(bbox, scratchCartographic);
  2631. centerCartesian = ellipsoid.cartographicToCartesian(centerCartographic);
  2632. }
  2633. if (
  2634. defined(viewBoundScale) &&
  2635. !CesiumMath.equalsEpsilon(viewBoundScale, 1.0, CesiumMath.EPSILON9)
  2636. ) {
  2637. const newHalfWidth = bbox.width * viewBoundScale * 0.5;
  2638. const newHalfHeight = bbox.height * viewBoundScale * 0.5;
  2639. bbox = new Rectangle(
  2640. fixLongitude(centerCartographic.longitude - newHalfWidth),
  2641. fixLatitude(centerCartographic.latitude - newHalfHeight),
  2642. fixLongitude(centerCartographic.longitude + newHalfWidth),
  2643. fixLatitude(centerCartographic.latitude + newHalfHeight)
  2644. );
  2645. }
  2646. queryString = queryString.replace(
  2647. "[bboxWest]",
  2648. CesiumMath.toDegrees(bbox.west).toString()
  2649. );
  2650. queryString = queryString.replace(
  2651. "[bboxSouth]",
  2652. CesiumMath.toDegrees(bbox.south).toString()
  2653. );
  2654. queryString = queryString.replace(
  2655. "[bboxEast]",
  2656. CesiumMath.toDegrees(bbox.east).toString()
  2657. );
  2658. queryString = queryString.replace(
  2659. "[bboxNorth]",
  2660. CesiumMath.toDegrees(bbox.north).toString()
  2661. );
  2662. const lon = CesiumMath.toDegrees(centerCartographic.longitude).toString();
  2663. const lat = CesiumMath.toDegrees(centerCartographic.latitude).toString();
  2664. queryString = queryString.replace("[lookatLon]", lon);
  2665. queryString = queryString.replace("[lookatLat]", lat);
  2666. queryString = queryString.replace(
  2667. "[lookatTilt]",
  2668. CesiumMath.toDegrees(camera.pitch).toString()
  2669. );
  2670. queryString = queryString.replace(
  2671. "[lookatHeading]",
  2672. CesiumMath.toDegrees(camera.heading).toString()
  2673. );
  2674. queryString = queryString.replace(
  2675. "[lookatRange]",
  2676. Cartesian3.distance(camera.positionWC, centerCartesian)
  2677. );
  2678. queryString = queryString.replace("[lookatTerrainLon]", lon);
  2679. queryString = queryString.replace("[lookatTerrainLat]", lat);
  2680. queryString = queryString.replace(
  2681. "[lookatTerrainAlt]",
  2682. centerCartographic.height.toString()
  2683. );
  2684. ellipsoid.cartesianToCartographic(camera.positionWC, scratchCartographic);
  2685. queryString = queryString.replace(
  2686. "[cameraLon]",
  2687. CesiumMath.toDegrees(scratchCartographic.longitude).toString()
  2688. );
  2689. queryString = queryString.replace(
  2690. "[cameraLat]",
  2691. CesiumMath.toDegrees(scratchCartographic.latitude).toString()
  2692. );
  2693. queryString = queryString.replace(
  2694. "[cameraAlt]",
  2695. CesiumMath.toDegrees(scratchCartographic.height).toString()
  2696. );
  2697. const frustum = camera.frustum;
  2698. const aspectRatio = frustum.aspectRatio;
  2699. let horizFov = "";
  2700. let vertFov = "";
  2701. if (defined(aspectRatio)) {
  2702. const fov = CesiumMath.toDegrees(frustum.fov);
  2703. if (aspectRatio > 1.0) {
  2704. horizFov = fov;
  2705. vertFov = fov / aspectRatio;
  2706. } else {
  2707. vertFov = fov;
  2708. horizFov = fov * aspectRatio;
  2709. }
  2710. }
  2711. queryString = queryString.replace("[horizFov]", horizFov.toString());
  2712. queryString = queryString.replace("[vertFov]", vertFov.toString());
  2713. } else {
  2714. queryString = queryString.replace("[bboxWest]", "-180");
  2715. queryString = queryString.replace("[bboxSouth]", "-90");
  2716. queryString = queryString.replace("[bboxEast]", "180");
  2717. queryString = queryString.replace("[bboxNorth]", "90");
  2718. queryString = queryString.replace("[lookatLon]", "");
  2719. queryString = queryString.replace("[lookatLat]", "");
  2720. queryString = queryString.replace("[lookatRange]", "");
  2721. queryString = queryString.replace("[lookatTilt]", "");
  2722. queryString = queryString.replace("[lookatHeading]", "");
  2723. queryString = queryString.replace("[lookatTerrainLon]", "");
  2724. queryString = queryString.replace("[lookatTerrainLat]", "");
  2725. queryString = queryString.replace("[lookatTerrainAlt]", "");
  2726. queryString = queryString.replace("[cameraLon]", "");
  2727. queryString = queryString.replace("[cameraLat]", "");
  2728. queryString = queryString.replace("[cameraAlt]", "");
  2729. queryString = queryString.replace("[horizFov]", "");
  2730. queryString = queryString.replace("[vertFov]", "");
  2731. }
  2732. if (defined(canvas)) {
  2733. queryString = queryString.replace("[horizPixels]", canvas.clientWidth);
  2734. queryString = queryString.replace("[vertPixels]", canvas.clientHeight);
  2735. } else {
  2736. queryString = queryString.replace("[horizPixels]", "");
  2737. queryString = queryString.replace("[vertPixels]", "");
  2738. }
  2739. queryString = queryString.replace("[terrainEnabled]", "1");
  2740. queryString = queryString.replace("[clientVersion]", "1");
  2741. queryString = queryString.replace("[kmlVersion]", "2.2");
  2742. queryString = queryString.replace("[clientName]", "Cesium");
  2743. queryString = queryString.replace("[language]", "English");
  2744. resource.setQueryParameters(queryToObject(queryString));
  2745. }
  2746. function processNetworkLink(dataSource, node, processingData, deferredLoading) {
  2747. const r = processFeature(dataSource, node, processingData);
  2748. const networkEntity = r.entity;
  2749. const sourceResource = processingData.sourceResource;
  2750. const uriResolver = processingData.uriResolver;
  2751. let link = queryFirstNode(node, "Link", namespaces.kml);
  2752. if (!defined(link)) {
  2753. link = queryFirstNode(node, "Url", namespaces.kml);
  2754. }
  2755. if (defined(link)) {
  2756. let href = queryStringValue(link, "href", namespaces.kml);
  2757. let viewRefreshMode;
  2758. let viewBoundScale;
  2759. if (defined(href)) {
  2760. let newSourceUri = href;
  2761. href = resolveHref(href, sourceResource, processingData.uriResolver);
  2762. // We need to pass in the original path if resolveHref returns a data uri because the network link
  2763. // references a document in a KMZ archive
  2764. if (/^data:/.test(href.getUrlComponent())) {
  2765. // So if sourceUri isn't the kmz file, then its another kml in the archive, so resolve it
  2766. if (!/\.kmz/i.test(sourceResource.getUrlComponent())) {
  2767. newSourceUri = sourceResource.getDerivedResource({
  2768. url: newSourceUri,
  2769. });
  2770. }
  2771. } else {
  2772. newSourceUri = href.clone(); // Not a data uri so use the fully qualified uri
  2773. viewRefreshMode = queryStringValue(
  2774. link,
  2775. "viewRefreshMode",
  2776. namespaces.kml
  2777. );
  2778. if (viewRefreshMode === "onRegion") {
  2779. oneTimeWarning(
  2780. "kml-refrehMode-onRegion",
  2781. "KML - Unsupported viewRefreshMode: onRegion"
  2782. );
  2783. return;
  2784. }
  2785. viewBoundScale = defaultValue(
  2786. queryStringValue(link, "viewBoundScale", namespaces.kml),
  2787. 1.0
  2788. );
  2789. const defaultViewFormat =
  2790. viewRefreshMode === "onStop"
  2791. ? "BBOX=[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]"
  2792. : "";
  2793. const viewFormat = defaultValue(
  2794. queryStringValue(link, "viewFormat", namespaces.kml),
  2795. defaultViewFormat
  2796. );
  2797. const httpQuery = queryStringValue(link, "httpQuery", namespaces.kml);
  2798. if (defined(viewFormat)) {
  2799. href.setQueryParameters(queryToObject(cleanupString(viewFormat)));
  2800. }
  2801. if (defined(httpQuery)) {
  2802. href.setQueryParameters(queryToObject(cleanupString(httpQuery)));
  2803. }
  2804. const ellipsoid = dataSource._ellipsoid;
  2805. processNetworkLinkQueryString(
  2806. href,
  2807. dataSource.camera,
  2808. dataSource.canvas,
  2809. viewBoundScale,
  2810. dataSource._lastCameraView.bbox,
  2811. ellipsoid
  2812. );
  2813. }
  2814. const options = {
  2815. sourceUri: newSourceUri,
  2816. uriResolver: uriResolver,
  2817. context: networkEntity.id,
  2818. screenOverlayContainer: processingData.screenOverlayContainer,
  2819. };
  2820. const networkLinkCollection = new EntityCollection();
  2821. const promise = load(dataSource, networkLinkCollection, href, options)
  2822. .then(function (rootElement) {
  2823. const entities = dataSource._entityCollection;
  2824. const newEntities = networkLinkCollection.values;
  2825. entities.suspendEvents();
  2826. for (let i = 0; i < newEntities.length; i++) {
  2827. const newEntity = newEntities[i];
  2828. if (!defined(newEntity.parent)) {
  2829. newEntity.parent = networkEntity;
  2830. mergeAvailabilityWithParent(newEntity);
  2831. }
  2832. entities.add(newEntity);
  2833. }
  2834. entities.resumeEvents();
  2835. // Add network links to a list if we need they will need to be updated
  2836. const refreshMode = queryStringValue(
  2837. link,
  2838. "refreshMode",
  2839. namespaces.kml
  2840. );
  2841. let refreshInterval = defaultValue(
  2842. queryNumericValue(link, "refreshInterval", namespaces.kml),
  2843. 0
  2844. );
  2845. if (
  2846. (refreshMode === "onInterval" && refreshInterval > 0) ||
  2847. refreshMode === "onExpire" ||
  2848. viewRefreshMode === "onStop"
  2849. ) {
  2850. const networkLinkControl = queryFirstNode(
  2851. rootElement,
  2852. "NetworkLinkControl",
  2853. namespaces.kml
  2854. );
  2855. const hasNetworkLinkControl = defined(networkLinkControl);
  2856. const now = JulianDate.now();
  2857. const networkLinkInfo = {
  2858. id: createGuid(),
  2859. href: href,
  2860. cookie: {},
  2861. lastUpdated: now,
  2862. updating: false,
  2863. entity: networkEntity,
  2864. viewBoundScale: viewBoundScale,
  2865. needsUpdate: false,
  2866. cameraUpdateTime: now,
  2867. };
  2868. let minRefreshPeriod = 0;
  2869. if (hasNetworkLinkControl) {
  2870. networkLinkInfo.cookie = queryToObject(
  2871. defaultValue(
  2872. queryStringValue(
  2873. networkLinkControl,
  2874. "cookie",
  2875. namespaces.kml
  2876. ),
  2877. ""
  2878. )
  2879. );
  2880. minRefreshPeriod = defaultValue(
  2881. queryNumericValue(
  2882. networkLinkControl,
  2883. "minRefreshPeriod",
  2884. namespaces.kml
  2885. ),
  2886. 0
  2887. );
  2888. }
  2889. if (refreshMode === "onInterval") {
  2890. if (hasNetworkLinkControl) {
  2891. refreshInterval = Math.max(minRefreshPeriod, refreshInterval);
  2892. }
  2893. networkLinkInfo.refreshMode = RefreshMode.INTERVAL;
  2894. networkLinkInfo.time = refreshInterval;
  2895. } else if (refreshMode === "onExpire") {
  2896. let expires;
  2897. if (hasNetworkLinkControl) {
  2898. expires = queryStringValue(
  2899. networkLinkControl,
  2900. "expires",
  2901. namespaces.kml
  2902. );
  2903. }
  2904. if (defined(expires)) {
  2905. try {
  2906. const date = JulianDate.fromIso8601(expires);
  2907. const diff = JulianDate.secondsDifference(date, now);
  2908. if (diff > 0 && diff < minRefreshPeriod) {
  2909. JulianDate.addSeconds(now, minRefreshPeriod, date);
  2910. }
  2911. networkLinkInfo.refreshMode = RefreshMode.EXPIRE;
  2912. networkLinkInfo.time = date;
  2913. } catch (e) {
  2914. oneTimeWarning(
  2915. "kml-refreshMode-onInterval-onExpire",
  2916. "KML - NetworkLinkControl expires is not a valid date"
  2917. );
  2918. }
  2919. } else {
  2920. oneTimeWarning(
  2921. "kml-refreshMode-onExpire",
  2922. "KML - refreshMode of onExpire requires the NetworkLinkControl to have an expires element"
  2923. );
  2924. }
  2925. } else if (defined(dataSource.camera)) {
  2926. // Only allow onStop refreshes if we have a camera
  2927. networkLinkInfo.refreshMode = RefreshMode.STOP;
  2928. networkLinkInfo.time = defaultValue(
  2929. queryNumericValue(link, "viewRefreshTime", namespaces.kml),
  2930. 0
  2931. );
  2932. } else {
  2933. oneTimeWarning(
  2934. "kml-refrehMode-onStop-noCamera",
  2935. "A NetworkLink with viewRefreshMode=onStop requires the `camera` property to be defined."
  2936. );
  2937. }
  2938. if (defined(networkLinkInfo.refreshMode)) {
  2939. dataSource._networkLinks.set(networkLinkInfo.id, networkLinkInfo);
  2940. }
  2941. }
  2942. })
  2943. .catch(function (error) {
  2944. oneTimeWarning(`An error occured during loading ${href.url}`);
  2945. dataSource._error.raiseEvent(dataSource, error);
  2946. });
  2947. deferredLoading.addPromise(promise);
  2948. }
  2949. }
  2950. }
  2951. function processFeatureNode(dataSource, node, processingData, deferredLoading) {
  2952. const featureProcessor = featureTypes[node.localName];
  2953. if (defined(featureProcessor)) {
  2954. return featureProcessor(dataSource, node, processingData, deferredLoading);
  2955. }
  2956. return processUnsupportedFeature(
  2957. dataSource,
  2958. node,
  2959. processingData,
  2960. deferredLoading
  2961. );
  2962. }
  2963. function loadKml(
  2964. dataSource,
  2965. entityCollection,
  2966. kml,
  2967. sourceResource,
  2968. uriResolver,
  2969. screenOverlayContainer,
  2970. context
  2971. ) {
  2972. entityCollection.removeAll();
  2973. const documentElement = kml.documentElement;
  2974. const document =
  2975. documentElement.localName === "Document"
  2976. ? documentElement
  2977. : queryFirstNode(documentElement, "Document", namespaces.kml);
  2978. let name = queryStringValue(document, "name", namespaces.kml);
  2979. if (!defined(name)) {
  2980. name = getFilenameFromUri(sourceResource.getUrlComponent());
  2981. }
  2982. // Only set the name from the root document
  2983. if (!defined(dataSource._name)) {
  2984. dataSource._name = name;
  2985. }
  2986. const deferredLoading = new KmlDataSource._DeferredLoading(dataSource);
  2987. const styleCollection = new EntityCollection(dataSource);
  2988. return Promise.all(
  2989. processStyles(
  2990. dataSource,
  2991. kml,
  2992. styleCollection,
  2993. sourceResource,
  2994. false,
  2995. uriResolver
  2996. )
  2997. ).then(function () {
  2998. let element = kml.documentElement;
  2999. if (element.localName === "kml") {
  3000. const childNodes = element.childNodes;
  3001. for (let i = 0; i < childNodes.length; i++) {
  3002. const tmp = childNodes[i];
  3003. if (defined(featureTypes[tmp.localName])) {
  3004. element = tmp;
  3005. break;
  3006. }
  3007. }
  3008. }
  3009. const processingData = {
  3010. parentEntity: undefined,
  3011. entityCollection: entityCollection,
  3012. styleCollection: styleCollection,
  3013. sourceResource: sourceResource,
  3014. uriResolver: uriResolver,
  3015. context: context,
  3016. screenOverlayContainer: screenOverlayContainer,
  3017. };
  3018. entityCollection.suspendEvents();
  3019. processFeatureNode(dataSource, element, processingData, deferredLoading);
  3020. entityCollection.resumeEvents();
  3021. return deferredLoading.wait().then(function () {
  3022. return kml.documentElement;
  3023. });
  3024. });
  3025. }
  3026. function loadKmz(
  3027. dataSource,
  3028. entityCollection,
  3029. blob,
  3030. sourceResource,
  3031. screenOverlayContainer
  3032. ) {
  3033. const zWorkerUrl = buildModuleUrl("ThirdParty/Workers/z-worker-pako.js");
  3034. zip.configure({
  3035. workerScripts: {
  3036. deflate: [zWorkerUrl, "./pako_deflate.min.js"],
  3037. inflate: [zWorkerUrl, "./pako_inflate.min.js"],
  3038. },
  3039. });
  3040. const reader = new zip.ZipReader(new zip.BlobReader(blob));
  3041. return Promise.resolve(reader.getEntries()).then(function (entries) {
  3042. const promises = [];
  3043. const uriResolver = {};
  3044. let docEntry;
  3045. for (let i = 0; i < entries.length; i++) {
  3046. const entry = entries[i];
  3047. if (!entry.directory) {
  3048. if (/\.kml$/i.test(entry.filename)) {
  3049. // We use the first KML document we come across
  3050. // https://developers.google.com/kml/documentation/kmzarchives
  3051. // Unless we come across a .kml file at the root of the archive because GE does this
  3052. if (!defined(docEntry) || !/\//i.test(entry.filename)) {
  3053. if (defined(docEntry)) {
  3054. // We found one at the root so load the initial kml as a data uri
  3055. promises.push(loadDataUriFromZip(docEntry, uriResolver));
  3056. }
  3057. docEntry = entry;
  3058. } else {
  3059. // Wasn't the first kml and wasn't at the root
  3060. promises.push(loadDataUriFromZip(entry, uriResolver));
  3061. }
  3062. } else {
  3063. promises.push(loadDataUriFromZip(entry, uriResolver));
  3064. }
  3065. }
  3066. }
  3067. // Now load the root KML document
  3068. if (defined(docEntry)) {
  3069. promises.push(loadXmlFromZip(docEntry, uriResolver));
  3070. }
  3071. return Promise.all(promises).then(function () {
  3072. reader.close();
  3073. if (!defined(uriResolver.kml)) {
  3074. throw new RuntimeError("KMZ file does not contain a KML document.");
  3075. }
  3076. uriResolver.keys = Object.keys(uriResolver);
  3077. return loadKml(
  3078. dataSource,
  3079. entityCollection,
  3080. uriResolver.kml,
  3081. sourceResource,
  3082. uriResolver,
  3083. screenOverlayContainer
  3084. );
  3085. });
  3086. });
  3087. }
  3088. function load(dataSource, entityCollection, data, options) {
  3089. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  3090. let sourceUri = options.sourceUri;
  3091. const uriResolver = options.uriResolver;
  3092. const context = options.context;
  3093. let screenOverlayContainer = options.screenOverlayContainer;
  3094. let promise = data;
  3095. if (typeof data === "string" || data instanceof Resource) {
  3096. data = Resource.createIfNeeded(data);
  3097. promise = data.fetchBlob();
  3098. sourceUri = defaultValue(sourceUri, data.clone());
  3099. // Add resource credits to our list of credits to display
  3100. const resourceCredits = dataSource._resourceCredits;
  3101. const credits = data.credits;
  3102. if (defined(credits)) {
  3103. const length = credits.length;
  3104. for (let i = 0; i < length; i++) {
  3105. resourceCredits.push(credits[i]);
  3106. }
  3107. }
  3108. } else {
  3109. sourceUri = defaultValue(sourceUri, Resource.DEFAULT.clone());
  3110. }
  3111. sourceUri = Resource.createIfNeeded(sourceUri);
  3112. if (defined(screenOverlayContainer)) {
  3113. screenOverlayContainer = getElement(screenOverlayContainer);
  3114. }
  3115. return Promise.resolve(promise)
  3116. .then(function (dataToLoad) {
  3117. if (dataToLoad instanceof Blob) {
  3118. return isZipFile(dataToLoad).then(function (isZip) {
  3119. if (isZip) {
  3120. return loadKmz(
  3121. dataSource,
  3122. entityCollection,
  3123. dataToLoad,
  3124. sourceUri,
  3125. screenOverlayContainer
  3126. );
  3127. }
  3128. return readBlobAsText(dataToLoad).then(function (text) {
  3129. //There's no official way to validate if a parse was successful.
  3130. //The following check detects the error on various browsers.
  3131. //Insert missing namespaces
  3132. text = insertNamespaces(text);
  3133. //Remove Duplicate Namespaces
  3134. text = removeDuplicateNamespaces(text);
  3135. //IE raises an exception
  3136. let kml;
  3137. let error;
  3138. try {
  3139. kml = parser.parseFromString(text, "application/xml");
  3140. } catch (e) {
  3141. error = e.toString();
  3142. }
  3143. //The parse succeeds on Chrome and Firefox, but the error
  3144. //handling is different in each.
  3145. if (
  3146. defined(error) ||
  3147. kml.body ||
  3148. kml.documentElement.tagName === "parsererror"
  3149. ) {
  3150. //Firefox has error information as the firstChild nodeValue.
  3151. let msg = defined(error)
  3152. ? error
  3153. : kml.documentElement.firstChild.nodeValue;
  3154. //Chrome has it in the body text.
  3155. if (!msg) {
  3156. msg = kml.body.innerText;
  3157. }
  3158. //Return the error
  3159. throw new RuntimeError(msg);
  3160. }
  3161. return loadKml(
  3162. dataSource,
  3163. entityCollection,
  3164. kml,
  3165. sourceUri,
  3166. uriResolver,
  3167. screenOverlayContainer,
  3168. context
  3169. );
  3170. });
  3171. });
  3172. }
  3173. return loadKml(
  3174. dataSource,
  3175. entityCollection,
  3176. dataToLoad,
  3177. sourceUri,
  3178. uriResolver,
  3179. screenOverlayContainer,
  3180. context
  3181. );
  3182. })
  3183. .catch(function (error) {
  3184. dataSource._error.raiseEvent(dataSource, error);
  3185. console.log(error);
  3186. return Promise.reject(error);
  3187. });
  3188. }
  3189. // NOTE: LoadOptions properties are repeated in ConstructorOptions because some
  3190. // tooling does not support "base types" for @typedef. Remove if/when
  3191. // https://github.com/microsoft/TypeScript/issues/20077 and/or
  3192. // https://github.com/jsdoc/jsdoc/issues/1199 actually get resolved
  3193. /**
  3194. * @typedef {object} KmlDataSource.LoadOptions
  3195. *
  3196. * Initialization options for the `load` method.
  3197. *
  3198. * @property {string} [sourceUri] Overrides the url to use for resolving relative links and other KML network features.
  3199. * @property {boolean} [clampToGround=false] true if we want the geometry features (Polygons, LineStrings and LinearRings) clamped to the ground.
  3200. * @property {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The global ellipsoid used for geographical calculations.
  3201. * @property {Element|string} [screenOverlayContainer] A container for ScreenOverlay images.
  3202. */
  3203. /**
  3204. * @typedef {object} KmlDataSource.ConstructorOptions
  3205. *
  3206. * Options for constructing a new KmlDataSource, or calling the static `load` method.
  3207. *
  3208. * @property {Camera} [camera] The camera that is used for viewRefreshModes and sending camera properties to network links.
  3209. * @property {HTMLCanvasElement} [canvas] The canvas that is used for sending viewer properties to network links.
  3210. * @property {Credit|string} [credit] A credit for the data source, which is displayed on the canvas.
  3211. *
  3212. * @property {string} [sourceUri] Overrides the url to use for resolving relative links and other KML network features.
  3213. * @property {boolean} [clampToGround=false] true if we want the geometry features (Polygons, LineStrings and LinearRings) clamped to the ground.
  3214. * @property {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The global ellipsoid used for geographical calculations.
  3215. * @property {Element|string} [screenOverlayContainer] A container for ScreenOverlay images.
  3216. */
  3217. /**
  3218. * A {@link DataSource} which processes Keyhole Markup Language 2.2 (KML).
  3219. * <p>
  3220. * KML support in Cesium is incomplete, but a large amount of the standard,
  3221. * as well as Google's <code>gx</code> extension namespace, is supported. See Github issue
  3222. * {@link https://github.com/CesiumGS/cesium/issues/873|#873} for a
  3223. * detailed list of what is and isn't supported. Cesium will also write information to the
  3224. * console when it encounters most unsupported features.
  3225. * </p>
  3226. * <p>
  3227. * Non visual feature data, such as <code>atom:author</code> and <code>ExtendedData</code>
  3228. * is exposed via an instance of {@link KmlFeatureData}, which is added to each {@link Entity}
  3229. * under the <code>kml</code> property.
  3230. * </p>
  3231. *
  3232. * @alias KmlDataSource
  3233. * @constructor
  3234. *
  3235. * @param {KmlDataSource.ConstructorOptions} [options] Object describing initialization options
  3236. *
  3237. * @see {@link http://www.opengeospatial.org/standards/kml/|Open Geospatial Consortium KML Standard}
  3238. * @see {@link https://developers.google.com/kml/|Google KML Documentation}
  3239. *
  3240. * @demo {@link https://sandcastle.cesium.com/index.html?src=KML.html|Cesium Sandcastle KML Demo}
  3241. *
  3242. * @example
  3243. * const viewer = new Cesium.Viewer('cesiumContainer');
  3244. * viewer.dataSources.add(Cesium.KmlDataSource.load('../../SampleData/facilities.kmz',
  3245. * {
  3246. * camera: viewer.scene.camera,
  3247. * canvas: viewer.scene.canvas
  3248. * })
  3249. * );
  3250. */
  3251. function KmlDataSource(options) {
  3252. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  3253. const camera = options.camera;
  3254. const canvas = options.canvas;
  3255. this._changed = new Event();
  3256. this._error = new Event();
  3257. this._loading = new Event();
  3258. this._refresh = new Event();
  3259. this._unsupportedNode = new Event();
  3260. this._clock = undefined;
  3261. this._entityCollection = new EntityCollection(this);
  3262. this._name = undefined;
  3263. this._isLoading = false;
  3264. this._pinBuilder = new PinBuilder();
  3265. this._networkLinks = new AssociativeArray();
  3266. this._entityCluster = new EntityCluster();
  3267. /**
  3268. * The current size of this Canvas will be used to populate the Link parameters
  3269. * for client height and width.
  3270. *
  3271. * @type {HTMLCanvasElement | undefined}
  3272. */
  3273. this.canvas = canvas;
  3274. /**
  3275. * The position and orientation of this {@link Camera} will be used to
  3276. * populate various camera parameters when making network requests.
  3277. * Camera movement will determine when to trigger NetworkLink refresh if
  3278. * <code>viewRefreshMode</code> is <code>onStop</code>.
  3279. *
  3280. * @type {Camera | undefined}
  3281. */
  3282. this.camera = camera;
  3283. this._lastCameraView = {
  3284. position: defined(camera) ? Cartesian3.clone(camera.positionWC) : undefined,
  3285. direction: defined(camera)
  3286. ? Cartesian3.clone(camera.directionWC)
  3287. : undefined,
  3288. up: defined(camera) ? Cartesian3.clone(camera.upWC) : undefined,
  3289. bbox: defined(camera)
  3290. ? camera.computeViewRectangle()
  3291. : Rectangle.clone(Rectangle.MAX_VALUE),
  3292. };
  3293. this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  3294. // User specified credit
  3295. let credit = options.credit;
  3296. if (typeof credit === "string") {
  3297. credit = new Credit(credit);
  3298. }
  3299. this._credit = credit;
  3300. // Create a list of Credit's from the resource that the user can't remove
  3301. this._resourceCredits = [];
  3302. this._kmlTours = [];
  3303. this._screenOverlays = [];
  3304. }
  3305. /**
  3306. * Creates a Promise to a new instance loaded with the provided KML data.
  3307. *
  3308. * @param {Resource|string|Document|Blob} data A url, parsed KML document, or Blob containing binary KMZ data or a parsed KML document.
  3309. * @param {KmlDataSource.ConstructorOptions} [options] An object specifying configuration options
  3310. *
  3311. * @returns {Promise<KmlDataSource>} A promise that will resolve to a new KmlDataSource instance once the KML is loaded.
  3312. */
  3313. KmlDataSource.load = function (data, options) {
  3314. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  3315. const dataSource = new KmlDataSource(options);
  3316. return dataSource.load(data, options);
  3317. };
  3318. Object.defineProperties(KmlDataSource.prototype, {
  3319. /**
  3320. * Gets or sets a human-readable name for this instance.
  3321. * This will be automatically be set to the KML document name on load.
  3322. * @memberof KmlDataSource.prototype
  3323. * @type {string}
  3324. */
  3325. name: {
  3326. get: function () {
  3327. return this._name;
  3328. },
  3329. set: function (value) {
  3330. if (this._name !== value) {
  3331. this._name = value;
  3332. this._changed.raiseEvent(this);
  3333. }
  3334. },
  3335. },
  3336. /**
  3337. * Gets the clock settings defined by the loaded KML. This represents the total
  3338. * availability interval for all time-dynamic data. If the KML does not contain
  3339. * time-dynamic data, this value is undefined.
  3340. * @memberof KmlDataSource.prototype
  3341. * @type {DataSourceClock}
  3342. */
  3343. clock: {
  3344. get: function () {
  3345. return this._clock;
  3346. },
  3347. },
  3348. /**
  3349. * Gets the collection of {@link Entity} instances.
  3350. * @memberof KmlDataSource.prototype
  3351. * @type {EntityCollection}
  3352. */
  3353. entities: {
  3354. get: function () {
  3355. return this._entityCollection;
  3356. },
  3357. },
  3358. /**
  3359. * Gets a value indicating if the data source is currently loading data.
  3360. * @memberof KmlDataSource.prototype
  3361. * @type {boolean}
  3362. */
  3363. isLoading: {
  3364. get: function () {
  3365. return this._isLoading;
  3366. },
  3367. },
  3368. /**
  3369. * Gets an event that will be raised when the underlying data changes.
  3370. * @memberof KmlDataSource.prototype
  3371. * @type {Event}
  3372. */
  3373. changedEvent: {
  3374. get: function () {
  3375. return this._changed;
  3376. },
  3377. },
  3378. /**
  3379. * Gets an event that will be raised if an error is encountered during processing.
  3380. * @memberof KmlDataSource.prototype
  3381. * @type {Event}
  3382. */
  3383. errorEvent: {
  3384. get: function () {
  3385. return this._error;
  3386. },
  3387. },
  3388. /**
  3389. * Gets an event that will be raised when the data source either starts or stops loading.
  3390. * @memberof KmlDataSource.prototype
  3391. * @type {Event}
  3392. */
  3393. loadingEvent: {
  3394. get: function () {
  3395. return this._loading;
  3396. },
  3397. },
  3398. /**
  3399. * Gets an event that will be raised when the data source refreshes a network link.
  3400. * @memberof KmlDataSource.prototype
  3401. * @type {Event}
  3402. */
  3403. refreshEvent: {
  3404. get: function () {
  3405. return this._refresh;
  3406. },
  3407. },
  3408. /**
  3409. * Gets an event that will be raised when the data source finds an unsupported node type.
  3410. * @memberof KmlDataSource.prototype
  3411. * @type {Event}
  3412. */
  3413. unsupportedNodeEvent: {
  3414. get: function () {
  3415. return this._unsupportedNode;
  3416. },
  3417. },
  3418. /**
  3419. * Gets whether or not this data source should be displayed.
  3420. * @memberof KmlDataSource.prototype
  3421. * @type {boolean}
  3422. */
  3423. show: {
  3424. get: function () {
  3425. return this._entityCollection.show;
  3426. },
  3427. set: function (value) {
  3428. this._entityCollection.show = value;
  3429. },
  3430. },
  3431. /**
  3432. * Gets or sets the clustering options for this data source. This object can be shared between multiple data sources.
  3433. *
  3434. * @memberof KmlDataSource.prototype
  3435. * @type {EntityCluster}
  3436. */
  3437. clustering: {
  3438. get: function () {
  3439. return this._entityCluster;
  3440. },
  3441. set: function (value) {
  3442. //>>includeStart('debug', pragmas.debug);
  3443. if (!defined(value)) {
  3444. throw new DeveloperError("value must be defined.");
  3445. }
  3446. //>>includeEnd('debug');
  3447. this._entityCluster = value;
  3448. },
  3449. },
  3450. /**
  3451. * Gets the credit that will be displayed for the data source
  3452. * @memberof KmlDataSource.prototype
  3453. * @type {Credit}
  3454. */
  3455. credit: {
  3456. get: function () {
  3457. return this._credit;
  3458. },
  3459. },
  3460. /**
  3461. * Gets the KML Tours that are used to guide the camera to specified destinations on given time intervals.
  3462. * @memberof KmlDataSource.prototype
  3463. * @type {KmlTour[]}
  3464. */
  3465. kmlTours: {
  3466. get: function () {
  3467. return this._kmlTours;
  3468. },
  3469. },
  3470. });
  3471. /**
  3472. * Asynchronously loads the provided KML data, replacing any existing data.
  3473. *
  3474. * @param {Resource|string|Document|Blob} data A url, parsed KML document, or Blob containing binary KMZ data or a parsed KML document.
  3475. * @param {KmlDataSource.LoadOptions} [options] An object specifying configuration options
  3476. *
  3477. * @returns {Promise<KmlDataSource>} A promise that will resolve to this instances once the KML is loaded.
  3478. */
  3479. KmlDataSource.prototype.load = function (data, options) {
  3480. //>>includeStart('debug', pragmas.debug);
  3481. if (!defined(data)) {
  3482. throw new DeveloperError("data is required.");
  3483. }
  3484. //>>includeEnd('debug');
  3485. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  3486. DataSource.setLoading(this, true);
  3487. const oldName = this._name;
  3488. this._name = undefined;
  3489. this._clampToGround = defaultValue(options.clampToGround, false);
  3490. const that = this;
  3491. return load(this, this._entityCollection, data, options)
  3492. .then(function () {
  3493. let clock;
  3494. const availability = that._entityCollection.computeAvailability();
  3495. let start = availability.start;
  3496. let stop = availability.stop;
  3497. const isMinStart = JulianDate.equals(start, Iso8601.MINIMUM_VALUE);
  3498. const isMaxStop = JulianDate.equals(stop, Iso8601.MAXIMUM_VALUE);
  3499. if (!isMinStart || !isMaxStop) {
  3500. let date;
  3501. //If start is min time just start at midnight this morning, local time
  3502. if (isMinStart) {
  3503. date = new Date();
  3504. date.setHours(0, 0, 0, 0);
  3505. start = JulianDate.fromDate(date);
  3506. }
  3507. //If stop is max value just stop at midnight tonight, local time
  3508. if (isMaxStop) {
  3509. date = new Date();
  3510. date.setHours(24, 0, 0, 0);
  3511. stop = JulianDate.fromDate(date);
  3512. }
  3513. clock = new DataSourceClock();
  3514. clock.startTime = start;
  3515. clock.stopTime = stop;
  3516. clock.currentTime = JulianDate.clone(start);
  3517. clock.clockRange = ClockRange.LOOP_STOP;
  3518. clock.clockStep = ClockStep.SYSTEM_CLOCK_MULTIPLIER;
  3519. clock.multiplier = Math.round(
  3520. Math.min(
  3521. Math.max(JulianDate.secondsDifference(stop, start) / 60, 1),
  3522. 3.15569e7
  3523. )
  3524. );
  3525. }
  3526. let changed = false;
  3527. if (clock !== that._clock) {
  3528. that._clock = clock;
  3529. changed = true;
  3530. }
  3531. if (oldName !== that._name) {
  3532. changed = true;
  3533. }
  3534. if (changed) {
  3535. that._changed.raiseEvent(that);
  3536. }
  3537. DataSource.setLoading(that, false);
  3538. return that;
  3539. })
  3540. .catch(function (error) {
  3541. DataSource.setLoading(that, false);
  3542. that._error.raiseEvent(that, error);
  3543. console.log(error);
  3544. return Promise.reject(error);
  3545. });
  3546. };
  3547. /**
  3548. * Cleans up any non-entity elements created by the data source. Currently this only affects ScreenOverlay elements.
  3549. */
  3550. KmlDataSource.prototype.destroy = function () {
  3551. while (this._screenOverlays.length > 0) {
  3552. const elem = this._screenOverlays.pop();
  3553. elem.remove();
  3554. }
  3555. };
  3556. function mergeAvailabilityWithParent(child) {
  3557. const parent = child.parent;
  3558. if (defined(parent)) {
  3559. const parentAvailability = parent.availability;
  3560. if (defined(parentAvailability)) {
  3561. const childAvailability = child.availability;
  3562. if (defined(childAvailability)) {
  3563. childAvailability.intersect(parentAvailability);
  3564. } else {
  3565. child.availability = parentAvailability;
  3566. }
  3567. }
  3568. }
  3569. }
  3570. function getNetworkLinkUpdateCallback(
  3571. dataSource,
  3572. networkLink,
  3573. newEntityCollection,
  3574. networkLinks,
  3575. processedHref
  3576. ) {
  3577. return function (rootElement) {
  3578. if (!networkLinks.contains(networkLink.id)) {
  3579. // Got into the odd case where a parent network link was updated while a child
  3580. // network link update was in flight, so just throw it away.
  3581. return;
  3582. }
  3583. let remove = false;
  3584. const networkLinkControl = queryFirstNode(
  3585. rootElement,
  3586. "NetworkLinkControl",
  3587. namespaces.kml
  3588. );
  3589. const hasNetworkLinkControl = defined(networkLinkControl);
  3590. let minRefreshPeriod = 0;
  3591. if (hasNetworkLinkControl) {
  3592. if (
  3593. defined(queryFirstNode(networkLinkControl, "Update", namespaces.kml))
  3594. ) {
  3595. oneTimeWarning(
  3596. "kml-networkLinkControl-update",
  3597. "KML - NetworkLinkControl updates aren't supported."
  3598. );
  3599. networkLink.updating = false;
  3600. networkLinks.remove(networkLink.id);
  3601. return;
  3602. }
  3603. networkLink.cookie = queryToObject(
  3604. defaultValue(
  3605. queryStringValue(networkLinkControl, "cookie", namespaces.kml),
  3606. ""
  3607. )
  3608. );
  3609. minRefreshPeriod = defaultValue(
  3610. queryNumericValue(
  3611. networkLinkControl,
  3612. "minRefreshPeriod",
  3613. namespaces.kml
  3614. ),
  3615. 0
  3616. );
  3617. }
  3618. const now = JulianDate.now();
  3619. const refreshMode = networkLink.refreshMode;
  3620. if (refreshMode === RefreshMode.INTERVAL) {
  3621. if (defined(networkLinkControl)) {
  3622. networkLink.time = Math.max(minRefreshPeriod, networkLink.time);
  3623. }
  3624. } else if (refreshMode === RefreshMode.EXPIRE) {
  3625. let expires;
  3626. if (defined(networkLinkControl)) {
  3627. expires = queryStringValue(
  3628. networkLinkControl,
  3629. "expires",
  3630. namespaces.kml
  3631. );
  3632. }
  3633. if (defined(expires)) {
  3634. try {
  3635. const date = JulianDate.fromIso8601(expires);
  3636. const diff = JulianDate.secondsDifference(date, now);
  3637. if (diff > 0 && diff < minRefreshPeriod) {
  3638. JulianDate.addSeconds(now, minRefreshPeriod, date);
  3639. }
  3640. networkLink.time = date;
  3641. } catch (e) {
  3642. oneTimeWarning(
  3643. "kml-networkLinkControl-expires",
  3644. "KML - NetworkLinkControl expires is not a valid date"
  3645. );
  3646. remove = true;
  3647. }
  3648. } else {
  3649. oneTimeWarning(
  3650. "kml-refreshMode-onExpire",
  3651. "KML - refreshMode of onExpire requires the NetworkLinkControl to have an expires element"
  3652. );
  3653. remove = true;
  3654. }
  3655. }
  3656. const networkLinkEntity = networkLink.entity;
  3657. const entityCollection = dataSource._entityCollection;
  3658. const newEntities = newEntityCollection.values;
  3659. function removeChildren(entity) {
  3660. entityCollection.remove(entity);
  3661. const children = entity._children;
  3662. const count = children.length;
  3663. for (let i = 0; i < count; ++i) {
  3664. removeChildren(children[i]);
  3665. }
  3666. }
  3667. // Remove old entities
  3668. entityCollection.suspendEvents();
  3669. const entitiesCopy = entityCollection.values.slice();
  3670. let i;
  3671. for (i = 0; i < entitiesCopy.length; ++i) {
  3672. const entityToRemove = entitiesCopy[i];
  3673. if (entityToRemove.parent === networkLinkEntity) {
  3674. entityToRemove.parent = undefined;
  3675. removeChildren(entityToRemove);
  3676. }
  3677. }
  3678. entityCollection.resumeEvents();
  3679. // Add new entities
  3680. entityCollection.suspendEvents();
  3681. for (i = 0; i < newEntities.length; i++) {
  3682. const newEntity = newEntities[i];
  3683. if (!defined(newEntity.parent)) {
  3684. newEntity.parent = networkLinkEntity;
  3685. mergeAvailabilityWithParent(newEntity);
  3686. }
  3687. entityCollection.add(newEntity);
  3688. }
  3689. entityCollection.resumeEvents();
  3690. // No refresh information remove it, otherwise update lastUpdate time
  3691. if (remove) {
  3692. networkLinks.remove(networkLink.id);
  3693. } else {
  3694. networkLink.lastUpdated = now;
  3695. }
  3696. const availability = entityCollection.computeAvailability();
  3697. const start = availability.start;
  3698. const stop = availability.stop;
  3699. const isMinStart = JulianDate.equals(start, Iso8601.MINIMUM_VALUE);
  3700. const isMaxStop = JulianDate.equals(stop, Iso8601.MAXIMUM_VALUE);
  3701. if (!isMinStart || !isMaxStop) {
  3702. const clock = dataSource._clock;
  3703. if (clock.startTime !== start || clock.stopTime !== stop) {
  3704. clock.startTime = start;
  3705. clock.stopTime = stop;
  3706. dataSource._changed.raiseEvent(dataSource);
  3707. }
  3708. }
  3709. networkLink.updating = false;
  3710. networkLink.needsUpdate = false;
  3711. dataSource._refresh.raiseEvent(
  3712. dataSource,
  3713. processedHref.getUrlComponent(true)
  3714. );
  3715. };
  3716. }
  3717. const entitiesToIgnore = new AssociativeArray();
  3718. /**
  3719. * Updates any NetworkLink that require updating.
  3720. *
  3721. * @param {JulianDate} time The simulation time.
  3722. * @returns {boolean} True if this data source is ready to be displayed at the provided time, false otherwise.
  3723. */
  3724. KmlDataSource.prototype.update = function (time) {
  3725. const networkLinks = this._networkLinks;
  3726. if (networkLinks.length === 0) {
  3727. return true;
  3728. }
  3729. const now = JulianDate.now();
  3730. const that = this;
  3731. entitiesToIgnore.removeAll();
  3732. function recurseIgnoreEntities(entity) {
  3733. const children = entity._children;
  3734. const count = children.length;
  3735. for (let i = 0; i < count; ++i) {
  3736. const child = children[i];
  3737. entitiesToIgnore.set(child.id, child);
  3738. recurseIgnoreEntities(child);
  3739. }
  3740. }
  3741. let cameraViewUpdate = false;
  3742. const lastCameraView = this._lastCameraView;
  3743. const camera = this.camera;
  3744. if (
  3745. defined(camera) &&
  3746. !(
  3747. camera.positionWC.equalsEpsilon(
  3748. lastCameraView.position,
  3749. CesiumMath.EPSILON7
  3750. ) &&
  3751. camera.directionWC.equalsEpsilon(
  3752. lastCameraView.direction,
  3753. CesiumMath.EPSILON7
  3754. ) &&
  3755. camera.upWC.equalsEpsilon(lastCameraView.up, CesiumMath.EPSILON7)
  3756. )
  3757. ) {
  3758. // Camera has changed so update the last view
  3759. lastCameraView.position = Cartesian3.clone(camera.positionWC);
  3760. lastCameraView.direction = Cartesian3.clone(camera.directionWC);
  3761. lastCameraView.up = Cartesian3.clone(camera.upWC);
  3762. lastCameraView.bbox = camera.computeViewRectangle();
  3763. cameraViewUpdate = true;
  3764. }
  3765. const newNetworkLinks = new AssociativeArray();
  3766. let changed = false;
  3767. networkLinks.values.forEach(function (networkLink) {
  3768. const entity = networkLink.entity;
  3769. if (entitiesToIgnore.contains(entity.id)) {
  3770. return;
  3771. }
  3772. if (!networkLink.updating) {
  3773. let doUpdate = false;
  3774. if (networkLink.refreshMode === RefreshMode.INTERVAL) {
  3775. if (
  3776. JulianDate.secondsDifference(now, networkLink.lastUpdated) >
  3777. networkLink.time
  3778. ) {
  3779. doUpdate = true;
  3780. }
  3781. } else if (networkLink.refreshMode === RefreshMode.EXPIRE) {
  3782. if (JulianDate.greaterThan(now, networkLink.time)) {
  3783. doUpdate = true;
  3784. }
  3785. } else if (networkLink.refreshMode === RefreshMode.STOP) {
  3786. if (cameraViewUpdate) {
  3787. networkLink.needsUpdate = true;
  3788. networkLink.cameraUpdateTime = now;
  3789. }
  3790. if (
  3791. networkLink.needsUpdate &&
  3792. JulianDate.secondsDifference(now, networkLink.cameraUpdateTime) >=
  3793. networkLink.time
  3794. ) {
  3795. doUpdate = true;
  3796. }
  3797. }
  3798. if (doUpdate) {
  3799. recurseIgnoreEntities(entity);
  3800. networkLink.updating = true;
  3801. const newEntityCollection = new EntityCollection();
  3802. const href = networkLink.href.clone();
  3803. href.setQueryParameters(networkLink.cookie);
  3804. const ellipsoid = defaultValue(that._ellipsoid, Ellipsoid.WGS84);
  3805. processNetworkLinkQueryString(
  3806. href,
  3807. that.camera,
  3808. that.canvas,
  3809. networkLink.viewBoundScale,
  3810. lastCameraView.bbox,
  3811. ellipsoid
  3812. );
  3813. load(that, newEntityCollection, href, {
  3814. context: entity.id,
  3815. })
  3816. .then(
  3817. getNetworkLinkUpdateCallback(
  3818. that,
  3819. networkLink,
  3820. newEntityCollection,
  3821. newNetworkLinks,
  3822. href
  3823. )
  3824. )
  3825. .catch(function (error) {
  3826. const msg = `NetworkLink ${networkLink.href} refresh failed: ${error}`;
  3827. console.log(msg);
  3828. that._error.raiseEvent(that, msg);
  3829. });
  3830. changed = true;
  3831. }
  3832. }
  3833. newNetworkLinks.set(networkLink.id, networkLink);
  3834. });
  3835. if (changed) {
  3836. this._networkLinks = newNetworkLinks;
  3837. this._changed.raiseEvent(this);
  3838. }
  3839. return true;
  3840. };
  3841. /**
  3842. * Contains KML Feature data loaded into the <code>Entity.kml</code> property by {@link KmlDataSource}.
  3843. * @alias KmlFeatureData
  3844. * @constructor
  3845. */
  3846. function KmlFeatureData() {
  3847. /**
  3848. * @typedef KmlFeatureData.Author
  3849. * @type {object}
  3850. * @property {string} name Gets the name.
  3851. * @property {string} uri Gets the URI.
  3852. * @property {number} age Gets the email.
  3853. */
  3854. /**
  3855. * Gets the atom syndication format author field.
  3856. * @type {KmlFeatureData.Author}
  3857. */
  3858. this.author = {
  3859. name: undefined,
  3860. uri: undefined,
  3861. email: undefined,
  3862. };
  3863. /**
  3864. * @typedef KmlFeatureData.Link
  3865. * @type {object}
  3866. * @property {string} href Gets the href.
  3867. * @property {string} hreflang Gets the language of the linked resource.
  3868. * @property {string} rel Gets the link relation.
  3869. * @property {string} type Gets the link type.
  3870. * @property {string} title Gets the link title.
  3871. * @property {string} length Gets the link length.
  3872. */
  3873. /**
  3874. * Gets the link.
  3875. * @type {KmlFeatureData.Link}
  3876. */
  3877. this.link = {
  3878. href: undefined,
  3879. hreflang: undefined,
  3880. rel: undefined,
  3881. type: undefined,
  3882. title: undefined,
  3883. length: undefined,
  3884. };
  3885. /**
  3886. * Gets the unstructured address field.
  3887. * @type {string}
  3888. */
  3889. this.address = undefined;
  3890. /**
  3891. * Gets the phone number.
  3892. * @type {string}
  3893. */
  3894. this.phoneNumber = undefined;
  3895. /**
  3896. * Gets the snippet.
  3897. * @type {string}
  3898. */
  3899. this.snippet = undefined;
  3900. /**
  3901. * Gets the extended data, parsed into a JSON object.
  3902. * Currently only the <code>Data</code> property is supported.
  3903. * <code>SchemaData</code> and custom data are ignored.
  3904. * @type {string}
  3905. */
  3906. this.extendedData = undefined;
  3907. }
  3908. // For testing
  3909. KmlDataSource._DeferredLoading = DeferredLoading;
  3910. KmlDataSource._getTimestamp = getTimestamp;
  3911. export default KmlDataSource;