KmlDataSource.js 126 KB

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