智慧水务管理系统 - 精河县供水工程综合管理平台

KmlDataSource.js 125KB

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