jsts.js 934 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200
  1. /**
  2. * JSTS. See https://github.com/bjornharrtell/jsts
  3. * https://github.com/bjornharrtell/jsts/blob/master/LICENSE_EDLv1.txt
  4. * https://github.com/bjornharrtell/jsts/blob/master/LICENSE_EPLv1.txt
  5. * @license
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  9. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  10. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jsts = {}));
  11. }(this, (function (exports) { 'use strict';
  12. class NumberUtil {
  13. static equalsWithTolerance(x1, x2, tolerance) {
  14. return Math.abs(x1 - x2) <= tolerance;
  15. }
  16. }
  17. class Exception extends Error {
  18. constructor(message) {
  19. super(message);
  20. this.name = Object.keys({
  21. Exception
  22. })[0];
  23. }
  24. toString() {
  25. return this.message;
  26. }
  27. }
  28. class IllegalArgumentException extends Exception {
  29. constructor(message) {
  30. super(message);
  31. this.name = Object.keys({
  32. IllegalArgumentException
  33. })[0];
  34. }
  35. }
  36. class Long {
  37. constructor(high, low) {
  38. this.low = low || 0;
  39. this.high = high || 0;
  40. }
  41. static toBinaryString(i) {
  42. let mask;
  43. let result = '';
  44. for (mask = 0x80000000; mask > 0; mask >>>= 1) result += (i.high & mask) === mask ? '1' : '0';
  45. for (mask = 0x80000000; mask > 0; mask >>>= 1) result += (i.low & mask) === mask ? '1' : '0';
  46. return result;
  47. }
  48. }
  49. function Double() {}
  50. Double.NaN = NaN;
  51. Double.isNaN = n => Number.isNaN(n);
  52. Double.isInfinite = n => !Number.isFinite(n);
  53. Double.MAX_VALUE = Number.MAX_VALUE;
  54. Double.POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
  55. Double.NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
  56. if (typeof Float64Array === 'function' && typeof Int32Array === 'function') // Simple and fast conversion between double and long bits
  57. // using TypedArrays and ArrayViewBuffers.
  58. (function () {
  59. const EXP_BIT_MASK = 0x7ff00000;
  60. const SIGNIF_BIT_MASK = 0xFFFFF;
  61. const f64buf = new Float64Array(1);
  62. const i32buf = new Int32Array(f64buf.buffer);
  63. Double.doubleToLongBits = function (value) {
  64. f64buf[0] = value;
  65. let low = i32buf[0] | 0;
  66. let high = i32buf[1] | 0; // Check for NaN based on values of bit fields, maximum
  67. // exponent and nonzero significand.
  68. if ((high & EXP_BIT_MASK) === EXP_BIT_MASK && (high & SIGNIF_BIT_MASK) !== 0 && low !== 0) {
  69. low = 0 | 0;
  70. high = 0x7ff80000 | 0;
  71. }
  72. return new Long(high, low);
  73. };
  74. Double.longBitsToDouble = function (bits) {
  75. i32buf[0] = bits.low;
  76. i32buf[1] = bits.high;
  77. return f64buf[0];
  78. };
  79. })();else // More complex and slower fallback implementation using
  80. // math and the divide-by-two and multiply-by-two algorithms.
  81. (function () {
  82. const BIAS = 1023;
  83. const log2 = Math.log2;
  84. const floor = Math.floor;
  85. const pow = Math.pow;
  86. const MAX_REL_BITS_INTEGER = function () {
  87. for (let i = 53; i > 0; i--) {
  88. const bits = pow(2, i) - 1;
  89. if (floor(log2(bits)) + 1 === i) return bits;
  90. }
  91. return 0;
  92. }();
  93. Double.doubleToLongBits = function (value) {
  94. let x, y, f, bits, skip;
  95. let sign, exp, high, low; // Get the sign bit and absolute value.
  96. if (value < 0 || 1 / value === Number.NEGATIVE_INFINITY) {
  97. sign = 1 << 31;
  98. value = -value;
  99. } else {
  100. sign = 0;
  101. } // Handle some special values.
  102. if (value === 0) {
  103. // Handle zeros (+/-0).
  104. low = 0 | 0;
  105. high = sign; // exponent: 00..00, significand: 00..00
  106. return new Long(high, low);
  107. }
  108. if (value === Infinity) {
  109. // Handle infinity (only positive values for value possible).
  110. low = 0 | 0;
  111. high = sign | 0x7ff00000; // exponent: 11..11, significand: 00..00
  112. return new Long(high, low);
  113. }
  114. if (value !== value) {
  115. // eslint-disable-line
  116. // Handle NaNs (boiled down to only one distinct NaN).
  117. low = 0 | 0;
  118. high = 0x7ff80000; // exponent: 11..11, significand: 10..00
  119. return new Long(high, low);
  120. } // Preinitialize variables, that are not neccessarily set by
  121. // the algorithm.
  122. bits = 0;
  123. low = 0 | 0; // Get the (always positive) integer part of value.
  124. x = floor(value); // Process the integer part if it's greater than 1. Zero requires
  125. // no bits at all, 1 represents the implicit (hidden) leading bit,
  126. // which must not be written as well.
  127. if (x > 1) // If we can reliably determine the number of bits required for
  128. // the integer part,
  129. if (x <= MAX_REL_BITS_INTEGER) {
  130. // get the number of bits required to represent it minus 1
  131. bits = floor(log2(x));
  132. /* + 1 - 1 */
  133. // and simply copy/shift the integer bits into low and high.
  134. // That's much faster than the divide-by-two algorithm (saves
  135. // up to ~60%).
  136. // We always need to mask out the most significant bit, which
  137. // is the implicit (aka hidden) bit.
  138. if (bits <= 20) {
  139. // The simple case in which the integer fits into the
  140. // lower 20 bits of the high word is worth to be handled
  141. // separately (saves ~25%).
  142. low = 0 | 0;
  143. high = x << 20 - bits & 0xfffff;
  144. } else {
  145. // Here, the integer part is split into low and high.
  146. // Since its value may require more than 32 bits, we
  147. // cannot use bitwise operators (which implicitly cast
  148. // to Int32), but use arithmetic operators % and / to
  149. // get low and high parts. The uppper 20 bits go to high,
  150. // the remaining bits (in f) to low.
  151. f = bits - 20; // Like (1 << f) but safe with even more than 32 bits.
  152. y = pow(2, f);
  153. low = x % y << 32 - f;
  154. high = x / y & 0xfffff;
  155. }
  156. } else {
  157. // For greater values, we must use the much slower divide-by-two
  158. // algorithm. Bits are generated from right to left, that is from
  159. // least to most significant bit. For each bit, we left-shift both
  160. // low and high by one and carry bit #0 from high to #31 in low.
  161. // The next bit is then copied into bit #19 in high, the leftmost
  162. // bit of the double's significand.
  163. // Preserve x for later user, so work with f.
  164. f = x;
  165. low = 0 | 0;
  166. for (;;) {
  167. y = f / 2;
  168. f = floor(y);
  169. if (f === 0) // We just found the most signigicant (1-)bit, which
  170. // is the implicit bit and so, not stored in the double
  171. // value. So, it's time to leave the loop.
  172. break; // Count this bit, shift low and carry bit #0 from high.
  173. bits++;
  174. low >>>= 1;
  175. low |= (high & 0x1) << 31; // Shift high.
  176. high >>>= 1;
  177. if (y !== f) // Copy the new bit into bit #19 in high (only required if 1).
  178. high |= 0x80000;
  179. }
  180. } // Bias the exponent.
  181. exp = bits + BIAS; // If the integer part is zero, we've not yet seen the implicit
  182. // leading bit. Variable skip is later used while processing the
  183. // fractional part (if any).
  184. skip = x === 0; // Get fraction only into x.
  185. x = value - x; // If some significand bits are still left to be filled and
  186. // the fractional part is not zero, convert the fraction using
  187. // the multiply-by-2 algorithm.
  188. if (bits < 52 && x !== 0) {
  189. // Initialize 'buffer' f, into which newly created bits get
  190. // shifted from right to left.
  191. f = 0;
  192. for (;;) {
  193. y = x * 2;
  194. if (y >= 1) {
  195. // This is a new 1-bit. Add and count this bit, if not
  196. // prohibited by skip.
  197. x = y - 1;
  198. if (!skip) {
  199. f <<= 1;
  200. f |= 1;
  201. bits++;
  202. } else {
  203. // Otherwise, decrement the exponent and unset
  204. // skip, so that all following bits get written.
  205. exp--;
  206. skip = false;
  207. }
  208. } else {
  209. // This is a new 0-bit. Add and count this bit, if not
  210. // prohibited by skip.
  211. x = y;
  212. if (!skip) {
  213. f <<= 1;
  214. bits++;
  215. } else if (--exp === 0) {
  216. // Otherwise we've just decremented the exponent. If the
  217. // biased exponent is zero now (-1023), we process a
  218. // subnormal number, which has no impled leading 1-bit.
  219. // So, count this 0-bit and unset skip to write out
  220. // all the following bits.
  221. bits++;
  222. skip = false;
  223. }
  224. }
  225. if (bits === 20) {
  226. // When 20 bits have been created in total, we're done with
  227. // the high word. Copy the bits from 'buffer' f into high
  228. // and reset 'buffer' f. Following bits will end up in the
  229. // low word.
  230. high |= f;
  231. f = 0;
  232. } else if (bits === 52) {
  233. // When 52 bits have been created in total, we're done with
  234. // low word as well. Copy the bits from 'buffer' f into low
  235. // and exit the loop.
  236. low |= f;
  237. break;
  238. }
  239. if (y === 1) {
  240. // When y is exactly 1, there is no remainder and the process
  241. // is complete (the number is finite). Copy the bits from
  242. // 'buffer' f into either low or high and exit the loop.
  243. if (bits < 20) high |= f << 20 - bits;else if (bits < 52) low |= f << 52 - bits;
  244. break;
  245. }
  246. }
  247. } // Copy/shift the exponent and sign bits into the high word.
  248. high |= exp << 20;
  249. high |= sign;
  250. return new Long(high, low);
  251. };
  252. Double.longBitsToDouble = function (bits) {
  253. let i;
  254. let x, exp, fract;
  255. const high = bits.high;
  256. const low = bits.low; // Extract the sign.
  257. const sign = high & 1 << 31 ? -1 : 1; // Extract the unbiased exponent.
  258. exp = ((high & 0x7ff00000) >> 20) - BIAS; // Calculate the fraction from left to right. Start
  259. // off with the 20 lower bits from the high word.
  260. fract = 0;
  261. x = 1 << 19;
  262. for (i = 1; i <= 20; i++) {
  263. if (high & x) fract += pow(2, -i);
  264. x >>>= 1;
  265. } // Continue with all 32 bits from the low word.
  266. x = 1 << 31;
  267. for (i = 21; i <= 52; i++) {
  268. if (low & x) fract += pow(2, -i);
  269. x >>>= 1;
  270. } // Handle special values.
  271. // Check for zero and subnormal values.
  272. if (exp === -BIAS) {
  273. if (fract === 0) // +/-1.0 * 0.0 => +/-0.0
  274. return sign * 0;
  275. exp = -1022;
  276. } else if (exp === BIAS + 1) {
  277. // Check for +/-Infinity or NaN.
  278. if (fract === 0) // +/-1.0 / 0.0 => +/-Infinity
  279. return sign / 0;
  280. return NaN;
  281. } else {
  282. // Nothing special? Seems to be a normal number.
  283. // Add the implicit leading bit (1*2^0).
  284. fract += 1;
  285. }
  286. return sign * fract * pow(2, exp);
  287. };
  288. })();
  289. function Comparable() {}
  290. function Clonable() {}
  291. function Comparator() {}
  292. function Serializable() {}
  293. class RuntimeException extends Exception {
  294. constructor(message) {
  295. super(message);
  296. this.name = Object.keys({
  297. RuntimeException
  298. })[0];
  299. }
  300. }
  301. class AssertionFailedException extends RuntimeException {
  302. constructor() {
  303. super();
  304. AssertionFailedException.constructor_.apply(this, arguments);
  305. }
  306. static constructor_() {
  307. if (arguments.length === 0) {
  308. RuntimeException.constructor_.call(this);
  309. } else if (arguments.length === 1) {
  310. const message = arguments[0];
  311. RuntimeException.constructor_.call(this, message);
  312. }
  313. }
  314. }
  315. class Assert {
  316. static shouldNeverReachHere() {
  317. if (arguments.length === 0) {
  318. Assert.shouldNeverReachHere(null);
  319. } else if (arguments.length === 1) {
  320. const message = arguments[0];
  321. throw new AssertionFailedException('Should never reach here' + (message !== null ? ': ' + message : ''));
  322. }
  323. }
  324. static isTrue() {
  325. if (arguments.length === 1) {
  326. const assertion = arguments[0];
  327. Assert.isTrue(assertion, null);
  328. } else if (arguments.length === 2) {
  329. const assertion = arguments[0],
  330. message = arguments[1];
  331. if (!assertion) if (message === null) throw new AssertionFailedException();else throw new AssertionFailedException(message);
  332. }
  333. }
  334. static equals() {
  335. if (arguments.length === 2) {
  336. const expectedValue = arguments[0],
  337. actualValue = arguments[1];
  338. Assert.equals(expectedValue, actualValue, null);
  339. } else if (arguments.length === 3) {
  340. const expectedValue = arguments[0],
  341. actualValue = arguments[1],
  342. message = arguments[2];
  343. if (!actualValue.equals(expectedValue)) throw new AssertionFailedException('Expected ' + expectedValue + ' but encountered ' + actualValue + (message !== null ? ': ' + message : ''));
  344. }
  345. }
  346. }
  347. const kBuf = new ArrayBuffer(8);
  348. const kBufAsF64 = new Float64Array(kBuf);
  349. const kBufAsI32 = new Int32Array(kBuf);
  350. class Coordinate {
  351. constructor() {
  352. Coordinate.constructor_.apply(this, arguments);
  353. }
  354. static constructor_() {
  355. this.x = null;
  356. this.y = null;
  357. this.z = null;
  358. if (arguments.length === 0) {
  359. Coordinate.constructor_.call(this, 0.0, 0.0);
  360. } else if (arguments.length === 1) {
  361. const c = arguments[0];
  362. Coordinate.constructor_.call(this, c.x, c.y, c.getZ());
  363. } else if (arguments.length === 2) {
  364. const x = arguments[0],
  365. y = arguments[1];
  366. Coordinate.constructor_.call(this, x, y, Coordinate.NULL_ORDINATE);
  367. } else if (arguments.length === 3) {
  368. const x = arguments[0],
  369. y = arguments[1],
  370. z = arguments[2];
  371. this.x = x;
  372. this.y = y;
  373. this.z = z;
  374. }
  375. }
  376. static hashCode(n) {
  377. kBufAsF64[0] = n;
  378. return kBufAsI32[0] ^ kBufAsI32[1];
  379. }
  380. getM() {
  381. return Double.NaN;
  382. }
  383. setOrdinate(ordinateIndex, value) {
  384. switch (ordinateIndex) {
  385. case Coordinate.X:
  386. this.x = value;
  387. break;
  388. case Coordinate.Y:
  389. this.y = value;
  390. break;
  391. case Coordinate.Z:
  392. this.setZ(value);
  393. break;
  394. default:
  395. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  396. }
  397. }
  398. equals2D() {
  399. if (arguments.length === 1) {
  400. const other = arguments[0];
  401. if (this.x !== other.x) return false;
  402. if (this.y !== other.y) return false;
  403. return true;
  404. } else if (arguments.length === 2) {
  405. const c = arguments[0],
  406. tolerance = arguments[1];
  407. if (!NumberUtil.equalsWithTolerance(this.x, c.x, tolerance)) return false;
  408. if (!NumberUtil.equalsWithTolerance(this.y, c.y, tolerance)) return false;
  409. return true;
  410. }
  411. }
  412. setM(m) {
  413. throw new IllegalArgumentException('Invalid ordinate index: ' + Coordinate.M);
  414. }
  415. getZ() {
  416. return this.z;
  417. }
  418. getOrdinate(ordinateIndex) {
  419. switch (ordinateIndex) {
  420. case Coordinate.X:
  421. return this.x;
  422. case Coordinate.Y:
  423. return this.y;
  424. case Coordinate.Z:
  425. return this.getZ();
  426. }
  427. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  428. }
  429. equals3D(other) {
  430. return this.x === other.x && this.y === other.y && (this.getZ() === other.getZ() || Double.isNaN(this.getZ()) && Double.isNaN(other.getZ()));
  431. }
  432. equals(other) {
  433. if (!(other instanceof Coordinate)) return false;
  434. return this.equals2D(other);
  435. }
  436. equalInZ(c, tolerance) {
  437. return NumberUtil.equalsWithTolerance(this.getZ(), c.getZ(), tolerance);
  438. }
  439. setX(x) {
  440. this.x = x;
  441. }
  442. compareTo(o) {
  443. const other = o;
  444. if (this.x < other.x) return -1;
  445. if (this.x > other.x) return 1;
  446. if (this.y < other.y) return -1;
  447. if (this.y > other.y) return 1;
  448. return 0;
  449. }
  450. getX() {
  451. return this.x;
  452. }
  453. setZ(z) {
  454. this.z = z;
  455. }
  456. clone() {
  457. try {
  458. const coord = null;
  459. return coord;
  460. } catch (e) {
  461. if (e instanceof CloneNotSupportedException) {
  462. Assert.shouldNeverReachHere('this shouldn\'t happen because this class is Cloneable');
  463. return null;
  464. } else {
  465. throw e;
  466. }
  467. } finally {}
  468. }
  469. copy() {
  470. return new Coordinate(this);
  471. }
  472. toString() {
  473. return '(' + this.x + ', ' + this.y + ', ' + this.getZ() + ')';
  474. }
  475. distance3D(c) {
  476. const dx = this.x - c.x;
  477. const dy = this.y - c.y;
  478. const dz = this.getZ() - c.getZ();
  479. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  480. }
  481. getY() {
  482. return this.y;
  483. }
  484. setY(y) {
  485. this.y = y;
  486. }
  487. distance(c) {
  488. const dx = this.x - c.x;
  489. const dy = this.y - c.y;
  490. return Math.sqrt(dx * dx + dy * dy);
  491. }
  492. hashCode() {
  493. let result = 17;
  494. result = 37 * result + Coordinate.hashCode(this.x);
  495. result = 37 * result + Coordinate.hashCode(this.y);
  496. return result;
  497. }
  498. setCoordinate(other) {
  499. this.x = other.x;
  500. this.y = other.y;
  501. this.z = other.getZ();
  502. }
  503. get interfaces_() {
  504. return [Comparable, Clonable, Serializable];
  505. }
  506. }
  507. class DimensionalComparator {
  508. constructor() {
  509. DimensionalComparator.constructor_.apply(this, arguments);
  510. }
  511. static constructor_() {
  512. this._dimensionsToTest = 2;
  513. if (arguments.length === 0) {
  514. DimensionalComparator.constructor_.call(this, 2);
  515. } else if (arguments.length === 1) {
  516. const dimensionsToTest = arguments[0];
  517. if (dimensionsToTest !== 2 && dimensionsToTest !== 3) throw new IllegalArgumentException('only 2 or 3 dimensions may be specified');
  518. this._dimensionsToTest = dimensionsToTest;
  519. }
  520. }
  521. static compare(a, b) {
  522. if (a < b) return -1;
  523. if (a > b) return 1;
  524. if (Double.isNaN(a)) {
  525. if (Double.isNaN(b)) return 0;
  526. return -1;
  527. }
  528. if (Double.isNaN(b)) return 1;
  529. return 0;
  530. }
  531. compare(c1, c2) {
  532. const compX = DimensionalComparator.compare(c1.x, c2.x);
  533. if (compX !== 0) return compX;
  534. const compY = DimensionalComparator.compare(c1.y, c2.y);
  535. if (compY !== 0) return compY;
  536. if (this._dimensionsToTest <= 2) return 0;
  537. const compZ = DimensionalComparator.compare(c1.getZ(), c2.getZ());
  538. return compZ;
  539. }
  540. get interfaces_() {
  541. return [Comparator];
  542. }
  543. }
  544. Coordinate.DimensionalComparator = DimensionalComparator;
  545. Coordinate.NULL_ORDINATE = Double.NaN;
  546. Coordinate.X = 0;
  547. Coordinate.Y = 1;
  548. Coordinate.Z = 2;
  549. Coordinate.M = 3;
  550. class CoordinateXY extends Coordinate {
  551. constructor() {
  552. super();
  553. CoordinateXY.constructor_.apply(this, arguments);
  554. }
  555. static constructor_() {
  556. if (arguments.length === 0) {
  557. Coordinate.constructor_.call(this);
  558. } else if (arguments.length === 1) {
  559. if (arguments[0] instanceof CoordinateXY) {
  560. const coord = arguments[0];
  561. Coordinate.constructor_.call(this, coord.x, coord.y);
  562. } else if (arguments[0] instanceof Coordinate) {
  563. const coord = arguments[0];
  564. Coordinate.constructor_.call(this, coord.x, coord.y);
  565. }
  566. } else if (arguments.length === 2) {
  567. const x = arguments[0],
  568. y = arguments[1];
  569. Coordinate.constructor_.call(this, x, y, Coordinate.NULL_ORDINATE);
  570. }
  571. }
  572. setOrdinate(ordinateIndex, value) {
  573. switch (ordinateIndex) {
  574. case CoordinateXY.X:
  575. this.x = value;
  576. break;
  577. case CoordinateXY.Y:
  578. this.y = value;
  579. break;
  580. default:
  581. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  582. }
  583. }
  584. getZ() {
  585. return Coordinate.NULL_ORDINATE;
  586. }
  587. getOrdinate(ordinateIndex) {
  588. switch (ordinateIndex) {
  589. case CoordinateXY.X:
  590. return this.x;
  591. case CoordinateXY.Y:
  592. return this.y;
  593. }
  594. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  595. }
  596. setZ(z) {
  597. throw new IllegalArgumentException('CoordinateXY dimension 2 does not support z-ordinate');
  598. }
  599. copy() {
  600. return new CoordinateXY(this);
  601. }
  602. toString() {
  603. return '(' + this.x + ', ' + this.y + ')';
  604. }
  605. setCoordinate(other) {
  606. this.x = other.x;
  607. this.y = other.y;
  608. this.z = other.getZ();
  609. }
  610. }
  611. CoordinateXY.X = 0;
  612. CoordinateXY.Y = 1;
  613. CoordinateXY.Z = -1;
  614. CoordinateXY.M = -1;
  615. class CoordinateXYM extends Coordinate {
  616. constructor() {
  617. super();
  618. CoordinateXYM.constructor_.apply(this, arguments);
  619. }
  620. static constructor_() {
  621. this._m = null;
  622. if (arguments.length === 0) {
  623. Coordinate.constructor_.call(this);
  624. this._m = 0.0;
  625. } else if (arguments.length === 1) {
  626. if (arguments[0] instanceof CoordinateXYM) {
  627. const coord = arguments[0];
  628. Coordinate.constructor_.call(this, coord.x, coord.y);
  629. this._m = coord._m;
  630. } else if (arguments[0] instanceof Coordinate) {
  631. const coord = arguments[0];
  632. Coordinate.constructor_.call(this, coord.x, coord.y);
  633. this._m = this.getM();
  634. }
  635. } else if (arguments.length === 3) {
  636. const x = arguments[0],
  637. y = arguments[1],
  638. m = arguments[2];
  639. Coordinate.constructor_.call(this, x, y, Coordinate.NULL_ORDINATE);
  640. this._m = m;
  641. }
  642. }
  643. getM() {
  644. return this._m;
  645. }
  646. setOrdinate(ordinateIndex, value) {
  647. switch (ordinateIndex) {
  648. case CoordinateXYM.X:
  649. this.x = value;
  650. break;
  651. case CoordinateXYM.Y:
  652. this.y = value;
  653. break;
  654. case CoordinateXYM.M:
  655. this._m = value;
  656. break;
  657. default:
  658. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  659. }
  660. }
  661. setM(m) {
  662. this._m = m;
  663. }
  664. getZ() {
  665. return Coordinate.NULL_ORDINATE;
  666. }
  667. getOrdinate(ordinateIndex) {
  668. switch (ordinateIndex) {
  669. case CoordinateXYM.X:
  670. return this.x;
  671. case CoordinateXYM.Y:
  672. return this.y;
  673. case CoordinateXYM.M:
  674. return this._m;
  675. }
  676. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  677. }
  678. setZ(z) {
  679. throw new IllegalArgumentException('CoordinateXY dimension 2 does not support z-ordinate');
  680. }
  681. copy() {
  682. return new CoordinateXYM(this);
  683. }
  684. toString() {
  685. return '(' + this.x + ', ' + this.y + ' m=' + this.getM() + ')';
  686. }
  687. setCoordinate(other) {
  688. this.x = other.x;
  689. this.y = other.y;
  690. this.z = other.getZ();
  691. this._m = other.getM();
  692. }
  693. }
  694. CoordinateXYM.X = 0;
  695. CoordinateXYM.Y = 1;
  696. CoordinateXYM.Z = -1;
  697. CoordinateXYM.M = 2;
  698. class CoordinateXYZM extends Coordinate {
  699. constructor() {
  700. super();
  701. CoordinateXYZM.constructor_.apply(this, arguments);
  702. }
  703. static constructor_() {
  704. this._m = null;
  705. if (arguments.length === 0) {
  706. Coordinate.constructor_.call(this);
  707. this._m = 0.0;
  708. } else if (arguments.length === 1) {
  709. if (arguments[0] instanceof CoordinateXYZM) {
  710. const coord = arguments[0];
  711. Coordinate.constructor_.call(this, coord);
  712. this._m = coord._m;
  713. } else if (arguments[0] instanceof Coordinate) {
  714. const coord = arguments[0];
  715. Coordinate.constructor_.call(this, coord);
  716. this._m = this.getM();
  717. }
  718. } else if (arguments.length === 4) {
  719. const x = arguments[0],
  720. y = arguments[1],
  721. z = arguments[2],
  722. m = arguments[3];
  723. Coordinate.constructor_.call(this, x, y, z);
  724. this._m = m;
  725. }
  726. }
  727. getM() {
  728. return this._m;
  729. }
  730. setOrdinate(ordinateIndex, value) {
  731. switch (ordinateIndex) {
  732. case Coordinate.X:
  733. this.x = value;
  734. break;
  735. case Coordinate.Y:
  736. this.y = value;
  737. break;
  738. case Coordinate.Z:
  739. this.z = value;
  740. break;
  741. case Coordinate.M:
  742. this._m = value;
  743. break;
  744. default:
  745. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  746. }
  747. }
  748. setM(m) {
  749. this._m = m;
  750. }
  751. getOrdinate(ordinateIndex) {
  752. switch (ordinateIndex) {
  753. case Coordinate.X:
  754. return this.x;
  755. case Coordinate.Y:
  756. return this.y;
  757. case Coordinate.Z:
  758. return this.getZ();
  759. case Coordinate.M:
  760. return this.getM();
  761. }
  762. throw new IllegalArgumentException('Invalid ordinate index: ' + ordinateIndex);
  763. }
  764. copy() {
  765. return new CoordinateXYZM(this);
  766. }
  767. toString() {
  768. return '(' + this.x + ', ' + this.y + ', ' + this.getZ() + ' m=' + this.getM() + ')';
  769. }
  770. setCoordinate(other) {
  771. this.x = other.x;
  772. this.y = other.y;
  773. this.z = other.getZ();
  774. this._m = other.getM();
  775. }
  776. }
  777. function hasInterface (o, i) {
  778. return o.interfaces_ && o.interfaces_.indexOf(i) > -1;
  779. }
  780. /**
  781. * @see http://download.oracle.com/javase/6/docs/api/java/util/Collection.html
  782. */
  783. class Collection {
  784. /**
  785. * Ensures that this collection contains the specified element (optional
  786. * operation).
  787. * @param {Object} e
  788. * @return {boolean}
  789. */
  790. add() {}
  791. /**
  792. * Appends all of the elements in the specified collection to the end of this
  793. * list, in the order that they are returned by the specified collection's
  794. * iterator (optional operation).
  795. * @param {javascript.util.Collection} c
  796. * @return {boolean}
  797. */
  798. addAll() {}
  799. /**
  800. * Returns true if this collection contains no elements.
  801. * @return {boolean}
  802. */
  803. isEmpty() {}
  804. /**
  805. * Returns an iterator over the elements in this collection.
  806. * @return {javascript.util.Iterator}
  807. */
  808. iterator() {}
  809. /**
  810. * Returns an iterator over the elements in this collection.
  811. * @return {number}
  812. */
  813. size() {}
  814. /**
  815. * Returns an array containing all of the elements in this collection.
  816. * @return {Array}
  817. */
  818. toArray() {}
  819. /**
  820. * Removes a single instance of the specified element from this collection if it
  821. * is present. (optional)
  822. * @param {Object} e
  823. * @return {boolean}
  824. */
  825. remove() {}
  826. }
  827. class IndexOutOfBoundsException extends Exception {
  828. constructor(message) {
  829. super(message);
  830. this.name = Object.keys({
  831. IndexOutOfBoundsException
  832. })[0];
  833. }
  834. }
  835. /**
  836. * @see http://download.oracle.com/javase/6/docs/api/java/util/List.html
  837. */
  838. class List extends Collection {
  839. /**
  840. * Returns the element at the specified position in this list.
  841. * @param {number} index
  842. * @return {Object}
  843. */
  844. get() {}
  845. /**
  846. * Replaces the element at the specified position in this list with the
  847. * specified element (optional operation).
  848. * @param {number} index
  849. * @param {Object} e
  850. * @return {Object}
  851. */
  852. set() {}
  853. /**
  854. * Returns true if this collection contains no elements.
  855. * @return {boolean}
  856. */
  857. isEmpty() {}
  858. }
  859. class NoSuchElementException extends Exception {
  860. constructor(message) {
  861. super(message);
  862. this.name = Object.keys({
  863. NoSuchElementException
  864. })[0];
  865. }
  866. }
  867. /**
  868. * @see http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html
  869. */
  870. class ArrayList extends List {
  871. constructor(o) {
  872. super();
  873. this.array = [];
  874. if (o instanceof Collection) this.addAll(o);
  875. }
  876. get interfaces_() {
  877. return [List, Collection];
  878. }
  879. ensureCapacity() {}
  880. add(e) {
  881. if (arguments.length === 1) this.array.push(e);else this.array.splice(arguments[0], 0, arguments[1]);
  882. return true;
  883. }
  884. clear() {
  885. this.array = [];
  886. }
  887. addAll(c) {
  888. for (const e of c) this.array.push(e);
  889. }
  890. set(index, element) {
  891. const oldElement = this.array[index];
  892. this.array[index] = element;
  893. return oldElement;
  894. }
  895. iterator() {
  896. return new Iterator$3(this);
  897. }
  898. get(index) {
  899. if (index < 0 || index >= this.size()) throw new IndexOutOfBoundsException();
  900. return this.array[index];
  901. }
  902. isEmpty() {
  903. return this.array.length === 0;
  904. }
  905. sort(comparator) {
  906. if (comparator) this.array.sort((a, b) => comparator.compare(a, b));else this.array.sort();
  907. }
  908. size() {
  909. return this.array.length;
  910. }
  911. toArray() {
  912. return this.array.slice();
  913. }
  914. remove(o) {
  915. for (let i = 0, len = this.array.length; i < len; i++) if (this.array[i] === o) return !!this.array.splice(i, 1);
  916. return false;
  917. }
  918. [Symbol.iterator]() {
  919. return this.array.values();
  920. }
  921. }
  922. class Iterator$3 {
  923. constructor(arrayList) {
  924. this.arrayList = arrayList;
  925. this.position = 0;
  926. }
  927. next() {
  928. if (this.position === this.arrayList.size()) throw new NoSuchElementException();
  929. return this.arrayList.get(this.position++);
  930. }
  931. hasNext() {
  932. return this.position < this.arrayList.size();
  933. }
  934. set(element) {
  935. return this.arrayList.set(this.position - 1, element);
  936. }
  937. remove() {
  938. this.arrayList.remove(this.arrayList.get(this.position));
  939. }
  940. }
  941. class CoordinateList extends ArrayList {
  942. constructor() {
  943. super();
  944. CoordinateList.constructor_.apply(this, arguments);
  945. }
  946. static constructor_() {
  947. if (arguments.length === 0) ; else if (arguments.length === 1) {
  948. const coord = arguments[0];
  949. this.ensureCapacity(coord.length);
  950. this.add(coord, true);
  951. } else if (arguments.length === 2) {
  952. const coord = arguments[0],
  953. allowRepeated = arguments[1];
  954. this.ensureCapacity(coord.length);
  955. this.add(coord, allowRepeated);
  956. }
  957. }
  958. getCoordinate(i) {
  959. return this.get(i);
  960. }
  961. addAll() {
  962. if (arguments.length === 2 && typeof arguments[1] === 'boolean' && hasInterface(arguments[0], Collection)) {
  963. const coll = arguments[0],
  964. allowRepeated = arguments[1];
  965. let isChanged = false;
  966. for (let i = coll.iterator(); i.hasNext();) {
  967. this.add(i.next(), allowRepeated);
  968. isChanged = true;
  969. }
  970. return isChanged;
  971. } else {
  972. return super.addAll.apply(this, arguments);
  973. }
  974. }
  975. clone() {
  976. const clone = super.clone.call(this);
  977. for (let i = 0; i < this.size(); i++) clone.add(i, this.get(i).clone());
  978. return clone;
  979. }
  980. toCoordinateArray() {
  981. if (arguments.length === 0) {
  982. return this.toArray(CoordinateList.coordArrayType);
  983. } else if (arguments.length === 1) {
  984. const isForward = arguments[0];
  985. if (isForward) return this.toArray(CoordinateList.coordArrayType);
  986. const size = this.size();
  987. const pts = new Array(size).fill(null);
  988. for (let i = 0; i < size; i++) pts[i] = this.get(size - i - 1);
  989. return pts;
  990. }
  991. }
  992. add() {
  993. if (arguments.length === 1) {
  994. const coord = arguments[0];
  995. return super.add.call(this, coord);
  996. } else if (arguments.length === 2) {
  997. if (arguments[0] instanceof Array && typeof arguments[1] === 'boolean') {
  998. const coord = arguments[0],
  999. allowRepeated = arguments[1];
  1000. this.add(coord, allowRepeated, true);
  1001. return true;
  1002. } else if (arguments[0] instanceof Coordinate && typeof arguments[1] === 'boolean') {
  1003. const coord = arguments[0],
  1004. allowRepeated = arguments[1];
  1005. if (!allowRepeated) if (this.size() >= 1) {
  1006. const last = this.get(this.size() - 1);
  1007. if (last.equals2D(coord)) return null;
  1008. }
  1009. super.add.call(this, coord);
  1010. } else if (arguments[0] instanceof Object && typeof arguments[1] === 'boolean') {
  1011. const obj = arguments[0],
  1012. allowRepeated = arguments[1];
  1013. this.add(obj, allowRepeated);
  1014. return true;
  1015. }
  1016. } else if (arguments.length === 3) {
  1017. if (typeof arguments[2] === 'boolean' && arguments[0] instanceof Array && typeof arguments[1] === 'boolean') {
  1018. const coord = arguments[0],
  1019. allowRepeated = arguments[1],
  1020. direction = arguments[2];
  1021. if (direction) for (let i = 0; i < coord.length; i++) this.add(coord[i], allowRepeated);else for (let i = coord.length - 1; i >= 0; i--) this.add(coord[i], allowRepeated);
  1022. return true;
  1023. } else if (typeof arguments[2] === 'boolean' && Number.isInteger(arguments[0]) && arguments[1] instanceof Coordinate) {
  1024. const i = arguments[0],
  1025. coord = arguments[1],
  1026. allowRepeated = arguments[2];
  1027. if (!allowRepeated) {
  1028. const size = this.size();
  1029. if (size > 0) {
  1030. if (i > 0) {
  1031. const prev = this.get(i - 1);
  1032. if (prev.equals2D(coord)) return null;
  1033. }
  1034. if (i < size) {
  1035. const next = this.get(i);
  1036. if (next.equals2D(coord)) return null;
  1037. }
  1038. }
  1039. }
  1040. super.add.call(this, i, coord);
  1041. }
  1042. } else if (arguments.length === 4) {
  1043. const coord = arguments[0],
  1044. allowRepeated = arguments[1],
  1045. start = arguments[2],
  1046. end = arguments[3];
  1047. let inc = 1;
  1048. if (start > end) inc = -1;
  1049. for (let i = start; i !== end; i += inc) this.add(coord[i], allowRepeated);
  1050. return true;
  1051. }
  1052. }
  1053. closeRing() {
  1054. if (this.size() > 0) {
  1055. const duplicate = this.get(0).copy();
  1056. this.add(duplicate, false);
  1057. }
  1058. }
  1059. }
  1060. CoordinateList.coordArrayType = new Array(0).fill(null);
  1061. class CoordinateSequenceFilter {
  1062. filter(seq, i) {}
  1063. isDone() {}
  1064. isGeometryChanged() {}
  1065. }
  1066. class Envelope {
  1067. constructor() {
  1068. Envelope.constructor_.apply(this, arguments);
  1069. }
  1070. static constructor_() {
  1071. this._minx = null;
  1072. this._maxx = null;
  1073. this._miny = null;
  1074. this._maxy = null;
  1075. if (arguments.length === 0) {
  1076. this.init();
  1077. } else if (arguments.length === 1) {
  1078. if (arguments[0] instanceof Coordinate) {
  1079. const p = arguments[0];
  1080. this.init(p.x, p.x, p.y, p.y);
  1081. } else if (arguments[0] instanceof Envelope) {
  1082. const env = arguments[0];
  1083. this.init(env);
  1084. }
  1085. } else if (arguments.length === 2) {
  1086. const p1 = arguments[0],
  1087. p2 = arguments[1];
  1088. this.init(p1.x, p2.x, p1.y, p2.y);
  1089. } else if (arguments.length === 4) {
  1090. const x1 = arguments[0],
  1091. x2 = arguments[1],
  1092. y1 = arguments[2],
  1093. y2 = arguments[3];
  1094. this.init(x1, x2, y1, y2);
  1095. }
  1096. }
  1097. static intersects() {
  1098. if (arguments.length === 3) {
  1099. const p1 = arguments[0],
  1100. p2 = arguments[1],
  1101. q = arguments[2];
  1102. if (q.x >= (p1.x < p2.x ? p1.x : p2.x) && q.x <= (p1.x > p2.x ? p1.x : p2.x) && q.y >= (p1.y < p2.y ? p1.y : p2.y) && q.y <= (p1.y > p2.y ? p1.y : p2.y)) return true;
  1103. return false;
  1104. } else if (arguments.length === 4) {
  1105. const p1 = arguments[0],
  1106. p2 = arguments[1],
  1107. q1 = arguments[2],
  1108. q2 = arguments[3];
  1109. let minq = Math.min(q1.x, q2.x);
  1110. let maxq = Math.max(q1.x, q2.x);
  1111. let minp = Math.min(p1.x, p2.x);
  1112. let maxp = Math.max(p1.x, p2.x);
  1113. if (minp > maxq) return false;
  1114. if (maxp < minq) return false;
  1115. minq = Math.min(q1.y, q2.y);
  1116. maxq = Math.max(q1.y, q2.y);
  1117. minp = Math.min(p1.y, p2.y);
  1118. maxp = Math.max(p1.y, p2.y);
  1119. if (minp > maxq) return false;
  1120. if (maxp < minq) return false;
  1121. return true;
  1122. }
  1123. }
  1124. getArea() {
  1125. return this.getWidth() * this.getHeight();
  1126. }
  1127. equals(other) {
  1128. if (!(other instanceof Envelope)) return false;
  1129. const otherEnvelope = other;
  1130. if (this.isNull()) return otherEnvelope.isNull();
  1131. return this._maxx === otherEnvelope.getMaxX() && this._maxy === otherEnvelope.getMaxY() && this._minx === otherEnvelope.getMinX() && this._miny === otherEnvelope.getMinY();
  1132. }
  1133. intersection(env) {
  1134. if (this.isNull() || env.isNull() || !this.intersects(env)) return new Envelope();
  1135. const intMinX = this._minx > env._minx ? this._minx : env._minx;
  1136. const intMinY = this._miny > env._miny ? this._miny : env._miny;
  1137. const intMaxX = this._maxx < env._maxx ? this._maxx : env._maxx;
  1138. const intMaxY = this._maxy < env._maxy ? this._maxy : env._maxy;
  1139. return new Envelope(intMinX, intMaxX, intMinY, intMaxY);
  1140. }
  1141. isNull() {
  1142. return this._maxx < this._minx;
  1143. }
  1144. getMaxX() {
  1145. return this._maxx;
  1146. }
  1147. covers() {
  1148. if (arguments.length === 1) {
  1149. if (arguments[0] instanceof Coordinate) {
  1150. const p = arguments[0];
  1151. return this.covers(p.x, p.y);
  1152. } else if (arguments[0] instanceof Envelope) {
  1153. const other = arguments[0];
  1154. if (this.isNull() || other.isNull()) return false;
  1155. return other.getMinX() >= this._minx && other.getMaxX() <= this._maxx && other.getMinY() >= this._miny && other.getMaxY() <= this._maxy;
  1156. }
  1157. } else if (arguments.length === 2) {
  1158. const x = arguments[0],
  1159. y = arguments[1];
  1160. if (this.isNull()) return false;
  1161. return x >= this._minx && x <= this._maxx && y >= this._miny && y <= this._maxy;
  1162. }
  1163. }
  1164. intersects() {
  1165. if (arguments.length === 1) {
  1166. if (arguments[0] instanceof Envelope) {
  1167. const other = arguments[0];
  1168. if (this.isNull() || other.isNull()) return false;
  1169. return !(other._minx > this._maxx || other._maxx < this._minx || other._miny > this._maxy || other._maxy < this._miny);
  1170. } else if (arguments[0] instanceof Coordinate) {
  1171. const p = arguments[0];
  1172. return this.intersects(p.x, p.y);
  1173. }
  1174. } else if (arguments.length === 2) {
  1175. if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  1176. const a = arguments[0],
  1177. b = arguments[1];
  1178. if (this.isNull()) return false;
  1179. const envminx = a.x < b.x ? a.x : b.x;
  1180. if (envminx > this._maxx) return false;
  1181. const envmaxx = a.x > b.x ? a.x : b.x;
  1182. if (envmaxx < this._minx) return false;
  1183. const envminy = a.y < b.y ? a.y : b.y;
  1184. if (envminy > this._maxy) return false;
  1185. const envmaxy = a.y > b.y ? a.y : b.y;
  1186. if (envmaxy < this._miny) return false;
  1187. return true;
  1188. } else if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  1189. const x = arguments[0],
  1190. y = arguments[1];
  1191. if (this.isNull()) return false;
  1192. return !(x > this._maxx || x < this._minx || y > this._maxy || y < this._miny);
  1193. }
  1194. }
  1195. }
  1196. getMinY() {
  1197. return this._miny;
  1198. }
  1199. getDiameter() {
  1200. if (this.isNull()) return 0;
  1201. const w = this.getWidth();
  1202. const h = this.getHeight();
  1203. return Math.sqrt(w * w + h * h);
  1204. }
  1205. getMinX() {
  1206. return this._minx;
  1207. }
  1208. expandToInclude() {
  1209. if (arguments.length === 1) {
  1210. if (arguments[0] instanceof Coordinate) {
  1211. const p = arguments[0];
  1212. this.expandToInclude(p.x, p.y);
  1213. } else if (arguments[0] instanceof Envelope) {
  1214. const other = arguments[0];
  1215. if (other.isNull()) return null;
  1216. if (this.isNull()) {
  1217. this._minx = other.getMinX();
  1218. this._maxx = other.getMaxX();
  1219. this._miny = other.getMinY();
  1220. this._maxy = other.getMaxY();
  1221. } else {
  1222. if (other._minx < this._minx) this._minx = other._minx;
  1223. if (other._maxx > this._maxx) this._maxx = other._maxx;
  1224. if (other._miny < this._miny) this._miny = other._miny;
  1225. if (other._maxy > this._maxy) this._maxy = other._maxy;
  1226. }
  1227. }
  1228. } else if (arguments.length === 2) {
  1229. const x = arguments[0],
  1230. y = arguments[1];
  1231. if (this.isNull()) {
  1232. this._minx = x;
  1233. this._maxx = x;
  1234. this._miny = y;
  1235. this._maxy = y;
  1236. } else {
  1237. if (x < this._minx) this._minx = x;
  1238. if (x > this._maxx) this._maxx = x;
  1239. if (y < this._miny) this._miny = y;
  1240. if (y > this._maxy) this._maxy = y;
  1241. }
  1242. }
  1243. }
  1244. minExtent() {
  1245. if (this.isNull()) return 0.0;
  1246. const w = this.getWidth();
  1247. const h = this.getHeight();
  1248. if (w < h) return w;
  1249. return h;
  1250. }
  1251. getWidth() {
  1252. if (this.isNull()) return 0;
  1253. return this._maxx - this._minx;
  1254. }
  1255. compareTo(o) {
  1256. const env = o;
  1257. if (this.isNull()) {
  1258. if (env.isNull()) return 0;
  1259. return -1;
  1260. } else {
  1261. if (env.isNull()) return 1;
  1262. }
  1263. if (this._minx < env._minx) return -1;
  1264. if (this._minx > env._minx) return 1;
  1265. if (this._miny < env._miny) return -1;
  1266. if (this._miny > env._miny) return 1;
  1267. if (this._maxx < env._maxx) return -1;
  1268. if (this._maxx > env._maxx) return 1;
  1269. if (this._maxy < env._maxy) return -1;
  1270. if (this._maxy > env._maxy) return 1;
  1271. return 0;
  1272. }
  1273. translate(transX, transY) {
  1274. if (this.isNull()) return null;
  1275. this.init(this.getMinX() + transX, this.getMaxX() + transX, this.getMinY() + transY, this.getMaxY() + transY);
  1276. }
  1277. copy() {
  1278. return new Envelope(this);
  1279. }
  1280. toString() {
  1281. return 'Env[' + this._minx + ' : ' + this._maxx + ', ' + this._miny + ' : ' + this._maxy + ']';
  1282. }
  1283. setToNull() {
  1284. this._minx = 0;
  1285. this._maxx = -1;
  1286. this._miny = 0;
  1287. this._maxy = -1;
  1288. }
  1289. disjoint(other) {
  1290. if (this.isNull() || other.isNull()) return true;
  1291. return other._minx > this._maxx || other._maxx < this._minx || other._miny > this._maxy || other._maxy < this._miny;
  1292. }
  1293. getHeight() {
  1294. if (this.isNull()) return 0;
  1295. return this._maxy - this._miny;
  1296. }
  1297. maxExtent() {
  1298. if (this.isNull()) return 0.0;
  1299. const w = this.getWidth();
  1300. const h = this.getHeight();
  1301. if (w > h) return w;
  1302. return h;
  1303. }
  1304. expandBy() {
  1305. if (arguments.length === 1) {
  1306. const distance = arguments[0];
  1307. this.expandBy(distance, distance);
  1308. } else if (arguments.length === 2) {
  1309. const deltaX = arguments[0],
  1310. deltaY = arguments[1];
  1311. if (this.isNull()) return null;
  1312. this._minx -= deltaX;
  1313. this._maxx += deltaX;
  1314. this._miny -= deltaY;
  1315. this._maxy += deltaY;
  1316. if (this._minx > this._maxx || this._miny > this._maxy) this.setToNull();
  1317. }
  1318. }
  1319. contains() {
  1320. if (arguments.length === 1) {
  1321. if (arguments[0] instanceof Envelope) {
  1322. const other = arguments[0];
  1323. return this.covers(other);
  1324. } else if (arguments[0] instanceof Coordinate) {
  1325. const p = arguments[0];
  1326. return this.covers(p);
  1327. }
  1328. } else if (arguments.length === 2) {
  1329. const x = arguments[0],
  1330. y = arguments[1];
  1331. return this.covers(x, y);
  1332. }
  1333. }
  1334. centre() {
  1335. if (this.isNull()) return null;
  1336. return new Coordinate((this.getMinX() + this.getMaxX()) / 2.0, (this.getMinY() + this.getMaxY()) / 2.0);
  1337. }
  1338. init() {
  1339. if (arguments.length === 0) {
  1340. this.setToNull();
  1341. } else if (arguments.length === 1) {
  1342. if (arguments[0] instanceof Coordinate) {
  1343. const p = arguments[0];
  1344. this.init(p.x, p.x, p.y, p.y);
  1345. } else if (arguments[0] instanceof Envelope) {
  1346. const env = arguments[0];
  1347. this._minx = env._minx;
  1348. this._maxx = env._maxx;
  1349. this._miny = env._miny;
  1350. this._maxy = env._maxy;
  1351. }
  1352. } else if (arguments.length === 2) {
  1353. const p1 = arguments[0],
  1354. p2 = arguments[1];
  1355. this.init(p1.x, p2.x, p1.y, p2.y);
  1356. } else if (arguments.length === 4) {
  1357. const x1 = arguments[0],
  1358. x2 = arguments[1],
  1359. y1 = arguments[2],
  1360. y2 = arguments[3];
  1361. if (x1 < x2) {
  1362. this._minx = x1;
  1363. this._maxx = x2;
  1364. } else {
  1365. this._minx = x2;
  1366. this._maxx = x1;
  1367. }
  1368. if (y1 < y2) {
  1369. this._miny = y1;
  1370. this._maxy = y2;
  1371. } else {
  1372. this._miny = y2;
  1373. this._maxy = y1;
  1374. }
  1375. }
  1376. }
  1377. getMaxY() {
  1378. return this._maxy;
  1379. }
  1380. distance(env) {
  1381. if (this.intersects(env)) return 0;
  1382. let dx = 0.0;
  1383. if (this._maxx < env._minx) dx = env._minx - this._maxx;else if (this._minx > env._maxx) dx = this._minx - env._maxx;
  1384. let dy = 0.0;
  1385. if (this._maxy < env._miny) dy = env._miny - this._maxy;else if (this._miny > env._maxy) dy = this._miny - env._maxy;
  1386. if (dx === 0.0) return dy;
  1387. if (dy === 0.0) return dx;
  1388. return Math.sqrt(dx * dx + dy * dy);
  1389. }
  1390. hashCode() {
  1391. let result = 17;
  1392. result = 37 * result + Coordinate.hashCode(this._minx);
  1393. result = 37 * result + Coordinate.hashCode(this._maxx);
  1394. result = 37 * result + Coordinate.hashCode(this._miny);
  1395. result = 37 * result + Coordinate.hashCode(this._maxy);
  1396. return result;
  1397. }
  1398. get interfaces_() {
  1399. return [Comparable, Serializable];
  1400. }
  1401. }
  1402. class StringBuffer {
  1403. constructor(str) {
  1404. this.str = str;
  1405. }
  1406. append(e) {
  1407. this.str += e;
  1408. }
  1409. setCharAt(i, c) {
  1410. this.str = this.str.substr(0, i) + c + this.str.substr(i + 1);
  1411. }
  1412. toString() {
  1413. return this.str;
  1414. }
  1415. }
  1416. class Integer {
  1417. constructor(value) {
  1418. this.value = value;
  1419. }
  1420. intValue() {
  1421. return this.value;
  1422. }
  1423. compareTo(o) {
  1424. if (this.value < o) return -1;
  1425. if (this.value > o) return 1;
  1426. return 0;
  1427. }
  1428. static compare(x, y) {
  1429. if (x < y) return -1;
  1430. if (x > y) return 1;
  1431. return 0;
  1432. }
  1433. static isNan(n) {
  1434. return Number.isNaN(n);
  1435. }
  1436. static valueOf(value) {
  1437. return new Integer(value);
  1438. }
  1439. }
  1440. class Character {
  1441. static isWhitespace(c) {
  1442. return c <= 32 && c >= 0 || c === 127;
  1443. }
  1444. static toUpperCase(c) {
  1445. return c.toUpperCase();
  1446. }
  1447. }
  1448. class DD {
  1449. constructor() {
  1450. DD.constructor_.apply(this, arguments);
  1451. }
  1452. static constructor_() {
  1453. this._hi = 0.0;
  1454. this._lo = 0.0;
  1455. if (arguments.length === 0) {
  1456. this.init(0.0);
  1457. } else if (arguments.length === 1) {
  1458. if (typeof arguments[0] === 'number') {
  1459. const x = arguments[0];
  1460. this.init(x);
  1461. } else if (arguments[0] instanceof DD) {
  1462. const dd = arguments[0];
  1463. this.init(dd);
  1464. } else if (typeof arguments[0] === 'string') {
  1465. const str = arguments[0];
  1466. DD.constructor_.call(this, DD.parse(str));
  1467. }
  1468. } else if (arguments.length === 2) {
  1469. const hi = arguments[0],
  1470. lo = arguments[1];
  1471. this.init(hi, lo);
  1472. }
  1473. }
  1474. static determinant() {
  1475. if (typeof arguments[3] === 'number' && typeof arguments[2] === 'number' && typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  1476. const x1 = arguments[0],
  1477. y1 = arguments[1],
  1478. x2 = arguments[2],
  1479. y2 = arguments[3];
  1480. return DD.determinant(DD.valueOf(x1), DD.valueOf(y1), DD.valueOf(x2), DD.valueOf(y2));
  1481. } else if (arguments[3] instanceof DD && arguments[2] instanceof DD && arguments[0] instanceof DD && arguments[1] instanceof DD) {
  1482. const x1 = arguments[0],
  1483. y1 = arguments[1],
  1484. x2 = arguments[2],
  1485. y2 = arguments[3];
  1486. const det = x1.multiply(y2).selfSubtract(y1.multiply(x2));
  1487. return det;
  1488. }
  1489. }
  1490. static sqr(x) {
  1491. return DD.valueOf(x).selfMultiply(x);
  1492. }
  1493. static valueOf() {
  1494. if (typeof arguments[0] === 'string') {
  1495. const str = arguments[0];
  1496. return DD.parse(str);
  1497. } else if (typeof arguments[0] === 'number') {
  1498. const x = arguments[0];
  1499. return new DD(x);
  1500. }
  1501. }
  1502. static sqrt(x) {
  1503. return DD.valueOf(x).sqrt();
  1504. }
  1505. static parse(str) {
  1506. let i = 0;
  1507. const strlen = str.length;
  1508. while (Character.isWhitespace(str.charAt(i))) i++;
  1509. let isNegative = false;
  1510. if (i < strlen) {
  1511. const signCh = str.charAt(i);
  1512. if (signCh === '-' || signCh === '+') {
  1513. i++;
  1514. if (signCh === '-') isNegative = true;
  1515. }
  1516. }
  1517. const val = new DD();
  1518. let numDigits = 0;
  1519. let numBeforeDec = 0;
  1520. let exp = 0;
  1521. let hasDecimalChar = false;
  1522. while (true) {
  1523. if (i >= strlen) break;
  1524. const ch = str.charAt(i);
  1525. i++;
  1526. if (Character.isDigit(ch)) {
  1527. const d = ch - '0';
  1528. val.selfMultiply(DD.TEN);
  1529. val.selfAdd(d);
  1530. numDigits++;
  1531. continue;
  1532. }
  1533. if (ch === '.') {
  1534. numBeforeDec = numDigits;
  1535. hasDecimalChar = true;
  1536. continue;
  1537. }
  1538. if (ch === 'e' || ch === 'E') {
  1539. const expStr = str.substring(i);
  1540. try {
  1541. exp = Integer.parseInt(expStr);
  1542. } catch (ex) {
  1543. if (ex instanceof NumberFormatException) throw new NumberFormatException('Invalid exponent ' + expStr + ' in string ' + str);else throw ex;
  1544. } finally {}
  1545. break;
  1546. }
  1547. throw new NumberFormatException('Unexpected character \'' + ch + '\' at position ' + i + ' in string ' + str);
  1548. }
  1549. let val2 = val;
  1550. if (!hasDecimalChar) numBeforeDec = numDigits;
  1551. const numDecPlaces = numDigits - numBeforeDec - exp;
  1552. if (numDecPlaces === 0) {
  1553. val2 = val;
  1554. } else if (numDecPlaces > 0) {
  1555. const scale = DD.TEN.pow(numDecPlaces);
  1556. val2 = val.divide(scale);
  1557. } else if (numDecPlaces < 0) {
  1558. const scale = DD.TEN.pow(-numDecPlaces);
  1559. val2 = val.multiply(scale);
  1560. }
  1561. if (isNegative) return val2.negate();
  1562. return val2;
  1563. }
  1564. static createNaN() {
  1565. return new DD(Double.NaN, Double.NaN);
  1566. }
  1567. static copy(dd) {
  1568. return new DD(dd);
  1569. }
  1570. static magnitude(x) {
  1571. const xAbs = Math.abs(x);
  1572. const xLog10 = Math.log(xAbs) / Math.log(10);
  1573. let xMag = Math.trunc(Math.floor(xLog10));
  1574. const xApprox = Math.pow(10, xMag);
  1575. if (xApprox * 10 <= xAbs) xMag += 1;
  1576. return xMag;
  1577. }
  1578. static stringOfChar(ch, len) {
  1579. const buf = new StringBuffer();
  1580. for (let i = 0; i < len; i++) buf.append(ch);
  1581. return buf.toString();
  1582. }
  1583. le(y) {
  1584. return this._hi < y._hi || this._hi === y._hi && this._lo <= y._lo;
  1585. }
  1586. extractSignificantDigits(insertDecimalPoint, magnitude) {
  1587. let y = this.abs();
  1588. let mag = DD.magnitude(y._hi);
  1589. const scale = DD.TEN.pow(mag);
  1590. y = y.divide(scale);
  1591. if (y.gt(DD.TEN)) {
  1592. y = y.divide(DD.TEN);
  1593. mag += 1;
  1594. } else if (y.lt(DD.ONE)) {
  1595. y = y.multiply(DD.TEN);
  1596. mag -= 1;
  1597. }
  1598. const decimalPointPos = mag + 1;
  1599. const buf = new StringBuffer();
  1600. const numDigits = DD.MAX_PRINT_DIGITS - 1;
  1601. for (let i = 0; i <= numDigits; i++) {
  1602. if (insertDecimalPoint && i === decimalPointPos) buf.append('.');
  1603. const digit = Math.trunc(y._hi);
  1604. if (digit < 0) break;
  1605. let rebiasBy10 = false;
  1606. let digitChar = 0;
  1607. if (digit > 9) {
  1608. rebiasBy10 = true;
  1609. digitChar = '9';
  1610. } else {
  1611. digitChar = '0' + digit;
  1612. }
  1613. buf.append(digitChar);
  1614. y = y.subtract(DD.valueOf(digit)).multiply(DD.TEN);
  1615. if (rebiasBy10) y.selfAdd(DD.TEN);
  1616. let continueExtractingDigits = true;
  1617. const remMag = DD.magnitude(y._hi);
  1618. if (remMag < 0 && Math.abs(remMag) >= numDigits - i) continueExtractingDigits = false;
  1619. if (!continueExtractingDigits) break;
  1620. }
  1621. magnitude[0] = mag;
  1622. return buf.toString();
  1623. }
  1624. sqr() {
  1625. return this.multiply(this);
  1626. }
  1627. doubleValue() {
  1628. return this._hi + this._lo;
  1629. }
  1630. subtract() {
  1631. if (arguments[0] instanceof DD) {
  1632. const y = arguments[0];
  1633. return this.add(y.negate());
  1634. } else if (typeof arguments[0] === 'number') {
  1635. const y = arguments[0];
  1636. return this.add(-y);
  1637. }
  1638. }
  1639. equals() {
  1640. if (arguments.length === 1 && arguments[0] instanceof DD) {
  1641. const y = arguments[0];
  1642. return this._hi === y._hi && this._lo === y._lo;
  1643. }
  1644. }
  1645. isZero() {
  1646. return this._hi === 0.0 && this._lo === 0.0;
  1647. }
  1648. selfSubtract() {
  1649. if (arguments[0] instanceof DD) {
  1650. const y = arguments[0];
  1651. if (this.isNaN()) return this;
  1652. return this.selfAdd(-y._hi, -y._lo);
  1653. } else if (typeof arguments[0] === 'number') {
  1654. const y = arguments[0];
  1655. if (this.isNaN()) return this;
  1656. return this.selfAdd(-y, 0.0);
  1657. }
  1658. }
  1659. getSpecialNumberString() {
  1660. if (this.isZero()) return '0.0';
  1661. if (this.isNaN()) return 'NaN ';
  1662. return null;
  1663. }
  1664. min(x) {
  1665. if (this.le(x)) return this;else return x;
  1666. }
  1667. selfDivide() {
  1668. if (arguments.length === 1) {
  1669. if (arguments[0] instanceof DD) {
  1670. const y = arguments[0];
  1671. return this.selfDivide(y._hi, y._lo);
  1672. } else if (typeof arguments[0] === 'number') {
  1673. const y = arguments[0];
  1674. return this.selfDivide(y, 0.0);
  1675. }
  1676. } else if (arguments.length === 2) {
  1677. const yhi = arguments[0],
  1678. ylo = arguments[1];
  1679. let hc = null,
  1680. tc = null,
  1681. hy = null,
  1682. ty = null,
  1683. C = null,
  1684. c = null,
  1685. U = null,
  1686. u = null;
  1687. C = this._hi / yhi;
  1688. c = DD.SPLIT * C;
  1689. hc = c - C;
  1690. u = DD.SPLIT * yhi;
  1691. hc = c - hc;
  1692. tc = C - hc;
  1693. hy = u - yhi;
  1694. U = C * yhi;
  1695. hy = u - hy;
  1696. ty = yhi - hy;
  1697. u = hc * hy - U + hc * ty + tc * hy + tc * ty;
  1698. c = (this._hi - U - u + this._lo - C * ylo) / yhi;
  1699. u = C + c;
  1700. this._hi = u;
  1701. this._lo = C - u + c;
  1702. return this;
  1703. }
  1704. }
  1705. dump() {
  1706. return 'DD<' + this._hi + ', ' + this._lo + '>';
  1707. }
  1708. divide() {
  1709. if (arguments[0] instanceof DD) {
  1710. const y = arguments[0];
  1711. let hc = null,
  1712. tc = null,
  1713. hy = null,
  1714. ty = null,
  1715. C = null,
  1716. c = null,
  1717. U = null,
  1718. u = null;
  1719. C = this._hi / y._hi;
  1720. c = DD.SPLIT * C;
  1721. hc = c - C;
  1722. u = DD.SPLIT * y._hi;
  1723. hc = c - hc;
  1724. tc = C - hc;
  1725. hy = u - y._hi;
  1726. U = C * y._hi;
  1727. hy = u - hy;
  1728. ty = y._hi - hy;
  1729. u = hc * hy - U + hc * ty + tc * hy + tc * ty;
  1730. c = (this._hi - U - u + this._lo - C * y._lo) / y._hi;
  1731. u = C + c;
  1732. const zhi = u;
  1733. const zlo = C - u + c;
  1734. return new DD(zhi, zlo);
  1735. } else if (typeof arguments[0] === 'number') {
  1736. const y = arguments[0];
  1737. if (Double.isNaN(y)) return DD.createNaN();
  1738. return DD.copy(this).selfDivide(y, 0.0);
  1739. }
  1740. }
  1741. ge(y) {
  1742. return this._hi > y._hi || this._hi === y._hi && this._lo >= y._lo;
  1743. }
  1744. pow(exp) {
  1745. if (exp === 0.0) return DD.valueOf(1.0);
  1746. let r = new DD(this);
  1747. let s = DD.valueOf(1.0);
  1748. let n = Math.abs(exp);
  1749. if (n > 1) while (n > 0) {
  1750. if (n % 2 === 1) s.selfMultiply(r);
  1751. n /= 2;
  1752. if (n > 0) r = r.sqr();
  1753. } else s = r;
  1754. if (exp < 0) return s.reciprocal();
  1755. return s;
  1756. }
  1757. ceil() {
  1758. if (this.isNaN()) return DD.NaN;
  1759. const fhi = Math.ceil(this._hi);
  1760. let flo = 0.0;
  1761. if (fhi === this._hi) flo = Math.ceil(this._lo);
  1762. return new DD(fhi, flo);
  1763. }
  1764. compareTo(o) {
  1765. const other = o;
  1766. if (this._hi < other._hi) return -1;
  1767. if (this._hi > other._hi) return 1;
  1768. if (this._lo < other._lo) return -1;
  1769. if (this._lo > other._lo) return 1;
  1770. return 0;
  1771. }
  1772. rint() {
  1773. if (this.isNaN()) return this;
  1774. const plus5 = this.add(0.5);
  1775. return plus5.floor();
  1776. }
  1777. setValue() {
  1778. if (arguments[0] instanceof DD) {
  1779. const value = arguments[0];
  1780. this.init(value);
  1781. return this;
  1782. } else if (typeof arguments[0] === 'number') {
  1783. const value = arguments[0];
  1784. this.init(value);
  1785. return this;
  1786. }
  1787. }
  1788. max(x) {
  1789. if (this.ge(x)) return this;else return x;
  1790. }
  1791. sqrt() {
  1792. if (this.isZero()) return DD.valueOf(0.0);
  1793. if (this.isNegative()) return DD.NaN;
  1794. const x = 1.0 / Math.sqrt(this._hi);
  1795. const ax = this._hi * x;
  1796. const axdd = DD.valueOf(ax);
  1797. const diffSq = this.subtract(axdd.sqr());
  1798. const d2 = diffSq._hi * (x * 0.5);
  1799. return axdd.add(d2);
  1800. }
  1801. selfAdd() {
  1802. if (arguments.length === 1) {
  1803. if (arguments[0] instanceof DD) {
  1804. const y = arguments[0];
  1805. return this.selfAdd(y._hi, y._lo);
  1806. } else if (typeof arguments[0] === 'number') {
  1807. const y = arguments[0];
  1808. let H = null,
  1809. h = null,
  1810. S = null,
  1811. s = null,
  1812. e = null,
  1813. f = null;
  1814. S = this._hi + y;
  1815. e = S - this._hi;
  1816. s = S - e;
  1817. s = y - e + (this._hi - s);
  1818. f = s + this._lo;
  1819. H = S + f;
  1820. h = f + (S - H);
  1821. this._hi = H + h;
  1822. this._lo = h + (H - this._hi);
  1823. return this;
  1824. }
  1825. } else if (arguments.length === 2) {
  1826. const yhi = arguments[0],
  1827. ylo = arguments[1];
  1828. let H = null,
  1829. h = null,
  1830. T = null,
  1831. t = null,
  1832. S = null,
  1833. s = null,
  1834. e = null,
  1835. f = null;
  1836. S = this._hi + yhi;
  1837. T = this._lo + ylo;
  1838. e = S - this._hi;
  1839. f = T - this._lo;
  1840. s = S - e;
  1841. t = T - f;
  1842. s = yhi - e + (this._hi - s);
  1843. t = ylo - f + (this._lo - t);
  1844. e = s + T;
  1845. H = S + e;
  1846. h = e + (S - H);
  1847. e = t + h;
  1848. const zhi = H + e;
  1849. const zlo = e + (H - zhi);
  1850. this._hi = zhi;
  1851. this._lo = zlo;
  1852. return this;
  1853. }
  1854. }
  1855. selfMultiply() {
  1856. if (arguments.length === 1) {
  1857. if (arguments[0] instanceof DD) {
  1858. const y = arguments[0];
  1859. return this.selfMultiply(y._hi, y._lo);
  1860. } else if (typeof arguments[0] === 'number') {
  1861. const y = arguments[0];
  1862. return this.selfMultiply(y, 0.0);
  1863. }
  1864. } else if (arguments.length === 2) {
  1865. const yhi = arguments[0],
  1866. ylo = arguments[1];
  1867. let hx = null,
  1868. tx = null,
  1869. hy = null,
  1870. ty = null,
  1871. C = null,
  1872. c = null;
  1873. C = DD.SPLIT * this._hi;
  1874. hx = C - this._hi;
  1875. c = DD.SPLIT * yhi;
  1876. hx = C - hx;
  1877. tx = this._hi - hx;
  1878. hy = c - yhi;
  1879. C = this._hi * yhi;
  1880. hy = c - hy;
  1881. ty = yhi - hy;
  1882. c = hx * hy - C + hx * ty + tx * hy + tx * ty + (this._hi * ylo + this._lo * yhi);
  1883. const zhi = C + c;
  1884. hx = C - zhi;
  1885. const zlo = c + hx;
  1886. this._hi = zhi;
  1887. this._lo = zlo;
  1888. return this;
  1889. }
  1890. }
  1891. selfSqr() {
  1892. return this.selfMultiply(this);
  1893. }
  1894. floor() {
  1895. if (this.isNaN()) return DD.NaN;
  1896. const fhi = Math.floor(this._hi);
  1897. let flo = 0.0;
  1898. if (fhi === this._hi) flo = Math.floor(this._lo);
  1899. return new DD(fhi, flo);
  1900. }
  1901. negate() {
  1902. if (this.isNaN()) return this;
  1903. return new DD(-this._hi, -this._lo);
  1904. }
  1905. clone() {
  1906. try {
  1907. return null;
  1908. } catch (ex) {
  1909. if (ex instanceof CloneNotSupportedException) return null;else throw ex;
  1910. } finally {}
  1911. }
  1912. multiply() {
  1913. if (arguments[0] instanceof DD) {
  1914. const y = arguments[0];
  1915. if (y.isNaN()) return DD.createNaN();
  1916. return DD.copy(this).selfMultiply(y);
  1917. } else if (typeof arguments[0] === 'number') {
  1918. const y = arguments[0];
  1919. if (Double.isNaN(y)) return DD.createNaN();
  1920. return DD.copy(this).selfMultiply(y, 0.0);
  1921. }
  1922. }
  1923. isNaN() {
  1924. return Double.isNaN(this._hi);
  1925. }
  1926. intValue() {
  1927. return Math.trunc(this._hi);
  1928. }
  1929. toString() {
  1930. const mag = DD.magnitude(this._hi);
  1931. if (mag >= -3 && mag <= 20) return this.toStandardNotation();
  1932. return this.toSciNotation();
  1933. }
  1934. toStandardNotation() {
  1935. const specialStr = this.getSpecialNumberString();
  1936. if (specialStr !== null) return specialStr;
  1937. const magnitude = new Array(1).fill(null);
  1938. const sigDigits = this.extractSignificantDigits(true, magnitude);
  1939. const decimalPointPos = magnitude[0] + 1;
  1940. let num = sigDigits;
  1941. if (sigDigits.charAt(0) === '.') {
  1942. num = '0' + sigDigits;
  1943. } else if (decimalPointPos < 0) {
  1944. num = '0.' + DD.stringOfChar('0', -decimalPointPos) + sigDigits;
  1945. } else if (sigDigits.indexOf('.') === -1) {
  1946. const numZeroes = decimalPointPos - sigDigits.length;
  1947. const zeroes = DD.stringOfChar('0', numZeroes);
  1948. num = sigDigits + zeroes + '.0';
  1949. }
  1950. if (this.isNegative()) return '-' + num;
  1951. return num;
  1952. }
  1953. reciprocal() {
  1954. let hc = null,
  1955. tc = null,
  1956. hy = null,
  1957. ty = null,
  1958. C = null,
  1959. c = null,
  1960. U = null,
  1961. u = null;
  1962. C = 1.0 / this._hi;
  1963. c = DD.SPLIT * C;
  1964. hc = c - C;
  1965. u = DD.SPLIT * this._hi;
  1966. hc = c - hc;
  1967. tc = C - hc;
  1968. hy = u - this._hi;
  1969. U = C * this._hi;
  1970. hy = u - hy;
  1971. ty = this._hi - hy;
  1972. u = hc * hy - U + hc * ty + tc * hy + tc * ty;
  1973. c = (1.0 - U - u - C * this._lo) / this._hi;
  1974. const zhi = C + c;
  1975. const zlo = C - zhi + c;
  1976. return new DD(zhi, zlo);
  1977. }
  1978. toSciNotation() {
  1979. if (this.isZero()) return DD.SCI_NOT_ZERO;
  1980. const specialStr = this.getSpecialNumberString();
  1981. if (specialStr !== null) return specialStr;
  1982. const magnitude = new Array(1).fill(null);
  1983. const digits = this.extractSignificantDigits(false, magnitude);
  1984. const expStr = DD.SCI_NOT_EXPONENT_CHAR + magnitude[0];
  1985. if (digits.charAt(0) === '0') throw new IllegalStateException('Found leading zero: ' + digits);
  1986. let trailingDigits = '';
  1987. if (digits.length > 1) trailingDigits = digits.substring(1);
  1988. const digitsWithDecimal = digits.charAt(0) + '.' + trailingDigits;
  1989. if (this.isNegative()) return '-' + digitsWithDecimal + expStr;
  1990. return digitsWithDecimal + expStr;
  1991. }
  1992. abs() {
  1993. if (this.isNaN()) return DD.NaN;
  1994. if (this.isNegative()) return this.negate();
  1995. return new DD(this);
  1996. }
  1997. isPositive() {
  1998. return this._hi > 0.0 || this._hi === 0.0 && this._lo > 0.0;
  1999. }
  2000. lt(y) {
  2001. return this._hi < y._hi || this._hi === y._hi && this._lo < y._lo;
  2002. }
  2003. add() {
  2004. if (arguments[0] instanceof DD) {
  2005. const y = arguments[0];
  2006. return DD.copy(this).selfAdd(y);
  2007. } else if (typeof arguments[0] === 'number') {
  2008. const y = arguments[0];
  2009. return DD.copy(this).selfAdd(y);
  2010. }
  2011. }
  2012. init() {
  2013. if (arguments.length === 1) {
  2014. if (typeof arguments[0] === 'number') {
  2015. const x = arguments[0];
  2016. this._hi = x;
  2017. this._lo = 0.0;
  2018. } else if (arguments[0] instanceof DD) {
  2019. const dd = arguments[0];
  2020. this._hi = dd._hi;
  2021. this._lo = dd._lo;
  2022. }
  2023. } else if (arguments.length === 2) {
  2024. const hi = arguments[0],
  2025. lo = arguments[1];
  2026. this._hi = hi;
  2027. this._lo = lo;
  2028. }
  2029. }
  2030. gt(y) {
  2031. return this._hi > y._hi || this._hi === y._hi && this._lo > y._lo;
  2032. }
  2033. isNegative() {
  2034. return this._hi < 0.0 || this._hi === 0.0 && this._lo < 0.0;
  2035. }
  2036. trunc() {
  2037. if (this.isNaN()) return DD.NaN;
  2038. if (this.isPositive()) return this.floor();else return this.ceil();
  2039. }
  2040. signum() {
  2041. if (this._hi > 0) return 1;
  2042. if (this._hi < 0) return -1;
  2043. if (this._lo > 0) return 1;
  2044. if (this._lo < 0) return -1;
  2045. return 0;
  2046. }
  2047. get interfaces_() {
  2048. return [Serializable, Comparable, Clonable];
  2049. }
  2050. }
  2051. DD.PI = new DD(3.141592653589793116e+00, 1.224646799147353207e-16);
  2052. DD.TWO_PI = new DD(6.283185307179586232e+00, 2.449293598294706414e-16);
  2053. DD.PI_2 = new DD(1.570796326794896558e+00, 6.123233995736766036e-17);
  2054. DD.E = new DD(2.718281828459045091e+00, 1.445646891729250158e-16);
  2055. DD.NaN = new DD(Double.NaN, Double.NaN);
  2056. DD.EPS = 1.23259516440783e-32;
  2057. DD.SPLIT = 134217729.0;
  2058. DD.MAX_PRINT_DIGITS = 32;
  2059. DD.TEN = DD.valueOf(10.0);
  2060. DD.ONE = DD.valueOf(1.0);
  2061. DD.SCI_NOT_EXPONENT_CHAR = 'E';
  2062. DD.SCI_NOT_ZERO = '0.0E0';
  2063. class CGAlgorithmsDD {
  2064. static orientationIndex(p1, p2, q) {
  2065. const index = CGAlgorithmsDD.orientationIndexFilter(p1, p2, q);
  2066. if (index <= 1) return index;
  2067. const dx1 = DD.valueOf(p2.x).selfAdd(-p1.x);
  2068. const dy1 = DD.valueOf(p2.y).selfAdd(-p1.y);
  2069. const dx2 = DD.valueOf(q.x).selfAdd(-p2.x);
  2070. const dy2 = DD.valueOf(q.y).selfAdd(-p2.y);
  2071. return dx1.selfMultiply(dy2).selfSubtract(dy1.selfMultiply(dx2)).signum();
  2072. }
  2073. static signOfDet2x2() {
  2074. if (arguments[3] instanceof DD && arguments[2] instanceof DD && arguments[0] instanceof DD && arguments[1] instanceof DD) {
  2075. const x1 = arguments[0],
  2076. y1 = arguments[1],
  2077. x2 = arguments[2],
  2078. y2 = arguments[3];
  2079. const det = x1.multiply(y2).selfSubtract(y1.multiply(x2));
  2080. return det.signum();
  2081. } else if (typeof arguments[3] === 'number' && typeof arguments[2] === 'number' && typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  2082. const dx1 = arguments[0],
  2083. dy1 = arguments[1],
  2084. dx2 = arguments[2],
  2085. dy2 = arguments[3];
  2086. const x1 = DD.valueOf(dx1);
  2087. const y1 = DD.valueOf(dy1);
  2088. const x2 = DD.valueOf(dx2);
  2089. const y2 = DD.valueOf(dy2);
  2090. const det = x1.multiply(y2).selfSubtract(y1.multiply(x2));
  2091. return det.signum();
  2092. }
  2093. }
  2094. static intersection(p1, p2, q1, q2) {
  2095. const px = new DD(p1.y).selfSubtract(p2.y);
  2096. const py = new DD(p2.x).selfSubtract(p1.x);
  2097. const pw = new DD(p1.x).selfMultiply(p2.y).selfSubtract(new DD(p2.x).selfMultiply(p1.y));
  2098. const qx = new DD(q1.y).selfSubtract(q2.y);
  2099. const qy = new DD(q2.x).selfSubtract(q1.x);
  2100. const qw = new DD(q1.x).selfMultiply(q2.y).selfSubtract(new DD(q2.x).selfMultiply(q1.y));
  2101. const x = py.multiply(qw).selfSubtract(qy.multiply(pw));
  2102. const y = qx.multiply(pw).selfSubtract(px.multiply(qw));
  2103. const w = px.multiply(qy).selfSubtract(qx.multiply(py));
  2104. const xInt = x.selfDivide(w).doubleValue();
  2105. const yInt = y.selfDivide(w).doubleValue();
  2106. if (Double.isNaN(xInt) || Double.isInfinite(xInt) || Double.isNaN(yInt) || Double.isInfinite(yInt)) return null;
  2107. return new Coordinate(xInt, yInt);
  2108. }
  2109. static orientationIndexFilter(pa, pb, pc) {
  2110. let detsum = null;
  2111. const detleft = (pa.x - pc.x) * (pb.y - pc.y);
  2112. const detright = (pa.y - pc.y) * (pb.x - pc.x);
  2113. const det = detleft - detright;
  2114. if (detleft > 0.0) {
  2115. if (detright <= 0.0) return CGAlgorithmsDD.signum(det);else detsum = detleft + detright;
  2116. } else if (detleft < 0.0) {
  2117. if (detright >= 0.0) return CGAlgorithmsDD.signum(det);else detsum = -detleft - detright;
  2118. } else return CGAlgorithmsDD.signum(det);
  2119. const errbound = CGAlgorithmsDD.DP_SAFE_EPSILON * detsum;
  2120. if (det >= errbound || -det >= errbound) return CGAlgorithmsDD.signum(det);
  2121. return 2;
  2122. }
  2123. static signum(x) {
  2124. if (x > 0) return 1;
  2125. if (x < 0) return -1;
  2126. return 0;
  2127. }
  2128. }
  2129. CGAlgorithmsDD.DP_SAFE_EPSILON = 1e-15;
  2130. class CoordinateSequence {
  2131. getM(index) {
  2132. if (this.hasM()) {
  2133. const mIndex = this.getDimension() - this.getMeasures();
  2134. return this.getOrdinate(index, mIndex);
  2135. } else {
  2136. return Double.NaN;
  2137. }
  2138. }
  2139. setOrdinate(index, ordinateIndex, value) {}
  2140. getZ(index) {
  2141. if (this.hasZ()) return this.getOrdinate(index, 2);else return Double.NaN;
  2142. }
  2143. size() {}
  2144. getOrdinate(index, ordinateIndex) {}
  2145. getCoordinate() {
  2146. }
  2147. getCoordinateCopy(i) {}
  2148. createCoordinate() {}
  2149. getDimension() {}
  2150. hasM() {
  2151. return this.getMeasures() > 0;
  2152. }
  2153. getX(index) {}
  2154. hasZ() {
  2155. return this.getDimension() - this.getMeasures() > 2;
  2156. }
  2157. getMeasures() {
  2158. return 0;
  2159. }
  2160. expandEnvelope(env) {}
  2161. copy() {}
  2162. getY(index) {}
  2163. toCoordinateArray() {}
  2164. get interfaces_() {
  2165. return [Clonable];
  2166. }
  2167. }
  2168. CoordinateSequence.X = 0;
  2169. CoordinateSequence.Y = 1;
  2170. CoordinateSequence.Z = 2;
  2171. CoordinateSequence.M = 3;
  2172. class Orientation {
  2173. static index(p1, p2, q) {
  2174. return CGAlgorithmsDD.orientationIndex(p1, p2, q);
  2175. }
  2176. static isCCW() {
  2177. if (arguments[0] instanceof Array) {
  2178. const ring = arguments[0];
  2179. const nPts = ring.length - 1;
  2180. if (nPts < 3) throw new IllegalArgumentException('Ring has fewer than 4 points, so orientation cannot be determined');
  2181. let hiPt = ring[0];
  2182. let hiIndex = 0;
  2183. for (let i = 1; i <= nPts; i++) {
  2184. const p = ring[i];
  2185. if (p.y > hiPt.y) {
  2186. hiPt = p;
  2187. hiIndex = i;
  2188. }
  2189. }
  2190. let iPrev = hiIndex;
  2191. do {
  2192. iPrev = iPrev - 1;
  2193. if (iPrev < 0) iPrev = nPts;
  2194. } while (ring[iPrev].equals2D(hiPt) && iPrev !== hiIndex);
  2195. let iNext = hiIndex;
  2196. do iNext = (iNext + 1) % nPts; while (ring[iNext].equals2D(hiPt) && iNext !== hiIndex);
  2197. const prev = ring[iPrev];
  2198. const next = ring[iNext];
  2199. if (prev.equals2D(hiPt) || next.equals2D(hiPt) || prev.equals2D(next)) return false;
  2200. const disc = Orientation.index(prev, hiPt, next);
  2201. let isCCW = null;
  2202. if (disc === 0) isCCW = prev.x > next.x;else isCCW = disc > 0;
  2203. return isCCW;
  2204. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  2205. const ring = arguments[0];
  2206. const nPts = ring.size() - 1;
  2207. if (nPts < 3) throw new IllegalArgumentException('Ring has fewer than 4 points, so orientation cannot be determined');
  2208. let hiPt = ring.getCoordinate(0);
  2209. let hiIndex = 0;
  2210. for (let i = 1; i <= nPts; i++) {
  2211. const p = ring.getCoordinate(i);
  2212. if (p.y > hiPt.y) {
  2213. hiPt = p;
  2214. hiIndex = i;
  2215. }
  2216. }
  2217. let prev = null;
  2218. let iPrev = hiIndex;
  2219. do {
  2220. iPrev = iPrev - 1;
  2221. if (iPrev < 0) iPrev = nPts;
  2222. prev = ring.getCoordinate(iPrev);
  2223. } while (prev.equals2D(hiPt) && iPrev !== hiIndex);
  2224. let next = null;
  2225. let iNext = hiIndex;
  2226. do {
  2227. iNext = (iNext + 1) % nPts;
  2228. next = ring.getCoordinate(iNext);
  2229. } while (next.equals2D(hiPt) && iNext !== hiIndex);
  2230. if (prev.equals2D(hiPt) || next.equals2D(hiPt) || prev.equals2D(next)) return false;
  2231. const disc = Orientation.index(prev, hiPt, next);
  2232. let isCCW = null;
  2233. if (disc === 0) isCCW = prev.x > next.x;else isCCW = disc > 0;
  2234. return isCCW;
  2235. }
  2236. }
  2237. }
  2238. Orientation.CLOCKWISE = -1;
  2239. Orientation.RIGHT = Orientation.CLOCKWISE;
  2240. Orientation.COUNTERCLOCKWISE = 1;
  2241. Orientation.LEFT = Orientation.COUNTERCLOCKWISE;
  2242. Orientation.COLLINEAR = 0;
  2243. Orientation.STRAIGHT = Orientation.COLLINEAR;
  2244. class Intersection {
  2245. static intersection(p1, p2, q1, q2) {
  2246. const minX0 = p1.x < p2.x ? p1.x : p2.x;
  2247. const minY0 = p1.y < p2.y ? p1.y : p2.y;
  2248. const maxX0 = p1.x > p2.x ? p1.x : p2.x;
  2249. const maxY0 = p1.y > p2.y ? p1.y : p2.y;
  2250. const minX1 = q1.x < q2.x ? q1.x : q2.x;
  2251. const minY1 = q1.y < q2.y ? q1.y : q2.y;
  2252. const maxX1 = q1.x > q2.x ? q1.x : q2.x;
  2253. const maxY1 = q1.y > q2.y ? q1.y : q2.y;
  2254. const intMinX = minX0 > minX1 ? minX0 : minX1;
  2255. const intMaxX = maxX0 < maxX1 ? maxX0 : maxX1;
  2256. const intMinY = minY0 > minY1 ? minY0 : minY1;
  2257. const intMaxY = maxY0 < maxY1 ? maxY0 : maxY1;
  2258. const midx = (intMinX + intMaxX) / 2.0;
  2259. const midy = (intMinY + intMaxY) / 2.0;
  2260. const p1x = p1.x - midx;
  2261. const p1y = p1.y - midy;
  2262. const p2x = p2.x - midx;
  2263. const p2y = p2.y - midy;
  2264. const q1x = q1.x - midx;
  2265. const q1y = q1.y - midy;
  2266. const q2x = q2.x - midx;
  2267. const q2y = q2.y - midy;
  2268. const px = p1y - p2y;
  2269. const py = p2x - p1x;
  2270. const pw = p1x * p2y - p2x * p1y;
  2271. const qx = q1y - q2y;
  2272. const qy = q2x - q1x;
  2273. const qw = q1x * q2y - q2x * q1y;
  2274. const x = py * qw - qy * pw;
  2275. const y = qx * pw - px * qw;
  2276. const w = px * qy - qx * py;
  2277. const xInt = x / w;
  2278. const yInt = y / w;
  2279. if (Double.isNaN(xInt) || Double.isInfinite(xInt) || Double.isNaN(yInt) || Double.isInfinite(yInt)) return null;
  2280. return new Coordinate(xInt + midx, yInt + midy);
  2281. }
  2282. }
  2283. class System {
  2284. static arraycopy(src, srcPos, dest, destPos, len) {
  2285. let c = 0;
  2286. for (let i = srcPos; i < srcPos + len; i++) {
  2287. dest[destPos + c] = src[i];
  2288. c++;
  2289. }
  2290. }
  2291. static getProperty(name) {
  2292. return {
  2293. 'line.separator': '\n'
  2294. }[name];
  2295. }
  2296. }
  2297. class MathUtil {
  2298. static log10(x) {
  2299. const ln = Math.log(x);
  2300. if (Double.isInfinite(ln)) return ln;
  2301. if (Double.isNaN(ln)) return ln;
  2302. return ln / MathUtil.LOG_10;
  2303. }
  2304. static min(v1, v2, v3, v4) {
  2305. let min = v1;
  2306. if (v2 < min) min = v2;
  2307. if (v3 < min) min = v3;
  2308. if (v4 < min) min = v4;
  2309. return min;
  2310. }
  2311. static clamp() {
  2312. if (typeof arguments[2] === 'number' && typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  2313. const x = arguments[0],
  2314. min = arguments[1],
  2315. max = arguments[2];
  2316. if (x < min) return min;
  2317. if (x > max) return max;
  2318. return x;
  2319. } else if (Number.isInteger(arguments[2]) && Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])) {
  2320. const x = arguments[0],
  2321. min = arguments[1],
  2322. max = arguments[2];
  2323. if (x < min) return min;
  2324. if (x > max) return max;
  2325. return x;
  2326. }
  2327. }
  2328. static wrap(index, max) {
  2329. if (index < 0) return max - -index % max;
  2330. return index % max;
  2331. }
  2332. static max() {
  2333. if (arguments.length === 3) {
  2334. const v1 = arguments[0],
  2335. v2 = arguments[1],
  2336. v3 = arguments[2];
  2337. let max = v1;
  2338. if (v2 > max) max = v2;
  2339. if (v3 > max) max = v3;
  2340. return max;
  2341. } else if (arguments.length === 4) {
  2342. const v1 = arguments[0],
  2343. v2 = arguments[1],
  2344. v3 = arguments[2],
  2345. v4 = arguments[3];
  2346. let max = v1;
  2347. if (v2 > max) max = v2;
  2348. if (v3 > max) max = v3;
  2349. if (v4 > max) max = v4;
  2350. return max;
  2351. }
  2352. }
  2353. static average(x1, x2) {
  2354. return (x1 + x2) / 2.0;
  2355. }
  2356. }
  2357. MathUtil.LOG_10 = Math.log(10);
  2358. class Distance {
  2359. static segmentToSegment(A, B, C, D) {
  2360. if (A.equals(B)) return Distance.pointToSegment(A, C, D);
  2361. if (C.equals(D)) return Distance.pointToSegment(D, A, B);
  2362. let noIntersection = false;
  2363. if (!Envelope.intersects(A, B, C, D)) {
  2364. noIntersection = true;
  2365. } else {
  2366. const denom = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);
  2367. if (denom === 0) {
  2368. noIntersection = true;
  2369. } else {
  2370. const r_num = (A.y - C.y) * (D.x - C.x) - (A.x - C.x) * (D.y - C.y);
  2371. const s_num = (A.y - C.y) * (B.x - A.x) - (A.x - C.x) * (B.y - A.y);
  2372. const s = s_num / denom;
  2373. const r = r_num / denom;
  2374. if (r < 0 || r > 1 || s < 0 || s > 1) noIntersection = true;
  2375. }
  2376. }
  2377. if (noIntersection) return MathUtil.min(Distance.pointToSegment(A, C, D), Distance.pointToSegment(B, C, D), Distance.pointToSegment(C, A, B), Distance.pointToSegment(D, A, B));
  2378. return 0.0;
  2379. }
  2380. static pointToSegment(p, A, B) {
  2381. if (A.x === B.x && A.y === B.y) return p.distance(A);
  2382. const len2 = (B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y);
  2383. const r = ((p.x - A.x) * (B.x - A.x) + (p.y - A.y) * (B.y - A.y)) / len2;
  2384. if (r <= 0.0) return p.distance(A);
  2385. if (r >= 1.0) return p.distance(B);
  2386. const s = ((A.y - p.y) * (B.x - A.x) - (A.x - p.x) * (B.y - A.y)) / len2;
  2387. return Math.abs(s) * Math.sqrt(len2);
  2388. }
  2389. static pointToLinePerpendicular(p, A, B) {
  2390. const len2 = (B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y);
  2391. const s = ((A.y - p.y) * (B.x - A.x) - (A.x - p.x) * (B.y - A.y)) / len2;
  2392. return Math.abs(s) * Math.sqrt(len2);
  2393. }
  2394. static pointToSegmentString(p, line) {
  2395. if (line.length === 0) throw new IllegalArgumentException('Line array must contain at least one vertex');
  2396. let minDistance = p.distance(line[0]);
  2397. for (let i = 0; i < line.length - 1; i++) {
  2398. const dist = Distance.pointToSegment(p, line[i], line[i + 1]);
  2399. if (dist < minDistance) minDistance = dist;
  2400. }
  2401. return minDistance;
  2402. }
  2403. }
  2404. class CoordinateSequenceFactory {
  2405. create() {
  2406. if (arguments.length === 1) {
  2407. if (arguments[0] instanceof Array) ; else if (hasInterface(arguments[0], CoordinateSequence)) ;
  2408. } else if (arguments.length === 2) ; else if (arguments.length === 3) {
  2409. const size = arguments[0],
  2410. dimension = arguments[1];
  2411. return this.create(size, dimension);
  2412. }
  2413. }
  2414. }
  2415. class GeometryComponentFilter {
  2416. filter(geom) {}
  2417. }
  2418. class Geometry {
  2419. constructor() {
  2420. Geometry.constructor_.apply(this, arguments);
  2421. }
  2422. isGeometryCollection() {
  2423. return this.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION;
  2424. }
  2425. getFactory() {
  2426. return this._factory;
  2427. }
  2428. getGeometryN(n) {
  2429. return this;
  2430. }
  2431. getArea() {
  2432. return 0.0;
  2433. }
  2434. isRectangle() {
  2435. return false;
  2436. }
  2437. equalsExact(other) {
  2438. return this === other || this.equalsExact(other, 0);
  2439. }
  2440. geometryChanged() {
  2441. this.apply(Geometry.geometryChangedFilter);
  2442. }
  2443. geometryChangedAction() {
  2444. this._envelope = null;
  2445. }
  2446. equalsNorm(g) {
  2447. if (g === null) return false;
  2448. return this.norm().equalsExact(g.norm());
  2449. }
  2450. getLength() {
  2451. return 0.0;
  2452. }
  2453. getNumGeometries() {
  2454. return 1;
  2455. }
  2456. compareTo() {
  2457. let other;
  2458. if (arguments.length === 1) {
  2459. const o = arguments[0];
  2460. other = o;
  2461. if (this.getTypeCode() !== other.getTypeCode()) return this.getTypeCode() - other.getTypeCode();
  2462. if (this.isEmpty() && other.isEmpty()) return 0;
  2463. if (this.isEmpty()) return -1;
  2464. if (other.isEmpty()) return 1;
  2465. return this.compareToSameClass(o);
  2466. } else if (arguments.length === 2) {
  2467. const o = arguments[0];
  2468. const comp = arguments[1];
  2469. other = o;
  2470. if (this.getTypeCode() !== other.getTypeCode()) return this.getTypeCode() - other.getTypeCode();
  2471. if (this.isEmpty() && other.isEmpty()) return 0;
  2472. if (this.isEmpty()) return -1;
  2473. if (other.isEmpty()) return 1;
  2474. return this.compareToSameClass(o, comp);
  2475. }
  2476. }
  2477. getUserData() {
  2478. return this._userData;
  2479. }
  2480. getSRID() {
  2481. return this._SRID;
  2482. }
  2483. getEnvelope() {
  2484. return this.getFactory().toGeometry(this.getEnvelopeInternal());
  2485. }
  2486. checkNotGeometryCollection(g) {
  2487. if (g.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION) throw new IllegalArgumentException('This method does not support GeometryCollection arguments');
  2488. }
  2489. equal(a, b, tolerance) {
  2490. if (tolerance === 0) return a.equals(b);
  2491. return a.distance(b) <= tolerance;
  2492. }
  2493. norm() {
  2494. const copy = this.copy();
  2495. copy.normalize();
  2496. return copy;
  2497. }
  2498. reverse() {
  2499. const res = this.reverseInternal();
  2500. if (this.envelope != null) res.envelope = this.envelope.copy();
  2501. res.setSRID(this.getSRID());
  2502. return res;
  2503. }
  2504. copy() {
  2505. const copy = this.copyInternal();
  2506. copy.envelope = this._envelope == null ? null : this._envelope.copy();
  2507. copy._SRID = this._SRID;
  2508. copy._userData = this._userData;
  2509. return copy;
  2510. }
  2511. getPrecisionModel() {
  2512. return this._factory.getPrecisionModel();
  2513. }
  2514. getEnvelopeInternal() {
  2515. if (this._envelope === null) this._envelope = this.computeEnvelopeInternal();
  2516. return new Envelope(this._envelope);
  2517. }
  2518. setSRID(SRID) {
  2519. this._SRID = SRID;
  2520. }
  2521. setUserData(userData) {
  2522. this._userData = userData;
  2523. }
  2524. compare(a, b) {
  2525. const i = a.iterator();
  2526. const j = b.iterator();
  2527. while (i.hasNext() && j.hasNext()) {
  2528. const aElement = i.next();
  2529. const bElement = j.next();
  2530. const comparison = aElement.compareTo(bElement);
  2531. if (comparison !== 0) return comparison;
  2532. }
  2533. if (i.hasNext()) return 1;
  2534. if (j.hasNext()) return -1;
  2535. return 0;
  2536. }
  2537. hashCode() {
  2538. return this.getEnvelopeInternal().hashCode();
  2539. }
  2540. isEquivalentClass(other) {
  2541. return this.getClass() === other.getClass();
  2542. }
  2543. isGeometryCollectionOrDerived() {
  2544. if (this.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION || this.getTypeCode() === Geometry.TYPECODE_MULTIPOINT || this.getTypeCode() === Geometry.TYPECODE_MULTILINESTRING || this.getTypeCode() === Geometry.TYPECODE_MULTIPOLYGON) return true;
  2545. return false;
  2546. }
  2547. get interfaces_() {
  2548. return [Clonable, Comparable, Serializable];
  2549. }
  2550. getClass() {
  2551. return Geometry;
  2552. }
  2553. static hasNonEmptyElements(geometries) {
  2554. for (let i = 0; i < geometries.length; i++) if (!geometries[i].isEmpty()) return true;
  2555. return false;
  2556. }
  2557. static hasNullElements(array) {
  2558. for (let i = 0; i < array.length; i++) if (array[i] === null) return true;
  2559. return false;
  2560. }
  2561. }
  2562. Geometry.constructor_ = function (factory) {
  2563. if (!factory) return;
  2564. this._envelope = null;
  2565. this._userData = null;
  2566. this._factory = factory;
  2567. this._SRID = factory.getSRID();
  2568. };
  2569. Geometry.TYPECODE_POINT = 0;
  2570. Geometry.TYPECODE_MULTIPOINT = 1;
  2571. Geometry.TYPECODE_LINESTRING = 2;
  2572. Geometry.TYPECODE_LINEARRING = 3;
  2573. Geometry.TYPECODE_MULTILINESTRING = 4;
  2574. Geometry.TYPECODE_POLYGON = 5;
  2575. Geometry.TYPECODE_MULTIPOLYGON = 6;
  2576. Geometry.TYPECODE_GEOMETRYCOLLECTION = 7;
  2577. Geometry.TYPENAME_POINT = 'Point';
  2578. Geometry.TYPENAME_MULTIPOINT = 'MultiPoint';
  2579. Geometry.TYPENAME_LINESTRING = 'LineString';
  2580. Geometry.TYPENAME_LINEARRING = 'LinearRing';
  2581. Geometry.TYPENAME_MULTILINESTRING = 'MultiLineString';
  2582. Geometry.TYPENAME_POLYGON = 'Polygon';
  2583. Geometry.TYPENAME_MULTIPOLYGON = 'MultiPolygon';
  2584. Geometry.TYPENAME_GEOMETRYCOLLECTION = 'GeometryCollection';
  2585. Geometry.geometryChangedFilter = {
  2586. get interfaces_() {
  2587. return [GeometryComponentFilter];
  2588. },
  2589. filter(geom) {
  2590. geom.geometryChangedAction();
  2591. }
  2592. };
  2593. class CoordinateFilter {
  2594. filter(coord) {}
  2595. }
  2596. class Length {
  2597. static ofLine(pts) {
  2598. const n = pts.size();
  2599. if (n <= 1) return 0.0;
  2600. let len = 0.0;
  2601. const p = new Coordinate();
  2602. pts.getCoordinate(0, p);
  2603. let x0 = p.x;
  2604. let y0 = p.y;
  2605. for (let i = 1; i < n; i++) {
  2606. pts.getCoordinate(i, p);
  2607. const x1 = p.x;
  2608. const y1 = p.y;
  2609. const dx = x1 - x0;
  2610. const dy = y1 - y0;
  2611. len += Math.sqrt(dx * dx + dy * dy);
  2612. x0 = x1;
  2613. y0 = y1;
  2614. }
  2615. return len;
  2616. }
  2617. }
  2618. class Lineal {}
  2619. class CoordinateSequences {
  2620. static copyCoord(src, srcPos, dest, destPos) {
  2621. const minDim = Math.min(src.getDimension(), dest.getDimension());
  2622. for (let dim = 0; dim < minDim; dim++) dest.setOrdinate(destPos, dim, src.getOrdinate(srcPos, dim));
  2623. }
  2624. static isRing(seq) {
  2625. const n = seq.size();
  2626. if (n === 0) return true;
  2627. if (n <= 3) return false;
  2628. return seq.getOrdinate(0, CoordinateSequence.X) === seq.getOrdinate(n - 1, CoordinateSequence.X) && seq.getOrdinate(0, CoordinateSequence.Y) === seq.getOrdinate(n - 1, CoordinateSequence.Y);
  2629. }
  2630. static scroll() {
  2631. if (arguments.length === 2) {
  2632. if (hasInterface(arguments[0], CoordinateSequence) && Number.isInteger(arguments[1])) {
  2633. const seq = arguments[0],
  2634. indexOfFirstCoordinate = arguments[1];
  2635. CoordinateSequences.scroll(seq, indexOfFirstCoordinate, CoordinateSequences.isRing(seq));
  2636. } else if (hasInterface(arguments[0], CoordinateSequence) && arguments[1] instanceof Coordinate) {
  2637. const seq = arguments[0],
  2638. firstCoordinate = arguments[1];
  2639. const i = CoordinateSequences.indexOf(firstCoordinate, seq);
  2640. if (i <= 0) return null;
  2641. CoordinateSequences.scroll(seq, i);
  2642. }
  2643. } else if (arguments.length === 3) {
  2644. const seq = arguments[0],
  2645. indexOfFirstCoordinate = arguments[1],
  2646. ensureRing = arguments[2];
  2647. const i = indexOfFirstCoordinate;
  2648. if (i <= 0) return null;
  2649. const copy = seq.copy();
  2650. const last = ensureRing ? seq.size() - 1 : seq.size();
  2651. for (let j = 0; j < last; j++) for (let k = 0; k < seq.getDimension(); k++) seq.setOrdinate(j, k, copy.getOrdinate((indexOfFirstCoordinate + j) % last, k));
  2652. if (ensureRing) for (let k = 0; k < seq.getDimension(); k++) seq.setOrdinate(last, k, seq.getOrdinate(0, k));
  2653. }
  2654. }
  2655. static isEqual(cs1, cs2) {
  2656. const cs1Size = cs1.size();
  2657. const cs2Size = cs2.size();
  2658. if (cs1Size !== cs2Size) return false;
  2659. const dim = Math.min(cs1.getDimension(), cs2.getDimension());
  2660. for (let i = 0; i < cs1Size; i++) for (let d = 0; d < dim; d++) {
  2661. const v1 = cs1.getOrdinate(i, d);
  2662. const v2 = cs2.getOrdinate(i, d);
  2663. if (cs1.getOrdinate(i, d) === cs2.getOrdinate(i, d)) continue;
  2664. if (Double.isNaN(v1) && Double.isNaN(v2)) continue;
  2665. return false;
  2666. }
  2667. return true;
  2668. }
  2669. static minCoordinateIndex() {
  2670. if (arguments.length === 1) {
  2671. const seq = arguments[0];
  2672. return CoordinateSequences.minCoordinateIndex(seq, 0, seq.size() - 1);
  2673. } else if (arguments.length === 3) {
  2674. const seq = arguments[0],
  2675. from = arguments[1],
  2676. to = arguments[2];
  2677. let minCoordIndex = -1;
  2678. let minCoord = null;
  2679. for (let i = from; i <= to; i++) {
  2680. const testCoord = seq.getCoordinate(i);
  2681. if (minCoord === null || minCoord.compareTo(testCoord) > 0) {
  2682. minCoord = testCoord;
  2683. minCoordIndex = i;
  2684. }
  2685. }
  2686. return minCoordIndex;
  2687. }
  2688. }
  2689. static extend(fact, seq, size) {
  2690. const newseq = fact.create(size, seq.getDimension());
  2691. const n = seq.size();
  2692. CoordinateSequences.copy(seq, 0, newseq, 0, n);
  2693. if (n > 0) for (let i = n; i < size; i++) CoordinateSequences.copy(seq, n - 1, newseq, i, 1);
  2694. return newseq;
  2695. }
  2696. static reverse(seq) {
  2697. const last = seq.size() - 1;
  2698. const mid = Math.trunc(last / 2);
  2699. for (let i = 0; i <= mid; i++) CoordinateSequences.swap(seq, i, last - i);
  2700. }
  2701. static swap(seq, i, j) {
  2702. if (i === j) return null;
  2703. for (let dim = 0; dim < seq.getDimension(); dim++) {
  2704. const tmp = seq.getOrdinate(i, dim);
  2705. seq.setOrdinate(i, dim, seq.getOrdinate(j, dim));
  2706. seq.setOrdinate(j, dim, tmp);
  2707. }
  2708. }
  2709. static copy(src, srcPos, dest, destPos, length) {
  2710. for (let i = 0; i < length; i++) CoordinateSequences.copyCoord(src, srcPos + i, dest, destPos + i);
  2711. }
  2712. static ensureValidRing(fact, seq) {
  2713. const n = seq.size();
  2714. if (n === 0) return seq;
  2715. if (n <= 3) return CoordinateSequences.createClosedRing(fact, seq, 4);
  2716. const isClosed = seq.getOrdinate(0, CoordinateSequence.X) === seq.getOrdinate(n - 1, CoordinateSequence.X) && seq.getOrdinate(0, CoordinateSequence.Y) === seq.getOrdinate(n - 1, CoordinateSequence.Y);
  2717. if (isClosed) return seq;
  2718. return CoordinateSequences.createClosedRing(fact, seq, n + 1);
  2719. }
  2720. static indexOf(coordinate, seq) {
  2721. for (let i = 0; i < seq.size(); i++) if (coordinate.x === seq.getOrdinate(i, CoordinateSequence.X) && coordinate.y === seq.getOrdinate(i, CoordinateSequence.Y)) return i;
  2722. return -1;
  2723. }
  2724. static createClosedRing(fact, seq, size) {
  2725. const newseq = fact.create(size, seq.getDimension());
  2726. const n = seq.size();
  2727. CoordinateSequences.copy(seq, 0, newseq, 0, n);
  2728. for (let i = n; i < size; i++) CoordinateSequences.copy(seq, 0, newseq, i, 1);
  2729. return newseq;
  2730. }
  2731. static minCoordinate(seq) {
  2732. let minCoord = null;
  2733. for (let i = 0; i < seq.size(); i++) {
  2734. const testCoord = seq.getCoordinate(i);
  2735. if (minCoord === null || minCoord.compareTo(testCoord) > 0) minCoord = testCoord;
  2736. }
  2737. return minCoord;
  2738. }
  2739. }
  2740. class UnsupportedOperationException extends Exception {
  2741. constructor(message) {
  2742. super(message);
  2743. this.name = Object.keys({
  2744. UnsupportedOperationException
  2745. })[0];
  2746. }
  2747. }
  2748. class Dimension {
  2749. static toDimensionSymbol(dimensionValue) {
  2750. switch (dimensionValue) {
  2751. case Dimension.FALSE:
  2752. return Dimension.SYM_FALSE;
  2753. case Dimension.TRUE:
  2754. return Dimension.SYM_TRUE;
  2755. case Dimension.DONTCARE:
  2756. return Dimension.SYM_DONTCARE;
  2757. case Dimension.P:
  2758. return Dimension.SYM_P;
  2759. case Dimension.L:
  2760. return Dimension.SYM_L;
  2761. case Dimension.A:
  2762. return Dimension.SYM_A;
  2763. }
  2764. throw new IllegalArgumentException('Unknown dimension value: ' + dimensionValue);
  2765. }
  2766. static toDimensionValue(dimensionSymbol) {
  2767. switch (Character.toUpperCase(dimensionSymbol)) {
  2768. case Dimension.SYM_FALSE:
  2769. return Dimension.FALSE;
  2770. case Dimension.SYM_TRUE:
  2771. return Dimension.TRUE;
  2772. case Dimension.SYM_DONTCARE:
  2773. return Dimension.DONTCARE;
  2774. case Dimension.SYM_P:
  2775. return Dimension.P;
  2776. case Dimension.SYM_L:
  2777. return Dimension.L;
  2778. case Dimension.SYM_A:
  2779. return Dimension.A;
  2780. }
  2781. throw new IllegalArgumentException('Unknown dimension symbol: ' + dimensionSymbol);
  2782. }
  2783. }
  2784. Dimension.P = 0;
  2785. Dimension.L = 1;
  2786. Dimension.A = 2;
  2787. Dimension.FALSE = -1;
  2788. Dimension.TRUE = -2;
  2789. Dimension.DONTCARE = -3;
  2790. Dimension.SYM_FALSE = 'F';
  2791. Dimension.SYM_TRUE = 'T';
  2792. Dimension.SYM_DONTCARE = '*';
  2793. Dimension.SYM_P = '0';
  2794. Dimension.SYM_L = '1';
  2795. Dimension.SYM_A = '2';
  2796. class GeometryFilter {
  2797. filter(geom) {}
  2798. }
  2799. class LineString extends Geometry {
  2800. constructor() {
  2801. super();
  2802. LineString.constructor_.apply(this, arguments);
  2803. }
  2804. static constructor_() {
  2805. this._points = null;
  2806. if (arguments.length === 0) ; else if (arguments.length === 2) {
  2807. const points = arguments[0],
  2808. factory = arguments[1];
  2809. Geometry.constructor_.call(this, factory);
  2810. this.init(points);
  2811. }
  2812. }
  2813. computeEnvelopeInternal() {
  2814. if (this.isEmpty()) return new Envelope();
  2815. return this._points.expandEnvelope(new Envelope());
  2816. }
  2817. isRing() {
  2818. return this.isClosed() && this.isSimple();
  2819. }
  2820. getCoordinates() {
  2821. return this._points.toCoordinateArray();
  2822. }
  2823. copyInternal() {
  2824. return new LineString(this._points.copy(), this._factory);
  2825. }
  2826. equalsExact() {
  2827. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  2828. const other = arguments[0],
  2829. tolerance = arguments[1];
  2830. if (!this.isEquivalentClass(other)) return false;
  2831. const otherLineString = other;
  2832. if (this._points.size() !== otherLineString._points.size()) return false;
  2833. for (let i = 0; i < this._points.size(); i++) if (!this.equal(this._points.getCoordinate(i), otherLineString._points.getCoordinate(i), tolerance)) return false;
  2834. return true;
  2835. } else {
  2836. return super.equalsExact.apply(this, arguments);
  2837. }
  2838. }
  2839. normalize() {
  2840. for (let i = 0; i < Math.trunc(this._points.size() / 2); i++) {
  2841. const j = this._points.size() - 1 - i;
  2842. if (!this._points.getCoordinate(i).equals(this._points.getCoordinate(j))) {
  2843. if (this._points.getCoordinate(i).compareTo(this._points.getCoordinate(j)) > 0) {
  2844. const copy = this._points.copy();
  2845. CoordinateSequences.reverse(copy);
  2846. this._points = copy;
  2847. }
  2848. return null;
  2849. }
  2850. }
  2851. }
  2852. getCoordinate() {
  2853. if (this.isEmpty()) return null;
  2854. return this._points.getCoordinate(0);
  2855. }
  2856. getBoundaryDimension() {
  2857. if (this.isClosed()) return Dimension.FALSE;
  2858. return 0;
  2859. }
  2860. isClosed() {
  2861. if (this.isEmpty()) return false;
  2862. return this.getCoordinateN(0).equals2D(this.getCoordinateN(this.getNumPoints() - 1));
  2863. }
  2864. reverseInternal() {
  2865. const seq = this._points.copy();
  2866. CoordinateSequences.reverse(seq);
  2867. return this.getFactory().createLineString(seq);
  2868. }
  2869. getEndPoint() {
  2870. if (this.isEmpty()) return null;
  2871. return this.getPointN(this.getNumPoints() - 1);
  2872. }
  2873. getTypeCode() {
  2874. return Geometry.TYPECODE_LINESTRING;
  2875. }
  2876. getDimension() {
  2877. return 1;
  2878. }
  2879. getLength() {
  2880. return Length.ofLine(this._points);
  2881. }
  2882. getNumPoints() {
  2883. return this._points.size();
  2884. }
  2885. compareToSameClass() {
  2886. if (arguments.length === 1) {
  2887. const o = arguments[0];
  2888. const line = o;
  2889. let i = 0;
  2890. let j = 0;
  2891. while (i < this._points.size() && j < line._points.size()) {
  2892. const comparison = this._points.getCoordinate(i).compareTo(line._points.getCoordinate(j));
  2893. if (comparison !== 0) return comparison;
  2894. i++;
  2895. j++;
  2896. }
  2897. if (i < this._points.size()) return 1;
  2898. if (j < line._points.size()) return -1;
  2899. return 0;
  2900. } else if (arguments.length === 2) {
  2901. const o = arguments[0],
  2902. comp = arguments[1];
  2903. const line = o;
  2904. return comp.compare(this._points, line._points);
  2905. }
  2906. }
  2907. apply() {
  2908. if (hasInterface(arguments[0], CoordinateFilter)) {
  2909. const filter = arguments[0];
  2910. for (let i = 0; i < this._points.size(); i++) filter.filter(this._points.getCoordinate(i));
  2911. } else if (hasInterface(arguments[0], CoordinateSequenceFilter)) {
  2912. const filter = arguments[0];
  2913. if (this._points.size() === 0) return null;
  2914. for (let i = 0; i < this._points.size(); i++) {
  2915. filter.filter(this._points, i);
  2916. if (filter.isDone()) break;
  2917. }
  2918. if (filter.isGeometryChanged()) this.geometryChanged();
  2919. } else if (hasInterface(arguments[0], GeometryFilter)) {
  2920. const filter = arguments[0];
  2921. filter.filter(this);
  2922. } else if (hasInterface(arguments[0], GeometryComponentFilter)) {
  2923. const filter = arguments[0];
  2924. filter.filter(this);
  2925. }
  2926. }
  2927. getBoundary() {
  2928. throw new UnsupportedOperationException();
  2929. }
  2930. isEquivalentClass(other) {
  2931. return other instanceof LineString;
  2932. }
  2933. getCoordinateN(n) {
  2934. return this._points.getCoordinate(n);
  2935. }
  2936. getGeometryType() {
  2937. return Geometry.TYPENAME_LINESTRING;
  2938. }
  2939. getCoordinateSequence() {
  2940. return this._points;
  2941. }
  2942. isEmpty() {
  2943. return this._points.size() === 0;
  2944. }
  2945. init(points) {
  2946. if (points === null) points = this.getFactory().getCoordinateSequenceFactory().create([]);
  2947. if (points.size() === 1) throw new IllegalArgumentException('Invalid number of points in LineString (found ' + points.size() + ' - must be 0 or >= 2)');
  2948. this._points = points;
  2949. }
  2950. isCoordinate(pt) {
  2951. for (let i = 0; i < this._points.size(); i++) if (this._points.getCoordinate(i).equals(pt)) return true;
  2952. return false;
  2953. }
  2954. getStartPoint() {
  2955. if (this.isEmpty()) return null;
  2956. return this.getPointN(0);
  2957. }
  2958. getPointN(n) {
  2959. return this.getFactory().createPoint(this._points.getCoordinate(n));
  2960. }
  2961. get interfaces_() {
  2962. return [Lineal];
  2963. }
  2964. }
  2965. class Puntal {}
  2966. class Point extends Geometry {
  2967. constructor() {
  2968. super();
  2969. Point.constructor_.apply(this, arguments);
  2970. }
  2971. static constructor_() {
  2972. this._coordinates = null;
  2973. const coordinates = arguments[0],
  2974. factory = arguments[1];
  2975. Geometry.constructor_.call(this, factory);
  2976. this.init(coordinates);
  2977. }
  2978. computeEnvelopeInternal() {
  2979. if (this.isEmpty()) return new Envelope();
  2980. const env = new Envelope();
  2981. env.expandToInclude(this._coordinates.getX(0), this._coordinates.getY(0));
  2982. return env;
  2983. }
  2984. getCoordinates() {
  2985. return this.isEmpty() ? [] : [this.getCoordinate()];
  2986. }
  2987. copyInternal() {
  2988. return new Point(this._coordinates.copy(), this._factory);
  2989. }
  2990. equalsExact() {
  2991. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  2992. const other = arguments[0],
  2993. tolerance = arguments[1];
  2994. if (!this.isEquivalentClass(other)) return false;
  2995. if (this.isEmpty() && other.isEmpty()) return true;
  2996. if (this.isEmpty() !== other.isEmpty()) return false;
  2997. return this.equal(other.getCoordinate(), this.getCoordinate(), tolerance);
  2998. } else {
  2999. return super.equalsExact.apply(this, arguments);
  3000. }
  3001. }
  3002. normalize() {}
  3003. getCoordinate() {
  3004. return this._coordinates.size() !== 0 ? this._coordinates.getCoordinate(0) : null;
  3005. }
  3006. getBoundaryDimension() {
  3007. return Dimension.FALSE;
  3008. }
  3009. reverseInternal() {
  3010. return this.getFactory().createPoint(this._coordinates.copy());
  3011. }
  3012. getTypeCode() {
  3013. return Geometry.TYPECODE_POINT;
  3014. }
  3015. getDimension() {
  3016. return 0;
  3017. }
  3018. getNumPoints() {
  3019. return this.isEmpty() ? 0 : 1;
  3020. }
  3021. getX() {
  3022. if (this.getCoordinate() === null) throw new IllegalStateException('getX called on empty Point');
  3023. return this.getCoordinate().x;
  3024. }
  3025. compareToSameClass() {
  3026. if (arguments.length === 1) {
  3027. const other = arguments[0];
  3028. const point = other;
  3029. return this.getCoordinate().compareTo(point.getCoordinate());
  3030. } else if (arguments.length === 2) {
  3031. const other = arguments[0],
  3032. comp = arguments[1];
  3033. const point = other;
  3034. return comp.compare(this._coordinates, point._coordinates);
  3035. }
  3036. }
  3037. apply() {
  3038. if (hasInterface(arguments[0], CoordinateFilter)) {
  3039. const filter = arguments[0];
  3040. if (this.isEmpty()) return null;
  3041. filter.filter(this.getCoordinate());
  3042. } else if (hasInterface(arguments[0], CoordinateSequenceFilter)) {
  3043. const filter = arguments[0];
  3044. if (this.isEmpty()) return null;
  3045. filter.filter(this._coordinates, 0);
  3046. if (filter.isGeometryChanged()) this.geometryChanged();
  3047. } else if (hasInterface(arguments[0], GeometryFilter)) {
  3048. const filter = arguments[0];
  3049. filter.filter(this);
  3050. } else if (hasInterface(arguments[0], GeometryComponentFilter)) {
  3051. const filter = arguments[0];
  3052. filter.filter(this);
  3053. }
  3054. }
  3055. getBoundary() {
  3056. return this.getFactory().createGeometryCollection();
  3057. }
  3058. getGeometryType() {
  3059. return Geometry.TYPENAME_POINT;
  3060. }
  3061. getCoordinateSequence() {
  3062. return this._coordinates;
  3063. }
  3064. getY() {
  3065. if (this.getCoordinate() === null) throw new IllegalStateException('getY called on empty Point');
  3066. return this.getCoordinate().y;
  3067. }
  3068. isEmpty() {
  3069. return this._coordinates.size() === 0;
  3070. }
  3071. init(coordinates) {
  3072. if (coordinates === null) coordinates = this.getFactory().getCoordinateSequenceFactory().create([]);
  3073. Assert.isTrue(coordinates.size() <= 1);
  3074. this._coordinates = coordinates;
  3075. }
  3076. isSimple() {
  3077. return true;
  3078. }
  3079. get interfaces_() {
  3080. return [Puntal];
  3081. }
  3082. }
  3083. class Area {
  3084. static ofRing() {
  3085. if (arguments[0] instanceof Array) {
  3086. const ring = arguments[0];
  3087. return Math.abs(Area.ofRingSigned(ring));
  3088. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  3089. const ring = arguments[0];
  3090. return Math.abs(Area.ofRingSigned(ring));
  3091. }
  3092. }
  3093. static ofRingSigned() {
  3094. if (arguments[0] instanceof Array) {
  3095. const ring = arguments[0];
  3096. if (ring.length < 3) return 0.0;
  3097. let sum = 0.0;
  3098. const x0 = ring[0].x;
  3099. for (let i = 1; i < ring.length - 1; i++) {
  3100. const x = ring[i].x - x0;
  3101. const y1 = ring[i + 1].y;
  3102. const y2 = ring[i - 1].y;
  3103. sum += x * (y2 - y1);
  3104. }
  3105. return sum / 2.0;
  3106. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  3107. const ring = arguments[0];
  3108. const n = ring.size();
  3109. if (n < 3) return 0.0;
  3110. const p0 = new Coordinate();
  3111. const p1 = new Coordinate();
  3112. const p2 = new Coordinate();
  3113. ring.getCoordinate(0, p1);
  3114. ring.getCoordinate(1, p2);
  3115. const x0 = p1.x;
  3116. p2.x -= x0;
  3117. let sum = 0.0;
  3118. for (let i = 1; i < n - 1; i++) {
  3119. p0.y = p1.y;
  3120. p1.x = p2.x;
  3121. p1.y = p2.y;
  3122. ring.getCoordinate(i + 1, p2);
  3123. p2.x -= x0;
  3124. sum += p1.x * (p0.y - p2.y);
  3125. }
  3126. return sum / 2.0;
  3127. }
  3128. }
  3129. }
  3130. /**
  3131. * @see http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html
  3132. */
  3133. class Arrays {
  3134. static sort() {
  3135. const a = arguments[0];
  3136. if (arguments.length === 1) {
  3137. a.sort((a, b) => a.compareTo(b));
  3138. } else if (arguments.length === 2) {
  3139. a.sort((a, b) => arguments[1].compare(a, b));
  3140. } else if (arguments.length === 3) {
  3141. const t = a.slice(arguments[1], arguments[2]);
  3142. t.sort();
  3143. const r = a.slice(0, arguments[1]).concat(t, a.slice(arguments[2], a.length));
  3144. a.splice(0, a.length);
  3145. for (const e of r) a.push(e);
  3146. } else if (arguments.length === 4) {
  3147. const t = a.slice(arguments[1], arguments[2]);
  3148. t.sort((a, b) => arguments[3].compare(a, b));
  3149. const r = a.slice(0, arguments[1]).concat(t, a.slice(arguments[2], a.length));
  3150. a.splice(0, a.length);
  3151. for (const e of r) a.push(e);
  3152. }
  3153. }
  3154. /**
  3155. * @param {Array} array
  3156. * @return {ArrayList}
  3157. */
  3158. static asList(array) {
  3159. const arrayList = new ArrayList();
  3160. for (const e of array) arrayList.add(e);
  3161. return arrayList;
  3162. }
  3163. static copyOf(original, newLength) {
  3164. return original.slice(0, newLength);
  3165. }
  3166. }
  3167. class Polygonal {}
  3168. class Polygon extends Geometry {
  3169. constructor() {
  3170. super();
  3171. Polygon.constructor_.apply(this, arguments);
  3172. }
  3173. static constructor_() {
  3174. this._shell = null;
  3175. this._holes = null;
  3176. let shell = arguments[0],
  3177. holes = arguments[1],
  3178. factory = arguments[2];
  3179. Geometry.constructor_.call(this, factory);
  3180. if (shell === null) shell = this.getFactory().createLinearRing();
  3181. if (holes === null) holes = [];
  3182. if (Geometry.hasNullElements(holes)) throw new IllegalArgumentException('holes must not contain null elements');
  3183. if (shell.isEmpty() && Geometry.hasNonEmptyElements(holes)) throw new IllegalArgumentException('shell is empty but holes are not');
  3184. this._shell = shell;
  3185. this._holes = holes;
  3186. }
  3187. computeEnvelopeInternal() {
  3188. return this._shell.getEnvelopeInternal();
  3189. }
  3190. getCoordinates() {
  3191. if (this.isEmpty()) return [];
  3192. const coordinates = new Array(this.getNumPoints()).fill(null);
  3193. let k = -1;
  3194. const shellCoordinates = this._shell.getCoordinates();
  3195. for (let x = 0; x < shellCoordinates.length; x++) {
  3196. k++;
  3197. coordinates[k] = shellCoordinates[x];
  3198. }
  3199. for (let i = 0; i < this._holes.length; i++) {
  3200. const childCoordinates = this._holes[i].getCoordinates();
  3201. for (let j = 0; j < childCoordinates.length; j++) {
  3202. k++;
  3203. coordinates[k] = childCoordinates[j];
  3204. }
  3205. }
  3206. return coordinates;
  3207. }
  3208. getArea() {
  3209. let area = 0.0;
  3210. area += Area.ofRing(this._shell.getCoordinateSequence());
  3211. for (let i = 0; i < this._holes.length; i++) area -= Area.ofRing(this._holes[i].getCoordinateSequence());
  3212. return area;
  3213. }
  3214. copyInternal() {
  3215. const shellCopy = this._shell.copy();
  3216. const holeCopies = new Array(this._holes.length).fill(null);
  3217. for (let i = 0; i < this._holes.length; i++) holeCopies[i] = this._holes[i].copy();
  3218. return new Polygon(shellCopy, holeCopies, this._factory);
  3219. }
  3220. isRectangle() {
  3221. if (this.getNumInteriorRing() !== 0) return false;
  3222. if (this._shell === null) return false;
  3223. if (this._shell.getNumPoints() !== 5) return false;
  3224. const seq = this._shell.getCoordinateSequence();
  3225. const env = this.getEnvelopeInternal();
  3226. for (let i = 0; i < 5; i++) {
  3227. const x = seq.getX(i);
  3228. if (!(x === env.getMinX() || x === env.getMaxX())) return false;
  3229. const y = seq.getY(i);
  3230. if (!(y === env.getMinY() || y === env.getMaxY())) return false;
  3231. }
  3232. let prevX = seq.getX(0);
  3233. let prevY = seq.getY(0);
  3234. for (let i = 1; i <= 4; i++) {
  3235. const x = seq.getX(i);
  3236. const y = seq.getY(i);
  3237. const xChanged = x !== prevX;
  3238. const yChanged = y !== prevY;
  3239. if (xChanged === yChanged) return false;
  3240. prevX = x;
  3241. prevY = y;
  3242. }
  3243. return true;
  3244. }
  3245. equalsExact() {
  3246. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  3247. const other = arguments[0],
  3248. tolerance = arguments[1];
  3249. if (!this.isEquivalentClass(other)) return false;
  3250. const otherPolygon = other;
  3251. const thisShell = this._shell;
  3252. const otherPolygonShell = otherPolygon._shell;
  3253. if (!thisShell.equalsExact(otherPolygonShell, tolerance)) return false;
  3254. if (this._holes.length !== otherPolygon._holes.length) return false;
  3255. for (let i = 0; i < this._holes.length; i++) if (!this._holes[i].equalsExact(otherPolygon._holes[i], tolerance)) return false;
  3256. return true;
  3257. } else {
  3258. return super.equalsExact.apply(this, arguments);
  3259. }
  3260. }
  3261. normalize() {
  3262. if (arguments.length === 0) {
  3263. this._shell = this.normalized(this._shell, true);
  3264. for (let i = 0; i < this._holes.length; i++) this._holes[i] = this.normalized(this._holes[i], false);
  3265. Arrays.sort(this._holes);
  3266. } else if (arguments.length === 2) {
  3267. const ring = arguments[0],
  3268. clockwise = arguments[1];
  3269. if (ring.isEmpty()) return null;
  3270. const seq = ring.getCoordinateSequence();
  3271. const minCoordinateIndex = CoordinateSequences.minCoordinateIndex(seq, 0, seq.size() - 2);
  3272. CoordinateSequences.scroll(seq, minCoordinateIndex, true);
  3273. if (Orientation.isCCW(seq) === clockwise) CoordinateSequences.reverse(seq);
  3274. }
  3275. }
  3276. getCoordinate() {
  3277. return this._shell.getCoordinate();
  3278. }
  3279. getNumInteriorRing() {
  3280. return this._holes.length;
  3281. }
  3282. getBoundaryDimension() {
  3283. return 1;
  3284. }
  3285. reverseInternal() {
  3286. const shell = this.getExteriorRing().reverse();
  3287. const holes = new Array(this.getNumInteriorRing()).fill(null);
  3288. for (let i = 0; i < holes.length; i++) holes[i] = this.getInteriorRingN(i).reverse();
  3289. return this.getFactory().createPolygon(shell, holes);
  3290. }
  3291. getTypeCode() {
  3292. return Geometry.TYPECODE_POLYGON;
  3293. }
  3294. getDimension() {
  3295. return 2;
  3296. }
  3297. getLength() {
  3298. let len = 0.0;
  3299. len += this._shell.getLength();
  3300. for (let i = 0; i < this._holes.length; i++) len += this._holes[i].getLength();
  3301. return len;
  3302. }
  3303. getNumPoints() {
  3304. let numPoints = this._shell.getNumPoints();
  3305. for (let i = 0; i < this._holes.length; i++) numPoints += this._holes[i].getNumPoints();
  3306. return numPoints;
  3307. }
  3308. convexHull() {
  3309. return this.getExteriorRing().convexHull();
  3310. }
  3311. normalized(ring, clockwise) {
  3312. const res = ring.copy();
  3313. this.normalize(res, clockwise);
  3314. return res;
  3315. }
  3316. compareToSameClass() {
  3317. if (arguments.length === 1) {
  3318. const o = arguments[0];
  3319. const thisShell = this._shell;
  3320. const otherShell = o._shell;
  3321. return thisShell.compareToSameClass(otherShell);
  3322. } else if (arguments.length === 2) {
  3323. const o = arguments[0],
  3324. comp = arguments[1];
  3325. const poly = o;
  3326. const thisShell = this._shell;
  3327. const otherShell = poly._shell;
  3328. const shellComp = thisShell.compareToSameClass(otherShell, comp);
  3329. if (shellComp !== 0) return shellComp;
  3330. const nHole1 = this.getNumInteriorRing();
  3331. const nHole2 = poly.getNumInteriorRing();
  3332. let i = 0;
  3333. while (i < nHole1 && i < nHole2) {
  3334. const thisHole = this.getInteriorRingN(i);
  3335. const otherHole = poly.getInteriorRingN(i);
  3336. const holeComp = thisHole.compareToSameClass(otherHole, comp);
  3337. if (holeComp !== 0) return holeComp;
  3338. i++;
  3339. }
  3340. if (i < nHole1) return 1;
  3341. if (i < nHole2) return -1;
  3342. return 0;
  3343. }
  3344. }
  3345. apply() {
  3346. if (hasInterface(arguments[0], CoordinateFilter)) {
  3347. const filter = arguments[0];
  3348. this._shell.apply(filter);
  3349. for (let i = 0; i < this._holes.length; i++) this._holes[i].apply(filter);
  3350. } else if (hasInterface(arguments[0], CoordinateSequenceFilter)) {
  3351. const filter = arguments[0];
  3352. this._shell.apply(filter);
  3353. if (!filter.isDone()) for (let i = 0; i < this._holes.length; i++) {
  3354. this._holes[i].apply(filter);
  3355. if (filter.isDone()) break;
  3356. }
  3357. if (filter.isGeometryChanged()) this.geometryChanged();
  3358. } else if (hasInterface(arguments[0], GeometryFilter)) {
  3359. const filter = arguments[0];
  3360. filter.filter(this);
  3361. } else if (hasInterface(arguments[0], GeometryComponentFilter)) {
  3362. const filter = arguments[0];
  3363. filter.filter(this);
  3364. this._shell.apply(filter);
  3365. for (let i = 0; i < this._holes.length; i++) this._holes[i].apply(filter);
  3366. }
  3367. }
  3368. getBoundary() {
  3369. if (this.isEmpty()) return this.getFactory().createMultiLineString();
  3370. const rings = new Array(this._holes.length + 1).fill(null);
  3371. rings[0] = this._shell;
  3372. for (let i = 0; i < this._holes.length; i++) rings[i + 1] = this._holes[i];
  3373. if (rings.length <= 1) return this.getFactory().createLinearRing(rings[0].getCoordinateSequence());
  3374. return this.getFactory().createMultiLineString(rings);
  3375. }
  3376. getGeometryType() {
  3377. return Geometry.TYPENAME_POLYGON;
  3378. }
  3379. getExteriorRing() {
  3380. return this._shell;
  3381. }
  3382. isEmpty() {
  3383. return this._shell.isEmpty();
  3384. }
  3385. getInteriorRingN(n) {
  3386. return this._holes[n];
  3387. }
  3388. get interfaces_() {
  3389. return [Polygonal];
  3390. }
  3391. }
  3392. /**
  3393. * @see http://download.oracle.com/javase/6/docs/api/java/util/Set.html
  3394. *
  3395. * @extends {Collection}
  3396. * @constructor
  3397. * @private
  3398. */
  3399. class Set extends Collection {
  3400. /**
  3401. * Returns true if this set contains the specified element. More formally,
  3402. * returns true if and only if this set contains an element e such that (o==null ?
  3403. * e==null : o.equals(e)).
  3404. * @param {Object} e
  3405. * @return {boolean}
  3406. */
  3407. contains() {}
  3408. }
  3409. /**
  3410. * @see http://download.oracle.com/javase/6/docs/api/java/util/SortedSet.html
  3411. */
  3412. class SortedSet extends Set {}
  3413. /**
  3414. * @see http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html
  3415. */
  3416. class TreeSet extends SortedSet {
  3417. constructor(o) {
  3418. super();
  3419. this.array = [];
  3420. if (o instanceof Collection) this.addAll(o);
  3421. }
  3422. contains(o) {
  3423. for (const e of this.array) if (e.compareTo(o) === 0) return true;
  3424. return false;
  3425. }
  3426. add(o) {
  3427. if (this.contains(o)) return false;
  3428. for (let i = 0, len = this.array.length; i < len; i++) {
  3429. const e = this.array[i];
  3430. if (e.compareTo(o) === 1) return !!this.array.splice(i, 0, o);
  3431. }
  3432. this.array.push(o);
  3433. return true;
  3434. }
  3435. addAll(c) {
  3436. for (const e of c) this.add(e);
  3437. return true;
  3438. }
  3439. remove() {
  3440. throw new UnsupportedOperationException();
  3441. }
  3442. size() {
  3443. return this.array.length;
  3444. }
  3445. isEmpty() {
  3446. return this.array.length === 0;
  3447. }
  3448. toArray() {
  3449. return this.array.slice();
  3450. }
  3451. iterator() {
  3452. return new Iterator$2(this.array);
  3453. }
  3454. }
  3455. class Iterator$2 {
  3456. constructor(array) {
  3457. this.array = array;
  3458. this.position = 0;
  3459. }
  3460. next() {
  3461. if (this.position === this.array.length) throw new NoSuchElementException();
  3462. return this.array[this.position++];
  3463. }
  3464. hasNext() {
  3465. return this.position < this.array.length;
  3466. }
  3467. remove() {
  3468. throw new UnsupportedOperationException();
  3469. }
  3470. }
  3471. class GeometryCollection extends Geometry {
  3472. constructor() {
  3473. super();
  3474. GeometryCollection.constructor_.apply(this, arguments);
  3475. }
  3476. static constructor_() {
  3477. this._geometries = null;
  3478. if (arguments.length === 0) ; else if (arguments.length === 2) {
  3479. let geometries = arguments[0],
  3480. factory = arguments[1];
  3481. Geometry.constructor_.call(this, factory);
  3482. if (geometries === null) geometries = [];
  3483. if (Geometry.hasNullElements(geometries)) throw new IllegalArgumentException('geometries must not contain null elements');
  3484. this._geometries = geometries;
  3485. }
  3486. }
  3487. computeEnvelopeInternal() {
  3488. const envelope = new Envelope();
  3489. for (let i = 0; i < this._geometries.length; i++) envelope.expandToInclude(this._geometries[i].getEnvelopeInternal());
  3490. return envelope;
  3491. }
  3492. getGeometryN(n) {
  3493. return this._geometries[n];
  3494. }
  3495. getCoordinates() {
  3496. const coordinates = new Array(this.getNumPoints()).fill(null);
  3497. let k = -1;
  3498. for (let i = 0; i < this._geometries.length; i++) {
  3499. const childCoordinates = this._geometries[i].getCoordinates();
  3500. for (let j = 0; j < childCoordinates.length; j++) {
  3501. k++;
  3502. coordinates[k] = childCoordinates[j];
  3503. }
  3504. }
  3505. return coordinates;
  3506. }
  3507. getArea() {
  3508. let area = 0.0;
  3509. for (let i = 0; i < this._geometries.length; i++) area += this._geometries[i].getArea();
  3510. return area;
  3511. }
  3512. copyInternal() {
  3513. const geometries = new Array(this._geometries.length).fill(null);
  3514. for (let i = 0; i < geometries.length; i++) geometries[i] = this._geometries[i].copy();
  3515. return new GeometryCollection(geometries, this._factory);
  3516. }
  3517. equalsExact() {
  3518. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  3519. const other = arguments[0],
  3520. tolerance = arguments[1];
  3521. if (!this.isEquivalentClass(other)) return false;
  3522. const otherCollection = other;
  3523. if (this._geometries.length !== otherCollection._geometries.length) return false;
  3524. for (let i = 0; i < this._geometries.length; i++) if (!this._geometries[i].equalsExact(otherCollection._geometries[i], tolerance)) return false;
  3525. return true;
  3526. } else {
  3527. return super.equalsExact.apply(this, arguments);
  3528. }
  3529. }
  3530. normalize() {
  3531. for (let i = 0; i < this._geometries.length; i++) this._geometries[i].normalize();
  3532. Arrays.sort(this._geometries);
  3533. }
  3534. getCoordinate() {
  3535. if (this.isEmpty()) return null;
  3536. return this._geometries[0].getCoordinate();
  3537. }
  3538. getBoundaryDimension() {
  3539. let dimension = Dimension.FALSE;
  3540. for (let i = 0; i < this._geometries.length; i++) dimension = Math.max(dimension, this._geometries[i].getBoundaryDimension());
  3541. return dimension;
  3542. }
  3543. reverseInternal() {
  3544. const numGeometries = this._geometries.length;
  3545. const reversed = new ArrayList(numGeometries);
  3546. for (let i = 0; i < numGeometries; i++) reversed.add(this._geometries[i].reverse());
  3547. return this.getFactory().buildGeometry(reversed);
  3548. }
  3549. getTypeCode() {
  3550. return Geometry.TYPECODE_GEOMETRYCOLLECTION;
  3551. }
  3552. getDimension() {
  3553. let dimension = Dimension.FALSE;
  3554. for (let i = 0; i < this._geometries.length; i++) dimension = Math.max(dimension, this._geometries[i].getDimension());
  3555. return dimension;
  3556. }
  3557. getLength() {
  3558. let sum = 0.0;
  3559. for (let i = 0; i < this._geometries.length; i++) sum += this._geometries[i].getLength();
  3560. return sum;
  3561. }
  3562. getNumPoints() {
  3563. let numPoints = 0;
  3564. for (let i = 0; i < this._geometries.length; i++) numPoints += this._geometries[i].getNumPoints();
  3565. return numPoints;
  3566. }
  3567. getNumGeometries() {
  3568. return this._geometries.length;
  3569. }
  3570. compareToSameClass() {
  3571. if (arguments.length === 1) {
  3572. const o = arguments[0];
  3573. const theseElements = new TreeSet(Arrays.asList(this._geometries));
  3574. const otherElements = new TreeSet(Arrays.asList(o._geometries));
  3575. return this.compare(theseElements, otherElements);
  3576. } else if (arguments.length === 2) {
  3577. const o = arguments[0],
  3578. comp = arguments[1];
  3579. const gc = o;
  3580. const n1 = this.getNumGeometries();
  3581. const n2 = gc.getNumGeometries();
  3582. let i = 0;
  3583. while (i < n1 && i < n2) {
  3584. const thisGeom = this.getGeometryN(i);
  3585. const otherGeom = gc.getGeometryN(i);
  3586. const holeComp = thisGeom.compareToSameClass(otherGeom, comp);
  3587. if (holeComp !== 0) return holeComp;
  3588. i++;
  3589. }
  3590. if (i < n1) return 1;
  3591. if (i < n2) return -1;
  3592. return 0;
  3593. }
  3594. }
  3595. apply() {
  3596. if (hasInterface(arguments[0], CoordinateFilter)) {
  3597. const filter = arguments[0];
  3598. for (let i = 0; i < this._geometries.length; i++) this._geometries[i].apply(filter);
  3599. } else if (hasInterface(arguments[0], CoordinateSequenceFilter)) {
  3600. const filter = arguments[0];
  3601. if (this._geometries.length === 0) return null;
  3602. for (let i = 0; i < this._geometries.length; i++) {
  3603. this._geometries[i].apply(filter);
  3604. if (filter.isDone()) break;
  3605. }
  3606. if (filter.isGeometryChanged()) this.geometryChanged();
  3607. } else if (hasInterface(arguments[0], GeometryFilter)) {
  3608. const filter = arguments[0];
  3609. filter.filter(this);
  3610. for (let i = 0; i < this._geometries.length; i++) this._geometries[i].apply(filter);
  3611. } else if (hasInterface(arguments[0], GeometryComponentFilter)) {
  3612. const filter = arguments[0];
  3613. filter.filter(this);
  3614. for (let i = 0; i < this._geometries.length; i++) this._geometries[i].apply(filter);
  3615. }
  3616. }
  3617. getBoundary() {
  3618. Geometry.checkNotGeometryCollection(this);
  3619. Assert.shouldNeverReachHere();
  3620. return null;
  3621. }
  3622. getGeometryType() {
  3623. return Geometry.TYPENAME_GEOMETRYCOLLECTION;
  3624. }
  3625. isEmpty() {
  3626. for (let i = 0; i < this._geometries.length; i++) if (!this._geometries[i].isEmpty()) return false;
  3627. return true;
  3628. }
  3629. }
  3630. class MultiPoint extends GeometryCollection {
  3631. constructor() {
  3632. super();
  3633. MultiPoint.constructor_.apply(this, arguments);
  3634. }
  3635. static constructor_() {
  3636. const points = arguments[0],
  3637. factory = arguments[1];
  3638. GeometryCollection.constructor_.call(this, points, factory);
  3639. }
  3640. copyInternal() {
  3641. const points = new Array(this._geometries.length).fill(null);
  3642. for (let i = 0; i < points.length; i++) points[i] = this._geometries[i].copy();
  3643. return new MultiPoint(points, this._factory);
  3644. }
  3645. isValid() {
  3646. return true;
  3647. }
  3648. equalsExact() {
  3649. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  3650. const other = arguments[0],
  3651. tolerance = arguments[1];
  3652. if (!this.isEquivalentClass(other)) return false;
  3653. return super.equalsExact.call(this, other, tolerance);
  3654. } else {
  3655. return super.equalsExact.apply(this, arguments);
  3656. }
  3657. }
  3658. getCoordinate() {
  3659. if (arguments.length === 1 && Number.isInteger(arguments[0])) {
  3660. const n = arguments[0];
  3661. return this._geometries[n].getCoordinate();
  3662. } else {
  3663. return super.getCoordinate.apply(this, arguments);
  3664. }
  3665. }
  3666. getBoundaryDimension() {
  3667. return Dimension.FALSE;
  3668. }
  3669. getTypeCode() {
  3670. return Geometry.TYPECODE_MULTIPOINT;
  3671. }
  3672. getDimension() {
  3673. return 0;
  3674. }
  3675. getBoundary() {
  3676. return this.getFactory().createGeometryCollection();
  3677. }
  3678. getGeometryType() {
  3679. return Geometry.TYPENAME_MULTIPOINT;
  3680. }
  3681. get interfaces_() {
  3682. return [Puntal];
  3683. }
  3684. }
  3685. class LinearRing extends LineString {
  3686. constructor() {
  3687. super();
  3688. LinearRing.constructor_.apply(this, arguments);
  3689. }
  3690. static constructor_() {
  3691. const points = arguments[0],
  3692. factory = arguments[1];
  3693. LineString.constructor_.call(this, points, factory);
  3694. this.validateConstruction();
  3695. }
  3696. copyInternal() {
  3697. return new LinearRing(this._points.copy(), this._factory);
  3698. }
  3699. getBoundaryDimension() {
  3700. return Dimension.FALSE;
  3701. }
  3702. isClosed() {
  3703. if (this.isEmpty()) return true;
  3704. return super.isClosed.call(this);
  3705. }
  3706. reverseInternal() {
  3707. const seq = this._points.copy();
  3708. CoordinateSequences.reverse(seq);
  3709. return this.getFactory().createLinearRing(seq);
  3710. }
  3711. getTypeCode() {
  3712. return Geometry.TYPECODE_LINEARRING;
  3713. }
  3714. validateConstruction() {
  3715. if (!this.isEmpty() && !super.isClosed.call(this)) throw new IllegalArgumentException('Points of LinearRing do not form a closed linestring');
  3716. if (this.getCoordinateSequence().size() >= 1 && this.getCoordinateSequence().size() < LinearRing.MINIMUM_VALID_SIZE) throw new IllegalArgumentException('Invalid number of points in LinearRing (found ' + this.getCoordinateSequence().size() + ' - must be 0 or >= 4)');
  3717. }
  3718. getGeometryType() {
  3719. return Geometry.TYPENAME_LINEARRING;
  3720. }
  3721. }
  3722. LinearRing.MINIMUM_VALID_SIZE = 4;
  3723. class Coordinates {
  3724. static measures(coordinate) {
  3725. if (coordinate instanceof CoordinateXY) return 0;else if (coordinate instanceof CoordinateXYM) return 1;else if (coordinate instanceof CoordinateXYZM) return 1;else if (coordinate instanceof Coordinate) return 0;
  3726. return 0;
  3727. }
  3728. static dimension(coordinate) {
  3729. if (coordinate instanceof CoordinateXY) return 2;else if (coordinate instanceof CoordinateXYM) return 3;else if (coordinate instanceof CoordinateXYZM) return 4;else if (coordinate instanceof Coordinate) return 3;
  3730. return 3;
  3731. }
  3732. static create() {
  3733. if (arguments.length === 1) {
  3734. const dimension = arguments[0];
  3735. return Coordinates.create(dimension, 0);
  3736. } else if (arguments.length === 2) {
  3737. const dimension = arguments[0],
  3738. measures = arguments[1];
  3739. if (dimension === 2) return new CoordinateXY();else if (dimension === 3 && measures === 0) return new Coordinate();else if (dimension === 3 && measures === 1) return new CoordinateXYM();else if (dimension === 4 && measures === 1) return new CoordinateXYZM();
  3740. return new Coordinate();
  3741. }
  3742. }
  3743. }
  3744. class CoordinateArrays {
  3745. static isRing(pts) {
  3746. if (pts.length < 4) return false;
  3747. if (!pts[0].equals2D(pts[pts.length - 1])) return false;
  3748. return true;
  3749. }
  3750. static ptNotInList(testPts, pts) {
  3751. for (let i = 0; i < testPts.length; i++) {
  3752. const testPt = testPts[i];
  3753. if (CoordinateArrays.indexOf(testPt, pts) < 0) return testPt;
  3754. }
  3755. return null;
  3756. }
  3757. static scroll(coordinates, firstCoordinate) {
  3758. const i = CoordinateArrays.indexOf(firstCoordinate, coordinates);
  3759. if (i < 0) return null;
  3760. const newCoordinates = new Array(coordinates.length).fill(null);
  3761. System.arraycopy(coordinates, i, newCoordinates, 0, coordinates.length - i);
  3762. System.arraycopy(coordinates, 0, newCoordinates, coordinates.length - i, i);
  3763. System.arraycopy(newCoordinates, 0, coordinates, 0, coordinates.length);
  3764. }
  3765. static equals() {
  3766. if (arguments.length === 2) {
  3767. const coord1 = arguments[0],
  3768. coord2 = arguments[1];
  3769. if (coord1 === coord2) return true;
  3770. if (coord1 === null || coord2 === null) return false;
  3771. if (coord1.length !== coord2.length) return false;
  3772. for (let i = 0; i < coord1.length; i++) if (!coord1[i].equals(coord2[i])) return false;
  3773. return true;
  3774. } else if (arguments.length === 3) {
  3775. const coord1 = arguments[0],
  3776. coord2 = arguments[1],
  3777. coordinateComparator = arguments[2];
  3778. if (coord1 === coord2) return true;
  3779. if (coord1 === null || coord2 === null) return false;
  3780. if (coord1.length !== coord2.length) return false;
  3781. for (let i = 0; i < coord1.length; i++) if (coordinateComparator.compare(coord1[i], coord2[i]) !== 0) return false;
  3782. return true;
  3783. }
  3784. }
  3785. static intersection(coordinates, env) {
  3786. const coordList = new CoordinateList();
  3787. for (let i = 0; i < coordinates.length; i++) if (env.intersects(coordinates[i])) coordList.add(coordinates[i], true);
  3788. return coordList.toCoordinateArray();
  3789. }
  3790. static measures(pts) {
  3791. if (pts === null || pts.length === 0) return 0;
  3792. let measures = 0;
  3793. for (const coordinate of pts) measures = Math.max(measures, Coordinates.measures(coordinate));
  3794. return measures;
  3795. }
  3796. static hasRepeatedPoints(coord) {
  3797. for (let i = 1; i < coord.length; i++) if (coord[i - 1].equals(coord[i])) return true;
  3798. return false;
  3799. }
  3800. static removeRepeatedPoints(coord) {
  3801. if (!CoordinateArrays.hasRepeatedPoints(coord)) return coord;
  3802. const coordList = new CoordinateList(coord, false);
  3803. return coordList.toCoordinateArray();
  3804. }
  3805. static reverse(coord) {
  3806. const last = coord.length - 1;
  3807. const mid = Math.trunc(last / 2);
  3808. for (let i = 0; i <= mid; i++) {
  3809. const tmp = coord[i];
  3810. coord[i] = coord[last - i];
  3811. coord[last - i] = tmp;
  3812. }
  3813. }
  3814. static removeNull(coord) {
  3815. let nonNull = 0;
  3816. for (let i = 0; i < coord.length; i++) if (coord[i] !== null) nonNull++;
  3817. const newCoord = new Array(nonNull).fill(null);
  3818. if (nonNull === 0) return newCoord;
  3819. let j = 0;
  3820. for (let i = 0; i < coord.length; i++) if (coord[i] !== null) newCoord[j++] = coord[i];
  3821. return newCoord;
  3822. }
  3823. static copyDeep() {
  3824. if (arguments.length === 1) {
  3825. const coordinates = arguments[0];
  3826. const copy = new Array(coordinates.length).fill(null);
  3827. for (let i = 0; i < coordinates.length; i++) copy[i] = coordinates[i].copy();
  3828. return copy;
  3829. } else if (arguments.length === 5) {
  3830. const src = arguments[0],
  3831. srcStart = arguments[1],
  3832. dest = arguments[2],
  3833. destStart = arguments[3],
  3834. length = arguments[4];
  3835. for (let i = 0; i < length; i++) dest[destStart + i] = src[srcStart + i].copy();
  3836. }
  3837. }
  3838. static isEqualReversed(pts1, pts2) {
  3839. for (let i = 0; i < pts1.length; i++) {
  3840. const p1 = pts1[i];
  3841. const p2 = pts2[pts1.length - i - 1];
  3842. if (p1.compareTo(p2) !== 0) return false;
  3843. }
  3844. return true;
  3845. }
  3846. static envelope(coordinates) {
  3847. const env = new Envelope();
  3848. for (let i = 0; i < coordinates.length; i++) env.expandToInclude(coordinates[i]);
  3849. return env;
  3850. }
  3851. static toCoordinateArray(coordList) {
  3852. return coordList.toArray(CoordinateArrays.coordArrayType);
  3853. }
  3854. static dimension(pts) {
  3855. if (pts === null || pts.length === 0) return 3;
  3856. let dimension = 0;
  3857. for (const coordinate of pts) dimension = Math.max(dimension, Coordinates.dimension(coordinate));
  3858. return dimension;
  3859. }
  3860. static atLeastNCoordinatesOrNothing(n, c) {
  3861. return c.length >= n ? c : [];
  3862. }
  3863. static indexOf(coordinate, coordinates) {
  3864. for (let i = 0; i < coordinates.length; i++) if (coordinate.equals(coordinates[i])) return i;
  3865. return -1;
  3866. }
  3867. static increasingDirection(pts) {
  3868. for (let i = 0; i < Math.trunc(pts.length / 2); i++) {
  3869. const j = pts.length - 1 - i;
  3870. const comp = pts[i].compareTo(pts[j]);
  3871. if (comp !== 0) return comp;
  3872. }
  3873. return 1;
  3874. }
  3875. static compare(pts1, pts2) {
  3876. let i = 0;
  3877. while (i < pts1.length && i < pts2.length) {
  3878. const compare = pts1[i].compareTo(pts2[i]);
  3879. if (compare !== 0) return compare;
  3880. i++;
  3881. }
  3882. if (i < pts2.length) return -1;
  3883. if (i < pts1.length) return 1;
  3884. return 0;
  3885. }
  3886. static minCoordinate(coordinates) {
  3887. let minCoord = null;
  3888. for (let i = 0; i < coordinates.length; i++) if (minCoord === null || minCoord.compareTo(coordinates[i]) > 0) minCoord = coordinates[i];
  3889. return minCoord;
  3890. }
  3891. static extract(pts, start, end) {
  3892. start = MathUtil.clamp(start, 0, pts.length);
  3893. end = MathUtil.clamp(end, -1, pts.length);
  3894. let npts = end - start + 1;
  3895. if (end < 0) npts = 0;
  3896. if (start >= pts.length) npts = 0;
  3897. if (end < start) npts = 0;
  3898. const extractPts = new Array(npts).fill(null);
  3899. if (npts === 0) return extractPts;
  3900. let iPts = 0;
  3901. for (let i = start; i <= end; i++) extractPts[iPts++] = pts[i];
  3902. return extractPts;
  3903. }
  3904. }
  3905. class ForwardComparator {
  3906. compare(o1, o2) {
  3907. const pts1 = o1;
  3908. const pts2 = o2;
  3909. return CoordinateArrays.compare(pts1, pts2);
  3910. }
  3911. get interfaces_() {
  3912. return [Comparator];
  3913. }
  3914. }
  3915. class BidirectionalComparator {
  3916. compare(o1, o2) {
  3917. const pts1 = o1;
  3918. const pts2 = o2;
  3919. if (pts1.length < pts2.length) return -1;
  3920. if (pts1.length > pts2.length) return 1;
  3921. if (pts1.length === 0) return 0;
  3922. const forwardComp = CoordinateArrays.compare(pts1, pts2);
  3923. const isEqualRev = CoordinateArrays.isEqualReversed(pts1, pts2);
  3924. if (isEqualRev) return 0;
  3925. return forwardComp;
  3926. }
  3927. OLDcompare(o1, o2) {
  3928. const pts1 = o1;
  3929. const pts2 = o2;
  3930. if (pts1.length < pts2.length) return -1;
  3931. if (pts1.length > pts2.length) return 1;
  3932. if (pts1.length === 0) return 0;
  3933. const dir1 = CoordinateArrays.increasingDirection(pts1);
  3934. const dir2 = CoordinateArrays.increasingDirection(pts2);
  3935. let i1 = dir1 > 0 ? 0 : pts1.length - 1;
  3936. let i2 = dir2 > 0 ? 0 : pts1.length - 1;
  3937. for (let i = 0; i < pts1.length; i++) {
  3938. const comparePt = pts1[i1].compareTo(pts2[i2]);
  3939. if (comparePt !== 0) return comparePt;
  3940. i1 += dir1;
  3941. i2 += dir2;
  3942. }
  3943. return 0;
  3944. }
  3945. get interfaces_() {
  3946. return [Comparator];
  3947. }
  3948. }
  3949. CoordinateArrays.ForwardComparator = ForwardComparator;
  3950. CoordinateArrays.BidirectionalComparator = BidirectionalComparator;
  3951. CoordinateArrays.coordArrayType = new Array(0).fill(null);
  3952. class StringBuilder {
  3953. constructor(str) {
  3954. this.str = str;
  3955. }
  3956. append(e) {
  3957. this.str += e;
  3958. }
  3959. setCharAt(i, c) {
  3960. this.str = this.str.substr(0, i) + c + this.str.substr(i + 1);
  3961. }
  3962. toString() {
  3963. return this.str;
  3964. }
  3965. }
  3966. class CoordinateArraySequence {
  3967. constructor() {
  3968. CoordinateArraySequence.constructor_.apply(this, arguments);
  3969. }
  3970. static constructor_() {
  3971. this._dimension = 3;
  3972. this._measures = 0;
  3973. this._coordinates = null;
  3974. if (arguments.length === 1) {
  3975. if (arguments[0] instanceof Array) {
  3976. const coordinates = arguments[0];
  3977. CoordinateArraySequence.constructor_.call(this, coordinates, CoordinateArrays.dimension(coordinates), CoordinateArrays.measures(coordinates));
  3978. } else if (Number.isInteger(arguments[0])) {
  3979. const size = arguments[0];
  3980. this._coordinates = new Array(size).fill(null);
  3981. for (let i = 0; i < size; i++) this._coordinates[i] = new Coordinate();
  3982. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  3983. const coordSeq = arguments[0];
  3984. if (coordSeq === null) {
  3985. this._coordinates = new Array(0).fill(null);
  3986. return null;
  3987. }
  3988. this._dimension = coordSeq.getDimension();
  3989. this._measures = coordSeq.getMeasures();
  3990. this._coordinates = new Array(coordSeq.size()).fill(null);
  3991. for (let i = 0; i < this._coordinates.length; i++) this._coordinates[i] = coordSeq.getCoordinateCopy(i);
  3992. }
  3993. } else if (arguments.length === 2) {
  3994. if (arguments[0] instanceof Array && Number.isInteger(arguments[1])) {
  3995. const coordinates = arguments[0],
  3996. dimension = arguments[1];
  3997. CoordinateArraySequence.constructor_.call(this, coordinates, dimension, CoordinateArrays.measures(coordinates));
  3998. } else if (Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])) {
  3999. const size = arguments[0],
  4000. dimension = arguments[1];
  4001. this._coordinates = new Array(size).fill(null);
  4002. this._dimension = dimension;
  4003. for (let i = 0; i < size; i++) this._coordinates[i] = Coordinates.create(dimension);
  4004. }
  4005. } else if (arguments.length === 3) {
  4006. if (Number.isInteger(arguments[2]) && arguments[0] instanceof Array && Number.isInteger(arguments[1])) {
  4007. const coordinates = arguments[0],
  4008. dimension = arguments[1],
  4009. measures = arguments[2];
  4010. this._dimension = dimension;
  4011. this._measures = measures;
  4012. if (coordinates === null) this._coordinates = new Array(0).fill(null);else this._coordinates = coordinates;
  4013. } else if (Number.isInteger(arguments[2]) && Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])) {
  4014. const size = arguments[0],
  4015. dimension = arguments[1],
  4016. measures = arguments[2];
  4017. this._coordinates = new Array(size).fill(null);
  4018. this._dimension = dimension;
  4019. this._measures = measures;
  4020. for (let i = 0; i < size; i++) this._coordinates[i] = this.createCoordinate();
  4021. }
  4022. }
  4023. }
  4024. getM(index) {
  4025. if (this.hasM()) return this._coordinates[index].getM();else return Double.NaN;
  4026. }
  4027. setOrdinate(index, ordinateIndex, value) {
  4028. switch (ordinateIndex) {
  4029. case CoordinateSequence.X:
  4030. this._coordinates[index].x = value;
  4031. break;
  4032. case CoordinateSequence.Y:
  4033. this._coordinates[index].y = value;
  4034. break;
  4035. default:
  4036. this._coordinates[index].setOrdinate(ordinateIndex, value);
  4037. }
  4038. }
  4039. getZ(index) {
  4040. if (this.hasZ()) return this._coordinates[index].getZ();else return Double.NaN;
  4041. }
  4042. size() {
  4043. return this._coordinates.length;
  4044. }
  4045. getOrdinate(index, ordinateIndex) {
  4046. switch (ordinateIndex) {
  4047. case CoordinateSequence.X:
  4048. return this._coordinates[index].x;
  4049. case CoordinateSequence.Y:
  4050. return this._coordinates[index].y;
  4051. default:
  4052. return this._coordinates[index].getOrdinate(ordinateIndex);
  4053. }
  4054. }
  4055. getCoordinate() {
  4056. if (arguments.length === 1) {
  4057. const i = arguments[0];
  4058. return this._coordinates[i];
  4059. } else if (arguments.length === 2) {
  4060. const index = arguments[0],
  4061. coord = arguments[1];
  4062. coord.setCoordinate(this._coordinates[index]);
  4063. }
  4064. }
  4065. getCoordinateCopy(i) {
  4066. const copy = this.createCoordinate();
  4067. copy.setCoordinate(this._coordinates[i]);
  4068. return copy;
  4069. }
  4070. createCoordinate() {
  4071. return Coordinates.create(this.getDimension(), this.getMeasures());
  4072. }
  4073. getDimension() {
  4074. return this._dimension;
  4075. }
  4076. getX(index) {
  4077. return this._coordinates[index].x;
  4078. }
  4079. getMeasures() {
  4080. return this._measures;
  4081. }
  4082. expandEnvelope(env) {
  4083. for (let i = 0; i < this._coordinates.length; i++) env.expandToInclude(this._coordinates[i]);
  4084. return env;
  4085. }
  4086. copy() {
  4087. const cloneCoordinates = new Array(this.size()).fill(null);
  4088. for (let i = 0; i < this._coordinates.length; i++) {
  4089. const duplicate = this.createCoordinate();
  4090. duplicate.setCoordinate(this._coordinates[i]);
  4091. cloneCoordinates[i] = duplicate;
  4092. }
  4093. return new CoordinateArraySequence(cloneCoordinates, this._dimension, this._measures);
  4094. }
  4095. toString() {
  4096. if (this._coordinates.length > 0) {
  4097. const strBuilder = new StringBuilder(17 * this._coordinates.length);
  4098. strBuilder.append('(');
  4099. strBuilder.append(this._coordinates[0]);
  4100. for (let i = 1; i < this._coordinates.length; i++) {
  4101. strBuilder.append(', ');
  4102. strBuilder.append(this._coordinates[i]);
  4103. }
  4104. strBuilder.append(')');
  4105. return strBuilder.toString();
  4106. } else {
  4107. return '()';
  4108. }
  4109. }
  4110. getY(index) {
  4111. return this._coordinates[index].y;
  4112. }
  4113. toCoordinateArray() {
  4114. return this._coordinates;
  4115. }
  4116. get interfaces_() {
  4117. return [CoordinateSequence, Serializable];
  4118. }
  4119. }
  4120. class CoordinateArraySequenceFactory {
  4121. static instance() {
  4122. return CoordinateArraySequenceFactory.instanceObject;
  4123. }
  4124. readResolve() {
  4125. return CoordinateArraySequenceFactory.instance();
  4126. }
  4127. create() {
  4128. if (arguments.length === 1) {
  4129. if (arguments[0] instanceof Array) {
  4130. const coordinates = arguments[0];
  4131. return new CoordinateArraySequence(coordinates);
  4132. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  4133. const coordSeq = arguments[0];
  4134. return new CoordinateArraySequence(coordSeq);
  4135. }
  4136. } else if (arguments.length === 2) {
  4137. let size = arguments[0],
  4138. dimension = arguments[1];
  4139. if (dimension > 3) dimension = 3;
  4140. if (dimension < 2) dimension = 2;
  4141. return new CoordinateArraySequence(size, dimension);
  4142. } else if (arguments.length === 3) {
  4143. let size = arguments[0],
  4144. dimension = arguments[1],
  4145. measures = arguments[2];
  4146. let spatial = dimension - measures;
  4147. if (measures > 1) measures = 1;
  4148. if (spatial > 3) spatial = 3;
  4149. if (spatial < 2) spatial = 2;
  4150. return new CoordinateArraySequence(size, spatial + measures, measures);
  4151. }
  4152. }
  4153. get interfaces_() {
  4154. return [CoordinateSequenceFactory, Serializable];
  4155. }
  4156. }
  4157. CoordinateArraySequenceFactory.instanceObject = new CoordinateArraySequenceFactory();
  4158. class MultiPolygon extends GeometryCollection {
  4159. constructor() {
  4160. super();
  4161. MultiPolygon.constructor_.apply(this, arguments);
  4162. }
  4163. static constructor_() {
  4164. const polygons = arguments[0],
  4165. factory = arguments[1];
  4166. GeometryCollection.constructor_.call(this, polygons, factory);
  4167. }
  4168. copyInternal() {
  4169. const polygons = new Array(this._geometries.length).fill(null);
  4170. for (let i = 0; i < polygons.length; i++) polygons[i] = this._geometries[i].copy();
  4171. return new MultiPolygon(polygons, this._factory);
  4172. }
  4173. equalsExact() {
  4174. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  4175. const other = arguments[0],
  4176. tolerance = arguments[1];
  4177. if (!this.isEquivalentClass(other)) return false;
  4178. return super.equalsExact.call(this, other, tolerance);
  4179. } else {
  4180. return super.equalsExact.apply(this, arguments);
  4181. }
  4182. }
  4183. getBoundaryDimension() {
  4184. return 1;
  4185. }
  4186. getTypeCode() {
  4187. return Geometry.TYPECODE_MULTIPOLYGON;
  4188. }
  4189. getDimension() {
  4190. return 2;
  4191. }
  4192. getBoundary() {
  4193. if (this.isEmpty()) return this.getFactory().createMultiLineString();
  4194. const allRings = new ArrayList();
  4195. for (let i = 0; i < this._geometries.length; i++) {
  4196. const polygon = this._geometries[i];
  4197. const rings = polygon.getBoundary();
  4198. for (let j = 0; j < rings.getNumGeometries(); j++) allRings.add(rings.getGeometryN(j));
  4199. }
  4200. const allRingsArray = new Array(allRings.size()).fill(null);
  4201. return this.getFactory().createMultiLineString(allRings.toArray(allRingsArray));
  4202. }
  4203. getGeometryType() {
  4204. return Geometry.TYPENAME_MULTIPOLYGON;
  4205. }
  4206. get interfaces_() {
  4207. return [Polygonal];
  4208. }
  4209. }
  4210. /**
  4211. * @see http://download.oracle.com/javase/6/docs/api/java/util/Map.html
  4212. */
  4213. class Map$1 {
  4214. /**
  4215. * Returns the value to which the specified key is mapped, or null if this map
  4216. * contains no mapping for the key.
  4217. * @param {Object} key
  4218. * @return {Object}
  4219. */
  4220. get() {}
  4221. /**
  4222. * Associates the specified value with the specified key in this map (optional
  4223. * operation).
  4224. * @param {Object} key
  4225. * @param {Object} value
  4226. * @return {Object}
  4227. */
  4228. put() {}
  4229. /**
  4230. * Returns the number of key-value mappings in this map.
  4231. * @return {number}
  4232. */
  4233. size() {}
  4234. /**
  4235. * Returns a Collection view of the values contained in this map.
  4236. * @return {javascript.util.Collection}
  4237. */
  4238. values() {}
  4239. /**
  4240. * Returns a {@link Set} view of the mappings contained in this map.
  4241. * The set is backed by the map, so changes to the map are
  4242. * reflected in the set, and vice-versa. If the map is modified
  4243. * while an iteration over the set is in progress (except through
  4244. * the iterator's own <tt>remove</tt> operation, or through the
  4245. * <tt>setValue</tt> operation on a map entry returned by the
  4246. * iterator) the results of the iteration are undefined. The set
  4247. * supports element removal, which removes the corresponding
  4248. * mapping from the map, via the <tt>Iterator.remove</tt>,
  4249. * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
  4250. * <tt>clear</tt> operations. It does not support the
  4251. * <tt>add</tt> or <tt>addAll</tt> operations.
  4252. *
  4253. * @return {Set} a set view of the mappings contained in this map
  4254. */
  4255. entrySet() {}
  4256. }
  4257. /**
  4258. * @see http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html
  4259. */
  4260. class HashSet extends Set {
  4261. constructor(o) {
  4262. super();
  4263. this.map = new Map();
  4264. if (o instanceof Collection) this.addAll(o);
  4265. }
  4266. contains(o) {
  4267. const hashCode = o.hashCode ? o.hashCode() : o;
  4268. if (this.map.has(hashCode)) return true;
  4269. return false;
  4270. }
  4271. add(o) {
  4272. const hashCode = o.hashCode ? o.hashCode() : o;
  4273. if (this.map.has(hashCode)) return false;
  4274. return !!this.map.set(hashCode, o);
  4275. }
  4276. addAll(c) {
  4277. for (const e of c) this.add(e);
  4278. return true;
  4279. }
  4280. remove() {
  4281. throw new UnsupportedOperationException();
  4282. }
  4283. size() {
  4284. return this.map.size;
  4285. }
  4286. isEmpty() {
  4287. return this.map.size === 0;
  4288. }
  4289. toArray() {
  4290. return Array.from(this.map.values());
  4291. }
  4292. iterator() {
  4293. return new Iterator$1(this.map);
  4294. }
  4295. [Symbol.iterator]() {
  4296. return this.map;
  4297. }
  4298. }
  4299. class Iterator$1 {
  4300. constructor(map) {
  4301. this.iterator = map.values();
  4302. const {
  4303. done,
  4304. value
  4305. } = this.iterator.next();
  4306. this.done = done;
  4307. this.value = value;
  4308. }
  4309. next() {
  4310. if (this.done) throw new NoSuchElementException();
  4311. const current = this.value;
  4312. const {
  4313. done,
  4314. value
  4315. } = this.iterator.next();
  4316. this.done = done;
  4317. this.value = value;
  4318. return current;
  4319. }
  4320. hasNext() {
  4321. return !this.done;
  4322. }
  4323. remove() {
  4324. throw new UnsupportedOperationException();
  4325. }
  4326. }
  4327. /**
  4328. * @see http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
  4329. */
  4330. class HashMap extends Map$1 {
  4331. constructor() {
  4332. super();
  4333. this.map = new Map();
  4334. }
  4335. get(key) {
  4336. return this.map.get(key) || null;
  4337. }
  4338. put(key, value) {
  4339. this.map.set(key, value);
  4340. return value;
  4341. }
  4342. values() {
  4343. const arrayList = new ArrayList();
  4344. const it = this.map.values();
  4345. let o = it.next();
  4346. while (!o.done) {
  4347. arrayList.add(o.value);
  4348. o = it.next();
  4349. }
  4350. return arrayList;
  4351. }
  4352. entrySet() {
  4353. const hashSet = new HashSet();
  4354. this.map.entries().forEach(entry => hashSet.add(entry));
  4355. return hashSet;
  4356. }
  4357. size() {
  4358. return this.map.size();
  4359. }
  4360. }
  4361. class PrecisionModel {
  4362. constructor() {
  4363. PrecisionModel.constructor_.apply(this, arguments);
  4364. }
  4365. static constructor_() {
  4366. this._modelType = null;
  4367. this._scale = null;
  4368. if (arguments.length === 0) this._modelType = PrecisionModel.FLOATING;else if (arguments.length === 1) if (arguments[0] instanceof Type) {
  4369. const modelType = arguments[0];
  4370. this._modelType = modelType;
  4371. if (modelType === PrecisionModel.FIXED) this.setScale(1.0);
  4372. } else if (typeof arguments[0] === 'number') {
  4373. const scale = arguments[0];
  4374. this._modelType = PrecisionModel.FIXED;
  4375. this.setScale(scale);
  4376. } else if (arguments[0] instanceof PrecisionModel) {
  4377. const pm = arguments[0];
  4378. this._modelType = pm._modelType;
  4379. this._scale = pm._scale;
  4380. }
  4381. }
  4382. static mostPrecise(pm1, pm2) {
  4383. if (pm1.compareTo(pm2) >= 0) return pm1;
  4384. return pm2;
  4385. }
  4386. equals(other) {
  4387. if (!(other instanceof PrecisionModel)) return false;
  4388. const otherPrecisionModel = other;
  4389. return this._modelType === otherPrecisionModel._modelType && this._scale === otherPrecisionModel._scale;
  4390. }
  4391. compareTo(o) {
  4392. const other = o;
  4393. const sigDigits = this.getMaximumSignificantDigits();
  4394. const otherSigDigits = other.getMaximumSignificantDigits();
  4395. return Integer.compare(sigDigits, otherSigDigits);
  4396. }
  4397. getScale() {
  4398. return this._scale;
  4399. }
  4400. isFloating() {
  4401. return this._modelType === PrecisionModel.FLOATING || this._modelType === PrecisionModel.FLOATING_SINGLE;
  4402. }
  4403. getType() {
  4404. return this._modelType;
  4405. }
  4406. toString() {
  4407. let description = 'UNKNOWN';
  4408. if (this._modelType === PrecisionModel.FLOATING) description = 'Floating';else if (this._modelType === PrecisionModel.FLOATING_SINGLE) description = 'Floating-Single';else if (this._modelType === PrecisionModel.FIXED) description = 'Fixed (Scale=' + this.getScale() + ')';
  4409. return description;
  4410. }
  4411. makePrecise() {
  4412. if (typeof arguments[0] === 'number') {
  4413. const val = arguments[0];
  4414. if (Double.isNaN(val)) return val;
  4415. if (this._modelType === PrecisionModel.FLOATING_SINGLE) {
  4416. const floatSingleVal = val;
  4417. return floatSingleVal;
  4418. }
  4419. if (this._modelType === PrecisionModel.FIXED) return Math.round(val * this._scale) / this._scale;
  4420. return val;
  4421. } else if (arguments[0] instanceof Coordinate) {
  4422. const coord = arguments[0];
  4423. if (this._modelType === PrecisionModel.FLOATING) return null;
  4424. coord.x = this.makePrecise(coord.x);
  4425. coord.y = this.makePrecise(coord.y);
  4426. }
  4427. }
  4428. getMaximumSignificantDigits() {
  4429. let maxSigDigits = 16;
  4430. if (this._modelType === PrecisionModel.FLOATING) maxSigDigits = 16;else if (this._modelType === PrecisionModel.FLOATING_SINGLE) maxSigDigits = 6;else if (this._modelType === PrecisionModel.FIXED) maxSigDigits = 1 + Math.trunc(Math.ceil(Math.log(this.getScale()) / Math.log(10)));
  4431. return maxSigDigits;
  4432. }
  4433. setScale(scale) {
  4434. this._scale = Math.abs(scale);
  4435. }
  4436. get interfaces_() {
  4437. return [Serializable, Comparable];
  4438. }
  4439. }
  4440. class Type {
  4441. constructor() {
  4442. Type.constructor_.apply(this, arguments);
  4443. }
  4444. static constructor_() {
  4445. this._name = null;
  4446. const name = arguments[0];
  4447. this._name = name;
  4448. Type.nameToTypeMap.put(name, this);
  4449. }
  4450. readResolve() {
  4451. return Type.nameToTypeMap.get(this._name);
  4452. }
  4453. toString() {
  4454. return this._name;
  4455. }
  4456. get interfaces_() {
  4457. return [Serializable];
  4458. }
  4459. }
  4460. Type.nameToTypeMap = new HashMap();
  4461. PrecisionModel.Type = Type;
  4462. PrecisionModel.FIXED = new Type('FIXED');
  4463. PrecisionModel.FLOATING = new Type('FLOATING');
  4464. PrecisionModel.FLOATING_SINGLE = new Type('FLOATING SINGLE');
  4465. PrecisionModel.maximumPreciseValue = 9007199254740992.0;
  4466. class MultiLineString extends GeometryCollection {
  4467. constructor() {
  4468. super();
  4469. MultiLineString.constructor_.apply(this, arguments);
  4470. }
  4471. static constructor_() {
  4472. const lineStrings = arguments[0],
  4473. factory = arguments[1];
  4474. GeometryCollection.constructor_.call(this, lineStrings, factory);
  4475. }
  4476. copyInternal() {
  4477. const lineStrings = new Array(this._geometries.length).fill(null);
  4478. for (let i = 0; i < lineStrings.length; i++) lineStrings[i] = this._geometries[i].copy();
  4479. return new MultiLineString(lineStrings, this._factory);
  4480. }
  4481. equalsExact() {
  4482. if (arguments.length === 2 && typeof arguments[1] === 'number' && arguments[0] instanceof Geometry) {
  4483. const other = arguments[0],
  4484. tolerance = arguments[1];
  4485. if (!this.isEquivalentClass(other)) return false;
  4486. return super.equalsExact.call(this, other, tolerance);
  4487. } else {
  4488. return super.equalsExact.apply(this, arguments);
  4489. }
  4490. }
  4491. getBoundaryDimension() {
  4492. if (this.isClosed()) return Dimension.FALSE;
  4493. return 0;
  4494. }
  4495. isClosed() {
  4496. if (this.isEmpty()) return false;
  4497. for (let i = 0; i < this._geometries.length; i++) if (!this._geometries[i].isClosed()) return false;
  4498. return true;
  4499. }
  4500. getTypeCode() {
  4501. return Geometry.TYPECODE_MULTILINESTRING;
  4502. }
  4503. getDimension() {
  4504. return 1;
  4505. }
  4506. getBoundary() {
  4507. throw new UnsupportedOperationException();
  4508. }
  4509. getGeometryType() {
  4510. return Geometry.TYPENAME_MULTILINESTRING;
  4511. }
  4512. get interfaces_() {
  4513. return [Lineal];
  4514. }
  4515. }
  4516. class GeometryFactory {
  4517. constructor() {
  4518. GeometryFactory.constructor_.apply(this, arguments);
  4519. }
  4520. static constructor_() {
  4521. this._precisionModel = null;
  4522. this._coordinateSequenceFactory = null;
  4523. this._SRID = null;
  4524. if (arguments.length === 0) {
  4525. GeometryFactory.constructor_.call(this, new PrecisionModel(), 0);
  4526. } else if (arguments.length === 1) {
  4527. if (hasInterface(arguments[0], CoordinateSequenceFactory)) {
  4528. const coordinateSequenceFactory = arguments[0];
  4529. GeometryFactory.constructor_.call(this, new PrecisionModel(), 0, coordinateSequenceFactory);
  4530. } else if (arguments[0] instanceof PrecisionModel) {
  4531. const precisionModel = arguments[0];
  4532. GeometryFactory.constructor_.call(this, precisionModel, 0, GeometryFactory.getDefaultCoordinateSequenceFactory());
  4533. }
  4534. } else if (arguments.length === 2) {
  4535. const precisionModel = arguments[0],
  4536. SRID = arguments[1];
  4537. GeometryFactory.constructor_.call(this, precisionModel, SRID, GeometryFactory.getDefaultCoordinateSequenceFactory());
  4538. } else if (arguments.length === 3) {
  4539. const precisionModel = arguments[0],
  4540. SRID = arguments[1],
  4541. coordinateSequenceFactory = arguments[2];
  4542. this._precisionModel = precisionModel;
  4543. this._coordinateSequenceFactory = coordinateSequenceFactory;
  4544. this._SRID = SRID;
  4545. }
  4546. }
  4547. static toMultiPolygonArray(multiPolygons) {
  4548. const multiPolygonArray = new Array(multiPolygons.size()).fill(null);
  4549. return multiPolygons.toArray(multiPolygonArray);
  4550. }
  4551. static toGeometryArray(geometries) {
  4552. if (geometries === null) return null;
  4553. const geometryArray = new Array(geometries.size()).fill(null);
  4554. return geometries.toArray(geometryArray);
  4555. }
  4556. static getDefaultCoordinateSequenceFactory() {
  4557. return CoordinateArraySequenceFactory.instance();
  4558. }
  4559. static toMultiLineStringArray(multiLineStrings) {
  4560. const multiLineStringArray = new Array(multiLineStrings.size()).fill(null);
  4561. return multiLineStrings.toArray(multiLineStringArray);
  4562. }
  4563. static toLineStringArray(lineStrings) {
  4564. const lineStringArray = new Array(lineStrings.size()).fill(null);
  4565. return lineStrings.toArray(lineStringArray);
  4566. }
  4567. static toMultiPointArray(multiPoints) {
  4568. const multiPointArray = new Array(multiPoints.size()).fill(null);
  4569. return multiPoints.toArray(multiPointArray);
  4570. }
  4571. static toLinearRingArray(linearRings) {
  4572. const linearRingArray = new Array(linearRings.size()).fill(null);
  4573. return linearRings.toArray(linearRingArray);
  4574. }
  4575. static toPointArray(points) {
  4576. const pointArray = new Array(points.size()).fill(null);
  4577. return points.toArray(pointArray);
  4578. }
  4579. static toPolygonArray(polygons) {
  4580. const polygonArray = new Array(polygons.size()).fill(null);
  4581. return polygons.toArray(polygonArray);
  4582. }
  4583. static createPointFromInternalCoord(coord, exemplar) {
  4584. exemplar.getPrecisionModel().makePrecise(coord);
  4585. return exemplar.getFactory().createPoint(coord);
  4586. }
  4587. createEmpty(dimension) {
  4588. switch (dimension) {
  4589. case -1:
  4590. return this.createGeometryCollection();
  4591. case 0:
  4592. return this.createPoint();
  4593. case 1:
  4594. return this.createLineString();
  4595. case 2:
  4596. return this.createPolygon();
  4597. default:
  4598. throw new IllegalArgumentException('Invalid dimension: ' + dimension);
  4599. }
  4600. }
  4601. toGeometry(envelope) {
  4602. if (envelope.isNull()) return this.createPoint();
  4603. if (envelope.getMinX() === envelope.getMaxX() && envelope.getMinY() === envelope.getMaxY()) return this.createPoint(new Coordinate(envelope.getMinX(), envelope.getMinY()));
  4604. if (envelope.getMinX() === envelope.getMaxX() || envelope.getMinY() === envelope.getMaxY()) return this.createLineString([new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY())]);
  4605. return this.createPolygon(this.createLinearRing([new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMinY())]), null);
  4606. }
  4607. createLineString() {
  4608. if (arguments.length === 0) return this.createLineString(this.getCoordinateSequenceFactory().create([]));else if (arguments.length === 1) if (arguments[0] instanceof Array) {
  4609. const coordinates = arguments[0];
  4610. return this.createLineString(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null);
  4611. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  4612. const coordinates = arguments[0];
  4613. return new LineString(coordinates, this);
  4614. }
  4615. }
  4616. createMultiLineString() {
  4617. if (arguments.length === 0) {
  4618. return new MultiLineString(null, this);
  4619. } else if (arguments.length === 1) {
  4620. const lineStrings = arguments[0];
  4621. return new MultiLineString(lineStrings, this);
  4622. }
  4623. }
  4624. buildGeometry(geomList) {
  4625. let geomType = null;
  4626. let isHeterogeneous = false;
  4627. let hasGeometryCollection = false;
  4628. for (let i = geomList.iterator(); i.hasNext();) {
  4629. const geom = i.next();
  4630. const partType = geom.getTypeCode();
  4631. if (geomType === null) geomType = partType;
  4632. if (partType !== geomType) isHeterogeneous = true;
  4633. if (geom instanceof GeometryCollection) hasGeometryCollection = true;
  4634. }
  4635. if (geomType === null) return this.createGeometryCollection();
  4636. if (isHeterogeneous || hasGeometryCollection) return this.createGeometryCollection(GeometryFactory.toGeometryArray(geomList));
  4637. const geom0 = geomList.iterator().next();
  4638. const isCollection = geomList.size() > 1;
  4639. if (isCollection) {
  4640. if (geom0 instanceof Polygon) return this.createMultiPolygon(GeometryFactory.toPolygonArray(geomList));else if (geom0 instanceof LineString) return this.createMultiLineString(GeometryFactory.toLineStringArray(geomList));else if (geom0 instanceof Point) return this.createMultiPoint(GeometryFactory.toPointArray(geomList));
  4641. Assert.shouldNeverReachHere('Unhandled geometry type: ' + geom0.getGeometryType());
  4642. }
  4643. return geom0;
  4644. }
  4645. createMultiPointFromCoords(coordinates) {
  4646. return this.createMultiPoint(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null);
  4647. }
  4648. createPoint() {
  4649. if (arguments.length === 0) return this.createPoint(this.getCoordinateSequenceFactory().create([]));else if (arguments.length === 1) if (arguments[0] instanceof Coordinate) {
  4650. const coordinate = arguments[0];
  4651. return this.createPoint(coordinate !== null ? this.getCoordinateSequenceFactory().create([coordinate]) : null);
  4652. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  4653. const coordinates = arguments[0];
  4654. return new Point(coordinates, this);
  4655. }
  4656. }
  4657. getCoordinateSequenceFactory() {
  4658. return this._coordinateSequenceFactory;
  4659. }
  4660. createPolygon() {
  4661. if (arguments.length === 0) {
  4662. return this.createPolygon(null, null);
  4663. } else if (arguments.length === 1) {
  4664. if (hasInterface(arguments[0], CoordinateSequence)) {
  4665. const shell = arguments[0];
  4666. return this.createPolygon(this.createLinearRing(shell));
  4667. } else if (arguments[0] instanceof Array) {
  4668. const shell = arguments[0];
  4669. return this.createPolygon(this.createLinearRing(shell));
  4670. } else if (arguments[0] instanceof LinearRing) {
  4671. const shell = arguments[0];
  4672. return this.createPolygon(shell, null);
  4673. }
  4674. } else if (arguments.length === 2) {
  4675. const shell = arguments[0],
  4676. holes = arguments[1];
  4677. return new Polygon(shell, holes, this);
  4678. }
  4679. }
  4680. getSRID() {
  4681. return this._SRID;
  4682. }
  4683. createGeometryCollection() {
  4684. if (arguments.length === 0) {
  4685. return new GeometryCollection(null, this);
  4686. } else if (arguments.length === 1) {
  4687. const geometries = arguments[0];
  4688. return new GeometryCollection(geometries, this);
  4689. }
  4690. }
  4691. getPrecisionModel() {
  4692. return this._precisionModel;
  4693. }
  4694. createLinearRing() {
  4695. if (arguments.length === 0) return this.createLinearRing(this.getCoordinateSequenceFactory().create([]));else if (arguments.length === 1) if (arguments[0] instanceof Array) {
  4696. const coordinates = arguments[0];
  4697. return this.createLinearRing(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null);
  4698. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  4699. const coordinates = arguments[0];
  4700. return new LinearRing(coordinates, this);
  4701. }
  4702. }
  4703. createMultiPolygon() {
  4704. if (arguments.length === 0) {
  4705. return new MultiPolygon(null, this);
  4706. } else if (arguments.length === 1) {
  4707. const polygons = arguments[0];
  4708. return new MultiPolygon(polygons, this);
  4709. }
  4710. }
  4711. createMultiPoint() {
  4712. if (arguments.length === 0) return new MultiPoint(null, this);else if (arguments.length === 1) if (arguments[0] instanceof Array) {
  4713. const point = arguments[0];
  4714. return new MultiPoint(point, this);
  4715. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  4716. const coordinates = arguments[0];
  4717. if (coordinates === null) return this.createMultiPoint(new Array(0).fill(null));
  4718. const points = new Array(coordinates.size()).fill(null);
  4719. for (let i = 0; i < coordinates.size(); i++) {
  4720. const ptSeq = this.getCoordinateSequenceFactory().create(1, coordinates.getDimension(), coordinates.getMeasures());
  4721. CoordinateSequences.copy(coordinates, i, ptSeq, 0, 1);
  4722. points[i] = this.createPoint(ptSeq);
  4723. }
  4724. return this.createMultiPoint(points);
  4725. }
  4726. }
  4727. get interfaces_() {
  4728. return [Serializable];
  4729. }
  4730. }
  4731. /**
  4732. * The coordinate layout for geometries, indicating whether a 3rd or 4th z ('Z')
  4733. * or measure ('M') coordinate is available. Supported values are `'XY'`,
  4734. * `'XYZ'`, `'XYM'`, `'XYZM'`.
  4735. * @enum {string}
  4736. */
  4737. const GeometryLayout = {
  4738. XY: 'XY',
  4739. XYZ: 'XYZ',
  4740. XYM: 'XYM',
  4741. XYZM: 'XYZM'
  4742. };
  4743. /**
  4744. * The geometry type. One of `'Point'`, `'LineString'`, `'LinearRing'`,
  4745. * `'Polygon'`, `'MultiPoint'`, `'MultiLineString'`, `'MultiPolygon'`,
  4746. * `'GeometryCollection'`, `'Circle'`.
  4747. * @enum {string}
  4748. */
  4749. const GeometryType = {
  4750. POINT: 'Point',
  4751. LINE_STRING: 'LineString',
  4752. LINEAR_RING: 'LinearRing',
  4753. POLYGON: 'Polygon',
  4754. MULTI_POINT: 'MultiPoint',
  4755. MULTI_LINE_STRING: 'MultiLineString',
  4756. MULTI_POLYGON: 'MultiPolygon',
  4757. GEOMETRY_COLLECTION: 'GeometryCollection',
  4758. CIRCLE: 'Circle'
  4759. };
  4760. /**
  4761. * @typedef {Object} Options
  4762. * @property {boolean} [splitCollection=false] Whether to split GeometryCollections into
  4763. * multiple features on reading.
  4764. */
  4765. /**
  4766. * @typedef {Object} Token
  4767. * @property {number} type
  4768. * @property {number|string} [value]
  4769. * @property {number} position
  4770. */
  4771. /**
  4772. * @const
  4773. * @type {string}
  4774. */
  4775. const EMPTY = 'EMPTY';
  4776. /**
  4777. * @const
  4778. * @type {string}
  4779. */
  4780. const Z = 'Z';
  4781. /**
  4782. * @const
  4783. * @type {string}
  4784. */
  4785. const M = 'M';
  4786. /**
  4787. * @const
  4788. * @type {string}
  4789. */
  4790. const ZM = 'ZM';
  4791. /**
  4792. * @const
  4793. * @enum {number}
  4794. */
  4795. const TokenType = {
  4796. TEXT: 1,
  4797. LEFT_PAREN: 2,
  4798. RIGHT_PAREN: 3,
  4799. NUMBER: 4,
  4800. COMMA: 5,
  4801. EOF: 6
  4802. };
  4803. /**
  4804. * @const
  4805. * @type {Object<string, string>}
  4806. */
  4807. const WKTGeometryType = {};
  4808. for (const type in GeometryType) WKTGeometryType[type] = GeometryType[type].toUpperCase();
  4809. /**
  4810. * Class to tokenize a WKT string.
  4811. */
  4812. class Lexer {
  4813. /**
  4814. * @param {string} wkt WKT string.
  4815. */
  4816. constructor(wkt) {
  4817. /**
  4818. * @type {string}
  4819. */
  4820. this.wkt = wkt;
  4821. /**
  4822. * @type {number}
  4823. * @private
  4824. */
  4825. this.index_ = -1;
  4826. }
  4827. /**
  4828. * @param {string} c Character.
  4829. * @return {boolean} Whether the character is alphabetic.
  4830. * @private
  4831. */
  4832. isAlpha_(c) {
  4833. return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
  4834. }
  4835. /**
  4836. * @param {string} c Character.
  4837. * @param {boolean=} opt_decimal Whether the string number
  4838. * contains a dot, i.e. is a decimal number.
  4839. * @return {boolean} Whether the character is numeric.
  4840. * @private
  4841. */
  4842. isNumeric_(c, opt_decimal) {
  4843. const decimal = opt_decimal !== undefined ? opt_decimal : false;
  4844. return c >= '0' && c <= '9' || c == '.' && !decimal;
  4845. }
  4846. /**
  4847. * @param {string} c Character.
  4848. * @return {boolean} Whether the character is whitespace.
  4849. * @private
  4850. */
  4851. isWhiteSpace_(c) {
  4852. return c == ' ' || c == '\t' || c == '\r' || c == '\n';
  4853. }
  4854. /**
  4855. * @return {string} Next string character.
  4856. * @private
  4857. */
  4858. nextChar_() {
  4859. return this.wkt.charAt(++this.index_);
  4860. }
  4861. /**
  4862. * Fetch and return the next token.
  4863. * @return {!Token} Next string token.
  4864. */
  4865. nextToken() {
  4866. const c = this.nextChar_();
  4867. const position = this.index_;
  4868. /** @type {number|string} */
  4869. let value = c;
  4870. let type;
  4871. if (c == '(') {
  4872. type = TokenType.LEFT_PAREN;
  4873. } else if (c == ',') {
  4874. type = TokenType.COMMA;
  4875. } else if (c == ')') {
  4876. type = TokenType.RIGHT_PAREN;
  4877. } else if (this.isNumeric_(c) || c == '-') {
  4878. type = TokenType.NUMBER;
  4879. value = this.readNumber_();
  4880. } else if (this.isAlpha_(c)) {
  4881. type = TokenType.TEXT;
  4882. value = this.readText_();
  4883. } else if (this.isWhiteSpace_(c)) {
  4884. return this.nextToken();
  4885. } else if (c === '') {
  4886. type = TokenType.EOF;
  4887. } else {
  4888. throw new Error('Unexpected character: ' + c);
  4889. }
  4890. return {
  4891. position: position,
  4892. value: value,
  4893. type: type
  4894. };
  4895. }
  4896. /**
  4897. * @return {number} Numeric token value.
  4898. * @private
  4899. */
  4900. readNumber_() {
  4901. let c;
  4902. const index = this.index_;
  4903. let decimal = false;
  4904. let scientificNotation = false;
  4905. do {
  4906. if (c == '.') decimal = true;else if (c == 'e' || c == 'E') scientificNotation = true;
  4907. c = this.nextChar_();
  4908. } while (this.isNumeric_(c, decimal) || // if we haven't detected a scientific number before, 'e' or 'E'
  4909. // hint that we should continue to read
  4910. !scientificNotation && (c == 'e' || c == 'E') || // once we know that we have a scientific number, both '-' and '+'
  4911. // are allowed
  4912. scientificNotation && (c == '-' || c == '+'));
  4913. return parseFloat(this.wkt.substring(index, this.index_--));
  4914. }
  4915. /**
  4916. * @return {string} String token value.
  4917. * @private
  4918. */
  4919. readText_() {
  4920. let c;
  4921. const index = this.index_;
  4922. do c = this.nextChar_(); while (this.isAlpha_(c));
  4923. return this.wkt.substring(index, this.index_--).toUpperCase();
  4924. }
  4925. }
  4926. /**
  4927. * Class to parse the tokens from the WKT string.
  4928. */
  4929. class Parser {
  4930. /**
  4931. * @param {Lexer} lexer The lexer.
  4932. */
  4933. constructor(lexer, factory) {
  4934. /**
  4935. * @type {Lexer}
  4936. * @private
  4937. */
  4938. this.lexer_ = lexer;
  4939. /**
  4940. * @type {Token}
  4941. * @private
  4942. */
  4943. this.token_;
  4944. /**
  4945. * @type {import("../geom/GeometryLayout.js").default}
  4946. * @private
  4947. */
  4948. this.layout_ = GeometryLayout.XY;
  4949. this.factory = factory;
  4950. }
  4951. /**
  4952. * Fetch the next token form the lexer and replace the active token.
  4953. * @private
  4954. */
  4955. consume_() {
  4956. this.token_ = this.lexer_.nextToken();
  4957. }
  4958. /**
  4959. * Tests if the given type matches the type of the current token.
  4960. * @param {TokenType} type Token type.
  4961. * @return {boolean} Whether the token matches the given type.
  4962. */
  4963. isTokenType(type) {
  4964. const isMatch = this.token_.type == type;
  4965. return isMatch;
  4966. }
  4967. /**
  4968. * If the given type matches the current token, consume it.
  4969. * @param {TokenType} type Token type.
  4970. * @return {boolean} Whether the token matches the given type.
  4971. */
  4972. match(type) {
  4973. const isMatch = this.isTokenType(type);
  4974. if (isMatch) this.consume_();
  4975. return isMatch;
  4976. }
  4977. /**
  4978. * Try to parse the tokens provided by the lexer.
  4979. * @return {import("../geom/Geometry.js").default} The geometry.
  4980. */
  4981. parse() {
  4982. this.consume_();
  4983. const geometry = this.parseGeometry_();
  4984. return geometry;
  4985. }
  4986. /**
  4987. * Try to parse the dimensional info.
  4988. * @return {import("../geom/GeometryLayout.js").default} The layout.
  4989. * @private
  4990. */
  4991. parseGeometryLayout_() {
  4992. let layout = GeometryLayout.XY;
  4993. const dimToken = this.token_;
  4994. if (this.isTokenType(TokenType.TEXT)) {
  4995. const dimInfo = dimToken.value;
  4996. if (dimInfo === Z) layout = GeometryLayout.XYZ;else if (dimInfo === M) layout = GeometryLayout.XYM;else if (dimInfo === ZM) layout = GeometryLayout.XYZM;
  4997. if (layout !== GeometryLayout.XY) this.consume_();
  4998. }
  4999. return layout;
  5000. }
  5001. /**
  5002. * @return {!Array<import("../geom/Geometry.js").default>} A collection of geometries.
  5003. * @private
  5004. */
  5005. parseGeometryCollectionText_() {
  5006. if (this.match(TokenType.LEFT_PAREN)) {
  5007. const geometries = [];
  5008. do geometries.push(this.parseGeometry_()); while (this.match(TokenType.COMMA));
  5009. if (this.match(TokenType.RIGHT_PAREN)) return geometries;
  5010. } else if (this.isEmptyGeometry_()) {
  5011. return [];
  5012. }
  5013. throw new Error(this.formatErrorMessage_());
  5014. }
  5015. /**
  5016. * @return {Array<number>} All values in a point.
  5017. * @private
  5018. */
  5019. parsePointText_() {
  5020. if (this.match(TokenType.LEFT_PAREN)) {
  5021. const coordinates = this.parsePoint_();
  5022. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5023. } else if (this.isEmptyGeometry_()) {
  5024. return null;
  5025. }
  5026. throw new Error(this.formatErrorMessage_());
  5027. }
  5028. /**
  5029. * @return {!Array<!Array<number>>} All points in a linestring.
  5030. * @private
  5031. */
  5032. parseLineStringText_() {
  5033. if (this.match(TokenType.LEFT_PAREN)) {
  5034. const coordinates = this.parsePointList_();
  5035. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5036. } else if (this.isEmptyGeometry_()) {
  5037. return [];
  5038. }
  5039. throw new Error(this.formatErrorMessage_());
  5040. }
  5041. /**
  5042. * @return {!Array<!Array<!Array<number>>>} All points in a polygon.
  5043. * @private
  5044. */
  5045. parsePolygonText_() {
  5046. if (this.match(TokenType.LEFT_PAREN)) {
  5047. const coordinates = this.parseLineStringTextList_();
  5048. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5049. } else if (this.isEmptyGeometry_()) {
  5050. return [];
  5051. }
  5052. throw new Error(this.formatErrorMessage_());
  5053. }
  5054. /**
  5055. * @return {!Array<!Array<number>>} All points in a multipoint.
  5056. * @private
  5057. */
  5058. parseMultiPointText_() {
  5059. if (this.match(TokenType.LEFT_PAREN)) {
  5060. let coordinates;
  5061. if (this.token_.type == TokenType.LEFT_PAREN) coordinates = this.parsePointTextList_();else coordinates = this.parsePointList_();
  5062. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5063. } else if (this.isEmptyGeometry_()) {
  5064. return [];
  5065. }
  5066. throw new Error(this.formatErrorMessage_());
  5067. }
  5068. /**
  5069. * @return {!Array<!Array<!Array<number>>>} All linestring points
  5070. * in a multilinestring.
  5071. * @private
  5072. */
  5073. parseMultiLineStringText_() {
  5074. if (this.match(TokenType.LEFT_PAREN)) {
  5075. const coordinates = this.parseLineStringTextList_();
  5076. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5077. } else if (this.isEmptyGeometry_()) {
  5078. return [];
  5079. }
  5080. throw new Error(this.formatErrorMessage_());
  5081. }
  5082. /**
  5083. * @return {!Array<!Array<!Array<!Array<number>>>>} All polygon points in a multipolygon.
  5084. * @private
  5085. */
  5086. parseMultiPolygonText_() {
  5087. if (this.match(TokenType.LEFT_PAREN)) {
  5088. const coordinates = this.parsePolygonTextList_();
  5089. if (this.match(TokenType.RIGHT_PAREN)) return coordinates;
  5090. } else if (this.isEmptyGeometry_()) {
  5091. return [];
  5092. }
  5093. throw new Error(this.formatErrorMessage_());
  5094. }
  5095. /**
  5096. * @return {!Array<number>} A point.
  5097. * @private
  5098. */
  5099. parsePoint_() {
  5100. const coordinates = [];
  5101. const dimensions = this.layout_.length;
  5102. for (let i = 0; i < dimensions; ++i) {
  5103. const token = this.token_;
  5104. if (this.match(TokenType.NUMBER)) coordinates.push(
  5105. /** @type {number} */
  5106. token.value);else break;
  5107. }
  5108. if (coordinates.length == dimensions) return coordinates;
  5109. throw new Error(this.formatErrorMessage_());
  5110. }
  5111. /**
  5112. * @return {!Array<!Array<number>>} An array of points.
  5113. * @private
  5114. */
  5115. parsePointList_() {
  5116. const coordinates = [this.parsePoint_()];
  5117. while (this.match(TokenType.COMMA)) coordinates.push(this.parsePoint_());
  5118. return coordinates;
  5119. }
  5120. /**
  5121. * @return {!Array<!Array<number>>} An array of points.
  5122. * @private
  5123. */
  5124. parsePointTextList_() {
  5125. const coordinates = [this.parsePointText_()];
  5126. while (this.match(TokenType.COMMA)) coordinates.push(this.parsePointText_());
  5127. return coordinates;
  5128. }
  5129. /**
  5130. * @return {!Array<!Array<!Array<number>>>} An array of points.
  5131. * @private
  5132. */
  5133. parseLineStringTextList_() {
  5134. const coordinates = [this.parseLineStringText_()];
  5135. while (this.match(TokenType.COMMA)) coordinates.push(this.parseLineStringText_());
  5136. return coordinates;
  5137. }
  5138. /**
  5139. * @return {!Array<!Array<!Array<!Array<number>>>>} An array of points.
  5140. * @private
  5141. */
  5142. parsePolygonTextList_() {
  5143. const coordinates = [this.parsePolygonText_()];
  5144. while (this.match(TokenType.COMMA)) coordinates.push(this.parsePolygonText_());
  5145. return coordinates;
  5146. }
  5147. /**
  5148. * @return {boolean} Whether the token implies an empty geometry.
  5149. * @private
  5150. */
  5151. isEmptyGeometry_() {
  5152. const isEmpty = this.isTokenType(TokenType.TEXT) && this.token_.value == EMPTY;
  5153. if (isEmpty) this.consume_();
  5154. return isEmpty;
  5155. }
  5156. /**
  5157. * Create an error message for an unexpected token error.
  5158. * @return {string} Error message.
  5159. * @private
  5160. */
  5161. formatErrorMessage_() {
  5162. return 'Unexpected `' + this.token_.value + '` at position ' + this.token_.position + ' in `' + this.lexer_.wkt + '`';
  5163. }
  5164. /**
  5165. * @return {!import("../geom/Geometry.js").default} The geometry.
  5166. * @private
  5167. */
  5168. parseGeometry_() {
  5169. const factory = this.factory;
  5170. const o2c = ordinates => new Coordinate(...ordinates);
  5171. const ca2p = coordinates => {
  5172. const rings = coordinates.map(a => factory.createLinearRing(a.map(o2c)));
  5173. if (rings.length > 1) return factory.createPolygon(rings[0], rings.slice(1));else return factory.createPolygon(rings[0]);
  5174. };
  5175. const token = this.token_;
  5176. if (this.match(TokenType.TEXT)) {
  5177. const geomType = token.value;
  5178. this.layout_ = this.parseGeometryLayout_();
  5179. if (geomType == 'GEOMETRYCOLLECTION') {
  5180. const geometries = this.parseGeometryCollectionText_();
  5181. return factory.createGeometryCollection(geometries);
  5182. } else {
  5183. switch (geomType) {
  5184. case 'POINT':
  5185. {
  5186. const ordinates = this.parsePointText_();
  5187. if (!ordinates) return factory.createPoint();
  5188. return factory.createPoint(new Coordinate(...ordinates));
  5189. }
  5190. case 'LINESTRING':
  5191. {
  5192. const coordinates = this.parseLineStringText_();
  5193. const components = coordinates.map(o2c);
  5194. return factory.createLineString(components);
  5195. }
  5196. case 'LINEARRING':
  5197. {
  5198. const coordinates = this.parseLineStringText_();
  5199. const components = coordinates.map(o2c);
  5200. return factory.createLinearRing(components);
  5201. }
  5202. case 'POLYGON':
  5203. {
  5204. const coordinates = this.parsePolygonText_();
  5205. if (!coordinates || coordinates.length === 0) return factory.createPolygon();
  5206. return ca2p(coordinates);
  5207. }
  5208. case 'MULTIPOINT':
  5209. {
  5210. const coordinates = this.parseMultiPointText_();
  5211. if (!coordinates || coordinates.length === 0) return factory.createMultiPoint();
  5212. const components = coordinates.map(o2c).map(c => factory.createPoint(c));
  5213. return factory.createMultiPoint(components);
  5214. }
  5215. case 'MULTILINESTRING':
  5216. {
  5217. const coordinates = this.parseMultiLineStringText_();
  5218. const components = coordinates.map(a => factory.createLineString(a.map(o2c)));
  5219. return factory.createMultiLineString(components);
  5220. }
  5221. case 'MULTIPOLYGON':
  5222. {
  5223. const coordinates = this.parseMultiPolygonText_();
  5224. if (!coordinates || coordinates.length === 0) return factory.createMultiPolygon();
  5225. const polygons = coordinates.map(ca2p);
  5226. return factory.createMultiPolygon(polygons);
  5227. }
  5228. default:
  5229. {
  5230. throw new Error('Invalid geometry type: ' + geomType);
  5231. }
  5232. }
  5233. }
  5234. }
  5235. throw new Error(this.formatErrorMessage_());
  5236. }
  5237. }
  5238. /**
  5239. * @param {Point} geom Point geometry.
  5240. * @return {string} Coordinates part of Point as WKT.
  5241. */
  5242. function encodePointGeometry(geom) {
  5243. if (geom.isEmpty()) return '';
  5244. const c = geom.getCoordinate();
  5245. const cs = [c.x, c.y];
  5246. if (c.z !== undefined && !Number.isNaN(c.z)) cs.push(c.z);
  5247. if (c.m !== undefined && !Number.isNaN(c.m)) cs.push(c.m);
  5248. return cs.join(' ');
  5249. }
  5250. /**
  5251. * @param {MultiPoint} geom MultiPoint geometry.
  5252. * @return {string} Coordinates part of MultiPoint as WKT.
  5253. */
  5254. function encodeMultiPointGeometry(geom) {
  5255. const array = [];
  5256. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i) array.push('(' + encodePointGeometry(geom.getGeometryN(i)) + ')');
  5257. return array.join(', ');
  5258. }
  5259. /**
  5260. * @param {GeometryCollection} geom GeometryCollection geometry.
  5261. * @return {string} Coordinates part of GeometryCollection as WKT.
  5262. */
  5263. function encodeGeometryCollectionGeometry(geom) {
  5264. const array = [];
  5265. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i) array.push(encode(geom.getGeometryN(i)));
  5266. return array.join(', ');
  5267. }
  5268. /**
  5269. * @param {LineString|import("../geom/LinearRing.js").default} geom LineString geometry.
  5270. * @return {string} Coordinates part of LineString as WKT.
  5271. */
  5272. function encodeLineStringGeometry(geom) {
  5273. const coordinates = geom.getCoordinates().map(c => {
  5274. const a = [c.x, c.y];
  5275. if (c.z !== undefined && !Number.isNaN(c.z)) a.push(c.z);
  5276. if (c.m !== undefined && !Number.isNaN(c.m)) a.push(c.m);
  5277. return a;
  5278. });
  5279. const array = [];
  5280. for (let i = 0, ii = coordinates.length; i < ii; ++i) array.push(coordinates[i].join(' '));
  5281. return array.join(', ');
  5282. }
  5283. /**
  5284. * @param {MultiLineString} geom MultiLineString geometry.
  5285. * @return {string} Coordinates part of MultiLineString as WKT.
  5286. */
  5287. function encodeMultiLineStringGeometry(geom) {
  5288. const array = [];
  5289. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i) array.push('(' + encodeLineStringGeometry(geom.getGeometryN(i)) + ')');
  5290. return array.join(', ');
  5291. }
  5292. /**
  5293. * @param {Polygon} geom Polygon geometry.
  5294. * @return {string} Coordinates part of Polygon as WKT.
  5295. */
  5296. function encodePolygonGeometry(geom) {
  5297. const array = [];
  5298. array.push('(' + encodeLineStringGeometry(geom.getExteriorRing()) + ')');
  5299. for (let i = 0, ii = geom.getNumInteriorRing(); i < ii; ++i) array.push('(' + encodeLineStringGeometry(geom.getInteriorRingN(i)) + ')');
  5300. return array.join(', ');
  5301. }
  5302. /**
  5303. * @param {MultiPolygon} geom MultiPolygon geometry.
  5304. * @return {string} Coordinates part of MultiPolygon as WKT.
  5305. */
  5306. function encodeMultiPolygonGeometry(geom) {
  5307. const array = [];
  5308. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i) array.push('(' + encodePolygonGeometry(geom.getGeometryN(i)) + ')');
  5309. return array.join(', ');
  5310. }
  5311. /**
  5312. * @param {Geometry} geom Geometry geometry.
  5313. * @return {string} Potential dimensional information for WKT type.
  5314. */
  5315. function encodeGeometryLayout(geom) {
  5316. let dimInfo = '';
  5317. if (geom.isEmpty()) return dimInfo;
  5318. const c = geom.getCoordinate();
  5319. if (c.z !== undefined && !Number.isNaN(c.z)) dimInfo += Z;
  5320. if (c.m !== undefined && !Number.isNaN(c.m)) dimInfo += M;
  5321. return dimInfo;
  5322. }
  5323. /**
  5324. * @const
  5325. * @type {Object<string, function(import("../geom/Geometry.js").default): string>}
  5326. */
  5327. const GeometryEncoder = {
  5328. 'Point': encodePointGeometry,
  5329. 'LineString': encodeLineStringGeometry,
  5330. 'LinearRing': encodeLineStringGeometry,
  5331. 'Polygon': encodePolygonGeometry,
  5332. 'MultiPoint': encodeMultiPointGeometry,
  5333. 'MultiLineString': encodeMultiLineStringGeometry,
  5334. 'MultiPolygon': encodeMultiPolygonGeometry,
  5335. 'GeometryCollection': encodeGeometryCollectionGeometry
  5336. };
  5337. /**
  5338. * Encode a geometry as WKT.
  5339. * @param {!import("../geom/Geometry.js").default} geom The geometry to encode.
  5340. * @return {string} WKT string for the geometry.
  5341. */
  5342. function encode(geom) {
  5343. let type = geom.getGeometryType();
  5344. const geometryEncoder = GeometryEncoder[type];
  5345. type = type.toUpperCase();
  5346. const dimInfo = encodeGeometryLayout(geom);
  5347. if (dimInfo.length > 0) type += ' ' + dimInfo;
  5348. if (geom.isEmpty()) return type + ' ' + EMPTY;
  5349. const enc = geometryEncoder(geom);
  5350. return type + ' (' + enc + ')';
  5351. }
  5352. /**
  5353. * Class for reading and writing Well-Known Text.
  5354. *
  5355. * NOTE: Adapted from OpenLayers.
  5356. */
  5357. class WKTParser {
  5358. /** Create a new parser for WKT
  5359. *
  5360. * @param {GeometryFactory} geometryFactory
  5361. * @return An instance of WKTParser.
  5362. * @private
  5363. */
  5364. constructor(geometryFactory) {
  5365. this.geometryFactory = geometryFactory || new GeometryFactory();
  5366. this.precisionModel = this.geometryFactory.getPrecisionModel();
  5367. }
  5368. /**
  5369. * Deserialize a WKT string and return a geometry. Supports WKT for POINT,
  5370. * MULTIPOINT, LINESTRING, LINEARRING, MULTILINESTRING, POLYGON, MULTIPOLYGON,
  5371. * and GEOMETRYCOLLECTION.
  5372. *
  5373. * @param {String} wkt A WKT string.
  5374. * @return {Geometry} A geometry instance.
  5375. * @private
  5376. */
  5377. read(wkt) {
  5378. const lexer = new Lexer(wkt);
  5379. const parser = new Parser(lexer, this.geometryFactory);
  5380. const geometry = parser.parse();
  5381. return geometry;
  5382. }
  5383. /**
  5384. * Serialize a geometry into a WKT string.
  5385. *
  5386. * @param {Geometry} geometry A feature or array of features.
  5387. * @return {String} The WKT string representation of the input geometries.
  5388. * @private
  5389. */
  5390. write(geometry) {
  5391. return encode(geometry);
  5392. }
  5393. }
  5394. /**
  5395. * @module org/locationtech/jts/io/WKTWriter
  5396. */
  5397. /**
  5398. * Writes the Well-Known Text representation of a {@link Geometry}. The
  5399. * Well-Known Text format is defined in the <A
  5400. * HREF="http://www.opengis.org/techno/specs.htm"> OGC Simple Features
  5401. * Specification for SQL</A>.
  5402. * <p>
  5403. * The <code>WKTWriter</code> outputs coordinates rounded to the precision
  5404. * model. Only the maximum number of decimal places necessary to represent the
  5405. * ordinates to the required precision will be output.
  5406. * <p>
  5407. * The SFS WKT spec does not define a special tag for {@link LinearRing}s.
  5408. * Under the spec, rings are output as <code>LINESTRING</code>s.
  5409. */
  5410. class WKTWriter {
  5411. /**
  5412. * @param {GeometryFactory} geometryFactory
  5413. */
  5414. constructor(geometryFactory) {
  5415. this.parser = new WKTParser(geometryFactory);
  5416. }
  5417. /**
  5418. * Converts a <code>Geometry</code> to its Well-known Text representation.
  5419. *
  5420. * @param {Geometry} geometry a <code>Geometry</code> to process.
  5421. * @return {string} a <Geometry Tagged Text> string (see the OpenGIS Simple
  5422. * Features Specification).
  5423. * @memberof module:org/locationtech/jts/io/WKTWriter#
  5424. */
  5425. write(geometry) {
  5426. return this.parser.write(geometry);
  5427. }
  5428. /**
  5429. * Generates the WKT for a <tt>LINESTRING</tt> specified by two
  5430. * {@link Coordinate}s.
  5431. *
  5432. * @param p0 the first coordinate.
  5433. * @param p1 the second coordinate.
  5434. *
  5435. * @return the WKT.
  5436. * @private
  5437. */
  5438. static toLineString(p0, p1) {
  5439. if (arguments.length !== 2) throw new Error('Not implemented');
  5440. return 'LINESTRING ( ' + p0.x + ' ' + p0.y + ', ' + p1.x + ' ' + p1.y + ' )';
  5441. }
  5442. }
  5443. class LineIntersector {
  5444. constructor() {
  5445. LineIntersector.constructor_.apply(this, arguments);
  5446. }
  5447. static constructor_() {
  5448. this._result = null;
  5449. this._inputLines = Array(2).fill().map(() => Array(2));
  5450. this._intPt = new Array(2).fill(null);
  5451. this._intLineIndex = null;
  5452. this._isProper = null;
  5453. this._pa = null;
  5454. this._pb = null;
  5455. this._precisionModel = null;
  5456. this._intPt[0] = new Coordinate();
  5457. this._intPt[1] = new Coordinate();
  5458. this._pa = this._intPt[0];
  5459. this._pb = this._intPt[1];
  5460. this._result = 0;
  5461. }
  5462. static computeEdgeDistance(p, p0, p1) {
  5463. const dx = Math.abs(p1.x - p0.x);
  5464. const dy = Math.abs(p1.y - p0.y);
  5465. let dist = -1.0;
  5466. if (p.equals(p0)) {
  5467. dist = 0.0;
  5468. } else if (p.equals(p1)) {
  5469. if (dx > dy) dist = dx;else dist = dy;
  5470. } else {
  5471. const pdx = Math.abs(p.x - p0.x);
  5472. const pdy = Math.abs(p.y - p0.y);
  5473. if (dx > dy) dist = pdx;else dist = pdy;
  5474. if (dist === 0.0 && !p.equals(p0)) dist = Math.max(pdx, pdy);
  5475. }
  5476. Assert.isTrue(!(dist === 0.0 && !p.equals(p0)), 'Bad distance calculation');
  5477. return dist;
  5478. }
  5479. static nonRobustComputeEdgeDistance(p, p1, p2) {
  5480. const dx = p.x - p1.x;
  5481. const dy = p.y - p1.y;
  5482. const dist = Math.sqrt(dx * dx + dy * dy);
  5483. Assert.isTrue(!(dist === 0.0 && !p.equals(p1)), 'Invalid distance calculation');
  5484. return dist;
  5485. }
  5486. getIndexAlongSegment(segmentIndex, intIndex) {
  5487. this.computeIntLineIndex();
  5488. return this._intLineIndex[segmentIndex][intIndex];
  5489. }
  5490. getTopologySummary() {
  5491. const catBuilder = new StringBuilder();
  5492. if (this.isEndPoint()) catBuilder.append(' endpoint');
  5493. if (this._isProper) catBuilder.append(' proper');
  5494. if (this.isCollinear()) catBuilder.append(' collinear');
  5495. return catBuilder.toString();
  5496. }
  5497. computeIntersection(p1, p2, p3, p4) {
  5498. this._inputLines[0][0] = p1;
  5499. this._inputLines[0][1] = p2;
  5500. this._inputLines[1][0] = p3;
  5501. this._inputLines[1][1] = p4;
  5502. this._result = this.computeIntersect(p1, p2, p3, p4);
  5503. }
  5504. getIntersectionNum() {
  5505. return this._result;
  5506. }
  5507. computeIntLineIndex() {
  5508. if (arguments.length === 0) {
  5509. if (this._intLineIndex === null) {
  5510. this._intLineIndex = Array(2).fill().map(() => Array(2));
  5511. this.computeIntLineIndex(0);
  5512. this.computeIntLineIndex(1);
  5513. }
  5514. } else if (arguments.length === 1) {
  5515. const segmentIndex = arguments[0];
  5516. const dist0 = this.getEdgeDistance(segmentIndex, 0);
  5517. const dist1 = this.getEdgeDistance(segmentIndex, 1);
  5518. if (dist0 > dist1) {
  5519. this._intLineIndex[segmentIndex][0] = 0;
  5520. this._intLineIndex[segmentIndex][1] = 1;
  5521. } else {
  5522. this._intLineIndex[segmentIndex][0] = 1;
  5523. this._intLineIndex[segmentIndex][1] = 0;
  5524. }
  5525. }
  5526. }
  5527. isProper() {
  5528. return this.hasIntersection() && this._isProper;
  5529. }
  5530. setPrecisionModel(precisionModel) {
  5531. this._precisionModel = precisionModel;
  5532. }
  5533. isInteriorIntersection() {
  5534. if (arguments.length === 0) {
  5535. if (this.isInteriorIntersection(0)) return true;
  5536. if (this.isInteriorIntersection(1)) return true;
  5537. return false;
  5538. } else if (arguments.length === 1) {
  5539. const inputLineIndex = arguments[0];
  5540. for (let i = 0; i < this._result; i++) if (!(this._intPt[i].equals2D(this._inputLines[inputLineIndex][0]) || this._intPt[i].equals2D(this._inputLines[inputLineIndex][1]))) return true;
  5541. return false;
  5542. }
  5543. }
  5544. getIntersection(intIndex) {
  5545. return this._intPt[intIndex];
  5546. }
  5547. isEndPoint() {
  5548. return this.hasIntersection() && !this._isProper;
  5549. }
  5550. hasIntersection() {
  5551. return this._result !== LineIntersector.NO_INTERSECTION;
  5552. }
  5553. getEdgeDistance(segmentIndex, intIndex) {
  5554. const dist = LineIntersector.computeEdgeDistance(this._intPt[intIndex], this._inputLines[segmentIndex][0], this._inputLines[segmentIndex][1]);
  5555. return dist;
  5556. }
  5557. isCollinear() {
  5558. return this._result === LineIntersector.COLLINEAR_INTERSECTION;
  5559. }
  5560. toString() {
  5561. return WKTWriter.toLineString(this._inputLines[0][0], this._inputLines[0][1]) + ' - ' + WKTWriter.toLineString(this._inputLines[1][0], this._inputLines[1][1]) + this.getTopologySummary();
  5562. }
  5563. getEndpoint(segmentIndex, ptIndex) {
  5564. return this._inputLines[segmentIndex][ptIndex];
  5565. }
  5566. isIntersection(pt) {
  5567. for (let i = 0; i < this._result; i++) if (this._intPt[i].equals2D(pt)) return true;
  5568. return false;
  5569. }
  5570. getIntersectionAlongSegment(segmentIndex, intIndex) {
  5571. this.computeIntLineIndex();
  5572. return this._intPt[this._intLineIndex[segmentIndex][intIndex]];
  5573. }
  5574. }
  5575. LineIntersector.DONT_INTERSECT = 0;
  5576. LineIntersector.DO_INTERSECT = 1;
  5577. LineIntersector.COLLINEAR = 2;
  5578. LineIntersector.NO_INTERSECTION = 0;
  5579. LineIntersector.POINT_INTERSECTION = 1;
  5580. LineIntersector.COLLINEAR_INTERSECTION = 2;
  5581. class RobustLineIntersector extends LineIntersector {
  5582. constructor() {
  5583. super();
  5584. }
  5585. static nearestEndpoint(p1, p2, q1, q2) {
  5586. let nearestPt = p1;
  5587. let minDist = Distance.pointToSegment(p1, q1, q2);
  5588. let dist = Distance.pointToSegment(p2, q1, q2);
  5589. if (dist < minDist) {
  5590. minDist = dist;
  5591. nearestPt = p2;
  5592. }
  5593. dist = Distance.pointToSegment(q1, p1, p2);
  5594. if (dist < minDist) {
  5595. minDist = dist;
  5596. nearestPt = q1;
  5597. }
  5598. dist = Distance.pointToSegment(q2, p1, p2);
  5599. if (dist < minDist) {
  5600. minDist = dist;
  5601. nearestPt = q2;
  5602. }
  5603. return nearestPt;
  5604. }
  5605. isInSegmentEnvelopes(intPt) {
  5606. const env0 = new Envelope(this._inputLines[0][0], this._inputLines[0][1]);
  5607. const env1 = new Envelope(this._inputLines[1][0], this._inputLines[1][1]);
  5608. return env0.contains(intPt) && env1.contains(intPt);
  5609. }
  5610. computeIntersection() {
  5611. if (arguments.length === 3) {
  5612. const p = arguments[0],
  5613. p1 = arguments[1],
  5614. p2 = arguments[2];
  5615. this._isProper = false;
  5616. if (Envelope.intersects(p1, p2, p)) if (Orientation.index(p1, p2, p) === 0 && Orientation.index(p2, p1, p) === 0) {
  5617. this._isProper = true;
  5618. if (p.equals(p1) || p.equals(p2)) this._isProper = false;
  5619. this._result = LineIntersector.POINT_INTERSECTION;
  5620. return null;
  5621. }
  5622. this._result = LineIntersector.NO_INTERSECTION;
  5623. } else {
  5624. return super.computeIntersection.apply(this, arguments);
  5625. }
  5626. }
  5627. intersection(p1, p2, q1, q2) {
  5628. let intPt = this.intersectionSafe(p1, p2, q1, q2);
  5629. if (!this.isInSegmentEnvelopes(intPt)) intPt = new Coordinate(RobustLineIntersector.nearestEndpoint(p1, p2, q1, q2));
  5630. if (this._precisionModel !== null) this._precisionModel.makePrecise(intPt);
  5631. return intPt;
  5632. }
  5633. checkDD(p1, p2, q1, q2, intPt) {
  5634. const intPtDD = CGAlgorithmsDD.intersection(p1, p2, q1, q2);
  5635. const isIn = this.isInSegmentEnvelopes(intPtDD);
  5636. System.out.println('DD in env = ' + isIn + ' --------------------- ' + intPtDD);
  5637. if (intPt.distance(intPtDD) > 0.0001) System.out.println('Distance = ' + intPt.distance(intPtDD));
  5638. }
  5639. intersectionSafe(p1, p2, q1, q2) {
  5640. let intPt = Intersection.intersection(p1, p2, q1, q2);
  5641. if (intPt === null) intPt = RobustLineIntersector.nearestEndpoint(p1, p2, q1, q2);
  5642. return intPt;
  5643. }
  5644. computeCollinearIntersection(p1, p2, q1, q2) {
  5645. const p1q1p2 = Envelope.intersects(p1, p2, q1);
  5646. const p1q2p2 = Envelope.intersects(p1, p2, q2);
  5647. const q1p1q2 = Envelope.intersects(q1, q2, p1);
  5648. const q1p2q2 = Envelope.intersects(q1, q2, p2);
  5649. if (p1q1p2 && p1q2p2) {
  5650. this._intPt[0] = q1;
  5651. this._intPt[1] = q2;
  5652. return LineIntersector.COLLINEAR_INTERSECTION;
  5653. }
  5654. if (q1p1q2 && q1p2q2) {
  5655. this._intPt[0] = p1;
  5656. this._intPt[1] = p2;
  5657. return LineIntersector.COLLINEAR_INTERSECTION;
  5658. }
  5659. if (p1q1p2 && q1p1q2) {
  5660. this._intPt[0] = q1;
  5661. this._intPt[1] = p1;
  5662. return q1.equals(p1) && !p1q2p2 && !q1p2q2 ? LineIntersector.POINT_INTERSECTION : LineIntersector.COLLINEAR_INTERSECTION;
  5663. }
  5664. if (p1q1p2 && q1p2q2) {
  5665. this._intPt[0] = q1;
  5666. this._intPt[1] = p2;
  5667. return q1.equals(p2) && !p1q2p2 && !q1p1q2 ? LineIntersector.POINT_INTERSECTION : LineIntersector.COLLINEAR_INTERSECTION;
  5668. }
  5669. if (p1q2p2 && q1p1q2) {
  5670. this._intPt[0] = q2;
  5671. this._intPt[1] = p1;
  5672. return q2.equals(p1) && !p1q1p2 && !q1p2q2 ? LineIntersector.POINT_INTERSECTION : LineIntersector.COLLINEAR_INTERSECTION;
  5673. }
  5674. if (p1q2p2 && q1p2q2) {
  5675. this._intPt[0] = q2;
  5676. this._intPt[1] = p2;
  5677. return q2.equals(p2) && !p1q1p2 && !q1p1q2 ? LineIntersector.POINT_INTERSECTION : LineIntersector.COLLINEAR_INTERSECTION;
  5678. }
  5679. return LineIntersector.NO_INTERSECTION;
  5680. }
  5681. computeIntersect(p1, p2, q1, q2) {
  5682. this._isProper = false;
  5683. if (!Envelope.intersects(p1, p2, q1, q2)) return LineIntersector.NO_INTERSECTION;
  5684. const Pq1 = Orientation.index(p1, p2, q1);
  5685. const Pq2 = Orientation.index(p1, p2, q2);
  5686. if (Pq1 > 0 && Pq2 > 0 || Pq1 < 0 && Pq2 < 0) return LineIntersector.NO_INTERSECTION;
  5687. const Qp1 = Orientation.index(q1, q2, p1);
  5688. const Qp2 = Orientation.index(q1, q2, p2);
  5689. if (Qp1 > 0 && Qp2 > 0 || Qp1 < 0 && Qp2 < 0) return LineIntersector.NO_INTERSECTION;
  5690. const collinear = Pq1 === 0 && Pq2 === 0 && Qp1 === 0 && Qp2 === 0;
  5691. if (collinear) return this.computeCollinearIntersection(p1, p2, q1, q2);
  5692. if (Pq1 === 0 || Pq2 === 0 || Qp1 === 0 || Qp2 === 0) {
  5693. this._isProper = false;
  5694. if (p1.equals2D(q1) || p1.equals2D(q2)) this._intPt[0] = p1;else if (p2.equals2D(q1) || p2.equals2D(q2)) this._intPt[0] = p2;else if (Pq1 === 0) this._intPt[0] = new Coordinate(q1);else if (Pq2 === 0) this._intPt[0] = new Coordinate(q2);else if (Qp1 === 0) this._intPt[0] = new Coordinate(p1);else if (Qp2 === 0) this._intPt[0] = new Coordinate(p2);
  5695. } else {
  5696. this._isProper = true;
  5697. this._intPt[0] = this.intersection(p1, p2, q1, q2);
  5698. }
  5699. return LineIntersector.POINT_INTERSECTION;
  5700. }
  5701. }
  5702. class LineSegment {
  5703. constructor() {
  5704. LineSegment.constructor_.apply(this, arguments);
  5705. }
  5706. static constructor_() {
  5707. this.p0 = null;
  5708. this.p1 = null;
  5709. if (arguments.length === 0) {
  5710. LineSegment.constructor_.call(this, new Coordinate(), new Coordinate());
  5711. } else if (arguments.length === 1) {
  5712. const ls = arguments[0];
  5713. LineSegment.constructor_.call(this, ls.p0, ls.p1);
  5714. } else if (arguments.length === 2) {
  5715. const p0 = arguments[0],
  5716. p1 = arguments[1];
  5717. this.p0 = p0;
  5718. this.p1 = p1;
  5719. } else if (arguments.length === 4) {
  5720. const x0 = arguments[0],
  5721. y0 = arguments[1],
  5722. x1 = arguments[2],
  5723. y1 = arguments[3];
  5724. LineSegment.constructor_.call(this, new Coordinate(x0, y0), new Coordinate(x1, y1));
  5725. }
  5726. }
  5727. static midPoint(p0, p1) {
  5728. return new Coordinate((p0.x + p1.x) / 2, (p0.y + p1.y) / 2);
  5729. }
  5730. minX() {
  5731. return Math.min(this.p0.x, this.p1.x);
  5732. }
  5733. orientationIndex() {
  5734. if (arguments[0] instanceof LineSegment) {
  5735. const seg = arguments[0];
  5736. const orient0 = Orientation.index(this.p0, this.p1, seg.p0);
  5737. const orient1 = Orientation.index(this.p0, this.p1, seg.p1);
  5738. if (orient0 >= 0 && orient1 >= 0) return Math.max(orient0, orient1);
  5739. if (orient0 <= 0 && orient1 <= 0) return Math.max(orient0, orient1);
  5740. return 0;
  5741. } else if (arguments[0] instanceof Coordinate) {
  5742. const p = arguments[0];
  5743. return Orientation.index(this.p0, this.p1, p);
  5744. }
  5745. }
  5746. toGeometry(geomFactory) {
  5747. return geomFactory.createLineString([this.p0, this.p1]);
  5748. }
  5749. isVertical() {
  5750. return this.p0.x === this.p1.x;
  5751. }
  5752. equals(o) {
  5753. if (!(o instanceof LineSegment)) return false;
  5754. const other = o;
  5755. return this.p0.equals(other.p0) && this.p1.equals(other.p1);
  5756. }
  5757. intersection(line) {
  5758. const li = new RobustLineIntersector();
  5759. li.computeIntersection(this.p0, this.p1, line.p0, line.p1);
  5760. if (li.hasIntersection()) return li.getIntersection(0);
  5761. return null;
  5762. }
  5763. project() {
  5764. if (arguments[0] instanceof Coordinate) {
  5765. const p = arguments[0];
  5766. if (p.equals(this.p0) || p.equals(this.p1)) return new Coordinate(p);
  5767. const r = this.projectionFactor(p);
  5768. const coord = new Coordinate();
  5769. coord.x = this.p0.x + r * (this.p1.x - this.p0.x);
  5770. coord.y = this.p0.y + r * (this.p1.y - this.p0.y);
  5771. return coord;
  5772. } else if (arguments[0] instanceof LineSegment) {
  5773. const seg = arguments[0];
  5774. const pf0 = this.projectionFactor(seg.p0);
  5775. const pf1 = this.projectionFactor(seg.p1);
  5776. if (pf0 >= 1.0 && pf1 >= 1.0) return null;
  5777. if (pf0 <= 0.0 && pf1 <= 0.0) return null;
  5778. let newp0 = this.project(seg.p0);
  5779. if (pf0 < 0.0) newp0 = this.p0;
  5780. if (pf0 > 1.0) newp0 = this.p1;
  5781. let newp1 = this.project(seg.p1);
  5782. if (pf1 < 0.0) newp1 = this.p0;
  5783. if (pf1 > 1.0) newp1 = this.p1;
  5784. return new LineSegment(newp0, newp1);
  5785. }
  5786. }
  5787. normalize() {
  5788. if (this.p1.compareTo(this.p0) < 0) this.reverse();
  5789. }
  5790. angle() {
  5791. return Math.atan2(this.p1.y - this.p0.y, this.p1.x - this.p0.x);
  5792. }
  5793. getCoordinate(i) {
  5794. if (i === 0) return this.p0;
  5795. return this.p1;
  5796. }
  5797. distancePerpendicular(p) {
  5798. return Distance.pointToLinePerpendicular(p, this.p0, this.p1);
  5799. }
  5800. minY() {
  5801. return Math.min(this.p0.y, this.p1.y);
  5802. }
  5803. midPoint() {
  5804. return LineSegment.midPoint(this.p0, this.p1);
  5805. }
  5806. projectionFactor(p) {
  5807. if (p.equals(this.p0)) return 0.0;
  5808. if (p.equals(this.p1)) return 1.0;
  5809. const dx = this.p1.x - this.p0.x;
  5810. const dy = this.p1.y - this.p0.y;
  5811. const len = dx * dx + dy * dy;
  5812. if (len <= 0.0) return Double.NaN;
  5813. const r = ((p.x - this.p0.x) * dx + (p.y - this.p0.y) * dy) / len;
  5814. return r;
  5815. }
  5816. closestPoints(line) {
  5817. const intPt = this.intersection(line);
  5818. if (intPt !== null) return [intPt, intPt];
  5819. const closestPt = new Array(2).fill(null);
  5820. let minDistance = Double.MAX_VALUE;
  5821. let dist = null;
  5822. const close00 = this.closestPoint(line.p0);
  5823. minDistance = close00.distance(line.p0);
  5824. closestPt[0] = close00;
  5825. closestPt[1] = line.p0;
  5826. const close01 = this.closestPoint(line.p1);
  5827. dist = close01.distance(line.p1);
  5828. if (dist < minDistance) {
  5829. minDistance = dist;
  5830. closestPt[0] = close01;
  5831. closestPt[1] = line.p1;
  5832. }
  5833. const close10 = line.closestPoint(this.p0);
  5834. dist = close10.distance(this.p0);
  5835. if (dist < minDistance) {
  5836. minDistance = dist;
  5837. closestPt[0] = this.p0;
  5838. closestPt[1] = close10;
  5839. }
  5840. const close11 = line.closestPoint(this.p1);
  5841. dist = close11.distance(this.p1);
  5842. if (dist < minDistance) {
  5843. minDistance = dist;
  5844. closestPt[0] = this.p1;
  5845. closestPt[1] = close11;
  5846. }
  5847. return closestPt;
  5848. }
  5849. closestPoint(p) {
  5850. const factor = this.projectionFactor(p);
  5851. if (factor > 0 && factor < 1) return this.project(p);
  5852. const dist0 = this.p0.distance(p);
  5853. const dist1 = this.p1.distance(p);
  5854. if (dist0 < dist1) return this.p0;
  5855. return this.p1;
  5856. }
  5857. maxX() {
  5858. return Math.max(this.p0.x, this.p1.x);
  5859. }
  5860. getLength() {
  5861. return this.p0.distance(this.p1);
  5862. }
  5863. compareTo(o) {
  5864. const other = o;
  5865. const comp0 = this.p0.compareTo(other.p0);
  5866. if (comp0 !== 0) return comp0;
  5867. return this.p1.compareTo(other.p1);
  5868. }
  5869. reverse() {
  5870. const temp = this.p0;
  5871. this.p0 = this.p1;
  5872. this.p1 = temp;
  5873. }
  5874. equalsTopo(other) {
  5875. return this.p0.equals(other.p0) && this.p1.equals(other.p1) || this.p0.equals(other.p1) && this.p1.equals(other.p0);
  5876. }
  5877. lineIntersection(line) {
  5878. const intPt = Intersection.intersection(this.p0, this.p1, line.p0, line.p1);
  5879. return intPt;
  5880. }
  5881. maxY() {
  5882. return Math.max(this.p0.y, this.p1.y);
  5883. }
  5884. pointAlongOffset(segmentLengthFraction, offsetDistance) {
  5885. const segx = this.p0.x + segmentLengthFraction * (this.p1.x - this.p0.x);
  5886. const segy = this.p0.y + segmentLengthFraction * (this.p1.y - this.p0.y);
  5887. const dx = this.p1.x - this.p0.x;
  5888. const dy = this.p1.y - this.p0.y;
  5889. const len = Math.sqrt(dx * dx + dy * dy);
  5890. let ux = 0.0;
  5891. let uy = 0.0;
  5892. if (offsetDistance !== 0.0) {
  5893. if (len <= 0.0) throw new IllegalStateException('Cannot compute offset from zero-length line segment');
  5894. ux = offsetDistance * dx / len;
  5895. uy = offsetDistance * dy / len;
  5896. }
  5897. const offsetx = segx - uy;
  5898. const offsety = segy + ux;
  5899. const coord = new Coordinate(offsetx, offsety);
  5900. return coord;
  5901. }
  5902. setCoordinates() {
  5903. if (arguments.length === 1) {
  5904. const ls = arguments[0];
  5905. this.setCoordinates(ls.p0, ls.p1);
  5906. } else if (arguments.length === 2) {
  5907. const p0 = arguments[0],
  5908. p1 = arguments[1];
  5909. this.p0.x = p0.x;
  5910. this.p0.y = p0.y;
  5911. this.p1.x = p1.x;
  5912. this.p1.y = p1.y;
  5913. }
  5914. }
  5915. segmentFraction(inputPt) {
  5916. let segFrac = this.projectionFactor(inputPt);
  5917. if (segFrac < 0.0) segFrac = 0.0;else if (segFrac > 1.0 || Double.isNaN(segFrac)) segFrac = 1.0;
  5918. return segFrac;
  5919. }
  5920. toString() {
  5921. return 'LINESTRING( ' + this.p0.x + ' ' + this.p0.y + ', ' + this.p1.x + ' ' + this.p1.y + ')';
  5922. }
  5923. isHorizontal() {
  5924. return this.p0.y === this.p1.y;
  5925. }
  5926. reflect(p) {
  5927. const A = this.p1.getY() - this.p0.getY();
  5928. const B = this.p0.getX() - this.p1.getX();
  5929. const C = this.p0.getY() * (this.p1.getX() - this.p0.getX()) - this.p0.getX() * (this.p1.getY() - this.p0.getY());
  5930. const A2plusB2 = A * A + B * B;
  5931. const A2subB2 = A * A - B * B;
  5932. const x = p.getX();
  5933. const y = p.getY();
  5934. const rx = (-A2subB2 * x - 2 * A * B * y - 2 * A * C) / A2plusB2;
  5935. const ry = (A2subB2 * y - 2 * A * B * x - 2 * B * C) / A2plusB2;
  5936. return new Coordinate(rx, ry);
  5937. }
  5938. distance() {
  5939. if (arguments[0] instanceof LineSegment) {
  5940. const ls = arguments[0];
  5941. return Distance.segmentToSegment(this.p0, this.p1, ls.p0, ls.p1);
  5942. } else if (arguments[0] instanceof Coordinate) {
  5943. const p = arguments[0];
  5944. return Distance.pointToSegment(p, this.p0, this.p1);
  5945. }
  5946. }
  5947. pointAlong(segmentLengthFraction) {
  5948. const coord = new Coordinate();
  5949. coord.x = this.p0.x + segmentLengthFraction * (this.p1.x - this.p0.x);
  5950. coord.y = this.p0.y + segmentLengthFraction * (this.p1.y - this.p0.y);
  5951. return coord;
  5952. }
  5953. hashCode() {
  5954. let bits0 = Double.doubleToLongBits(this.p0.x);
  5955. bits0 ^= Double.doubleToLongBits(this.p0.y) * 31;
  5956. const hash0 = Math.trunc(bits0) ^ Math.trunc(bits0 >> 32);
  5957. let bits1 = Double.doubleToLongBits(this.p1.x);
  5958. bits1 ^= Double.doubleToLongBits(this.p1.y) * 31;
  5959. const hash1 = Math.trunc(bits1) ^ Math.trunc(bits1 >> 32);
  5960. return hash0 ^ hash1;
  5961. }
  5962. get interfaces_() {
  5963. return [Comparable, Serializable];
  5964. }
  5965. }
  5966. class Location {
  5967. static toLocationSymbol(locationValue) {
  5968. switch (locationValue) {
  5969. case Location.EXTERIOR:
  5970. return 'e';
  5971. case Location.BOUNDARY:
  5972. return 'b';
  5973. case Location.INTERIOR:
  5974. return 'i';
  5975. case Location.NONE:
  5976. return '-';
  5977. }
  5978. throw new IllegalArgumentException('Unknown location value: ' + locationValue);
  5979. }
  5980. }
  5981. Location.INTERIOR = 0;
  5982. Location.BOUNDARY = 1;
  5983. Location.EXTERIOR = 2;
  5984. Location.NONE = -1;
  5985. class IntersectionMatrix {
  5986. constructor() {
  5987. IntersectionMatrix.constructor_.apply(this, arguments);
  5988. }
  5989. static constructor_() {
  5990. this._matrix = null;
  5991. if (arguments.length === 0) {
  5992. this._matrix = Array(3).fill().map(() => Array(3));
  5993. this.setAll(Dimension.FALSE);
  5994. } else if (arguments.length === 1) {
  5995. if (typeof arguments[0] === 'string') {
  5996. const elements = arguments[0];
  5997. IntersectionMatrix.constructor_.call(this);
  5998. this.set(elements);
  5999. } else if (arguments[0] instanceof IntersectionMatrix) {
  6000. const other = arguments[0];
  6001. IntersectionMatrix.constructor_.call(this);
  6002. this._matrix[Location.INTERIOR][Location.INTERIOR] = other._matrix[Location.INTERIOR][Location.INTERIOR];
  6003. this._matrix[Location.INTERIOR][Location.BOUNDARY] = other._matrix[Location.INTERIOR][Location.BOUNDARY];
  6004. this._matrix[Location.INTERIOR][Location.EXTERIOR] = other._matrix[Location.INTERIOR][Location.EXTERIOR];
  6005. this._matrix[Location.BOUNDARY][Location.INTERIOR] = other._matrix[Location.BOUNDARY][Location.INTERIOR];
  6006. this._matrix[Location.BOUNDARY][Location.BOUNDARY] = other._matrix[Location.BOUNDARY][Location.BOUNDARY];
  6007. this._matrix[Location.BOUNDARY][Location.EXTERIOR] = other._matrix[Location.BOUNDARY][Location.EXTERIOR];
  6008. this._matrix[Location.EXTERIOR][Location.INTERIOR] = other._matrix[Location.EXTERIOR][Location.INTERIOR];
  6009. this._matrix[Location.EXTERIOR][Location.BOUNDARY] = other._matrix[Location.EXTERIOR][Location.BOUNDARY];
  6010. this._matrix[Location.EXTERIOR][Location.EXTERIOR] = other._matrix[Location.EXTERIOR][Location.EXTERIOR];
  6011. }
  6012. }
  6013. }
  6014. static matches() {
  6015. if (Number.isInteger(arguments[0]) && typeof arguments[1] === 'string') {
  6016. const actualDimensionValue = arguments[0],
  6017. requiredDimensionSymbol = arguments[1];
  6018. if (requiredDimensionSymbol === Dimension.SYM_DONTCARE) return true;
  6019. if (requiredDimensionSymbol === Dimension.SYM_TRUE && (actualDimensionValue >= 0 || actualDimensionValue === Dimension.TRUE)) return true;
  6020. if (requiredDimensionSymbol === Dimension.SYM_FALSE && actualDimensionValue === Dimension.FALSE) return true;
  6021. if (requiredDimensionSymbol === Dimension.SYM_P && actualDimensionValue === Dimension.P) return true;
  6022. if (requiredDimensionSymbol === Dimension.SYM_L && actualDimensionValue === Dimension.L) return true;
  6023. if (requiredDimensionSymbol === Dimension.SYM_A && actualDimensionValue === Dimension.A) return true;
  6024. return false;
  6025. } else if (typeof arguments[0] === 'string' && typeof arguments[1] === 'string') {
  6026. const actualDimensionSymbols = arguments[0],
  6027. requiredDimensionSymbols = arguments[1];
  6028. const m = new IntersectionMatrix(actualDimensionSymbols);
  6029. return m.matches(requiredDimensionSymbols);
  6030. }
  6031. }
  6032. static isTrue(actualDimensionValue) {
  6033. if (actualDimensionValue >= 0 || actualDimensionValue === Dimension.TRUE) return true;
  6034. return false;
  6035. }
  6036. isIntersects() {
  6037. return !this.isDisjoint();
  6038. }
  6039. isCovers() {
  6040. const hasPointInCommon = IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) || IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.BOUNDARY]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.INTERIOR]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.BOUNDARY]);
  6041. return hasPointInCommon && this._matrix[Location.EXTERIOR][Location.INTERIOR] === Dimension.FALSE && this._matrix[Location.EXTERIOR][Location.BOUNDARY] === Dimension.FALSE;
  6042. }
  6043. isCoveredBy() {
  6044. const hasPointInCommon = IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) || IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.BOUNDARY]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.INTERIOR]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.BOUNDARY]);
  6045. return hasPointInCommon && this._matrix[Location.INTERIOR][Location.EXTERIOR] === Dimension.FALSE && this._matrix[Location.BOUNDARY][Location.EXTERIOR] === Dimension.FALSE;
  6046. }
  6047. set() {
  6048. if (arguments.length === 1) {
  6049. const dimensionSymbols = arguments[0];
  6050. for (let i = 0; i < dimensionSymbols.length; i++) {
  6051. const row = Math.trunc(i / 3);
  6052. const col = i % 3;
  6053. this._matrix[row][col] = Dimension.toDimensionValue(dimensionSymbols.charAt(i));
  6054. }
  6055. } else if (arguments.length === 3) {
  6056. const row = arguments[0],
  6057. column = arguments[1],
  6058. dimensionValue = arguments[2];
  6059. this._matrix[row][column] = dimensionValue;
  6060. }
  6061. }
  6062. isContains() {
  6063. return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && this._matrix[Location.EXTERIOR][Location.INTERIOR] === Dimension.FALSE && this._matrix[Location.EXTERIOR][Location.BOUNDARY] === Dimension.FALSE;
  6064. }
  6065. setAtLeast() {
  6066. if (arguments.length === 1) {
  6067. const minimumDimensionSymbols = arguments[0];
  6068. for (let i = 0; i < minimumDimensionSymbols.length; i++) {
  6069. const row = Math.trunc(i / 3);
  6070. const col = i % 3;
  6071. this.setAtLeast(row, col, Dimension.toDimensionValue(minimumDimensionSymbols.charAt(i)));
  6072. }
  6073. } else if (arguments.length === 3) {
  6074. const row = arguments[0],
  6075. column = arguments[1],
  6076. minimumDimensionValue = arguments[2];
  6077. if (this._matrix[row][column] < minimumDimensionValue) this._matrix[row][column] = minimumDimensionValue;
  6078. }
  6079. }
  6080. setAtLeastIfValid(row, column, minimumDimensionValue) {
  6081. if (row >= 0 && column >= 0) this.setAtLeast(row, column, minimumDimensionValue);
  6082. }
  6083. isWithin() {
  6084. return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && this._matrix[Location.INTERIOR][Location.EXTERIOR] === Dimension.FALSE && this._matrix[Location.BOUNDARY][Location.EXTERIOR] === Dimension.FALSE;
  6085. }
  6086. isTouches(dimensionOfGeometryA, dimensionOfGeometryB) {
  6087. if (dimensionOfGeometryA > dimensionOfGeometryB) return this.isTouches(dimensionOfGeometryB, dimensionOfGeometryA);
  6088. if (dimensionOfGeometryA === Dimension.A && dimensionOfGeometryB === Dimension.A || dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.L || dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.A || dimensionOfGeometryA === Dimension.P && dimensionOfGeometryB === Dimension.A || dimensionOfGeometryA === Dimension.P && dimensionOfGeometryB === Dimension.L) return this._matrix[Location.INTERIOR][Location.INTERIOR] === Dimension.FALSE && (IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.BOUNDARY]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.INTERIOR]) || IntersectionMatrix.isTrue(this._matrix[Location.BOUNDARY][Location.BOUNDARY]));
  6089. return false;
  6090. }
  6091. isOverlaps(dimensionOfGeometryA, dimensionOfGeometryB) {
  6092. if (dimensionOfGeometryA === Dimension.P && dimensionOfGeometryB === Dimension.P || dimensionOfGeometryA === Dimension.A && dimensionOfGeometryB === Dimension.A) return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.EXTERIOR]) && IntersectionMatrix.isTrue(this._matrix[Location.EXTERIOR][Location.INTERIOR]);
  6093. if (dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.L) return this._matrix[Location.INTERIOR][Location.INTERIOR] === 1 && IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.EXTERIOR]) && IntersectionMatrix.isTrue(this._matrix[Location.EXTERIOR][Location.INTERIOR]);
  6094. return false;
  6095. }
  6096. isEquals(dimensionOfGeometryA, dimensionOfGeometryB) {
  6097. if (dimensionOfGeometryA !== dimensionOfGeometryB) return false;
  6098. return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && this._matrix[Location.INTERIOR][Location.EXTERIOR] === Dimension.FALSE && this._matrix[Location.BOUNDARY][Location.EXTERIOR] === Dimension.FALSE && this._matrix[Location.EXTERIOR][Location.INTERIOR] === Dimension.FALSE && this._matrix[Location.EXTERIOR][Location.BOUNDARY] === Dimension.FALSE;
  6099. }
  6100. toString() {
  6101. const builder = new StringBuilder('123456789');
  6102. for (let ai = 0; ai < 3; ai++) for (let bi = 0; bi < 3; bi++) builder.setCharAt(3 * ai + bi, Dimension.toDimensionSymbol(this._matrix[ai][bi]));
  6103. return builder.toString();
  6104. }
  6105. setAll(dimensionValue) {
  6106. for (let ai = 0; ai < 3; ai++) for (let bi = 0; bi < 3; bi++) this._matrix[ai][bi] = dimensionValue;
  6107. }
  6108. get(row, column) {
  6109. return this._matrix[row][column];
  6110. }
  6111. transpose() {
  6112. let temp = this._matrix[1][0];
  6113. this._matrix[1][0] = this._matrix[0][1];
  6114. this._matrix[0][1] = temp;
  6115. temp = this._matrix[2][0];
  6116. this._matrix[2][0] = this._matrix[0][2];
  6117. this._matrix[0][2] = temp;
  6118. temp = this._matrix[2][1];
  6119. this._matrix[2][1] = this._matrix[1][2];
  6120. this._matrix[1][2] = temp;
  6121. return this;
  6122. }
  6123. matches(requiredDimensionSymbols) {
  6124. if (requiredDimensionSymbols.length !== 9) throw new IllegalArgumentException('Should be length 9: ' + requiredDimensionSymbols);
  6125. for (let ai = 0; ai < 3; ai++) for (let bi = 0; bi < 3; bi++) if (!IntersectionMatrix.matches(this._matrix[ai][bi], requiredDimensionSymbols.charAt(3 * ai + bi))) return false;
  6126. return true;
  6127. }
  6128. add(im) {
  6129. for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) this.setAtLeast(i, j, im.get(i, j));
  6130. }
  6131. isDisjoint() {
  6132. return this._matrix[Location.INTERIOR][Location.INTERIOR] === Dimension.FALSE && this._matrix[Location.INTERIOR][Location.BOUNDARY] === Dimension.FALSE && this._matrix[Location.BOUNDARY][Location.INTERIOR] === Dimension.FALSE && this._matrix[Location.BOUNDARY][Location.BOUNDARY] === Dimension.FALSE;
  6133. }
  6134. isCrosses(dimensionOfGeometryA, dimensionOfGeometryB) {
  6135. if (dimensionOfGeometryA === Dimension.P && dimensionOfGeometryB === Dimension.L || dimensionOfGeometryA === Dimension.P && dimensionOfGeometryB === Dimension.A || dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.A) return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.EXTERIOR]);
  6136. if (dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.P || dimensionOfGeometryA === Dimension.A && dimensionOfGeometryB === Dimension.P || dimensionOfGeometryA === Dimension.A && dimensionOfGeometryB === Dimension.L) return IntersectionMatrix.isTrue(this._matrix[Location.INTERIOR][Location.INTERIOR]) && IntersectionMatrix.isTrue(this._matrix[Location.EXTERIOR][Location.INTERIOR]);
  6137. if (dimensionOfGeometryA === Dimension.L && dimensionOfGeometryB === Dimension.L) return this._matrix[Location.INTERIOR][Location.INTERIOR] === 0;
  6138. return false;
  6139. }
  6140. get interfaces_() {
  6141. return [Clonable];
  6142. }
  6143. }
  6144. class Angle {
  6145. static toDegrees(radians) {
  6146. return radians * 180 / Math.PI;
  6147. }
  6148. static normalize(angle) {
  6149. while (angle > Math.PI) angle -= Angle.PI_TIMES_2;
  6150. while (angle <= -Math.PI) angle += Angle.PI_TIMES_2;
  6151. return angle;
  6152. }
  6153. static angle() {
  6154. if (arguments.length === 1) {
  6155. const p = arguments[0];
  6156. return Math.atan2(p.y, p.x);
  6157. } else if (arguments.length === 2) {
  6158. const p0 = arguments[0],
  6159. p1 = arguments[1];
  6160. const dx = p1.x - p0.x;
  6161. const dy = p1.y - p0.y;
  6162. return Math.atan2(dy, dx);
  6163. }
  6164. }
  6165. static isAcute(p0, p1, p2) {
  6166. const dx0 = p0.x - p1.x;
  6167. const dy0 = p0.y - p1.y;
  6168. const dx1 = p2.x - p1.x;
  6169. const dy1 = p2.y - p1.y;
  6170. const dotprod = dx0 * dx1 + dy0 * dy1;
  6171. return dotprod > 0;
  6172. }
  6173. static isObtuse(p0, p1, p2) {
  6174. const dx0 = p0.x - p1.x;
  6175. const dy0 = p0.y - p1.y;
  6176. const dx1 = p2.x - p1.x;
  6177. const dy1 = p2.y - p1.y;
  6178. const dotprod = dx0 * dx1 + dy0 * dy1;
  6179. return dotprod < 0;
  6180. }
  6181. static interiorAngle(p0, p1, p2) {
  6182. const anglePrev = Angle.angle(p1, p0);
  6183. const angleNext = Angle.angle(p1, p2);
  6184. return Math.abs(angleNext - anglePrev);
  6185. }
  6186. static normalizePositive(angle) {
  6187. if (angle < 0.0) {
  6188. while (angle < 0.0) angle += Angle.PI_TIMES_2;
  6189. if (angle >= Angle.PI_TIMES_2) angle = 0.0;
  6190. } else {
  6191. while (angle >= Angle.PI_TIMES_2) angle -= Angle.PI_TIMES_2;
  6192. if (angle < 0.0) angle = 0.0;
  6193. }
  6194. return angle;
  6195. }
  6196. static angleBetween(tip1, tail, tip2) {
  6197. const a1 = Angle.angle(tail, tip1);
  6198. const a2 = Angle.angle(tail, tip2);
  6199. return Angle.diff(a1, a2);
  6200. }
  6201. static diff(ang1, ang2) {
  6202. let delAngle = null;
  6203. if (ang1 < ang2) delAngle = ang2 - ang1;else delAngle = ang1 - ang2;
  6204. if (delAngle > Math.PI) delAngle = 2 * Math.PI - delAngle;
  6205. return delAngle;
  6206. }
  6207. static toRadians(angleDegrees) {
  6208. return angleDegrees * Math.PI / 180.0;
  6209. }
  6210. static getTurn(ang1, ang2) {
  6211. const crossproduct = Math.sin(ang2 - ang1);
  6212. if (crossproduct > 0) return Angle.COUNTERCLOCKWISE;
  6213. if (crossproduct < 0) return Angle.CLOCKWISE;
  6214. return Angle.NONE;
  6215. }
  6216. static angleBetweenOriented(tip1, tail, tip2) {
  6217. const a1 = Angle.angle(tail, tip1);
  6218. const a2 = Angle.angle(tail, tip2);
  6219. const angDel = a2 - a1;
  6220. if (angDel <= -Math.PI) return angDel + Angle.PI_TIMES_2;
  6221. if (angDel > Math.PI) return angDel - Angle.PI_TIMES_2;
  6222. return angDel;
  6223. }
  6224. }
  6225. Angle.PI_TIMES_2 = 2.0 * Math.PI;
  6226. Angle.PI_OVER_2 = Math.PI / 2.0;
  6227. Angle.PI_OVER_4 = Math.PI / 4.0;
  6228. Angle.COUNTERCLOCKWISE = Orientation.COUNTERCLOCKWISE;
  6229. Angle.CLOCKWISE = Orientation.CLOCKWISE;
  6230. Angle.NONE = Orientation.COLLINEAR;
  6231. class NotRepresentableException extends Exception {
  6232. constructor() {
  6233. super();
  6234. NotRepresentableException.constructor_.apply(this, arguments);
  6235. }
  6236. static constructor_() {
  6237. Exception.constructor_.call(this, 'Projective point not representable on the Cartesian plane.');
  6238. }
  6239. }
  6240. class HCoordinate {
  6241. constructor() {
  6242. HCoordinate.constructor_.apply(this, arguments);
  6243. }
  6244. static constructor_() {
  6245. this.x = null;
  6246. this.y = null;
  6247. this.w = null;
  6248. if (arguments.length === 0) {
  6249. this.x = 0.0;
  6250. this.y = 0.0;
  6251. this.w = 1.0;
  6252. } else if (arguments.length === 1) {
  6253. const p = arguments[0];
  6254. this.x = p.x;
  6255. this.y = p.y;
  6256. this.w = 1.0;
  6257. } else if (arguments.length === 2) {
  6258. if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  6259. const _x = arguments[0],
  6260. _y = arguments[1];
  6261. this.x = _x;
  6262. this.y = _y;
  6263. this.w = 1.0;
  6264. } else if (arguments[0] instanceof HCoordinate && arguments[1] instanceof HCoordinate) {
  6265. const p1 = arguments[0],
  6266. p2 = arguments[1];
  6267. this.x = p1.y * p2.w - p2.y * p1.w;
  6268. this.y = p2.x * p1.w - p1.x * p2.w;
  6269. this.w = p1.x * p2.y - p2.x * p1.y;
  6270. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  6271. const p1 = arguments[0],
  6272. p2 = arguments[1];
  6273. this.x = p1.y - p2.y;
  6274. this.y = p2.x - p1.x;
  6275. this.w = p1.x * p2.y - p2.x * p1.y;
  6276. }
  6277. } else if (arguments.length === 3) {
  6278. const _x = arguments[0],
  6279. _y = arguments[1],
  6280. _w = arguments[2];
  6281. this.x = _x;
  6282. this.y = _y;
  6283. this.w = _w;
  6284. } else if (arguments.length === 4) {
  6285. const p1 = arguments[0],
  6286. p2 = arguments[1],
  6287. q1 = arguments[2],
  6288. q2 = arguments[3];
  6289. const px = p1.y - p2.y;
  6290. const py = p2.x - p1.x;
  6291. const pw = p1.x * p2.y - p2.x * p1.y;
  6292. const qx = q1.y - q2.y;
  6293. const qy = q2.x - q1.x;
  6294. const qw = q1.x * q2.y - q2.x * q1.y;
  6295. this.x = py * qw - qy * pw;
  6296. this.y = qx * pw - px * qw;
  6297. this.w = px * qy - qx * py;
  6298. }
  6299. }
  6300. getY() {
  6301. const a = this.y / this.w;
  6302. if (Double.isNaN(a) || Double.isInfinite(a)) throw new NotRepresentableException();
  6303. return a;
  6304. }
  6305. getX() {
  6306. const a = this.x / this.w;
  6307. if (Double.isNaN(a) || Double.isInfinite(a)) throw new NotRepresentableException();
  6308. return a;
  6309. }
  6310. getCoordinate() {
  6311. const p = new Coordinate();
  6312. p.x = this.getX();
  6313. p.y = this.getY();
  6314. return p;
  6315. }
  6316. }
  6317. class Triangle {
  6318. constructor() {
  6319. Triangle.constructor_.apply(this, arguments);
  6320. }
  6321. static constructor_() {
  6322. this.p0 = null;
  6323. this.p1 = null;
  6324. this.p2 = null;
  6325. const p0 = arguments[0],
  6326. p1 = arguments[1],
  6327. p2 = arguments[2];
  6328. this.p0 = p0;
  6329. this.p1 = p1;
  6330. this.p2 = p2;
  6331. }
  6332. static area(a, b, c) {
  6333. return Math.abs(((c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y)) / 2);
  6334. }
  6335. static signedArea(a, b, c) {
  6336. return ((c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y)) / 2;
  6337. }
  6338. static det(m00, m01, m10, m11) {
  6339. return m00 * m11 - m01 * m10;
  6340. }
  6341. static interpolateZ(p, v0, v1, v2) {
  6342. const x0 = v0.x;
  6343. const y0 = v0.y;
  6344. const a = v1.x - x0;
  6345. const b = v2.x - x0;
  6346. const c = v1.y - y0;
  6347. const d = v2.y - y0;
  6348. const det = a * d - b * c;
  6349. const dx = p.x - x0;
  6350. const dy = p.y - y0;
  6351. const t = (d * dx - b * dy) / det;
  6352. const u = (-c * dx + a * dy) / det;
  6353. const z = v0.getZ() + t * (v1.getZ() - v0.getZ()) + u * (v2.getZ() - v0.getZ());
  6354. return z;
  6355. }
  6356. static longestSideLength(a, b, c) {
  6357. const lenAB = a.distance(b);
  6358. const lenBC = b.distance(c);
  6359. const lenCA = c.distance(a);
  6360. let maxLen = lenAB;
  6361. if (lenBC > maxLen) maxLen = lenBC;
  6362. if (lenCA > maxLen) maxLen = lenCA;
  6363. return maxLen;
  6364. }
  6365. static circumcentreDD(a, b, c) {
  6366. const ax = DD.valueOf(a.x).subtract(c.x);
  6367. const ay = DD.valueOf(a.y).subtract(c.y);
  6368. const bx = DD.valueOf(b.x).subtract(c.x);
  6369. const by = DD.valueOf(b.y).subtract(c.y);
  6370. const denom = DD.determinant(ax, ay, bx, by).multiply(2);
  6371. const asqr = ax.sqr().add(ay.sqr());
  6372. const bsqr = bx.sqr().add(by.sqr());
  6373. const numx = DD.determinant(ay, asqr, by, bsqr);
  6374. const numy = DD.determinant(ax, asqr, bx, bsqr);
  6375. const ccx = DD.valueOf(c.x).subtract(numx.divide(denom)).doubleValue();
  6376. const ccy = DD.valueOf(c.y).add(numy.divide(denom)).doubleValue();
  6377. return new Coordinate(ccx, ccy);
  6378. }
  6379. static isAcute(a, b, c) {
  6380. if (!Angle.isAcute(a, b, c)) return false;
  6381. if (!Angle.isAcute(b, c, a)) return false;
  6382. if (!Angle.isAcute(c, a, b)) return false;
  6383. return true;
  6384. }
  6385. static circumcentre(a, b, c) {
  6386. const cx = c.x;
  6387. const cy = c.y;
  6388. const ax = a.x - cx;
  6389. const ay = a.y - cy;
  6390. const bx = b.x - cx;
  6391. const by = b.y - cy;
  6392. const denom = 2 * Triangle.det(ax, ay, bx, by);
  6393. const numx = Triangle.det(ay, ax * ax + ay * ay, by, bx * bx + by * by);
  6394. const numy = Triangle.det(ax, ax * ax + ay * ay, bx, bx * bx + by * by);
  6395. const ccx = cx - numx / denom;
  6396. const ccy = cy + numy / denom;
  6397. return new Coordinate(ccx, ccy);
  6398. }
  6399. static perpendicularBisector(a, b) {
  6400. const dx = b.x - a.x;
  6401. const dy = b.y - a.y;
  6402. const l1 = new HCoordinate(a.x + dx / 2.0, a.y + dy / 2.0, 1.0);
  6403. const l2 = new HCoordinate(a.x - dy + dx / 2.0, a.y + dx + dy / 2.0, 1.0);
  6404. return new HCoordinate(l1, l2);
  6405. }
  6406. static angleBisector(a, b, c) {
  6407. const len0 = b.distance(a);
  6408. const len2 = b.distance(c);
  6409. const frac = len0 / (len0 + len2);
  6410. const dx = c.x - a.x;
  6411. const dy = c.y - a.y;
  6412. const splitPt = new Coordinate(a.x + frac * dx, a.y + frac * dy);
  6413. return splitPt;
  6414. }
  6415. static area3D(a, b, c) {
  6416. const ux = b.x - a.x;
  6417. const uy = b.y - a.y;
  6418. const uz = b.getZ() - a.getZ();
  6419. const vx = c.x - a.x;
  6420. const vy = c.y - a.y;
  6421. const vz = c.getZ() - a.getZ();
  6422. const crossx = uy * vz - uz * vy;
  6423. const crossy = uz * vx - ux * vz;
  6424. const crossz = ux * vy - uy * vx;
  6425. const absSq = crossx * crossx + crossy * crossy + crossz * crossz;
  6426. const area3D = Math.sqrt(absSq) / 2;
  6427. return area3D;
  6428. }
  6429. static centroid(a, b, c) {
  6430. const x = (a.x + b.x + c.x) / 3;
  6431. const y = (a.y + b.y + c.y) / 3;
  6432. return new Coordinate(x, y);
  6433. }
  6434. static inCentre(a, b, c) {
  6435. const len0 = b.distance(c);
  6436. const len1 = a.distance(c);
  6437. const len2 = a.distance(b);
  6438. const circum = len0 + len1 + len2;
  6439. const inCentreX = (len0 * a.x + len1 * b.x + len2 * c.x) / circum;
  6440. const inCentreY = (len0 * a.y + len1 * b.y + len2 * c.y) / circum;
  6441. return new Coordinate(inCentreX, inCentreY);
  6442. }
  6443. area() {
  6444. return Triangle.area(this.p0, this.p1, this.p2);
  6445. }
  6446. signedArea() {
  6447. return Triangle.signedArea(this.p0, this.p1, this.p2);
  6448. }
  6449. interpolateZ(p) {
  6450. if (p === null) throw new IllegalArgumentException('Supplied point is null.');
  6451. return Triangle.interpolateZ(p, this.p0, this.p1, this.p2);
  6452. }
  6453. longestSideLength() {
  6454. return Triangle.longestSideLength(this.p0, this.p1, this.p2);
  6455. }
  6456. isAcute() {
  6457. return Triangle.isAcute(this.p0, this.p1, this.p2);
  6458. }
  6459. circumcentre() {
  6460. return Triangle.circumcentre(this.p0, this.p1, this.p2);
  6461. }
  6462. area3D() {
  6463. return Triangle.area3D(this.p0, this.p1, this.p2);
  6464. }
  6465. centroid() {
  6466. return Triangle.centroid(this.p0, this.p1, this.p2);
  6467. }
  6468. inCentre() {
  6469. return Triangle.inCentre(this.p0, this.p1, this.p2);
  6470. }
  6471. }
  6472. class NoninvertibleTransformationException extends Exception {
  6473. constructor() {
  6474. super();
  6475. NoninvertibleTransformationException.constructor_.apply(this, arguments);
  6476. }
  6477. static constructor_() {
  6478. if (arguments.length === 0) {
  6479. Exception.constructor_.call(this);
  6480. } else if (arguments.length === 1) {
  6481. const msg = arguments[0];
  6482. Exception.constructor_.call(this, msg);
  6483. }
  6484. }
  6485. }
  6486. class AffineTransformation {
  6487. constructor() {
  6488. AffineTransformation.constructor_.apply(this, arguments);
  6489. }
  6490. static constructor_() {
  6491. this._m00 = null;
  6492. this._m01 = null;
  6493. this._m02 = null;
  6494. this._m10 = null;
  6495. this._m11 = null;
  6496. this._m12 = null;
  6497. if (arguments.length === 0) {
  6498. this.setToIdentity();
  6499. } else if (arguments.length === 1) {
  6500. if (arguments[0] instanceof Array) {
  6501. const matrix = arguments[0];
  6502. this._m00 = matrix[0];
  6503. this._m01 = matrix[1];
  6504. this._m02 = matrix[2];
  6505. this._m10 = matrix[3];
  6506. this._m11 = matrix[4];
  6507. this._m12 = matrix[5];
  6508. } else if (arguments[0] instanceof AffineTransformation) {
  6509. const trans = arguments[0];
  6510. this.setTransformation(trans);
  6511. }
  6512. } else if (arguments.length === 6) {
  6513. if (typeof arguments[5] === 'number' && typeof arguments[4] === 'number' && typeof arguments[3] === 'number' && typeof arguments[2] === 'number' && typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  6514. const m00 = arguments[0],
  6515. m01 = arguments[1],
  6516. m02 = arguments[2],
  6517. m10 = arguments[3],
  6518. m11 = arguments[4],
  6519. m12 = arguments[5];
  6520. this.setTransformation(m00, m01, m02, m10, m11, m12);
  6521. }
  6522. }
  6523. }
  6524. static translationInstance(x, y) {
  6525. const trans = new AffineTransformation();
  6526. trans.setToTranslation(x, y);
  6527. return trans;
  6528. }
  6529. static shearInstance(xShear, yShear) {
  6530. const trans = new AffineTransformation();
  6531. trans.setToShear(xShear, yShear);
  6532. return trans;
  6533. }
  6534. static reflectionInstance() {
  6535. if (arguments.length === 2) {
  6536. const x = arguments[0],
  6537. y = arguments[1];
  6538. const trans = new AffineTransformation();
  6539. trans.setToReflection(x, y);
  6540. return trans;
  6541. } else if (arguments.length === 4) {
  6542. const x0 = arguments[0],
  6543. y0 = arguments[1],
  6544. x1 = arguments[2],
  6545. y1 = arguments[3];
  6546. const trans = new AffineTransformation();
  6547. trans.setToReflection(x0, y0, x1, y1);
  6548. return trans;
  6549. }
  6550. }
  6551. static rotationInstance() {
  6552. if (arguments.length === 1) {
  6553. const theta = arguments[0];
  6554. return AffineTransformation.rotationInstance(Math.sin(theta), Math.cos(theta));
  6555. } else if (arguments.length === 2) {
  6556. const sinTheta = arguments[0],
  6557. cosTheta = arguments[1];
  6558. const trans = new AffineTransformation();
  6559. trans.setToRotation(sinTheta, cosTheta);
  6560. return trans;
  6561. } else if (arguments.length === 3) {
  6562. const theta = arguments[0],
  6563. x = arguments[1],
  6564. y = arguments[2];
  6565. return AffineTransformation.rotationInstance(Math.sin(theta), Math.cos(theta), x, y);
  6566. } else if (arguments.length === 4) {
  6567. const sinTheta = arguments[0],
  6568. cosTheta = arguments[1],
  6569. x = arguments[2],
  6570. y = arguments[3];
  6571. const trans = new AffineTransformation();
  6572. trans.setToRotation(sinTheta, cosTheta, x, y);
  6573. return trans;
  6574. }
  6575. }
  6576. static scaleInstance() {
  6577. if (arguments.length === 2) {
  6578. const xScale = arguments[0],
  6579. yScale = arguments[1];
  6580. const trans = new AffineTransformation();
  6581. trans.setToScale(xScale, yScale);
  6582. return trans;
  6583. } else if (arguments.length === 4) {
  6584. const xScale = arguments[0],
  6585. yScale = arguments[1],
  6586. x = arguments[2],
  6587. y = arguments[3];
  6588. const trans = new AffineTransformation();
  6589. trans.translate(-x, -y);
  6590. trans.scale(xScale, yScale);
  6591. trans.translate(x, y);
  6592. return trans;
  6593. }
  6594. }
  6595. setToReflectionBasic(x0, y0, x1, y1) {
  6596. if (x0 === x1 && y0 === y1) throw new IllegalArgumentException('Reflection line points must be distinct');
  6597. const dx = x1 - x0;
  6598. const dy = y1 - y0;
  6599. const d = Math.sqrt(dx * dx + dy * dy);
  6600. const sin = dy / d;
  6601. const cos = dx / d;
  6602. const cs2 = 2 * sin * cos;
  6603. const c2s2 = cos * cos - sin * sin;
  6604. this._m00 = c2s2;
  6605. this._m01 = cs2;
  6606. this._m02 = 0.0;
  6607. this._m10 = cs2;
  6608. this._m11 = -c2s2;
  6609. this._m12 = 0.0;
  6610. return this;
  6611. }
  6612. getInverse() {
  6613. const det = this.getDeterminant();
  6614. if (det === 0) throw new NoninvertibleTransformationException('Transformation is non-invertible');
  6615. const im00 = this._m11 / det;
  6616. const im10 = -this._m10 / det;
  6617. const im01 = -this._m01 / det;
  6618. const im11 = this._m00 / det;
  6619. const im02 = (this._m01 * this._m12 - this._m02 * this._m11) / det;
  6620. const im12 = (-this._m00 * this._m12 + this._m10 * this._m02) / det;
  6621. return new AffineTransformation(im00, im01, im02, im10, im11, im12);
  6622. }
  6623. compose(trans) {
  6624. const mp00 = trans._m00 * this._m00 + trans._m01 * this._m10;
  6625. const mp01 = trans._m00 * this._m01 + trans._m01 * this._m11;
  6626. const mp02 = trans._m00 * this._m02 + trans._m01 * this._m12 + trans._m02;
  6627. const mp10 = trans._m10 * this._m00 + trans._m11 * this._m10;
  6628. const mp11 = trans._m10 * this._m01 + trans._m11 * this._m11;
  6629. const mp12 = trans._m10 * this._m02 + trans._m11 * this._m12 + trans._m12;
  6630. this._m00 = mp00;
  6631. this._m01 = mp01;
  6632. this._m02 = mp02;
  6633. this._m10 = mp10;
  6634. this._m11 = mp11;
  6635. this._m12 = mp12;
  6636. return this;
  6637. }
  6638. equals(obj) {
  6639. if (obj === null) return false;
  6640. if (!(obj instanceof AffineTransformation)) return false;
  6641. const trans = obj;
  6642. return this._m00 === trans._m00 && this._m01 === trans._m01 && this._m02 === trans._m02 && this._m10 === trans._m10 && this._m11 === trans._m11 && this._m12 === trans._m12;
  6643. }
  6644. setToScale(xScale, yScale) {
  6645. this._m00 = xScale;
  6646. this._m01 = 0.0;
  6647. this._m02 = 0.0;
  6648. this._m10 = 0.0;
  6649. this._m11 = yScale;
  6650. this._m12 = 0.0;
  6651. return this;
  6652. }
  6653. isIdentity() {
  6654. return this._m00 === 1 && this._m01 === 0 && this._m02 === 0 && this._m10 === 0 && this._m11 === 1 && this._m12 === 0;
  6655. }
  6656. scale(xScale, yScale) {
  6657. this.compose(AffineTransformation.scaleInstance(xScale, yScale));
  6658. return this;
  6659. }
  6660. setToIdentity() {
  6661. this._m00 = 1.0;
  6662. this._m01 = 0.0;
  6663. this._m02 = 0.0;
  6664. this._m10 = 0.0;
  6665. this._m11 = 1.0;
  6666. this._m12 = 0.0;
  6667. return this;
  6668. }
  6669. isGeometryChanged() {
  6670. return true;
  6671. }
  6672. setTransformation() {
  6673. if (arguments.length === 1) {
  6674. const trans = arguments[0];
  6675. this._m00 = trans._m00;
  6676. this._m01 = trans._m01;
  6677. this._m02 = trans._m02;
  6678. this._m10 = trans._m10;
  6679. this._m11 = trans._m11;
  6680. this._m12 = trans._m12;
  6681. return this;
  6682. } else if (arguments.length === 6) {
  6683. const m00 = arguments[0],
  6684. m01 = arguments[1],
  6685. m02 = arguments[2],
  6686. m10 = arguments[3],
  6687. m11 = arguments[4],
  6688. m12 = arguments[5];
  6689. this._m00 = m00;
  6690. this._m01 = m01;
  6691. this._m02 = m02;
  6692. this._m10 = m10;
  6693. this._m11 = m11;
  6694. this._m12 = m12;
  6695. return this;
  6696. }
  6697. }
  6698. setToRotation() {
  6699. if (arguments.length === 1) {
  6700. const theta = arguments[0];
  6701. this.setToRotation(Math.sin(theta), Math.cos(theta));
  6702. return this;
  6703. } else if (arguments.length === 2) {
  6704. const sinTheta = arguments[0],
  6705. cosTheta = arguments[1];
  6706. this._m00 = cosTheta;
  6707. this._m01 = -sinTheta;
  6708. this._m02 = 0.0;
  6709. this._m10 = sinTheta;
  6710. this._m11 = cosTheta;
  6711. this._m12 = 0.0;
  6712. return this;
  6713. } else if (arguments.length === 3) {
  6714. const theta = arguments[0],
  6715. x = arguments[1],
  6716. y = arguments[2];
  6717. this.setToRotation(Math.sin(theta), Math.cos(theta), x, y);
  6718. return this;
  6719. } else if (arguments.length === 4) {
  6720. const sinTheta = arguments[0],
  6721. cosTheta = arguments[1],
  6722. x = arguments[2],
  6723. y = arguments[3];
  6724. this._m00 = cosTheta;
  6725. this._m01 = -sinTheta;
  6726. this._m02 = x - x * cosTheta + y * sinTheta;
  6727. this._m10 = sinTheta;
  6728. this._m11 = cosTheta;
  6729. this._m12 = y - x * sinTheta - y * cosTheta;
  6730. return this;
  6731. }
  6732. }
  6733. getMatrixEntries() {
  6734. return [this._m00, this._m01, this._m02, this._m10, this._m11, this._m12];
  6735. }
  6736. filter(seq, i) {
  6737. this.transform(seq, i);
  6738. }
  6739. rotate() {
  6740. if (arguments.length === 1) {
  6741. const theta = arguments[0];
  6742. this.compose(AffineTransformation.rotationInstance(theta));
  6743. return this;
  6744. } else if (arguments.length === 2) {
  6745. const sinTheta = arguments[0],
  6746. cosTheta = arguments[1];
  6747. this.compose(AffineTransformation.rotationInstance(sinTheta, cosTheta));
  6748. return this;
  6749. } else if (arguments.length === 3) {
  6750. const theta = arguments[0],
  6751. x = arguments[1],
  6752. y = arguments[2];
  6753. this.compose(AffineTransformation.rotationInstance(theta, x, y));
  6754. return this;
  6755. } else if (arguments.length === 4) {
  6756. const sinTheta = arguments[0],
  6757. cosTheta = arguments[1],
  6758. x = arguments[2],
  6759. y = arguments[3];
  6760. this.compose(AffineTransformation.rotationInstance(sinTheta, cosTheta, x, y));
  6761. return this;
  6762. }
  6763. }
  6764. getDeterminant() {
  6765. return this._m00 * this._m11 - this._m01 * this._m10;
  6766. }
  6767. composeBefore(trans) {
  6768. const mp00 = this._m00 * trans._m00 + this._m01 * trans._m10;
  6769. const mp01 = this._m00 * trans._m01 + this._m01 * trans._m11;
  6770. const mp02 = this._m00 * trans._m02 + this._m01 * trans._m12 + this._m02;
  6771. const mp10 = this._m10 * trans._m00 + this._m11 * trans._m10;
  6772. const mp11 = this._m10 * trans._m01 + this._m11 * trans._m11;
  6773. const mp12 = this._m10 * trans._m02 + this._m11 * trans._m12 + this._m12;
  6774. this._m00 = mp00;
  6775. this._m01 = mp01;
  6776. this._m02 = mp02;
  6777. this._m10 = mp10;
  6778. this._m11 = mp11;
  6779. this._m12 = mp12;
  6780. return this;
  6781. }
  6782. setToShear(xShear, yShear) {
  6783. this._m00 = 1.0;
  6784. this._m01 = xShear;
  6785. this._m02 = 0.0;
  6786. this._m10 = yShear;
  6787. this._m11 = 1.0;
  6788. this._m12 = 0.0;
  6789. return this;
  6790. }
  6791. isDone() {
  6792. return false;
  6793. }
  6794. clone() {
  6795. try {
  6796. return null;
  6797. } catch (ex) {
  6798. if (ex instanceof Exception) Assert.shouldNeverReachHere();else throw ex;
  6799. } finally {}
  6800. return null;
  6801. }
  6802. translate(x, y) {
  6803. this.compose(AffineTransformation.translationInstance(x, y));
  6804. return this;
  6805. }
  6806. setToReflection() {
  6807. if (arguments.length === 2) {
  6808. const x = arguments[0],
  6809. y = arguments[1];
  6810. if (x === 0.0 && y === 0.0) throw new IllegalArgumentException('Reflection vector must be non-zero');
  6811. if (x === y) {
  6812. this._m00 = 0.0;
  6813. this._m01 = 1.0;
  6814. this._m02 = 0.0;
  6815. this._m10 = 1.0;
  6816. this._m11 = 0.0;
  6817. this._m12 = 0.0;
  6818. return this;
  6819. }
  6820. const d = Math.sqrt(x * x + y * y);
  6821. const sin = y / d;
  6822. const cos = x / d;
  6823. this.rotate(-sin, cos);
  6824. this.scale(1, -1);
  6825. this.rotate(sin, cos);
  6826. return this;
  6827. } else if (arguments.length === 4) {
  6828. const x0 = arguments[0],
  6829. y0 = arguments[1],
  6830. x1 = arguments[2],
  6831. y1 = arguments[3];
  6832. if (x0 === x1 && y0 === y1) throw new IllegalArgumentException('Reflection line points must be distinct');
  6833. this.setToTranslation(-x0, -y0);
  6834. const dx = x1 - x0;
  6835. const dy = y1 - y0;
  6836. const d = Math.sqrt(dx * dx + dy * dy);
  6837. const sin = dy / d;
  6838. const cos = dx / d;
  6839. this.rotate(-sin, cos);
  6840. this.scale(1, -1);
  6841. this.rotate(sin, cos);
  6842. this.translate(x0, y0);
  6843. return this;
  6844. }
  6845. }
  6846. toString() {
  6847. return 'AffineTransformation[[' + this._m00 + ', ' + this._m01 + ', ' + this._m02 + '], [' + this._m10 + ', ' + this._m11 + ', ' + this._m12 + ']]';
  6848. }
  6849. setToTranslation(dx, dy) {
  6850. this._m00 = 1.0;
  6851. this._m01 = 0.0;
  6852. this._m02 = dx;
  6853. this._m10 = 0.0;
  6854. this._m11 = 1.0;
  6855. this._m12 = dy;
  6856. return this;
  6857. }
  6858. shear(xShear, yShear) {
  6859. this.compose(AffineTransformation.shearInstance(xShear, yShear));
  6860. return this;
  6861. }
  6862. transform() {
  6863. if (arguments.length === 1) {
  6864. const g = arguments[0];
  6865. const g2 = g.copy();
  6866. g2.apply(this);
  6867. return g2;
  6868. } else if (arguments.length === 2) {
  6869. if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  6870. const src = arguments[0],
  6871. dest = arguments[1];
  6872. const xp = this._m00 * src.x + this._m01 * src.y + this._m02;
  6873. const yp = this._m10 * src.x + this._m11 * src.y + this._m12;
  6874. dest.x = xp;
  6875. dest.y = yp;
  6876. return dest;
  6877. } else if (hasInterface(arguments[0], CoordinateSequence) && Number.isInteger(arguments[1])) {
  6878. const seq = arguments[0],
  6879. i = arguments[1];
  6880. const xp = this._m00 * seq.getOrdinate(i, 0) + this._m01 * seq.getOrdinate(i, 1) + this._m02;
  6881. const yp = this._m10 * seq.getOrdinate(i, 0) + this._m11 * seq.getOrdinate(i, 1) + this._m12;
  6882. seq.setOrdinate(i, 0, xp);
  6883. seq.setOrdinate(i, 1, yp);
  6884. }
  6885. }
  6886. }
  6887. reflect() {
  6888. if (arguments.length === 2) {
  6889. const x = arguments[0],
  6890. y = arguments[1];
  6891. this.compose(AffineTransformation.reflectionInstance(x, y));
  6892. return this;
  6893. } else if (arguments.length === 4) {
  6894. const x0 = arguments[0],
  6895. y0 = arguments[1],
  6896. x1 = arguments[2],
  6897. y1 = arguments[3];
  6898. this.compose(AffineTransformation.reflectionInstance(x0, y0, x1, y1));
  6899. return this;
  6900. }
  6901. }
  6902. get interfaces_() {
  6903. return [Clonable, CoordinateSequenceFilter];
  6904. }
  6905. }
  6906. class Matrix {
  6907. static solve(a, b) {
  6908. const n = b.length;
  6909. if (a.length !== n || a[0].length !== n) throw new IllegalArgumentException('Matrix A is incorrectly sized');
  6910. for (let i = 0; i < n; i++) {
  6911. let maxElementRow = i;
  6912. for (let j = i + 1; j < n; j++) if (Math.abs(a[j][i]) > Math.abs(a[maxElementRow][i])) maxElementRow = j;
  6913. if (a[maxElementRow][i] === 0.0) return null;
  6914. Matrix.swapRows(a, i, maxElementRow);
  6915. Matrix.swapRows(b, i, maxElementRow);
  6916. for (let j = i + 1; j < n; j++) {
  6917. const rowFactor = a[j][i] / a[i][i];
  6918. for (let k = n - 1; k >= i; k--) a[j][k] -= a[i][k] * rowFactor;
  6919. b[j] -= b[i] * rowFactor;
  6920. }
  6921. }
  6922. const solution = new Array(n).fill(null);
  6923. for (let j = n - 1; j >= 0; j--) {
  6924. let t = 0.0;
  6925. for (let k = j + 1; k < n; k++) t += a[j][k] * solution[k];
  6926. solution[j] = (b[j] - t) / a[j][j];
  6927. }
  6928. return solution;
  6929. }
  6930. static swapRows() {
  6931. if (Number.isInteger(arguments[2]) && arguments[0] instanceof Array && Number.isInteger(arguments[1])) {
  6932. const m = arguments[0],
  6933. i = arguments[1],
  6934. j = arguments[2];
  6935. if (i === j) return null;
  6936. for (let col = 0; col < m[0].length; col++) {
  6937. const temp = m[i][col];
  6938. m[i][col] = m[j][col];
  6939. m[j][col] = temp;
  6940. }
  6941. } else if (Number.isInteger(arguments[2]) && arguments[0] instanceof Array && Number.isInteger(arguments[1])) {
  6942. const m = arguments[0],
  6943. i = arguments[1],
  6944. j = arguments[2];
  6945. if (i === j) return null;
  6946. const temp = m[i];
  6947. m[i] = m[j];
  6948. m[j] = temp;
  6949. }
  6950. }
  6951. }
  6952. class AffineTransformationBuilder {
  6953. constructor() {
  6954. AffineTransformationBuilder.constructor_.apply(this, arguments);
  6955. }
  6956. static constructor_() {
  6957. this._src0 = null;
  6958. this._src1 = null;
  6959. this._src2 = null;
  6960. this._dest0 = null;
  6961. this._dest1 = null;
  6962. this._dest2 = null;
  6963. this._m00 = null;
  6964. this._m01 = null;
  6965. this._m02 = null;
  6966. this._m10 = null;
  6967. this._m11 = null;
  6968. this._m12 = null;
  6969. const src0 = arguments[0],
  6970. src1 = arguments[1],
  6971. src2 = arguments[2],
  6972. dest0 = arguments[3],
  6973. dest1 = arguments[4],
  6974. dest2 = arguments[5];
  6975. this._src0 = src0;
  6976. this._src1 = src1;
  6977. this._src2 = src2;
  6978. this._dest0 = dest0;
  6979. this._dest1 = dest1;
  6980. this._dest2 = dest2;
  6981. }
  6982. solve(b) {
  6983. const a = [[this._src0.x, this._src0.y, 1], [this._src1.x, this._src1.y, 1], [this._src2.x, this._src2.y, 1]];
  6984. return Matrix.solve(a, b);
  6985. }
  6986. compute() {
  6987. const bx = [this._dest0.x, this._dest1.x, this._dest2.x];
  6988. const row0 = this.solve(bx);
  6989. if (row0 === null) return false;
  6990. this._m00 = row0[0];
  6991. this._m01 = row0[1];
  6992. this._m02 = row0[2];
  6993. const by = [this._dest0.y, this._dest1.y, this._dest2.y];
  6994. const row1 = this.solve(by);
  6995. if (row1 === null) return false;
  6996. this._m10 = row1[0];
  6997. this._m11 = row1[1];
  6998. this._m12 = row1[2];
  6999. return true;
  7000. }
  7001. getTransformation() {
  7002. const isSolvable = this.compute();
  7003. if (isSolvable) return new AffineTransformation(this._m00, this._m01, this._m02, this._m10, this._m11, this._m12);
  7004. return null;
  7005. }
  7006. }
  7007. class AffineTransformationFactory {
  7008. static createFromBaseLines(src0, src1, dest0, dest1) {
  7009. const rotPt = new Coordinate(src0.x + dest1.x - dest0.x, src0.y + dest1.y - dest0.y);
  7010. const ang = Angle.angleBetweenOriented(src1, src0, rotPt);
  7011. const srcDist = src1.distance(src0);
  7012. const destDist = dest1.distance(dest0);
  7013. if (srcDist === 0.0) return new AffineTransformation();
  7014. const scale = destDist / srcDist;
  7015. const trans = AffineTransformation.translationInstance(-src0.x, -src0.y);
  7016. trans.rotate(ang);
  7017. trans.scale(scale, scale);
  7018. trans.translate(dest0.x, dest0.y);
  7019. return trans;
  7020. }
  7021. static createFromControlVectors() {
  7022. if (arguments.length === 2) {
  7023. if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  7024. const src0 = arguments[0],
  7025. dest0 = arguments[1];
  7026. const dx = dest0.x - src0.x;
  7027. const dy = dest0.y - src0.y;
  7028. return AffineTransformation.translationInstance(dx, dy);
  7029. } else if (arguments[0] instanceof Array && arguments[1] instanceof Array) {
  7030. const src = arguments[0],
  7031. dest = arguments[1];
  7032. if (src.length !== dest.length) throw new IllegalArgumentException('Src and Dest arrays are not the same length');
  7033. if (src.length <= 0) throw new IllegalArgumentException('Too few control points');
  7034. if (src.length > 3) throw new IllegalArgumentException('Too many control points');
  7035. if (src.length === 1) return AffineTransformationFactory.createFromControlVectors(src[0], dest[0]);
  7036. if (src.length === 2) return AffineTransformationFactory.createFromControlVectors(src[0], src[1], dest[0], dest[1]);
  7037. return AffineTransformationFactory.createFromControlVectors(src[0], src[1], src[2], dest[0], dest[1], dest[2]);
  7038. }
  7039. } else if (arguments.length === 4) {
  7040. const src0 = arguments[0],
  7041. src1 = arguments[1],
  7042. dest0 = arguments[2],
  7043. dest1 = arguments[3];
  7044. const rotPt = new Coordinate(dest1.x - dest0.x, dest1.y - dest0.y);
  7045. const ang = Angle.angleBetweenOriented(src1, src0, rotPt);
  7046. const srcDist = src1.distance(src0);
  7047. const destDist = dest1.distance(dest0);
  7048. if (srcDist === 0.0) return null;
  7049. const scale = destDist / srcDist;
  7050. const trans = AffineTransformation.translationInstance(-src0.x, -src0.y);
  7051. trans.rotate(ang);
  7052. trans.scale(scale, scale);
  7053. trans.translate(dest0.x, dest0.y);
  7054. return trans;
  7055. } else if (arguments.length === 6) {
  7056. const src0 = arguments[0],
  7057. src1 = arguments[1],
  7058. src2 = arguments[2],
  7059. dest0 = arguments[3],
  7060. dest1 = arguments[4],
  7061. dest2 = arguments[5];
  7062. const builder = new AffineTransformationBuilder(src0, src1, src2, dest0, dest1, dest2);
  7063. return builder.getTransformation();
  7064. }
  7065. }
  7066. }
  7067. class ComponentCoordinateExtracter {
  7068. constructor() {
  7069. ComponentCoordinateExtracter.constructor_.apply(this, arguments);
  7070. }
  7071. static constructor_() {
  7072. this._coords = null;
  7073. const coords = arguments[0];
  7074. this._coords = coords;
  7075. }
  7076. static getCoordinates(geom) {
  7077. const coords = new ArrayList();
  7078. geom.apply(new ComponentCoordinateExtracter(coords));
  7079. return coords;
  7080. }
  7081. filter(geom) {
  7082. if (geom instanceof LineString || geom instanceof Point) this._coords.add(geom.getCoordinate());
  7083. }
  7084. get interfaces_() {
  7085. return [GeometryComponentFilter];
  7086. }
  7087. }
  7088. class GeometryCollectionMapper {
  7089. constructor() {
  7090. GeometryCollectionMapper.constructor_.apply(this, arguments);
  7091. }
  7092. static constructor_() {
  7093. this._mapOp = null;
  7094. const mapOp = arguments[0];
  7095. this._mapOp = mapOp;
  7096. }
  7097. static map(gc, op) {
  7098. const mapper = new GeometryCollectionMapper(op);
  7099. return mapper.map(gc);
  7100. }
  7101. map(gc) {
  7102. const mapped = new ArrayList();
  7103. for (let i = 0; i < gc.getNumGeometries(); i++) {
  7104. const g = this._mapOp.map(gc.getGeometryN(i));
  7105. if (!g.isEmpty()) mapped.add(g);
  7106. }
  7107. return gc.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(mapped));
  7108. }
  7109. }
  7110. class GeometryCombiner {
  7111. constructor() {
  7112. GeometryCombiner.constructor_.apply(this, arguments);
  7113. }
  7114. static constructor_() {
  7115. this._geomFactory = null;
  7116. this._skipEmpty = false;
  7117. this._inputGeoms = null;
  7118. const geoms = arguments[0];
  7119. this._geomFactory = GeometryCombiner.extractFactory(geoms);
  7120. this._inputGeoms = geoms;
  7121. }
  7122. static combine() {
  7123. if (arguments.length === 1) {
  7124. const geoms = arguments[0];
  7125. const combiner = new GeometryCombiner(geoms);
  7126. return combiner.combine();
  7127. } else if (arguments.length === 2) {
  7128. const g0 = arguments[0],
  7129. g1 = arguments[1];
  7130. const combiner = new GeometryCombiner(GeometryCombiner.createList(g0, g1));
  7131. return combiner.combine();
  7132. } else if (arguments.length === 3) {
  7133. const g0 = arguments[0],
  7134. g1 = arguments[1],
  7135. g2 = arguments[2];
  7136. const combiner = new GeometryCombiner(GeometryCombiner.createList(g0, g1, g2));
  7137. return combiner.combine();
  7138. }
  7139. }
  7140. static extractFactory(geoms) {
  7141. if (geoms.isEmpty()) return null;
  7142. return geoms.iterator().next().getFactory();
  7143. }
  7144. static createList() {
  7145. if (arguments.length === 2) {
  7146. const obj0 = arguments[0],
  7147. obj1 = arguments[1];
  7148. const list = new ArrayList();
  7149. list.add(obj0);
  7150. list.add(obj1);
  7151. return list;
  7152. } else if (arguments.length === 3) {
  7153. const obj0 = arguments[0],
  7154. obj1 = arguments[1],
  7155. obj2 = arguments[2];
  7156. const list = new ArrayList();
  7157. list.add(obj0);
  7158. list.add(obj1);
  7159. list.add(obj2);
  7160. return list;
  7161. }
  7162. }
  7163. extractElements(geom, elems) {
  7164. if (geom === null) return null;
  7165. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7166. const elemGeom = geom.getGeometryN(i);
  7167. if (this._skipEmpty && elemGeom.isEmpty()) continue;
  7168. elems.add(elemGeom);
  7169. }
  7170. }
  7171. combine() {
  7172. const elems = new ArrayList();
  7173. for (let i = this._inputGeoms.iterator(); i.hasNext();) {
  7174. const g = i.next();
  7175. this.extractElements(g, elems);
  7176. }
  7177. if (elems.size() === 0) {
  7178. if (this._geomFactory !== null) return this._geomFactory.createGeometryCollection();
  7179. return null;
  7180. }
  7181. return this._geomFactory.buildGeometry(elems);
  7182. }
  7183. }
  7184. class GeometryEditor {
  7185. constructor() {
  7186. GeometryEditor.constructor_.apply(this, arguments);
  7187. }
  7188. static constructor_() {
  7189. this._factory = null;
  7190. this._isUserDataCopied = false;
  7191. if (arguments.length === 0) ; else if (arguments.length === 1) {
  7192. const factory = arguments[0];
  7193. this._factory = factory;
  7194. }
  7195. }
  7196. setCopyUserData(isUserDataCopied) {
  7197. this._isUserDataCopied = isUserDataCopied;
  7198. }
  7199. edit(geometry, operation) {
  7200. if (geometry === null) return null;
  7201. const result = this.editInternal(geometry, operation);
  7202. if (this._isUserDataCopied) result.setUserData(geometry.getUserData());
  7203. return result;
  7204. }
  7205. editInternal(geometry, operation) {
  7206. if (this._factory === null) this._factory = geometry.getFactory();
  7207. if (geometry instanceof GeometryCollection) return this.editGeometryCollection(geometry, operation);
  7208. if (geometry instanceof Polygon) return this.editPolygon(geometry, operation);
  7209. if (geometry instanceof Point) return operation.edit(geometry, this._factory);
  7210. if (geometry instanceof LineString) return operation.edit(geometry, this._factory);
  7211. Assert.shouldNeverReachHere('Unsupported Geometry type: ' + geometry.getGeometryType());
  7212. return null;
  7213. }
  7214. editGeometryCollection(collection, operation) {
  7215. const collectionForType = operation.edit(collection, this._factory);
  7216. const geometries = new ArrayList();
  7217. for (let i = 0; i < collectionForType.getNumGeometries(); i++) {
  7218. const geometry = this.edit(collectionForType.getGeometryN(i), operation);
  7219. if (geometry === null || geometry.isEmpty()) continue;
  7220. geometries.add(geometry);
  7221. }
  7222. if (collectionForType.getGeometryType() === Geometry.TYPENAME_MULTIPOINT) return this._factory.createMultiPoint(geometries.toArray([]));
  7223. if (collectionForType.getGeometryType() === Geometry.TYPENAME_MULTILINESTRING) return this._factory.createMultiLineString(geometries.toArray([]));
  7224. if (collectionForType.getGeometryType() === Geometry.TYPENAME_MULTIPOLYGON) return this._factory.createMultiPolygon(geometries.toArray([]));
  7225. return this._factory.createGeometryCollection(geometries.toArray([]));
  7226. }
  7227. editPolygon(polygon, operation) {
  7228. let newPolygon = operation.edit(polygon, this._factory);
  7229. if (newPolygon === null) newPolygon = this._factory.createPolygon();
  7230. if (newPolygon.isEmpty()) return newPolygon;
  7231. const shell = this.edit(newPolygon.getExteriorRing(), operation);
  7232. if (shell === null || shell.isEmpty()) return this._factory.createPolygon();
  7233. const holes = new ArrayList();
  7234. for (let i = 0; i < newPolygon.getNumInteriorRing(); i++) {
  7235. const hole = this.edit(newPolygon.getInteriorRingN(i), operation);
  7236. if (hole === null || hole.isEmpty()) continue;
  7237. holes.add(hole);
  7238. }
  7239. return this._factory.createPolygon(shell, holes.toArray([]));
  7240. }
  7241. }
  7242. function GeometryEditorOperation() {}
  7243. GeometryEditor.GeometryEditorOperation = GeometryEditorOperation;
  7244. class NoOpGeometryOperation {
  7245. edit(geometry, factory) {
  7246. return geometry;
  7247. }
  7248. get interfaces_() {
  7249. return [GeometryEditorOperation];
  7250. }
  7251. }
  7252. class CoordinateOperation {
  7253. edit(geometry, factory) {
  7254. const coordinates = this.edit(geometry.getCoordinates(), geometry);
  7255. if (geometry instanceof LinearRing) if (coordinates === null) return factory.createLinearRing();else return factory.createLinearRing(coordinates);
  7256. if (geometry instanceof LineString) if (coordinates === null) return factory.createLineString();else return factory.createLineString(coordinates);
  7257. if (geometry instanceof Point) if (coordinates === null || coordinates.length === 0) return factory.createPoint();else return factory.createPoint(coordinates[0]);
  7258. return geometry;
  7259. }
  7260. get interfaces_() {
  7261. return [GeometryEditorOperation];
  7262. }
  7263. }
  7264. class CoordinateSequenceOperation {
  7265. edit(geometry, factory) {
  7266. if (geometry instanceof LinearRing) return factory.createLinearRing(this.edit(geometry.getCoordinateSequence(), geometry));
  7267. if (geometry instanceof LineString) return factory.createLineString(this.edit(geometry.getCoordinateSequence(), geometry));
  7268. if (geometry instanceof Point) return factory.createPoint(this.edit(geometry.getCoordinateSequence(), geometry));
  7269. return geometry;
  7270. }
  7271. get interfaces_() {
  7272. return [GeometryEditorOperation];
  7273. }
  7274. }
  7275. GeometryEditor.NoOpGeometryOperation = NoOpGeometryOperation;
  7276. GeometryEditor.CoordinateOperation = CoordinateOperation;
  7277. GeometryEditor.CoordinateSequenceOperation = CoordinateSequenceOperation;
  7278. class GeometryExtracter {
  7279. constructor() {
  7280. GeometryExtracter.constructor_.apply(this, arguments);
  7281. }
  7282. static constructor_() {
  7283. this._geometryType = null;
  7284. this._comps = null;
  7285. const geometryType = arguments[0],
  7286. comps = arguments[1];
  7287. this._geometryType = geometryType;
  7288. this._comps = comps;
  7289. }
  7290. static isOfType(geom, geometryType) {
  7291. if (geom.getGeometryType() === geometryType) return true;
  7292. if (geometryType === Geometry.TYPENAME_LINESTRING && geom.getGeometryType() === Geometry.TYPENAME_LINEARRING) return true;
  7293. return false;
  7294. }
  7295. static extract() {
  7296. if (arguments.length === 2) {
  7297. const geom = arguments[0],
  7298. geometryType = arguments[1];
  7299. return GeometryExtracter.extract(geom, geometryType, new ArrayList());
  7300. } else if (arguments.length === 3) {
  7301. const geom = arguments[0],
  7302. geometryType = arguments[1],
  7303. list = arguments[2];
  7304. if (geom.getGeometryType() === geometryType) list.add(geom);else if (geom instanceof GeometryCollection) geom.apply(new GeometryExtracter(geometryType, list));
  7305. return list;
  7306. }
  7307. }
  7308. filter(geom) {
  7309. if (this._geometryType === null || GeometryExtracter.isOfType(geom, this._geometryType)) this._comps.add(geom);
  7310. }
  7311. get interfaces_() {
  7312. return [GeometryFilter];
  7313. }
  7314. }
  7315. class GeometryMapper {
  7316. static map() {
  7317. if (arguments[0] instanceof Geometry && hasInterface(arguments[1], MapOp$1)) {
  7318. const geom = arguments[0],
  7319. op = arguments[1];
  7320. const mapped = new ArrayList();
  7321. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7322. const g = op.map(geom.getGeometryN(i));
  7323. if (g !== null) mapped.add(g);
  7324. }
  7325. return geom.getFactory().buildGeometry(mapped);
  7326. } else if (hasInterface(arguments[0], Collection) && hasInterface(arguments[1], MapOp$1)) {
  7327. const geoms = arguments[0],
  7328. op = arguments[1];
  7329. const mapped = new ArrayList();
  7330. for (let i = geoms.iterator(); i.hasNext();) {
  7331. const g = i.next();
  7332. const gr = op.map(g);
  7333. if (gr !== null) mapped.add(gr);
  7334. }
  7335. return mapped;
  7336. }
  7337. }
  7338. }
  7339. function MapOp$1() {}
  7340. GeometryMapper.MapOp = MapOp$1;
  7341. class GeometryTransformer {
  7342. constructor() {
  7343. GeometryTransformer.constructor_.apply(this, arguments);
  7344. }
  7345. static constructor_() {
  7346. this._inputGeom = null;
  7347. this._factory = null;
  7348. this._pruneEmptyGeometry = true;
  7349. this._preserveGeometryCollectionType = true;
  7350. this._preserveCollections = false;
  7351. this._preserveType = false;
  7352. }
  7353. transformPoint(geom, parent) {
  7354. return this._factory.createPoint(this.transformCoordinates(geom.getCoordinateSequence(), geom));
  7355. }
  7356. transformPolygon(geom, parent) {
  7357. let isAllValidLinearRings = true;
  7358. const shell = this.transformLinearRing(geom.getExteriorRing(), geom);
  7359. if (shell === null || !(shell instanceof LinearRing) || shell.isEmpty()) isAllValidLinearRings = false;
  7360. const holes = new ArrayList();
  7361. for (let i = 0; i < geom.getNumInteriorRing(); i++) {
  7362. const hole = this.transformLinearRing(geom.getInteriorRingN(i), geom);
  7363. if (hole === null || hole.isEmpty()) continue;
  7364. if (!(hole instanceof LinearRing)) isAllValidLinearRings = false;
  7365. holes.add(hole);
  7366. }
  7367. if (isAllValidLinearRings) {
  7368. return this._factory.createPolygon(shell, holes.toArray([]));
  7369. } else {
  7370. const components = new ArrayList();
  7371. if (shell !== null) components.add(shell);
  7372. components.addAll(holes);
  7373. return this._factory.buildGeometry(components);
  7374. }
  7375. }
  7376. createCoordinateSequence(coords) {
  7377. return this._factory.getCoordinateSequenceFactory().create(coords);
  7378. }
  7379. getInputGeometry() {
  7380. return this._inputGeom;
  7381. }
  7382. transformMultiLineString(geom, parent) {
  7383. const transGeomList = new ArrayList();
  7384. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7385. const transformGeom = this.transformLineString(geom.getGeometryN(i), geom);
  7386. if (transformGeom === null) continue;
  7387. if (transformGeom.isEmpty()) continue;
  7388. transGeomList.add(transformGeom);
  7389. }
  7390. return this._factory.buildGeometry(transGeomList);
  7391. }
  7392. transformCoordinates(coords, parent) {
  7393. return this.copy(coords);
  7394. }
  7395. transformLineString(geom, parent) {
  7396. return this._factory.createLineString(this.transformCoordinates(geom.getCoordinateSequence(), geom));
  7397. }
  7398. transformMultiPoint(geom, parent) {
  7399. const transGeomList = new ArrayList();
  7400. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7401. const transformGeom = this.transformPoint(geom.getGeometryN(i), geom);
  7402. if (transformGeom === null) continue;
  7403. if (transformGeom.isEmpty()) continue;
  7404. transGeomList.add(transformGeom);
  7405. }
  7406. return this._factory.buildGeometry(transGeomList);
  7407. }
  7408. transformMultiPolygon(geom, parent) {
  7409. const transGeomList = new ArrayList();
  7410. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7411. const transformGeom = this.transformPolygon(geom.getGeometryN(i), geom);
  7412. if (transformGeom === null) continue;
  7413. if (transformGeom.isEmpty()) continue;
  7414. transGeomList.add(transformGeom);
  7415. }
  7416. return this._factory.buildGeometry(transGeomList);
  7417. }
  7418. copy(seq) {
  7419. return seq.copy();
  7420. }
  7421. transformGeometryCollection(geom, parent) {
  7422. const transGeomList = new ArrayList();
  7423. for (let i = 0; i < geom.getNumGeometries(); i++) {
  7424. const transformGeom = this.transform(geom.getGeometryN(i));
  7425. if (transformGeom === null) continue;
  7426. if (this._pruneEmptyGeometry && transformGeom.isEmpty()) continue;
  7427. transGeomList.add(transformGeom);
  7428. }
  7429. if (this._preserveGeometryCollectionType) return this._factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList));
  7430. return this._factory.buildGeometry(transGeomList);
  7431. }
  7432. transform(inputGeom) {
  7433. this._inputGeom = inputGeom;
  7434. this._factory = inputGeom.getFactory();
  7435. if (inputGeom instanceof Point) return this.transformPoint(inputGeom, null);
  7436. if (inputGeom instanceof MultiPoint) return this.transformMultiPoint(inputGeom, null);
  7437. if (inputGeom instanceof LinearRing) return this.transformLinearRing(inputGeom, null);
  7438. if (inputGeom instanceof LineString) return this.transformLineString(inputGeom, null);
  7439. if (inputGeom instanceof MultiLineString) return this.transformMultiLineString(inputGeom, null);
  7440. if (inputGeom instanceof Polygon) return this.transformPolygon(inputGeom, null);
  7441. if (inputGeom instanceof MultiPolygon) return this.transformMultiPolygon(inputGeom, null);
  7442. if (inputGeom instanceof GeometryCollection) return this.transformGeometryCollection(inputGeom, null);
  7443. throw new IllegalArgumentException('Unknown Geometry subtype: ' + inputGeom.getGeometryType());
  7444. }
  7445. transformLinearRing(geom, parent) {
  7446. const seq = this.transformCoordinates(geom.getCoordinateSequence(), geom);
  7447. if (seq === null) return this._factory.createLinearRing(null);
  7448. const seqSize = seq.size();
  7449. if (seqSize > 0 && seqSize < 4 && !this._preserveType) return this._factory.createLineString(seq);
  7450. return this._factory.createLinearRing(seq);
  7451. }
  7452. }
  7453. class LineStringExtracter {
  7454. constructor() {
  7455. LineStringExtracter.constructor_.apply(this, arguments);
  7456. }
  7457. static constructor_() {
  7458. this._comps = null;
  7459. const comps = arguments[0];
  7460. this._comps = comps;
  7461. }
  7462. static getGeometry(geom) {
  7463. return geom.getFactory().buildGeometry(LineStringExtracter.getLines(geom));
  7464. }
  7465. static getLines() {
  7466. if (arguments.length === 1) {
  7467. const geom = arguments[0];
  7468. return LineStringExtracter.getLines(geom, new ArrayList());
  7469. } else if (arguments.length === 2) {
  7470. const geom = arguments[0],
  7471. lines = arguments[1];
  7472. if (geom instanceof LineString) lines.add(geom);else if (geom instanceof GeometryCollection) geom.apply(new LineStringExtracter(lines));
  7473. return lines;
  7474. }
  7475. }
  7476. filter(geom) {
  7477. if (geom instanceof LineString) this._comps.add(geom);
  7478. }
  7479. get interfaces_() {
  7480. return [GeometryFilter];
  7481. }
  7482. }
  7483. class LinearComponentExtracter {
  7484. constructor() {
  7485. LinearComponentExtracter.constructor_.apply(this, arguments);
  7486. }
  7487. static constructor_() {
  7488. this._lines = null;
  7489. this._isForcedToLineString = false;
  7490. if (arguments.length === 1) {
  7491. const lines = arguments[0];
  7492. this._lines = lines;
  7493. } else if (arguments.length === 2) {
  7494. const lines = arguments[0],
  7495. isForcedToLineString = arguments[1];
  7496. this._lines = lines;
  7497. this._isForcedToLineString = isForcedToLineString;
  7498. }
  7499. }
  7500. static getGeometry() {
  7501. if (arguments.length === 1) {
  7502. const geom = arguments[0];
  7503. return geom.getFactory().buildGeometry(LinearComponentExtracter.getLines(geom));
  7504. } else if (arguments.length === 2) {
  7505. const geom = arguments[0],
  7506. forceToLineString = arguments[1];
  7507. return geom.getFactory().buildGeometry(LinearComponentExtracter.getLines(geom, forceToLineString));
  7508. }
  7509. }
  7510. static getLines() {
  7511. if (arguments.length === 1) {
  7512. const geom = arguments[0];
  7513. return LinearComponentExtracter.getLines(geom, false);
  7514. } else if (arguments.length === 2) {
  7515. if (hasInterface(arguments[0], Collection) && hasInterface(arguments[1], Collection)) {
  7516. const geoms = arguments[0],
  7517. lines = arguments[1];
  7518. for (let i = geoms.iterator(); i.hasNext();) {
  7519. const g = i.next();
  7520. LinearComponentExtracter.getLines(g, lines);
  7521. }
  7522. return lines;
  7523. } else if (arguments[0] instanceof Geometry && typeof arguments[1] === 'boolean') {
  7524. const geom = arguments[0],
  7525. forceToLineString = arguments[1];
  7526. const lines = new ArrayList();
  7527. geom.apply(new LinearComponentExtracter(lines, forceToLineString));
  7528. return lines;
  7529. } else if (arguments[0] instanceof Geometry && hasInterface(arguments[1], Collection)) {
  7530. const geom = arguments[0],
  7531. lines = arguments[1];
  7532. if (geom instanceof LineString) lines.add(geom);else geom.apply(new LinearComponentExtracter(lines));
  7533. return lines;
  7534. }
  7535. } else if (arguments.length === 3) {
  7536. if (typeof arguments[2] === 'boolean' && hasInterface(arguments[0], Collection) && hasInterface(arguments[1], Collection)) {
  7537. const geoms = arguments[0],
  7538. lines = arguments[1],
  7539. forceToLineString = arguments[2];
  7540. for (let i = geoms.iterator(); i.hasNext();) {
  7541. const g = i.next();
  7542. LinearComponentExtracter.getLines(g, lines, forceToLineString);
  7543. }
  7544. return lines;
  7545. } else if (typeof arguments[2] === 'boolean' && arguments[0] instanceof Geometry && hasInterface(arguments[1], Collection)) {
  7546. const geom = arguments[0],
  7547. lines = arguments[1],
  7548. forceToLineString = arguments[2];
  7549. geom.apply(new LinearComponentExtracter(lines, forceToLineString));
  7550. return lines;
  7551. }
  7552. }
  7553. }
  7554. filter(geom) {
  7555. if (this._isForcedToLineString && geom instanceof LinearRing) {
  7556. const line = geom.getFactory().createLineString(geom.getCoordinateSequence());
  7557. this._lines.add(line);
  7558. return null;
  7559. }
  7560. if (geom instanceof LineString) this._lines.add(geom);
  7561. }
  7562. setForceToLineString(isForcedToLineString) {
  7563. this._isForcedToLineString = isForcedToLineString;
  7564. }
  7565. get interfaces_() {
  7566. return [GeometryComponentFilter];
  7567. }
  7568. }
  7569. const Collections = {
  7570. reverseOrder: function () {
  7571. return {
  7572. compare(a, b) {
  7573. return b.compareTo(a);
  7574. }
  7575. };
  7576. },
  7577. min: function (l) {
  7578. Collections.sort(l);
  7579. return l.get(0);
  7580. },
  7581. sort: function (l, c) {
  7582. const a = l.toArray();
  7583. if (c) Arrays.sort(a, c);else Arrays.sort(a);
  7584. const i = l.iterator();
  7585. for (let pos = 0, alen = a.length; pos < alen; pos++) {
  7586. i.next();
  7587. i.set(a[pos]);
  7588. }
  7589. },
  7590. singletonList: function (o) {
  7591. const arrayList = new ArrayList();
  7592. arrayList.add(o);
  7593. return arrayList;
  7594. }
  7595. };
  7596. class PointExtracter {
  7597. constructor() {
  7598. PointExtracter.constructor_.apply(this, arguments);
  7599. }
  7600. static constructor_() {
  7601. this._pts = null;
  7602. const pts = arguments[0];
  7603. this._pts = pts;
  7604. }
  7605. static getPoints() {
  7606. if (arguments.length === 1) {
  7607. const geom = arguments[0];
  7608. if (geom instanceof Point) return Collections.singletonList(geom);
  7609. return PointExtracter.getPoints(geom, new ArrayList());
  7610. } else if (arguments.length === 2) {
  7611. const geom = arguments[0],
  7612. list = arguments[1];
  7613. if (geom instanceof Point) list.add(geom);else if (geom instanceof GeometryCollection) geom.apply(new PointExtracter(list));
  7614. return list;
  7615. }
  7616. }
  7617. filter(geom) {
  7618. if (geom instanceof Point) this._pts.add(geom);
  7619. }
  7620. get interfaces_() {
  7621. return [GeometryFilter];
  7622. }
  7623. }
  7624. class PolygonExtracter {
  7625. constructor() {
  7626. PolygonExtracter.constructor_.apply(this, arguments);
  7627. }
  7628. static constructor_() {
  7629. this._comps = null;
  7630. const comps = arguments[0];
  7631. this._comps = comps;
  7632. }
  7633. static getPolygons() {
  7634. if (arguments.length === 1) {
  7635. const geom = arguments[0];
  7636. return PolygonExtracter.getPolygons(geom, new ArrayList());
  7637. } else if (arguments.length === 2) {
  7638. const geom = arguments[0],
  7639. list = arguments[1];
  7640. if (geom instanceof Polygon) list.add(geom);else if (geom instanceof GeometryCollection) geom.apply(new PolygonExtracter(list));
  7641. return list;
  7642. }
  7643. }
  7644. filter(geom) {
  7645. if (geom instanceof Polygon) this._comps.add(geom);
  7646. }
  7647. get interfaces_() {
  7648. return [GeometryFilter];
  7649. }
  7650. }
  7651. class ShortCircuitedGeometryVisitor {
  7652. constructor() {
  7653. ShortCircuitedGeometryVisitor.constructor_.apply(this, arguments);
  7654. }
  7655. static constructor_() {
  7656. this._isDone = false;
  7657. }
  7658. applyTo(geom) {
  7659. for (let i = 0; i < geom.getNumGeometries() && !this._isDone; i++) {
  7660. const element = geom.getGeometryN(i);
  7661. if (!(element instanceof GeometryCollection)) {
  7662. this.visit(element);
  7663. if (this.isDone()) {
  7664. this._isDone = true;
  7665. return null;
  7666. }
  7667. } else {
  7668. this.applyTo(element);
  7669. }
  7670. }
  7671. }
  7672. }
  7673. class GeometricShapeFactory {
  7674. constructor() {
  7675. GeometricShapeFactory.constructor_.apply(this, arguments);
  7676. }
  7677. static constructor_() {
  7678. this._geomFact = null;
  7679. this._precModel = null;
  7680. this._dim = new Dimensions();
  7681. this._nPts = 100;
  7682. this._rotationAngle = 0.0;
  7683. if (arguments.length === 0) {
  7684. GeometricShapeFactory.constructor_.call(this, new GeometryFactory());
  7685. } else if (arguments.length === 1) {
  7686. const geomFact = arguments[0];
  7687. this._geomFact = geomFact;
  7688. this._precModel = geomFact.getPrecisionModel();
  7689. }
  7690. }
  7691. createSupercircle(power) {
  7692. const recipPow = 1.0 / power;
  7693. const radius = this._dim.getMinSize() / 2;
  7694. const centre = this._dim.getCentre();
  7695. const r4 = Math.pow(radius, power);
  7696. const y0 = radius;
  7697. const xyInt = Math.pow(r4 / 2, recipPow);
  7698. const nSegsInOct = Math.trunc(this._nPts / 8);
  7699. const totPts = nSegsInOct * 8 + 1;
  7700. const pts = new Array(totPts).fill(null);
  7701. const xInc = xyInt / nSegsInOct;
  7702. for (let i = 0; i <= nSegsInOct; i++) {
  7703. let x = 0.0;
  7704. let y = y0;
  7705. if (i !== 0) {
  7706. x = xInc * i;
  7707. const x4 = Math.pow(x, power);
  7708. y = Math.pow(r4 - x4, recipPow);
  7709. }
  7710. pts[i] = this.coordTrans(x, y, centre);
  7711. pts[2 * nSegsInOct - i] = this.coordTrans(y, x, centre);
  7712. pts[2 * nSegsInOct + i] = this.coordTrans(y, -x, centre);
  7713. pts[4 * nSegsInOct - i] = this.coordTrans(x, -y, centre);
  7714. pts[4 * nSegsInOct + i] = this.coordTrans(-x, -y, centre);
  7715. pts[6 * nSegsInOct - i] = this.coordTrans(-y, -x, centre);
  7716. pts[6 * nSegsInOct + i] = this.coordTrans(-y, x, centre);
  7717. pts[8 * nSegsInOct - i] = this.coordTrans(-x, y, centre);
  7718. }
  7719. pts[pts.length - 1] = new Coordinate(pts[0]);
  7720. const ring = this._geomFact.createLinearRing(pts);
  7721. const poly = this._geomFact.createPolygon(ring);
  7722. return this.rotate(poly);
  7723. }
  7724. setNumPoints(nPts) {
  7725. this._nPts = nPts;
  7726. }
  7727. setBase(base) {
  7728. this._dim.setBase(base);
  7729. }
  7730. setRotation(radians) {
  7731. this._rotationAngle = radians;
  7732. }
  7733. setWidth(width) {
  7734. this._dim.setWidth(width);
  7735. }
  7736. createEllipse() {
  7737. const env = this._dim.getEnvelope();
  7738. const xRadius = env.getWidth() / 2.0;
  7739. const yRadius = env.getHeight() / 2.0;
  7740. const centreX = env.getMinX() + xRadius;
  7741. const centreY = env.getMinY() + yRadius;
  7742. const pts = new Array(this._nPts + 1).fill(null);
  7743. let iPt = 0;
  7744. for (let i = 0; i < this._nPts; i++) {
  7745. const ang = i * (2 * Math.PI / this._nPts);
  7746. const x = xRadius * Math.cos(ang) + centreX;
  7747. const y = yRadius * Math.sin(ang) + centreY;
  7748. pts[iPt++] = this.coord(x, y);
  7749. }
  7750. pts[iPt] = new Coordinate(pts[0]);
  7751. const ring = this._geomFact.createLinearRing(pts);
  7752. const poly = this._geomFact.createPolygon(ring);
  7753. return this.rotate(poly);
  7754. }
  7755. coordTrans(x, y, trans) {
  7756. return this.coord(x + trans.x, y + trans.y);
  7757. }
  7758. createSquircle() {
  7759. return this.createSupercircle(4);
  7760. }
  7761. setEnvelope(env) {
  7762. this._dim.setEnvelope(env);
  7763. }
  7764. setCentre(centre) {
  7765. this._dim.setCentre(centre);
  7766. }
  7767. createArc(startAng, angExtent) {
  7768. const env = this._dim.getEnvelope();
  7769. const xRadius = env.getWidth() / 2.0;
  7770. const yRadius = env.getHeight() / 2.0;
  7771. const centreX = env.getMinX() + xRadius;
  7772. const centreY = env.getMinY() + yRadius;
  7773. let angSize = angExtent;
  7774. if (angSize <= 0.0 || angSize > 2 * Math.PI) angSize = 2 * Math.PI;
  7775. const angInc = angSize / (this._nPts - 1);
  7776. const pts = new Array(this._nPts).fill(null);
  7777. let iPt = 0;
  7778. for (let i = 0; i < this._nPts; i++) {
  7779. const ang = startAng + i * angInc;
  7780. const x = xRadius * Math.cos(ang) + centreX;
  7781. const y = yRadius * Math.sin(ang) + centreY;
  7782. pts[iPt++] = this.coord(x, y);
  7783. }
  7784. const line = this._geomFact.createLineString(pts);
  7785. return this.rotate(line);
  7786. }
  7787. rotate(geom) {
  7788. if (this._rotationAngle !== 0.0) {
  7789. const trans = AffineTransformation.rotationInstance(this._rotationAngle, this._dim.getCentre().x, this._dim.getCentre().y);
  7790. geom.apply(trans);
  7791. }
  7792. return geom;
  7793. }
  7794. coord(x, y) {
  7795. const pt = new Coordinate(x, y);
  7796. this._precModel.makePrecise(pt);
  7797. return pt;
  7798. }
  7799. createArcPolygon(startAng, angExtent) {
  7800. const env = this._dim.getEnvelope();
  7801. const xRadius = env.getWidth() / 2.0;
  7802. const yRadius = env.getHeight() / 2.0;
  7803. const centreX = env.getMinX() + xRadius;
  7804. const centreY = env.getMinY() + yRadius;
  7805. let angSize = angExtent;
  7806. if (angSize <= 0.0 || angSize > 2 * Math.PI) angSize = 2 * Math.PI;
  7807. const angInc = angSize / (this._nPts - 1);
  7808. const pts = new Array(this._nPts + 2).fill(null);
  7809. let iPt = 0;
  7810. pts[iPt++] = this.coord(centreX, centreY);
  7811. for (let i = 0; i < this._nPts; i++) {
  7812. const ang = startAng + angInc * i;
  7813. const x = xRadius * Math.cos(ang) + centreX;
  7814. const y = yRadius * Math.sin(ang) + centreY;
  7815. pts[iPt++] = this.coord(x, y);
  7816. }
  7817. pts[iPt++] = this.coord(centreX, centreY);
  7818. const ring = this._geomFact.createLinearRing(pts);
  7819. const poly = this._geomFact.createPolygon(ring);
  7820. return this.rotate(poly);
  7821. }
  7822. createRectangle() {
  7823. let i = null;
  7824. let ipt = 0;
  7825. let nSide = Math.trunc(this._nPts / 4);
  7826. if (nSide < 1) nSide = 1;
  7827. const XsegLen = this._dim.getEnvelope().getWidth() / nSide;
  7828. const YsegLen = this._dim.getEnvelope().getHeight() / nSide;
  7829. const pts = new Array(4 * nSide + 1).fill(null);
  7830. const env = this._dim.getEnvelope();
  7831. for (i = 0; i < nSide; i++) {
  7832. const x = env.getMinX() + i * XsegLen;
  7833. const y = env.getMinY();
  7834. pts[ipt++] = this.coord(x, y);
  7835. }
  7836. for (i = 0; i < nSide; i++) {
  7837. const x = env.getMaxX();
  7838. const y = env.getMinY() + i * YsegLen;
  7839. pts[ipt++] = this.coord(x, y);
  7840. }
  7841. for (i = 0; i < nSide; i++) {
  7842. const x = env.getMaxX() - i * XsegLen;
  7843. const y = env.getMaxY();
  7844. pts[ipt++] = this.coord(x, y);
  7845. }
  7846. for (i = 0; i < nSide; i++) {
  7847. const x = env.getMinX();
  7848. const y = env.getMaxY() - i * YsegLen;
  7849. pts[ipt++] = this.coord(x, y);
  7850. }
  7851. pts[ipt++] = new Coordinate(pts[0]);
  7852. const ring = this._geomFact.createLinearRing(pts);
  7853. const poly = this._geomFact.createPolygon(ring);
  7854. return this.rotate(poly);
  7855. }
  7856. createCircle() {
  7857. return this.createEllipse();
  7858. }
  7859. setHeight(height) {
  7860. this._dim.setHeight(height);
  7861. }
  7862. setSize(size) {
  7863. this._dim.setSize(size);
  7864. }
  7865. }
  7866. class Dimensions {
  7867. constructor() {
  7868. Dimensions.constructor_.apply(this, arguments);
  7869. }
  7870. static constructor_() {
  7871. this.base = null;
  7872. this.centre = null;
  7873. this.width = null;
  7874. this.height = null;
  7875. }
  7876. setBase(base) {
  7877. this.base = base;
  7878. }
  7879. setWidth(width) {
  7880. this.width = width;
  7881. }
  7882. getBase() {
  7883. return this.base;
  7884. }
  7885. getWidth() {
  7886. return this.width;
  7887. }
  7888. setEnvelope(env) {
  7889. this.width = env.getWidth();
  7890. this.height = env.getHeight();
  7891. this.base = new Coordinate(env.getMinX(), env.getMinY());
  7892. this.centre = new Coordinate(env.centre());
  7893. }
  7894. setCentre(centre) {
  7895. this.centre = centre;
  7896. }
  7897. getMinSize() {
  7898. return Math.min(this.width, this.height);
  7899. }
  7900. getEnvelope() {
  7901. if (this.base !== null) return new Envelope(this.base.x, this.base.x + this.width, this.base.y, this.base.y + this.height);
  7902. if (this.centre !== null) return new Envelope(this.centre.x - this.width / 2, this.centre.x + this.width / 2, this.centre.y - this.height / 2, this.centre.y + this.height / 2);
  7903. return new Envelope(0, this.width, 0, this.height);
  7904. }
  7905. getCentre() {
  7906. if (this.centre === null) this.centre = new Coordinate(this.base.x + this.width / 2, this.base.y + this.height / 2);
  7907. return this.centre;
  7908. }
  7909. getHeight() {
  7910. return this.height;
  7911. }
  7912. setHeight(height) {
  7913. this.height = height;
  7914. }
  7915. setSize(size) {
  7916. this.height = size;
  7917. this.width = size;
  7918. }
  7919. }
  7920. GeometricShapeFactory.Dimensions = Dimensions;
  7921. class SineStarFactory extends GeometricShapeFactory {
  7922. constructor() {
  7923. super();
  7924. SineStarFactory.constructor_.apply(this, arguments);
  7925. }
  7926. static constructor_() {
  7927. this._numArms = 8;
  7928. this._armLengthRatio = 0.5;
  7929. if (arguments.length === 0) {
  7930. GeometricShapeFactory.constructor_.call(this);
  7931. } else if (arguments.length === 1) {
  7932. const geomFact = arguments[0];
  7933. GeometricShapeFactory.constructor_.call(this, geomFact);
  7934. }
  7935. }
  7936. static create(origin, size, nPts, nArms, armLengthRatio) {
  7937. const gsf = new SineStarFactory();
  7938. gsf.setCentre(origin);
  7939. gsf.setSize(size);
  7940. gsf.setNumPoints(nPts);
  7941. gsf.setArmLengthRatio(armLengthRatio);
  7942. gsf.setNumArms(nArms);
  7943. const poly = gsf.createSineStar();
  7944. return poly;
  7945. }
  7946. setNumArms(numArms) {
  7947. this._numArms = numArms;
  7948. }
  7949. setArmLengthRatio(armLengthRatio) {
  7950. this._armLengthRatio = armLengthRatio;
  7951. }
  7952. createSineStar() {
  7953. const env = this._dim.getEnvelope();
  7954. const radius = env.getWidth() / 2.0;
  7955. let armRatio = this._armLengthRatio;
  7956. if (armRatio < 0.0) armRatio = 0.0;
  7957. if (armRatio > 1.0) armRatio = 1.0;
  7958. const armMaxLen = armRatio * radius;
  7959. const insideRadius = (1 - armRatio) * radius;
  7960. const centreX = env.getMinX() + radius;
  7961. const centreY = env.getMinY() + radius;
  7962. const pts = new Array(this._nPts + 1).fill(null);
  7963. let iPt = 0;
  7964. for (let i = 0; i < this._nPts; i++) {
  7965. const ptArcFrac = i / this._nPts * this._numArms;
  7966. const armAngFrac = ptArcFrac - Math.floor(ptArcFrac);
  7967. const armAng = 2 * Math.PI * armAngFrac;
  7968. const armLenFrac = (Math.cos(armAng) + 1.0) / 2.0;
  7969. const curveRadius = insideRadius + armMaxLen * armLenFrac;
  7970. const ang = i * (2 * Math.PI / this._nPts);
  7971. const x = curveRadius * Math.cos(ang) + centreX;
  7972. const y = curveRadius * Math.sin(ang) + centreY;
  7973. pts[iPt++] = this.coord(x, y);
  7974. }
  7975. pts[iPt] = new Coordinate(pts[0]);
  7976. const ring = this._geomFact.createLinearRing(pts);
  7977. const poly = this._geomFact.createPolygon(ring);
  7978. return poly;
  7979. }
  7980. }
  7981. var util$1 = /*#__PURE__*/Object.freeze({
  7982. __proto__: null,
  7983. AffineTransformation: AffineTransformation,
  7984. AffineTransformationBuilder: AffineTransformationBuilder,
  7985. AffineTransformationFactory: AffineTransformationFactory,
  7986. ComponentCoordinateExtracter: ComponentCoordinateExtracter,
  7987. GeometryCollectionMapper: GeometryCollectionMapper,
  7988. GeometryCombiner: GeometryCombiner,
  7989. GeometryEditor: GeometryEditor,
  7990. GeometryExtracter: GeometryExtracter,
  7991. GeometryMapper: GeometryMapper,
  7992. GeometryTransformer: GeometryTransformer,
  7993. LineStringExtracter: LineStringExtracter,
  7994. LinearComponentExtracter: LinearComponentExtracter,
  7995. PointExtracter: PointExtracter,
  7996. PolygonExtracter: PolygonExtracter,
  7997. ShortCircuitedGeometryVisitor: ShortCircuitedGeometryVisitor,
  7998. SineStarFactory: SineStarFactory
  7999. });
  8000. var geom = /*#__PURE__*/Object.freeze({
  8001. __proto__: null,
  8002. Coordinate: Coordinate,
  8003. CoordinateXY: CoordinateXY,
  8004. CoordinateXYM: CoordinateXYM,
  8005. CoordinateXYZM: CoordinateXYZM,
  8006. CoordinateList: CoordinateList,
  8007. CoordinateSequenceFilter: CoordinateSequenceFilter,
  8008. Envelope: Envelope,
  8009. LineSegment: LineSegment,
  8010. GeometryFactory: GeometryFactory,
  8011. Geometry: Geometry,
  8012. Point: Point,
  8013. LineString: LineString,
  8014. LinearRing: LinearRing,
  8015. Polygon: Polygon,
  8016. GeometryCollection: GeometryCollection,
  8017. MultiPoint: MultiPoint,
  8018. MultiLineString: MultiLineString,
  8019. MultiPolygon: MultiPolygon,
  8020. Dimension: Dimension,
  8021. IntersectionMatrix: IntersectionMatrix,
  8022. PrecisionModel: PrecisionModel,
  8023. Location: Location,
  8024. Triangle: Triangle,
  8025. util: util$1
  8026. });
  8027. class PointPairDistance {
  8028. constructor() {
  8029. PointPairDistance.constructor_.apply(this, arguments);
  8030. }
  8031. static constructor_() {
  8032. this._pt = [new Coordinate(), new Coordinate()];
  8033. this._distance = Double.NaN;
  8034. this._isNull = true;
  8035. }
  8036. getCoordinates() {
  8037. return this._pt;
  8038. }
  8039. getCoordinate(i) {
  8040. return this._pt[i];
  8041. }
  8042. setMinimum() {
  8043. if (arguments.length === 1) {
  8044. const ptDist = arguments[0];
  8045. this.setMinimum(ptDist._pt[0], ptDist._pt[1]);
  8046. } else if (arguments.length === 2) {
  8047. const p0 = arguments[0],
  8048. p1 = arguments[1];
  8049. if (this._isNull) {
  8050. this.initialize(p0, p1);
  8051. return null;
  8052. }
  8053. const dist = p0.distance(p1);
  8054. if (dist < this._distance) this.initialize(p0, p1, dist);
  8055. }
  8056. }
  8057. initialize() {
  8058. if (arguments.length === 0) {
  8059. this._isNull = true;
  8060. } else if (arguments.length === 2) {
  8061. const p0 = arguments[0],
  8062. p1 = arguments[1];
  8063. this._pt[0].setCoordinate(p0);
  8064. this._pt[1].setCoordinate(p1);
  8065. this._distance = p0.distance(p1);
  8066. this._isNull = false;
  8067. } else if (arguments.length === 3) {
  8068. const p0 = arguments[0],
  8069. p1 = arguments[1],
  8070. distance = arguments[2];
  8071. this._pt[0].setCoordinate(p0);
  8072. this._pt[1].setCoordinate(p1);
  8073. this._distance = distance;
  8074. this._isNull = false;
  8075. }
  8076. }
  8077. toString() {
  8078. return WKTWriter.toLineString(this._pt[0], this._pt[1]);
  8079. }
  8080. getDistance() {
  8081. return this._distance;
  8082. }
  8083. setMaximum() {
  8084. if (arguments.length === 1) {
  8085. const ptDist = arguments[0];
  8086. this.setMaximum(ptDist._pt[0], ptDist._pt[1]);
  8087. } else if (arguments.length === 2) {
  8088. const p0 = arguments[0],
  8089. p1 = arguments[1];
  8090. if (this._isNull) {
  8091. this.initialize(p0, p1);
  8092. return null;
  8093. }
  8094. const dist = p0.distance(p1);
  8095. if (dist > this._distance) this.initialize(p0, p1, dist);
  8096. }
  8097. }
  8098. }
  8099. class DistanceToPoint {
  8100. static computeDistance() {
  8101. if (arguments[2] instanceof PointPairDistance && arguments[0] instanceof LineString && arguments[1] instanceof Coordinate) {
  8102. const line = arguments[0],
  8103. pt = arguments[1],
  8104. ptDist = arguments[2];
  8105. const tempSegment = new LineSegment();
  8106. const coords = line.getCoordinates();
  8107. for (let i = 0; i < coords.length - 1; i++) {
  8108. tempSegment.setCoordinates(coords[i], coords[i + 1]);
  8109. const closestPt = tempSegment.closestPoint(pt);
  8110. ptDist.setMinimum(closestPt, pt);
  8111. }
  8112. } else if (arguments[2] instanceof PointPairDistance && arguments[0] instanceof Polygon && arguments[1] instanceof Coordinate) {
  8113. const poly = arguments[0],
  8114. pt = arguments[1],
  8115. ptDist = arguments[2];
  8116. DistanceToPoint.computeDistance(poly.getExteriorRing(), pt, ptDist);
  8117. for (let i = 0; i < poly.getNumInteriorRing(); i++) DistanceToPoint.computeDistance(poly.getInteriorRingN(i), pt, ptDist);
  8118. } else if (arguments[2] instanceof PointPairDistance && arguments[0] instanceof Geometry && arguments[1] instanceof Coordinate) {
  8119. const geom = arguments[0],
  8120. pt = arguments[1],
  8121. ptDist = arguments[2];
  8122. if (geom instanceof LineString) {
  8123. DistanceToPoint.computeDistance(geom, pt, ptDist);
  8124. } else if (geom instanceof Polygon) {
  8125. DistanceToPoint.computeDistance(geom, pt, ptDist);
  8126. } else if (geom instanceof GeometryCollection) {
  8127. const gc = geom;
  8128. for (let i = 0; i < gc.getNumGeometries(); i++) {
  8129. const g = gc.getGeometryN(i);
  8130. DistanceToPoint.computeDistance(g, pt, ptDist);
  8131. }
  8132. } else {
  8133. ptDist.setMinimum(geom.getCoordinate(), pt);
  8134. }
  8135. } else if (arguments[2] instanceof PointPairDistance && arguments[0] instanceof LineSegment && arguments[1] instanceof Coordinate) {
  8136. const segment = arguments[0],
  8137. pt = arguments[1],
  8138. ptDist = arguments[2];
  8139. const closestPt = segment.closestPoint(pt);
  8140. ptDist.setMinimum(closestPt, pt);
  8141. }
  8142. }
  8143. }
  8144. class DiscreteHausdorffDistance {
  8145. constructor() {
  8146. DiscreteHausdorffDistance.constructor_.apply(this, arguments);
  8147. }
  8148. static constructor_() {
  8149. this._g0 = null;
  8150. this._g1 = null;
  8151. this._ptDist = new PointPairDistance();
  8152. this._densifyFrac = 0.0;
  8153. const g0 = arguments[0],
  8154. g1 = arguments[1];
  8155. this._g0 = g0;
  8156. this._g1 = g1;
  8157. }
  8158. static distance() {
  8159. if (arguments.length === 2) {
  8160. const g0 = arguments[0],
  8161. g1 = arguments[1];
  8162. const dist = new DiscreteHausdorffDistance(g0, g1);
  8163. return dist.distance();
  8164. } else if (arguments.length === 3) {
  8165. const g0 = arguments[0],
  8166. g1 = arguments[1],
  8167. densifyFrac = arguments[2];
  8168. const dist = new DiscreteHausdorffDistance(g0, g1);
  8169. dist.setDensifyFraction(densifyFrac);
  8170. return dist.distance();
  8171. }
  8172. }
  8173. getCoordinates() {
  8174. return this._ptDist.getCoordinates();
  8175. }
  8176. setDensifyFraction(densifyFrac) {
  8177. if (densifyFrac > 1.0 || densifyFrac <= 0.0) throw new IllegalArgumentException('Fraction is not in range (0.0 - 1.0]');
  8178. this._densifyFrac = densifyFrac;
  8179. }
  8180. compute(g0, g1) {
  8181. this.computeOrientedDistance(g0, g1, this._ptDist);
  8182. this.computeOrientedDistance(g1, g0, this._ptDist);
  8183. }
  8184. distance() {
  8185. this.compute(this._g0, this._g1);
  8186. return this._ptDist.getDistance();
  8187. }
  8188. computeOrientedDistance(discreteGeom, geom, ptDist) {
  8189. const distFilter = new MaxPointDistanceFilter(geom);
  8190. discreteGeom.apply(distFilter);
  8191. ptDist.setMaximum(distFilter.getMaxPointDistance());
  8192. if (this._densifyFrac > 0) {
  8193. const fracFilter = new MaxDensifiedByFractionDistanceFilter(geom, this._densifyFrac);
  8194. discreteGeom.apply(fracFilter);
  8195. ptDist.setMaximum(fracFilter.getMaxPointDistance());
  8196. }
  8197. }
  8198. orientedDistance() {
  8199. this.computeOrientedDistance(this._g0, this._g1, this._ptDist);
  8200. return this._ptDist.getDistance();
  8201. }
  8202. }
  8203. class MaxPointDistanceFilter {
  8204. constructor() {
  8205. MaxPointDistanceFilter.constructor_.apply(this, arguments);
  8206. }
  8207. static constructor_() {
  8208. this._maxPtDist = new PointPairDistance();
  8209. this._minPtDist = new PointPairDistance();
  8210. this._euclideanDist = new DistanceToPoint();
  8211. this._geom = null;
  8212. const geom = arguments[0];
  8213. this._geom = geom;
  8214. }
  8215. filter(pt) {
  8216. this._minPtDist.initialize();
  8217. DistanceToPoint.computeDistance(this._geom, pt, this._minPtDist);
  8218. this._maxPtDist.setMaximum(this._minPtDist);
  8219. }
  8220. getMaxPointDistance() {
  8221. return this._maxPtDist;
  8222. }
  8223. get interfaces_() {
  8224. return [CoordinateFilter];
  8225. }
  8226. }
  8227. class MaxDensifiedByFractionDistanceFilter {
  8228. constructor() {
  8229. MaxDensifiedByFractionDistanceFilter.constructor_.apply(this, arguments);
  8230. }
  8231. static constructor_() {
  8232. this._maxPtDist = new PointPairDistance();
  8233. this._minPtDist = new PointPairDistance();
  8234. this._geom = null;
  8235. this._numSubSegs = 0;
  8236. const geom = arguments[0],
  8237. fraction = arguments[1];
  8238. this._geom = geom;
  8239. this._numSubSegs = Math.trunc(Math.round(1.0 / fraction));
  8240. }
  8241. filter(seq, index) {
  8242. if (index === 0) return null;
  8243. const p0 = seq.getCoordinate(index - 1);
  8244. const p1 = seq.getCoordinate(index);
  8245. const delx = (p1.x - p0.x) / this._numSubSegs;
  8246. const dely = (p1.y - p0.y) / this._numSubSegs;
  8247. for (let i = 0; i < this._numSubSegs; i++) {
  8248. const x = p0.x + i * delx;
  8249. const y = p0.y + i * dely;
  8250. const pt = new Coordinate(x, y);
  8251. this._minPtDist.initialize();
  8252. DistanceToPoint.computeDistance(this._geom, pt, this._minPtDist);
  8253. this._maxPtDist.setMaximum(this._minPtDist);
  8254. }
  8255. }
  8256. isDone() {
  8257. return false;
  8258. }
  8259. isGeometryChanged() {
  8260. return false;
  8261. }
  8262. getMaxPointDistance() {
  8263. return this._maxPtDist;
  8264. }
  8265. get interfaces_() {
  8266. return [CoordinateSequenceFilter];
  8267. }
  8268. }
  8269. DiscreteHausdorffDistance.MaxPointDistanceFilter = MaxPointDistanceFilter;
  8270. DiscreteHausdorffDistance.MaxDensifiedByFractionDistanceFilter = MaxDensifiedByFractionDistanceFilter;
  8271. var distance_module = /*#__PURE__*/Object.freeze({
  8272. __proto__: null,
  8273. DiscreteHausdorffDistance: DiscreteHausdorffDistance,
  8274. DistanceToPoint: DistanceToPoint,
  8275. PointPairDistance: PointPairDistance
  8276. });
  8277. class ItemVisitor {
  8278. visitItem(item) {}
  8279. }
  8280. class PointOnGeometryLocator {
  8281. locate(p) {}
  8282. }
  8283. class IntervalRTreeNode {
  8284. constructor() {
  8285. IntervalRTreeNode.constructor_.apply(this, arguments);
  8286. }
  8287. static constructor_() {
  8288. this._min = Double.POSITIVE_INFINITY;
  8289. this._max = Double.NEGATIVE_INFINITY;
  8290. }
  8291. getMin() {
  8292. return this._min;
  8293. }
  8294. intersects(queryMin, queryMax) {
  8295. if (this._min > queryMax || this._max < queryMin) return false;
  8296. return true;
  8297. }
  8298. getMax() {
  8299. return this._max;
  8300. }
  8301. toString() {
  8302. return WKTWriter.toLineString(new Coordinate(this._min, 0), new Coordinate(this._max, 0));
  8303. }
  8304. }
  8305. class NodeComparator {
  8306. compare(o1, o2) {
  8307. const n1 = o1;
  8308. const n2 = o2;
  8309. const mid1 = (n1._min + n1._max) / 2;
  8310. const mid2 = (n2._min + n2._max) / 2;
  8311. if (mid1 < mid2) return -1;
  8312. if (mid1 > mid2) return 1;
  8313. return 0;
  8314. }
  8315. get interfaces_() {
  8316. return [Comparator];
  8317. }
  8318. }
  8319. IntervalRTreeNode.NodeComparator = NodeComparator;
  8320. class IntervalRTreeLeafNode extends IntervalRTreeNode {
  8321. constructor() {
  8322. super();
  8323. IntervalRTreeLeafNode.constructor_.apply(this, arguments);
  8324. }
  8325. static constructor_() {
  8326. this._item = null;
  8327. const min = arguments[0],
  8328. max = arguments[1],
  8329. item = arguments[2];
  8330. this._min = min;
  8331. this._max = max;
  8332. this._item = item;
  8333. }
  8334. query(queryMin, queryMax, visitor) {
  8335. if (!this.intersects(queryMin, queryMax)) return null;
  8336. visitor.visitItem(this._item);
  8337. }
  8338. }
  8339. class IntervalRTreeBranchNode extends IntervalRTreeNode {
  8340. constructor() {
  8341. super();
  8342. IntervalRTreeBranchNode.constructor_.apply(this, arguments);
  8343. }
  8344. static constructor_() {
  8345. this._node1 = null;
  8346. this._node2 = null;
  8347. const n1 = arguments[0],
  8348. n2 = arguments[1];
  8349. this._node1 = n1;
  8350. this._node2 = n2;
  8351. this.buildExtent(this._node1, this._node2);
  8352. }
  8353. buildExtent(n1, n2) {
  8354. this._min = Math.min(n1._min, n2._min);
  8355. this._max = Math.max(n1._max, n2._max);
  8356. }
  8357. query(queryMin, queryMax, visitor) {
  8358. if (!this.intersects(queryMin, queryMax)) return null;
  8359. if (this._node1 !== null) this._node1.query(queryMin, queryMax, visitor);
  8360. if (this._node2 !== null) this._node2.query(queryMin, queryMax, visitor);
  8361. }
  8362. }
  8363. class SortedPackedIntervalRTree {
  8364. constructor() {
  8365. SortedPackedIntervalRTree.constructor_.apply(this, arguments);
  8366. }
  8367. static constructor_() {
  8368. this._leaves = new ArrayList();
  8369. this._root = null;
  8370. this._level = 0;
  8371. }
  8372. buildTree() {
  8373. Collections.sort(this._leaves, new IntervalRTreeNode.NodeComparator());
  8374. let src = this._leaves;
  8375. let temp = null;
  8376. let dest = new ArrayList();
  8377. while (true) {
  8378. this.buildLevel(src, dest);
  8379. if (dest.size() === 1) return dest.get(0);
  8380. temp = src;
  8381. src = dest;
  8382. dest = temp;
  8383. }
  8384. }
  8385. insert(min, max, item) {
  8386. if (this._root !== null) throw new IllegalStateException('Index cannot be added to once it has been queried');
  8387. this._leaves.add(new IntervalRTreeLeafNode(min, max, item));
  8388. }
  8389. query(min, max, visitor) {
  8390. this.init();
  8391. if (this._root === null) return null;
  8392. this._root.query(min, max, visitor);
  8393. }
  8394. buildRoot() {
  8395. if (this._root !== null) return null;
  8396. this._root = this.buildTree();
  8397. }
  8398. printNode(node) {
  8399. System.out.println(WKTWriter.toLineString(new Coordinate(node._min, this._level), new Coordinate(node._max, this._level)));
  8400. }
  8401. init() {
  8402. if (this._root !== null) return null;
  8403. if (this._leaves.size() === 0) return null;
  8404. this.buildRoot();
  8405. }
  8406. buildLevel(src, dest) {
  8407. this._level++;
  8408. dest.clear();
  8409. for (let i = 0; i < src.size(); i += 2) {
  8410. const n1 = src.get(i);
  8411. const n2 = i + 1 < src.size() ? src.get(i) : null;
  8412. if (n2 === null) {
  8413. dest.add(n1);
  8414. } else {
  8415. const node = new IntervalRTreeBranchNode(src.get(i), src.get(i + 1));
  8416. dest.add(node);
  8417. }
  8418. }
  8419. }
  8420. }
  8421. class ArrayListVisitor {
  8422. constructor() {
  8423. ArrayListVisitor.constructor_.apply(this, arguments);
  8424. }
  8425. static constructor_() {
  8426. this._items = new ArrayList();
  8427. }
  8428. visitItem(item) {
  8429. this._items.add(item);
  8430. }
  8431. getItems() {
  8432. return this._items;
  8433. }
  8434. get interfaces_() {
  8435. return [ItemVisitor];
  8436. }
  8437. }
  8438. class RayCrossingCounter {
  8439. constructor() {
  8440. RayCrossingCounter.constructor_.apply(this, arguments);
  8441. }
  8442. static constructor_() {
  8443. this._p = null;
  8444. this._crossingCount = 0;
  8445. this._isPointOnSegment = false;
  8446. const p = arguments[0];
  8447. this._p = p;
  8448. }
  8449. static locatePointInRing() {
  8450. if (arguments[0] instanceof Coordinate && hasInterface(arguments[1], CoordinateSequence)) {
  8451. const p = arguments[0],
  8452. ring = arguments[1];
  8453. const counter = new RayCrossingCounter(p);
  8454. const p1 = new Coordinate();
  8455. const p2 = new Coordinate();
  8456. for (let i = 1; i < ring.size(); i++) {
  8457. ring.getCoordinate(i, p1);
  8458. ring.getCoordinate(i - 1, p2);
  8459. counter.countSegment(p1, p2);
  8460. if (counter.isOnSegment()) return counter.getLocation();
  8461. }
  8462. return counter.getLocation();
  8463. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Array) {
  8464. const p = arguments[0],
  8465. ring = arguments[1];
  8466. const counter = new RayCrossingCounter(p);
  8467. for (let i = 1; i < ring.length; i++) {
  8468. const p1 = ring[i];
  8469. const p2 = ring[i - 1];
  8470. counter.countSegment(p1, p2);
  8471. if (counter.isOnSegment()) return counter.getLocation();
  8472. }
  8473. return counter.getLocation();
  8474. }
  8475. }
  8476. countSegment(p1, p2) {
  8477. if (p1.x < this._p.x && p2.x < this._p.x) return null;
  8478. if (this._p.x === p2.x && this._p.y === p2.y) {
  8479. this._isPointOnSegment = true;
  8480. return null;
  8481. }
  8482. if (p1.y === this._p.y && p2.y === this._p.y) {
  8483. let minx = p1.x;
  8484. let maxx = p2.x;
  8485. if (minx > maxx) {
  8486. minx = p2.x;
  8487. maxx = p1.x;
  8488. }
  8489. if (this._p.x >= minx && this._p.x <= maxx) this._isPointOnSegment = true;
  8490. return null;
  8491. }
  8492. if (p1.y > this._p.y && p2.y <= this._p.y || p2.y > this._p.y && p1.y <= this._p.y) {
  8493. let orient = Orientation.index(p1, p2, this._p);
  8494. if (orient === Orientation.COLLINEAR) {
  8495. this._isPointOnSegment = true;
  8496. return null;
  8497. }
  8498. if (p2.y < p1.y) orient = -orient;
  8499. if (orient === Orientation.LEFT) this._crossingCount++;
  8500. }
  8501. }
  8502. isPointInPolygon() {
  8503. return this.getLocation() !== Location.EXTERIOR;
  8504. }
  8505. getLocation() {
  8506. if (this._isPointOnSegment) return Location.BOUNDARY;
  8507. if (this._crossingCount % 2 === 1) return Location.INTERIOR;
  8508. return Location.EXTERIOR;
  8509. }
  8510. isOnSegment() {
  8511. return this._isPointOnSegment;
  8512. }
  8513. }
  8514. class IndexedPointInAreaLocator {
  8515. constructor() {
  8516. IndexedPointInAreaLocator.constructor_.apply(this, arguments);
  8517. }
  8518. static constructor_() {
  8519. this._geom = null;
  8520. this._index = null;
  8521. const g = arguments[0];
  8522. if (!(hasInterface(g, Polygonal) || g instanceof LinearRing)) throw new IllegalArgumentException('Argument must be Polygonal or LinearRing');
  8523. this._geom = g;
  8524. }
  8525. locate(p) {
  8526. if (this._index === null) {
  8527. this._index = new IntervalIndexedGeometry(this._geom);
  8528. this._geom = null;
  8529. }
  8530. const rcc = new RayCrossingCounter(p);
  8531. const visitor = new SegmentVisitor(rcc);
  8532. this._index.query(p.y, p.y, visitor);
  8533. return rcc.getLocation();
  8534. }
  8535. get interfaces_() {
  8536. return [PointOnGeometryLocator];
  8537. }
  8538. }
  8539. class SegmentVisitor {
  8540. constructor() {
  8541. SegmentVisitor.constructor_.apply(this, arguments);
  8542. }
  8543. static constructor_() {
  8544. this._counter = null;
  8545. const counter = arguments[0];
  8546. this._counter = counter;
  8547. }
  8548. visitItem(item) {
  8549. const seg = item;
  8550. this._counter.countSegment(seg.getCoordinate(0), seg.getCoordinate(1));
  8551. }
  8552. get interfaces_() {
  8553. return [ItemVisitor];
  8554. }
  8555. }
  8556. class IntervalIndexedGeometry {
  8557. constructor() {
  8558. IntervalIndexedGeometry.constructor_.apply(this, arguments);
  8559. }
  8560. static constructor_() {
  8561. this._isEmpty = false;
  8562. this._index = new SortedPackedIntervalRTree();
  8563. const geom = arguments[0];
  8564. if (geom.isEmpty()) this._isEmpty = true;else this.init(geom);
  8565. }
  8566. init(geom) {
  8567. const lines = LinearComponentExtracter.getLines(geom);
  8568. for (let i = lines.iterator(); i.hasNext();) {
  8569. const line = i.next();
  8570. const pts = line.getCoordinates();
  8571. this.addLine(pts);
  8572. }
  8573. }
  8574. addLine(pts) {
  8575. for (let i = 1; i < pts.length; i++) {
  8576. const seg = new LineSegment(pts[i - 1], pts[i]);
  8577. const min = Math.min(seg.p0.y, seg.p1.y);
  8578. const max = Math.max(seg.p0.y, seg.p1.y);
  8579. this._index.insert(min, max, seg);
  8580. }
  8581. }
  8582. query() {
  8583. if (arguments.length === 2) {
  8584. const min = arguments[0],
  8585. max = arguments[1];
  8586. if (this._isEmpty) return new ArrayList();
  8587. const visitor = new ArrayListVisitor();
  8588. this._index.query(min, max, visitor);
  8589. return visitor.getItems();
  8590. } else if (arguments.length === 3) {
  8591. const min = arguments[0],
  8592. max = arguments[1],
  8593. visitor = arguments[2];
  8594. if (this._isEmpty) return null;
  8595. this._index.query(min, max, visitor);
  8596. }
  8597. }
  8598. }
  8599. IndexedPointInAreaLocator.SegmentVisitor = SegmentVisitor;
  8600. IndexedPointInAreaLocator.IntervalIndexedGeometry = IntervalIndexedGeometry;
  8601. class PointLocation {
  8602. static isOnLine() {
  8603. if (arguments[0] instanceof Coordinate && hasInterface(arguments[1], CoordinateSequence)) {
  8604. const p = arguments[0],
  8605. line = arguments[1];
  8606. const lineIntersector = new RobustLineIntersector();
  8607. const p0 = new Coordinate();
  8608. const p1 = new Coordinate();
  8609. const n = line.size();
  8610. for (let i = 1; i < n; i++) {
  8611. line.getCoordinate(i - 1, p0);
  8612. line.getCoordinate(i, p1);
  8613. lineIntersector.computeIntersection(p, p0, p1);
  8614. if (lineIntersector.hasIntersection()) return true;
  8615. }
  8616. return false;
  8617. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Array) {
  8618. const p = arguments[0],
  8619. line = arguments[1];
  8620. const lineIntersector = new RobustLineIntersector();
  8621. for (let i = 1; i < line.length; i++) {
  8622. const p0 = line[i - 1];
  8623. const p1 = line[i];
  8624. lineIntersector.computeIntersection(p, p0, p1);
  8625. if (lineIntersector.hasIntersection()) return true;
  8626. }
  8627. return false;
  8628. }
  8629. }
  8630. static locateInRing(p, ring) {
  8631. return RayCrossingCounter.locatePointInRing(p, ring);
  8632. }
  8633. static isInRing(p, ring) {
  8634. return PointLocation.locateInRing(p, ring) !== Location.EXTERIOR;
  8635. }
  8636. }
  8637. /**
  8638. * @see http://download.oracle.com/javase/6/docs/api/java/util/Iterator.html
  8639. * @constructor
  8640. * @private
  8641. */
  8642. class Iterator {
  8643. /**
  8644. * Returns true if the iteration has more elements.
  8645. * @return {boolean}
  8646. */
  8647. hasNext() {}
  8648. /**
  8649. * Returns the next element in the iteration.
  8650. * @return {Object}
  8651. */
  8652. next() {}
  8653. /**
  8654. * Removes from the underlying collection the last element returned by the
  8655. * iterator (optional operation).
  8656. */
  8657. remove() {}
  8658. }
  8659. class GeometryCollectionIterator {
  8660. constructor() {
  8661. GeometryCollectionIterator.constructor_.apply(this, arguments);
  8662. }
  8663. static constructor_() {
  8664. this._parent = null;
  8665. this._atStart = null;
  8666. this._max = null;
  8667. this._index = null;
  8668. this._subcollectionIterator = null;
  8669. const parent = arguments[0];
  8670. this._parent = parent;
  8671. this._atStart = true;
  8672. this._index = 0;
  8673. this._max = parent.getNumGeometries();
  8674. }
  8675. static isAtomic(geom) {
  8676. return !(geom instanceof GeometryCollection);
  8677. }
  8678. next() {
  8679. if (this._atStart) {
  8680. this._atStart = false;
  8681. if (GeometryCollectionIterator.isAtomic(this._parent)) this._index++;
  8682. return this._parent;
  8683. }
  8684. if (this._subcollectionIterator !== null) if (this._subcollectionIterator.hasNext()) return this._subcollectionIterator.next();else this._subcollectionIterator = null;
  8685. if (this._index >= this._max) throw new NoSuchElementException();
  8686. const obj = this._parent.getGeometryN(this._index++);
  8687. if (obj instanceof GeometryCollection) {
  8688. this._subcollectionIterator = new GeometryCollectionIterator(obj);
  8689. return this._subcollectionIterator.next();
  8690. }
  8691. return obj;
  8692. }
  8693. remove() {
  8694. throw new UnsupportedOperationException(this.getClass().getName());
  8695. }
  8696. hasNext() {
  8697. if (this._atStart) return true;
  8698. if (this._subcollectionIterator !== null) {
  8699. if (this._subcollectionIterator.hasNext()) return true;
  8700. this._subcollectionIterator = null;
  8701. }
  8702. if (this._index >= this._max) return false;
  8703. return true;
  8704. }
  8705. get interfaces_() {
  8706. return [Iterator];
  8707. }
  8708. }
  8709. class SimplePointInAreaLocator {
  8710. constructor() {
  8711. SimplePointInAreaLocator.constructor_.apply(this, arguments);
  8712. }
  8713. static constructor_() {
  8714. this._geom = null;
  8715. const geom = arguments[0];
  8716. this._geom = geom;
  8717. }
  8718. static locatePointInPolygon(p, poly) {
  8719. if (poly.isEmpty()) return Location.EXTERIOR;
  8720. const shell = poly.getExteriorRing();
  8721. const shellLoc = SimplePointInAreaLocator.locatePointInRing(p, shell);
  8722. if (shellLoc !== Location.INTERIOR) return shellLoc;
  8723. for (let i = 0; i < poly.getNumInteriorRing(); i++) {
  8724. const hole = poly.getInteriorRingN(i);
  8725. const holeLoc = SimplePointInAreaLocator.locatePointInRing(p, hole);
  8726. if (holeLoc === Location.BOUNDARY) return Location.BOUNDARY;
  8727. if (holeLoc === Location.INTERIOR) return Location.EXTERIOR;
  8728. }
  8729. return Location.INTERIOR;
  8730. }
  8731. static locatePointInRing(p, ring) {
  8732. if (!ring.getEnvelopeInternal().intersects(p)) return Location.EXTERIOR;
  8733. return PointLocation.locateInRing(p, ring.getCoordinates());
  8734. }
  8735. static containsPointInPolygon(p, poly) {
  8736. return Location.EXTERIOR !== SimplePointInAreaLocator.locatePointInPolygon(p, poly);
  8737. }
  8738. static locateInGeometry(p, geom) {
  8739. if (geom instanceof Polygon) return SimplePointInAreaLocator.locatePointInPolygon(p, geom);
  8740. if (geom instanceof GeometryCollection) {
  8741. const geomi = new GeometryCollectionIterator(geom);
  8742. while (geomi.hasNext()) {
  8743. const g2 = geomi.next();
  8744. if (g2 !== geom) {
  8745. const loc = SimplePointInAreaLocator.locateInGeometry(p, g2);
  8746. if (loc !== Location.EXTERIOR) return loc;
  8747. }
  8748. }
  8749. }
  8750. return Location.EXTERIOR;
  8751. }
  8752. static isContained(p, geom) {
  8753. return Location.EXTERIOR !== SimplePointInAreaLocator.locate(p, geom);
  8754. }
  8755. static locate(p, geom) {
  8756. if (geom.isEmpty()) return Location.EXTERIOR;
  8757. if (!geom.getEnvelopeInternal().intersects(p)) return Location.EXTERIOR;
  8758. return SimplePointInAreaLocator.locateInGeometry(p, geom);
  8759. }
  8760. locate(p) {
  8761. return SimplePointInAreaLocator.locate(p, this._geom);
  8762. }
  8763. get interfaces_() {
  8764. return [PointOnGeometryLocator];
  8765. }
  8766. }
  8767. var locate = /*#__PURE__*/Object.freeze({
  8768. __proto__: null,
  8769. IndexedPointInAreaLocator: IndexedPointInAreaLocator,
  8770. PointOnGeometryLocator: PointOnGeometryLocator,
  8771. SimplePointInAreaLocator: SimplePointInAreaLocator
  8772. });
  8773. class SimilarityMeasure {
  8774. measure(g1, g2) {}
  8775. }
  8776. class AreaSimilarityMeasure {
  8777. measure(g1, g2) {
  8778. const areaInt = g1.intersection(g2).getArea();
  8779. const areaUnion = g1.union(g2).getArea();
  8780. return areaInt / areaUnion;
  8781. }
  8782. get interfaces_() {
  8783. return [SimilarityMeasure];
  8784. }
  8785. }
  8786. class HausdorffSimilarityMeasure {
  8787. static diagonalSize(env) {
  8788. if (env.isNull()) return 0.0;
  8789. const width = env.getWidth();
  8790. const hgt = env.getHeight();
  8791. return Math.sqrt(width * width + hgt * hgt);
  8792. }
  8793. measure(g1, g2) {
  8794. const distance = DiscreteHausdorffDistance.distance(g1, g2, HausdorffSimilarityMeasure.DENSIFY_FRACTION);
  8795. const env = new Envelope(g1.getEnvelopeInternal());
  8796. env.expandToInclude(g2.getEnvelopeInternal());
  8797. const envSize = HausdorffSimilarityMeasure.diagonalSize(env);
  8798. const measure = 1 - distance / envSize;
  8799. return measure;
  8800. }
  8801. get interfaces_() {
  8802. return [SimilarityMeasure];
  8803. }
  8804. }
  8805. HausdorffSimilarityMeasure.DENSIFY_FRACTION = 0.25;
  8806. class SimilarityMeasureCombiner {
  8807. static combine(measure1, measure2) {
  8808. return Math.min(measure1, measure2);
  8809. }
  8810. }
  8811. var match = /*#__PURE__*/Object.freeze({
  8812. __proto__: null,
  8813. AreaSimilarityMeasure: AreaSimilarityMeasure,
  8814. HausdorffSimilarityMeasure: HausdorffSimilarityMeasure,
  8815. SimilarityMeasure: SimilarityMeasure,
  8816. SimilarityMeasureCombiner: SimilarityMeasureCombiner
  8817. });
  8818. class Centroid {
  8819. constructor() {
  8820. Centroid.constructor_.apply(this, arguments);
  8821. }
  8822. static constructor_() {
  8823. this._areaBasePt = null;
  8824. this._triangleCent3 = new Coordinate();
  8825. this._areasum2 = 0;
  8826. this._cg3 = new Coordinate();
  8827. this._lineCentSum = new Coordinate();
  8828. this._totalLength = 0.0;
  8829. this._ptCount = 0;
  8830. this._ptCentSum = new Coordinate();
  8831. const geom = arguments[0];
  8832. this._areaBasePt = null;
  8833. this.add(geom);
  8834. }
  8835. static area2(p1, p2, p3) {
  8836. return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
  8837. }
  8838. static centroid3(p1, p2, p3, c) {
  8839. c.x = p1.x + p2.x + p3.x;
  8840. c.y = p1.y + p2.y + p3.y;
  8841. return null;
  8842. }
  8843. static getCentroid(geom) {
  8844. const cent = new Centroid(geom);
  8845. return cent.getCentroid();
  8846. }
  8847. setAreaBasePoint(basePt) {
  8848. this._areaBasePt = basePt;
  8849. }
  8850. addPoint(pt) {
  8851. this._ptCount += 1;
  8852. this._ptCentSum.x += pt.x;
  8853. this._ptCentSum.y += pt.y;
  8854. }
  8855. addLineSegments(pts) {
  8856. let lineLen = 0.0;
  8857. for (let i = 0; i < pts.length - 1; i++) {
  8858. const segmentLen = pts[i].distance(pts[i + 1]);
  8859. if (segmentLen === 0.0) continue;
  8860. lineLen += segmentLen;
  8861. const midx = (pts[i].x + pts[i + 1].x) / 2;
  8862. this._lineCentSum.x += segmentLen * midx;
  8863. const midy = (pts[i].y + pts[i + 1].y) / 2;
  8864. this._lineCentSum.y += segmentLen * midy;
  8865. }
  8866. this._totalLength += lineLen;
  8867. if (lineLen === 0.0 && pts.length > 0) this.addPoint(pts[0]);
  8868. }
  8869. addHole(pts) {
  8870. const isPositiveArea = Orientation.isCCW(pts);
  8871. for (let i = 0; i < pts.length - 1; i++) this.addTriangle(this._areaBasePt, pts[i], pts[i + 1], isPositiveArea);
  8872. this.addLineSegments(pts);
  8873. }
  8874. getCentroid() {
  8875. const cent = new Coordinate();
  8876. if (Math.abs(this._areasum2) > 0.0) {
  8877. cent.x = this._cg3.x / 3 / this._areasum2;
  8878. cent.y = this._cg3.y / 3 / this._areasum2;
  8879. } else if (this._totalLength > 0.0) {
  8880. cent.x = this._lineCentSum.x / this._totalLength;
  8881. cent.y = this._lineCentSum.y / this._totalLength;
  8882. } else if (this._ptCount > 0) {
  8883. cent.x = this._ptCentSum.x / this._ptCount;
  8884. cent.y = this._ptCentSum.y / this._ptCount;
  8885. } else {
  8886. return null;
  8887. }
  8888. return cent;
  8889. }
  8890. addShell(pts) {
  8891. if (pts.length > 0) this.setAreaBasePoint(pts[0]);
  8892. const isPositiveArea = !Orientation.isCCW(pts);
  8893. for (let i = 0; i < pts.length - 1; i++) this.addTriangle(this._areaBasePt, pts[i], pts[i + 1], isPositiveArea);
  8894. this.addLineSegments(pts);
  8895. }
  8896. addTriangle(p0, p1, p2, isPositiveArea) {
  8897. const sign = isPositiveArea ? 1.0 : -1.0;
  8898. Centroid.centroid3(p0, p1, p2, this._triangleCent3);
  8899. const area2 = Centroid.area2(p0, p1, p2);
  8900. this._cg3.x += sign * area2 * this._triangleCent3.x;
  8901. this._cg3.y += sign * area2 * this._triangleCent3.y;
  8902. this._areasum2 += sign * area2;
  8903. }
  8904. add() {
  8905. if (arguments[0] instanceof Polygon) {
  8906. const poly = arguments[0];
  8907. this.addShell(poly.getExteriorRing().getCoordinates());
  8908. for (let i = 0; i < poly.getNumInteriorRing(); i++) this.addHole(poly.getInteriorRingN(i).getCoordinates());
  8909. } else if (arguments[0] instanceof Geometry) {
  8910. const geom = arguments[0];
  8911. if (geom.isEmpty()) return null;
  8912. if (geom instanceof Point) {
  8913. this.addPoint(geom.getCoordinate());
  8914. } else if (geom instanceof LineString) {
  8915. this.addLineSegments(geom.getCoordinates());
  8916. } else if (geom instanceof Polygon) {
  8917. const poly = geom;
  8918. this.add(poly);
  8919. } else if (geom instanceof GeometryCollection) {
  8920. const gc = geom;
  8921. for (let i = 0; i < gc.getNumGeometries(); i++) this.add(gc.getGeometryN(i));
  8922. }
  8923. }
  8924. }
  8925. }
  8926. class EmptyStackException extends Exception {
  8927. constructor(message) {
  8928. super(message);
  8929. this.name = Object.keys({
  8930. EmptyStackException
  8931. })[0];
  8932. }
  8933. }
  8934. /**
  8935. * @see http://download.oracle.com/javase/6/docs/api/java/util/Stack.html
  8936. */
  8937. class Stack extends List {
  8938. constructor() {
  8939. super();
  8940. this.array = [];
  8941. }
  8942. add(e) {
  8943. this.array.push(e);
  8944. return true;
  8945. }
  8946. get(index) {
  8947. if (index < 0 || index >= this.size()) throw new IndexOutOfBoundsException();
  8948. return this.array[index];
  8949. }
  8950. /**
  8951. * Pushes an item onto the top of this stack.
  8952. * @param {Object} e
  8953. * @return {Object}
  8954. */
  8955. push(e) {
  8956. this.array.push(e);
  8957. return e;
  8958. }
  8959. /**
  8960. * Removes the object at the top of this stack and returns that object as the value of this function.
  8961. * @return {Object}
  8962. */
  8963. pop() {
  8964. if (this.array.length === 0) throw new EmptyStackException();
  8965. return this.array.pop();
  8966. }
  8967. /**
  8968. * Looks at the object at the top of this stack without removing it from the
  8969. * stack.
  8970. * @return {Object}
  8971. */
  8972. peek() {
  8973. if (this.array.length === 0) throw new EmptyStackException();
  8974. return this.array[this.array.length - 1];
  8975. }
  8976. /**
  8977. * Tests if this stack is empty.
  8978. * @return {boolean} true if and only if this stack contains no items; false
  8979. * otherwise.
  8980. */
  8981. empty() {
  8982. return this.array.length === 0;
  8983. }
  8984. /**
  8985. * @return {boolean}
  8986. */
  8987. isEmpty() {
  8988. return this.empty();
  8989. }
  8990. /**
  8991. * Returns the 1-based position where an object is on this stack. If the object
  8992. * o occurs as an item in this stack, this method returns the distance from the
  8993. * top of the stack of the occurrence nearest the top of the stack; the topmost
  8994. * item on the stack is considered to be at distance 1. The equals method is
  8995. * used to compare o to the items in this stack.
  8996. *
  8997. * NOTE: does not currently actually use equals. (=== is used)
  8998. *
  8999. * @param {Object} o
  9000. * @return {number} the 1-based position from the top of the stack where the
  9001. * object is located; the return value -1 indicates that the object is
  9002. * not on the stack.
  9003. */
  9004. search(o) {
  9005. return this.array.indexOf(o);
  9006. }
  9007. /**
  9008. * @return {number}
  9009. */
  9010. size() {
  9011. return this.array.length;
  9012. }
  9013. /**
  9014. * @return {Array}
  9015. */
  9016. toArray() {
  9017. return this.array.slice();
  9018. }
  9019. }
  9020. class UniqueCoordinateArrayFilter {
  9021. constructor() {
  9022. UniqueCoordinateArrayFilter.constructor_.apply(this, arguments);
  9023. }
  9024. static constructor_() {
  9025. this._coordSet = new HashSet();
  9026. this._list = new ArrayList();
  9027. }
  9028. static filterCoordinates(coords) {
  9029. const filter = new UniqueCoordinateArrayFilter();
  9030. for (let i = 0; i < coords.length; i++) filter.filter(coords[i]);
  9031. return filter.getCoordinates();
  9032. }
  9033. filter(coord) {
  9034. if (this._coordSet.add(coord)) this._list.add(coord);
  9035. }
  9036. getCoordinates() {
  9037. const coordinates = new Array(this._list.size()).fill(null);
  9038. return this._list.toArray(coordinates);
  9039. }
  9040. get interfaces_() {
  9041. return [CoordinateFilter];
  9042. }
  9043. }
  9044. class ConvexHull {
  9045. constructor() {
  9046. ConvexHull.constructor_.apply(this, arguments);
  9047. }
  9048. static constructor_() {
  9049. this._geomFactory = null;
  9050. this._inputPts = null;
  9051. if (arguments.length === 1) {
  9052. const geometry = arguments[0];
  9053. ConvexHull.constructor_.call(this, ConvexHull.extractCoordinates(geometry), geometry.getFactory());
  9054. } else if (arguments.length === 2) {
  9055. const pts = arguments[0],
  9056. geomFactory = arguments[1];
  9057. this._inputPts = UniqueCoordinateArrayFilter.filterCoordinates(pts);
  9058. this._geomFactory = geomFactory;
  9059. }
  9060. }
  9061. static extractCoordinates(geom) {
  9062. const filter = new UniqueCoordinateArrayFilter();
  9063. geom.apply(filter);
  9064. return filter.getCoordinates();
  9065. }
  9066. preSort(pts) {
  9067. let t = null;
  9068. for (let i = 1; i < pts.length; i++) if (pts[i].y < pts[0].y || pts[i].y === pts[0].y && pts[i].x < pts[0].x) {
  9069. t = pts[0];
  9070. pts[0] = pts[i];
  9071. pts[i] = t;
  9072. }
  9073. Arrays.sort(pts, 1, pts.length, new RadialComparator(pts[0]));
  9074. return pts;
  9075. }
  9076. computeOctRing(inputPts) {
  9077. const octPts = this.computeOctPts(inputPts);
  9078. const coordList = new CoordinateList();
  9079. coordList.add(octPts, false);
  9080. if (coordList.size() < 3) return null;
  9081. coordList.closeRing();
  9082. return coordList.toCoordinateArray();
  9083. }
  9084. lineOrPolygon(coordinates) {
  9085. coordinates = this.cleanRing(coordinates);
  9086. if (coordinates.length === 3) return this._geomFactory.createLineString([coordinates[0], coordinates[1]]);
  9087. const linearRing = this._geomFactory.createLinearRing(coordinates);
  9088. return this._geomFactory.createPolygon(linearRing);
  9089. }
  9090. cleanRing(original) {
  9091. Assert.equals(original[0], original[original.length - 1]);
  9092. const cleanedRing = new ArrayList();
  9093. let previousDistinctCoordinate = null;
  9094. for (let i = 0; i <= original.length - 2; i++) {
  9095. const currentCoordinate = original[i];
  9096. const nextCoordinate = original[i + 1];
  9097. if (currentCoordinate.equals(nextCoordinate)) continue;
  9098. if (previousDistinctCoordinate !== null && this.isBetween(previousDistinctCoordinate, currentCoordinate, nextCoordinate)) continue;
  9099. cleanedRing.add(currentCoordinate);
  9100. previousDistinctCoordinate = currentCoordinate;
  9101. }
  9102. cleanedRing.add(original[original.length - 1]);
  9103. const cleanedRingCoordinates = new Array(cleanedRing.size()).fill(null);
  9104. return cleanedRing.toArray(cleanedRingCoordinates);
  9105. }
  9106. isBetween(c1, c2, c3) {
  9107. if (Orientation.index(c1, c2, c3) !== 0) return false;
  9108. if (c1.x !== c3.x) {
  9109. if (c1.x <= c2.x && c2.x <= c3.x) return true;
  9110. if (c3.x <= c2.x && c2.x <= c1.x) return true;
  9111. }
  9112. if (c1.y !== c3.y) {
  9113. if (c1.y <= c2.y && c2.y <= c3.y) return true;
  9114. if (c3.y <= c2.y && c2.y <= c1.y) return true;
  9115. }
  9116. return false;
  9117. }
  9118. reduce(inputPts) {
  9119. const polyPts = this.computeOctRing(inputPts);
  9120. if (polyPts === null) return inputPts;
  9121. const reducedSet = new TreeSet();
  9122. for (let i = 0; i < polyPts.length; i++) reducedSet.add(polyPts[i]);
  9123. for (let i = 0; i < inputPts.length; i++) if (!PointLocation.isInRing(inputPts[i], polyPts)) reducedSet.add(inputPts[i]);
  9124. const reducedPts = CoordinateArrays.toCoordinateArray(reducedSet);
  9125. if (reducedPts.length < 3) return this.padArray3(reducedPts);
  9126. return reducedPts;
  9127. }
  9128. getConvexHull() {
  9129. if (this._inputPts.length === 0) return this._geomFactory.createGeometryCollection();
  9130. if (this._inputPts.length === 1) return this._geomFactory.createPoint(this._inputPts[0]);
  9131. if (this._inputPts.length === 2) return this._geomFactory.createLineString(this._inputPts);
  9132. let reducedPts = this._inputPts;
  9133. if (this._inputPts.length > 50) reducedPts = this.reduce(this._inputPts);
  9134. const sortedPts = this.preSort(reducedPts);
  9135. const cHS = this.grahamScan(sortedPts);
  9136. const cH = this.toCoordinateArray(cHS);
  9137. return this.lineOrPolygon(cH);
  9138. }
  9139. padArray3(pts) {
  9140. const pad = new Array(3).fill(null);
  9141. for (let i = 0; i < pad.length; i++) if (i < pts.length) pad[i] = pts[i];else pad[i] = pts[0];
  9142. return pad;
  9143. }
  9144. computeOctPts(inputPts) {
  9145. const pts = new Array(8).fill(null);
  9146. for (let j = 0; j < pts.length; j++) pts[j] = inputPts[0];
  9147. for (let i = 1; i < inputPts.length; i++) {
  9148. if (inputPts[i].x < pts[0].x) pts[0] = inputPts[i];
  9149. if (inputPts[i].x - inputPts[i].y < pts[1].x - pts[1].y) pts[1] = inputPts[i];
  9150. if (inputPts[i].y > pts[2].y) pts[2] = inputPts[i];
  9151. if (inputPts[i].x + inputPts[i].y > pts[3].x + pts[3].y) pts[3] = inputPts[i];
  9152. if (inputPts[i].x > pts[4].x) pts[4] = inputPts[i];
  9153. if (inputPts[i].x - inputPts[i].y > pts[5].x - pts[5].y) pts[5] = inputPts[i];
  9154. if (inputPts[i].y < pts[6].y) pts[6] = inputPts[i];
  9155. if (inputPts[i].x + inputPts[i].y < pts[7].x + pts[7].y) pts[7] = inputPts[i];
  9156. }
  9157. return pts;
  9158. }
  9159. toCoordinateArray(stack) {
  9160. const coordinates = new Array(stack.size()).fill(null);
  9161. for (let i = 0; i < stack.size(); i++) {
  9162. const coordinate = stack.get(i);
  9163. coordinates[i] = coordinate;
  9164. }
  9165. return coordinates;
  9166. }
  9167. grahamScan(c) {
  9168. let p = null;
  9169. const ps = new Stack();
  9170. ps.push(c[0]);
  9171. ps.push(c[1]);
  9172. ps.push(c[2]);
  9173. for (let i = 3; i < c.length; i++) {
  9174. p = ps.pop();
  9175. while (!ps.empty() && Orientation.index(ps.peek(), p, c[i]) > 0) p = ps.pop();
  9176. ps.push(p);
  9177. ps.push(c[i]);
  9178. }
  9179. ps.push(c[0]);
  9180. return ps;
  9181. }
  9182. }
  9183. class RadialComparator {
  9184. constructor() {
  9185. RadialComparator.constructor_.apply(this, arguments);
  9186. }
  9187. static constructor_() {
  9188. this._origin = null;
  9189. const origin = arguments[0];
  9190. this._origin = origin;
  9191. }
  9192. static polarCompare(o, p, q) {
  9193. const dxp = p.x - o.x;
  9194. const dyp = p.y - o.y;
  9195. const dxq = q.x - o.x;
  9196. const dyq = q.y - o.y;
  9197. const orient = Orientation.index(o, p, q);
  9198. if (orient === Orientation.COUNTERCLOCKWISE) return 1;
  9199. if (orient === Orientation.CLOCKWISE) return -1;
  9200. const op = dxp * dxp + dyp * dyp;
  9201. const oq = dxq * dxq + dyq * dyq;
  9202. if (op < oq) return -1;
  9203. if (op > oq) return 1;
  9204. return 0;
  9205. }
  9206. compare(o1, o2) {
  9207. const p1 = o1;
  9208. const p2 = o2;
  9209. return RadialComparator.polarCompare(this._origin, p1, p2);
  9210. }
  9211. get interfaces_() {
  9212. return [Comparator];
  9213. }
  9214. }
  9215. ConvexHull.RadialComparator = RadialComparator;
  9216. class InteriorPointArea {
  9217. constructor() {
  9218. InteriorPointArea.constructor_.apply(this, arguments);
  9219. }
  9220. static constructor_() {
  9221. this._interiorPoint = null;
  9222. this._maxWidth = -1;
  9223. const g = arguments[0];
  9224. this.process(g);
  9225. }
  9226. static getInteriorPoint(geom) {
  9227. const intPt = new InteriorPointArea(geom);
  9228. return intPt.getInteriorPoint();
  9229. }
  9230. static avg(a, b) {
  9231. return (a + b) / 2.0;
  9232. }
  9233. getInteriorPoint() {
  9234. return this._interiorPoint;
  9235. }
  9236. process(geom) {
  9237. if (geom.isEmpty()) return null;
  9238. if (geom instanceof Polygon) {
  9239. this.processPolygon(geom);
  9240. } else if (geom instanceof GeometryCollection) {
  9241. const gc = geom;
  9242. for (let i = 0; i < gc.getNumGeometries(); i++) this.process(gc.getGeometryN(i));
  9243. }
  9244. }
  9245. processPolygon(polygon) {
  9246. const intPtPoly = new InteriorPointPolygon(polygon);
  9247. intPtPoly.process();
  9248. const width = intPtPoly.getWidth();
  9249. if (width > this._maxWidth) {
  9250. this._maxWidth = width;
  9251. this._interiorPoint = intPtPoly.getInteriorPoint();
  9252. }
  9253. }
  9254. }
  9255. class InteriorPointPolygon {
  9256. constructor() {
  9257. InteriorPointPolygon.constructor_.apply(this, arguments);
  9258. }
  9259. static constructor_() {
  9260. this._polygon = null;
  9261. this._interiorPointY = null;
  9262. this._interiorSectionWidth = 0.0;
  9263. this._interiorPoint = null;
  9264. const polygon = arguments[0];
  9265. this._polygon = polygon;
  9266. this._interiorPointY = ScanLineYOrdinateFinder.getScanLineY(polygon);
  9267. }
  9268. static isEdgeCrossingCounted(p0, p1, scanY) {
  9269. const y0 = p0.getY();
  9270. const y1 = p1.getY();
  9271. if (y0 === y1) return false;
  9272. if (y0 === scanY && y1 < scanY) return false;
  9273. if (y1 === scanY && y0 < scanY) return false;
  9274. return true;
  9275. }
  9276. static intersectsHorizontalLine() {
  9277. if (arguments.length === 2) {
  9278. const env = arguments[0],
  9279. y = arguments[1];
  9280. if (y < env.getMinY()) return false;
  9281. if (y > env.getMaxY()) return false;
  9282. return true;
  9283. } else if (arguments.length === 3) {
  9284. const p0 = arguments[0],
  9285. p1 = arguments[1],
  9286. y = arguments[2];
  9287. if (p0.getY() > y && p1.getY() > y) return false;
  9288. if (p0.getY() < y && p1.getY() < y) return false;
  9289. return true;
  9290. }
  9291. }
  9292. static intersection(p0, p1, Y) {
  9293. const x0 = p0.getX();
  9294. const x1 = p1.getX();
  9295. if (x0 === x1) return x0;
  9296. const segDX = x1 - x0;
  9297. const segDY = p1.getY() - p0.getY();
  9298. const m = segDY / segDX;
  9299. const x = x0 + (Y - p0.getY()) / m;
  9300. return x;
  9301. }
  9302. findBestMidpoint(crossings) {
  9303. if (crossings.size() === 0) return null;
  9304. Assert.isTrue(0 === crossings.size() % 2, 'Interior Point robustness failure: odd number of scanline crossings');
  9305. crossings.sort(new DoubleComparator());
  9306. for (let i = 0; i < crossings.size(); i += 2) {
  9307. const x1 = crossings.get(i);
  9308. const x2 = crossings.get(i + 1);
  9309. const width = x2 - x1;
  9310. if (width > this._interiorSectionWidth) {
  9311. this._interiorSectionWidth = width;
  9312. const interiorPointX = InteriorPointArea.avg(x1, x2);
  9313. this._interiorPoint = new Coordinate(interiorPointX, this._interiorPointY);
  9314. }
  9315. }
  9316. }
  9317. process() {
  9318. if (this._polygon.isEmpty()) return null;
  9319. this._interiorPoint = new Coordinate(this._polygon.getCoordinate());
  9320. const crossings = new ArrayList();
  9321. this.scanRing(this._polygon.getExteriorRing(), crossings);
  9322. for (let i = 0; i < this._polygon.getNumInteriorRing(); i++) this.scanRing(this._polygon.getInteriorRingN(i), crossings);
  9323. this.findBestMidpoint(crossings);
  9324. }
  9325. scanRing(ring, crossings) {
  9326. if (!InteriorPointPolygon.intersectsHorizontalLine(ring.getEnvelopeInternal(), this._interiorPointY)) return null;
  9327. const seq = ring.getCoordinateSequence();
  9328. for (let i = 1; i < seq.size(); i++) {
  9329. const ptPrev = seq.getCoordinate(i - 1);
  9330. const pt = seq.getCoordinate(i);
  9331. this.addEdgeCrossing(ptPrev, pt, this._interiorPointY, crossings);
  9332. }
  9333. }
  9334. getWidth() {
  9335. return this._interiorSectionWidth;
  9336. }
  9337. getInteriorPoint() {
  9338. return this._interiorPoint;
  9339. }
  9340. addEdgeCrossing(p0, p1, scanY, crossings) {
  9341. if (!InteriorPointPolygon.intersectsHorizontalLine(p0, p1, scanY)) return null;
  9342. if (!InteriorPointPolygon.isEdgeCrossingCounted(p0, p1, scanY)) return null;
  9343. const xInt = InteriorPointPolygon.intersection(p0, p1, scanY);
  9344. crossings.add(xInt);
  9345. }
  9346. }
  9347. class DoubleComparator {
  9348. compare(v1, v2) {
  9349. return v1 < v2 ? -1 : v1 > v2 ? +1 : 0;
  9350. }
  9351. get interfaces_() {
  9352. return [Comparator];
  9353. }
  9354. }
  9355. InteriorPointPolygon.DoubleComparator = DoubleComparator;
  9356. class ScanLineYOrdinateFinder {
  9357. constructor() {
  9358. ScanLineYOrdinateFinder.constructor_.apply(this, arguments);
  9359. }
  9360. static constructor_() {
  9361. this._poly = null;
  9362. this._centreY = null;
  9363. this._hiY = Double.MAX_VALUE;
  9364. this._loY = -Double.MAX_VALUE;
  9365. const poly = arguments[0];
  9366. this._poly = poly;
  9367. this._hiY = poly.getEnvelopeInternal().getMaxY();
  9368. this._loY = poly.getEnvelopeInternal().getMinY();
  9369. this._centreY = InteriorPointArea.avg(this._loY, this._hiY);
  9370. }
  9371. static getScanLineY(poly) {
  9372. const finder = new ScanLineYOrdinateFinder(poly);
  9373. return finder.getScanLineY();
  9374. }
  9375. updateInterval(y) {
  9376. if (y <= this._centreY) {
  9377. if (y > this._loY) this._loY = y;
  9378. } else if (y > this._centreY) {
  9379. if (y < this._hiY) this._hiY = y;
  9380. }
  9381. }
  9382. getScanLineY() {
  9383. this.process(this._poly.getExteriorRing());
  9384. for (let i = 0; i < this._poly.getNumInteriorRing(); i++) this.process(this._poly.getInteriorRingN(i));
  9385. const scanLineY = InteriorPointArea.avg(this._hiY, this._loY);
  9386. return scanLineY;
  9387. }
  9388. process(line) {
  9389. const seq = line.getCoordinateSequence();
  9390. for (let i = 0; i < seq.size(); i++) {
  9391. const y = seq.getY(i);
  9392. this.updateInterval(y);
  9393. }
  9394. }
  9395. }
  9396. InteriorPointArea.InteriorPointPolygon = InteriorPointPolygon;
  9397. InteriorPointArea.ScanLineYOrdinateFinder = ScanLineYOrdinateFinder;
  9398. class InteriorPointLine {
  9399. constructor() {
  9400. InteriorPointLine.constructor_.apply(this, arguments);
  9401. }
  9402. static constructor_() {
  9403. this._centroid = null;
  9404. this._minDistance = Double.MAX_VALUE;
  9405. this._interiorPoint = null;
  9406. const g = arguments[0];
  9407. if (g.isEmpty()) {
  9408. this._centroid = null;
  9409. } else {
  9410. this._centroid = Centroid.getCentroid(g);
  9411. g.getPrecisionModel().makePrecise(this._centroid);
  9412. }
  9413. this.addInterior(g);
  9414. if (this._interiorPoint === null) this.addEndpoints(g);
  9415. }
  9416. static getInteriorPoint(geom) {
  9417. const intPt = new InteriorPointLine(geom);
  9418. return intPt.getInteriorPoint();
  9419. }
  9420. addEndpoints() {
  9421. if (arguments[0] instanceof Geometry) {
  9422. const geom = arguments[0];
  9423. if (geom instanceof LineString) {
  9424. this.addEndpoints(geom.getCoordinates());
  9425. } else if (geom instanceof GeometryCollection) {
  9426. const gc = geom;
  9427. for (let i = 0; i < gc.getNumGeometries(); i++) this.addEndpoints(gc.getGeometryN(i));
  9428. }
  9429. } else if (arguments[0] instanceof Array) {
  9430. const pts = arguments[0];
  9431. this.add(pts[0]);
  9432. this.add(pts[pts.length - 1]);
  9433. }
  9434. }
  9435. getInteriorPoint() {
  9436. return this._interiorPoint;
  9437. }
  9438. addInterior() {
  9439. if (arguments[0] instanceof Geometry) {
  9440. const geom = arguments[0];
  9441. if (geom instanceof LineString) {
  9442. this.addInterior(geom.getCoordinates());
  9443. } else if (geom instanceof GeometryCollection) {
  9444. const gc = geom;
  9445. for (let i = 0; i < gc.getNumGeometries(); i++) this.addInterior(gc.getGeometryN(i));
  9446. }
  9447. } else if (arguments[0] instanceof Array) {
  9448. const pts = arguments[0];
  9449. for (let i = 1; i < pts.length - 1; i++) this.add(pts[i]);
  9450. }
  9451. }
  9452. add(point) {
  9453. const dist = point.distance(this._centroid);
  9454. if (dist < this._minDistance) {
  9455. this._interiorPoint = new Coordinate(point);
  9456. this._minDistance = dist;
  9457. }
  9458. }
  9459. }
  9460. class InteriorPointPoint {
  9461. constructor() {
  9462. InteriorPointPoint.constructor_.apply(this, arguments);
  9463. }
  9464. static constructor_() {
  9465. this._centroid = null;
  9466. this._minDistance = Double.MAX_VALUE;
  9467. this._interiorPoint = null;
  9468. const g = arguments[0];
  9469. this._centroid = Centroid.getCentroid(g);
  9470. this.add(g);
  9471. }
  9472. static getInteriorPoint(geom) {
  9473. const intPt = new InteriorPointPoint(geom);
  9474. return intPt.getInteriorPoint();
  9475. }
  9476. getInteriorPoint() {
  9477. return this._interiorPoint;
  9478. }
  9479. add() {
  9480. if (arguments[0] instanceof Geometry) {
  9481. const geom = arguments[0];
  9482. if (geom instanceof Point) {
  9483. this.add(geom.getCoordinate());
  9484. } else if (geom instanceof GeometryCollection) {
  9485. const gc = geom;
  9486. for (let i = 0; i < gc.getNumGeometries(); i++) this.add(gc.getGeometryN(i));
  9487. }
  9488. } else if (arguments[0] instanceof Coordinate) {
  9489. const point = arguments[0];
  9490. const dist = point.distance(this._centroid);
  9491. if (dist < this._minDistance) {
  9492. this._interiorPoint = new Coordinate(point);
  9493. this._minDistance = dist;
  9494. }
  9495. }
  9496. }
  9497. }
  9498. class BoundaryNodeRule {
  9499. isInBoundary(boundaryCount) {}
  9500. }
  9501. class Mod2BoundaryNodeRule {
  9502. isInBoundary(boundaryCount) {
  9503. return boundaryCount % 2 === 1;
  9504. }
  9505. get interfaces_() {
  9506. return [BoundaryNodeRule];
  9507. }
  9508. }
  9509. class EndPointBoundaryNodeRule {
  9510. isInBoundary(boundaryCount) {
  9511. return boundaryCount > 0;
  9512. }
  9513. get interfaces_() {
  9514. return [BoundaryNodeRule];
  9515. }
  9516. }
  9517. class MultiValentEndPointBoundaryNodeRule {
  9518. isInBoundary(boundaryCount) {
  9519. return boundaryCount > 1;
  9520. }
  9521. get interfaces_() {
  9522. return [BoundaryNodeRule];
  9523. }
  9524. }
  9525. class MonoValentEndPointBoundaryNodeRule {
  9526. isInBoundary(boundaryCount) {
  9527. return boundaryCount === 1;
  9528. }
  9529. get interfaces_() {
  9530. return [BoundaryNodeRule];
  9531. }
  9532. }
  9533. BoundaryNodeRule.Mod2BoundaryNodeRule = Mod2BoundaryNodeRule;
  9534. BoundaryNodeRule.EndPointBoundaryNodeRule = EndPointBoundaryNodeRule;
  9535. BoundaryNodeRule.MultiValentEndPointBoundaryNodeRule = MultiValentEndPointBoundaryNodeRule;
  9536. BoundaryNodeRule.MonoValentEndPointBoundaryNodeRule = MonoValentEndPointBoundaryNodeRule;
  9537. BoundaryNodeRule.MOD2_BOUNDARY_RULE = new Mod2BoundaryNodeRule();
  9538. BoundaryNodeRule.ENDPOINT_BOUNDARY_RULE = new EndPointBoundaryNodeRule();
  9539. BoundaryNodeRule.MULTIVALENT_ENDPOINT_BOUNDARY_RULE = new MultiValentEndPointBoundaryNodeRule();
  9540. BoundaryNodeRule.MONOVALENT_ENDPOINT_BOUNDARY_RULE = new MonoValentEndPointBoundaryNodeRule();
  9541. BoundaryNodeRule.OGC_SFS_BOUNDARY_RULE = BoundaryNodeRule.MOD2_BOUNDARY_RULE;
  9542. class PointLocator {
  9543. constructor() {
  9544. PointLocator.constructor_.apply(this, arguments);
  9545. }
  9546. static constructor_() {
  9547. this._boundaryRule = BoundaryNodeRule.OGC_SFS_BOUNDARY_RULE;
  9548. this._isIn = null;
  9549. this._numBoundaries = null;
  9550. if (arguments.length === 0) ; else if (arguments.length === 1) {
  9551. const boundaryRule = arguments[0];
  9552. if (boundaryRule === null) throw new IllegalArgumentException('Rule must be non-null');
  9553. this._boundaryRule = boundaryRule;
  9554. }
  9555. }
  9556. locateInPolygonRing(p, ring) {
  9557. if (!ring.getEnvelopeInternal().intersects(p)) return Location.EXTERIOR;
  9558. return PointLocation.locateInRing(p, ring.getCoordinates());
  9559. }
  9560. intersects(p, geom) {
  9561. return this.locate(p, geom) !== Location.EXTERIOR;
  9562. }
  9563. updateLocationInfo(loc) {
  9564. if (loc === Location.INTERIOR) this._isIn = true;
  9565. if (loc === Location.BOUNDARY) this._numBoundaries++;
  9566. }
  9567. computeLocation(p, geom) {
  9568. if (geom instanceof Point) this.updateLocationInfo(this.locateOnPoint(p, geom));
  9569. if (geom instanceof LineString) {
  9570. this.updateLocationInfo(this.locateOnLineString(p, geom));
  9571. } else if (geom instanceof Polygon) {
  9572. this.updateLocationInfo(this.locateInPolygon(p, geom));
  9573. } else if (geom instanceof MultiLineString) {
  9574. const ml = geom;
  9575. for (let i = 0; i < ml.getNumGeometries(); i++) {
  9576. const l = ml.getGeometryN(i);
  9577. this.updateLocationInfo(this.locateOnLineString(p, l));
  9578. }
  9579. } else if (geom instanceof MultiPolygon) {
  9580. const mpoly = geom;
  9581. for (let i = 0; i < mpoly.getNumGeometries(); i++) {
  9582. const poly = mpoly.getGeometryN(i);
  9583. this.updateLocationInfo(this.locateInPolygon(p, poly));
  9584. }
  9585. } else if (geom instanceof GeometryCollection) {
  9586. const geomi = new GeometryCollectionIterator(geom);
  9587. while (geomi.hasNext()) {
  9588. const g2 = geomi.next();
  9589. if (g2 !== geom) this.computeLocation(p, g2);
  9590. }
  9591. }
  9592. }
  9593. locateOnPoint(p, pt) {
  9594. const ptCoord = pt.getCoordinate();
  9595. if (ptCoord.equals2D(p)) return Location.INTERIOR;
  9596. return Location.EXTERIOR;
  9597. }
  9598. locateOnLineString(p, l) {
  9599. if (!l.getEnvelopeInternal().intersects(p)) return Location.EXTERIOR;
  9600. const seq = l.getCoordinateSequence();
  9601. if (!l.isClosed()) if (p.equals(seq.getCoordinate(0)) || p.equals(seq.getCoordinate(seq.size() - 1))) return Location.BOUNDARY;
  9602. if (PointLocation.isOnLine(p, seq)) return Location.INTERIOR;
  9603. return Location.EXTERIOR;
  9604. }
  9605. locateInPolygon(p, poly) {
  9606. if (poly.isEmpty()) return Location.EXTERIOR;
  9607. const shell = poly.getExteriorRing();
  9608. const shellLoc = this.locateInPolygonRing(p, shell);
  9609. if (shellLoc === Location.EXTERIOR) return Location.EXTERIOR;
  9610. if (shellLoc === Location.BOUNDARY) return Location.BOUNDARY;
  9611. for (let i = 0; i < poly.getNumInteriorRing(); i++) {
  9612. const hole = poly.getInteriorRingN(i);
  9613. const holeLoc = this.locateInPolygonRing(p, hole);
  9614. if (holeLoc === Location.INTERIOR) return Location.EXTERIOR;
  9615. if (holeLoc === Location.BOUNDARY) return Location.BOUNDARY;
  9616. }
  9617. return Location.INTERIOR;
  9618. }
  9619. locate(p, geom) {
  9620. if (geom.isEmpty()) return Location.EXTERIOR;
  9621. if (geom instanceof LineString) return this.locateOnLineString(p, geom);else if (geom instanceof Polygon) return this.locateInPolygon(p, geom);
  9622. this._isIn = false;
  9623. this._numBoundaries = 0;
  9624. this.computeLocation(p, geom);
  9625. if (this._boundaryRule.isInBoundary(this._numBoundaries)) return Location.BOUNDARY;
  9626. if (this._numBoundaries > 0 || this._isIn) return Location.INTERIOR;
  9627. return Location.EXTERIOR;
  9628. }
  9629. }
  9630. class MinimumBoundingCircle {
  9631. constructor() {
  9632. MinimumBoundingCircle.constructor_.apply(this, arguments);
  9633. }
  9634. static constructor_() {
  9635. this._input = null;
  9636. this._extremalPts = null;
  9637. this._centre = null;
  9638. this._radius = 0.0;
  9639. const geom = arguments[0];
  9640. this._input = geom;
  9641. }
  9642. static farthestPoints(pts) {
  9643. const dist01 = pts[0].distance(pts[1]);
  9644. const dist12 = pts[1].distance(pts[2]);
  9645. const dist20 = pts[2].distance(pts[0]);
  9646. if (dist01 >= dist12 && dist01 >= dist20) return [pts[0], pts[1]];
  9647. if (dist12 >= dist01 && dist12 >= dist20) return [pts[1], pts[2]];
  9648. return [pts[2], pts[0]];
  9649. }
  9650. static pointWitMinAngleWithX(pts, P) {
  9651. let minSin = Double.MAX_VALUE;
  9652. let minAngPt = null;
  9653. for (let i = 0; i < pts.length; i++) {
  9654. const p = pts[i];
  9655. if (p === P) continue;
  9656. const dx = p.x - P.x;
  9657. let dy = p.y - P.y;
  9658. if (dy < 0) dy = -dy;
  9659. const len = Math.sqrt(dx * dx + dy * dy);
  9660. const sin = dy / len;
  9661. if (sin < minSin) {
  9662. minSin = sin;
  9663. minAngPt = p;
  9664. }
  9665. }
  9666. return minAngPt;
  9667. }
  9668. static lowestPoint(pts) {
  9669. let min = pts[0];
  9670. for (let i = 1; i < pts.length; i++) if (pts[i].y < min.y) min = pts[i];
  9671. return min;
  9672. }
  9673. static pointWithMinAngleWithSegment(pts, P, Q) {
  9674. let minAng = Double.MAX_VALUE;
  9675. let minAngPt = null;
  9676. for (let i = 0; i < pts.length; i++) {
  9677. const p = pts[i];
  9678. if (p === P) continue;
  9679. if (p === Q) continue;
  9680. const ang = Angle.angleBetween(P, p, Q);
  9681. if (ang < minAng) {
  9682. minAng = ang;
  9683. minAngPt = p;
  9684. }
  9685. }
  9686. return minAngPt;
  9687. }
  9688. getRadius() {
  9689. this.compute();
  9690. return this._radius;
  9691. }
  9692. getDiameter() {
  9693. this.compute();
  9694. switch (this._extremalPts.length) {
  9695. case 0:
  9696. return this._input.getFactory().createLineString();
  9697. case 1:
  9698. return this._input.getFactory().createPoint(this._centre);
  9699. }
  9700. const p0 = this._extremalPts[0];
  9701. const p1 = this._extremalPts[1];
  9702. return this._input.getFactory().createLineString([p0, p1]);
  9703. }
  9704. getExtremalPoints() {
  9705. this.compute();
  9706. return this._extremalPts;
  9707. }
  9708. computeCirclePoints() {
  9709. if (this._input.isEmpty()) {
  9710. this._extremalPts = new Array(0).fill(null);
  9711. return null;
  9712. }
  9713. if (this._input.getNumPoints() === 1) {
  9714. const pts = this._input.getCoordinates();
  9715. this._extremalPts = [new Coordinate(pts[0])];
  9716. return null;
  9717. }
  9718. const convexHull = this._input.convexHull();
  9719. const hullPts = convexHull.getCoordinates();
  9720. let pts = hullPts;
  9721. if (hullPts[0].equals2D(hullPts[hullPts.length - 1])) {
  9722. pts = new Array(hullPts.length - 1).fill(null);
  9723. CoordinateArrays.copyDeep(hullPts, 0, pts, 0, hullPts.length - 1);
  9724. }
  9725. if (pts.length <= 2) {
  9726. this._extremalPts = CoordinateArrays.copyDeep(pts);
  9727. return null;
  9728. }
  9729. let P = MinimumBoundingCircle.lowestPoint(pts);
  9730. let Q = MinimumBoundingCircle.pointWitMinAngleWithX(pts, P);
  9731. for (let i = 0; i < pts.length; i++) {
  9732. const R = MinimumBoundingCircle.pointWithMinAngleWithSegment(pts, P, Q);
  9733. if (Angle.isObtuse(P, R, Q)) {
  9734. this._extremalPts = [new Coordinate(P), new Coordinate(Q)];
  9735. return null;
  9736. }
  9737. if (Angle.isObtuse(R, P, Q)) {
  9738. P = R;
  9739. continue;
  9740. }
  9741. if (Angle.isObtuse(R, Q, P)) {
  9742. Q = R;
  9743. continue;
  9744. }
  9745. this._extremalPts = [new Coordinate(P), new Coordinate(Q), new Coordinate(R)];
  9746. return null;
  9747. }
  9748. Assert.shouldNeverReachHere('Logic failure in Minimum Bounding Circle algorithm!');
  9749. }
  9750. compute() {
  9751. if (this._extremalPts !== null) return null;
  9752. this.computeCirclePoints();
  9753. this.computeCentre();
  9754. if (this._centre !== null) this._radius = this._centre.distance(this._extremalPts[0]);
  9755. }
  9756. getCircle() {
  9757. this.compute();
  9758. if (this._centre === null) return this._input.getFactory().createPolygon();
  9759. const centrePoint = this._input.getFactory().createPoint(this._centre);
  9760. if (this._radius === 0.0) return centrePoint;
  9761. return centrePoint.buffer(this._radius);
  9762. }
  9763. getCentre() {
  9764. this.compute();
  9765. return this._centre;
  9766. }
  9767. getMaximumDiameter() {
  9768. this.compute();
  9769. switch (this._extremalPts.length) {
  9770. case 0:
  9771. return this._input.getFactory().createLineString();
  9772. case 1:
  9773. return this._input.getFactory().createPoint(this._centre);
  9774. case 2:
  9775. return this._input.getFactory().createLineString([this._extremalPts[0], this._extremalPts[1]]);
  9776. default:
  9777. const maxDiameter = MinimumBoundingCircle.farthestPoints(this._extremalPts);
  9778. return this._input.getFactory().createLineString(maxDiameter);
  9779. }
  9780. }
  9781. computeCentre() {
  9782. switch (this._extremalPts.length) {
  9783. case 0:
  9784. this._centre = null;
  9785. break;
  9786. case 1:
  9787. this._centre = this._extremalPts[0];
  9788. break;
  9789. case 2:
  9790. this._centre = new Coordinate((this._extremalPts[0].x + this._extremalPts[1].x) / 2.0, (this._extremalPts[0].y + this._extremalPts[1].y) / 2.0);
  9791. break;
  9792. case 3:
  9793. this._centre = Triangle.circumcentre(this._extremalPts[0], this._extremalPts[1], this._extremalPts[2]);
  9794. break;
  9795. }
  9796. }
  9797. }
  9798. class MinimumDiameter {
  9799. constructor() {
  9800. MinimumDiameter.constructor_.apply(this, arguments);
  9801. }
  9802. static constructor_() {
  9803. this._inputGeom = null;
  9804. this._isConvex = null;
  9805. this._convexHullPts = null;
  9806. this._minBaseSeg = new LineSegment();
  9807. this._minWidthPt = null;
  9808. this._minPtIndex = null;
  9809. this._minWidth = 0.0;
  9810. if (arguments.length === 1) {
  9811. const inputGeom = arguments[0];
  9812. MinimumDiameter.constructor_.call(this, inputGeom, false);
  9813. } else if (arguments.length === 2) {
  9814. const inputGeom = arguments[0],
  9815. isConvex = arguments[1];
  9816. this._inputGeom = inputGeom;
  9817. this._isConvex = isConvex;
  9818. }
  9819. }
  9820. static nextIndex(pts, index) {
  9821. index++;
  9822. if (index >= pts.length) index = 0;
  9823. return index;
  9824. }
  9825. static computeC(a, b, p) {
  9826. return a * p.y - b * p.x;
  9827. }
  9828. static getMinimumDiameter(geom) {
  9829. return new MinimumDiameter(geom).getDiameter();
  9830. }
  9831. static getMinimumRectangle(geom) {
  9832. return new MinimumDiameter(geom).getMinimumRectangle();
  9833. }
  9834. static computeSegmentForLine(a, b, c) {
  9835. let p0 = null;
  9836. let p1 = null;
  9837. if (Math.abs(b) > Math.abs(a)) {
  9838. p0 = new Coordinate(0.0, c / b);
  9839. p1 = new Coordinate(1.0, c / b - a / b);
  9840. } else {
  9841. p0 = new Coordinate(c / a, 0.0);
  9842. p1 = new Coordinate(c / a - b / a, 1.0);
  9843. }
  9844. return new LineSegment(p0, p1);
  9845. }
  9846. getWidthCoordinate() {
  9847. this.computeMinimumDiameter();
  9848. return this._minWidthPt;
  9849. }
  9850. getSupportingSegment() {
  9851. this.computeMinimumDiameter();
  9852. return this._inputGeom.getFactory().createLineString([this._minBaseSeg.p0, this._minBaseSeg.p1]);
  9853. }
  9854. getDiameter() {
  9855. this.computeMinimumDiameter();
  9856. if (this._minWidthPt === null) return this._inputGeom.getFactory().createLineString();
  9857. const basePt = this._minBaseSeg.project(this._minWidthPt);
  9858. return this._inputGeom.getFactory().createLineString([basePt, this._minWidthPt]);
  9859. }
  9860. computeWidthConvex(convexGeom) {
  9861. if (convexGeom instanceof Polygon) this._convexHullPts = convexGeom.getExteriorRing().getCoordinates();else this._convexHullPts = convexGeom.getCoordinates();
  9862. if (this._convexHullPts.length === 0) {
  9863. this._minWidth = 0.0;
  9864. this._minWidthPt = null;
  9865. this._minBaseSeg = null;
  9866. } else if (this._convexHullPts.length === 1) {
  9867. this._minWidth = 0.0;
  9868. this._minWidthPt = this._convexHullPts[0];
  9869. this._minBaseSeg.p0 = this._convexHullPts[0];
  9870. this._minBaseSeg.p1 = this._convexHullPts[0];
  9871. } else if (this._convexHullPts.length === 2 || this._convexHullPts.length === 3) {
  9872. this._minWidth = 0.0;
  9873. this._minWidthPt = this._convexHullPts[0];
  9874. this._minBaseSeg.p0 = this._convexHullPts[0];
  9875. this._minBaseSeg.p1 = this._convexHullPts[1];
  9876. } else {
  9877. this.computeConvexRingMinDiameter(this._convexHullPts);
  9878. }
  9879. }
  9880. computeConvexRingMinDiameter(pts) {
  9881. this._minWidth = Double.MAX_VALUE;
  9882. let currMaxIndex = 1;
  9883. const seg = new LineSegment();
  9884. for (let i = 0; i < pts.length - 1; i++) {
  9885. seg.p0 = pts[i];
  9886. seg.p1 = pts[i + 1];
  9887. currMaxIndex = this.findMaxPerpDistance(pts, seg, currMaxIndex);
  9888. }
  9889. }
  9890. computeMinimumDiameter() {
  9891. if (this._minWidthPt !== null) return null;
  9892. if (this._isConvex) {
  9893. this.computeWidthConvex(this._inputGeom);
  9894. } else {
  9895. const convexGeom = new ConvexHull(this._inputGeom).getConvexHull();
  9896. this.computeWidthConvex(convexGeom);
  9897. }
  9898. }
  9899. getLength() {
  9900. this.computeMinimumDiameter();
  9901. return this._minWidth;
  9902. }
  9903. findMaxPerpDistance(pts, seg, startIndex) {
  9904. let maxPerpDistance = seg.distancePerpendicular(pts[startIndex]);
  9905. let nextPerpDistance = maxPerpDistance;
  9906. let maxIndex = startIndex;
  9907. let nextIndex = maxIndex;
  9908. while (nextPerpDistance >= maxPerpDistance) {
  9909. maxPerpDistance = nextPerpDistance;
  9910. maxIndex = nextIndex;
  9911. nextIndex = MinimumDiameter.nextIndex(pts, maxIndex);
  9912. nextPerpDistance = seg.distancePerpendicular(pts[nextIndex]);
  9913. }
  9914. if (maxPerpDistance < this._minWidth) {
  9915. this._minPtIndex = maxIndex;
  9916. this._minWidth = maxPerpDistance;
  9917. this._minWidthPt = pts[this._minPtIndex];
  9918. this._minBaseSeg = new LineSegment(seg);
  9919. }
  9920. return maxIndex;
  9921. }
  9922. getMinimumRectangle() {
  9923. this.computeMinimumDiameter();
  9924. if (this._minWidth === 0.0) {
  9925. if (this._minBaseSeg.p0.equals2D(this._minBaseSeg.p1)) return this._inputGeom.getFactory().createPoint(this._minBaseSeg.p0);
  9926. return this._minBaseSeg.toGeometry(this._inputGeom.getFactory());
  9927. }
  9928. const dx = this._minBaseSeg.p1.x - this._minBaseSeg.p0.x;
  9929. const dy = this._minBaseSeg.p1.y - this._minBaseSeg.p0.y;
  9930. let minPara = Double.MAX_VALUE;
  9931. let maxPara = -Double.MAX_VALUE;
  9932. let minPerp = Double.MAX_VALUE;
  9933. let maxPerp = -Double.MAX_VALUE;
  9934. for (let i = 0; i < this._convexHullPts.length; i++) {
  9935. const paraC = MinimumDiameter.computeC(dx, dy, this._convexHullPts[i]);
  9936. if (paraC > maxPara) maxPara = paraC;
  9937. if (paraC < minPara) minPara = paraC;
  9938. const perpC = MinimumDiameter.computeC(-dy, dx, this._convexHullPts[i]);
  9939. if (perpC > maxPerp) maxPerp = perpC;
  9940. if (perpC < minPerp) minPerp = perpC;
  9941. }
  9942. const maxPerpLine = MinimumDiameter.computeSegmentForLine(-dx, -dy, maxPerp);
  9943. const minPerpLine = MinimumDiameter.computeSegmentForLine(-dx, -dy, minPerp);
  9944. const maxParaLine = MinimumDiameter.computeSegmentForLine(-dy, dx, maxPara);
  9945. const minParaLine = MinimumDiameter.computeSegmentForLine(-dy, dx, minPara);
  9946. const p0 = maxParaLine.lineIntersection(maxPerpLine);
  9947. const p1 = minParaLine.lineIntersection(maxPerpLine);
  9948. const p2 = minParaLine.lineIntersection(minPerpLine);
  9949. const p3 = maxParaLine.lineIntersection(minPerpLine);
  9950. const shell = this._inputGeom.getFactory().createLinearRing([p0, p1, p2, p3, p0]);
  9951. return this._inputGeom.getFactory().createPolygon(shell);
  9952. }
  9953. }
  9954. var algorithm = /*#__PURE__*/Object.freeze({
  9955. __proto__: null,
  9956. distance: distance_module,
  9957. locate: locate,
  9958. match: match,
  9959. Angle: Angle,
  9960. Area: Area,
  9961. Centroid: Centroid,
  9962. ConvexHull: ConvexHull,
  9963. Distance: Distance,
  9964. InteriorPointArea: InteriorPointArea,
  9965. InteriorPointLine: InteriorPointLine,
  9966. InteriorPointPoint: InteriorPointPoint,
  9967. Length: Length,
  9968. Orientation: Orientation,
  9969. PointLocation: PointLocation,
  9970. PointLocator: PointLocator,
  9971. RobustLineIntersector: RobustLineIntersector,
  9972. MinimumBoundingCircle: MinimumBoundingCircle,
  9973. MinimumDiameter: MinimumDiameter
  9974. });
  9975. class Densifier {
  9976. constructor() {
  9977. Densifier.constructor_.apply(this, arguments);
  9978. }
  9979. static constructor_() {
  9980. this._inputGeom = null;
  9981. this._distanceTolerance = null;
  9982. const inputGeom = arguments[0];
  9983. this._inputGeom = inputGeom;
  9984. }
  9985. static densifyPoints(pts, distanceTolerance, precModel) {
  9986. const seg = new LineSegment();
  9987. const coordList = new CoordinateList();
  9988. for (let i = 0; i < pts.length - 1; i++) {
  9989. seg.p0 = pts[i];
  9990. seg.p1 = pts[i + 1];
  9991. coordList.add(seg.p0, false);
  9992. const len = seg.getLength();
  9993. const densifiedSegCount = Math.trunc(len / distanceTolerance) + 1;
  9994. if (densifiedSegCount > 1) {
  9995. const densifiedSegLen = len / densifiedSegCount;
  9996. for (let j = 1; j < densifiedSegCount; j++) {
  9997. const segFract = j * densifiedSegLen / len;
  9998. const p = seg.pointAlong(segFract);
  9999. precModel.makePrecise(p);
  10000. coordList.add(p, false);
  10001. }
  10002. }
  10003. }
  10004. coordList.add(pts[pts.length - 1], false);
  10005. return coordList.toCoordinateArray();
  10006. }
  10007. static densify(geom, distanceTolerance) {
  10008. const densifier = new Densifier(geom);
  10009. densifier.setDistanceTolerance(distanceTolerance);
  10010. return densifier.getResultGeometry();
  10011. }
  10012. getResultGeometry() {
  10013. return new DensifyTransformer(this._distanceTolerance).transform(this._inputGeom);
  10014. }
  10015. setDistanceTolerance(distanceTolerance) {
  10016. if (distanceTolerance <= 0.0) throw new IllegalArgumentException('Tolerance must be positive');
  10017. this._distanceTolerance = distanceTolerance;
  10018. }
  10019. }
  10020. class DensifyTransformer extends GeometryTransformer {
  10021. constructor() {
  10022. super();
  10023. DensifyTransformer.constructor_.apply(this, arguments);
  10024. }
  10025. static constructor_() {
  10026. this.distanceTolerance = null;
  10027. const distanceTolerance = arguments[0];
  10028. this.distanceTolerance = distanceTolerance;
  10029. }
  10030. transformMultiPolygon(geom, parent) {
  10031. const roughGeom = super.transformMultiPolygon.call(this, geom, parent);
  10032. return this.createValidArea(roughGeom);
  10033. }
  10034. transformPolygon(geom, parent) {
  10035. const roughGeom = super.transformPolygon.call(this, geom, parent);
  10036. if (parent instanceof MultiPolygon) return roughGeom;
  10037. return this.createValidArea(roughGeom);
  10038. }
  10039. transformCoordinates(coords, parent) {
  10040. const inputPts = coords.toCoordinateArray();
  10041. let newPts = Densifier.densifyPoints(inputPts, this.distanceTolerance, parent.getPrecisionModel());
  10042. if (parent instanceof LineString && newPts.length === 1) newPts = new Array(0).fill(null);
  10043. return this._factory.getCoordinateSequenceFactory().create(newPts);
  10044. }
  10045. createValidArea(roughAreaGeom) {
  10046. return roughAreaGeom.buffer(0.0);
  10047. }
  10048. }
  10049. Densifier.DensifyTransformer = DensifyTransformer;
  10050. var densify = /*#__PURE__*/Object.freeze({
  10051. __proto__: null,
  10052. Densifier: Densifier
  10053. });
  10054. class Quadrant {
  10055. static isNorthern(quad) {
  10056. return quad === Quadrant.NE || quad === Quadrant.NW;
  10057. }
  10058. static isOpposite(quad1, quad2) {
  10059. if (quad1 === quad2) return false;
  10060. const diff = (quad1 - quad2 + 4) % 4;
  10061. if (diff === 2) return true;
  10062. return false;
  10063. }
  10064. static commonHalfPlane(quad1, quad2) {
  10065. if (quad1 === quad2) return quad1;
  10066. const diff = (quad1 - quad2 + 4) % 4;
  10067. if (diff === 2) return -1;
  10068. const min = quad1 < quad2 ? quad1 : quad2;
  10069. const max = quad1 > quad2 ? quad1 : quad2;
  10070. if (min === 0 && max === 3) return 3;
  10071. return min;
  10072. }
  10073. static isInHalfPlane(quad, halfPlane) {
  10074. if (halfPlane === Quadrant.SE) return quad === Quadrant.SE || quad === Quadrant.SW;
  10075. return quad === halfPlane || quad === halfPlane + 1;
  10076. }
  10077. static quadrant() {
  10078. if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  10079. const dx = arguments[0],
  10080. dy = arguments[1];
  10081. if (dx === 0.0 && dy === 0.0) throw new IllegalArgumentException('Cannot compute the quadrant for point ( ' + dx + ', ' + dy + ' )');
  10082. if (dx >= 0.0) {
  10083. if (dy >= 0.0) return Quadrant.NE;else return Quadrant.SE;
  10084. } else if (dy >= 0.0) return Quadrant.NW;else return Quadrant.SW;
  10085. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  10086. const p0 = arguments[0],
  10087. p1 = arguments[1];
  10088. if (p1.x === p0.x && p1.y === p0.y) throw new IllegalArgumentException('Cannot compute the quadrant for two identical points ' + p0);
  10089. if (p1.x >= p0.x) {
  10090. if (p1.y >= p0.y) return Quadrant.NE;else return Quadrant.SE;
  10091. } else if (p1.y >= p0.y) return Quadrant.NW;else return Quadrant.SW;
  10092. }
  10093. }
  10094. }
  10095. Quadrant.NE = 0;
  10096. Quadrant.NW = 1;
  10097. Quadrant.SW = 2;
  10098. Quadrant.SE = 3;
  10099. class HalfEdge {
  10100. constructor() {
  10101. HalfEdge.constructor_.apply(this, arguments);
  10102. }
  10103. static constructor_() {
  10104. this._orig = null;
  10105. this._sym = null;
  10106. this._next = null;
  10107. const orig = arguments[0];
  10108. this._orig = orig;
  10109. }
  10110. static create(p0, p1) {
  10111. const e0 = new HalfEdge(p0);
  10112. const e1 = new HalfEdge(p1);
  10113. e0.link(e1);
  10114. return e0;
  10115. }
  10116. find(dest) {
  10117. let oNext = this;
  10118. do {
  10119. if (oNext === null) return null;
  10120. if (oNext.dest().equals2D(dest)) return oNext;
  10121. oNext = oNext.oNext();
  10122. } while (oNext !== this);
  10123. return null;
  10124. }
  10125. dest() {
  10126. return this._sym._orig;
  10127. }
  10128. isEdgesSorted() {
  10129. const lowest = this.findLowest();
  10130. let e = lowest;
  10131. do {
  10132. const eNext = e.oNext();
  10133. if (eNext === lowest) break;
  10134. const isSorted = eNext.compareTo(e) > 0;
  10135. if (!isSorted) return false;
  10136. e = eNext;
  10137. } while (e !== lowest);
  10138. return true;
  10139. }
  10140. oNext() {
  10141. return this._sym._next;
  10142. }
  10143. directionY() {
  10144. return this.directionPt().getY() - this._orig.getY();
  10145. }
  10146. insert(eAdd) {
  10147. if (this.oNext() === this) {
  10148. this.insertAfter(eAdd);
  10149. return null;
  10150. }
  10151. const ePrev = this.insertionEdge(eAdd);
  10152. ePrev.insertAfter(eAdd);
  10153. }
  10154. insertAfter(e) {
  10155. Assert.equals(this._orig, e.orig());
  10156. const save = this.oNext();
  10157. this._sym.setNext(e);
  10158. e.sym().setNext(save);
  10159. }
  10160. degree() {
  10161. let degree = 0;
  10162. let e = this;
  10163. do {
  10164. degree++;
  10165. e = e.oNext();
  10166. } while (e !== this);
  10167. return degree;
  10168. }
  10169. equals() {
  10170. if (arguments.length === 2 && arguments[1] instanceof Coordinate && arguments[0] instanceof Coordinate) {
  10171. const p0 = arguments[0],
  10172. p1 = arguments[1];
  10173. return this._orig.equals2D(p0) && this._sym._orig.equals(p1);
  10174. }
  10175. }
  10176. findLowest() {
  10177. let lowest = this;
  10178. let e = this.oNext();
  10179. do {
  10180. if (e.compareTo(lowest) < 0) lowest = e;
  10181. e = e.oNext();
  10182. } while (e !== this);
  10183. return lowest;
  10184. }
  10185. directionPt() {
  10186. return this.dest();
  10187. }
  10188. sym() {
  10189. return this._sym;
  10190. }
  10191. prev() {
  10192. return this._sym.next()._sym;
  10193. }
  10194. compareAngularDirection(e) {
  10195. const dx = this.directionX();
  10196. const dy = this.directionY();
  10197. const dx2 = e.directionX();
  10198. const dy2 = e.directionY();
  10199. if (dx === dx2 && dy === dy2) return 0;
  10200. const quadrant = Quadrant.quadrant(dx, dy);
  10201. const quadrant2 = Quadrant.quadrant(dx2, dy2);
  10202. if (quadrant > quadrant2) return 1;
  10203. if (quadrant < quadrant2) return -1;
  10204. const dir1 = this.directionPt();
  10205. const dir2 = e.directionPt();
  10206. return Orientation.index(e._orig, dir2, dir1);
  10207. }
  10208. prevNode() {
  10209. let e = this;
  10210. while (e.degree() === 2) {
  10211. e = e.prev();
  10212. if (e === this) return null;
  10213. }
  10214. return e;
  10215. }
  10216. directionX() {
  10217. return this.directionPt().getX() - this._orig.getX();
  10218. }
  10219. insertionEdge(eAdd) {
  10220. let ePrev = this;
  10221. do {
  10222. const eNext = ePrev.oNext();
  10223. if (eNext.compareTo(ePrev) > 0 && eAdd.compareTo(ePrev) >= 0 && eAdd.compareTo(eNext) <= 0) return ePrev;
  10224. if (eNext.compareTo(ePrev) <= 0 && (eAdd.compareTo(eNext) <= 0 || eAdd.compareTo(ePrev) >= 0)) return ePrev;
  10225. ePrev = eNext;
  10226. } while (ePrev !== this);
  10227. Assert.shouldNeverReachHere();
  10228. return null;
  10229. }
  10230. compareTo(obj) {
  10231. const e = obj;
  10232. const comp = this.compareAngularDirection(e);
  10233. return comp;
  10234. }
  10235. toStringNode() {
  10236. const orig = this.orig();
  10237. this.dest();
  10238. const sb = new StringBuilder();
  10239. sb.append('Node( ' + WKTWriter.format(orig) + ' )' + '\n');
  10240. let e = this;
  10241. do {
  10242. sb.append(' -> ' + e);
  10243. sb.append('\n');
  10244. e = e.oNext();
  10245. } while (e !== this);
  10246. return sb.toString();
  10247. }
  10248. link(sym) {
  10249. this.setSym(sym);
  10250. sym.setSym(this);
  10251. this.setNext(sym);
  10252. sym.setNext(this);
  10253. }
  10254. next() {
  10255. return this._next;
  10256. }
  10257. setSym(e) {
  10258. this._sym = e;
  10259. }
  10260. orig() {
  10261. return this._orig;
  10262. }
  10263. toString() {
  10264. return 'HE(' + this._orig.x + ' ' + this._orig.y + ', ' + this._sym._orig.x + ' ' + this._sym._orig.y + ')';
  10265. }
  10266. toStringNodeEdge() {
  10267. return ' -> (' + WKTWriter.format(this.dest());
  10268. }
  10269. setNext(e) {
  10270. this._next = e;
  10271. }
  10272. }
  10273. class MarkHalfEdge extends HalfEdge {
  10274. constructor() {
  10275. super();
  10276. MarkHalfEdge.constructor_.apply(this, arguments);
  10277. }
  10278. static constructor_() {
  10279. this._isMarked = false;
  10280. const orig = arguments[0];
  10281. HalfEdge.constructor_.call(this, orig);
  10282. }
  10283. static setMarkBoth(e, isMarked) {
  10284. e.setMark(isMarked);
  10285. e.sym().setMark(isMarked);
  10286. }
  10287. static isMarked(e) {
  10288. return e.isMarked();
  10289. }
  10290. static setMark(e, isMarked) {
  10291. e.setMark(isMarked);
  10292. }
  10293. static markBoth(e) {
  10294. e.mark();
  10295. e.sym().mark();
  10296. }
  10297. static mark(e) {
  10298. e.mark();
  10299. }
  10300. mark() {
  10301. this._isMarked = true;
  10302. }
  10303. setMark(isMarked) {
  10304. this._isMarked = isMarked;
  10305. }
  10306. isMarked() {
  10307. return this._isMarked;
  10308. }
  10309. }
  10310. class EdgeGraph {
  10311. constructor() {
  10312. EdgeGraph.constructor_.apply(this, arguments);
  10313. }
  10314. static constructor_() {
  10315. this._vertexMap = new HashMap();
  10316. }
  10317. static isValidEdge(orig, dest) {
  10318. const cmp = dest.compareTo(orig);
  10319. return cmp !== 0;
  10320. }
  10321. insert(orig, dest, eAdj) {
  10322. const e = this.create(orig, dest);
  10323. if (eAdj !== null) eAdj.insert(e);else this._vertexMap.put(orig, e);
  10324. const eAdjDest = this._vertexMap.get(dest);
  10325. if (eAdjDest !== null) eAdjDest.insert(e.sym());else this._vertexMap.put(dest, e.sym());
  10326. return e;
  10327. }
  10328. create(p0, p1) {
  10329. const e0 = this.createEdge(p0);
  10330. const e1 = this.createEdge(p1);
  10331. e0.link(e1);
  10332. return e0;
  10333. }
  10334. createEdge(orig) {
  10335. return new HalfEdge(orig);
  10336. }
  10337. addEdge(orig, dest) {
  10338. if (!EdgeGraph.isValidEdge(orig, dest)) return null;
  10339. const eAdj = this._vertexMap.get(orig);
  10340. let eSame = null;
  10341. if (eAdj !== null) eSame = eAdj.find(dest);
  10342. if (eSame !== null) return eSame;
  10343. const e = this.insert(orig, dest, eAdj);
  10344. return e;
  10345. }
  10346. getVertexEdges() {
  10347. return this._vertexMap.values();
  10348. }
  10349. findEdge(orig, dest) {
  10350. const e = this._vertexMap.get(orig);
  10351. if (e === null) return null;
  10352. return e.find(dest);
  10353. }
  10354. }
  10355. class DissolveHalfEdge extends MarkHalfEdge {
  10356. constructor() {
  10357. super();
  10358. DissolveHalfEdge.constructor_.apply(this, arguments);
  10359. }
  10360. static constructor_() {
  10361. this._isStart = false;
  10362. const orig = arguments[0];
  10363. MarkHalfEdge.constructor_.call(this, orig);
  10364. }
  10365. setStart() {
  10366. this._isStart = true;
  10367. }
  10368. isStart() {
  10369. return this._isStart;
  10370. }
  10371. }
  10372. class DissolveEdgeGraph extends EdgeGraph {
  10373. constructor() {
  10374. super();
  10375. }
  10376. createEdge(p0) {
  10377. return new DissolveHalfEdge(p0);
  10378. }
  10379. }
  10380. class LineDissolver {
  10381. constructor() {
  10382. LineDissolver.constructor_.apply(this, arguments);
  10383. }
  10384. static constructor_() {
  10385. this._result = null;
  10386. this._factory = null;
  10387. this._graph = null;
  10388. this._lines = new ArrayList();
  10389. this._nodeEdgeStack = new Stack();
  10390. this._ringStartEdge = null;
  10391. this._graph = new DissolveEdgeGraph();
  10392. }
  10393. static dissolve(g) {
  10394. const d = new LineDissolver();
  10395. d.add(g);
  10396. return d.getResult();
  10397. }
  10398. addLine(line) {
  10399. this._lines.add(this._factory.createLineString(line.toCoordinateArray()));
  10400. }
  10401. updateRingStartEdge(e) {
  10402. if (!e.isStart()) {
  10403. e = e.sym();
  10404. if (!e.isStart()) return null;
  10405. }
  10406. if (this._ringStartEdge === null) {
  10407. this._ringStartEdge = e;
  10408. return null;
  10409. }
  10410. if (e.orig().compareTo(this._ringStartEdge.orig()) < 0) this._ringStartEdge = e;
  10411. }
  10412. getResult() {
  10413. if (this._result === null) this.computeResult();
  10414. return this._result;
  10415. }
  10416. process(e) {
  10417. let eNode = e.prevNode();
  10418. if (eNode === null) eNode = e;
  10419. this.stackEdges(eNode);
  10420. this.buildLines();
  10421. }
  10422. buildRing(eStartRing) {
  10423. const line = new CoordinateList();
  10424. let e = eStartRing;
  10425. line.add(e.orig().copy(), false);
  10426. while (e.sym().degree() === 2) {
  10427. const eNext = e.next();
  10428. if (eNext === eStartRing) break;
  10429. line.add(eNext.orig().copy(), false);
  10430. e = eNext;
  10431. }
  10432. line.add(e.dest().copy(), false);
  10433. this.addLine(line);
  10434. }
  10435. buildLine(eStart) {
  10436. const line = new CoordinateList();
  10437. let e = eStart;
  10438. this._ringStartEdge = null;
  10439. MarkHalfEdge.markBoth(e);
  10440. line.add(e.orig().copy(), false);
  10441. while (e.sym().degree() === 2) {
  10442. this.updateRingStartEdge(e);
  10443. const eNext = e.next();
  10444. if (eNext === eStart) {
  10445. this.buildRing(this._ringStartEdge);
  10446. return null;
  10447. }
  10448. line.add(eNext.orig().copy(), false);
  10449. e = eNext;
  10450. MarkHalfEdge.markBoth(e);
  10451. }
  10452. line.add(e.dest().clone(), false);
  10453. this.stackEdges(e.sym());
  10454. this.addLine(line);
  10455. }
  10456. stackEdges(node) {
  10457. let e = node;
  10458. do {
  10459. if (!MarkHalfEdge.isMarked(e)) this._nodeEdgeStack.add(e);
  10460. e = e.oNext();
  10461. } while (e !== node);
  10462. }
  10463. computeResult() {
  10464. const edges = this._graph.getVertexEdges();
  10465. for (let i = edges.iterator(); i.hasNext();) {
  10466. const e = i.next();
  10467. if (MarkHalfEdge.isMarked(e)) continue;
  10468. this.process(e);
  10469. }
  10470. this._result = this._factory.buildGeometry(this._lines);
  10471. }
  10472. buildLines() {
  10473. while (!this._nodeEdgeStack.empty()) {
  10474. const e = this._nodeEdgeStack.pop();
  10475. if (MarkHalfEdge.isMarked(e)) continue;
  10476. this.buildLine(e);
  10477. }
  10478. }
  10479. add() {
  10480. if (arguments[0] instanceof Geometry) {
  10481. const geometry = arguments[0];
  10482. geometry.apply(new class {
  10483. get interfaces_() {
  10484. return [GeometryComponentFilter];
  10485. }
  10486. filter(component) {
  10487. if (component instanceof LineString) this.add(component);
  10488. }
  10489. }());
  10490. } else if (hasInterface(arguments[0], Collection)) {
  10491. const geometries = arguments[0];
  10492. for (let i = geometries.iterator(); i.hasNext();) {
  10493. const geometry = i.next();
  10494. this.add(geometry);
  10495. }
  10496. } else if (arguments[0] instanceof LineString) {
  10497. const lineString = arguments[0];
  10498. if (this._factory === null) this._factory = lineString.getFactory();
  10499. const seq = lineString.getCoordinateSequence();
  10500. let doneStart = false;
  10501. for (let i = 1; i < seq.size(); i++) {
  10502. const e = this._graph.addEdge(seq.getCoordinate(i - 1), seq.getCoordinate(i));
  10503. if (e === null) continue;
  10504. if (!doneStart) {
  10505. e.setStart();
  10506. doneStart = true;
  10507. }
  10508. }
  10509. }
  10510. }
  10511. }
  10512. var dissolve = /*#__PURE__*/Object.freeze({
  10513. __proto__: null,
  10514. LineDissolver: LineDissolver
  10515. });
  10516. class Position {
  10517. static opposite(position) {
  10518. if (position === Position.LEFT) return Position.RIGHT;
  10519. if (position === Position.RIGHT) return Position.LEFT;
  10520. return position;
  10521. }
  10522. }
  10523. Position.ON = 0;
  10524. Position.LEFT = 1;
  10525. Position.RIGHT = 2;
  10526. class MonotoneChain$1 {
  10527. constructor() {
  10528. MonotoneChain$1.constructor_.apply(this, arguments);
  10529. }
  10530. static constructor_() {
  10531. this.mce = null;
  10532. this.chainIndex = null;
  10533. const mce = arguments[0],
  10534. chainIndex = arguments[1];
  10535. this.mce = mce;
  10536. this.chainIndex = chainIndex;
  10537. }
  10538. computeIntersections(mc, si) {
  10539. this.mce.computeIntersectsForChain(this.chainIndex, mc.mce, mc.chainIndex, si);
  10540. }
  10541. }
  10542. class SweepLineEvent {
  10543. constructor() {
  10544. SweepLineEvent.constructor_.apply(this, arguments);
  10545. }
  10546. static constructor_() {
  10547. this._label = null;
  10548. this._xValue = null;
  10549. this._eventType = null;
  10550. this._insertEvent = null;
  10551. this._deleteEventIndex = null;
  10552. this._obj = null;
  10553. if (arguments.length === 2) {
  10554. const x = arguments[0],
  10555. insertEvent = arguments[1];
  10556. this._eventType = SweepLineEvent.DELETE;
  10557. this._xValue = x;
  10558. this._insertEvent = insertEvent;
  10559. } else if (arguments.length === 3) {
  10560. const label = arguments[0],
  10561. x = arguments[1],
  10562. obj = arguments[2];
  10563. this._eventType = SweepLineEvent.INSERT;
  10564. this._label = label;
  10565. this._xValue = x;
  10566. this._obj = obj;
  10567. }
  10568. }
  10569. isDelete() {
  10570. return this._eventType === SweepLineEvent.DELETE;
  10571. }
  10572. setDeleteEventIndex(deleteEventIndex) {
  10573. this._deleteEventIndex = deleteEventIndex;
  10574. }
  10575. getObject() {
  10576. return this._obj;
  10577. }
  10578. compareTo(o) {
  10579. const pe = o;
  10580. if (this._xValue < pe._xValue) return -1;
  10581. if (this._xValue > pe._xValue) return 1;
  10582. if (this._eventType < pe._eventType) return -1;
  10583. if (this._eventType > pe._eventType) return 1;
  10584. return 0;
  10585. }
  10586. getInsertEvent() {
  10587. return this._insertEvent;
  10588. }
  10589. isInsert() {
  10590. return this._eventType === SweepLineEvent.INSERT;
  10591. }
  10592. isSameLabel(ev) {
  10593. if (this._label === null) return false;
  10594. return this._label === ev._label;
  10595. }
  10596. getDeleteEventIndex() {
  10597. return this._deleteEventIndex;
  10598. }
  10599. get interfaces_() {
  10600. return [Comparable];
  10601. }
  10602. }
  10603. SweepLineEvent.INSERT = 1;
  10604. SweepLineEvent.DELETE = 2;
  10605. class EdgeSetIntersector {}
  10606. class SegmentIntersector$1 {
  10607. constructor() {
  10608. SegmentIntersector$1.constructor_.apply(this, arguments);
  10609. }
  10610. static constructor_() {
  10611. this._hasIntersection = false;
  10612. this._hasProper = false;
  10613. this._hasProperInterior = false;
  10614. this._properIntersectionPoint = null;
  10615. this._li = null;
  10616. this._includeProper = null;
  10617. this._recordIsolated = null;
  10618. this._isSelfIntersection = null;
  10619. this._numIntersections = 0;
  10620. this.numTests = 0;
  10621. this._bdyNodes = null;
  10622. this._isDone = false;
  10623. this._isDoneWhenProperInt = false;
  10624. const li = arguments[0],
  10625. includeProper = arguments[1],
  10626. recordIsolated = arguments[2];
  10627. this._li = li;
  10628. this._includeProper = includeProper;
  10629. this._recordIsolated = recordIsolated;
  10630. }
  10631. static isAdjacentSegments(i1, i2) {
  10632. return Math.abs(i1 - i2) === 1;
  10633. }
  10634. isTrivialIntersection(e0, segIndex0, e1, segIndex1) {
  10635. if (e0 === e1) if (this._li.getIntersectionNum() === 1) {
  10636. if (SegmentIntersector$1.isAdjacentSegments(segIndex0, segIndex1)) return true;
  10637. if (e0.isClosed()) {
  10638. const maxSegIndex = e0.getNumPoints() - 1;
  10639. if (segIndex0 === 0 && segIndex1 === maxSegIndex || segIndex1 === 0 && segIndex0 === maxSegIndex) return true;
  10640. }
  10641. }
  10642. return false;
  10643. }
  10644. getProperIntersectionPoint() {
  10645. return this._properIntersectionPoint;
  10646. }
  10647. setIsDoneIfProperInt(isDoneWhenProperInt) {
  10648. this._isDoneWhenProperInt = isDoneWhenProperInt;
  10649. }
  10650. hasProperInteriorIntersection() {
  10651. return this._hasProperInterior;
  10652. }
  10653. isBoundaryPointInternal(li, bdyNodes) {
  10654. for (let i = bdyNodes.iterator(); i.hasNext();) {
  10655. const node = i.next();
  10656. const pt = node.getCoordinate();
  10657. if (li.isIntersection(pt)) return true;
  10658. }
  10659. return false;
  10660. }
  10661. hasProperIntersection() {
  10662. return this._hasProper;
  10663. }
  10664. hasIntersection() {
  10665. return this._hasIntersection;
  10666. }
  10667. isDone() {
  10668. return this._isDone;
  10669. }
  10670. isBoundaryPoint(li, bdyNodes) {
  10671. if (bdyNodes === null) return false;
  10672. if (this.isBoundaryPointInternal(li, bdyNodes[0])) return true;
  10673. if (this.isBoundaryPointInternal(li, bdyNodes[1])) return true;
  10674. return false;
  10675. }
  10676. setBoundaryNodes(bdyNodes0, bdyNodes1) {
  10677. this._bdyNodes = new Array(2).fill(null);
  10678. this._bdyNodes[0] = bdyNodes0;
  10679. this._bdyNodes[1] = bdyNodes1;
  10680. }
  10681. addIntersections(e0, segIndex0, e1, segIndex1) {
  10682. if (e0 === e1 && segIndex0 === segIndex1) return null;
  10683. this.numTests++;
  10684. const p00 = e0.getCoordinates()[segIndex0];
  10685. const p01 = e0.getCoordinates()[segIndex0 + 1];
  10686. const p10 = e1.getCoordinates()[segIndex1];
  10687. const p11 = e1.getCoordinates()[segIndex1 + 1];
  10688. this._li.computeIntersection(p00, p01, p10, p11);
  10689. if (this._li.hasIntersection()) {
  10690. if (this._recordIsolated) {
  10691. e0.setIsolated(false);
  10692. e1.setIsolated(false);
  10693. }
  10694. this._numIntersections++;
  10695. if (!this.isTrivialIntersection(e0, segIndex0, e1, segIndex1)) {
  10696. this._hasIntersection = true;
  10697. if (this._includeProper || !this._li.isProper()) {
  10698. e0.addIntersections(this._li, segIndex0, 0);
  10699. e1.addIntersections(this._li, segIndex1, 1);
  10700. }
  10701. if (this._li.isProper()) {
  10702. this._properIntersectionPoint = this._li.getIntersection(0).copy();
  10703. this._hasProper = true;
  10704. if (this._isDoneWhenProperInt) this._isDone = true;
  10705. if (!this.isBoundaryPoint(this._li, this._bdyNodes)) this._hasProperInterior = true;
  10706. }
  10707. }
  10708. }
  10709. }
  10710. }
  10711. class SimpleMCSweepLineIntersector extends EdgeSetIntersector {
  10712. constructor() {
  10713. super();
  10714. SimpleMCSweepLineIntersector.constructor_.apply(this, arguments);
  10715. }
  10716. static constructor_() {
  10717. this.events = new ArrayList();
  10718. this.nOverlaps = null;
  10719. }
  10720. prepareEvents() {
  10721. Collections.sort(this.events);
  10722. for (let i = 0; i < this.events.size(); i++) {
  10723. const ev = this.events.get(i);
  10724. if (ev.isDelete()) ev.getInsertEvent().setDeleteEventIndex(i);
  10725. }
  10726. }
  10727. computeIntersections() {
  10728. if (arguments.length === 1) {
  10729. const si = arguments[0];
  10730. this.nOverlaps = 0;
  10731. this.prepareEvents();
  10732. for (let i = 0; i < this.events.size(); i++) {
  10733. const ev = this.events.get(i);
  10734. if (ev.isInsert()) this.processOverlaps(i, ev.getDeleteEventIndex(), ev, si);
  10735. if (si.isDone()) break;
  10736. }
  10737. } else if (arguments.length === 3) {
  10738. if (arguments[2] instanceof SegmentIntersector$1 && hasInterface(arguments[0], List) && hasInterface(arguments[1], List)) {
  10739. const edges0 = arguments[0],
  10740. edges1 = arguments[1],
  10741. si = arguments[2];
  10742. this.addEdges(edges0, edges0);
  10743. this.addEdges(edges1, edges1);
  10744. this.computeIntersections(si);
  10745. } else if (typeof arguments[2] === 'boolean' && hasInterface(arguments[0], List) && arguments[1] instanceof SegmentIntersector$1) {
  10746. const edges = arguments[0],
  10747. si = arguments[1],
  10748. testAllSegments = arguments[2];
  10749. if (testAllSegments) this.addEdges(edges, null);else this.addEdges(edges);
  10750. this.computeIntersections(si);
  10751. }
  10752. }
  10753. }
  10754. addEdge(edge, edgeSet) {
  10755. const mce = edge.getMonotoneChainEdge();
  10756. const startIndex = mce.getStartIndexes();
  10757. for (let i = 0; i < startIndex.length - 1; i++) {
  10758. const mc = new MonotoneChain$1(mce, i);
  10759. const insertEvent = new SweepLineEvent(edgeSet, mce.getMinX(i), mc);
  10760. this.events.add(insertEvent);
  10761. this.events.add(new SweepLineEvent(mce.getMaxX(i), insertEvent));
  10762. }
  10763. }
  10764. processOverlaps(start, end, ev0, si) {
  10765. const mc0 = ev0.getObject();
  10766. for (let i = start; i < end; i++) {
  10767. const ev1 = this.events.get(i);
  10768. if (ev1.isInsert()) {
  10769. const mc1 = ev1.getObject();
  10770. if (!ev0.isSameLabel(ev1)) {
  10771. mc0.computeIntersections(mc1, si);
  10772. this.nOverlaps++;
  10773. }
  10774. }
  10775. }
  10776. }
  10777. addEdges() {
  10778. if (arguments.length === 1) {
  10779. const edges = arguments[0];
  10780. for (let i = edges.iterator(); i.hasNext();) {
  10781. const edge = i.next();
  10782. this.addEdge(edge, edge);
  10783. }
  10784. } else if (arguments.length === 2) {
  10785. const edges = arguments[0],
  10786. edgeSet = arguments[1];
  10787. for (let i = edges.iterator(); i.hasNext();) {
  10788. const edge = i.next();
  10789. this.addEdge(edge, edgeSet);
  10790. }
  10791. }
  10792. }
  10793. }
  10794. class TopologyLocation {
  10795. constructor() {
  10796. TopologyLocation.constructor_.apply(this, arguments);
  10797. }
  10798. static constructor_() {
  10799. this.location = null;
  10800. if (arguments.length === 1) {
  10801. if (arguments[0] instanceof Array) {
  10802. const location = arguments[0];
  10803. this.init(location.length);
  10804. } else if (Number.isInteger(arguments[0])) {
  10805. const on = arguments[0];
  10806. this.init(1);
  10807. this.location[Position.ON] = on;
  10808. } else if (arguments[0] instanceof TopologyLocation) {
  10809. const gl = arguments[0];
  10810. this.init(gl.location.length);
  10811. if (gl !== null) for (let i = 0; i < this.location.length; i++) this.location[i] = gl.location[i];
  10812. }
  10813. } else if (arguments.length === 3) {
  10814. const on = arguments[0],
  10815. left = arguments[1],
  10816. right = arguments[2];
  10817. this.init(3);
  10818. this.location[Position.ON] = on;
  10819. this.location[Position.LEFT] = left;
  10820. this.location[Position.RIGHT] = right;
  10821. }
  10822. }
  10823. setAllLocations(locValue) {
  10824. for (let i = 0; i < this.location.length; i++) this.location[i] = locValue;
  10825. }
  10826. isNull() {
  10827. for (let i = 0; i < this.location.length; i++) if (this.location[i] !== Location.NONE) return false;
  10828. return true;
  10829. }
  10830. setAllLocationsIfNull(locValue) {
  10831. for (let i = 0; i < this.location.length; i++) if (this.location[i] === Location.NONE) this.location[i] = locValue;
  10832. }
  10833. isLine() {
  10834. return this.location.length === 1;
  10835. }
  10836. merge(gl) {
  10837. if (gl.location.length > this.location.length) {
  10838. const newLoc = new Array(3).fill(null);
  10839. newLoc[Position.ON] = this.location[Position.ON];
  10840. newLoc[Position.LEFT] = Location.NONE;
  10841. newLoc[Position.RIGHT] = Location.NONE;
  10842. this.location = newLoc;
  10843. }
  10844. for (let i = 0; i < this.location.length; i++) if (this.location[i] === Location.NONE && i < gl.location.length) this.location[i] = gl.location[i];
  10845. }
  10846. getLocations() {
  10847. return this.location;
  10848. }
  10849. flip() {
  10850. if (this.location.length <= 1) return null;
  10851. const temp = this.location[Position.LEFT];
  10852. this.location[Position.LEFT] = this.location[Position.RIGHT];
  10853. this.location[Position.RIGHT] = temp;
  10854. }
  10855. toString() {
  10856. const buf = new StringBuffer();
  10857. if (this.location.length > 1) buf.append(Location.toLocationSymbol(this.location[Position.LEFT]));
  10858. buf.append(Location.toLocationSymbol(this.location[Position.ON]));
  10859. if (this.location.length > 1) buf.append(Location.toLocationSymbol(this.location[Position.RIGHT]));
  10860. return buf.toString();
  10861. }
  10862. setLocations(on, left, right) {
  10863. this.location[Position.ON] = on;
  10864. this.location[Position.LEFT] = left;
  10865. this.location[Position.RIGHT] = right;
  10866. }
  10867. get(posIndex) {
  10868. if (posIndex < this.location.length) return this.location[posIndex];
  10869. return Location.NONE;
  10870. }
  10871. isArea() {
  10872. return this.location.length > 1;
  10873. }
  10874. isAnyNull() {
  10875. for (let i = 0; i < this.location.length; i++) if (this.location[i] === Location.NONE) return true;
  10876. return false;
  10877. }
  10878. setLocation() {
  10879. if (arguments.length === 1) {
  10880. const locValue = arguments[0];
  10881. this.setLocation(Position.ON, locValue);
  10882. } else if (arguments.length === 2) {
  10883. const locIndex = arguments[0],
  10884. locValue = arguments[1];
  10885. this.location[locIndex] = locValue;
  10886. }
  10887. }
  10888. init(size) {
  10889. this.location = new Array(size).fill(null);
  10890. this.setAllLocations(Location.NONE);
  10891. }
  10892. isEqualOnSide(le, locIndex) {
  10893. return this.location[locIndex] === le.location[locIndex];
  10894. }
  10895. allPositionsEqual(loc) {
  10896. for (let i = 0; i < this.location.length; i++) if (this.location[i] !== loc) return false;
  10897. return true;
  10898. }
  10899. }
  10900. class Label {
  10901. constructor() {
  10902. Label.constructor_.apply(this, arguments);
  10903. }
  10904. static constructor_() {
  10905. this.elt = new Array(2).fill(null);
  10906. if (arguments.length === 1) {
  10907. if (Number.isInteger(arguments[0])) {
  10908. const onLoc = arguments[0];
  10909. this.elt[0] = new TopologyLocation(onLoc);
  10910. this.elt[1] = new TopologyLocation(onLoc);
  10911. } else if (arguments[0] instanceof Label) {
  10912. const lbl = arguments[0];
  10913. this.elt[0] = new TopologyLocation(lbl.elt[0]);
  10914. this.elt[1] = new TopologyLocation(lbl.elt[1]);
  10915. }
  10916. } else if (arguments.length === 2) {
  10917. const geomIndex = arguments[0],
  10918. onLoc = arguments[1];
  10919. this.elt[0] = new TopologyLocation(Location.NONE);
  10920. this.elt[1] = new TopologyLocation(Location.NONE);
  10921. this.elt[geomIndex].setLocation(onLoc);
  10922. } else if (arguments.length === 3) {
  10923. const onLoc = arguments[0],
  10924. leftLoc = arguments[1],
  10925. rightLoc = arguments[2];
  10926. this.elt[0] = new TopologyLocation(onLoc, leftLoc, rightLoc);
  10927. this.elt[1] = new TopologyLocation(onLoc, leftLoc, rightLoc);
  10928. } else if (arguments.length === 4) {
  10929. const geomIndex = arguments[0],
  10930. onLoc = arguments[1],
  10931. leftLoc = arguments[2],
  10932. rightLoc = arguments[3];
  10933. this.elt[0] = new TopologyLocation(Location.NONE, Location.NONE, Location.NONE);
  10934. this.elt[1] = new TopologyLocation(Location.NONE, Location.NONE, Location.NONE);
  10935. this.elt[geomIndex].setLocations(onLoc, leftLoc, rightLoc);
  10936. }
  10937. }
  10938. static toLineLabel(label) {
  10939. const lineLabel = new Label(Location.NONE);
  10940. for (let i = 0; i < 2; i++) lineLabel.setLocation(i, label.getLocation(i));
  10941. return lineLabel;
  10942. }
  10943. getGeometryCount() {
  10944. let count = 0;
  10945. if (!this.elt[0].isNull()) count++;
  10946. if (!this.elt[1].isNull()) count++;
  10947. return count;
  10948. }
  10949. setAllLocations(geomIndex, location) {
  10950. this.elt[geomIndex].setAllLocations(location);
  10951. }
  10952. isNull(geomIndex) {
  10953. return this.elt[geomIndex].isNull();
  10954. }
  10955. setAllLocationsIfNull() {
  10956. if (arguments.length === 1) {
  10957. const location = arguments[0];
  10958. this.setAllLocationsIfNull(0, location);
  10959. this.setAllLocationsIfNull(1, location);
  10960. } else if (arguments.length === 2) {
  10961. const geomIndex = arguments[0],
  10962. location = arguments[1];
  10963. this.elt[geomIndex].setAllLocationsIfNull(location);
  10964. }
  10965. }
  10966. isLine(geomIndex) {
  10967. return this.elt[geomIndex].isLine();
  10968. }
  10969. merge(lbl) {
  10970. for (let i = 0; i < 2; i++) if (this.elt[i] === null && lbl.elt[i] !== null) this.elt[i] = new TopologyLocation(lbl.elt[i]);else this.elt[i].merge(lbl.elt[i]);
  10971. }
  10972. flip() {
  10973. this.elt[0].flip();
  10974. this.elt[1].flip();
  10975. }
  10976. getLocation() {
  10977. if (arguments.length === 1) {
  10978. const geomIndex = arguments[0];
  10979. return this.elt[geomIndex].get(Position.ON);
  10980. } else if (arguments.length === 2) {
  10981. const geomIndex = arguments[0],
  10982. posIndex = arguments[1];
  10983. return this.elt[geomIndex].get(posIndex);
  10984. }
  10985. }
  10986. toString() {
  10987. const buf = new StringBuffer();
  10988. if (this.elt[0] !== null) {
  10989. buf.append('A:');
  10990. buf.append(this.elt[0].toString());
  10991. }
  10992. if (this.elt[1] !== null) {
  10993. buf.append(' B:');
  10994. buf.append(this.elt[1].toString());
  10995. }
  10996. return buf.toString();
  10997. }
  10998. isArea() {
  10999. if (arguments.length === 0) {
  11000. return this.elt[0].isArea() || this.elt[1].isArea();
  11001. } else if (arguments.length === 1) {
  11002. const geomIndex = arguments[0];
  11003. return this.elt[geomIndex].isArea();
  11004. }
  11005. }
  11006. isAnyNull(geomIndex) {
  11007. return this.elt[geomIndex].isAnyNull();
  11008. }
  11009. setLocation() {
  11010. if (arguments.length === 2) {
  11011. const geomIndex = arguments[0],
  11012. location = arguments[1];
  11013. this.elt[geomIndex].setLocation(Position.ON, location);
  11014. } else if (arguments.length === 3) {
  11015. const geomIndex = arguments[0],
  11016. posIndex = arguments[1],
  11017. location = arguments[2];
  11018. this.elt[geomIndex].setLocation(posIndex, location);
  11019. }
  11020. }
  11021. isEqualOnSide(lbl, side) {
  11022. return this.elt[0].isEqualOnSide(lbl.elt[0], side) && this.elt[1].isEqualOnSide(lbl.elt[1], side);
  11023. }
  11024. allPositionsEqual(geomIndex, loc) {
  11025. return this.elt[geomIndex].allPositionsEqual(loc);
  11026. }
  11027. toLine(geomIndex) {
  11028. if (this.elt[geomIndex].isArea()) this.elt[geomIndex] = new TopologyLocation(this.elt[geomIndex].location[0]);
  11029. }
  11030. }
  11031. class EdgeIntersection {
  11032. constructor() {
  11033. EdgeIntersection.constructor_.apply(this, arguments);
  11034. }
  11035. static constructor_() {
  11036. this.coord = null;
  11037. this.segmentIndex = null;
  11038. this.dist = null;
  11039. const coord = arguments[0],
  11040. segmentIndex = arguments[1],
  11041. dist = arguments[2];
  11042. this.coord = new Coordinate(coord);
  11043. this.segmentIndex = segmentIndex;
  11044. this.dist = dist;
  11045. }
  11046. getSegmentIndex() {
  11047. return this.segmentIndex;
  11048. }
  11049. getCoordinate() {
  11050. return this.coord;
  11051. }
  11052. print(out) {
  11053. out.print(this.coord);
  11054. out.print(' seg # = ' + this.segmentIndex);
  11055. out.println(' dist = ' + this.dist);
  11056. }
  11057. compareTo(obj) {
  11058. const other = obj;
  11059. return this.compare(other.segmentIndex, other.dist);
  11060. }
  11061. isEndPoint(maxSegmentIndex) {
  11062. if (this.segmentIndex === 0 && this.dist === 0.0) return true;
  11063. if (this.segmentIndex === maxSegmentIndex) return true;
  11064. return false;
  11065. }
  11066. toString() {
  11067. return this.coord + ' seg # = ' + this.segmentIndex + ' dist = ' + this.dist;
  11068. }
  11069. getDistance() {
  11070. return this.dist;
  11071. }
  11072. compare(segmentIndex, dist) {
  11073. if (this.segmentIndex < segmentIndex) return -1;
  11074. if (this.segmentIndex > segmentIndex) return 1;
  11075. if (this.dist < dist) return -1;
  11076. if (this.dist > dist) return 1;
  11077. return 0;
  11078. }
  11079. get interfaces_() {
  11080. return [Comparable];
  11081. }
  11082. }
  11083. /**
  11084. * @see http://download.oracle.com/javase/6/docs/api/java/util/SortedMap.html
  11085. */
  11086. class SortedMap extends Map$1 {}
  11087. const BLACK = 0;
  11088. const RED = 1;
  11089. function colorOf(p) {
  11090. return p == null ? BLACK : p.color;
  11091. }
  11092. function parentOf(p) {
  11093. return p == null ? null : p.parent;
  11094. }
  11095. function setColor(p, c) {
  11096. if (p !== null) p.color = c;
  11097. }
  11098. function leftOf(p) {
  11099. return p == null ? null : p.left;
  11100. }
  11101. function rightOf(p) {
  11102. return p == null ? null : p.right;
  11103. }
  11104. /**
  11105. * @see http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html
  11106. */
  11107. class TreeMap extends SortedMap {
  11108. constructor() {
  11109. super();
  11110. this.root_ = null;
  11111. this.size_ = 0;
  11112. }
  11113. get(key) {
  11114. let p = this.root_;
  11115. while (p !== null) {
  11116. const cmp = key.compareTo(p.key);
  11117. if (cmp < 0) p = p.left;else if (cmp > 0) p = p.right;else return p.value;
  11118. }
  11119. return null;
  11120. }
  11121. put(key, value) {
  11122. if (this.root_ === null) {
  11123. this.root_ = {
  11124. key: key,
  11125. value: value,
  11126. left: null,
  11127. right: null,
  11128. parent: null,
  11129. color: BLACK,
  11130. getValue() {
  11131. return this.value;
  11132. },
  11133. getKey() {
  11134. return this.key;
  11135. }
  11136. };
  11137. this.size_ = 1;
  11138. return null;
  11139. }
  11140. let t = this.root_;
  11141. let parent;
  11142. let cmp;
  11143. do {
  11144. parent = t;
  11145. cmp = key.compareTo(t.key);
  11146. if (cmp < 0) {
  11147. t = t.left;
  11148. } else if (cmp > 0) {
  11149. t = t.right;
  11150. } else {
  11151. const oldValue = t.value;
  11152. t.value = value;
  11153. return oldValue;
  11154. }
  11155. } while (t !== null);
  11156. const e = {
  11157. key: key,
  11158. left: null,
  11159. right: null,
  11160. value: value,
  11161. parent: parent,
  11162. color: BLACK,
  11163. getValue() {
  11164. return this.value;
  11165. },
  11166. getKey() {
  11167. return this.key;
  11168. }
  11169. };
  11170. if (cmp < 0) parent.left = e;else parent.right = e;
  11171. this.fixAfterInsertion(e);
  11172. this.size_++;
  11173. return null;
  11174. }
  11175. /**
  11176. * @param {Object} x
  11177. */
  11178. fixAfterInsertion(x) {
  11179. let y;
  11180. x.color = RED;
  11181. while (x != null && x !== this.root_ && x.parent.color === RED) if (parentOf(x) === leftOf(parentOf(parentOf(x)))) {
  11182. y = rightOf(parentOf(parentOf(x)));
  11183. if (colorOf(y) === RED) {
  11184. setColor(parentOf(x), BLACK);
  11185. setColor(y, BLACK);
  11186. setColor(parentOf(parentOf(x)), RED);
  11187. x = parentOf(parentOf(x));
  11188. } else {
  11189. if (x === rightOf(parentOf(x))) {
  11190. x = parentOf(x);
  11191. this.rotateLeft(x);
  11192. }
  11193. setColor(parentOf(x), BLACK);
  11194. setColor(parentOf(parentOf(x)), RED);
  11195. this.rotateRight(parentOf(parentOf(x)));
  11196. }
  11197. } else {
  11198. y = leftOf(parentOf(parentOf(x)));
  11199. if (colorOf(y) === RED) {
  11200. setColor(parentOf(x), BLACK);
  11201. setColor(y, BLACK);
  11202. setColor(parentOf(parentOf(x)), RED);
  11203. x = parentOf(parentOf(x));
  11204. } else {
  11205. if (x === leftOf(parentOf(x))) {
  11206. x = parentOf(x);
  11207. this.rotateRight(x);
  11208. }
  11209. setColor(parentOf(x), BLACK);
  11210. setColor(parentOf(parentOf(x)), RED);
  11211. this.rotateLeft(parentOf(parentOf(x)));
  11212. }
  11213. }
  11214. this.root_.color = BLACK;
  11215. }
  11216. values() {
  11217. const arrayList = new ArrayList();
  11218. let p = this.getFirstEntry();
  11219. if (p !== null) {
  11220. arrayList.add(p.value);
  11221. while ((p = TreeMap.successor(p)) !== null) arrayList.add(p.value);
  11222. }
  11223. return arrayList;
  11224. }
  11225. entrySet() {
  11226. const hashSet = new HashSet();
  11227. let p = this.getFirstEntry();
  11228. if (p !== null) {
  11229. hashSet.add(p);
  11230. while ((p = TreeMap.successor(p)) !== null) hashSet.add(p);
  11231. }
  11232. return hashSet;
  11233. }
  11234. /**
  11235. * @param {Object} p
  11236. */
  11237. rotateLeft(p) {
  11238. if (p != null) {
  11239. const r = p.right;
  11240. p.right = r.left;
  11241. if (r.left != null) r.left.parent = p;
  11242. r.parent = p.parent;
  11243. if (p.parent == null) this.root_ = r;else if (p.parent.left === p) p.parent.left = r;else p.parent.right = r;
  11244. r.left = p;
  11245. p.parent = r;
  11246. }
  11247. }
  11248. /**
  11249. * @param {Object} p
  11250. */
  11251. rotateRight(p) {
  11252. if (p != null) {
  11253. const l = p.left;
  11254. p.left = l.right;
  11255. if (l.right != null) l.right.parent = p;
  11256. l.parent = p.parent;
  11257. if (p.parent == null) this.root_ = l;else if (p.parent.right === p) p.parent.right = l;else p.parent.left = l;
  11258. l.right = p;
  11259. p.parent = l;
  11260. }
  11261. }
  11262. /**
  11263. * @return {Object}
  11264. */
  11265. getFirstEntry() {
  11266. let p = this.root_;
  11267. if (p != null) while (p.left != null) p = p.left;
  11268. return p;
  11269. }
  11270. /**
  11271. * @param {Object} t
  11272. * @return {Object}
  11273. * @private
  11274. */
  11275. static successor(t) {
  11276. let p;
  11277. if (t === null) {
  11278. return null;
  11279. } else if (t.right !== null) {
  11280. p = t.right;
  11281. while (p.left !== null) p = p.left;
  11282. return p;
  11283. } else {
  11284. p = t.parent;
  11285. let ch = t;
  11286. while (p !== null && ch === p.right) {
  11287. ch = p;
  11288. p = p.parent;
  11289. }
  11290. return p;
  11291. }
  11292. }
  11293. size() {
  11294. return this.size_;
  11295. }
  11296. containsKey(key) {
  11297. let p = this.root_;
  11298. while (p !== null) {
  11299. const cmp = key.compareTo(p.key);
  11300. if (cmp < 0) p = p.left;else if (cmp > 0) p = p.right;else return true;
  11301. }
  11302. return false;
  11303. }
  11304. }
  11305. class EdgeIntersectionList {
  11306. constructor() {
  11307. EdgeIntersectionList.constructor_.apply(this, arguments);
  11308. }
  11309. static constructor_() {
  11310. this._nodeMap = new TreeMap();
  11311. this.edge = null;
  11312. const edge = arguments[0];
  11313. this.edge = edge;
  11314. }
  11315. print(out) {
  11316. out.println('Intersections:');
  11317. for (let it = this.iterator(); it.hasNext();) {
  11318. const ei = it.next();
  11319. ei.print(out);
  11320. }
  11321. }
  11322. iterator() {
  11323. return this._nodeMap.values().iterator();
  11324. }
  11325. addSplitEdges(edgeList) {
  11326. this.addEndpoints();
  11327. const it = this.iterator();
  11328. let eiPrev = it.next();
  11329. while (it.hasNext()) {
  11330. const ei = it.next();
  11331. const newEdge = this.createSplitEdge(eiPrev, ei);
  11332. edgeList.add(newEdge);
  11333. eiPrev = ei;
  11334. }
  11335. }
  11336. addEndpoints() {
  11337. const maxSegIndex = this.edge.pts.length - 1;
  11338. this.add(this.edge.pts[0], 0, 0.0);
  11339. this.add(this.edge.pts[maxSegIndex], maxSegIndex, 0.0);
  11340. }
  11341. createSplitEdge(ei0, ei1) {
  11342. let npts = ei1.segmentIndex - ei0.segmentIndex + 2;
  11343. const lastSegStartPt = this.edge.pts[ei1.segmentIndex];
  11344. const useIntPt1 = ei1.dist > 0.0 || !ei1.coord.equals2D(lastSegStartPt);
  11345. if (!useIntPt1) npts--;
  11346. const pts = new Array(npts).fill(null);
  11347. let ipt = 0;
  11348. pts[ipt++] = new Coordinate(ei0.coord);
  11349. for (let i = ei0.segmentIndex + 1; i <= ei1.segmentIndex; i++) pts[ipt++] = this.edge.pts[i];
  11350. if (useIntPt1) pts[ipt] = ei1.coord;
  11351. return new Edge$1(pts, new Label(this.edge._label));
  11352. }
  11353. add(intPt, segmentIndex, dist) {
  11354. const eiNew = new EdgeIntersection(intPt, segmentIndex, dist);
  11355. const ei = this._nodeMap.get(eiNew);
  11356. if (ei !== null) return ei;
  11357. this._nodeMap.put(eiNew, eiNew);
  11358. return eiNew;
  11359. }
  11360. isIntersection(pt) {
  11361. for (let it = this.iterator(); it.hasNext();) {
  11362. const ei = it.next();
  11363. if (ei.coord.equals(pt)) return true;
  11364. }
  11365. return false;
  11366. }
  11367. }
  11368. class IntArrayList {
  11369. constructor() {
  11370. IntArrayList.constructor_.apply(this, arguments);
  11371. }
  11372. static constructor_() {
  11373. this._data = null;
  11374. this._size = 0;
  11375. if (arguments.length === 0) {
  11376. IntArrayList.constructor_.call(this, 10);
  11377. } else if (arguments.length === 1) {
  11378. const initialCapacity = arguments[0];
  11379. this._data = new Array(initialCapacity).fill(null);
  11380. }
  11381. }
  11382. size() {
  11383. return this._size;
  11384. }
  11385. addAll(values) {
  11386. if (values === null) return null;
  11387. if (values.length === 0) return null;
  11388. this.ensureCapacity(this._size + values.length);
  11389. System.arraycopy(values, 0, this._data, this._size, values.length);
  11390. this._size += values.length;
  11391. }
  11392. ensureCapacity(capacity) {
  11393. if (capacity <= this._data.length) return null;
  11394. const newLength = Math.max(capacity, this._data.length * 2);
  11395. this._data = Arrays.copyOf(this._data, newLength);
  11396. }
  11397. toArray() {
  11398. const array = new Array(this._size).fill(null);
  11399. System.arraycopy(this._data, 0, array, 0, this._size);
  11400. return array;
  11401. }
  11402. add(value) {
  11403. this.ensureCapacity(this._size + 1);
  11404. this._data[this._size] = value;
  11405. ++this._size;
  11406. }
  11407. }
  11408. class MonotoneChainIndexer {
  11409. static toIntArray(list) {
  11410. const array = new Array(list.size()).fill(null);
  11411. for (let i = 0; i < array.length; i++) array[i] = list.get(i).intValue();
  11412. return array;
  11413. }
  11414. getChainStartIndices(pts) {
  11415. let start = 0;
  11416. const startIndexList = new IntArrayList(Math.trunc(pts.length / 2));
  11417. startIndexList.add(start);
  11418. do {
  11419. const last = this.findChainEnd(pts, start);
  11420. startIndexList.add(last);
  11421. start = last;
  11422. } while (start < pts.length - 1);
  11423. return startIndexList.toArray();
  11424. }
  11425. findChainEnd(pts, start) {
  11426. const chainQuad = Quadrant.quadrant(pts[start], pts[start + 1]);
  11427. let last = start + 1;
  11428. while (last < pts.length) {
  11429. const quad = Quadrant.quadrant(pts[last - 1], pts[last]);
  11430. if (quad !== chainQuad) break;
  11431. last++;
  11432. }
  11433. return last - 1;
  11434. }
  11435. OLDgetChainStartIndices(pts) {
  11436. let start = 0;
  11437. const startIndexList = new ArrayList();
  11438. startIndexList.add(start);
  11439. do {
  11440. const last = this.findChainEnd(pts, start);
  11441. startIndexList.add(last);
  11442. start = last;
  11443. } while (start < pts.length - 1);
  11444. const startIndex = MonotoneChainIndexer.toIntArray(startIndexList);
  11445. return startIndex;
  11446. }
  11447. }
  11448. class MonotoneChainEdge {
  11449. constructor() {
  11450. MonotoneChainEdge.constructor_.apply(this, arguments);
  11451. }
  11452. static constructor_() {
  11453. this.e = null;
  11454. this.pts = null;
  11455. this.startIndex = null;
  11456. const e = arguments[0];
  11457. this.e = e;
  11458. this.pts = e.getCoordinates();
  11459. const mcb = new MonotoneChainIndexer();
  11460. this.startIndex = mcb.getChainStartIndices(this.pts);
  11461. }
  11462. getCoordinates() {
  11463. return this.pts;
  11464. }
  11465. getMaxX(chainIndex) {
  11466. const x1 = this.pts[this.startIndex[chainIndex]].x;
  11467. const x2 = this.pts[this.startIndex[chainIndex + 1]].x;
  11468. return x1 > x2 ? x1 : x2;
  11469. }
  11470. getMinX(chainIndex) {
  11471. const x1 = this.pts[this.startIndex[chainIndex]].x;
  11472. const x2 = this.pts[this.startIndex[chainIndex + 1]].x;
  11473. return x1 < x2 ? x1 : x2;
  11474. }
  11475. computeIntersectsForChain() {
  11476. if (arguments.length === 4) {
  11477. const chainIndex0 = arguments[0],
  11478. mce = arguments[1],
  11479. chainIndex1 = arguments[2],
  11480. si = arguments[3];
  11481. this.computeIntersectsForChain(this.startIndex[chainIndex0], this.startIndex[chainIndex0 + 1], mce, mce.startIndex[chainIndex1], mce.startIndex[chainIndex1 + 1], si);
  11482. } else if (arguments.length === 6) {
  11483. const start0 = arguments[0],
  11484. end0 = arguments[1],
  11485. mce = arguments[2],
  11486. start1 = arguments[3],
  11487. end1 = arguments[4],
  11488. ei = arguments[5];
  11489. if (end0 - start0 === 1 && end1 - start1 === 1) {
  11490. ei.addIntersections(this.e, start0, mce.e, start1);
  11491. return null;
  11492. }
  11493. if (!this.overlaps(start0, end0, mce, start1, end1)) return null;
  11494. const mid0 = Math.trunc((start0 + end0) / 2);
  11495. const mid1 = Math.trunc((start1 + end1) / 2);
  11496. if (start0 < mid0) {
  11497. if (start1 < mid1) this.computeIntersectsForChain(start0, mid0, mce, start1, mid1, ei);
  11498. if (mid1 < end1) this.computeIntersectsForChain(start0, mid0, mce, mid1, end1, ei);
  11499. }
  11500. if (mid0 < end0) {
  11501. if (start1 < mid1) this.computeIntersectsForChain(mid0, end0, mce, start1, mid1, ei);
  11502. if (mid1 < end1) this.computeIntersectsForChain(mid0, end0, mce, mid1, end1, ei);
  11503. }
  11504. }
  11505. }
  11506. overlaps(start0, end0, mce, start1, end1) {
  11507. return Envelope.intersects(this.pts[start0], this.pts[end0], mce.pts[start1], mce.pts[end1]);
  11508. }
  11509. getStartIndexes() {
  11510. return this.startIndex;
  11511. }
  11512. computeIntersects(mce, si) {
  11513. for (let i = 0; i < this.startIndex.length - 1; i++) for (let j = 0; j < mce.startIndex.length - 1; j++) this.computeIntersectsForChain(i, mce, j, si);
  11514. }
  11515. }
  11516. class Depth {
  11517. constructor() {
  11518. Depth.constructor_.apply(this, arguments);
  11519. }
  11520. static constructor_() {
  11521. this._depth = Array(2).fill().map(() => Array(3));
  11522. for (let i = 0; i < 2; i++) for (let j = 0; j < 3; j++) this._depth[i][j] = Depth.NULL_VALUE;
  11523. }
  11524. static depthAtLocation(location) {
  11525. if (location === Location.EXTERIOR) return 0;
  11526. if (location === Location.INTERIOR) return 1;
  11527. return Depth.NULL_VALUE;
  11528. }
  11529. getDepth(geomIndex, posIndex) {
  11530. return this._depth[geomIndex][posIndex];
  11531. }
  11532. setDepth(geomIndex, posIndex, depthValue) {
  11533. this._depth[geomIndex][posIndex] = depthValue;
  11534. }
  11535. isNull() {
  11536. if (arguments.length === 0) {
  11537. for (let i = 0; i < 2; i++) for (let j = 0; j < 3; j++) if (this._depth[i][j] !== Depth.NULL_VALUE) return false;
  11538. return true;
  11539. } else if (arguments.length === 1) {
  11540. const geomIndex = arguments[0];
  11541. return this._depth[geomIndex][1] === Depth.NULL_VALUE;
  11542. } else if (arguments.length === 2) {
  11543. const geomIndex = arguments[0],
  11544. posIndex = arguments[1];
  11545. return this._depth[geomIndex][posIndex] === Depth.NULL_VALUE;
  11546. }
  11547. }
  11548. normalize() {
  11549. for (let i = 0; i < 2; i++) if (!this.isNull(i)) {
  11550. let minDepth = this._depth[i][1];
  11551. if (this._depth[i][2] < minDepth) minDepth = this._depth[i][2];
  11552. if (minDepth < 0) minDepth = 0;
  11553. for (let j = 1; j < 3; j++) {
  11554. let newValue = 0;
  11555. if (this._depth[i][j] > minDepth) newValue = 1;
  11556. this._depth[i][j] = newValue;
  11557. }
  11558. }
  11559. }
  11560. getDelta(geomIndex) {
  11561. return this._depth[geomIndex][Position.RIGHT] - this._depth[geomIndex][Position.LEFT];
  11562. }
  11563. getLocation(geomIndex, posIndex) {
  11564. if (this._depth[geomIndex][posIndex] <= 0) return Location.EXTERIOR;
  11565. return Location.INTERIOR;
  11566. }
  11567. toString() {
  11568. return 'A: ' + this._depth[0][1] + ',' + this._depth[0][2] + ' B: ' + this._depth[1][1] + ',' + this._depth[1][2];
  11569. }
  11570. add() {
  11571. if (arguments.length === 1) {
  11572. const lbl = arguments[0];
  11573. for (let i = 0; i < 2; i++) for (let j = 1; j < 3; j++) {
  11574. const loc = lbl.getLocation(i, j);
  11575. if (loc === Location.EXTERIOR || loc === Location.INTERIOR) if (this.isNull(i, j)) this._depth[i][j] = Depth.depthAtLocation(loc);else this._depth[i][j] += Depth.depthAtLocation(loc);
  11576. }
  11577. } else if (arguments.length === 3) {
  11578. const geomIndex = arguments[0],
  11579. posIndex = arguments[1],
  11580. location = arguments[2];
  11581. if (location === Location.INTERIOR) this._depth[geomIndex][posIndex]++;
  11582. }
  11583. }
  11584. }
  11585. Depth.NULL_VALUE = -1;
  11586. class GraphComponent$1 {
  11587. constructor() {
  11588. GraphComponent$1.constructor_.apply(this, arguments);
  11589. }
  11590. static constructor_() {
  11591. this._label = null;
  11592. this._isInResult = false;
  11593. this._isCovered = false;
  11594. this._isCoveredSet = false;
  11595. this._isVisited = false;
  11596. if (arguments.length === 0) ; else if (arguments.length === 1) {
  11597. const label = arguments[0];
  11598. this._label = label;
  11599. }
  11600. }
  11601. setVisited(isVisited) {
  11602. this._isVisited = isVisited;
  11603. }
  11604. setInResult(isInResult) {
  11605. this._isInResult = isInResult;
  11606. }
  11607. isCovered() {
  11608. return this._isCovered;
  11609. }
  11610. isCoveredSet() {
  11611. return this._isCoveredSet;
  11612. }
  11613. setLabel(label) {
  11614. this._label = label;
  11615. }
  11616. getLabel() {
  11617. return this._label;
  11618. }
  11619. setCovered(isCovered) {
  11620. this._isCovered = isCovered;
  11621. this._isCoveredSet = true;
  11622. }
  11623. updateIM(im) {
  11624. Assert.isTrue(this._label.getGeometryCount() >= 2, 'found partial label');
  11625. this.computeIM(im);
  11626. }
  11627. isInResult() {
  11628. return this._isInResult;
  11629. }
  11630. isVisited() {
  11631. return this._isVisited;
  11632. }
  11633. }
  11634. class Edge$1 extends GraphComponent$1 {
  11635. constructor() {
  11636. super();
  11637. Edge$1.constructor_.apply(this, arguments);
  11638. }
  11639. static constructor_() {
  11640. this.pts = null;
  11641. this._env = null;
  11642. this.eiList = new EdgeIntersectionList(this);
  11643. this._name = null;
  11644. this._mce = null;
  11645. this._isIsolated = true;
  11646. this._depth = new Depth();
  11647. this._depthDelta = 0;
  11648. if (arguments.length === 1) {
  11649. const pts = arguments[0];
  11650. Edge$1.constructor_.call(this, pts, null);
  11651. } else if (arguments.length === 2) {
  11652. const pts = arguments[0],
  11653. label = arguments[1];
  11654. this.pts = pts;
  11655. this._label = label;
  11656. }
  11657. }
  11658. static updateIM() {
  11659. if (arguments.length === 2 && arguments[1] instanceof IntersectionMatrix && arguments[0] instanceof Label) {
  11660. const label = arguments[0],
  11661. im = arguments[1];
  11662. im.setAtLeastIfValid(label.getLocation(0, Position.ON), label.getLocation(1, Position.ON), 1);
  11663. if (label.isArea()) {
  11664. im.setAtLeastIfValid(label.getLocation(0, Position.LEFT), label.getLocation(1, Position.LEFT), 2);
  11665. im.setAtLeastIfValid(label.getLocation(0, Position.RIGHT), label.getLocation(1, Position.RIGHT), 2);
  11666. }
  11667. } else {
  11668. return super.updateIM.apply(this, arguments);
  11669. }
  11670. }
  11671. getDepth() {
  11672. return this._depth;
  11673. }
  11674. getCollapsedEdge() {
  11675. const newPts = new Array(2).fill(null);
  11676. newPts[0] = this.pts[0];
  11677. newPts[1] = this.pts[1];
  11678. const newe = new Edge$1(newPts, Label.toLineLabel(this._label));
  11679. return newe;
  11680. }
  11681. isIsolated() {
  11682. return this._isIsolated;
  11683. }
  11684. getCoordinates() {
  11685. return this.pts;
  11686. }
  11687. setIsolated(isIsolated) {
  11688. this._isIsolated = isIsolated;
  11689. }
  11690. setName(name) {
  11691. this._name = name;
  11692. }
  11693. equals(o) {
  11694. if (!(o instanceof Edge$1)) return false;
  11695. const e = o;
  11696. if (this.pts.length !== e.pts.length) return false;
  11697. let isEqualForward = true;
  11698. let isEqualReverse = true;
  11699. let iRev = this.pts.length;
  11700. for (let i = 0; i < this.pts.length; i++) {
  11701. if (!this.pts[i].equals2D(e.pts[i])) isEqualForward = false;
  11702. if (!this.pts[i].equals2D(e.pts[--iRev])) isEqualReverse = false;
  11703. if (!isEqualForward && !isEqualReverse) return false;
  11704. }
  11705. return true;
  11706. }
  11707. getCoordinate() {
  11708. if (arguments.length === 0) {
  11709. if (this.pts.length > 0) return this.pts[0];
  11710. return null;
  11711. } else if (arguments.length === 1) {
  11712. const i = arguments[0];
  11713. return this.pts[i];
  11714. }
  11715. }
  11716. print(out) {
  11717. out.print('edge ' + this._name + ': ');
  11718. out.print('LINESTRING (');
  11719. for (let i = 0; i < this.pts.length; i++) {
  11720. if (i > 0) out.print(',');
  11721. out.print(this.pts[i].x + ' ' + this.pts[i].y);
  11722. }
  11723. out.print(') ' + this._label + ' ' + this._depthDelta);
  11724. }
  11725. computeIM(im) {
  11726. Edge$1.updateIM(this._label, im);
  11727. }
  11728. isCollapsed() {
  11729. if (!this._label.isArea()) return false;
  11730. if (this.pts.length !== 3) return false;
  11731. if (this.pts[0].equals(this.pts[2])) return true;
  11732. return false;
  11733. }
  11734. isClosed() {
  11735. return this.pts[0].equals(this.pts[this.pts.length - 1]);
  11736. }
  11737. getMaximumSegmentIndex() {
  11738. return this.pts.length - 1;
  11739. }
  11740. getDepthDelta() {
  11741. return this._depthDelta;
  11742. }
  11743. getNumPoints() {
  11744. return this.pts.length;
  11745. }
  11746. printReverse(out) {
  11747. out.print('edge ' + this._name + ': ');
  11748. for (let i = this.pts.length - 1; i >= 0; i--) out.print(this.pts[i] + ' ');
  11749. out.println('');
  11750. }
  11751. getMonotoneChainEdge() {
  11752. if (this._mce === null) this._mce = new MonotoneChainEdge(this);
  11753. return this._mce;
  11754. }
  11755. getEnvelope() {
  11756. if (this._env === null) {
  11757. this._env = new Envelope();
  11758. for (let i = 0; i < this.pts.length; i++) this._env.expandToInclude(this.pts[i]);
  11759. }
  11760. return this._env;
  11761. }
  11762. addIntersection(li, segmentIndex, geomIndex, intIndex) {
  11763. const intPt = new Coordinate(li.getIntersection(intIndex));
  11764. let normalizedSegmentIndex = segmentIndex;
  11765. let dist = li.getEdgeDistance(geomIndex, intIndex);
  11766. const nextSegIndex = normalizedSegmentIndex + 1;
  11767. if (nextSegIndex < this.pts.length) {
  11768. const nextPt = this.pts[nextSegIndex];
  11769. if (intPt.equals2D(nextPt)) {
  11770. normalizedSegmentIndex = nextSegIndex;
  11771. dist = 0.0;
  11772. }
  11773. }
  11774. this.eiList.add(intPt, normalizedSegmentIndex, dist);
  11775. }
  11776. toString() {
  11777. const builder = new StringBuilder();
  11778. builder.append('edge ' + this._name + ': ');
  11779. builder.append('LINESTRING (');
  11780. for (let i = 0; i < this.pts.length; i++) {
  11781. if (i > 0) builder.append(',');
  11782. builder.append(this.pts[i].x + ' ' + this.pts[i].y);
  11783. }
  11784. builder.append(') ' + this._label + ' ' + this._depthDelta);
  11785. return builder.toString();
  11786. }
  11787. isPointwiseEqual(e) {
  11788. if (this.pts.length !== e.pts.length) return false;
  11789. for (let i = 0; i < this.pts.length; i++) if (!this.pts[i].equals2D(e.pts[i])) return false;
  11790. return true;
  11791. }
  11792. setDepthDelta(depthDelta) {
  11793. this._depthDelta = depthDelta;
  11794. }
  11795. getEdgeIntersectionList() {
  11796. return this.eiList;
  11797. }
  11798. addIntersections(li, segmentIndex, geomIndex) {
  11799. for (let i = 0; i < li.getIntersectionNum(); i++) this.addIntersection(li, segmentIndex, geomIndex, i);
  11800. }
  11801. }
  11802. class Node$2 extends GraphComponent$1 {
  11803. constructor() {
  11804. super();
  11805. Node$2.constructor_.apply(this, arguments);
  11806. }
  11807. static constructor_() {
  11808. this._coord = null;
  11809. this._edges = null;
  11810. const coord = arguments[0],
  11811. edges = arguments[1];
  11812. this._coord = coord;
  11813. this._edges = edges;
  11814. this._label = new Label(0, Location.NONE);
  11815. }
  11816. isIncidentEdgeInResult() {
  11817. for (let it = this.getEdges().getEdges().iterator(); it.hasNext();) {
  11818. const de = it.next();
  11819. if (de.getEdge().isInResult()) return true;
  11820. }
  11821. return false;
  11822. }
  11823. isIsolated() {
  11824. return this._label.getGeometryCount() === 1;
  11825. }
  11826. getCoordinate() {
  11827. return this._coord;
  11828. }
  11829. print(out) {
  11830. out.println('node ' + this._coord + ' lbl: ' + this._label);
  11831. }
  11832. computeIM(im) {}
  11833. computeMergedLocation(label2, eltIndex) {
  11834. let loc = Location.NONE;
  11835. loc = this._label.getLocation(eltIndex);
  11836. if (!label2.isNull(eltIndex)) {
  11837. const nLoc = label2.getLocation(eltIndex);
  11838. if (loc !== Location.BOUNDARY) loc = nLoc;
  11839. }
  11840. return loc;
  11841. }
  11842. setLabel() {
  11843. if (arguments.length === 2 && Number.isInteger(arguments[1]) && Number.isInteger(arguments[0])) {
  11844. const argIndex = arguments[0],
  11845. onLocation = arguments[1];
  11846. if (this._label === null) this._label = new Label(argIndex, onLocation);else this._label.setLocation(argIndex, onLocation);
  11847. } else {
  11848. return super.setLabel.apply(this, arguments);
  11849. }
  11850. }
  11851. getEdges() {
  11852. return this._edges;
  11853. }
  11854. mergeLabel() {
  11855. if (arguments[0] instanceof Node$2) {
  11856. const n = arguments[0];
  11857. this.mergeLabel(n._label);
  11858. } else if (arguments[0] instanceof Label) {
  11859. const label2 = arguments[0];
  11860. for (let i = 0; i < 2; i++) {
  11861. const loc = this.computeMergedLocation(label2, i);
  11862. const thisLoc = this._label.getLocation(i);
  11863. if (thisLoc === Location.NONE) this._label.setLocation(i, loc);
  11864. }
  11865. }
  11866. }
  11867. add(e) {
  11868. this._edges.insert(e);
  11869. e.setNode(this);
  11870. }
  11871. setLabelBoundary(argIndex) {
  11872. if (this._label === null) return null;
  11873. let loc = Location.NONE;
  11874. if (this._label !== null) loc = this._label.getLocation(argIndex);
  11875. let newLoc = null;
  11876. switch (loc) {
  11877. case Location.BOUNDARY:
  11878. newLoc = Location.INTERIOR;
  11879. break;
  11880. case Location.INTERIOR:
  11881. newLoc = Location.BOUNDARY;
  11882. break;
  11883. default:
  11884. newLoc = Location.BOUNDARY;
  11885. break;
  11886. }
  11887. this._label.setLocation(argIndex, newLoc);
  11888. }
  11889. }
  11890. class NodeMap$1 {
  11891. constructor() {
  11892. NodeMap$1.constructor_.apply(this, arguments);
  11893. }
  11894. static constructor_() {
  11895. this.nodeMap = new TreeMap();
  11896. this.nodeFact = null;
  11897. const nodeFact = arguments[0];
  11898. this.nodeFact = nodeFact;
  11899. }
  11900. find(coord) {
  11901. return this.nodeMap.get(coord);
  11902. }
  11903. addNode() {
  11904. if (arguments[0] instanceof Coordinate) {
  11905. const coord = arguments[0];
  11906. let node = this.nodeMap.get(coord);
  11907. if (node === null) {
  11908. node = this.nodeFact.createNode(coord);
  11909. this.nodeMap.put(coord, node);
  11910. }
  11911. return node;
  11912. } else if (arguments[0] instanceof Node$2) {
  11913. const n = arguments[0];
  11914. const node = this.nodeMap.get(n.getCoordinate());
  11915. if (node === null) {
  11916. this.nodeMap.put(n.getCoordinate(), n);
  11917. return n;
  11918. }
  11919. node.mergeLabel(n);
  11920. return node;
  11921. }
  11922. }
  11923. print(out) {
  11924. for (let it = this.iterator(); it.hasNext();) {
  11925. const n = it.next();
  11926. n.print(out);
  11927. }
  11928. }
  11929. iterator() {
  11930. return this.nodeMap.values().iterator();
  11931. }
  11932. values() {
  11933. return this.nodeMap.values();
  11934. }
  11935. getBoundaryNodes(geomIndex) {
  11936. const bdyNodes = new ArrayList();
  11937. for (let i = this.iterator(); i.hasNext();) {
  11938. const node = i.next();
  11939. if (node.getLabel().getLocation(geomIndex) === Location.BOUNDARY) bdyNodes.add(node);
  11940. }
  11941. return bdyNodes;
  11942. }
  11943. add(e) {
  11944. const p = e.getCoordinate();
  11945. const n = this.addNode(p);
  11946. n.add(e);
  11947. }
  11948. }
  11949. class EdgeEnd {
  11950. constructor() {
  11951. EdgeEnd.constructor_.apply(this, arguments);
  11952. }
  11953. static constructor_() {
  11954. this._edge = null;
  11955. this._label = null;
  11956. this._node = null;
  11957. this._p0 = null;
  11958. this._p1 = null;
  11959. this._dx = null;
  11960. this._dy = null;
  11961. this._quadrant = null;
  11962. if (arguments.length === 1) {
  11963. const edge = arguments[0];
  11964. this._edge = edge;
  11965. } else if (arguments.length === 3) {
  11966. const edge = arguments[0],
  11967. p0 = arguments[1],
  11968. p1 = arguments[2];
  11969. EdgeEnd.constructor_.call(this, edge, p0, p1, null);
  11970. } else if (arguments.length === 4) {
  11971. const edge = arguments[0],
  11972. p0 = arguments[1],
  11973. p1 = arguments[2],
  11974. label = arguments[3];
  11975. EdgeEnd.constructor_.call(this, edge);
  11976. this.init(p0, p1);
  11977. this._label = label;
  11978. }
  11979. }
  11980. compareDirection(e) {
  11981. if (this._dx === e._dx && this._dy === e._dy) return 0;
  11982. if (this._quadrant > e._quadrant) return 1;
  11983. if (this._quadrant < e._quadrant) return -1;
  11984. return Orientation.index(e._p0, e._p1, this._p1);
  11985. }
  11986. getDy() {
  11987. return this._dy;
  11988. }
  11989. getCoordinate() {
  11990. return this._p0;
  11991. }
  11992. setNode(node) {
  11993. this._node = node;
  11994. }
  11995. print(out) {
  11996. const angle = Math.atan2(this._dy, this._dx);
  11997. const className = this.getClass().getName();
  11998. const lastDotPos = className.lastIndexOf('.');
  11999. const name = className.substring(lastDotPos + 1);
  12000. out.print(' ' + name + ': ' + this._p0 + ' - ' + this._p1 + ' ' + this._quadrant + ':' + angle + ' ' + this._label);
  12001. }
  12002. compareTo(obj) {
  12003. const e = obj;
  12004. return this.compareDirection(e);
  12005. }
  12006. getDirectedCoordinate() {
  12007. return this._p1;
  12008. }
  12009. getDx() {
  12010. return this._dx;
  12011. }
  12012. getLabel() {
  12013. return this._label;
  12014. }
  12015. getEdge() {
  12016. return this._edge;
  12017. }
  12018. getQuadrant() {
  12019. return this._quadrant;
  12020. }
  12021. getNode() {
  12022. return this._node;
  12023. }
  12024. toString() {
  12025. const angle = Math.atan2(this._dy, this._dx);
  12026. const className = this.getClass().getName();
  12027. const lastDotPos = className.lastIndexOf('.');
  12028. const name = className.substring(lastDotPos + 1);
  12029. return ' ' + name + ': ' + this._p0 + ' - ' + this._p1 + ' ' + this._quadrant + ':' + angle + ' ' + this._label;
  12030. }
  12031. computeLabel(boundaryNodeRule) {}
  12032. init(p0, p1) {
  12033. this._p0 = p0;
  12034. this._p1 = p1;
  12035. this._dx = p1.x - p0.x;
  12036. this._dy = p1.y - p0.y;
  12037. this._quadrant = Quadrant.quadrant(this._dx, this._dy);
  12038. Assert.isTrue(!(this._dx === 0 && this._dy === 0), 'EdgeEnd with identical endpoints found');
  12039. }
  12040. get interfaces_() {
  12041. return [Comparable];
  12042. }
  12043. }
  12044. class TopologyException extends RuntimeException {
  12045. constructor(msg, pt) {
  12046. super(pt ? msg + ' [ ' + pt + ' ]' : msg);
  12047. this.pt = pt ? new Coordinate(pt) : undefined;
  12048. this.name = Object.keys({
  12049. TopologyException
  12050. })[0];
  12051. }
  12052. getCoordinate() {
  12053. return this.pt;
  12054. }
  12055. }
  12056. class DirectedEdge$1 extends EdgeEnd {
  12057. constructor() {
  12058. super();
  12059. DirectedEdge$1.constructor_.apply(this, arguments);
  12060. }
  12061. static constructor_() {
  12062. this._isForward = null;
  12063. this._isInResult = false;
  12064. this._isVisited = false;
  12065. this._sym = null;
  12066. this._next = null;
  12067. this._nextMin = null;
  12068. this._edgeRing = null;
  12069. this._minEdgeRing = null;
  12070. this._depth = [0, -999, -999];
  12071. const edge = arguments[0],
  12072. isForward = arguments[1];
  12073. EdgeEnd.constructor_.call(this, edge);
  12074. this._isForward = isForward;
  12075. if (isForward) {
  12076. this.init(edge.getCoordinate(0), edge.getCoordinate(1));
  12077. } else {
  12078. const n = edge.getNumPoints() - 1;
  12079. this.init(edge.getCoordinate(n), edge.getCoordinate(n - 1));
  12080. }
  12081. this.computeDirectedLabel();
  12082. }
  12083. static depthFactor(currLocation, nextLocation) {
  12084. if (currLocation === Location.EXTERIOR && nextLocation === Location.INTERIOR) return 1;else if (currLocation === Location.INTERIOR && nextLocation === Location.EXTERIOR) return -1;
  12085. return 0;
  12086. }
  12087. getNextMin() {
  12088. return this._nextMin;
  12089. }
  12090. getDepth(position) {
  12091. return this._depth[position];
  12092. }
  12093. setVisited(isVisited) {
  12094. this._isVisited = isVisited;
  12095. }
  12096. computeDirectedLabel() {
  12097. this._label = new Label(this._edge.getLabel());
  12098. if (!this._isForward) this._label.flip();
  12099. }
  12100. getNext() {
  12101. return this._next;
  12102. }
  12103. setDepth(position, depthVal) {
  12104. if (this._depth[position] !== -999) if (this._depth[position] !== depthVal) throw new TopologyException('assigned depths do not match', this.getCoordinate());
  12105. this._depth[position] = depthVal;
  12106. }
  12107. isInteriorAreaEdge() {
  12108. let isInteriorAreaEdge = true;
  12109. for (let i = 0; i < 2; i++) if (!(this._label.isArea(i) && this._label.getLocation(i, Position.LEFT) === Location.INTERIOR && this._label.getLocation(i, Position.RIGHT) === Location.INTERIOR)) isInteriorAreaEdge = false;
  12110. return isInteriorAreaEdge;
  12111. }
  12112. setNextMin(nextMin) {
  12113. this._nextMin = nextMin;
  12114. }
  12115. print(out) {
  12116. super.print.call(this, out);
  12117. out.print(' ' + this._depth[Position.LEFT] + '/' + this._depth[Position.RIGHT]);
  12118. out.print(' (' + this.getDepthDelta() + ')');
  12119. if (this._isInResult) out.print(' inResult');
  12120. }
  12121. setMinEdgeRing(minEdgeRing) {
  12122. this._minEdgeRing = minEdgeRing;
  12123. }
  12124. isLineEdge() {
  12125. const isLine = this._label.isLine(0) || this._label.isLine(1);
  12126. const isExteriorIfArea0 = !this._label.isArea(0) || this._label.allPositionsEqual(0, Location.EXTERIOR);
  12127. const isExteriorIfArea1 = !this._label.isArea(1) || this._label.allPositionsEqual(1, Location.EXTERIOR);
  12128. return isLine && isExteriorIfArea0 && isExteriorIfArea1;
  12129. }
  12130. setEdgeRing(edgeRing) {
  12131. this._edgeRing = edgeRing;
  12132. }
  12133. getMinEdgeRing() {
  12134. return this._minEdgeRing;
  12135. }
  12136. getDepthDelta() {
  12137. let depthDelta = this._edge.getDepthDelta();
  12138. if (!this._isForward) depthDelta = -depthDelta;
  12139. return depthDelta;
  12140. }
  12141. setInResult(isInResult) {
  12142. this._isInResult = isInResult;
  12143. }
  12144. getSym() {
  12145. return this._sym;
  12146. }
  12147. isForward() {
  12148. return this._isForward;
  12149. }
  12150. getEdge() {
  12151. return this._edge;
  12152. }
  12153. printEdge(out) {
  12154. this.print(out);
  12155. out.print(' ');
  12156. if (this._isForward) this._edge.print(out);else this._edge.printReverse(out);
  12157. }
  12158. setSym(de) {
  12159. this._sym = de;
  12160. }
  12161. setVisitedEdge(isVisited) {
  12162. this.setVisited(isVisited);
  12163. this._sym.setVisited(isVisited);
  12164. }
  12165. setEdgeDepths(position, depth) {
  12166. let depthDelta = this.getEdge().getDepthDelta();
  12167. if (!this._isForward) depthDelta = -depthDelta;
  12168. let directionFactor = 1;
  12169. if (position === Position.LEFT) directionFactor = -1;
  12170. const oppositePos = Position.opposite(position);
  12171. const delta = depthDelta * directionFactor;
  12172. const oppositeDepth = depth + delta;
  12173. this.setDepth(position, depth);
  12174. this.setDepth(oppositePos, oppositeDepth);
  12175. }
  12176. getEdgeRing() {
  12177. return this._edgeRing;
  12178. }
  12179. isInResult() {
  12180. return this._isInResult;
  12181. }
  12182. setNext(next) {
  12183. this._next = next;
  12184. }
  12185. isVisited() {
  12186. return this._isVisited;
  12187. }
  12188. }
  12189. class NodeFactory {
  12190. createNode(coord) {
  12191. return new Node$2(coord, null);
  12192. }
  12193. }
  12194. class PlanarGraph$1 {
  12195. constructor() {
  12196. PlanarGraph$1.constructor_.apply(this, arguments);
  12197. }
  12198. static constructor_() {
  12199. this._edges = new ArrayList();
  12200. this._nodes = null;
  12201. this._edgeEndList = new ArrayList();
  12202. if (arguments.length === 0) {
  12203. this._nodes = new NodeMap$1(new NodeFactory());
  12204. } else if (arguments.length === 1) {
  12205. const nodeFact = arguments[0];
  12206. this._nodes = new NodeMap$1(nodeFact);
  12207. }
  12208. }
  12209. static linkResultDirectedEdges(nodes) {
  12210. for (let nodeit = nodes.iterator(); nodeit.hasNext();) {
  12211. const node = nodeit.next();
  12212. node.getEdges().linkResultDirectedEdges();
  12213. }
  12214. }
  12215. printEdges(out) {
  12216. out.println('Edges:');
  12217. for (let i = 0; i < this._edges.size(); i++) {
  12218. out.println('edge ' + i + ':');
  12219. const e = this._edges.get(i);
  12220. e.print(out);
  12221. e.eiList.print(out);
  12222. }
  12223. }
  12224. find(coord) {
  12225. return this._nodes.find(coord);
  12226. }
  12227. addNode() {
  12228. if (arguments[0] instanceof Node$2) {
  12229. const node = arguments[0];
  12230. return this._nodes.addNode(node);
  12231. } else if (arguments[0] instanceof Coordinate) {
  12232. const coord = arguments[0];
  12233. return this._nodes.addNode(coord);
  12234. }
  12235. }
  12236. getNodeIterator() {
  12237. return this._nodes.iterator();
  12238. }
  12239. linkResultDirectedEdges() {
  12240. for (let nodeit = this._nodes.iterator(); nodeit.hasNext();) {
  12241. const node = nodeit.next();
  12242. node.getEdges().linkResultDirectedEdges();
  12243. }
  12244. }
  12245. debugPrintln(o) {
  12246. System.out.println(o);
  12247. }
  12248. isBoundaryNode(geomIndex, coord) {
  12249. const node = this._nodes.find(coord);
  12250. if (node === null) return false;
  12251. const label = node.getLabel();
  12252. if (label !== null && label.getLocation(geomIndex) === Location.BOUNDARY) return true;
  12253. return false;
  12254. }
  12255. linkAllDirectedEdges() {
  12256. for (let nodeit = this._nodes.iterator(); nodeit.hasNext();) {
  12257. const node = nodeit.next();
  12258. node.getEdges().linkAllDirectedEdges();
  12259. }
  12260. }
  12261. matchInSameDirection(p0, p1, ep0, ep1) {
  12262. if (!p0.equals(ep0)) return false;
  12263. if (Orientation.index(p0, p1, ep1) === Orientation.COLLINEAR && Quadrant.quadrant(p0, p1) === Quadrant.quadrant(ep0, ep1)) return true;
  12264. return false;
  12265. }
  12266. getEdgeEnds() {
  12267. return this._edgeEndList;
  12268. }
  12269. debugPrint(o) {
  12270. System.out.print(o);
  12271. }
  12272. getEdgeIterator() {
  12273. return this._edges.iterator();
  12274. }
  12275. findEdgeInSameDirection(p0, p1) {
  12276. for (let i = 0; i < this._edges.size(); i++) {
  12277. const e = this._edges.get(i);
  12278. const eCoord = e.getCoordinates();
  12279. if (this.matchInSameDirection(p0, p1, eCoord[0], eCoord[1])) return e;
  12280. if (this.matchInSameDirection(p0, p1, eCoord[eCoord.length - 1], eCoord[eCoord.length - 2])) return e;
  12281. }
  12282. return null;
  12283. }
  12284. insertEdge(e) {
  12285. this._edges.add(e);
  12286. }
  12287. findEdgeEnd(e) {
  12288. for (let i = this.getEdgeEnds().iterator(); i.hasNext();) {
  12289. const ee = i.next();
  12290. if (ee.getEdge() === e) return ee;
  12291. }
  12292. return null;
  12293. }
  12294. addEdges(edgesToAdd) {
  12295. for (let it = edgesToAdd.iterator(); it.hasNext();) {
  12296. const e = it.next();
  12297. this._edges.add(e);
  12298. const de1 = new DirectedEdge$1(e, true);
  12299. const de2 = new DirectedEdge$1(e, false);
  12300. de1.setSym(de2);
  12301. de2.setSym(de1);
  12302. this.add(de1);
  12303. this.add(de2);
  12304. }
  12305. }
  12306. add(e) {
  12307. this._nodes.add(e);
  12308. this._edgeEndList.add(e);
  12309. }
  12310. getNodes() {
  12311. return this._nodes.values();
  12312. }
  12313. findEdge(p0, p1) {
  12314. for (let i = 0; i < this._edges.size(); i++) {
  12315. const e = this._edges.get(i);
  12316. const eCoord = e.getCoordinates();
  12317. if (p0.equals(eCoord[0]) && p1.equals(eCoord[1])) return e;
  12318. }
  12319. return null;
  12320. }
  12321. }
  12322. class GeometryGraph extends PlanarGraph$1 {
  12323. constructor() {
  12324. super();
  12325. GeometryGraph.constructor_.apply(this, arguments);
  12326. }
  12327. static constructor_() {
  12328. this._parentGeom = null;
  12329. this._lineEdgeMap = new HashMap();
  12330. this._boundaryNodeRule = null;
  12331. this._useBoundaryDeterminationRule = true;
  12332. this._argIndex = null;
  12333. this._boundaryNodes = null;
  12334. this._hasTooFewPoints = false;
  12335. this._invalidPoint = null;
  12336. this._areaPtLocator = null;
  12337. this._ptLocator = new PointLocator();
  12338. if (arguments.length === 2) {
  12339. const argIndex = arguments[0],
  12340. parentGeom = arguments[1];
  12341. GeometryGraph.constructor_.call(this, argIndex, parentGeom, BoundaryNodeRule.OGC_SFS_BOUNDARY_RULE);
  12342. } else if (arguments.length === 3) {
  12343. const argIndex = arguments[0],
  12344. parentGeom = arguments[1],
  12345. boundaryNodeRule = arguments[2];
  12346. this._argIndex = argIndex;
  12347. this._parentGeom = parentGeom;
  12348. this._boundaryNodeRule = boundaryNodeRule;
  12349. if (parentGeom !== null) this.add(parentGeom);
  12350. }
  12351. }
  12352. static determineBoundary(boundaryNodeRule, boundaryCount) {
  12353. return boundaryNodeRule.isInBoundary(boundaryCount) ? Location.BOUNDARY : Location.INTERIOR;
  12354. }
  12355. insertBoundaryPoint(argIndex, coord) {
  12356. const n = this._nodes.addNode(coord);
  12357. const lbl = n.getLabel();
  12358. let boundaryCount = 1;
  12359. let loc = Location.NONE;
  12360. loc = lbl.getLocation(argIndex, Position.ON);
  12361. if (loc === Location.BOUNDARY) boundaryCount++;
  12362. const newLoc = GeometryGraph.determineBoundary(this._boundaryNodeRule, boundaryCount);
  12363. lbl.setLocation(argIndex, newLoc);
  12364. }
  12365. computeSelfNodes() {
  12366. if (arguments.length === 2) {
  12367. const li = arguments[0],
  12368. computeRingSelfNodes = arguments[1];
  12369. return this.computeSelfNodes(li, computeRingSelfNodes, false);
  12370. } else if (arguments.length === 3) {
  12371. const li = arguments[0],
  12372. computeRingSelfNodes = arguments[1],
  12373. isDoneIfProperInt = arguments[2];
  12374. const si = new SegmentIntersector$1(li, true, false);
  12375. si.setIsDoneIfProperInt(isDoneIfProperInt);
  12376. const esi = this.createEdgeSetIntersector();
  12377. const isRings = this._parentGeom instanceof LinearRing || this._parentGeom instanceof Polygon || this._parentGeom instanceof MultiPolygon;
  12378. const computeAllSegments = computeRingSelfNodes || !isRings;
  12379. esi.computeIntersections(this._edges, si, computeAllSegments);
  12380. this.addSelfIntersectionNodes(this._argIndex);
  12381. return si;
  12382. }
  12383. }
  12384. computeSplitEdges(edgelist) {
  12385. for (let i = this._edges.iterator(); i.hasNext();) {
  12386. const e = i.next();
  12387. e.eiList.addSplitEdges(edgelist);
  12388. }
  12389. }
  12390. computeEdgeIntersections(g, li, includeProper) {
  12391. const si = new SegmentIntersector$1(li, includeProper, true);
  12392. si.setBoundaryNodes(this.getBoundaryNodes(), g.getBoundaryNodes());
  12393. const esi = this.createEdgeSetIntersector();
  12394. esi.computeIntersections(this._edges, g._edges, si);
  12395. return si;
  12396. }
  12397. getGeometry() {
  12398. return this._parentGeom;
  12399. }
  12400. getBoundaryNodeRule() {
  12401. return this._boundaryNodeRule;
  12402. }
  12403. hasTooFewPoints() {
  12404. return this._hasTooFewPoints;
  12405. }
  12406. addPoint() {
  12407. if (arguments[0] instanceof Point) {
  12408. const p = arguments[0];
  12409. const coord = p.getCoordinate();
  12410. this.insertPoint(this._argIndex, coord, Location.INTERIOR);
  12411. } else if (arguments[0] instanceof Coordinate) {
  12412. const pt = arguments[0];
  12413. this.insertPoint(this._argIndex, pt, Location.INTERIOR);
  12414. }
  12415. }
  12416. addPolygon(p) {
  12417. this.addPolygonRing(p.getExteriorRing(), Location.EXTERIOR, Location.INTERIOR);
  12418. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  12419. const hole = p.getInteriorRingN(i);
  12420. this.addPolygonRing(hole, Location.INTERIOR, Location.EXTERIOR);
  12421. }
  12422. }
  12423. addEdge(e) {
  12424. this.insertEdge(e);
  12425. const coord = e.getCoordinates();
  12426. this.insertPoint(this._argIndex, coord[0], Location.BOUNDARY);
  12427. this.insertPoint(this._argIndex, coord[coord.length - 1], Location.BOUNDARY);
  12428. }
  12429. addLineString(line) {
  12430. const coord = CoordinateArrays.removeRepeatedPoints(line.getCoordinates());
  12431. if (coord.length < 2) {
  12432. this._hasTooFewPoints = true;
  12433. this._invalidPoint = coord[0];
  12434. return null;
  12435. }
  12436. const e = new Edge$1(coord, new Label(this._argIndex, Location.INTERIOR));
  12437. this._lineEdgeMap.put(line, e);
  12438. this.insertEdge(e);
  12439. Assert.isTrue(coord.length >= 2, 'found LineString with single point');
  12440. this.insertBoundaryPoint(this._argIndex, coord[0]);
  12441. this.insertBoundaryPoint(this._argIndex, coord[coord.length - 1]);
  12442. }
  12443. getInvalidPoint() {
  12444. return this._invalidPoint;
  12445. }
  12446. getBoundaryPoints() {
  12447. const coll = this.getBoundaryNodes();
  12448. const pts = new Array(coll.size()).fill(null);
  12449. let i = 0;
  12450. for (let it = coll.iterator(); it.hasNext();) {
  12451. const node = it.next();
  12452. pts[i++] = node.getCoordinate().copy();
  12453. }
  12454. return pts;
  12455. }
  12456. getBoundaryNodes() {
  12457. if (this._boundaryNodes === null) this._boundaryNodes = this._nodes.getBoundaryNodes(this._argIndex);
  12458. return this._boundaryNodes;
  12459. }
  12460. addSelfIntersectionNode(argIndex, coord, loc) {
  12461. if (this.isBoundaryNode(argIndex, coord)) return null;
  12462. if (loc === Location.BOUNDARY && this._useBoundaryDeterminationRule) this.insertBoundaryPoint(argIndex, coord);else this.insertPoint(argIndex, coord, loc);
  12463. }
  12464. addPolygonRing(lr, cwLeft, cwRight) {
  12465. if (lr.isEmpty()) return null;
  12466. const coord = CoordinateArrays.removeRepeatedPoints(lr.getCoordinates());
  12467. if (coord.length < 4) {
  12468. this._hasTooFewPoints = true;
  12469. this._invalidPoint = coord[0];
  12470. return null;
  12471. }
  12472. let left = cwLeft;
  12473. let right = cwRight;
  12474. if (Orientation.isCCW(coord)) {
  12475. left = cwRight;
  12476. right = cwLeft;
  12477. }
  12478. const e = new Edge$1(coord, new Label(this._argIndex, Location.BOUNDARY, left, right));
  12479. this._lineEdgeMap.put(lr, e);
  12480. this.insertEdge(e);
  12481. this.insertPoint(this._argIndex, coord[0], Location.BOUNDARY);
  12482. }
  12483. insertPoint(argIndex, coord, onLocation) {
  12484. const n = this._nodes.addNode(coord);
  12485. const lbl = n.getLabel();
  12486. if (lbl === null) n._label = new Label(argIndex, onLocation);else lbl.setLocation(argIndex, onLocation);
  12487. }
  12488. createEdgeSetIntersector() {
  12489. return new SimpleMCSweepLineIntersector();
  12490. }
  12491. addSelfIntersectionNodes(argIndex) {
  12492. for (let i = this._edges.iterator(); i.hasNext();) {
  12493. const e = i.next();
  12494. const eLoc = e.getLabel().getLocation(argIndex);
  12495. for (let eiIt = e.eiList.iterator(); eiIt.hasNext();) {
  12496. const ei = eiIt.next();
  12497. this.addSelfIntersectionNode(argIndex, ei.coord, eLoc);
  12498. }
  12499. }
  12500. }
  12501. add() {
  12502. if (arguments.length === 1 && arguments[0] instanceof Geometry) {
  12503. const g = arguments[0];
  12504. if (g.isEmpty()) return null;
  12505. if (g instanceof MultiPolygon) this._useBoundaryDeterminationRule = false;
  12506. if (g instanceof Polygon) this.addPolygon(g);else if (g instanceof LineString) this.addLineString(g);else if (g instanceof Point) this.addPoint(g);else if (g instanceof MultiPoint) this.addCollection(g);else if (g instanceof MultiLineString) this.addCollection(g);else if (g instanceof MultiPolygon) this.addCollection(g);else if (g instanceof GeometryCollection) this.addCollection(g);else throw new UnsupportedOperationException(g.getGeometryType());
  12507. } else {
  12508. return super.add.apply(this, arguments);
  12509. }
  12510. }
  12511. addCollection(gc) {
  12512. for (let i = 0; i < gc.getNumGeometries(); i++) {
  12513. const g = gc.getGeometryN(i);
  12514. this.add(g);
  12515. }
  12516. }
  12517. locate(pt) {
  12518. if (hasInterface(this._parentGeom, Polygonal) && this._parentGeom.getNumGeometries() > 50) {
  12519. if (this._areaPtLocator === null) this._areaPtLocator = new IndexedPointInAreaLocator(this._parentGeom);
  12520. return this._areaPtLocator.locate(pt);
  12521. }
  12522. return this._ptLocator.locate(pt, this._parentGeom);
  12523. }
  12524. findEdge() {
  12525. if (arguments.length === 1 && arguments[0] instanceof LineString) {
  12526. const line = arguments[0];
  12527. return this._lineEdgeMap.get(line);
  12528. } else {
  12529. return super.findEdge.apply(this, arguments);
  12530. }
  12531. }
  12532. }
  12533. var geomgraph = /*#__PURE__*/Object.freeze({
  12534. __proto__: null,
  12535. GeometryGraph: GeometryGraph
  12536. });
  12537. class KdNodeVisitor {
  12538. visit(node) {}
  12539. }
  12540. class KdNode {
  12541. constructor() {
  12542. KdNode.constructor_.apply(this, arguments);
  12543. }
  12544. static constructor_() {
  12545. this._p = null;
  12546. this._data = null;
  12547. this._left = null;
  12548. this._right = null;
  12549. this._count = null;
  12550. if (arguments.length === 2) {
  12551. const p = arguments[0],
  12552. data = arguments[1];
  12553. this._p = new Coordinate(p);
  12554. this._left = null;
  12555. this._right = null;
  12556. this._count = 1;
  12557. this._data = data;
  12558. } else if (arguments.length === 3) {
  12559. const _x = arguments[0],
  12560. _y = arguments[1],
  12561. data = arguments[2];
  12562. this._p = new Coordinate(_x, _y);
  12563. this._left = null;
  12564. this._right = null;
  12565. this._count = 1;
  12566. this._data = data;
  12567. }
  12568. }
  12569. isRepeated() {
  12570. return this._count > 1;
  12571. }
  12572. getRight() {
  12573. return this._right;
  12574. }
  12575. getCoordinate() {
  12576. return this._p;
  12577. }
  12578. setLeft(_left) {
  12579. this._left = _left;
  12580. }
  12581. getX() {
  12582. return this._p.x;
  12583. }
  12584. getData() {
  12585. return this._data;
  12586. }
  12587. getCount() {
  12588. return this._count;
  12589. }
  12590. getLeft() {
  12591. return this._left;
  12592. }
  12593. getY() {
  12594. return this._p.y;
  12595. }
  12596. increment() {
  12597. this._count = this._count + 1;
  12598. }
  12599. setRight(_right) {
  12600. this._right = _right;
  12601. }
  12602. }
  12603. class KdTree {
  12604. constructor() {
  12605. KdTree.constructor_.apply(this, arguments);
  12606. }
  12607. static constructor_() {
  12608. this._root = null;
  12609. this._numberOfNodes = null;
  12610. this._tolerance = null;
  12611. if (arguments.length === 0) {
  12612. KdTree.constructor_.call(this, 0.0);
  12613. } else if (arguments.length === 1) {
  12614. const tolerance = arguments[0];
  12615. this._tolerance = tolerance;
  12616. }
  12617. }
  12618. static toCoordinates() {
  12619. if (arguments.length === 1) {
  12620. const kdnodes = arguments[0];
  12621. return KdTree.toCoordinates(kdnodes, false);
  12622. } else if (arguments.length === 2) {
  12623. const kdnodes = arguments[0],
  12624. includeRepeated = arguments[1];
  12625. const coord = new CoordinateList();
  12626. for (let it = kdnodes.iterator(); it.hasNext();) {
  12627. const node = it.next();
  12628. const count = includeRepeated ? node.getCount() : 1;
  12629. for (let i = 0; i < count; i++) coord.add(node.getCoordinate(), true);
  12630. }
  12631. return coord.toCoordinateArray();
  12632. }
  12633. }
  12634. insert() {
  12635. if (arguments.length === 1) {
  12636. const p = arguments[0];
  12637. return this.insert(p, null);
  12638. } else if (arguments.length === 2) {
  12639. const p = arguments[0],
  12640. data = arguments[1];
  12641. if (this._root === null) {
  12642. this._root = new KdNode(p, data);
  12643. return this._root;
  12644. }
  12645. if (this._tolerance > 0) {
  12646. const matchNode = this.findBestMatchNode(p);
  12647. if (matchNode !== null) {
  12648. matchNode.increment();
  12649. return matchNode;
  12650. }
  12651. }
  12652. return this.insertExact(p, data);
  12653. }
  12654. }
  12655. query() {
  12656. if (arguments.length === 1) {
  12657. const queryEnv = arguments[0];
  12658. const result = new ArrayList();
  12659. this.query(queryEnv, result);
  12660. return result;
  12661. } else if (arguments.length === 2) {
  12662. if (arguments[0] instanceof Envelope && hasInterface(arguments[1], List)) {
  12663. const queryEnv = arguments[0],
  12664. result = arguments[1];
  12665. this.queryNode(this._root, queryEnv, true, new class {
  12666. get interfaces_() {
  12667. return [KdNodeVisitor];
  12668. }
  12669. visit(node) {
  12670. result.add(node);
  12671. }
  12672. }());
  12673. } else if (arguments[0] instanceof Envelope && hasInterface(arguments[1], KdNodeVisitor)) {
  12674. const queryEnv = arguments[0],
  12675. visitor = arguments[1];
  12676. this.queryNode(this._root, queryEnv, true, visitor);
  12677. }
  12678. }
  12679. }
  12680. queryNode(currentNode, queryEnv, odd, visitor) {
  12681. if (currentNode === null) return null;
  12682. let min = null;
  12683. let max = null;
  12684. let discriminant = null;
  12685. if (odd) {
  12686. min = queryEnv.getMinX();
  12687. max = queryEnv.getMaxX();
  12688. discriminant = currentNode.getX();
  12689. } else {
  12690. min = queryEnv.getMinY();
  12691. max = queryEnv.getMaxY();
  12692. discriminant = currentNode.getY();
  12693. }
  12694. const searchLeft = min < discriminant;
  12695. const searchRight = discriminant <= max;
  12696. if (searchLeft) this.queryNode(currentNode.getLeft(), queryEnv, !odd, visitor);
  12697. if (queryEnv.contains(currentNode.getCoordinate())) visitor.visit(currentNode);
  12698. if (searchRight) this.queryNode(currentNode.getRight(), queryEnv, !odd, visitor);
  12699. }
  12700. findBestMatchNode(p) {
  12701. const visitor = new BestMatchVisitor(p, this._tolerance);
  12702. this.query(visitor.queryEnvelope(), visitor);
  12703. return visitor.getNode();
  12704. }
  12705. isEmpty() {
  12706. if (this._root === null) return true;
  12707. return false;
  12708. }
  12709. insertExact(p, data) {
  12710. let currentNode = this._root;
  12711. let leafNode = this._root;
  12712. let isOddLevel = true;
  12713. let isLessThan = true;
  12714. while (currentNode !== null) {
  12715. if (currentNode !== null) {
  12716. const isInTolerance = p.distance(currentNode.getCoordinate()) <= this._tolerance;
  12717. if (isInTolerance) {
  12718. currentNode.increment();
  12719. return currentNode;
  12720. }
  12721. }
  12722. if (isOddLevel) isLessThan = p.x < currentNode.getX();else isLessThan = p.y < currentNode.getY();
  12723. leafNode = currentNode;
  12724. if (isLessThan) currentNode = currentNode.getLeft();else currentNode = currentNode.getRight();
  12725. isOddLevel = !isOddLevel;
  12726. }
  12727. this._numberOfNodes = this._numberOfNodes + 1;
  12728. const node = new KdNode(p, data);
  12729. if (isLessThan) leafNode.setLeft(node);else leafNode.setRight(node);
  12730. return node;
  12731. }
  12732. }
  12733. class BestMatchVisitor {
  12734. constructor() {
  12735. BestMatchVisitor.constructor_.apply(this, arguments);
  12736. }
  12737. static constructor_() {
  12738. this._tolerance = null;
  12739. this._matchNode = null;
  12740. this._matchDist = 0.0;
  12741. this._p = null;
  12742. const p = arguments[0],
  12743. tolerance = arguments[1];
  12744. this._p = p;
  12745. this._tolerance = tolerance;
  12746. }
  12747. visit(node) {
  12748. const dist = this._p.distance(node.getCoordinate());
  12749. const isInTolerance = dist <= this._tolerance;
  12750. if (!isInTolerance) return null;
  12751. let update = false;
  12752. if (this._matchNode === null || dist < this._matchDist || this._matchNode !== null && dist === this._matchDist && node.getCoordinate().compareTo(this._matchNode.getCoordinate()) < 1) update = true;
  12753. if (update) {
  12754. this._matchNode = node;
  12755. this._matchDist = dist;
  12756. }
  12757. }
  12758. queryEnvelope() {
  12759. const queryEnv = new Envelope(this._p);
  12760. queryEnv.expandBy(this._tolerance);
  12761. return queryEnv;
  12762. }
  12763. getNode() {
  12764. return this._matchNode;
  12765. }
  12766. get interfaces_() {
  12767. return [KdNodeVisitor];
  12768. }
  12769. }
  12770. KdTree.BestMatchVisitor = BestMatchVisitor;
  12771. var kdtree = /*#__PURE__*/Object.freeze({
  12772. __proto__: null,
  12773. KdTree: KdTree
  12774. });
  12775. class NodeBase {
  12776. constructor() {
  12777. NodeBase.constructor_.apply(this, arguments);
  12778. }
  12779. static constructor_() {
  12780. this._items = new ArrayList();
  12781. this._subnode = new Array(4).fill(null);
  12782. }
  12783. static getSubnodeIndex(env, centrex, centrey) {
  12784. let subnodeIndex = -1;
  12785. if (env.getMinX() >= centrex) {
  12786. if (env.getMinY() >= centrey) subnodeIndex = 3;
  12787. if (env.getMaxY() <= centrey) subnodeIndex = 1;
  12788. }
  12789. if (env.getMaxX() <= centrex) {
  12790. if (env.getMinY() >= centrey) subnodeIndex = 2;
  12791. if (env.getMaxY() <= centrey) subnodeIndex = 0;
  12792. }
  12793. return subnodeIndex;
  12794. }
  12795. hasChildren() {
  12796. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) return true;
  12797. return false;
  12798. }
  12799. isPrunable() {
  12800. return !(this.hasChildren() || this.hasItems());
  12801. }
  12802. addAllItems(resultItems) {
  12803. resultItems.addAll(this._items);
  12804. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) this._subnode[i].addAllItems(resultItems);
  12805. return resultItems;
  12806. }
  12807. getNodeCount() {
  12808. let subSize = 0;
  12809. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) subSize += this._subnode[i].size();
  12810. return subSize + 1;
  12811. }
  12812. size() {
  12813. let subSize = 0;
  12814. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) subSize += this._subnode[i].size();
  12815. return subSize + this._items.size();
  12816. }
  12817. addAllItemsFromOverlapping(searchEnv, resultItems) {
  12818. if (!this.isSearchMatch(searchEnv)) return null;
  12819. resultItems.addAll(this._items);
  12820. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) this._subnode[i].addAllItemsFromOverlapping(searchEnv, resultItems);
  12821. }
  12822. visitItems(searchEnv, visitor) {
  12823. for (let i = this._items.iterator(); i.hasNext();) visitor.visitItem(i.next());
  12824. }
  12825. hasItems() {
  12826. return !this._items.isEmpty();
  12827. }
  12828. remove(itemEnv, item) {
  12829. if (!this.isSearchMatch(itemEnv)) return false;
  12830. let found = false;
  12831. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) {
  12832. found = this._subnode[i].remove(itemEnv, item);
  12833. if (found) {
  12834. if (this._subnode[i].isPrunable()) this._subnode[i] = null;
  12835. break;
  12836. }
  12837. }
  12838. if (found) return found;
  12839. found = this._items.remove(item);
  12840. return found;
  12841. }
  12842. visit(searchEnv, visitor) {
  12843. if (!this.isSearchMatch(searchEnv)) return null;
  12844. this.visitItems(searchEnv, visitor);
  12845. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) this._subnode[i].visit(searchEnv, visitor);
  12846. }
  12847. getItems() {
  12848. return this._items;
  12849. }
  12850. depth() {
  12851. let maxSubDepth = 0;
  12852. for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) {
  12853. const sqd = this._subnode[i].depth();
  12854. if (sqd > maxSubDepth) maxSubDepth = sqd;
  12855. }
  12856. return maxSubDepth + 1;
  12857. }
  12858. isEmpty() {
  12859. let isEmpty = true;
  12860. if (!this._items.isEmpty()) isEmpty = false;else for (let i = 0; i < 4; i++) if (this._subnode[i] !== null) if (!this._subnode[i].isEmpty()) {
  12861. isEmpty = false;
  12862. break;
  12863. }
  12864. return isEmpty;
  12865. }
  12866. add(item) {
  12867. this._items.add(item);
  12868. }
  12869. get interfaces_() {
  12870. return [Serializable];
  12871. }
  12872. }
  12873. function DoubleBits() {}
  12874. DoubleBits.exponent = function (d) {
  12875. return CVTFWD(64, d) - 1023;
  12876. };
  12877. DoubleBits.powerOf2 = function (exp) {
  12878. return Math.pow(2, exp);
  12879. };
  12880. /**
  12881. * Calculates the exponent of the bit-pattern for a number. Uses code from:
  12882. * http://www.merlyn.demon.co.uk/js-exact.htm
  12883. *
  12884. * @param {Number}
  12885. * NumW 32 or 64 to denote the number of bits.
  12886. * @param {Number}
  12887. * Qty the number to calculate the bit pattern for.
  12888. * @return {Number} The integer value of the exponent.
  12889. * @private
  12890. */
  12891. function CVTFWD(NumW, Qty) {
  12892. let Sign;
  12893. let Expo;
  12894. let Mant;
  12895. let Bin;
  12896. const Inf = {
  12897. 32: {
  12898. d: 0x7F,
  12899. c: 0x80,
  12900. b: 0,
  12901. a: 0
  12902. },
  12903. 64: {
  12904. d: 0x7FF0,
  12905. c: 0,
  12906. b: 0,
  12907. a: 0
  12908. }
  12909. };
  12910. const ExW = {
  12911. 32: 8,
  12912. 64: 11
  12913. }[NumW];
  12914. if (!Bin) {
  12915. Sign = Qty < 0 || 1 / Qty < 0; // OK for +-0
  12916. if (!isFinite(Qty)) {
  12917. Bin = Inf[NumW];
  12918. if (Sign) Bin.d += 1 << NumW / 4 - 1;
  12919. Expo = Math.pow(2, ExW) - 1;
  12920. Mant = 0;
  12921. }
  12922. }
  12923. if (!Bin) {
  12924. Expo = {
  12925. 32: 127,
  12926. 64: 1023
  12927. }[NumW];
  12928. Mant = Math.abs(Qty);
  12929. while (Mant >= 2) {
  12930. Expo++;
  12931. Mant /= 2;
  12932. }
  12933. while (Mant < 1 && Expo > 0) {
  12934. Expo--;
  12935. Mant *= 2;
  12936. }
  12937. if (Expo <= 0) Mant /= 2;
  12938. if (NumW === 32 && Expo > 254) {
  12939. Bin = {
  12940. d: Sign ? 0xFF : 0x7F,
  12941. c: 0x80,
  12942. b: 0,
  12943. a: 0
  12944. };
  12945. Expo = Math.pow(2, ExW) - 1;
  12946. Mant = 0;
  12947. }
  12948. }
  12949. return Expo;
  12950. }
  12951. class Key {
  12952. constructor() {
  12953. Key.constructor_.apply(this, arguments);
  12954. }
  12955. static constructor_() {
  12956. this._pt = new Coordinate();
  12957. this._level = 0;
  12958. this._env = null;
  12959. const itemEnv = arguments[0];
  12960. this.computeKey(itemEnv);
  12961. }
  12962. static computeQuadLevel(env) {
  12963. const dx = env.getWidth();
  12964. const dy = env.getHeight();
  12965. const dMax = dx > dy ? dx : dy;
  12966. const level = DoubleBits.exponent(dMax) + 1;
  12967. return level;
  12968. }
  12969. getLevel() {
  12970. return this._level;
  12971. }
  12972. computeKey() {
  12973. if (arguments.length === 1) {
  12974. const itemEnv = arguments[0];
  12975. this._level = Key.computeQuadLevel(itemEnv);
  12976. this._env = new Envelope();
  12977. this.computeKey(this._level, itemEnv);
  12978. while (!this._env.contains(itemEnv)) {
  12979. this._level += 1;
  12980. this.computeKey(this._level, itemEnv);
  12981. }
  12982. } else if (arguments.length === 2) {
  12983. const level = arguments[0],
  12984. itemEnv = arguments[1];
  12985. const quadSize = DoubleBits.powerOf2(level);
  12986. this._pt.x = Math.floor(itemEnv.getMinX() / quadSize) * quadSize;
  12987. this._pt.y = Math.floor(itemEnv.getMinY() / quadSize) * quadSize;
  12988. this._env.init(this._pt.x, this._pt.x + quadSize, this._pt.y, this._pt.y + quadSize);
  12989. }
  12990. }
  12991. getEnvelope() {
  12992. return this._env;
  12993. }
  12994. getCentre() {
  12995. return new Coordinate((this._env.getMinX() + this._env.getMaxX()) / 2, (this._env.getMinY() + this._env.getMaxY()) / 2);
  12996. }
  12997. getPoint() {
  12998. return this._pt;
  12999. }
  13000. }
  13001. class Node$1 extends NodeBase {
  13002. constructor() {
  13003. super();
  13004. Node$1.constructor_.apply(this, arguments);
  13005. }
  13006. static constructor_() {
  13007. this._env = null;
  13008. this._centrex = null;
  13009. this._centrey = null;
  13010. this._level = null;
  13011. const env = arguments[0],
  13012. level = arguments[1];
  13013. this._env = env;
  13014. this._level = level;
  13015. this._centrex = (env.getMinX() + env.getMaxX()) / 2;
  13016. this._centrey = (env.getMinY() + env.getMaxY()) / 2;
  13017. }
  13018. static createNode(env) {
  13019. const key = new Key(env);
  13020. const node = new Node$1(key.getEnvelope(), key.getLevel());
  13021. return node;
  13022. }
  13023. static createExpanded(node, addEnv) {
  13024. const expandEnv = new Envelope(addEnv);
  13025. if (node !== null) expandEnv.expandToInclude(node._env);
  13026. const largerNode = Node$1.createNode(expandEnv);
  13027. if (node !== null) largerNode.insertNode(node);
  13028. return largerNode;
  13029. }
  13030. find(searchEnv) {
  13031. const subnodeIndex = NodeBase.getSubnodeIndex(searchEnv, this._centrex, this._centrey);
  13032. if (subnodeIndex === -1) return this;
  13033. if (this._subnode[subnodeIndex] !== null) {
  13034. const node = this._subnode[subnodeIndex];
  13035. return node.find(searchEnv);
  13036. }
  13037. return this;
  13038. }
  13039. isSearchMatch(searchEnv) {
  13040. if (searchEnv === null) return false;
  13041. return this._env.intersects(searchEnv);
  13042. }
  13043. getSubnode(index) {
  13044. if (this._subnode[index] === null) this._subnode[index] = this.createSubnode(index);
  13045. return this._subnode[index];
  13046. }
  13047. getEnvelope() {
  13048. return this._env;
  13049. }
  13050. getNode(searchEnv) {
  13051. const subnodeIndex = NodeBase.getSubnodeIndex(searchEnv, this._centrex, this._centrey);
  13052. if (subnodeIndex !== -1) {
  13053. const node = this.getSubnode(subnodeIndex);
  13054. return node.getNode(searchEnv);
  13055. } else {
  13056. return this;
  13057. }
  13058. }
  13059. createSubnode(index) {
  13060. let minx = 0.0;
  13061. let maxx = 0.0;
  13062. let miny = 0.0;
  13063. let maxy = 0.0;
  13064. switch (index) {
  13065. case 0:
  13066. minx = this._env.getMinX();
  13067. maxx = this._centrex;
  13068. miny = this._env.getMinY();
  13069. maxy = this._centrey;
  13070. break;
  13071. case 1:
  13072. minx = this._centrex;
  13073. maxx = this._env.getMaxX();
  13074. miny = this._env.getMinY();
  13075. maxy = this._centrey;
  13076. break;
  13077. case 2:
  13078. minx = this._env.getMinX();
  13079. maxx = this._centrex;
  13080. miny = this._centrey;
  13081. maxy = this._env.getMaxY();
  13082. break;
  13083. case 3:
  13084. minx = this._centrex;
  13085. maxx = this._env.getMaxX();
  13086. miny = this._centrey;
  13087. maxy = this._env.getMaxY();
  13088. break;
  13089. }
  13090. const sqEnv = new Envelope(minx, maxx, miny, maxy);
  13091. const node = new Node$1(sqEnv, this._level - 1);
  13092. return node;
  13093. }
  13094. insertNode(node) {
  13095. Assert.isTrue(this._env === null || this._env.contains(node._env));
  13096. const index = NodeBase.getSubnodeIndex(node._env, this._centrex, this._centrey);
  13097. if (node._level === this._level - 1) {
  13098. this._subnode[index] = node;
  13099. } else {
  13100. const childNode = this.createSubnode(index);
  13101. childNode.insertNode(node);
  13102. this._subnode[index] = childNode;
  13103. }
  13104. }
  13105. }
  13106. class IntervalSize {
  13107. static isZeroWidth(min, max) {
  13108. const width = max - min;
  13109. if (width === 0.0) return true;
  13110. const maxAbs = Math.max(Math.abs(min), Math.abs(max));
  13111. const scaledInterval = width / maxAbs;
  13112. const level = DoubleBits.exponent(scaledInterval);
  13113. return level <= IntervalSize.MIN_BINARY_EXPONENT;
  13114. }
  13115. }
  13116. IntervalSize.MIN_BINARY_EXPONENT = -50;
  13117. class Root extends NodeBase {
  13118. constructor() {
  13119. super();
  13120. }
  13121. insert(itemEnv, item) {
  13122. const index = NodeBase.getSubnodeIndex(itemEnv, Root.origin.x, Root.origin.y);
  13123. if (index === -1) {
  13124. this.add(item);
  13125. return null;
  13126. }
  13127. const node = this._subnode[index];
  13128. if (node === null || !node.getEnvelope().contains(itemEnv)) {
  13129. const largerNode = Node$1.createExpanded(node, itemEnv);
  13130. this._subnode[index] = largerNode;
  13131. }
  13132. this.insertContained(this._subnode[index], itemEnv, item);
  13133. }
  13134. isSearchMatch(searchEnv) {
  13135. return true;
  13136. }
  13137. insertContained(tree, itemEnv, item) {
  13138. Assert.isTrue(tree.getEnvelope().contains(itemEnv));
  13139. const isZeroX = IntervalSize.isZeroWidth(itemEnv.getMinX(), itemEnv.getMaxX());
  13140. const isZeroY = IntervalSize.isZeroWidth(itemEnv.getMinY(), itemEnv.getMaxY());
  13141. let node = null;
  13142. if (isZeroX || isZeroY) node = tree.find(itemEnv);else node = tree.getNode(itemEnv);
  13143. node.add(item);
  13144. }
  13145. }
  13146. Root.origin = new Coordinate(0.0, 0.0);
  13147. class SpatialIndex {
  13148. insert(itemEnv, item) {}
  13149. remove(itemEnv, item) {}
  13150. query() {
  13151. }
  13152. }
  13153. class Quadtree {
  13154. constructor() {
  13155. Quadtree.constructor_.apply(this, arguments);
  13156. }
  13157. static constructor_() {
  13158. this._root = null;
  13159. this._minExtent = 1.0;
  13160. this._root = new Root();
  13161. }
  13162. static ensureExtent(itemEnv, minExtent) {
  13163. let minx = itemEnv.getMinX();
  13164. let maxx = itemEnv.getMaxX();
  13165. let miny = itemEnv.getMinY();
  13166. let maxy = itemEnv.getMaxY();
  13167. if (minx !== maxx && miny !== maxy) return itemEnv;
  13168. if (minx === maxx) {
  13169. minx = minx - minExtent / 2.0;
  13170. maxx = maxx + minExtent / 2.0;
  13171. }
  13172. if (miny === maxy) {
  13173. miny = miny - minExtent / 2.0;
  13174. maxy = maxy + minExtent / 2.0;
  13175. }
  13176. return new Envelope(minx, maxx, miny, maxy);
  13177. }
  13178. size() {
  13179. if (this._root !== null) return this._root.size();
  13180. return 0;
  13181. }
  13182. insert(itemEnv, item) {
  13183. this.collectStats(itemEnv);
  13184. const insertEnv = Quadtree.ensureExtent(itemEnv, this._minExtent);
  13185. this._root.insert(insertEnv, item);
  13186. }
  13187. query() {
  13188. if (arguments.length === 1) {
  13189. const searchEnv = arguments[0];
  13190. const visitor = new ArrayListVisitor();
  13191. this.query(searchEnv, visitor);
  13192. return visitor.getItems();
  13193. } else if (arguments.length === 2) {
  13194. const searchEnv = arguments[0],
  13195. visitor = arguments[1];
  13196. this._root.visit(searchEnv, visitor);
  13197. }
  13198. }
  13199. queryAll() {
  13200. const foundItems = new ArrayList();
  13201. this._root.addAllItems(foundItems);
  13202. return foundItems;
  13203. }
  13204. remove(itemEnv, item) {
  13205. const posEnv = Quadtree.ensureExtent(itemEnv, this._minExtent);
  13206. return this._root.remove(posEnv, item);
  13207. }
  13208. collectStats(itemEnv) {
  13209. const delX = itemEnv.getWidth();
  13210. if (delX < this._minExtent && delX > 0.0) this._minExtent = delX;
  13211. const delY = itemEnv.getHeight();
  13212. if (delY < this._minExtent && delY > 0.0) this._minExtent = delY;
  13213. }
  13214. depth() {
  13215. if (this._root !== null) return this._root.depth();
  13216. return 0;
  13217. }
  13218. isEmpty() {
  13219. if (this._root === null) return true;
  13220. return this._root.isEmpty();
  13221. }
  13222. get interfaces_() {
  13223. return [SpatialIndex, Serializable];
  13224. }
  13225. }
  13226. var quadtree = /*#__PURE__*/Object.freeze({
  13227. __proto__: null,
  13228. Quadtree: Quadtree
  13229. });
  13230. class Boundable {
  13231. getBounds() {}
  13232. }
  13233. class ItemBoundable {
  13234. constructor() {
  13235. ItemBoundable.constructor_.apply(this, arguments);
  13236. }
  13237. static constructor_() {
  13238. this._bounds = null;
  13239. this._item = null;
  13240. const bounds = arguments[0],
  13241. item = arguments[1];
  13242. this._bounds = bounds;
  13243. this._item = item;
  13244. }
  13245. getItem() {
  13246. return this._item;
  13247. }
  13248. getBounds() {
  13249. return this._bounds;
  13250. }
  13251. get interfaces_() {
  13252. return [Boundable, Serializable];
  13253. }
  13254. }
  13255. class PriorityQueue {
  13256. constructor() {
  13257. PriorityQueue.constructor_.apply(this, arguments);
  13258. }
  13259. static constructor_() {
  13260. this._size = null;
  13261. this._items = null;
  13262. this._size = 0;
  13263. this._items = new ArrayList();
  13264. this._items.add(null);
  13265. }
  13266. poll() {
  13267. if (this.isEmpty()) return null;
  13268. const minItem = this._items.get(1);
  13269. this._items.set(1, this._items.get(this._size));
  13270. this._size -= 1;
  13271. this.reorder(1);
  13272. return minItem;
  13273. }
  13274. size() {
  13275. return this._size;
  13276. }
  13277. reorder(hole) {
  13278. let child = null;
  13279. const tmp = this._items.get(hole);
  13280. for (; hole * 2 <= this._size; hole = child) {
  13281. child = hole * 2;
  13282. if (child !== this._size && this._items.get(child + 1).compareTo(this._items.get(child)) < 0) child++;
  13283. if (this._items.get(child).compareTo(tmp) < 0) this._items.set(hole, this._items.get(child));else break;
  13284. }
  13285. this._items.set(hole, tmp);
  13286. }
  13287. clear() {
  13288. this._size = 0;
  13289. this._items.clear();
  13290. }
  13291. peek() {
  13292. if (this.isEmpty()) return null;
  13293. const minItem = this._items.get(1);
  13294. return minItem;
  13295. }
  13296. isEmpty() {
  13297. return this._size === 0;
  13298. }
  13299. add(x) {
  13300. this._items.add(null);
  13301. this._size += 1;
  13302. let hole = this._size;
  13303. this._items.set(0, x);
  13304. for (; x.compareTo(this._items.get(Math.trunc(hole / 2))) < 0; hole /= 2) this._items.set(hole, this._items.get(Math.trunc(hole / 2)));
  13305. this._items.set(hole, x);
  13306. }
  13307. }
  13308. class AbstractNode {
  13309. constructor() {
  13310. AbstractNode.constructor_.apply(this, arguments);
  13311. }
  13312. static constructor_() {
  13313. this._childBoundables = new ArrayList();
  13314. this._bounds = null;
  13315. this._level = null;
  13316. if (arguments.length === 0) ; else if (arguments.length === 1) {
  13317. const level = arguments[0];
  13318. this._level = level;
  13319. }
  13320. }
  13321. getLevel() {
  13322. return this._level;
  13323. }
  13324. size() {
  13325. return this._childBoundables.size();
  13326. }
  13327. getChildBoundables() {
  13328. return this._childBoundables;
  13329. }
  13330. addChildBoundable(childBoundable) {
  13331. Assert.isTrue(this._bounds === null);
  13332. this._childBoundables.add(childBoundable);
  13333. }
  13334. isEmpty() {
  13335. return this._childBoundables.isEmpty();
  13336. }
  13337. getBounds() {
  13338. if (this._bounds === null) this._bounds = this.computeBounds();
  13339. return this._bounds;
  13340. }
  13341. get interfaces_() {
  13342. return [Boundable, Serializable];
  13343. }
  13344. }
  13345. class EnvelopeDistance {
  13346. static maxDistance(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
  13347. let dist = EnvelopeDistance.distance(ax1, ay1, bx1, by1);
  13348. dist = Math.max(dist, EnvelopeDistance.distance(ax1, ay1, bx2, by2));
  13349. dist = Math.max(dist, EnvelopeDistance.distance(ax2, ay2, bx1, by1));
  13350. dist = Math.max(dist, EnvelopeDistance.distance(ax2, ay2, bx2, by2));
  13351. return dist;
  13352. }
  13353. static distance(x1, y1, x2, y2) {
  13354. const dx = x2 - x1;
  13355. const dy = y2 - y1;
  13356. return Math.sqrt(dx * dx + dy * dy);
  13357. }
  13358. static maximumDistance(env1, env2) {
  13359. const minx = Math.min(env1.getMinX(), env2.getMinX());
  13360. const miny = Math.min(env1.getMinY(), env2.getMinY());
  13361. const maxx = Math.max(env1.getMaxX(), env2.getMaxX());
  13362. const maxy = Math.max(env1.getMaxY(), env2.getMaxY());
  13363. return EnvelopeDistance.distance(minx, miny, maxx, maxy);
  13364. }
  13365. static minMaxDistance(a, b) {
  13366. const aminx = a.getMinX();
  13367. const aminy = a.getMinY();
  13368. const amaxx = a.getMaxX();
  13369. const amaxy = a.getMaxY();
  13370. const bminx = b.getMinX();
  13371. const bminy = b.getMinY();
  13372. const bmaxx = b.getMaxX();
  13373. const bmaxy = b.getMaxY();
  13374. let dist = EnvelopeDistance.maxDistance(aminx, aminy, aminx, amaxy, bminx, bminy, bminx, bmaxy);
  13375. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, aminx, amaxy, bminx, bminy, bmaxx, bminy));
  13376. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, aminx, amaxy, bmaxx, bmaxy, bminx, bmaxy));
  13377. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, aminx, amaxy, bmaxx, bmaxy, bmaxx, bminy));
  13378. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, amaxx, aminy, bminx, bminy, bminx, bmaxy));
  13379. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, amaxx, aminy, bminx, bminy, bmaxx, bminy));
  13380. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, amaxx, aminy, bmaxx, bmaxy, bminx, bmaxy));
  13381. dist = Math.min(dist, EnvelopeDistance.maxDistance(aminx, aminy, amaxx, aminy, bmaxx, bmaxy, bmaxx, bminy));
  13382. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, aminx, amaxy, bminx, bminy, bminx, bmaxy));
  13383. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, aminx, amaxy, bminx, bminy, bmaxx, bminy));
  13384. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, aminx, amaxy, bmaxx, bmaxy, bminx, bmaxy));
  13385. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, aminx, amaxy, bmaxx, bmaxy, bmaxx, bminy));
  13386. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, amaxx, aminy, bminx, bminy, bminx, bmaxy));
  13387. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, amaxx, aminy, bminx, bminy, bmaxx, bminy));
  13388. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, amaxx, aminy, bmaxx, bmaxy, bminx, bmaxy));
  13389. dist = Math.min(dist, EnvelopeDistance.maxDistance(amaxx, amaxy, amaxx, aminy, bmaxx, bmaxy, bmaxx, bminy));
  13390. return dist;
  13391. }
  13392. }
  13393. class BoundablePair {
  13394. constructor() {
  13395. BoundablePair.constructor_.apply(this, arguments);
  13396. }
  13397. static constructor_() {
  13398. this._boundable1 = null;
  13399. this._boundable2 = null;
  13400. this._distance = null;
  13401. this._itemDistance = null;
  13402. const boundable1 = arguments[0],
  13403. boundable2 = arguments[1],
  13404. itemDistance = arguments[2];
  13405. this._boundable1 = boundable1;
  13406. this._boundable2 = boundable2;
  13407. this._itemDistance = itemDistance;
  13408. this._distance = this.distance();
  13409. }
  13410. static area(b) {
  13411. return b.getBounds().getArea();
  13412. }
  13413. static isComposite(item) {
  13414. return item instanceof AbstractNode;
  13415. }
  13416. maximumDistance() {
  13417. return EnvelopeDistance.maximumDistance(this._boundable1.getBounds(), this._boundable2.getBounds());
  13418. }
  13419. expandToQueue(priQ, minDistance) {
  13420. const isComp1 = BoundablePair.isComposite(this._boundable1);
  13421. const isComp2 = BoundablePair.isComposite(this._boundable2);
  13422. if (isComp1 && isComp2) {
  13423. if (BoundablePair.area(this._boundable1) > BoundablePair.area(this._boundable2)) {
  13424. this.expand(this._boundable1, this._boundable2, false, priQ, minDistance);
  13425. return null;
  13426. } else {
  13427. this.expand(this._boundable2, this._boundable1, true, priQ, minDistance);
  13428. return null;
  13429. }
  13430. } else if (isComp1) {
  13431. this.expand(this._boundable1, this._boundable2, false, priQ, minDistance);
  13432. return null;
  13433. } else if (isComp2) {
  13434. this.expand(this._boundable2, this._boundable1, true, priQ, minDistance);
  13435. return null;
  13436. }
  13437. throw new IllegalArgumentException('neither boundable is composite');
  13438. }
  13439. isLeaves() {
  13440. return !(BoundablePair.isComposite(this._boundable1) || BoundablePair.isComposite(this._boundable2));
  13441. }
  13442. compareTo(o) {
  13443. const nd = o;
  13444. if (this._distance < nd._distance) return -1;
  13445. if (this._distance > nd._distance) return 1;
  13446. return 0;
  13447. }
  13448. expand(bndComposite, bndOther, isFlipped, priQ, minDistance) {
  13449. const children = bndComposite.getChildBoundables();
  13450. for (let i = children.iterator(); i.hasNext();) {
  13451. const child = i.next();
  13452. let bp = null;
  13453. if (isFlipped) bp = new BoundablePair(bndOther, child, this._itemDistance);else bp = new BoundablePair(child, bndOther, this._itemDistance);
  13454. if (bp.getDistance() < minDistance) priQ.add(bp);
  13455. }
  13456. }
  13457. getBoundable(i) {
  13458. if (i === 0) return this._boundable1;
  13459. return this._boundable2;
  13460. }
  13461. getDistance() {
  13462. return this._distance;
  13463. }
  13464. distance() {
  13465. if (this.isLeaves()) return this._itemDistance.distance(this._boundable1, this._boundable2);
  13466. return this._boundable1.getBounds().distance(this._boundable2.getBounds());
  13467. }
  13468. get interfaces_() {
  13469. return [Comparable];
  13470. }
  13471. }
  13472. class AbstractSTRtree {
  13473. constructor() {
  13474. AbstractSTRtree.constructor_.apply(this, arguments);
  13475. }
  13476. static constructor_() {
  13477. this._root = null;
  13478. this._built = false;
  13479. this._itemBoundables = new ArrayList();
  13480. this._nodeCapacity = null;
  13481. if (arguments.length === 0) {
  13482. AbstractSTRtree.constructor_.call(this, AbstractSTRtree.DEFAULT_NODE_CAPACITY);
  13483. } else if (arguments.length === 1) {
  13484. const nodeCapacity = arguments[0];
  13485. Assert.isTrue(nodeCapacity > 1, 'Node capacity must be greater than 1');
  13486. this._nodeCapacity = nodeCapacity;
  13487. }
  13488. }
  13489. static compareDoubles(a, b) {
  13490. return a > b ? 1 : a < b ? -1 : 0;
  13491. }
  13492. queryInternal() {
  13493. if (hasInterface(arguments[2], ItemVisitor) && arguments[0] instanceof Object && arguments[1] instanceof AbstractNode) {
  13494. const searchBounds = arguments[0],
  13495. node = arguments[1],
  13496. visitor = arguments[2];
  13497. const childBoundables = node.getChildBoundables();
  13498. for (let i = 0; i < childBoundables.size(); i++) {
  13499. const childBoundable = childBoundables.get(i);
  13500. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) continue;
  13501. if (childBoundable instanceof AbstractNode) this.queryInternal(searchBounds, childBoundable, visitor);else if (childBoundable instanceof ItemBoundable) visitor.visitItem(childBoundable.getItem());else Assert.shouldNeverReachHere();
  13502. }
  13503. } else if (hasInterface(arguments[2], List) && arguments[0] instanceof Object && arguments[1] instanceof AbstractNode) {
  13504. const searchBounds = arguments[0],
  13505. node = arguments[1],
  13506. matches = arguments[2];
  13507. const childBoundables = node.getChildBoundables();
  13508. for (let i = 0; i < childBoundables.size(); i++) {
  13509. const childBoundable = childBoundables.get(i);
  13510. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) continue;
  13511. if (childBoundable instanceof AbstractNode) this.queryInternal(searchBounds, childBoundable, matches);else if (childBoundable instanceof ItemBoundable) matches.add(childBoundable.getItem());else Assert.shouldNeverReachHere();
  13512. }
  13513. }
  13514. }
  13515. getNodeCapacity() {
  13516. return this._nodeCapacity;
  13517. }
  13518. lastNode(nodes) {
  13519. return nodes.get(nodes.size() - 1);
  13520. }
  13521. size() {
  13522. if (arguments.length === 0) {
  13523. if (this.isEmpty()) return 0;
  13524. this.build();
  13525. return this.size(this._root);
  13526. } else if (arguments.length === 1) {
  13527. const node = arguments[0];
  13528. let size = 0;
  13529. for (let i = node.getChildBoundables().iterator(); i.hasNext();) {
  13530. const childBoundable = i.next();
  13531. if (childBoundable instanceof AbstractNode) size += this.size(childBoundable);else if (childBoundable instanceof ItemBoundable) size += 1;
  13532. }
  13533. return size;
  13534. }
  13535. }
  13536. removeItem(node, item) {
  13537. let childToRemove = null;
  13538. for (let i = node.getChildBoundables().iterator(); i.hasNext();) {
  13539. const childBoundable = i.next();
  13540. if (childBoundable instanceof ItemBoundable) if (childBoundable.getItem() === item) childToRemove = childBoundable;
  13541. }
  13542. if (childToRemove !== null) {
  13543. node.getChildBoundables().remove(childToRemove);
  13544. return true;
  13545. }
  13546. return false;
  13547. }
  13548. itemsTree() {
  13549. if (arguments.length === 0) {
  13550. this.build();
  13551. const valuesTree = this.itemsTree(this._root);
  13552. if (valuesTree === null) return new ArrayList();
  13553. return valuesTree;
  13554. } else if (arguments.length === 1) {
  13555. const node = arguments[0];
  13556. const valuesTreeForNode = new ArrayList();
  13557. for (let i = node.getChildBoundables().iterator(); i.hasNext();) {
  13558. const childBoundable = i.next();
  13559. if (childBoundable instanceof AbstractNode) {
  13560. const valuesTreeForChild = this.itemsTree(childBoundable);
  13561. if (valuesTreeForChild !== null) valuesTreeForNode.add(valuesTreeForChild);
  13562. } else if (childBoundable instanceof ItemBoundable) {
  13563. valuesTreeForNode.add(childBoundable.getItem());
  13564. } else {
  13565. Assert.shouldNeverReachHere();
  13566. }
  13567. }
  13568. if (valuesTreeForNode.size() <= 0) return null;
  13569. return valuesTreeForNode;
  13570. }
  13571. }
  13572. insert(bounds, item) {
  13573. Assert.isTrue(!this._built, 'Cannot insert items into an STR packed R-tree after it has been built.');
  13574. this._itemBoundables.add(new ItemBoundable(bounds, item));
  13575. }
  13576. boundablesAtLevel() {
  13577. if (arguments.length === 1) {
  13578. const level = arguments[0];
  13579. const boundables = new ArrayList();
  13580. this.boundablesAtLevel(level, this._root, boundables);
  13581. return boundables;
  13582. } else if (arguments.length === 3) {
  13583. const level = arguments[0],
  13584. top = arguments[1],
  13585. boundables = arguments[2];
  13586. Assert.isTrue(level > -2);
  13587. if (top.getLevel() === level) {
  13588. boundables.add(top);
  13589. return null;
  13590. }
  13591. for (let i = top.getChildBoundables().iterator(); i.hasNext();) {
  13592. const boundable = i.next();
  13593. if (boundable instanceof AbstractNode) {
  13594. this.boundablesAtLevel(level, boundable, boundables);
  13595. } else {
  13596. Assert.isTrue(boundable instanceof ItemBoundable);
  13597. if (level === -1) boundables.add(boundable);
  13598. }
  13599. }
  13600. return null;
  13601. }
  13602. }
  13603. query() {
  13604. if (arguments.length === 1) {
  13605. const searchBounds = arguments[0];
  13606. this.build();
  13607. const matches = new ArrayList();
  13608. if (this.isEmpty()) return matches;
  13609. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds)) this.queryInternal(searchBounds, this._root, matches);
  13610. return matches;
  13611. } else if (arguments.length === 2) {
  13612. const searchBounds = arguments[0],
  13613. visitor = arguments[1];
  13614. this.build();
  13615. if (this.isEmpty()) return null;
  13616. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds)) this.queryInternal(searchBounds, this._root, visitor);
  13617. }
  13618. }
  13619. build() {
  13620. if (this._built) return null;
  13621. this._root = this._itemBoundables.isEmpty() ? this.createNode(0) : this.createHigherLevels(this._itemBoundables, -1);
  13622. this._itemBoundables = null;
  13623. this._built = true;
  13624. }
  13625. getRoot() {
  13626. this.build();
  13627. return this._root;
  13628. }
  13629. remove() {
  13630. if (arguments.length === 2) {
  13631. const searchBounds = arguments[0],
  13632. item = arguments[1];
  13633. this.build();
  13634. if (this.getIntersectsOp().intersects(this._root.getBounds(), searchBounds)) return this.remove(searchBounds, this._root, item);
  13635. return false;
  13636. } else if (arguments.length === 3) {
  13637. const searchBounds = arguments[0],
  13638. node = arguments[1],
  13639. item = arguments[2];
  13640. let found = this.removeItem(node, item);
  13641. if (found) return true;
  13642. let childToPrune = null;
  13643. for (let i = node.getChildBoundables().iterator(); i.hasNext();) {
  13644. const childBoundable = i.next();
  13645. if (!this.getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) continue;
  13646. if (childBoundable instanceof AbstractNode) {
  13647. found = this.remove(searchBounds, childBoundable, item);
  13648. if (found) {
  13649. childToPrune = childBoundable;
  13650. break;
  13651. }
  13652. }
  13653. }
  13654. if (childToPrune !== null) if (childToPrune.getChildBoundables().isEmpty()) node.getChildBoundables().remove(childToPrune);
  13655. return found;
  13656. }
  13657. }
  13658. createHigherLevels(boundablesOfALevel, level) {
  13659. Assert.isTrue(!boundablesOfALevel.isEmpty());
  13660. const parentBoundables = this.createParentBoundables(boundablesOfALevel, level + 1);
  13661. if (parentBoundables.size() === 1) return parentBoundables.get(0);
  13662. return this.createHigherLevels(parentBoundables, level + 1);
  13663. }
  13664. depth() {
  13665. if (arguments.length === 0) {
  13666. if (this.isEmpty()) return 0;
  13667. this.build();
  13668. return this.depth(this._root);
  13669. } else if (arguments.length === 1) {
  13670. const node = arguments[0];
  13671. let maxChildDepth = 0;
  13672. for (let i = node.getChildBoundables().iterator(); i.hasNext();) {
  13673. const childBoundable = i.next();
  13674. if (childBoundable instanceof AbstractNode) {
  13675. const childDepth = this.depth(childBoundable);
  13676. if (childDepth > maxChildDepth) maxChildDepth = childDepth;
  13677. }
  13678. }
  13679. return maxChildDepth + 1;
  13680. }
  13681. }
  13682. createParentBoundables(childBoundables, newLevel) {
  13683. Assert.isTrue(!childBoundables.isEmpty());
  13684. const parentBoundables = new ArrayList();
  13685. parentBoundables.add(this.createNode(newLevel));
  13686. const sortedChildBoundables = new ArrayList(childBoundables);
  13687. Collections.sort(sortedChildBoundables, this.getComparator());
  13688. for (let i = sortedChildBoundables.iterator(); i.hasNext();) {
  13689. const childBoundable = i.next();
  13690. if (this.lastNode(parentBoundables).getChildBoundables().size() === this.getNodeCapacity()) parentBoundables.add(this.createNode(newLevel));
  13691. this.lastNode(parentBoundables).addChildBoundable(childBoundable);
  13692. }
  13693. return parentBoundables;
  13694. }
  13695. isEmpty() {
  13696. if (!this._built) return this._itemBoundables.isEmpty();
  13697. return this._root.isEmpty();
  13698. }
  13699. get interfaces_() {
  13700. return [Serializable];
  13701. }
  13702. }
  13703. function IntersectsOp$1() {}
  13704. AbstractSTRtree.IntersectsOp = IntersectsOp$1;
  13705. AbstractSTRtree.DEFAULT_NODE_CAPACITY = 10;
  13706. class ItemDistance {
  13707. distance(item1, item2) {}
  13708. }
  13709. class STRtree extends AbstractSTRtree {
  13710. constructor() {
  13711. super();
  13712. STRtree.constructor_.apply(this, arguments);
  13713. }
  13714. static constructor_() {
  13715. if (arguments.length === 0) {
  13716. STRtree.constructor_.call(this, STRtree.DEFAULT_NODE_CAPACITY);
  13717. } else if (arguments.length === 1) {
  13718. const nodeCapacity = arguments[0];
  13719. AbstractSTRtree.constructor_.call(this, nodeCapacity);
  13720. }
  13721. }
  13722. static centreX(e) {
  13723. return STRtree.avg(e.getMinX(), e.getMaxX());
  13724. }
  13725. static avg(a, b) {
  13726. return (a + b) / 2;
  13727. }
  13728. static getItems(kNearestNeighbors) {
  13729. const items = new Array(kNearestNeighbors.size()).fill(null);
  13730. let count = 0;
  13731. while (!kNearestNeighbors.isEmpty()) {
  13732. const bp = kNearestNeighbors.poll();
  13733. items[count] = bp.getBoundable(0).getItem();
  13734. count++;
  13735. }
  13736. return items;
  13737. }
  13738. static centreY(e) {
  13739. return STRtree.avg(e.getMinY(), e.getMaxY());
  13740. }
  13741. createParentBoundablesFromVerticalSlices(verticalSlices, newLevel) {
  13742. Assert.isTrue(verticalSlices.length > 0);
  13743. const parentBoundables = new ArrayList();
  13744. for (let i = 0; i < verticalSlices.length; i++) parentBoundables.addAll(this.createParentBoundablesFromVerticalSlice(verticalSlices[i], newLevel));
  13745. return parentBoundables;
  13746. }
  13747. nearestNeighbourK() {
  13748. if (arguments.length === 2) {
  13749. const initBndPair = arguments[0],
  13750. k = arguments[1];
  13751. return this.nearestNeighbourK(initBndPair, Double.POSITIVE_INFINITY, k);
  13752. } else if (arguments.length === 3) {
  13753. const initBndPair = arguments[0],
  13754. maxDistance = arguments[1],
  13755. k = arguments[2];
  13756. let distanceLowerBound = maxDistance;
  13757. const priQ = new PriorityQueue();
  13758. priQ.add(initBndPair);
  13759. const kNearestNeighbors = new PriorityQueue();
  13760. while (!priQ.isEmpty() && distanceLowerBound >= 0.0) {
  13761. const bndPair = priQ.poll();
  13762. const pairDistance = bndPair.getDistance();
  13763. if (pairDistance >= distanceLowerBound) break;
  13764. if (bndPair.isLeaves()) {
  13765. if (kNearestNeighbors.size() < k) {
  13766. kNearestNeighbors.add(bndPair);
  13767. } else {
  13768. const bp1 = kNearestNeighbors.peek();
  13769. if (bp1.getDistance() > pairDistance) {
  13770. kNearestNeighbors.poll();
  13771. kNearestNeighbors.add(bndPair);
  13772. }
  13773. const bp2 = kNearestNeighbors.peek();
  13774. distanceLowerBound = bp2.getDistance();
  13775. }
  13776. } else bndPair.expandToQueue(priQ, distanceLowerBound);
  13777. }
  13778. return STRtree.getItems(kNearestNeighbors);
  13779. }
  13780. }
  13781. createNode(level) {
  13782. return new STRtreeNode(level);
  13783. }
  13784. size() {
  13785. if (arguments.length === 0) return super.size.call(this);else return super.size.apply(this, arguments);
  13786. }
  13787. insert() {
  13788. if (arguments.length === 2 && arguments[1] instanceof Object && arguments[0] instanceof Envelope) {
  13789. const itemEnv = arguments[0],
  13790. item = arguments[1];
  13791. if (itemEnv.isNull()) return null;
  13792. super.insert.call(this, itemEnv, item);
  13793. } else {
  13794. return super.insert.apply(this, arguments);
  13795. }
  13796. }
  13797. getIntersectsOp() {
  13798. return STRtree.intersectsOp;
  13799. }
  13800. verticalSlices(childBoundables, sliceCount) {
  13801. const sliceCapacity = Math.trunc(Math.ceil(childBoundables.size() / sliceCount));
  13802. const slices = new Array(sliceCount).fill(null);
  13803. const i = childBoundables.iterator();
  13804. for (let j = 0; j < sliceCount; j++) {
  13805. slices[j] = new ArrayList();
  13806. let boundablesAddedToSlice = 0;
  13807. while (i.hasNext() && boundablesAddedToSlice < sliceCapacity) {
  13808. const childBoundable = i.next();
  13809. slices[j].add(childBoundable);
  13810. boundablesAddedToSlice++;
  13811. }
  13812. }
  13813. return slices;
  13814. }
  13815. query() {
  13816. if (arguments.length === 1) {
  13817. const searchEnv = arguments[0];
  13818. return super.query.call(this, searchEnv);
  13819. } else if (arguments.length === 2) {
  13820. const searchEnv = arguments[0],
  13821. visitor = arguments[1];
  13822. super.query.call(this, searchEnv, visitor);
  13823. }
  13824. }
  13825. getComparator() {
  13826. return STRtree.yComparator;
  13827. }
  13828. createParentBoundablesFromVerticalSlice(childBoundables, newLevel) {
  13829. return super.createParentBoundables.call(this, childBoundables, newLevel);
  13830. }
  13831. remove() {
  13832. if (arguments.length === 2 && arguments[1] instanceof Object && arguments[0] instanceof Envelope) {
  13833. const itemEnv = arguments[0],
  13834. item = arguments[1];
  13835. return super.remove.call(this, itemEnv, item);
  13836. } else {
  13837. return super.remove.apply(this, arguments);
  13838. }
  13839. }
  13840. depth() {
  13841. if (arguments.length === 0) return super.depth.call(this);else return super.depth.apply(this, arguments);
  13842. }
  13843. createParentBoundables(childBoundables, newLevel) {
  13844. Assert.isTrue(!childBoundables.isEmpty());
  13845. const minLeafCount = Math.trunc(Math.ceil(childBoundables.size() / this.getNodeCapacity()));
  13846. const sortedChildBoundables = new ArrayList(childBoundables);
  13847. Collections.sort(sortedChildBoundables, STRtree.xComparator);
  13848. const verticalSlices = this.verticalSlices(sortedChildBoundables, Math.trunc(Math.ceil(Math.sqrt(minLeafCount))));
  13849. return this.createParentBoundablesFromVerticalSlices(verticalSlices, newLevel);
  13850. }
  13851. nearestNeighbour() {
  13852. if (arguments.length === 1) {
  13853. if (hasInterface(arguments[0], ItemDistance)) {
  13854. const itemDist = arguments[0];
  13855. if (this.isEmpty()) return null;
  13856. const bp = new BoundablePair(this.getRoot(), this.getRoot(), itemDist);
  13857. return this.nearestNeighbour(bp);
  13858. } else if (arguments[0] instanceof BoundablePair) {
  13859. const initBndPair = arguments[0];
  13860. let distanceLowerBound = Double.POSITIVE_INFINITY;
  13861. let minPair = null;
  13862. const priQ = new PriorityQueue();
  13863. priQ.add(initBndPair);
  13864. while (!priQ.isEmpty() && distanceLowerBound > 0.0) {
  13865. const bndPair = priQ.poll();
  13866. const pairDistance = bndPair.getDistance();
  13867. if (pairDistance >= distanceLowerBound) break;
  13868. if (bndPair.isLeaves()) {
  13869. distanceLowerBound = pairDistance;
  13870. minPair = bndPair;
  13871. } else {
  13872. bndPair.expandToQueue(priQ, distanceLowerBound);
  13873. }
  13874. }
  13875. if (minPair === null) return null;
  13876. return [minPair.getBoundable(0).getItem(), minPair.getBoundable(1).getItem()];
  13877. }
  13878. } else if (arguments.length === 2) {
  13879. const tree = arguments[0],
  13880. itemDist = arguments[1];
  13881. if (this.isEmpty() || tree.isEmpty()) return null;
  13882. const bp = new BoundablePair(this.getRoot(), tree.getRoot(), itemDist);
  13883. return this.nearestNeighbour(bp);
  13884. } else if (arguments.length === 3) {
  13885. const env = arguments[0],
  13886. item = arguments[1],
  13887. itemDist = arguments[2];
  13888. const bnd = new ItemBoundable(env, item);
  13889. const bp = new BoundablePair(this.getRoot(), bnd, itemDist);
  13890. return this.nearestNeighbour(bp)[0];
  13891. } else if (arguments.length === 4) {
  13892. const env = arguments[0],
  13893. item = arguments[1],
  13894. itemDist = arguments[2],
  13895. k = arguments[3];
  13896. const bnd = new ItemBoundable(env, item);
  13897. const bp = new BoundablePair(this.getRoot(), bnd, itemDist);
  13898. return this.nearestNeighbourK(bp, k);
  13899. }
  13900. }
  13901. isWithinDistance() {
  13902. if (arguments.length === 2) {
  13903. const initBndPair = arguments[0],
  13904. maxDistance = arguments[1];
  13905. let distanceUpperBound = Double.POSITIVE_INFINITY;
  13906. const priQ = new PriorityQueue();
  13907. priQ.add(initBndPair);
  13908. while (!priQ.isEmpty()) {
  13909. const bndPair = priQ.poll();
  13910. const pairDistance = bndPair.getDistance();
  13911. if (pairDistance > maxDistance) return false;
  13912. if (bndPair.maximumDistance() <= maxDistance) return true;
  13913. if (bndPair.isLeaves()) {
  13914. distanceUpperBound = pairDistance;
  13915. if (distanceUpperBound <= maxDistance) return true;
  13916. } else {
  13917. bndPair.expandToQueue(priQ, distanceUpperBound);
  13918. }
  13919. }
  13920. return false;
  13921. } else if (arguments.length === 3) {
  13922. const tree = arguments[0],
  13923. itemDist = arguments[1],
  13924. maxDistance = arguments[2];
  13925. const bp = new BoundablePair(this.getRoot(), tree.getRoot(), itemDist);
  13926. return this.isWithinDistance(bp, maxDistance);
  13927. }
  13928. }
  13929. get interfaces_() {
  13930. return [SpatialIndex, Serializable];
  13931. }
  13932. }
  13933. class STRtreeNode extends AbstractNode {
  13934. constructor() {
  13935. super();
  13936. STRtreeNode.constructor_.apply(this, arguments);
  13937. }
  13938. static constructor_() {
  13939. const level = arguments[0];
  13940. AbstractNode.constructor_.call(this, level);
  13941. }
  13942. computeBounds() {
  13943. let bounds = null;
  13944. for (let i = this.getChildBoundables().iterator(); i.hasNext();) {
  13945. const childBoundable = i.next();
  13946. if (bounds === null) bounds = new Envelope(childBoundable.getBounds());else bounds.expandToInclude(childBoundable.getBounds());
  13947. }
  13948. return bounds;
  13949. }
  13950. }
  13951. STRtree.STRtreeNode = STRtreeNode;
  13952. STRtree.xComparator = new class {
  13953. get interfaces_() {
  13954. return [Comparator];
  13955. }
  13956. compare(o1, o2) {
  13957. return AbstractSTRtree.compareDoubles(STRtree.centreX(o1.getBounds()), STRtree.centreX(o2.getBounds()));
  13958. }
  13959. }();
  13960. STRtree.yComparator = new class {
  13961. get interfaces_() {
  13962. return [Comparator];
  13963. }
  13964. compare(o1, o2) {
  13965. return AbstractSTRtree.compareDoubles(STRtree.centreY(o1.getBounds()), STRtree.centreY(o2.getBounds()));
  13966. }
  13967. }();
  13968. STRtree.intersectsOp = new class {
  13969. get interfaces_() {
  13970. return [IntersectsOp];
  13971. }
  13972. intersects(aBounds, bBounds) {
  13973. return aBounds.intersects(bBounds);
  13974. }
  13975. }();
  13976. STRtree.DEFAULT_NODE_CAPACITY = 10;
  13977. var strtree = /*#__PURE__*/Object.freeze({
  13978. __proto__: null,
  13979. STRtree: STRtree
  13980. });
  13981. var index = /*#__PURE__*/Object.freeze({
  13982. __proto__: null,
  13983. kdtree: kdtree,
  13984. quadtree: quadtree,
  13985. strtree: strtree
  13986. });
  13987. const geometryTypes = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'];
  13988. /**
  13989. * Class for reading and writing Well-Known Text.Create a new parser for GeoJSON
  13990. * NOTE: Adapted from OpenLayers 2.11 implementation.
  13991. */
  13992. /**
  13993. * Create a new parser for GeoJSON
  13994. *
  13995. * @param {GeometryFactory} geometryFactory
  13996. * @return An instance of GeoJsonParser.
  13997. * @constructor
  13998. * @private
  13999. */
  14000. class GeoJSONParser {
  14001. constructor(geometryFactory) {
  14002. this.geometryFactory = geometryFactory || new GeometryFactory();
  14003. }
  14004. /**
  14005. * Deserialize a GeoJSON object and return the Geometry or Feature(Collection) with JSTS Geometries
  14006. *
  14007. * @param {}
  14008. * A GeoJSON object.
  14009. * @return {} A Geometry instance or object representing a Feature(Collection) with Geometry instances.
  14010. * @private
  14011. */
  14012. read(json) {
  14013. let obj;
  14014. if (typeof json === 'string') obj = JSON.parse(json);else obj = json;
  14015. const type = obj.type;
  14016. if (!parse[type]) throw new Error('Unknown GeoJSON type: ' + obj.type);
  14017. if (geometryTypes.indexOf(type) !== -1) return parse[type].call(this, obj.coordinates);else if (type === 'GeometryCollection') return parse[type].call(this, obj.geometries); // feature or feature collection
  14018. return parse[type].call(this, obj);
  14019. }
  14020. /**
  14021. * Serialize a Geometry object into GeoJSON
  14022. *
  14023. * @param {Geometry}
  14024. * geometry A Geometry or array of Geometries.
  14025. * @return {Object} A GeoJSON object represting the input Geometry/Geometries.
  14026. * @private
  14027. */
  14028. write(geometry) {
  14029. const type = geometry.getGeometryType();
  14030. if (!extract[type]) throw new Error('Geometry is not supported');
  14031. return extract[type].call(this, geometry);
  14032. }
  14033. }
  14034. const parse = {
  14035. /**
  14036. * Parse a GeoJSON Feature object
  14037. *
  14038. * @param {Object}
  14039. * obj Object to parse.
  14040. *
  14041. * @return {Object} Feature with geometry/bbox converted to JSTS Geometries.
  14042. */
  14043. Feature: function (obj) {
  14044. const feature = {};
  14045. for (const key in obj) feature[key] = obj[key];
  14046. if (obj.geometry) {
  14047. const type = obj.geometry.type;
  14048. if (!parse[type]) throw new Error('Unknown GeoJSON type: ' + obj.type);
  14049. feature.geometry = this.read(obj.geometry);
  14050. }
  14051. if (obj.bbox) feature.bbox = parse.bbox.call(this, obj.bbox);
  14052. return feature;
  14053. },
  14054. /**
  14055. * Parse a GeoJSON FeatureCollection object
  14056. *
  14057. * @param {Object}
  14058. * obj Object to parse.
  14059. *
  14060. * @return {Object} FeatureCollection with geometry/bbox converted to JSTS Geometries.
  14061. */
  14062. FeatureCollection: function (obj) {
  14063. const featureCollection = {};
  14064. if (obj.features) {
  14065. featureCollection.features = [];
  14066. for (let i = 0; i < obj.features.length; ++i) featureCollection.features.push(this.read(obj.features[i]));
  14067. }
  14068. if (obj.bbox) featureCollection.bbox = this.parse.bbox.call(this, obj.bbox);
  14069. return featureCollection;
  14070. },
  14071. /**
  14072. * Convert the ordinates in an array to an array of Coordinates
  14073. *
  14074. * @param {Array}
  14075. * array Array with {Number}s.
  14076. *
  14077. * @return {Array} Array with Coordinates.
  14078. */
  14079. coordinates: function (array) {
  14080. const coordinates = [];
  14081. for (let i = 0; i < array.length; ++i) {
  14082. const sub = array[i];
  14083. coordinates.push(new Coordinate(...sub));
  14084. }
  14085. return coordinates;
  14086. },
  14087. /**
  14088. * Convert the bbox to a LinearRing
  14089. *
  14090. * @param {Array}
  14091. * array Array with [xMin, yMin, xMax, yMax].
  14092. *
  14093. * @return {Array} Array with Coordinates.
  14094. */
  14095. bbox: function (array) {
  14096. return this.geometryFactory.createLinearRing([new Coordinate(array[0], array[1]), new Coordinate(array[2], array[1]), new Coordinate(array[2], array[3]), new Coordinate(array[0], array[3]), new Coordinate(array[0], array[1])]);
  14097. },
  14098. /**
  14099. * Convert an Array with ordinates to a Point
  14100. *
  14101. * @param {Array}
  14102. * array Array with ordinates.
  14103. *
  14104. * @return {Point} Point.
  14105. */
  14106. Point: function (array) {
  14107. const coordinate = new Coordinate(...array);
  14108. return this.geometryFactory.createPoint(coordinate);
  14109. },
  14110. /**
  14111. * Convert an Array with coordinates to a MultiPoint
  14112. *
  14113. * @param {Array}
  14114. * array Array with coordinates.
  14115. *
  14116. * @return {MultiPoint} MultiPoint.
  14117. */
  14118. MultiPoint: function (array) {
  14119. const points = [];
  14120. for (let i = 0; i < array.length; ++i) points.push(parse.Point.call(this, array[i]));
  14121. return this.geometryFactory.createMultiPoint(points);
  14122. },
  14123. /**
  14124. * Convert an Array with coordinates to a LineString
  14125. *
  14126. * @param {Array}
  14127. * array Array with coordinates.
  14128. *
  14129. * @return {LineString} LineString.
  14130. */
  14131. LineString: function (array) {
  14132. const coordinates = parse.coordinates.call(this, array);
  14133. return this.geometryFactory.createLineString(coordinates);
  14134. },
  14135. /**
  14136. * Convert an Array with coordinates to a MultiLineString
  14137. *
  14138. * @param {Array}
  14139. * array Array with coordinates.
  14140. *
  14141. * @return {MultiLineString} MultiLineString.
  14142. */
  14143. MultiLineString: function (array) {
  14144. const lineStrings = [];
  14145. for (let i = 0; i < array.length; ++i) lineStrings.push(parse.LineString.call(this, array[i]));
  14146. return this.geometryFactory.createMultiLineString(lineStrings);
  14147. },
  14148. /**
  14149. * Convert an Array to a Polygon
  14150. *
  14151. * @param {Array}
  14152. * array Array with shell and holes.
  14153. *
  14154. * @return {Polygon} Polygon.
  14155. */
  14156. Polygon: function (array) {
  14157. const shellCoordinates = parse.coordinates.call(this, array[0]);
  14158. const shell = this.geometryFactory.createLinearRing(shellCoordinates);
  14159. const holes = [];
  14160. for (let i = 1; i < array.length; ++i) {
  14161. const hole = array[i];
  14162. const coordinates = parse.coordinates.call(this, hole);
  14163. const linearRing = this.geometryFactory.createLinearRing(coordinates);
  14164. holes.push(linearRing);
  14165. }
  14166. return this.geometryFactory.createPolygon(shell, holes);
  14167. },
  14168. /**
  14169. * Convert an Array to a MultiPolygon
  14170. *
  14171. * @param {Array}
  14172. * array Array of arrays with shell and rings.
  14173. *
  14174. * @return {MultiPolygon} MultiPolygon.
  14175. */
  14176. MultiPolygon: function (array) {
  14177. const polygons = [];
  14178. for (let i = 0; i < array.length; ++i) {
  14179. const polygon = array[i];
  14180. polygons.push(parse.Polygon.call(this, polygon));
  14181. }
  14182. return this.geometryFactory.createMultiPolygon(polygons);
  14183. },
  14184. /**
  14185. * Convert an Array to a GeometryCollection
  14186. *
  14187. * @param {Array}
  14188. * array Array of GeoJSON geometries.
  14189. *
  14190. * @return {GeometryCollection} GeometryCollection.
  14191. */
  14192. GeometryCollection: function (array) {
  14193. const geometries = [];
  14194. for (let i = 0; i < array.length; ++i) {
  14195. const geometry = array[i];
  14196. geometries.push(this.read(geometry));
  14197. }
  14198. return this.geometryFactory.createGeometryCollection(geometries);
  14199. }
  14200. };
  14201. const extract = {
  14202. /**
  14203. * Convert a Coordinate to an Array
  14204. *
  14205. * @param {Coordinate}
  14206. * coordinate Coordinate to convert.
  14207. *
  14208. * @return {Array} Array of ordinates.
  14209. */
  14210. coordinate: function (coordinate) {
  14211. const a = [coordinate.x, coordinate.y];
  14212. if (coordinate.z) a.push(coordinate.z);
  14213. if (coordinate.m) a.push(coordinate.m);
  14214. return a;
  14215. },
  14216. /**
  14217. * Convert a Point to a GeoJSON object
  14218. *
  14219. * @param {Point}
  14220. * point Point to convert.
  14221. *
  14222. * @return {Array} Array of 2 ordinates (paired to a coordinate).
  14223. */
  14224. Point: function (point) {
  14225. const array = extract.coordinate.call(this, point.getCoordinate());
  14226. return {
  14227. type: 'Point',
  14228. coordinates: array
  14229. };
  14230. },
  14231. /**
  14232. * Convert a MultiPoint to a GeoJSON object
  14233. *
  14234. * @param {MultiPoint}
  14235. * multipoint MultiPoint to convert.
  14236. *
  14237. * @return {Array} Array of coordinates.
  14238. */
  14239. MultiPoint: function (multipoint) {
  14240. const array = [];
  14241. for (let i = 0; i < multipoint._geometries.length; ++i) {
  14242. const point = multipoint._geometries[i];
  14243. const geoJson = extract.Point.call(this, point);
  14244. array.push(geoJson.coordinates);
  14245. }
  14246. return {
  14247. type: 'MultiPoint',
  14248. coordinates: array
  14249. };
  14250. },
  14251. /**
  14252. * Convert a LineString to a GeoJSON object
  14253. *
  14254. * @param {LineString}
  14255. * linestring LineString to convert.
  14256. *
  14257. * @return {Array} Array of coordinates.
  14258. */
  14259. LineString: function (linestring) {
  14260. const array = [];
  14261. const coordinates = linestring.getCoordinates();
  14262. for (let i = 0; i < coordinates.length; ++i) {
  14263. const coordinate = coordinates[i];
  14264. array.push(extract.coordinate.call(this, coordinate));
  14265. }
  14266. return {
  14267. type: 'LineString',
  14268. coordinates: array
  14269. };
  14270. },
  14271. /**
  14272. * Convert a MultiLineString to a GeoJSON object
  14273. *
  14274. * @param {MultiLineString}
  14275. * multilinestring MultiLineString to convert.
  14276. *
  14277. * @return {Array} Array of Array of coordinates.
  14278. */
  14279. MultiLineString: function (multilinestring) {
  14280. const array = [];
  14281. for (let i = 0; i < multilinestring._geometries.length; ++i) {
  14282. const linestring = multilinestring._geometries[i];
  14283. const geoJson = extract.LineString.call(this, linestring);
  14284. array.push(geoJson.coordinates);
  14285. }
  14286. return {
  14287. type: 'MultiLineString',
  14288. coordinates: array
  14289. };
  14290. },
  14291. /**
  14292. * Convert a Polygon to a GeoJSON object
  14293. *
  14294. * @param {Polygon}
  14295. * polygon Polygon to convert.
  14296. *
  14297. * @return {Array} Array with shell, holes.
  14298. */
  14299. Polygon: function (polygon) {
  14300. const array = [];
  14301. const shellGeoJson = extract.LineString.call(this, polygon._shell);
  14302. array.push(shellGeoJson.coordinates);
  14303. for (let i = 0; i < polygon._holes.length; ++i) {
  14304. const hole = polygon._holes[i];
  14305. const holeGeoJson = extract.LineString.call(this, hole);
  14306. array.push(holeGeoJson.coordinates);
  14307. }
  14308. return {
  14309. type: 'Polygon',
  14310. coordinates: array
  14311. };
  14312. },
  14313. /**
  14314. * Convert a MultiPolygon to a GeoJSON object
  14315. *
  14316. * @param {MultiPolygon}
  14317. * multipolygon MultiPolygon to convert.
  14318. *
  14319. * @return {Array} Array of polygons.
  14320. */
  14321. MultiPolygon: function (multipolygon) {
  14322. const array = [];
  14323. for (let i = 0; i < multipolygon._geometries.length; ++i) {
  14324. const polygon = multipolygon._geometries[i];
  14325. const geoJson = extract.Polygon.call(this, polygon);
  14326. array.push(geoJson.coordinates);
  14327. }
  14328. return {
  14329. type: 'MultiPolygon',
  14330. coordinates: array
  14331. };
  14332. },
  14333. /**
  14334. * Convert a GeometryCollection to a GeoJSON object
  14335. *
  14336. * @param {GeometryCollection}
  14337. * collection GeometryCollection to convert.
  14338. *
  14339. * @return {Array} Array of geometries.
  14340. */
  14341. GeometryCollection: function (collection) {
  14342. const array = [];
  14343. for (let i = 0; i < collection._geometries.length; ++i) {
  14344. const geometry = collection._geometries[i];
  14345. const type = geometry.getGeometryType();
  14346. array.push(extract[type].call(this, geometry));
  14347. }
  14348. return {
  14349. type: 'GeometryCollection',
  14350. geometries: array
  14351. };
  14352. }
  14353. };
  14354. /**
  14355. * @module org/locationtech/jts/io/GeoJSONReader
  14356. */
  14357. /**
  14358. * Converts a geometry in GeoJSON to a {@link Geometry}.
  14359. */
  14360. class GeoJSONReader {
  14361. /**
  14362. * A <code>GeoJSONReader</code> is parameterized by a <code>GeometryFactory</code>,
  14363. * to allow it to create <code>Geometry</code> objects of the appropriate
  14364. * implementation. In particular, the <code>GeometryFactory</code> determines
  14365. * the <code>PrecisionModel</code> and <code>SRID</code> that is used.
  14366. *
  14367. * @param {GeometryFactory} geometryFactory
  14368. */
  14369. constructor(geometryFactory) {
  14370. this.parser = new GeoJSONParser(geometryFactory || new GeometryFactory());
  14371. }
  14372. /**
  14373. * Reads a GeoJSON representation of a {@link Geometry}
  14374. *
  14375. * Will also parse GeoJSON Features/FeatureCollections as custom objects.
  14376. *
  14377. * @param {Object|String} geoJson a GeoJSON Object or String.
  14378. * @return {Geometry|Object} a <code>Geometry or Feature/FeatureCollection representation.</code>
  14379. * @memberof module:org/locationtech/jts/io/GeoJSONReader#
  14380. */
  14381. read(geoJson) {
  14382. const geometry = this.parser.read(geoJson);
  14383. return geometry;
  14384. }
  14385. }
  14386. /**
  14387. * @module org/locationtech/jts/io/GeoJSONWriter
  14388. */
  14389. /**
  14390. * Writes the GeoJSON representation of a {@link Geometry}. The
  14391. * The GeoJSON format is defined <A
  14392. * HREF="http://geojson.org/geojson-spec.html">here</A>.
  14393. */
  14394. class GeoJSONWriter {
  14395. /**
  14396. * The <code>GeoJSONWriter</code> outputs coordinates rounded to the precision
  14397. * model. Only the maximum number of decimal places necessary to represent the
  14398. * ordinates to the required precision will be output.
  14399. *
  14400. * @param {GeometryFactory} geometryFactory
  14401. * @constructor
  14402. */
  14403. constructor() {
  14404. this.parser = new GeoJSONParser(this.geometryFactory);
  14405. }
  14406. /**
  14407. * Converts a <code>Geometry</code> to its GeoJSON representation.
  14408. *
  14409. * @param {Geometry}
  14410. * geometry a <code>Geometry</code> to process.
  14411. * @return {Object} The GeoJSON representation of the Geometry.
  14412. * @memberof module:org/locationtech/jts/io/GeoJSONWriter#
  14413. */
  14414. write(geometry) {
  14415. return this.parser.write(geometry);
  14416. }
  14417. }
  14418. /**
  14419. * @module org/locationtech/jts/io/WKTReader
  14420. */
  14421. /**
  14422. * Converts a geometry in Well-Known Text format to a {@link Geometry}.
  14423. * <p>
  14424. * <code>WKTReader</code> supports extracting <code>Geometry</code> objects
  14425. * from either {@link Reader}s or {@link String}s. This allows it to function
  14426. * as a parser to read <code>Geometry</code> objects from text blocks embedded
  14427. * in other data formats (e.g. XML).
  14428. */
  14429. class WKTReader {
  14430. /**
  14431. * A <code>WKTReader</code> is parameterized by a <code>GeometryFactory</code>,
  14432. * to allow it to create <code>Geometry</code> objects of the appropriate
  14433. * implementation. In particular, the <code>GeometryFactory</code> determines
  14434. * the <code>PrecisionModel</code> and <code>SRID</code> that is used.
  14435. * @param {GeometryFactory} geometryFactory
  14436. */
  14437. constructor(geometryFactory) {
  14438. this.parser = new WKTParser(geometryFactory || new GeometryFactory());
  14439. }
  14440. /**
  14441. * Reads a Well-Known Text representation of a {@link Geometry}
  14442. *
  14443. * @param {string}
  14444. * wkt a <Geometry Tagged Text> string (see the OpenGIS Simple Features
  14445. * Specification).
  14446. * @return {Geometry} a <code>Geometry</code> read from
  14447. * <code>string.</code>
  14448. * @memberof module:org/locationtech/jts/io/WKTReader#
  14449. */
  14450. read(wkt) {
  14451. return this.parser.read(wkt);
  14452. }
  14453. }
  14454. /* eslint-disable no-undef */
  14455. function p2c(p) {
  14456. return [p.x, p.y];
  14457. }
  14458. class OL3Parser {
  14459. /**
  14460. * OpenLayers Geometry parser and writer
  14461. * @param {GeometryFactory} geometryFactory
  14462. * @param {ol} olReference
  14463. */
  14464. constructor(geometryFactory, olReference) {
  14465. this.geometryFactory = geometryFactory || new GeometryFactory();
  14466. this.ol = olReference || typeof ol !== 'undefined' && ol;
  14467. }
  14468. /**
  14469. * Inject OpenLayers geom classes
  14470. */
  14471. inject(Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection) {
  14472. this.ol = {
  14473. geom: {
  14474. Point,
  14475. LineString,
  14476. LinearRing,
  14477. Polygon,
  14478. MultiPoint,
  14479. MultiLineString,
  14480. MultiPolygon,
  14481. GeometryCollection
  14482. }
  14483. };
  14484. }
  14485. /**
  14486. * @param geometry {ol.geom.Geometry}
  14487. * @return {Geometry}
  14488. * @memberof module:org/locationtech/jts/io/OL3Parser#
  14489. */
  14490. read(geometry) {
  14491. const ol = this.ol;
  14492. if (geometry instanceof ol.geom.Point) return this.convertFromPoint(geometry);else if (geometry instanceof ol.geom.LineString) return this.convertFromLineString(geometry);else if (geometry instanceof ol.geom.LinearRing) return this.convertFromLinearRing(geometry);else if (geometry instanceof ol.geom.Polygon) return this.convertFromPolygon(geometry);else if (geometry instanceof ol.geom.MultiPoint) return this.convertFromMultiPoint(geometry);else if (geometry instanceof ol.geom.MultiLineString) return this.convertFromMultiLineString(geometry);else if (geometry instanceof ol.geom.MultiPolygon) return this.convertFromMultiPolygon(geometry);else if (geometry instanceof ol.geom.GeometryCollection) return this.convertFromCollection(geometry);
  14493. }
  14494. convertFromPoint(point) {
  14495. const coordinates = point.getCoordinates();
  14496. return this.geometryFactory.createPoint(new Coordinate(coordinates[0], coordinates[1]));
  14497. }
  14498. convertFromLineString(lineString) {
  14499. return this.geometryFactory.createLineString(lineString.getCoordinates().map(function (coordinates) {
  14500. return new Coordinate(coordinates[0], coordinates[1]);
  14501. }));
  14502. }
  14503. convertFromLinearRing(linearRing) {
  14504. return this.geometryFactory.createLinearRing(linearRing.getCoordinates().map(function (coordinates) {
  14505. return new Coordinate(coordinates[0], coordinates[1]);
  14506. }));
  14507. }
  14508. convertFromPolygon(polygon) {
  14509. const linearRings = polygon.getLinearRings();
  14510. let shell = null;
  14511. const holes = [];
  14512. for (let i = 0; i < linearRings.length; i++) {
  14513. const linearRing = this.convertFromLinearRing(linearRings[i]);
  14514. if (i === 0) shell = linearRing;else holes.push(linearRing);
  14515. }
  14516. return this.geometryFactory.createPolygon(shell, holes);
  14517. }
  14518. convertFromMultiPoint(multiPoint) {
  14519. const points = multiPoint.getPoints().map(function (point) {
  14520. return this.convertFromPoint(point);
  14521. }, this);
  14522. return this.geometryFactory.createMultiPoint(points);
  14523. }
  14524. convertFromMultiLineString(multiLineString) {
  14525. const lineStrings = multiLineString.getLineStrings().map(function (lineString) {
  14526. return this.convertFromLineString(lineString);
  14527. }, this);
  14528. return this.geometryFactory.createMultiLineString(lineStrings);
  14529. }
  14530. convertFromMultiPolygon(multiPolygon) {
  14531. const polygons = multiPolygon.getPolygons().map(function (polygon) {
  14532. return this.convertFromPolygon(polygon);
  14533. }, this);
  14534. return this.geometryFactory.createMultiPolygon(polygons);
  14535. }
  14536. convertFromCollection(collection) {
  14537. const geometries = collection.getGeometries().map(function (geometry) {
  14538. return this.read(geometry);
  14539. }, this);
  14540. return this.geometryFactory.createGeometryCollection(geometries);
  14541. }
  14542. /**
  14543. * @param geometry
  14544. * {Geometry}
  14545. * @return {ol.geom.Geometry}
  14546. * @memberof module:org/locationtech/jts/io/OL3Parser#
  14547. */
  14548. write(geometry) {
  14549. if (geometry.getGeometryType() === 'Point') return this.convertToPoint(geometry.getCoordinate());else if (geometry.getGeometryType() === 'LineString') return this.convertToLineString(geometry);else if (geometry.getGeometryType() === 'LinearRing') return this.convertToLinearRing(geometry);else if (geometry.getGeometryType() === 'Polygon') return this.convertToPolygon(geometry);else if (geometry.getGeometryType() === 'MultiPoint') return this.convertToMultiPoint(geometry);else if (geometry.getGeometryType() === 'MultiLineString') return this.convertToMultiLineString(geometry);else if (geometry.getGeometryType() === 'MultiPolygon') return this.convertToMultiPolygon(geometry);else if (geometry.getGeometryType() === 'GeometryCollection') return this.convertToCollection(geometry);
  14550. }
  14551. convertToPoint(coordinate) {
  14552. return new this.ol.geom.Point([coordinate.x, coordinate.y]);
  14553. }
  14554. convertToLineString(lineString) {
  14555. const points = lineString._points._coordinates.map(p2c);
  14556. return new this.ol.geom.LineString(points);
  14557. }
  14558. convertToLinearRing(linearRing) {
  14559. const points = linearRing._points._coordinates.map(p2c);
  14560. return new this.ol.geom.LinearRing(points);
  14561. }
  14562. convertToPolygon(polygon) {
  14563. const rings = [polygon._shell._points._coordinates.map(p2c)];
  14564. for (let i = 0; i < polygon._holes.length; i++) rings.push(polygon._holes[i]._points._coordinates.map(p2c));
  14565. return new this.ol.geom.Polygon(rings);
  14566. }
  14567. convertToMultiPoint(multiPoint) {
  14568. return new this.ol.geom.MultiPoint(multiPoint.getCoordinates().map(p2c));
  14569. }
  14570. convertToMultiLineString(multiLineString) {
  14571. const lineStrings = [];
  14572. for (let i = 0; i < multiLineString._geometries.length; i++) lineStrings.push(this.convertToLineString(multiLineString._geometries[i]).getCoordinates());
  14573. return new this.ol.geom.MultiLineString(lineStrings);
  14574. }
  14575. convertToMultiPolygon(multiPolygon) {
  14576. const polygons = [];
  14577. for (let i = 0; i < multiPolygon._geometries.length; i++) polygons.push(this.convertToPolygon(multiPolygon._geometries[i]).getCoordinates());
  14578. return new this.ol.geom.MultiPolygon(polygons);
  14579. }
  14580. convertToCollection(geometryCollection) {
  14581. const geometries = [];
  14582. for (let i = 0; i < geometryCollection._geometries.length; i++) {
  14583. const geometry = geometryCollection._geometries[i];
  14584. geometries.push(this.write(geometry));
  14585. }
  14586. return new this.ol.geom.GeometryCollection(geometries);
  14587. }
  14588. }
  14589. var io = /*#__PURE__*/Object.freeze({
  14590. __proto__: null,
  14591. GeoJSONReader: GeoJSONReader,
  14592. GeoJSONWriter: GeoJSONWriter,
  14593. OL3Parser: OL3Parser,
  14594. WKTReader: WKTReader,
  14595. WKTWriter: WKTWriter
  14596. });
  14597. class SegmentPointComparator {
  14598. static relativeSign(x0, x1) {
  14599. if (x0 < x1) return -1;
  14600. if (x0 > x1) return 1;
  14601. return 0;
  14602. }
  14603. static compare(octant, p0, p1) {
  14604. if (p0.equals2D(p1)) return 0;
  14605. const xSign = SegmentPointComparator.relativeSign(p0.x, p1.x);
  14606. const ySign = SegmentPointComparator.relativeSign(p0.y, p1.y);
  14607. switch (octant) {
  14608. case 0:
  14609. return SegmentPointComparator.compareValue(xSign, ySign);
  14610. case 1:
  14611. return SegmentPointComparator.compareValue(ySign, xSign);
  14612. case 2:
  14613. return SegmentPointComparator.compareValue(ySign, -xSign);
  14614. case 3:
  14615. return SegmentPointComparator.compareValue(-xSign, ySign);
  14616. case 4:
  14617. return SegmentPointComparator.compareValue(-xSign, -ySign);
  14618. case 5:
  14619. return SegmentPointComparator.compareValue(-ySign, -xSign);
  14620. case 6:
  14621. return SegmentPointComparator.compareValue(-ySign, xSign);
  14622. case 7:
  14623. return SegmentPointComparator.compareValue(xSign, -ySign);
  14624. }
  14625. Assert.shouldNeverReachHere('invalid octant value');
  14626. return 0;
  14627. }
  14628. static compareValue(compareSign0, compareSign1) {
  14629. if (compareSign0 < 0) return -1;
  14630. if (compareSign0 > 0) return 1;
  14631. if (compareSign1 < 0) return -1;
  14632. if (compareSign1 > 0) return 1;
  14633. return 0;
  14634. }
  14635. }
  14636. class SegmentNode {
  14637. constructor() {
  14638. SegmentNode.constructor_.apply(this, arguments);
  14639. }
  14640. static constructor_() {
  14641. this._segString = null;
  14642. this.coord = null;
  14643. this.segmentIndex = null;
  14644. this._segmentOctant = null;
  14645. this._isInterior = null;
  14646. const segString = arguments[0],
  14647. coord = arguments[1],
  14648. segmentIndex = arguments[2],
  14649. segmentOctant = arguments[3];
  14650. this._segString = segString;
  14651. this.coord = new Coordinate(coord);
  14652. this.segmentIndex = segmentIndex;
  14653. this._segmentOctant = segmentOctant;
  14654. this._isInterior = !coord.equals2D(segString.getCoordinate(segmentIndex));
  14655. }
  14656. getCoordinate() {
  14657. return this.coord;
  14658. }
  14659. print(out) {
  14660. out.print(this.coord);
  14661. out.print(' seg # = ' + this.segmentIndex);
  14662. }
  14663. compareTo(obj) {
  14664. const other = obj;
  14665. if (this.segmentIndex < other.segmentIndex) return -1;
  14666. if (this.segmentIndex > other.segmentIndex) return 1;
  14667. if (this.coord.equals2D(other.coord)) return 0;
  14668. if (!this._isInterior) return -1;
  14669. if (!other._isInterior) return 1;
  14670. return SegmentPointComparator.compare(this._segmentOctant, this.coord, other.coord);
  14671. }
  14672. isEndPoint(maxSegmentIndex) {
  14673. if (this.segmentIndex === 0 && !this._isInterior) return true;
  14674. if (this.segmentIndex === maxSegmentIndex) return true;
  14675. return false;
  14676. }
  14677. toString() {
  14678. return this.segmentIndex + ':' + this.coord.toString();
  14679. }
  14680. isInterior() {
  14681. return this._isInterior;
  14682. }
  14683. get interfaces_() {
  14684. return [Comparable];
  14685. }
  14686. }
  14687. class SegmentNodeList {
  14688. constructor() {
  14689. SegmentNodeList.constructor_.apply(this, arguments);
  14690. }
  14691. static constructor_() {
  14692. this._nodeMap = new TreeMap();
  14693. this._edge = null;
  14694. const edge = arguments[0];
  14695. this._edge = edge;
  14696. }
  14697. getSplitCoordinates() {
  14698. const coordList = new CoordinateList();
  14699. this.addEndpoints();
  14700. const it = this.iterator();
  14701. let eiPrev = it.next();
  14702. while (it.hasNext()) {
  14703. const ei = it.next();
  14704. this.addEdgeCoordinates(eiPrev, ei, coordList);
  14705. eiPrev = ei;
  14706. }
  14707. return coordList.toCoordinateArray();
  14708. }
  14709. addCollapsedNodes() {
  14710. const collapsedVertexIndexes = new ArrayList();
  14711. this.findCollapsesFromInsertedNodes(collapsedVertexIndexes);
  14712. this.findCollapsesFromExistingVertices(collapsedVertexIndexes);
  14713. for (let it = collapsedVertexIndexes.iterator(); it.hasNext();) {
  14714. const vertexIndex = it.next().intValue();
  14715. this.add(this._edge.getCoordinate(vertexIndex), vertexIndex);
  14716. }
  14717. }
  14718. createSplitEdgePts(ei0, ei1) {
  14719. let npts = ei1.segmentIndex - ei0.segmentIndex + 2;
  14720. if (npts === 2) return [new Coordinate(ei0.coord), new Coordinate(ei1.coord)];
  14721. const lastSegStartPt = this._edge.getCoordinate(ei1.segmentIndex);
  14722. const useIntPt1 = ei1.isInterior() || !ei1.coord.equals2D(lastSegStartPt);
  14723. if (!useIntPt1) npts--;
  14724. const pts = new Array(npts).fill(null);
  14725. let ipt = 0;
  14726. pts[ipt++] = new Coordinate(ei0.coord);
  14727. for (let i = ei0.segmentIndex + 1; i <= ei1.segmentIndex; i++) pts[ipt++] = this._edge.getCoordinate(i);
  14728. if (useIntPt1) pts[ipt] = new Coordinate(ei1.coord);
  14729. return pts;
  14730. }
  14731. print(out) {
  14732. out.println('Intersections:');
  14733. for (let it = this.iterator(); it.hasNext();) {
  14734. const ei = it.next();
  14735. ei.print(out);
  14736. }
  14737. }
  14738. findCollapsesFromExistingVertices(collapsedVertexIndexes) {
  14739. for (let i = 0; i < this._edge.size() - 2; i++) {
  14740. const p0 = this._edge.getCoordinate(i);
  14741. this._edge.getCoordinate(i + 1);
  14742. const p2 = this._edge.getCoordinate(i + 2);
  14743. if (p0.equals2D(p2)) collapsedVertexIndexes.add(Integer.valueOf(i + 1));
  14744. }
  14745. }
  14746. addEdgeCoordinates(ei0, ei1, coordList) {
  14747. const pts = this.createSplitEdgePts(ei0, ei1);
  14748. coordList.add(pts, false);
  14749. }
  14750. iterator() {
  14751. return this._nodeMap.values().iterator();
  14752. }
  14753. addSplitEdges(edgeList) {
  14754. this.addEndpoints();
  14755. this.addCollapsedNodes();
  14756. const it = this.iterator();
  14757. let eiPrev = it.next();
  14758. while (it.hasNext()) {
  14759. const ei = it.next();
  14760. const newEdge = this.createSplitEdge(eiPrev, ei);
  14761. edgeList.add(newEdge);
  14762. eiPrev = ei;
  14763. }
  14764. }
  14765. findCollapseIndex(ei0, ei1, collapsedVertexIndex) {
  14766. if (!ei0.coord.equals2D(ei1.coord)) return false;
  14767. let numVerticesBetween = ei1.segmentIndex - ei0.segmentIndex;
  14768. if (!ei1.isInterior()) numVerticesBetween--;
  14769. if (numVerticesBetween === 1) {
  14770. collapsedVertexIndex[0] = ei0.segmentIndex + 1;
  14771. return true;
  14772. }
  14773. return false;
  14774. }
  14775. findCollapsesFromInsertedNodes(collapsedVertexIndexes) {
  14776. const collapsedVertexIndex = new Array(1).fill(null);
  14777. const it = this.iterator();
  14778. let eiPrev = it.next();
  14779. while (it.hasNext()) {
  14780. const ei = it.next();
  14781. const isCollapsed = this.findCollapseIndex(eiPrev, ei, collapsedVertexIndex);
  14782. if (isCollapsed) collapsedVertexIndexes.add(Integer.valueOf(collapsedVertexIndex[0]));
  14783. eiPrev = ei;
  14784. }
  14785. }
  14786. getEdge() {
  14787. return this._edge;
  14788. }
  14789. addEndpoints() {
  14790. const maxSegIndex = this._edge.size() - 1;
  14791. this.add(this._edge.getCoordinate(0), 0);
  14792. this.add(this._edge.getCoordinate(maxSegIndex), maxSegIndex);
  14793. }
  14794. createSplitEdge(ei0, ei1) {
  14795. const pts = this.createSplitEdgePts(ei0, ei1);
  14796. return new NodedSegmentString(pts, this._edge.getData());
  14797. }
  14798. add(intPt, segmentIndex) {
  14799. const eiNew = new SegmentNode(this._edge, intPt, segmentIndex, this._edge.getSegmentOctant(segmentIndex));
  14800. const ei = this._nodeMap.get(eiNew);
  14801. if (ei !== null) {
  14802. Assert.isTrue(ei.coord.equals2D(intPt), 'Found equal nodes with different coordinates');
  14803. return ei;
  14804. }
  14805. this._nodeMap.put(eiNew, eiNew);
  14806. return eiNew;
  14807. }
  14808. checkSplitEdgesCorrectness(splitEdges) {
  14809. const edgePts = this._edge.getCoordinates();
  14810. const split0 = splitEdges.get(0);
  14811. const pt0 = split0.getCoordinate(0);
  14812. if (!pt0.equals2D(edgePts[0])) throw new RuntimeException('bad split edge start point at ' + pt0);
  14813. const splitn = splitEdges.get(splitEdges.size() - 1);
  14814. const splitnPts = splitn.getCoordinates();
  14815. const ptn = splitnPts[splitnPts.length - 1];
  14816. if (!ptn.equals2D(edgePts[edgePts.length - 1])) throw new RuntimeException('bad split edge end point at ' + ptn);
  14817. }
  14818. }
  14819. class Octant {
  14820. static octant() {
  14821. if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  14822. const dx = arguments[0],
  14823. dy = arguments[1];
  14824. if (dx === 0.0 && dy === 0.0) throw new IllegalArgumentException('Cannot compute the octant for point ( ' + dx + ', ' + dy + ' )');
  14825. const adx = Math.abs(dx);
  14826. const ady = Math.abs(dy);
  14827. if (dx >= 0) {
  14828. if (dy >= 0) {
  14829. if (adx >= ady) return 0;else return 1;
  14830. } else if (adx >= ady) return 7;else return 6;
  14831. } else if (dy >= 0) {
  14832. if (adx >= ady) return 3;else return 2;
  14833. } else if (adx >= ady) return 4;else return 5;
  14834. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  14835. const p0 = arguments[0],
  14836. p1 = arguments[1];
  14837. const dx = p1.x - p0.x;
  14838. const dy = p1.y - p0.y;
  14839. if (dx === 0.0 && dy === 0.0) throw new IllegalArgumentException('Cannot compute the octant for two identical points ' + p0);
  14840. return Octant.octant(dx, dy);
  14841. }
  14842. }
  14843. }
  14844. class SegmentString {
  14845. getCoordinates() {}
  14846. size() {}
  14847. getCoordinate(i) {}
  14848. isClosed() {}
  14849. setData(data) {}
  14850. getData() {}
  14851. }
  14852. class NodableSegmentString {
  14853. addIntersection(intPt, segmentIndex) {}
  14854. get interfaces_() {
  14855. return [SegmentString];
  14856. }
  14857. }
  14858. class NodedSegmentString {
  14859. constructor() {
  14860. NodedSegmentString.constructor_.apply(this, arguments);
  14861. }
  14862. static constructor_() {
  14863. this._nodeList = new SegmentNodeList(this);
  14864. this._pts = null;
  14865. this._data = null;
  14866. const pts = arguments[0],
  14867. data = arguments[1];
  14868. this._pts = pts;
  14869. this._data = data;
  14870. }
  14871. static getNodedSubstrings() {
  14872. if (arguments.length === 1) {
  14873. const segStrings = arguments[0];
  14874. const resultEdgelist = new ArrayList();
  14875. NodedSegmentString.getNodedSubstrings(segStrings, resultEdgelist);
  14876. return resultEdgelist;
  14877. } else if (arguments.length === 2) {
  14878. const segStrings = arguments[0],
  14879. resultEdgelist = arguments[1];
  14880. for (let i = segStrings.iterator(); i.hasNext();) {
  14881. const ss = i.next();
  14882. ss.getNodeList().addSplitEdges(resultEdgelist);
  14883. }
  14884. }
  14885. }
  14886. getCoordinates() {
  14887. return this._pts;
  14888. }
  14889. size() {
  14890. return this._pts.length;
  14891. }
  14892. getCoordinate(i) {
  14893. return this._pts[i];
  14894. }
  14895. isClosed() {
  14896. return this._pts[0].equals(this._pts[this._pts.length - 1]);
  14897. }
  14898. getSegmentOctant(index) {
  14899. if (index === this._pts.length - 1) return -1;
  14900. return this.safeOctant(this.getCoordinate(index), this.getCoordinate(index + 1));
  14901. }
  14902. setData(data) {
  14903. this._data = data;
  14904. }
  14905. safeOctant(p0, p1) {
  14906. if (p0.equals2D(p1)) return 0;
  14907. return Octant.octant(p0, p1);
  14908. }
  14909. getData() {
  14910. return this._data;
  14911. }
  14912. addIntersection() {
  14913. if (arguments.length === 2) {
  14914. const intPt = arguments[0],
  14915. segmentIndex = arguments[1];
  14916. this.addIntersectionNode(intPt, segmentIndex);
  14917. } else if (arguments.length === 4) {
  14918. const li = arguments[0],
  14919. segmentIndex = arguments[1],
  14920. intIndex = arguments[3];
  14921. const intPt = new Coordinate(li.getIntersection(intIndex));
  14922. this.addIntersection(intPt, segmentIndex);
  14923. }
  14924. }
  14925. toString() {
  14926. return WKTWriter.toLineString(new CoordinateArraySequence(this._pts));
  14927. }
  14928. getNodeList() {
  14929. return this._nodeList;
  14930. }
  14931. addIntersectionNode(intPt, segmentIndex) {
  14932. let normalizedSegmentIndex = segmentIndex;
  14933. const nextSegIndex = normalizedSegmentIndex + 1;
  14934. if (nextSegIndex < this._pts.length) {
  14935. const nextPt = this._pts[nextSegIndex];
  14936. if (intPt.equals2D(nextPt)) normalizedSegmentIndex = nextSegIndex;
  14937. }
  14938. const ei = this._nodeList.add(intPt, normalizedSegmentIndex);
  14939. return ei;
  14940. }
  14941. addIntersections(li, segmentIndex, geomIndex) {
  14942. for (let i = 0; i < li.getIntersectionNum(); i++) this.addIntersection(li, segmentIndex, geomIndex, i);
  14943. }
  14944. get interfaces_() {
  14945. return [NodableSegmentString];
  14946. }
  14947. }
  14948. class MonotoneChainOverlapAction {
  14949. constructor() {
  14950. MonotoneChainOverlapAction.constructor_.apply(this, arguments);
  14951. }
  14952. static constructor_() {
  14953. this._overlapSeg1 = new LineSegment();
  14954. this._overlapSeg2 = new LineSegment();
  14955. }
  14956. overlap() {
  14957. if (arguments.length === 2) ; else if (arguments.length === 4) {
  14958. const mc1 = arguments[0],
  14959. start1 = arguments[1],
  14960. mc2 = arguments[2],
  14961. start2 = arguments[3];
  14962. mc1.getLineSegment(start1, this._overlapSeg1);
  14963. mc2.getLineSegment(start2, this._overlapSeg2);
  14964. this.overlap(this._overlapSeg1, this._overlapSeg2);
  14965. }
  14966. }
  14967. }
  14968. class MonotoneChain {
  14969. constructor() {
  14970. MonotoneChain.constructor_.apply(this, arguments);
  14971. }
  14972. static constructor_() {
  14973. this._pts = null;
  14974. this._start = null;
  14975. this._end = null;
  14976. this._env = null;
  14977. this._context = null;
  14978. this._id = null;
  14979. const pts = arguments[0],
  14980. start = arguments[1],
  14981. end = arguments[2],
  14982. context = arguments[3];
  14983. this._pts = pts;
  14984. this._start = start;
  14985. this._end = end;
  14986. this._context = context;
  14987. }
  14988. getLineSegment(index, ls) {
  14989. ls.p0 = this._pts[index];
  14990. ls.p1 = this._pts[index + 1];
  14991. }
  14992. computeSelect(searchEnv, start0, end0, mcs) {
  14993. const p0 = this._pts[start0];
  14994. const p1 = this._pts[end0];
  14995. if (end0 - start0 === 1) {
  14996. mcs.select(this, start0);
  14997. return null;
  14998. }
  14999. if (!searchEnv.intersects(p0, p1)) return null;
  15000. const mid = Math.trunc((start0 + end0) / 2);
  15001. if (start0 < mid) this.computeSelect(searchEnv, start0, mid, mcs);
  15002. if (mid < end0) this.computeSelect(searchEnv, mid, end0, mcs);
  15003. }
  15004. getCoordinates() {
  15005. const coord = new Array(this._end - this._start + 1).fill(null);
  15006. let index = 0;
  15007. for (let i = this._start; i <= this._end; i++) coord[index++] = this._pts[i];
  15008. return coord;
  15009. }
  15010. computeOverlaps() {
  15011. if (arguments.length === 2) {
  15012. const mc = arguments[0],
  15013. mco = arguments[1];
  15014. this.computeOverlaps(this._start, this._end, mc, mc._start, mc._end, mco);
  15015. } else if (arguments.length === 6) {
  15016. const start0 = arguments[0],
  15017. end0 = arguments[1],
  15018. mc = arguments[2],
  15019. start1 = arguments[3],
  15020. end1 = arguments[4],
  15021. mco = arguments[5];
  15022. if (end0 - start0 === 1 && end1 - start1 === 1) {
  15023. mco.overlap(this, start0, mc, start1);
  15024. return null;
  15025. }
  15026. if (!this.overlaps(start0, end0, mc, start1, end1)) return null;
  15027. const mid0 = Math.trunc((start0 + end0) / 2);
  15028. const mid1 = Math.trunc((start1 + end1) / 2);
  15029. if (start0 < mid0) {
  15030. if (start1 < mid1) this.computeOverlaps(start0, mid0, mc, start1, mid1, mco);
  15031. if (mid1 < end1) this.computeOverlaps(start0, mid0, mc, mid1, end1, mco);
  15032. }
  15033. if (mid0 < end0) {
  15034. if (start1 < mid1) this.computeOverlaps(mid0, end0, mc, start1, mid1, mco);
  15035. if (mid1 < end1) this.computeOverlaps(mid0, end0, mc, mid1, end1, mco);
  15036. }
  15037. }
  15038. }
  15039. setId(id) {
  15040. this._id = id;
  15041. }
  15042. select(searchEnv, mcs) {
  15043. this.computeSelect(searchEnv, this._start, this._end, mcs);
  15044. }
  15045. getEnvelope() {
  15046. if (this._env === null) {
  15047. const p0 = this._pts[this._start];
  15048. const p1 = this._pts[this._end];
  15049. this._env = new Envelope(p0, p1);
  15050. }
  15051. return this._env;
  15052. }
  15053. overlaps(start0, end0, mc, start1, end1) {
  15054. return Envelope.intersects(this._pts[start0], this._pts[end0], mc._pts[start1], mc._pts[end1]);
  15055. }
  15056. getEndIndex() {
  15057. return this._end;
  15058. }
  15059. getStartIndex() {
  15060. return this._start;
  15061. }
  15062. getContext() {
  15063. return this._context;
  15064. }
  15065. getId() {
  15066. return this._id;
  15067. }
  15068. }
  15069. class MonotoneChainBuilder {
  15070. static findChainEnd(pts, start) {
  15071. let safeStart = start;
  15072. while (safeStart < pts.length - 1 && pts[safeStart].equals2D(pts[safeStart + 1])) safeStart++;
  15073. if (safeStart >= pts.length - 1) return pts.length - 1;
  15074. const chainQuad = Quadrant.quadrant(pts[safeStart], pts[safeStart + 1]);
  15075. let last = start + 1;
  15076. while (last < pts.length) {
  15077. if (!pts[last - 1].equals2D(pts[last])) {
  15078. const quad = Quadrant.quadrant(pts[last - 1], pts[last]);
  15079. if (quad !== chainQuad) break;
  15080. }
  15081. last++;
  15082. }
  15083. return last - 1;
  15084. }
  15085. static getChains() {
  15086. if (arguments.length === 1) {
  15087. const pts = arguments[0];
  15088. return MonotoneChainBuilder.getChains(pts, null);
  15089. } else if (arguments.length === 2) {
  15090. const pts = arguments[0],
  15091. context = arguments[1];
  15092. const mcList = new ArrayList();
  15093. let chainStart = 0;
  15094. do {
  15095. const chainEnd = MonotoneChainBuilder.findChainEnd(pts, chainStart);
  15096. const mc = new MonotoneChain(pts, chainStart, chainEnd, context);
  15097. mcList.add(mc);
  15098. chainStart = chainEnd;
  15099. } while (chainStart < pts.length - 1);
  15100. return mcList;
  15101. }
  15102. }
  15103. }
  15104. class Noder {
  15105. computeNodes(segStrings) {}
  15106. getNodedSubstrings() {}
  15107. }
  15108. class SinglePassNoder {
  15109. constructor() {
  15110. SinglePassNoder.constructor_.apply(this, arguments);
  15111. }
  15112. static constructor_() {
  15113. this._segInt = null;
  15114. if (arguments.length === 0) ; else if (arguments.length === 1) {
  15115. const segInt = arguments[0];
  15116. this.setSegmentIntersector(segInt);
  15117. }
  15118. }
  15119. setSegmentIntersector(segInt) {
  15120. this._segInt = segInt;
  15121. }
  15122. get interfaces_() {
  15123. return [Noder];
  15124. }
  15125. }
  15126. class MCIndexNoder extends SinglePassNoder {
  15127. constructor() {
  15128. super();
  15129. MCIndexNoder.constructor_.apply(this, arguments);
  15130. }
  15131. static constructor_() {
  15132. this._monoChains = new ArrayList();
  15133. this._index = new STRtree();
  15134. this._idCounter = 0;
  15135. this._nodedSegStrings = null;
  15136. this._nOverlaps = 0;
  15137. if (arguments.length === 0) ; else if (arguments.length === 1) {
  15138. const si = arguments[0];
  15139. SinglePassNoder.constructor_.call(this, si);
  15140. }
  15141. }
  15142. getMonotoneChains() {
  15143. return this._monoChains;
  15144. }
  15145. getNodedSubstrings() {
  15146. return NodedSegmentString.getNodedSubstrings(this._nodedSegStrings);
  15147. }
  15148. getIndex() {
  15149. return this._index;
  15150. }
  15151. add(segStr) {
  15152. const segChains = MonotoneChainBuilder.getChains(segStr.getCoordinates(), segStr);
  15153. for (let i = segChains.iterator(); i.hasNext();) {
  15154. const mc = i.next();
  15155. mc.setId(this._idCounter++);
  15156. this._index.insert(mc.getEnvelope(), mc);
  15157. this._monoChains.add(mc);
  15158. }
  15159. }
  15160. computeNodes(inputSegStrings) {
  15161. this._nodedSegStrings = inputSegStrings;
  15162. for (let i = inputSegStrings.iterator(); i.hasNext();) this.add(i.next());
  15163. this.intersectChains();
  15164. }
  15165. intersectChains() {
  15166. const overlapAction = new SegmentOverlapAction(this._segInt);
  15167. for (let i = this._monoChains.iterator(); i.hasNext();) {
  15168. const queryChain = i.next();
  15169. const overlapChains = this._index.query(queryChain.getEnvelope());
  15170. for (let j = overlapChains.iterator(); j.hasNext();) {
  15171. const testChain = j.next();
  15172. if (testChain.getId() > queryChain.getId()) {
  15173. queryChain.computeOverlaps(testChain, overlapAction);
  15174. this._nOverlaps++;
  15175. }
  15176. if (this._segInt.isDone()) return null;
  15177. }
  15178. }
  15179. }
  15180. }
  15181. class SegmentOverlapAction extends MonotoneChainOverlapAction {
  15182. constructor() {
  15183. super();
  15184. SegmentOverlapAction.constructor_.apply(this, arguments);
  15185. }
  15186. static constructor_() {
  15187. this._si = null;
  15188. const si = arguments[0];
  15189. this._si = si;
  15190. }
  15191. overlap() {
  15192. if (arguments.length === 4) {
  15193. const mc1 = arguments[0],
  15194. start1 = arguments[1],
  15195. mc2 = arguments[2],
  15196. start2 = arguments[3];
  15197. const ss1 = mc1.getContext();
  15198. const ss2 = mc2.getContext();
  15199. this._si.processIntersections(ss1, start1, ss2, start2);
  15200. } else {
  15201. return super.overlap.apply(this, arguments);
  15202. }
  15203. }
  15204. }
  15205. MCIndexNoder.SegmentOverlapAction = SegmentOverlapAction;
  15206. class ScaledNoder {
  15207. constructor() {
  15208. ScaledNoder.constructor_.apply(this, arguments);
  15209. }
  15210. static constructor_() {
  15211. this._noder = null;
  15212. this._scaleFactor = null;
  15213. this._offsetX = null;
  15214. this._offsetY = null;
  15215. this._isScaled = false;
  15216. if (arguments.length === 2) {
  15217. const noder = arguments[0],
  15218. scaleFactor = arguments[1];
  15219. ScaledNoder.constructor_.call(this, noder, scaleFactor, 0, 0);
  15220. } else if (arguments.length === 4) {
  15221. const noder = arguments[0],
  15222. scaleFactor = arguments[1];
  15223. this._noder = noder;
  15224. this._scaleFactor = scaleFactor;
  15225. this._isScaled = !this.isIntegerPrecision();
  15226. }
  15227. }
  15228. rescale() {
  15229. if (hasInterface(arguments[0], Collection)) {
  15230. const segStrings = arguments[0];
  15231. for (let i = segStrings.iterator(); i.hasNext();) {
  15232. const ss = i.next();
  15233. this.rescale(ss.getCoordinates());
  15234. }
  15235. } else if (arguments[0] instanceof Array) {
  15236. const pts = arguments[0];
  15237. for (let i = 0; i < pts.length; i++) {
  15238. pts[i].x = pts[i].x / this._scaleFactor + this._offsetX;
  15239. pts[i].y = pts[i].y / this._scaleFactor + this._offsetY;
  15240. }
  15241. if (pts.length === 2 && pts[0].equals2D(pts[1])) System.out.println(pts);
  15242. }
  15243. }
  15244. scale() {
  15245. if (hasInterface(arguments[0], Collection)) {
  15246. const segStrings = arguments[0];
  15247. const nodedSegmentStrings = new ArrayList(segStrings.size());
  15248. for (let i = segStrings.iterator(); i.hasNext();) {
  15249. const ss = i.next();
  15250. nodedSegmentStrings.add(new NodedSegmentString(this.scale(ss.getCoordinates()), ss.getData()));
  15251. }
  15252. return nodedSegmentStrings;
  15253. } else if (arguments[0] instanceof Array) {
  15254. const pts = arguments[0];
  15255. const roundPts = new Array(pts.length).fill(null);
  15256. for (let i = 0; i < pts.length; i++) roundPts[i] = new Coordinate(Math.round((pts[i].x - this._offsetX) * this._scaleFactor), Math.round((pts[i].y - this._offsetY) * this._scaleFactor), pts[i].getZ());
  15257. const roundPtsNoDup = CoordinateArrays.removeRepeatedPoints(roundPts);
  15258. return roundPtsNoDup;
  15259. }
  15260. }
  15261. isIntegerPrecision() {
  15262. return this._scaleFactor === 1.0;
  15263. }
  15264. getNodedSubstrings() {
  15265. const splitSS = this._noder.getNodedSubstrings();
  15266. if (this._isScaled) this.rescale(splitSS);
  15267. return splitSS;
  15268. }
  15269. computeNodes(inputSegStrings) {
  15270. let intSegStrings = inputSegStrings;
  15271. if (this._isScaled) intSegStrings = this.scale(inputSegStrings);
  15272. this._noder.computeNodes(intSegStrings);
  15273. }
  15274. get interfaces_() {
  15275. return [Noder];
  15276. }
  15277. }
  15278. var noding = /*#__PURE__*/Object.freeze({
  15279. __proto__: null,
  15280. MCIndexNoder: MCIndexNoder,
  15281. ScaledNoder: ScaledNoder,
  15282. SegmentString: SegmentString
  15283. });
  15284. class BoundaryOp {
  15285. constructor() {
  15286. BoundaryOp.constructor_.apply(this, arguments);
  15287. }
  15288. static constructor_() {
  15289. this._geom = null;
  15290. this._geomFact = null;
  15291. this._bnRule = null;
  15292. this._endpointMap = null;
  15293. if (arguments.length === 1) {
  15294. const geom = arguments[0];
  15295. BoundaryOp.constructor_.call(this, geom, BoundaryNodeRule.MOD2_BOUNDARY_RULE);
  15296. } else if (arguments.length === 2) {
  15297. const geom = arguments[0],
  15298. bnRule = arguments[1];
  15299. this._geom = geom;
  15300. this._geomFact = geom.getFactory();
  15301. this._bnRule = bnRule;
  15302. }
  15303. }
  15304. static getBoundary() {
  15305. if (arguments.length === 1) {
  15306. const g = arguments[0];
  15307. const bop = new BoundaryOp(g);
  15308. return bop.getBoundary();
  15309. } else if (arguments.length === 2) {
  15310. const g = arguments[0],
  15311. bnRule = arguments[1];
  15312. const bop = new BoundaryOp(g, bnRule);
  15313. return bop.getBoundary();
  15314. }
  15315. }
  15316. boundaryMultiLineString(mLine) {
  15317. if (this._geom.isEmpty()) return this.getEmptyMultiPoint();
  15318. const bdyPts = this.computeBoundaryCoordinates(mLine);
  15319. if (bdyPts.length === 1) return this._geomFact.createPoint(bdyPts[0]);
  15320. return this._geomFact.createMultiPointFromCoords(bdyPts);
  15321. }
  15322. getBoundary() {
  15323. if (this._geom instanceof LineString) return this.boundaryLineString(this._geom);
  15324. if (this._geom instanceof MultiLineString) return this.boundaryMultiLineString(this._geom);
  15325. return this._geom.getBoundary();
  15326. }
  15327. boundaryLineString(line) {
  15328. if (this._geom.isEmpty()) return this.getEmptyMultiPoint();
  15329. if (line.isClosed()) {
  15330. const closedEndpointOnBoundary = this._bnRule.isInBoundary(2);
  15331. if (closedEndpointOnBoundary) return line.getStartPoint();else return this._geomFact.createMultiPoint();
  15332. }
  15333. return this._geomFact.createMultiPoint([line.getStartPoint(), line.getEndPoint()]);
  15334. }
  15335. getEmptyMultiPoint() {
  15336. return this._geomFact.createMultiPoint();
  15337. }
  15338. computeBoundaryCoordinates(mLine) {
  15339. const bdyPts = new ArrayList();
  15340. this._endpointMap = new TreeMap();
  15341. for (let i = 0; i < mLine.getNumGeometries(); i++) {
  15342. const line = mLine.getGeometryN(i);
  15343. if (line.getNumPoints() === 0) continue;
  15344. this.addEndpoint(line.getCoordinateN(0));
  15345. this.addEndpoint(line.getCoordinateN(line.getNumPoints() - 1));
  15346. }
  15347. for (let it = this._endpointMap.entrySet().iterator(); it.hasNext();) {
  15348. const entry = it.next();
  15349. const counter = entry.getValue();
  15350. const valence = counter.count;
  15351. if (this._bnRule.isInBoundary(valence)) bdyPts.add(entry.getKey());
  15352. }
  15353. return CoordinateArrays.toCoordinateArray(bdyPts);
  15354. }
  15355. addEndpoint(pt) {
  15356. let counter = this._endpointMap.get(pt);
  15357. if (counter === null) {
  15358. counter = new Counter$1();
  15359. this._endpointMap.put(pt, counter);
  15360. }
  15361. counter.count++;
  15362. }
  15363. }
  15364. class Counter$1 {
  15365. constructor() {
  15366. Counter$1.constructor_.apply(this, arguments);
  15367. }
  15368. static constructor_() {
  15369. this.count = null;
  15370. }
  15371. }
  15372. class IsSimpleOp {
  15373. constructor() {
  15374. IsSimpleOp.constructor_.apply(this, arguments);
  15375. }
  15376. static constructor_() {
  15377. this._inputGeom = null;
  15378. this._isClosedEndpointsInInterior = true;
  15379. this._nonSimpleLocation = null;
  15380. if (arguments.length === 1) {
  15381. const geom = arguments[0];
  15382. this._inputGeom = geom;
  15383. } else if (arguments.length === 2) {
  15384. const geom = arguments[0],
  15385. boundaryNodeRule = arguments[1];
  15386. this._inputGeom = geom;
  15387. this._isClosedEndpointsInInterior = !boundaryNodeRule.isInBoundary(2);
  15388. }
  15389. }
  15390. static isSimple() {
  15391. if (arguments.length === 1) {
  15392. const geom = arguments[0];
  15393. const op = new IsSimpleOp(geom);
  15394. return op.isSimple();
  15395. } else if (arguments.length === 2) {
  15396. const geom = arguments[0],
  15397. boundaryNodeRule = arguments[1];
  15398. const op = new IsSimpleOp(geom, boundaryNodeRule);
  15399. return op.isSimple();
  15400. }
  15401. }
  15402. isSimpleMultiPoint(mp) {
  15403. if (mp.isEmpty()) return true;
  15404. const points = new TreeSet();
  15405. for (let i = 0; i < mp.getNumGeometries(); i++) {
  15406. const pt = mp.getGeometryN(i);
  15407. const p = pt.getCoordinate();
  15408. if (points.contains(p)) {
  15409. this._nonSimpleLocation = p;
  15410. return false;
  15411. }
  15412. points.add(p);
  15413. }
  15414. return true;
  15415. }
  15416. isSimplePolygonal(geom) {
  15417. const rings = LinearComponentExtracter.getLines(geom);
  15418. for (let i = rings.iterator(); i.hasNext();) {
  15419. const ring = i.next();
  15420. if (!this.isSimpleLinearGeometry(ring)) return false;
  15421. }
  15422. return true;
  15423. }
  15424. hasClosedEndpointIntersection(graph) {
  15425. const endPoints = new TreeMap();
  15426. for (let i = graph.getEdgeIterator(); i.hasNext();) {
  15427. const e = i.next();
  15428. const isClosed = e.isClosed();
  15429. const p0 = e.getCoordinate(0);
  15430. this.addEndpoint(endPoints, p0, isClosed);
  15431. const p1 = e.getCoordinate(e.getNumPoints() - 1);
  15432. this.addEndpoint(endPoints, p1, isClosed);
  15433. }
  15434. for (let i = endPoints.values().iterator(); i.hasNext();) {
  15435. const eiInfo = i.next();
  15436. if (eiInfo.isClosed && eiInfo.degree !== 2) {
  15437. this._nonSimpleLocation = eiInfo.getCoordinate();
  15438. return true;
  15439. }
  15440. }
  15441. return false;
  15442. }
  15443. getNonSimpleLocation() {
  15444. return this._nonSimpleLocation;
  15445. }
  15446. isSimpleLinearGeometry(geom) {
  15447. if (geom.isEmpty()) return true;
  15448. const graph = new GeometryGraph(0, geom);
  15449. const li = new RobustLineIntersector();
  15450. const si = graph.computeSelfNodes(li, true);
  15451. if (!si.hasIntersection()) return true;
  15452. if (si.hasProperIntersection()) {
  15453. this._nonSimpleLocation = si.getProperIntersectionPoint();
  15454. return false;
  15455. }
  15456. if (this.hasNonEndpointIntersection(graph)) return false;
  15457. if (this._isClosedEndpointsInInterior) if (this.hasClosedEndpointIntersection(graph)) return false;
  15458. return true;
  15459. }
  15460. hasNonEndpointIntersection(graph) {
  15461. for (let i = graph.getEdgeIterator(); i.hasNext();) {
  15462. const e = i.next();
  15463. const maxSegmentIndex = e.getMaximumSegmentIndex();
  15464. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext();) {
  15465. const ei = eiIt.next();
  15466. if (!ei.isEndPoint(maxSegmentIndex)) {
  15467. this._nonSimpleLocation = ei.getCoordinate();
  15468. return true;
  15469. }
  15470. }
  15471. }
  15472. return false;
  15473. }
  15474. addEndpoint(endPoints, p, isClosed) {
  15475. let eiInfo = endPoints.get(p);
  15476. if (eiInfo === null) {
  15477. eiInfo = new EndpointInfo(p);
  15478. endPoints.put(p, eiInfo);
  15479. }
  15480. eiInfo.addEndpoint(isClosed);
  15481. }
  15482. computeSimple(geom) {
  15483. this._nonSimpleLocation = null;
  15484. if (geom.isEmpty()) return true;
  15485. if (geom instanceof LineString) return this.isSimpleLinearGeometry(geom);
  15486. if (geom instanceof MultiLineString) return this.isSimpleLinearGeometry(geom);
  15487. if (geom instanceof MultiPoint) return this.isSimpleMultiPoint(geom);
  15488. if (hasInterface(geom, Polygonal)) return this.isSimplePolygonal(geom);
  15489. if (geom instanceof GeometryCollection) return this.isSimpleGeometryCollection(geom);
  15490. return true;
  15491. }
  15492. isSimple() {
  15493. this._nonSimpleLocation = null;
  15494. return this.computeSimple(this._inputGeom);
  15495. }
  15496. isSimpleGeometryCollection(geom) {
  15497. for (let i = 0; i < geom.getNumGeometries(); i++) {
  15498. const comp = geom.getGeometryN(i);
  15499. if (!this.computeSimple(comp)) return false;
  15500. }
  15501. return true;
  15502. }
  15503. }
  15504. class EndpointInfo {
  15505. constructor() {
  15506. EndpointInfo.constructor_.apply(this, arguments);
  15507. }
  15508. static constructor_() {
  15509. this.pt = null;
  15510. this.isClosed = null;
  15511. this.degree = null;
  15512. const pt = arguments[0];
  15513. this.pt = pt;
  15514. this.isClosed = false;
  15515. this.degree = 0;
  15516. }
  15517. addEndpoint(isClosed) {
  15518. this.degree++;
  15519. this.isClosed |= isClosed;
  15520. }
  15521. getCoordinate() {
  15522. return this.pt;
  15523. }
  15524. }
  15525. IsSimpleOp.EndpointInfo = EndpointInfo;
  15526. class BufferParameters {
  15527. constructor() {
  15528. BufferParameters.constructor_.apply(this, arguments);
  15529. }
  15530. static constructor_() {
  15531. this._quadrantSegments = BufferParameters.DEFAULT_QUADRANT_SEGMENTS;
  15532. this._endCapStyle = BufferParameters.CAP_ROUND;
  15533. this._joinStyle = BufferParameters.JOIN_ROUND;
  15534. this._mitreLimit = BufferParameters.DEFAULT_MITRE_LIMIT;
  15535. this._isSingleSided = false;
  15536. this._simplifyFactor = BufferParameters.DEFAULT_SIMPLIFY_FACTOR;
  15537. if (arguments.length === 0) ; else if (arguments.length === 1) {
  15538. const quadrantSegments = arguments[0];
  15539. this.setQuadrantSegments(quadrantSegments);
  15540. } else if (arguments.length === 2) {
  15541. const quadrantSegments = arguments[0],
  15542. endCapStyle = arguments[1];
  15543. this.setQuadrantSegments(quadrantSegments);
  15544. this.setEndCapStyle(endCapStyle);
  15545. } else if (arguments.length === 4) {
  15546. const quadrantSegments = arguments[0],
  15547. endCapStyle = arguments[1],
  15548. joinStyle = arguments[2],
  15549. mitreLimit = arguments[3];
  15550. this.setQuadrantSegments(quadrantSegments);
  15551. this.setEndCapStyle(endCapStyle);
  15552. this.setJoinStyle(joinStyle);
  15553. this.setMitreLimit(mitreLimit);
  15554. }
  15555. }
  15556. static bufferDistanceError(quadSegs) {
  15557. const alpha = Math.PI / 2.0 / quadSegs;
  15558. return 1 - Math.cos(alpha / 2.0);
  15559. }
  15560. getEndCapStyle() {
  15561. return this._endCapStyle;
  15562. }
  15563. isSingleSided() {
  15564. return this._isSingleSided;
  15565. }
  15566. setQuadrantSegments(quadSegs) {
  15567. this._quadrantSegments = quadSegs;
  15568. if (this._quadrantSegments === 0) this._joinStyle = BufferParameters.JOIN_BEVEL;
  15569. if (this._quadrantSegments < 0) {
  15570. this._joinStyle = BufferParameters.JOIN_MITRE;
  15571. this._mitreLimit = Math.abs(this._quadrantSegments);
  15572. }
  15573. if (quadSegs <= 0) this._quadrantSegments = 1;
  15574. if (this._joinStyle !== BufferParameters.JOIN_ROUND) this._quadrantSegments = BufferParameters.DEFAULT_QUADRANT_SEGMENTS;
  15575. }
  15576. getJoinStyle() {
  15577. return this._joinStyle;
  15578. }
  15579. setJoinStyle(joinStyle) {
  15580. this._joinStyle = joinStyle;
  15581. }
  15582. setSimplifyFactor(simplifyFactor) {
  15583. this._simplifyFactor = simplifyFactor < 0 ? 0 : simplifyFactor;
  15584. }
  15585. getSimplifyFactor() {
  15586. return this._simplifyFactor;
  15587. }
  15588. getQuadrantSegments() {
  15589. return this._quadrantSegments;
  15590. }
  15591. setEndCapStyle(endCapStyle) {
  15592. this._endCapStyle = endCapStyle;
  15593. }
  15594. getMitreLimit() {
  15595. return this._mitreLimit;
  15596. }
  15597. setMitreLimit(mitreLimit) {
  15598. this._mitreLimit = mitreLimit;
  15599. }
  15600. setSingleSided(isSingleSided) {
  15601. this._isSingleSided = isSingleSided;
  15602. }
  15603. }
  15604. BufferParameters.CAP_ROUND = 1;
  15605. BufferParameters.CAP_FLAT = 2;
  15606. BufferParameters.CAP_SQUARE = 3;
  15607. BufferParameters.JOIN_ROUND = 1;
  15608. BufferParameters.JOIN_MITRE = 2;
  15609. BufferParameters.JOIN_BEVEL = 3;
  15610. BufferParameters.DEFAULT_QUADRANT_SEGMENTS = 8;
  15611. BufferParameters.DEFAULT_MITRE_LIMIT = 5.0;
  15612. BufferParameters.DEFAULT_SIMPLIFY_FACTOR = 0.01;
  15613. class RightmostEdgeFinder {
  15614. constructor() {
  15615. RightmostEdgeFinder.constructor_.apply(this, arguments);
  15616. }
  15617. static constructor_() {
  15618. this._minIndex = -1;
  15619. this._minCoord = null;
  15620. this._minDe = null;
  15621. this._orientedDe = null;
  15622. }
  15623. getCoordinate() {
  15624. return this._minCoord;
  15625. }
  15626. getRightmostSide(de, index) {
  15627. let side = this.getRightmostSideOfSegment(de, index);
  15628. if (side < 0) side = this.getRightmostSideOfSegment(de, index - 1);
  15629. if (side < 0) {
  15630. this._minCoord = null;
  15631. this.checkForRightmostCoordinate(de);
  15632. }
  15633. return side;
  15634. }
  15635. findRightmostEdgeAtVertex() {
  15636. const pts = this._minDe.getEdge().getCoordinates();
  15637. Assert.isTrue(this._minIndex > 0 && this._minIndex < pts.length, 'rightmost point expected to be interior vertex of edge');
  15638. const pPrev = pts[this._minIndex - 1];
  15639. const pNext = pts[this._minIndex + 1];
  15640. const orientation = Orientation.index(this._minCoord, pNext, pPrev);
  15641. let usePrev = false;
  15642. if (pPrev.y < this._minCoord.y && pNext.y < this._minCoord.y && orientation === Orientation.COUNTERCLOCKWISE) usePrev = true;else if (pPrev.y > this._minCoord.y && pNext.y > this._minCoord.y && orientation === Orientation.CLOCKWISE) usePrev = true;
  15643. if (usePrev) this._minIndex = this._minIndex - 1;
  15644. }
  15645. getRightmostSideOfSegment(de, i) {
  15646. const e = de.getEdge();
  15647. const coord = e.getCoordinates();
  15648. if (i < 0 || i + 1 >= coord.length) return -1;
  15649. if (coord[i].y === coord[i + 1].y) return -1;
  15650. let pos = Position.LEFT;
  15651. if (coord[i].y < coord[i + 1].y) pos = Position.RIGHT;
  15652. return pos;
  15653. }
  15654. getEdge() {
  15655. return this._orientedDe;
  15656. }
  15657. checkForRightmostCoordinate(de) {
  15658. const coord = de.getEdge().getCoordinates();
  15659. for (let i = 0; i < coord.length - 1; i++) if (this._minCoord === null || coord[i].x > this._minCoord.x) {
  15660. this._minDe = de;
  15661. this._minIndex = i;
  15662. this._minCoord = coord[i];
  15663. }
  15664. }
  15665. findRightmostEdgeAtNode() {
  15666. const node = this._minDe.getNode();
  15667. const star = node.getEdges();
  15668. this._minDe = star.getRightmostEdge();
  15669. if (!this._minDe.isForward()) {
  15670. this._minDe = this._minDe.getSym();
  15671. this._minIndex = this._minDe.getEdge().getCoordinates().length - 1;
  15672. }
  15673. }
  15674. findEdge(dirEdgeList) {
  15675. for (let i = dirEdgeList.iterator(); i.hasNext();) {
  15676. const de = i.next();
  15677. if (!de.isForward()) continue;
  15678. this.checkForRightmostCoordinate(de);
  15679. }
  15680. Assert.isTrue(this._minIndex !== 0 || this._minCoord.equals(this._minDe.getCoordinate()), 'inconsistency in rightmost processing');
  15681. if (this._minIndex === 0) this.findRightmostEdgeAtNode();else this.findRightmostEdgeAtVertex();
  15682. this._orientedDe = this._minDe;
  15683. const rightmostSide = this.getRightmostSide(this._minDe, this._minIndex);
  15684. if (rightmostSide === Position.LEFT) this._orientedDe = this._minDe.getSym();
  15685. }
  15686. }
  15687. class LinkedList {
  15688. constructor() {
  15689. this.array = [];
  15690. }
  15691. addLast(e) {
  15692. this.array.push(e);
  15693. }
  15694. removeFirst() {
  15695. return this.array.shift();
  15696. }
  15697. isEmpty() {
  15698. return this.array.length === 0;
  15699. }
  15700. }
  15701. class BufferSubgraph {
  15702. constructor() {
  15703. BufferSubgraph.constructor_.apply(this, arguments);
  15704. }
  15705. static constructor_() {
  15706. this._finder = null;
  15707. this._dirEdgeList = new ArrayList();
  15708. this._nodes = new ArrayList();
  15709. this._rightMostCoord = null;
  15710. this._env = null;
  15711. this._finder = new RightmostEdgeFinder();
  15712. }
  15713. clearVisitedEdges() {
  15714. for (let it = this._dirEdgeList.iterator(); it.hasNext();) {
  15715. const de = it.next();
  15716. de.setVisited(false);
  15717. }
  15718. }
  15719. getRightmostCoordinate() {
  15720. return this._rightMostCoord;
  15721. }
  15722. computeNodeDepth(n) {
  15723. let startEdge = null;
  15724. for (let i = n.getEdges().iterator(); i.hasNext();) {
  15725. const de = i.next();
  15726. if (de.isVisited() || de.getSym().isVisited()) {
  15727. startEdge = de;
  15728. break;
  15729. }
  15730. }
  15731. if (startEdge === null) throw new TopologyException('unable to find edge to compute depths at ' + n.getCoordinate());
  15732. n.getEdges().computeDepths(startEdge);
  15733. for (let i = n.getEdges().iterator(); i.hasNext();) {
  15734. const de = i.next();
  15735. de.setVisited(true);
  15736. this.copySymDepths(de);
  15737. }
  15738. }
  15739. computeDepth(outsideDepth) {
  15740. this.clearVisitedEdges();
  15741. const de = this._finder.getEdge();
  15742. de.getNode();
  15743. de.getLabel();
  15744. de.setEdgeDepths(Position.RIGHT, outsideDepth);
  15745. this.copySymDepths(de);
  15746. this.computeDepths(de);
  15747. }
  15748. create(node) {
  15749. this.addReachable(node);
  15750. this._finder.findEdge(this._dirEdgeList);
  15751. this._rightMostCoord = this._finder.getCoordinate();
  15752. }
  15753. findResultEdges() {
  15754. for (let it = this._dirEdgeList.iterator(); it.hasNext();) {
  15755. const de = it.next();
  15756. if (de.getDepth(Position.RIGHT) >= 1 && de.getDepth(Position.LEFT) <= 0 && !de.isInteriorAreaEdge()) de.setInResult(true);
  15757. }
  15758. }
  15759. computeDepths(startEdge) {
  15760. const nodesVisited = new HashSet();
  15761. const nodeQueue = new LinkedList();
  15762. const startNode = startEdge.getNode();
  15763. nodeQueue.addLast(startNode);
  15764. nodesVisited.add(startNode);
  15765. startEdge.setVisited(true);
  15766. while (!nodeQueue.isEmpty()) {
  15767. const n = nodeQueue.removeFirst();
  15768. nodesVisited.add(n);
  15769. this.computeNodeDepth(n);
  15770. for (let i = n.getEdges().iterator(); i.hasNext();) {
  15771. const de = i.next();
  15772. const sym = de.getSym();
  15773. if (sym.isVisited()) continue;
  15774. const adjNode = sym.getNode();
  15775. if (!nodesVisited.contains(adjNode)) {
  15776. nodeQueue.addLast(adjNode);
  15777. nodesVisited.add(adjNode);
  15778. }
  15779. }
  15780. }
  15781. }
  15782. compareTo(o) {
  15783. const graph = o;
  15784. if (this._rightMostCoord.x < graph._rightMostCoord.x) return -1;
  15785. if (this._rightMostCoord.x > graph._rightMostCoord.x) return 1;
  15786. return 0;
  15787. }
  15788. getEnvelope() {
  15789. if (this._env === null) {
  15790. const edgeEnv = new Envelope();
  15791. for (let it = this._dirEdgeList.iterator(); it.hasNext();) {
  15792. const dirEdge = it.next();
  15793. const pts = dirEdge.getEdge().getCoordinates();
  15794. for (let i = 0; i < pts.length - 1; i++) edgeEnv.expandToInclude(pts[i]);
  15795. }
  15796. this._env = edgeEnv;
  15797. }
  15798. return this._env;
  15799. }
  15800. addReachable(startNode) {
  15801. const nodeStack = new Stack();
  15802. nodeStack.add(startNode);
  15803. while (!nodeStack.empty()) {
  15804. const node = nodeStack.pop();
  15805. this.add(node, nodeStack);
  15806. }
  15807. }
  15808. copySymDepths(de) {
  15809. const sym = de.getSym();
  15810. sym.setDepth(Position.LEFT, de.getDepth(Position.RIGHT));
  15811. sym.setDepth(Position.RIGHT, de.getDepth(Position.LEFT));
  15812. }
  15813. add(node, nodeStack) {
  15814. node.setVisited(true);
  15815. this._nodes.add(node);
  15816. for (let i = node.getEdges().iterator(); i.hasNext();) {
  15817. const de = i.next();
  15818. this._dirEdgeList.add(de);
  15819. const sym = de.getSym();
  15820. const symNode = sym.getNode();
  15821. if (!symNode.isVisited()) nodeStack.push(symNode);
  15822. }
  15823. }
  15824. getNodes() {
  15825. return this._nodes;
  15826. }
  15827. getDirectedEdges() {
  15828. return this._dirEdgeList;
  15829. }
  15830. get interfaces_() {
  15831. return [Comparable];
  15832. }
  15833. }
  15834. class EdgeRing$1 {
  15835. constructor() {
  15836. EdgeRing$1.constructor_.apply(this, arguments);
  15837. }
  15838. static constructor_() {
  15839. this._startDe = null;
  15840. this._maxNodeDegree = -1;
  15841. this._edges = new ArrayList();
  15842. this._pts = new ArrayList();
  15843. this._label = new Label(Location.NONE);
  15844. this._ring = null;
  15845. this._isHole = null;
  15846. this._shell = null;
  15847. this._holes = new ArrayList();
  15848. this._geometryFactory = null;
  15849. if (arguments.length === 0) ; else if (arguments.length === 2) {
  15850. const start = arguments[0],
  15851. geometryFactory = arguments[1];
  15852. this._geometryFactory = geometryFactory;
  15853. this.computePoints(start);
  15854. this.computeRing();
  15855. }
  15856. }
  15857. computeRing() {
  15858. if (this._ring !== null) return null;
  15859. const coord = new Array(this._pts.size()).fill(null);
  15860. for (let i = 0; i < this._pts.size(); i++) coord[i] = this._pts.get(i);
  15861. this._ring = this._geometryFactory.createLinearRing(coord);
  15862. this._isHole = Orientation.isCCW(this._ring.getCoordinates());
  15863. }
  15864. isIsolated() {
  15865. return this._label.getGeometryCount() === 1;
  15866. }
  15867. computePoints(start) {
  15868. this._startDe = start;
  15869. let de = start;
  15870. let isFirstEdge = true;
  15871. do {
  15872. if (de === null) throw new TopologyException('Found null DirectedEdge');
  15873. if (de.getEdgeRing() === this) throw new TopologyException('Directed Edge visited twice during ring-building at ' + de.getCoordinate());
  15874. this._edges.add(de);
  15875. const label = de.getLabel();
  15876. Assert.isTrue(label.isArea());
  15877. this.mergeLabel(label);
  15878. this.addPoints(de.getEdge(), de.isForward(), isFirstEdge);
  15879. isFirstEdge = false;
  15880. this.setEdgeRing(de, this);
  15881. de = this.getNext(de);
  15882. } while (de !== this._startDe);
  15883. }
  15884. getLinearRing() {
  15885. return this._ring;
  15886. }
  15887. getCoordinate(i) {
  15888. return this._pts.get(i);
  15889. }
  15890. computeMaxNodeDegree() {
  15891. this._maxNodeDegree = 0;
  15892. let de = this._startDe;
  15893. do {
  15894. const node = de.getNode();
  15895. const degree = node.getEdges().getOutgoingDegree(this);
  15896. if (degree > this._maxNodeDegree) this._maxNodeDegree = degree;
  15897. de = this.getNext(de);
  15898. } while (de !== this._startDe);
  15899. this._maxNodeDegree *= 2;
  15900. }
  15901. addPoints(edge, isForward, isFirstEdge) {
  15902. const edgePts = edge.getCoordinates();
  15903. if (isForward) {
  15904. let startIndex = 1;
  15905. if (isFirstEdge) startIndex = 0;
  15906. for (let i = startIndex; i < edgePts.length; i++) this._pts.add(edgePts[i]);
  15907. } else {
  15908. let startIndex = edgePts.length - 2;
  15909. if (isFirstEdge) startIndex = edgePts.length - 1;
  15910. for (let i = startIndex; i >= 0; i--) this._pts.add(edgePts[i]);
  15911. }
  15912. }
  15913. isHole() {
  15914. return this._isHole;
  15915. }
  15916. setInResult() {
  15917. let de = this._startDe;
  15918. do {
  15919. de.getEdge().setInResult(true);
  15920. de = de.getNext();
  15921. } while (de !== this._startDe);
  15922. }
  15923. containsPoint(p) {
  15924. const shell = this.getLinearRing();
  15925. const env = shell.getEnvelopeInternal();
  15926. if (!env.contains(p)) return false;
  15927. if (!PointLocation.isInRing(p, shell.getCoordinates())) return false;
  15928. for (let i = this._holes.iterator(); i.hasNext();) {
  15929. const hole = i.next();
  15930. if (hole.containsPoint(p)) return false;
  15931. }
  15932. return true;
  15933. }
  15934. addHole(ring) {
  15935. this._holes.add(ring);
  15936. }
  15937. isShell() {
  15938. return this._shell === null;
  15939. }
  15940. getLabel() {
  15941. return this._label;
  15942. }
  15943. getEdges() {
  15944. return this._edges;
  15945. }
  15946. getMaxNodeDegree() {
  15947. if (this._maxNodeDegree < 0) this.computeMaxNodeDegree();
  15948. return this._maxNodeDegree;
  15949. }
  15950. getShell() {
  15951. return this._shell;
  15952. }
  15953. mergeLabel() {
  15954. if (arguments.length === 1) {
  15955. const deLabel = arguments[0];
  15956. this.mergeLabel(deLabel, 0);
  15957. this.mergeLabel(deLabel, 1);
  15958. } else if (arguments.length === 2) {
  15959. const deLabel = arguments[0],
  15960. geomIndex = arguments[1];
  15961. const loc = deLabel.getLocation(geomIndex, Position.RIGHT);
  15962. if (loc === Location.NONE) return null;
  15963. if (this._label.getLocation(geomIndex) === Location.NONE) {
  15964. this._label.setLocation(geomIndex, loc);
  15965. return null;
  15966. }
  15967. }
  15968. }
  15969. setShell(shell) {
  15970. this._shell = shell;
  15971. if (shell !== null) shell.addHole(this);
  15972. }
  15973. toPolygon(geometryFactory) {
  15974. const holeLR = new Array(this._holes.size()).fill(null);
  15975. for (let i = 0; i < this._holes.size(); i++) holeLR[i] = this._holes.get(i).getLinearRing();
  15976. const poly = geometryFactory.createPolygon(this.getLinearRing(), holeLR);
  15977. return poly;
  15978. }
  15979. }
  15980. class MinimalEdgeRing extends EdgeRing$1 {
  15981. constructor() {
  15982. super();
  15983. MinimalEdgeRing.constructor_.apply(this, arguments);
  15984. }
  15985. static constructor_() {
  15986. const start = arguments[0],
  15987. geometryFactory = arguments[1];
  15988. EdgeRing$1.constructor_.call(this, start, geometryFactory);
  15989. }
  15990. setEdgeRing(de, er) {
  15991. de.setMinEdgeRing(er);
  15992. }
  15993. getNext(de) {
  15994. return de.getNextMin();
  15995. }
  15996. }
  15997. class MaximalEdgeRing extends EdgeRing$1 {
  15998. constructor() {
  15999. super();
  16000. MaximalEdgeRing.constructor_.apply(this, arguments);
  16001. }
  16002. static constructor_() {
  16003. const start = arguments[0],
  16004. geometryFactory = arguments[1];
  16005. EdgeRing$1.constructor_.call(this, start, geometryFactory);
  16006. }
  16007. buildMinimalRings() {
  16008. const minEdgeRings = new ArrayList();
  16009. let de = this._startDe;
  16010. do {
  16011. if (de.getMinEdgeRing() === null) {
  16012. const minEr = new MinimalEdgeRing(de, this._geometryFactory);
  16013. minEdgeRings.add(minEr);
  16014. }
  16015. de = de.getNext();
  16016. } while (de !== this._startDe);
  16017. return minEdgeRings;
  16018. }
  16019. setEdgeRing(de, er) {
  16020. de.setEdgeRing(er);
  16021. }
  16022. linkDirectedEdgesForMinimalEdgeRings() {
  16023. let de = this._startDe;
  16024. do {
  16025. const node = de.getNode();
  16026. node.getEdges().linkMinimalDirectedEdges(this);
  16027. de = de.getNext();
  16028. } while (de !== this._startDe);
  16029. }
  16030. getNext(de) {
  16031. return de.getNext();
  16032. }
  16033. }
  16034. class PolygonBuilder {
  16035. constructor() {
  16036. PolygonBuilder.constructor_.apply(this, arguments);
  16037. }
  16038. static constructor_() {
  16039. this._geometryFactory = null;
  16040. this._shellList = new ArrayList();
  16041. const geometryFactory = arguments[0];
  16042. this._geometryFactory = geometryFactory;
  16043. }
  16044. static findEdgeRingContaining(testEr, shellList) {
  16045. const testRing = testEr.getLinearRing();
  16046. const testEnv = testRing.getEnvelopeInternal();
  16047. let testPt = testRing.getCoordinateN(0);
  16048. let minShell = null;
  16049. let minShellEnv = null;
  16050. for (let it = shellList.iterator(); it.hasNext();) {
  16051. const tryShell = it.next();
  16052. const tryShellRing = tryShell.getLinearRing();
  16053. const tryShellEnv = tryShellRing.getEnvelopeInternal();
  16054. if (tryShellEnv.equals(testEnv)) continue;
  16055. if (!tryShellEnv.contains(testEnv)) continue;
  16056. testPt = CoordinateArrays.ptNotInList(testRing.getCoordinates(), tryShellRing.getCoordinates());
  16057. let isContained = false;
  16058. if (PointLocation.isInRing(testPt, tryShellRing.getCoordinates())) isContained = true;
  16059. if (isContained) if (minShell === null || minShellEnv.contains(tryShellEnv)) {
  16060. minShell = tryShell;
  16061. minShellEnv = minShell.getLinearRing().getEnvelopeInternal();
  16062. }
  16063. }
  16064. return minShell;
  16065. }
  16066. sortShellsAndHoles(edgeRings, shellList, freeHoleList) {
  16067. for (let it = edgeRings.iterator(); it.hasNext();) {
  16068. const er = it.next();
  16069. if (er.isHole()) freeHoleList.add(er);else shellList.add(er);
  16070. }
  16071. }
  16072. computePolygons(shellList) {
  16073. const resultPolyList = new ArrayList();
  16074. for (let it = shellList.iterator(); it.hasNext();) {
  16075. const er = it.next();
  16076. const poly = er.toPolygon(this._geometryFactory);
  16077. resultPolyList.add(poly);
  16078. }
  16079. return resultPolyList;
  16080. }
  16081. placeFreeHoles(shellList, freeHoleList) {
  16082. for (let it = freeHoleList.iterator(); it.hasNext();) {
  16083. const hole = it.next();
  16084. if (hole.getShell() === null) {
  16085. const shell = PolygonBuilder.findEdgeRingContaining(hole, shellList);
  16086. if (shell === null) throw new TopologyException('unable to assign hole to a shell', hole.getCoordinate(0));
  16087. hole.setShell(shell);
  16088. }
  16089. }
  16090. }
  16091. buildMinimalEdgeRings(maxEdgeRings, shellList, freeHoleList) {
  16092. const edgeRings = new ArrayList();
  16093. for (let it = maxEdgeRings.iterator(); it.hasNext();) {
  16094. const er = it.next();
  16095. if (er.getMaxNodeDegree() > 2) {
  16096. er.linkDirectedEdgesForMinimalEdgeRings();
  16097. const minEdgeRings = er.buildMinimalRings();
  16098. const shell = this.findShell(minEdgeRings);
  16099. if (shell !== null) {
  16100. this.placePolygonHoles(shell, minEdgeRings);
  16101. shellList.add(shell);
  16102. } else {
  16103. freeHoleList.addAll(minEdgeRings);
  16104. }
  16105. } else {
  16106. edgeRings.add(er);
  16107. }
  16108. }
  16109. return edgeRings;
  16110. }
  16111. buildMaximalEdgeRings(dirEdges) {
  16112. const maxEdgeRings = new ArrayList();
  16113. for (let it = dirEdges.iterator(); it.hasNext();) {
  16114. const de = it.next();
  16115. if (de.isInResult() && de.getLabel().isArea()) if (de.getEdgeRing() === null) {
  16116. const er = new MaximalEdgeRing(de, this._geometryFactory);
  16117. maxEdgeRings.add(er);
  16118. er.setInResult();
  16119. }
  16120. }
  16121. return maxEdgeRings;
  16122. }
  16123. placePolygonHoles(shell, minEdgeRings) {
  16124. for (let it = minEdgeRings.iterator(); it.hasNext();) {
  16125. const er = it.next();
  16126. if (er.isHole()) er.setShell(shell);
  16127. }
  16128. }
  16129. getPolygons() {
  16130. const resultPolyList = this.computePolygons(this._shellList);
  16131. return resultPolyList;
  16132. }
  16133. findShell(minEdgeRings) {
  16134. let shellCount = 0;
  16135. let shell = null;
  16136. for (let it = minEdgeRings.iterator(); it.hasNext();) {
  16137. const er = it.next();
  16138. if (!er.isHole()) {
  16139. shell = er;
  16140. shellCount++;
  16141. }
  16142. }
  16143. Assert.isTrue(shellCount <= 1, 'found two shells in MinimalEdgeRing list');
  16144. return shell;
  16145. }
  16146. add() {
  16147. if (arguments.length === 1) {
  16148. const graph = arguments[0];
  16149. this.add(graph.getEdgeEnds(), graph.getNodes());
  16150. } else if (arguments.length === 2) {
  16151. const dirEdges = arguments[0],
  16152. nodes = arguments[1];
  16153. PlanarGraph$1.linkResultDirectedEdges(nodes);
  16154. const maxEdgeRings = this.buildMaximalEdgeRings(dirEdges);
  16155. const freeHoleList = new ArrayList();
  16156. const edgeRings = this.buildMinimalEdgeRings(maxEdgeRings, this._shellList, freeHoleList);
  16157. this.sortShellsAndHoles(edgeRings, this._shellList, freeHoleList);
  16158. this.placeFreeHoles(this._shellList, freeHoleList);
  16159. }
  16160. }
  16161. }
  16162. class BufferInputLineSimplifier {
  16163. constructor() {
  16164. BufferInputLineSimplifier.constructor_.apply(this, arguments);
  16165. }
  16166. static constructor_() {
  16167. this._inputLine = null;
  16168. this._distanceTol = null;
  16169. this._isDeleted = null;
  16170. this._angleOrientation = Orientation.COUNTERCLOCKWISE;
  16171. const inputLine = arguments[0];
  16172. this._inputLine = inputLine;
  16173. }
  16174. static simplify(inputLine, distanceTol) {
  16175. const simp = new BufferInputLineSimplifier(inputLine);
  16176. return simp.simplify(distanceTol);
  16177. }
  16178. isDeletable(i0, i1, i2, distanceTol) {
  16179. const p0 = this._inputLine[i0];
  16180. const p1 = this._inputLine[i1];
  16181. const p2 = this._inputLine[i2];
  16182. if (!this.isConcave(p0, p1, p2)) return false;
  16183. if (!this.isShallow(p0, p1, p2, distanceTol)) return false;
  16184. return this.isShallowSampled(p0, p1, i0, i2, distanceTol);
  16185. }
  16186. deleteShallowConcavities() {
  16187. let index = 1;
  16188. let midIndex = this.findNextNonDeletedIndex(index);
  16189. let lastIndex = this.findNextNonDeletedIndex(midIndex);
  16190. let isChanged = false;
  16191. while (lastIndex < this._inputLine.length) {
  16192. let isMiddleVertexDeleted = false;
  16193. if (this.isDeletable(index, midIndex, lastIndex, this._distanceTol)) {
  16194. this._isDeleted[midIndex] = BufferInputLineSimplifier.DELETE;
  16195. isMiddleVertexDeleted = true;
  16196. isChanged = true;
  16197. }
  16198. if (isMiddleVertexDeleted) index = lastIndex;else index = midIndex;
  16199. midIndex = this.findNextNonDeletedIndex(index);
  16200. lastIndex = this.findNextNonDeletedIndex(midIndex);
  16201. }
  16202. return isChanged;
  16203. }
  16204. isShallowConcavity(p0, p1, p2, distanceTol) {
  16205. const orientation = Orientation.index(p0, p1, p2);
  16206. const isAngleToSimplify = orientation === this._angleOrientation;
  16207. if (!isAngleToSimplify) return false;
  16208. const dist = Distance.pointToSegment(p1, p0, p2);
  16209. return dist < distanceTol;
  16210. }
  16211. isShallowSampled(p0, p2, i0, i2, distanceTol) {
  16212. let inc = Math.trunc((i2 - i0) / BufferInputLineSimplifier.NUM_PTS_TO_CHECK);
  16213. if (inc <= 0) inc = 1;
  16214. for (let i = i0; i < i2; i += inc) if (!this.isShallow(p0, p2, this._inputLine[i], distanceTol)) return false;
  16215. return true;
  16216. }
  16217. isConcave(p0, p1, p2) {
  16218. const orientation = Orientation.index(p0, p1, p2);
  16219. const isConcave = orientation === this._angleOrientation;
  16220. return isConcave;
  16221. }
  16222. simplify(distanceTol) {
  16223. this._distanceTol = Math.abs(distanceTol);
  16224. if (distanceTol < 0) this._angleOrientation = Orientation.CLOCKWISE;
  16225. this._isDeleted = new Array(this._inputLine.length).fill(null);
  16226. let isChanged = false;
  16227. do isChanged = this.deleteShallowConcavities(); while (isChanged);
  16228. return this.collapseLine();
  16229. }
  16230. findNextNonDeletedIndex(index) {
  16231. let next = index + 1;
  16232. while (next < this._inputLine.length && this._isDeleted[next] === BufferInputLineSimplifier.DELETE) next++;
  16233. return next;
  16234. }
  16235. isShallow(p0, p1, p2, distanceTol) {
  16236. const dist = Distance.pointToSegment(p1, p0, p2);
  16237. return dist < distanceTol;
  16238. }
  16239. collapseLine() {
  16240. const coordList = new CoordinateList();
  16241. for (let i = 0; i < this._inputLine.length; i++) if (this._isDeleted[i] !== BufferInputLineSimplifier.DELETE) coordList.add(this._inputLine[i]);
  16242. return coordList.toCoordinateArray();
  16243. }
  16244. }
  16245. BufferInputLineSimplifier.INIT = 0;
  16246. BufferInputLineSimplifier.DELETE = 1;
  16247. BufferInputLineSimplifier.KEEP = 1;
  16248. BufferInputLineSimplifier.NUM_PTS_TO_CHECK = 10;
  16249. class OffsetSegmentString {
  16250. constructor() {
  16251. OffsetSegmentString.constructor_.apply(this, arguments);
  16252. }
  16253. static constructor_() {
  16254. this._ptList = null;
  16255. this._precisionModel = null;
  16256. this._minimimVertexDistance = 0.0;
  16257. this._ptList = new ArrayList();
  16258. }
  16259. getCoordinates() {
  16260. const coord = this._ptList.toArray(OffsetSegmentString.COORDINATE_ARRAY_TYPE);
  16261. return coord;
  16262. }
  16263. setPrecisionModel(precisionModel) {
  16264. this._precisionModel = precisionModel;
  16265. }
  16266. addPt(pt) {
  16267. const bufPt = new Coordinate(pt);
  16268. this._precisionModel.makePrecise(bufPt);
  16269. if (this.isRedundant(bufPt)) return null;
  16270. this._ptList.add(bufPt);
  16271. }
  16272. reverse() {}
  16273. addPts(pt, isForward) {
  16274. if (isForward) for (let i = 0; i < pt.length; i++) this.addPt(pt[i]);else for (let i = pt.length - 1; i >= 0; i--) this.addPt(pt[i]);
  16275. }
  16276. isRedundant(pt) {
  16277. if (this._ptList.size() < 1) return false;
  16278. const lastPt = this._ptList.get(this._ptList.size() - 1);
  16279. const ptDist = pt.distance(lastPt);
  16280. if (ptDist < this._minimimVertexDistance) return true;
  16281. return false;
  16282. }
  16283. toString() {
  16284. const fact = new GeometryFactory();
  16285. const line = fact.createLineString(this.getCoordinates());
  16286. return line.toString();
  16287. }
  16288. closeRing() {
  16289. if (this._ptList.size() < 1) return null;
  16290. const startPt = new Coordinate(this._ptList.get(0));
  16291. const lastPt = this._ptList.get(this._ptList.size() - 1);
  16292. if (startPt.equals(lastPt)) return null;
  16293. this._ptList.add(startPt);
  16294. }
  16295. setMinimumVertexDistance(minimimVertexDistance) {
  16296. this._minimimVertexDistance = minimimVertexDistance;
  16297. }
  16298. }
  16299. OffsetSegmentString.COORDINATE_ARRAY_TYPE = new Array(0).fill(null);
  16300. class OffsetSegmentGenerator {
  16301. constructor() {
  16302. OffsetSegmentGenerator.constructor_.apply(this, arguments);
  16303. }
  16304. static constructor_() {
  16305. this._maxCurveSegmentError = 0.0;
  16306. this._filletAngleQuantum = null;
  16307. this._closingSegLengthFactor = 1;
  16308. this._segList = null;
  16309. this._distance = 0.0;
  16310. this._precisionModel = null;
  16311. this._bufParams = null;
  16312. this._li = null;
  16313. this._s0 = null;
  16314. this._s1 = null;
  16315. this._s2 = null;
  16316. this._seg0 = new LineSegment();
  16317. this._seg1 = new LineSegment();
  16318. this._offset0 = new LineSegment();
  16319. this._offset1 = new LineSegment();
  16320. this._side = 0;
  16321. this._hasNarrowConcaveAngle = false;
  16322. const precisionModel = arguments[0],
  16323. bufParams = arguments[1],
  16324. distance = arguments[2];
  16325. this._precisionModel = precisionModel;
  16326. this._bufParams = bufParams;
  16327. this._li = new RobustLineIntersector();
  16328. this._filletAngleQuantum = Math.PI / 2.0 / bufParams.getQuadrantSegments();
  16329. if (bufParams.getQuadrantSegments() >= 8 && bufParams.getJoinStyle() === BufferParameters.JOIN_ROUND) this._closingSegLengthFactor = OffsetSegmentGenerator.MAX_CLOSING_SEG_LEN_FACTOR;
  16330. this.init(distance);
  16331. }
  16332. addNextSegment(p, addStartPoint) {
  16333. this._s0 = this._s1;
  16334. this._s1 = this._s2;
  16335. this._s2 = p;
  16336. this._seg0.setCoordinates(this._s0, this._s1);
  16337. this.computeOffsetSegment(this._seg0, this._side, this._distance, this._offset0);
  16338. this._seg1.setCoordinates(this._s1, this._s2);
  16339. this.computeOffsetSegment(this._seg1, this._side, this._distance, this._offset1);
  16340. if (this._s1.equals(this._s2)) return null;
  16341. const orientation = Orientation.index(this._s0, this._s1, this._s2);
  16342. const outsideTurn = orientation === Orientation.CLOCKWISE && this._side === Position.LEFT || orientation === Orientation.COUNTERCLOCKWISE && this._side === Position.RIGHT;
  16343. if (orientation === 0) this.addCollinear(addStartPoint);else if (outsideTurn) this.addOutsideTurn(orientation, addStartPoint);else this.addInsideTurn(orientation, addStartPoint);
  16344. }
  16345. addLineEndCap(p0, p1) {
  16346. const seg = new LineSegment(p0, p1);
  16347. const offsetL = new LineSegment();
  16348. this.computeOffsetSegment(seg, Position.LEFT, this._distance, offsetL);
  16349. const offsetR = new LineSegment();
  16350. this.computeOffsetSegment(seg, Position.RIGHT, this._distance, offsetR);
  16351. const dx = p1.x - p0.x;
  16352. const dy = p1.y - p0.y;
  16353. const angle = Math.atan2(dy, dx);
  16354. switch (this._bufParams.getEndCapStyle()) {
  16355. case BufferParameters.CAP_ROUND:
  16356. this._segList.addPt(offsetL.p1);
  16357. this.addDirectedFillet(p1, angle + Math.PI / 2, angle - Math.PI / 2, Orientation.CLOCKWISE, this._distance);
  16358. this._segList.addPt(offsetR.p1);
  16359. break;
  16360. case BufferParameters.CAP_FLAT:
  16361. this._segList.addPt(offsetL.p1);
  16362. this._segList.addPt(offsetR.p1);
  16363. break;
  16364. case BufferParameters.CAP_SQUARE:
  16365. const squareCapSideOffset = new Coordinate();
  16366. squareCapSideOffset.x = Math.abs(this._distance) * Math.cos(angle);
  16367. squareCapSideOffset.y = Math.abs(this._distance) * Math.sin(angle);
  16368. const squareCapLOffset = new Coordinate(offsetL.p1.x + squareCapSideOffset.x, offsetL.p1.y + squareCapSideOffset.y);
  16369. const squareCapROffset = new Coordinate(offsetR.p1.x + squareCapSideOffset.x, offsetR.p1.y + squareCapSideOffset.y);
  16370. this._segList.addPt(squareCapLOffset);
  16371. this._segList.addPt(squareCapROffset);
  16372. break;
  16373. }
  16374. }
  16375. getCoordinates() {
  16376. const pts = this._segList.getCoordinates();
  16377. return pts;
  16378. }
  16379. addMitreJoin(p, offset0, offset1, distance) {
  16380. const intPt = Intersection.intersection(offset0.p0, offset0.p1, offset1.p0, offset1.p1);
  16381. if (intPt !== null) {
  16382. const mitreRatio = distance <= 0.0 ? 1.0 : intPt.distance(p) / Math.abs(distance);
  16383. if (mitreRatio <= this._bufParams.getMitreLimit()) {
  16384. this._segList.addPt(intPt);
  16385. return null;
  16386. }
  16387. }
  16388. this.addLimitedMitreJoin(offset0, offset1, distance, this._bufParams.getMitreLimit());
  16389. }
  16390. addOutsideTurn(orientation, addStartPoint) {
  16391. if (this._offset0.p1.distance(this._offset1.p0) < this._distance * OffsetSegmentGenerator.OFFSET_SEGMENT_SEPARATION_FACTOR) {
  16392. this._segList.addPt(this._offset0.p1);
  16393. return null;
  16394. }
  16395. if (this._bufParams.getJoinStyle() === BufferParameters.JOIN_MITRE) {
  16396. this.addMitreJoin(this._s1, this._offset0, this._offset1, this._distance);
  16397. } else if (this._bufParams.getJoinStyle() === BufferParameters.JOIN_BEVEL) {
  16398. this.addBevelJoin(this._offset0, this._offset1);
  16399. } else {
  16400. if (addStartPoint) this._segList.addPt(this._offset0.p1);
  16401. this.addCornerFillet(this._s1, this._offset0.p1, this._offset1.p0, orientation, this._distance);
  16402. this._segList.addPt(this._offset1.p0);
  16403. }
  16404. }
  16405. createSquare(p) {
  16406. this._segList.addPt(new Coordinate(p.x + this._distance, p.y + this._distance));
  16407. this._segList.addPt(new Coordinate(p.x + this._distance, p.y - this._distance));
  16408. this._segList.addPt(new Coordinate(p.x - this._distance, p.y - this._distance));
  16409. this._segList.addPt(new Coordinate(p.x - this._distance, p.y + this._distance));
  16410. this._segList.closeRing();
  16411. }
  16412. addSegments(pt, isForward) {
  16413. this._segList.addPts(pt, isForward);
  16414. }
  16415. addFirstSegment() {
  16416. this._segList.addPt(this._offset1.p0);
  16417. }
  16418. addCornerFillet(p, p0, p1, direction, radius) {
  16419. const dx0 = p0.x - p.x;
  16420. const dy0 = p0.y - p.y;
  16421. let startAngle = Math.atan2(dy0, dx0);
  16422. const dx1 = p1.x - p.x;
  16423. const dy1 = p1.y - p.y;
  16424. const endAngle = Math.atan2(dy1, dx1);
  16425. if (direction === Orientation.CLOCKWISE) {
  16426. if (startAngle <= endAngle) startAngle += 2.0 * Math.PI;
  16427. } else {
  16428. if (startAngle >= endAngle) startAngle -= 2.0 * Math.PI;
  16429. }
  16430. this._segList.addPt(p0);
  16431. this.addDirectedFillet(p, startAngle, endAngle, direction, radius);
  16432. this._segList.addPt(p1);
  16433. }
  16434. addLastSegment() {
  16435. this._segList.addPt(this._offset1.p1);
  16436. }
  16437. initSideSegments(s1, s2, side) {
  16438. this._s1 = s1;
  16439. this._s2 = s2;
  16440. this._side = side;
  16441. this._seg1.setCoordinates(s1, s2);
  16442. this.computeOffsetSegment(this._seg1, side, this._distance, this._offset1);
  16443. }
  16444. addLimitedMitreJoin(offset0, offset1, distance, mitreLimit) {
  16445. const basePt = this._seg0.p1;
  16446. const ang0 = Angle.angle(basePt, this._seg0.p0);
  16447. const angDiff = Angle.angleBetweenOriented(this._seg0.p0, basePt, this._seg1.p1);
  16448. const angDiffHalf = angDiff / 2;
  16449. const midAng = Angle.normalize(ang0 + angDiffHalf);
  16450. const mitreMidAng = Angle.normalize(midAng + Math.PI);
  16451. const mitreDist = mitreLimit * distance;
  16452. const bevelDelta = mitreDist * Math.abs(Math.sin(angDiffHalf));
  16453. const bevelHalfLen = distance - bevelDelta;
  16454. const bevelMidX = basePt.x + mitreDist * Math.cos(mitreMidAng);
  16455. const bevelMidY = basePt.y + mitreDist * Math.sin(mitreMidAng);
  16456. const bevelMidPt = new Coordinate(bevelMidX, bevelMidY);
  16457. const mitreMidLine = new LineSegment(basePt, bevelMidPt);
  16458. const bevelEndLeft = mitreMidLine.pointAlongOffset(1.0, bevelHalfLen);
  16459. const bevelEndRight = mitreMidLine.pointAlongOffset(1.0, -bevelHalfLen);
  16460. if (this._side === Position.LEFT) {
  16461. this._segList.addPt(bevelEndLeft);
  16462. this._segList.addPt(bevelEndRight);
  16463. } else {
  16464. this._segList.addPt(bevelEndRight);
  16465. this._segList.addPt(bevelEndLeft);
  16466. }
  16467. }
  16468. addDirectedFillet(p, startAngle, endAngle, direction, radius) {
  16469. const directionFactor = direction === Orientation.CLOCKWISE ? -1 : 1;
  16470. const totalAngle = Math.abs(startAngle - endAngle);
  16471. const nSegs = Math.trunc(totalAngle / this._filletAngleQuantum + 0.5);
  16472. if (nSegs < 1) return null;
  16473. const angleInc = totalAngle / nSegs;
  16474. const pt = new Coordinate();
  16475. for (let i = 0; i < nSegs; i++) {
  16476. const angle = startAngle + directionFactor * i * angleInc;
  16477. pt.x = p.x + radius * Math.cos(angle);
  16478. pt.y = p.y + radius * Math.sin(angle);
  16479. this._segList.addPt(pt);
  16480. }
  16481. }
  16482. computeOffsetSegment(seg, side, distance, offset) {
  16483. const sideSign = side === Position.LEFT ? 1 : -1;
  16484. const dx = seg.p1.x - seg.p0.x;
  16485. const dy = seg.p1.y - seg.p0.y;
  16486. const len = Math.sqrt(dx * dx + dy * dy);
  16487. const ux = sideSign * distance * dx / len;
  16488. const uy = sideSign * distance * dy / len;
  16489. offset.p0.x = seg.p0.x - uy;
  16490. offset.p0.y = seg.p0.y + ux;
  16491. offset.p1.x = seg.p1.x - uy;
  16492. offset.p1.y = seg.p1.y + ux;
  16493. }
  16494. addInsideTurn(orientation, addStartPoint) {
  16495. this._li.computeIntersection(this._offset0.p0, this._offset0.p1, this._offset1.p0, this._offset1.p1);
  16496. if (this._li.hasIntersection()) {
  16497. this._segList.addPt(this._li.getIntersection(0));
  16498. } else {
  16499. this._hasNarrowConcaveAngle = true;
  16500. if (this._offset0.p1.distance(this._offset1.p0) < this._distance * OffsetSegmentGenerator.INSIDE_TURN_VERTEX_SNAP_DISTANCE_FACTOR) {
  16501. this._segList.addPt(this._offset0.p1);
  16502. } else {
  16503. this._segList.addPt(this._offset0.p1);
  16504. if (this._closingSegLengthFactor > 0) {
  16505. const mid0 = new Coordinate((this._closingSegLengthFactor * this._offset0.p1.x + this._s1.x) / (this._closingSegLengthFactor + 1), (this._closingSegLengthFactor * this._offset0.p1.y + this._s1.y) / (this._closingSegLengthFactor + 1));
  16506. this._segList.addPt(mid0);
  16507. const mid1 = new Coordinate((this._closingSegLengthFactor * this._offset1.p0.x + this._s1.x) / (this._closingSegLengthFactor + 1), (this._closingSegLengthFactor * this._offset1.p0.y + this._s1.y) / (this._closingSegLengthFactor + 1));
  16508. this._segList.addPt(mid1);
  16509. } else {
  16510. this._segList.addPt(this._s1);
  16511. }
  16512. this._segList.addPt(this._offset1.p0);
  16513. }
  16514. }
  16515. }
  16516. createCircle(p) {
  16517. const pt = new Coordinate(p.x + this._distance, p.y);
  16518. this._segList.addPt(pt);
  16519. this.addDirectedFillet(p, 0.0, 2.0 * Math.PI, -1, this._distance);
  16520. this._segList.closeRing();
  16521. }
  16522. addBevelJoin(offset0, offset1) {
  16523. this._segList.addPt(offset0.p1);
  16524. this._segList.addPt(offset1.p0);
  16525. }
  16526. init(distance) {
  16527. this._distance = distance;
  16528. this._maxCurveSegmentError = distance * (1 - Math.cos(this._filletAngleQuantum / 2.0));
  16529. this._segList = new OffsetSegmentString();
  16530. this._segList.setPrecisionModel(this._precisionModel);
  16531. this._segList.setMinimumVertexDistance(distance * OffsetSegmentGenerator.CURVE_VERTEX_SNAP_DISTANCE_FACTOR);
  16532. }
  16533. addCollinear(addStartPoint) {
  16534. this._li.computeIntersection(this._s0, this._s1, this._s1, this._s2);
  16535. const numInt = this._li.getIntersectionNum();
  16536. if (numInt >= 2) if (this._bufParams.getJoinStyle() === BufferParameters.JOIN_BEVEL || this._bufParams.getJoinStyle() === BufferParameters.JOIN_MITRE) {
  16537. if (addStartPoint) this._segList.addPt(this._offset0.p1);
  16538. this._segList.addPt(this._offset1.p0);
  16539. } else {
  16540. this.addCornerFillet(this._s1, this._offset0.p1, this._offset1.p0, Orientation.CLOCKWISE, this._distance);
  16541. }
  16542. }
  16543. closeRing() {
  16544. this._segList.closeRing();
  16545. }
  16546. hasNarrowConcaveAngle() {
  16547. return this._hasNarrowConcaveAngle;
  16548. }
  16549. }
  16550. OffsetSegmentGenerator.OFFSET_SEGMENT_SEPARATION_FACTOR = 1.0E-3;
  16551. OffsetSegmentGenerator.INSIDE_TURN_VERTEX_SNAP_DISTANCE_FACTOR = 1.0E-3;
  16552. OffsetSegmentGenerator.CURVE_VERTEX_SNAP_DISTANCE_FACTOR = 1.0E-6;
  16553. OffsetSegmentGenerator.MAX_CLOSING_SEG_LEN_FACTOR = 80;
  16554. class OffsetCurveBuilder {
  16555. constructor() {
  16556. OffsetCurveBuilder.constructor_.apply(this, arguments);
  16557. }
  16558. static constructor_() {
  16559. this._distance = 0.0;
  16560. this._precisionModel = null;
  16561. this._bufParams = null;
  16562. const precisionModel = arguments[0],
  16563. bufParams = arguments[1];
  16564. this._precisionModel = precisionModel;
  16565. this._bufParams = bufParams;
  16566. }
  16567. static copyCoordinates(pts) {
  16568. const copy = new Array(pts.length).fill(null);
  16569. for (let i = 0; i < copy.length; i++) copy[i] = new Coordinate(pts[i]);
  16570. return copy;
  16571. }
  16572. getOffsetCurve(inputPts, distance) {
  16573. this._distance = distance;
  16574. if (distance === 0.0) return null;
  16575. const isRightSide = distance < 0.0;
  16576. const posDistance = Math.abs(distance);
  16577. const segGen = this.getSegGen(posDistance);
  16578. if (inputPts.length <= 1) this.computePointCurve(inputPts[0], segGen);else this.computeOffsetCurve(inputPts, isRightSide, segGen);
  16579. const curvePts = segGen.getCoordinates();
  16580. if (isRightSide) CoordinateArrays.reverse(curvePts);
  16581. return curvePts;
  16582. }
  16583. computeSingleSidedBufferCurve(inputPts, isRightSide, segGen) {
  16584. const distTol = this.simplifyTolerance(this._distance);
  16585. if (isRightSide) {
  16586. segGen.addSegments(inputPts, true);
  16587. const simp2 = BufferInputLineSimplifier.simplify(inputPts, -distTol);
  16588. const n2 = simp2.length - 1;
  16589. segGen.initSideSegments(simp2[n2], simp2[n2 - 1], Position.LEFT);
  16590. segGen.addFirstSegment();
  16591. for (let i = n2 - 2; i >= 0; i--) segGen.addNextSegment(simp2[i], true);
  16592. } else {
  16593. segGen.addSegments(inputPts, false);
  16594. const simp1 = BufferInputLineSimplifier.simplify(inputPts, distTol);
  16595. const n1 = simp1.length - 1;
  16596. segGen.initSideSegments(simp1[0], simp1[1], Position.LEFT);
  16597. segGen.addFirstSegment();
  16598. for (let i = 2; i <= n1; i++) segGen.addNextSegment(simp1[i], true);
  16599. }
  16600. segGen.addLastSegment();
  16601. segGen.closeRing();
  16602. }
  16603. computeRingBufferCurve(inputPts, side, segGen) {
  16604. let distTol = this.simplifyTolerance(this._distance);
  16605. if (side === Position.RIGHT) distTol = -distTol;
  16606. const simp = BufferInputLineSimplifier.simplify(inputPts, distTol);
  16607. const n = simp.length - 1;
  16608. segGen.initSideSegments(simp[n - 1], simp[0], side);
  16609. for (let i = 1; i <= n; i++) {
  16610. const addStartPoint = i !== 1;
  16611. segGen.addNextSegment(simp[i], addStartPoint);
  16612. }
  16613. segGen.closeRing();
  16614. }
  16615. computeLineBufferCurve(inputPts, segGen) {
  16616. const distTol = this.simplifyTolerance(this._distance);
  16617. const simp1 = BufferInputLineSimplifier.simplify(inputPts, distTol);
  16618. const n1 = simp1.length - 1;
  16619. segGen.initSideSegments(simp1[0], simp1[1], Position.LEFT);
  16620. for (let i = 2; i <= n1; i++) segGen.addNextSegment(simp1[i], true);
  16621. segGen.addLastSegment();
  16622. segGen.addLineEndCap(simp1[n1 - 1], simp1[n1]);
  16623. const simp2 = BufferInputLineSimplifier.simplify(inputPts, -distTol);
  16624. const n2 = simp2.length - 1;
  16625. segGen.initSideSegments(simp2[n2], simp2[n2 - 1], Position.LEFT);
  16626. for (let i = n2 - 2; i >= 0; i--) segGen.addNextSegment(simp2[i], true);
  16627. segGen.addLastSegment();
  16628. segGen.addLineEndCap(simp2[1], simp2[0]);
  16629. segGen.closeRing();
  16630. }
  16631. computePointCurve(pt, segGen) {
  16632. switch (this._bufParams.getEndCapStyle()) {
  16633. case BufferParameters.CAP_ROUND:
  16634. segGen.createCircle(pt);
  16635. break;
  16636. case BufferParameters.CAP_SQUARE:
  16637. segGen.createSquare(pt);
  16638. break;
  16639. }
  16640. }
  16641. getLineCurve(inputPts, distance) {
  16642. this._distance = distance;
  16643. if (this.isLineOffsetEmpty(distance)) return null;
  16644. const posDistance = Math.abs(distance);
  16645. const segGen = this.getSegGen(posDistance);
  16646. if (inputPts.length <= 1) {
  16647. this.computePointCurve(inputPts[0], segGen);
  16648. } else if (this._bufParams.isSingleSided()) {
  16649. const isRightSide = distance < 0.0;
  16650. this.computeSingleSidedBufferCurve(inputPts, isRightSide, segGen);
  16651. } else {
  16652. this.computeLineBufferCurve(inputPts, segGen);
  16653. }
  16654. const lineCoord = segGen.getCoordinates();
  16655. return lineCoord;
  16656. }
  16657. getBufferParameters() {
  16658. return this._bufParams;
  16659. }
  16660. simplifyTolerance(bufDistance) {
  16661. return bufDistance * this._bufParams.getSimplifyFactor();
  16662. }
  16663. getRingCurve(inputPts, side, distance) {
  16664. this._distance = distance;
  16665. if (inputPts.length <= 2) return this.getLineCurve(inputPts, distance);
  16666. if (distance === 0.0) return OffsetCurveBuilder.copyCoordinates(inputPts);
  16667. const segGen = this.getSegGen(distance);
  16668. this.computeRingBufferCurve(inputPts, side, segGen);
  16669. return segGen.getCoordinates();
  16670. }
  16671. computeOffsetCurve(inputPts, isRightSide, segGen) {
  16672. const distTol = this.simplifyTolerance(this._distance);
  16673. if (isRightSide) {
  16674. const simp2 = BufferInputLineSimplifier.simplify(inputPts, -distTol);
  16675. const n2 = simp2.length - 1;
  16676. segGen.initSideSegments(simp2[n2], simp2[n2 - 1], Position.LEFT);
  16677. segGen.addFirstSegment();
  16678. for (let i = n2 - 2; i >= 0; i--) segGen.addNextSegment(simp2[i], true);
  16679. } else {
  16680. const simp1 = BufferInputLineSimplifier.simplify(inputPts, distTol);
  16681. const n1 = simp1.length - 1;
  16682. segGen.initSideSegments(simp1[0], simp1[1], Position.LEFT);
  16683. segGen.addFirstSegment();
  16684. for (let i = 2; i <= n1; i++) segGen.addNextSegment(simp1[i], true);
  16685. }
  16686. segGen.addLastSegment();
  16687. }
  16688. isLineOffsetEmpty(distance) {
  16689. if (distance === 0.0) return true;
  16690. if (distance < 0.0 && !this._bufParams.isSingleSided()) return true;
  16691. return false;
  16692. }
  16693. getSegGen(distance) {
  16694. return new OffsetSegmentGenerator(this._precisionModel, this._bufParams, distance);
  16695. }
  16696. }
  16697. class SubgraphDepthLocater {
  16698. constructor() {
  16699. SubgraphDepthLocater.constructor_.apply(this, arguments);
  16700. }
  16701. static constructor_() {
  16702. this._subgraphs = null;
  16703. this._seg = new LineSegment();
  16704. const subgraphs = arguments[0];
  16705. this._subgraphs = subgraphs;
  16706. }
  16707. findStabbedSegments() {
  16708. if (arguments.length === 1) {
  16709. const stabbingRayLeftPt = arguments[0];
  16710. const stabbedSegments = new ArrayList();
  16711. for (let i = this._subgraphs.iterator(); i.hasNext();) {
  16712. const bsg = i.next();
  16713. const env = bsg.getEnvelope();
  16714. if (stabbingRayLeftPt.y < env.getMinY() || stabbingRayLeftPt.y > env.getMaxY()) continue;
  16715. this.findStabbedSegments(stabbingRayLeftPt, bsg.getDirectedEdges(), stabbedSegments);
  16716. }
  16717. return stabbedSegments;
  16718. } else if (arguments.length === 3) {
  16719. if (hasInterface(arguments[2], List) && arguments[0] instanceof Coordinate && arguments[1] instanceof DirectedEdge$1) {
  16720. const stabbingRayLeftPt = arguments[0],
  16721. dirEdge = arguments[1],
  16722. stabbedSegments = arguments[2];
  16723. const pts = dirEdge.getEdge().getCoordinates();
  16724. for (let i = 0; i < pts.length - 1; i++) {
  16725. this._seg.p0 = pts[i];
  16726. this._seg.p1 = pts[i + 1];
  16727. if (this._seg.p0.y > this._seg.p1.y) this._seg.reverse();
  16728. const maxx = Math.max(this._seg.p0.x, this._seg.p1.x);
  16729. if (maxx < stabbingRayLeftPt.x) continue;
  16730. if (this._seg.isHorizontal()) continue;
  16731. if (stabbingRayLeftPt.y < this._seg.p0.y || stabbingRayLeftPt.y > this._seg.p1.y) continue;
  16732. if (Orientation.index(this._seg.p0, this._seg.p1, stabbingRayLeftPt) === Orientation.RIGHT) continue;
  16733. let depth = dirEdge.getDepth(Position.LEFT);
  16734. if (!this._seg.p0.equals(pts[i])) depth = dirEdge.getDepth(Position.RIGHT);
  16735. const ds = new DepthSegment(this._seg, depth);
  16736. stabbedSegments.add(ds);
  16737. }
  16738. } else if (hasInterface(arguments[2], List) && arguments[0] instanceof Coordinate && hasInterface(arguments[1], List)) {
  16739. const stabbingRayLeftPt = arguments[0],
  16740. dirEdges = arguments[1],
  16741. stabbedSegments = arguments[2];
  16742. for (let i = dirEdges.iterator(); i.hasNext();) {
  16743. const de = i.next();
  16744. if (!de.isForward()) continue;
  16745. this.findStabbedSegments(stabbingRayLeftPt, de, stabbedSegments);
  16746. }
  16747. }
  16748. }
  16749. }
  16750. getDepth(p) {
  16751. const stabbedSegments = this.findStabbedSegments(p);
  16752. if (stabbedSegments.size() === 0) return 0;
  16753. const ds = Collections.min(stabbedSegments);
  16754. return ds._leftDepth;
  16755. }
  16756. }
  16757. class DepthSegment {
  16758. constructor() {
  16759. DepthSegment.constructor_.apply(this, arguments);
  16760. }
  16761. static constructor_() {
  16762. this._upwardSeg = null;
  16763. this._leftDepth = null;
  16764. const seg = arguments[0],
  16765. depth = arguments[1];
  16766. this._upwardSeg = new LineSegment(seg);
  16767. this._leftDepth = depth;
  16768. }
  16769. compareTo(obj) {
  16770. const other = obj;
  16771. if (this._upwardSeg.minX() >= other._upwardSeg.maxX()) return 1;
  16772. if (this._upwardSeg.maxX() <= other._upwardSeg.minX()) return -1;
  16773. let orientIndex = this._upwardSeg.orientationIndex(other._upwardSeg);
  16774. if (orientIndex !== 0) return orientIndex;
  16775. orientIndex = -1 * other._upwardSeg.orientationIndex(this._upwardSeg);
  16776. if (orientIndex !== 0) return orientIndex;
  16777. return this._upwardSeg.compareTo(other._upwardSeg);
  16778. }
  16779. compareX(seg0, seg1) {
  16780. const compare0 = seg0.p0.compareTo(seg1.p0);
  16781. if (compare0 !== 0) return compare0;
  16782. return seg0.p1.compareTo(seg1.p1);
  16783. }
  16784. toString() {
  16785. return this._upwardSeg.toString();
  16786. }
  16787. get interfaces_() {
  16788. return [Comparable];
  16789. }
  16790. }
  16791. SubgraphDepthLocater.DepthSegment = DepthSegment;
  16792. class OffsetCurveSetBuilder {
  16793. constructor() {
  16794. OffsetCurveSetBuilder.constructor_.apply(this, arguments);
  16795. }
  16796. static constructor_() {
  16797. this._inputGeom = null;
  16798. this._distance = null;
  16799. this._curveBuilder = null;
  16800. this._curveList = new ArrayList();
  16801. const inputGeom = arguments[0],
  16802. distance = arguments[1],
  16803. curveBuilder = arguments[2];
  16804. this._inputGeom = inputGeom;
  16805. this._distance = distance;
  16806. this._curveBuilder = curveBuilder;
  16807. }
  16808. addRingSide(coord, offsetDistance, side, cwLeftLoc, cwRightLoc) {
  16809. if (offsetDistance === 0.0 && coord.length < LinearRing.MINIMUM_VALID_SIZE) return null;
  16810. let leftLoc = cwLeftLoc;
  16811. let rightLoc = cwRightLoc;
  16812. if (coord.length >= LinearRing.MINIMUM_VALID_SIZE && Orientation.isCCW(coord)) {
  16813. leftLoc = cwRightLoc;
  16814. rightLoc = cwLeftLoc;
  16815. side = Position.opposite(side);
  16816. }
  16817. const curve = this._curveBuilder.getRingCurve(coord, side, offsetDistance);
  16818. this.addCurve(curve, leftLoc, rightLoc);
  16819. }
  16820. addRingBothSides(coord, distance) {
  16821. this.addRingSide(coord, distance, Position.LEFT, Location.EXTERIOR, Location.INTERIOR);
  16822. this.addRingSide(coord, distance, Position.RIGHT, Location.INTERIOR, Location.EXTERIOR);
  16823. }
  16824. addPoint(p) {
  16825. if (this._distance <= 0.0) return null;
  16826. const coord = p.getCoordinates();
  16827. const curve = this._curveBuilder.getLineCurve(coord, this._distance);
  16828. this.addCurve(curve, Location.EXTERIOR, Location.INTERIOR);
  16829. }
  16830. addPolygon(p) {
  16831. let offsetDistance = this._distance;
  16832. let offsetSide = Position.LEFT;
  16833. if (this._distance < 0.0) {
  16834. offsetDistance = -this._distance;
  16835. offsetSide = Position.RIGHT;
  16836. }
  16837. const shell = p.getExteriorRing();
  16838. const shellCoord = CoordinateArrays.removeRepeatedPoints(shell.getCoordinates());
  16839. if (this._distance < 0.0 && this.isErodedCompletely(shell, this._distance)) return null;
  16840. if (this._distance <= 0.0 && shellCoord.length < 3) return null;
  16841. this.addRingSide(shellCoord, offsetDistance, offsetSide, Location.EXTERIOR, Location.INTERIOR);
  16842. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  16843. const hole = p.getInteriorRingN(i);
  16844. const holeCoord = CoordinateArrays.removeRepeatedPoints(hole.getCoordinates());
  16845. if (this._distance > 0.0 && this.isErodedCompletely(hole, -this._distance)) continue;
  16846. this.addRingSide(holeCoord, offsetDistance, Position.opposite(offsetSide), Location.INTERIOR, Location.EXTERIOR);
  16847. }
  16848. }
  16849. isTriangleErodedCompletely(triangleCoord, bufferDistance) {
  16850. const tri = new Triangle(triangleCoord[0], triangleCoord[1], triangleCoord[2]);
  16851. const inCentre = tri.inCentre();
  16852. const distToCentre = Distance.pointToSegment(inCentre, tri.p0, tri.p1);
  16853. return distToCentre < Math.abs(bufferDistance);
  16854. }
  16855. addLineString(line) {
  16856. if (this._curveBuilder.isLineOffsetEmpty(this._distance)) return null;
  16857. const coord = CoordinateArrays.removeRepeatedPoints(line.getCoordinates());
  16858. if (CoordinateArrays.isRing(coord) && !this._curveBuilder.getBufferParameters().isSingleSided()) {
  16859. this.addRingBothSides(coord, this._distance);
  16860. } else {
  16861. const curve = this._curveBuilder.getLineCurve(coord, this._distance);
  16862. this.addCurve(curve, Location.EXTERIOR, Location.INTERIOR);
  16863. }
  16864. }
  16865. addCurve(coord, leftLoc, rightLoc) {
  16866. if (coord === null || coord.length < 2) return null;
  16867. const e = new NodedSegmentString(coord, new Label(0, Location.BOUNDARY, leftLoc, rightLoc));
  16868. this._curveList.add(e);
  16869. }
  16870. getCurves() {
  16871. this.add(this._inputGeom);
  16872. return this._curveList;
  16873. }
  16874. add(g) {
  16875. if (g.isEmpty()) return null;
  16876. if (g instanceof Polygon) this.addPolygon(g);else if (g instanceof LineString) this.addLineString(g);else if (g instanceof Point) this.addPoint(g);else if (g instanceof MultiPoint) this.addCollection(g);else if (g instanceof MultiLineString) this.addCollection(g);else if (g instanceof MultiPolygon) this.addCollection(g);else if (g instanceof GeometryCollection) this.addCollection(g);else throw new UnsupportedOperationException(g.getGeometryType());
  16877. }
  16878. isErodedCompletely(ring, bufferDistance) {
  16879. const ringCoord = ring.getCoordinates();
  16880. if (ringCoord.length < 4) return bufferDistance < 0;
  16881. if (ringCoord.length === 4) return this.isTriangleErodedCompletely(ringCoord, bufferDistance);
  16882. const env = ring.getEnvelopeInternal();
  16883. const envMinDimension = Math.min(env.getHeight(), env.getWidth());
  16884. if (bufferDistance < 0.0 && 2 * Math.abs(bufferDistance) > envMinDimension) return true;
  16885. return false;
  16886. }
  16887. addCollection(gc) {
  16888. for (let i = 0; i < gc.getNumGeometries(); i++) {
  16889. const g = gc.getGeometryN(i);
  16890. this.add(g);
  16891. }
  16892. }
  16893. }
  16894. class EdgeEndStar {
  16895. constructor() {
  16896. EdgeEndStar.constructor_.apply(this, arguments);
  16897. }
  16898. static constructor_() {
  16899. this._edgeMap = new TreeMap();
  16900. this._edgeList = null;
  16901. this._ptInAreaLocation = [Location.NONE, Location.NONE];
  16902. }
  16903. getNextCW(ee) {
  16904. this.getEdges();
  16905. const i = this._edgeList.indexOf(ee);
  16906. let iNextCW = i - 1;
  16907. if (i === 0) iNextCW = this._edgeList.size() - 1;
  16908. return this._edgeList.get(iNextCW);
  16909. }
  16910. propagateSideLabels(geomIndex) {
  16911. let startLoc = Location.NONE;
  16912. for (let it = this.iterator(); it.hasNext();) {
  16913. const e = it.next();
  16914. const label = e.getLabel();
  16915. if (label.isArea(geomIndex) && label.getLocation(geomIndex, Position.LEFT) !== Location.NONE) startLoc = label.getLocation(geomIndex, Position.LEFT);
  16916. }
  16917. if (startLoc === Location.NONE) return null;
  16918. let currLoc = startLoc;
  16919. for (let it = this.iterator(); it.hasNext();) {
  16920. const e = it.next();
  16921. const label = e.getLabel();
  16922. if (label.getLocation(geomIndex, Position.ON) === Location.NONE) label.setLocation(geomIndex, Position.ON, currLoc);
  16923. if (label.isArea(geomIndex)) {
  16924. const leftLoc = label.getLocation(geomIndex, Position.LEFT);
  16925. const rightLoc = label.getLocation(geomIndex, Position.RIGHT);
  16926. if (rightLoc !== Location.NONE) {
  16927. if (rightLoc !== currLoc) throw new TopologyException('side location conflict', e.getCoordinate());
  16928. if (leftLoc === Location.NONE) Assert.shouldNeverReachHere('found single null side (at ' + e.getCoordinate() + ')');
  16929. currLoc = leftLoc;
  16930. } else {
  16931. Assert.isTrue(label.getLocation(geomIndex, Position.LEFT) === Location.NONE, 'found single null side');
  16932. label.setLocation(geomIndex, Position.RIGHT, currLoc);
  16933. label.setLocation(geomIndex, Position.LEFT, currLoc);
  16934. }
  16935. }
  16936. }
  16937. }
  16938. getCoordinate() {
  16939. const it = this.iterator();
  16940. if (!it.hasNext()) return null;
  16941. const e = it.next();
  16942. return e.getCoordinate();
  16943. }
  16944. print(out) {
  16945. System.out.println('EdgeEndStar: ' + this.getCoordinate());
  16946. for (let it = this.iterator(); it.hasNext();) {
  16947. const e = it.next();
  16948. e.print(out);
  16949. }
  16950. }
  16951. isAreaLabelsConsistent(geomGraph) {
  16952. this.computeEdgeEndLabels(geomGraph.getBoundaryNodeRule());
  16953. return this.checkAreaLabelsConsistent(0);
  16954. }
  16955. checkAreaLabelsConsistent(geomIndex) {
  16956. const edges = this.getEdges();
  16957. if (edges.size() <= 0) return true;
  16958. const lastEdgeIndex = edges.size() - 1;
  16959. const startLabel = edges.get(lastEdgeIndex).getLabel();
  16960. const startLoc = startLabel.getLocation(geomIndex, Position.LEFT);
  16961. Assert.isTrue(startLoc !== Location.NONE, 'Found unlabelled area edge');
  16962. let currLoc = startLoc;
  16963. for (let it = this.iterator(); it.hasNext();) {
  16964. const e = it.next();
  16965. const label = e.getLabel();
  16966. Assert.isTrue(label.isArea(geomIndex), 'Found non-area edge');
  16967. const leftLoc = label.getLocation(geomIndex, Position.LEFT);
  16968. const rightLoc = label.getLocation(geomIndex, Position.RIGHT);
  16969. if (leftLoc === rightLoc) return false;
  16970. if (rightLoc !== currLoc) return false;
  16971. currLoc = leftLoc;
  16972. }
  16973. return true;
  16974. }
  16975. findIndex(eSearch) {
  16976. this.iterator();
  16977. for (let i = 0; i < this._edgeList.size(); i++) {
  16978. const e = this._edgeList.get(i);
  16979. if (e === eSearch) return i;
  16980. }
  16981. return -1;
  16982. }
  16983. iterator() {
  16984. return this.getEdges().iterator();
  16985. }
  16986. getEdges() {
  16987. if (this._edgeList === null) this._edgeList = new ArrayList(this._edgeMap.values());
  16988. return this._edgeList;
  16989. }
  16990. getLocation(geomIndex, p, geom) {
  16991. if (this._ptInAreaLocation[geomIndex] === Location.NONE) this._ptInAreaLocation[geomIndex] = SimplePointInAreaLocator.locate(p, geom[geomIndex].getGeometry());
  16992. return this._ptInAreaLocation[geomIndex];
  16993. }
  16994. toString() {
  16995. const buf = new StringBuffer();
  16996. buf.append('EdgeEndStar: ' + this.getCoordinate());
  16997. buf.append('\n');
  16998. for (let it = this.iterator(); it.hasNext();) {
  16999. const e = it.next();
  17000. buf.append(e);
  17001. buf.append('\n');
  17002. }
  17003. return buf.toString();
  17004. }
  17005. computeEdgeEndLabels(boundaryNodeRule) {
  17006. for (let it = this.iterator(); it.hasNext();) {
  17007. const ee = it.next();
  17008. ee.computeLabel(boundaryNodeRule);
  17009. }
  17010. }
  17011. computeLabelling(geomGraph) {
  17012. this.computeEdgeEndLabels(geomGraph[0].getBoundaryNodeRule());
  17013. this.propagateSideLabels(0);
  17014. this.propagateSideLabels(1);
  17015. const hasDimensionalCollapseEdge = [false, false];
  17016. for (let it = this.iterator(); it.hasNext();) {
  17017. const e = it.next();
  17018. const label = e.getLabel();
  17019. for (let geomi = 0; geomi < 2; geomi++) if (label.isLine(geomi) && label.getLocation(geomi) === Location.BOUNDARY) hasDimensionalCollapseEdge[geomi] = true;
  17020. }
  17021. for (let it = this.iterator(); it.hasNext();) {
  17022. const e = it.next();
  17023. const label = e.getLabel();
  17024. for (let geomi = 0; geomi < 2; geomi++) if (label.isAnyNull(geomi)) {
  17025. let loc = Location.NONE;
  17026. if (hasDimensionalCollapseEdge[geomi]) {
  17027. loc = Location.EXTERIOR;
  17028. } else {
  17029. const p = e.getCoordinate();
  17030. loc = this.getLocation(geomi, p, geomGraph);
  17031. }
  17032. label.setAllLocationsIfNull(geomi, loc);
  17033. }
  17034. }
  17035. }
  17036. getDegree() {
  17037. return this._edgeMap.size();
  17038. }
  17039. insertEdgeEnd(e, obj) {
  17040. this._edgeMap.put(e, obj);
  17041. this._edgeList = null;
  17042. }
  17043. }
  17044. class DirectedEdgeStar$1 extends EdgeEndStar {
  17045. constructor() {
  17046. super();
  17047. DirectedEdgeStar$1.constructor_.apply(this, arguments);
  17048. }
  17049. static constructor_() {
  17050. this._resultAreaEdgeList = null;
  17051. this._label = null;
  17052. this._SCANNING_FOR_INCOMING = 1;
  17053. this._LINKING_TO_OUTGOING = 2;
  17054. }
  17055. linkResultDirectedEdges() {
  17056. this.getResultAreaEdges();
  17057. let firstOut = null;
  17058. let incoming = null;
  17059. let state = this._SCANNING_FOR_INCOMING;
  17060. for (let i = 0; i < this._resultAreaEdgeList.size(); i++) {
  17061. const nextOut = this._resultAreaEdgeList.get(i);
  17062. const nextIn = nextOut.getSym();
  17063. if (!nextOut.getLabel().isArea()) continue;
  17064. if (firstOut === null && nextOut.isInResult()) firstOut = nextOut;
  17065. switch (state) {
  17066. case this._SCANNING_FOR_INCOMING:
  17067. if (!nextIn.isInResult()) continue;
  17068. incoming = nextIn;
  17069. state = this._LINKING_TO_OUTGOING;
  17070. break;
  17071. case this._LINKING_TO_OUTGOING:
  17072. if (!nextOut.isInResult()) continue;
  17073. incoming.setNext(nextOut);
  17074. state = this._SCANNING_FOR_INCOMING;
  17075. break;
  17076. }
  17077. }
  17078. if (state === this._LINKING_TO_OUTGOING) {
  17079. if (firstOut === null) throw new TopologyException('no outgoing dirEdge found', this.getCoordinate());
  17080. Assert.isTrue(firstOut.isInResult(), 'unable to link last incoming dirEdge');
  17081. incoming.setNext(firstOut);
  17082. }
  17083. }
  17084. insert(ee) {
  17085. const de = ee;
  17086. this.insertEdgeEnd(de, de);
  17087. }
  17088. getRightmostEdge() {
  17089. const edges = this.getEdges();
  17090. const size = edges.size();
  17091. if (size < 1) return null;
  17092. const de0 = edges.get(0);
  17093. if (size === 1) return de0;
  17094. const deLast = edges.get(size - 1);
  17095. const quad0 = de0.getQuadrant();
  17096. const quad1 = deLast.getQuadrant();
  17097. if (Quadrant.isNorthern(quad0) && Quadrant.isNorthern(quad1)) {
  17098. return de0;
  17099. } else if (!Quadrant.isNorthern(quad0) && !Quadrant.isNorthern(quad1)) {
  17100. return deLast;
  17101. } else {
  17102. if (de0.getDy() !== 0) return de0;else if (deLast.getDy() !== 0) return deLast;
  17103. }
  17104. Assert.shouldNeverReachHere('found two horizontal edges incident on node');
  17105. return null;
  17106. }
  17107. print(out) {
  17108. System.out.println('DirectedEdgeStar: ' + this.getCoordinate());
  17109. for (let it = this.iterator(); it.hasNext();) {
  17110. const de = it.next();
  17111. out.print('out ');
  17112. de.print(out);
  17113. out.println();
  17114. out.print('in ');
  17115. de.getSym().print(out);
  17116. out.println();
  17117. }
  17118. }
  17119. getResultAreaEdges() {
  17120. if (this._resultAreaEdgeList !== null) return this._resultAreaEdgeList;
  17121. this._resultAreaEdgeList = new ArrayList();
  17122. for (let it = this.iterator(); it.hasNext();) {
  17123. const de = it.next();
  17124. if (de.isInResult() || de.getSym().isInResult()) this._resultAreaEdgeList.add(de);
  17125. }
  17126. return this._resultAreaEdgeList;
  17127. }
  17128. updateLabelling(nodeLabel) {
  17129. for (let it = this.iterator(); it.hasNext();) {
  17130. const de = it.next();
  17131. const label = de.getLabel();
  17132. label.setAllLocationsIfNull(0, nodeLabel.getLocation(0));
  17133. label.setAllLocationsIfNull(1, nodeLabel.getLocation(1));
  17134. }
  17135. }
  17136. linkAllDirectedEdges() {
  17137. this.getEdges();
  17138. let prevOut = null;
  17139. let firstIn = null;
  17140. for (let i = this._edgeList.size() - 1; i >= 0; i--) {
  17141. const nextOut = this._edgeList.get(i);
  17142. const nextIn = nextOut.getSym();
  17143. if (firstIn === null) firstIn = nextIn;
  17144. if (prevOut !== null) nextIn.setNext(prevOut);
  17145. prevOut = nextOut;
  17146. }
  17147. firstIn.setNext(prevOut);
  17148. }
  17149. computeDepths() {
  17150. if (arguments.length === 1) {
  17151. const de = arguments[0];
  17152. const edgeIndex = this.findIndex(de);
  17153. const startDepth = de.getDepth(Position.LEFT);
  17154. const targetLastDepth = de.getDepth(Position.RIGHT);
  17155. const nextDepth = this.computeDepths(edgeIndex + 1, this._edgeList.size(), startDepth);
  17156. const lastDepth = this.computeDepths(0, edgeIndex, nextDepth);
  17157. if (lastDepth !== targetLastDepth) throw new TopologyException('depth mismatch at ' + de.getCoordinate());
  17158. } else if (arguments.length === 3) {
  17159. const startIndex = arguments[0],
  17160. endIndex = arguments[1],
  17161. startDepth = arguments[2];
  17162. let currDepth = startDepth;
  17163. for (let i = startIndex; i < endIndex; i++) {
  17164. const nextDe = this._edgeList.get(i);
  17165. nextDe.setEdgeDepths(Position.RIGHT, currDepth);
  17166. currDepth = nextDe.getDepth(Position.LEFT);
  17167. }
  17168. return currDepth;
  17169. }
  17170. }
  17171. mergeSymLabels() {
  17172. for (let it = this.iterator(); it.hasNext();) {
  17173. const de = it.next();
  17174. const label = de.getLabel();
  17175. label.merge(de.getSym().getLabel());
  17176. }
  17177. }
  17178. linkMinimalDirectedEdges(er) {
  17179. let firstOut = null;
  17180. let incoming = null;
  17181. let state = this._SCANNING_FOR_INCOMING;
  17182. for (let i = this._resultAreaEdgeList.size() - 1; i >= 0; i--) {
  17183. const nextOut = this._resultAreaEdgeList.get(i);
  17184. const nextIn = nextOut.getSym();
  17185. if (firstOut === null && nextOut.getEdgeRing() === er) firstOut = nextOut;
  17186. switch (state) {
  17187. case this._SCANNING_FOR_INCOMING:
  17188. if (nextIn.getEdgeRing() !== er) continue;
  17189. incoming = nextIn;
  17190. state = this._LINKING_TO_OUTGOING;
  17191. break;
  17192. case this._LINKING_TO_OUTGOING:
  17193. if (nextOut.getEdgeRing() !== er) continue;
  17194. incoming.setNextMin(nextOut);
  17195. state = this._SCANNING_FOR_INCOMING;
  17196. break;
  17197. }
  17198. }
  17199. if (state === this._LINKING_TO_OUTGOING) {
  17200. Assert.isTrue(firstOut !== null, 'found null for first outgoing dirEdge');
  17201. Assert.isTrue(firstOut.getEdgeRing() === er, 'unable to link last incoming dirEdge');
  17202. incoming.setNextMin(firstOut);
  17203. }
  17204. }
  17205. getOutgoingDegree() {
  17206. if (arguments.length === 0) {
  17207. let degree = 0;
  17208. for (let it = this.iterator(); it.hasNext();) {
  17209. const de = it.next();
  17210. if (de.isInResult()) degree++;
  17211. }
  17212. return degree;
  17213. } else if (arguments.length === 1) {
  17214. const er = arguments[0];
  17215. let degree = 0;
  17216. for (let it = this.iterator(); it.hasNext();) {
  17217. const de = it.next();
  17218. if (de.getEdgeRing() === er) degree++;
  17219. }
  17220. return degree;
  17221. }
  17222. }
  17223. getLabel() {
  17224. return this._label;
  17225. }
  17226. findCoveredLineEdges() {
  17227. let startLoc = Location.NONE;
  17228. for (let it = this.iterator(); it.hasNext();) {
  17229. const nextOut = it.next();
  17230. const nextIn = nextOut.getSym();
  17231. if (!nextOut.isLineEdge()) {
  17232. if (nextOut.isInResult()) {
  17233. startLoc = Location.INTERIOR;
  17234. break;
  17235. }
  17236. if (nextIn.isInResult()) {
  17237. startLoc = Location.EXTERIOR;
  17238. break;
  17239. }
  17240. }
  17241. }
  17242. if (startLoc === Location.NONE) return null;
  17243. let currLoc = startLoc;
  17244. for (let it = this.iterator(); it.hasNext();) {
  17245. const nextOut = it.next();
  17246. const nextIn = nextOut.getSym();
  17247. if (nextOut.isLineEdge()) {
  17248. nextOut.getEdge().setCovered(currLoc === Location.INTERIOR);
  17249. } else {
  17250. if (nextOut.isInResult()) currLoc = Location.EXTERIOR;
  17251. if (nextIn.isInResult()) currLoc = Location.INTERIOR;
  17252. }
  17253. }
  17254. }
  17255. computeLabelling(geom) {
  17256. super.computeLabelling.call(this, geom);
  17257. this._label = new Label(Location.NONE);
  17258. for (let it = this.iterator(); it.hasNext();) {
  17259. const ee = it.next();
  17260. const e = ee.getEdge();
  17261. const eLabel = e.getLabel();
  17262. for (let i = 0; i < 2; i++) {
  17263. const eLoc = eLabel.getLocation(i);
  17264. if (eLoc === Location.INTERIOR || eLoc === Location.BOUNDARY) this._label.setLocation(i, Location.INTERIOR);
  17265. }
  17266. }
  17267. }
  17268. }
  17269. class OverlayNodeFactory extends NodeFactory {
  17270. constructor() {
  17271. super();
  17272. }
  17273. createNode(coord) {
  17274. return new Node$2(coord, new DirectedEdgeStar$1());
  17275. }
  17276. }
  17277. class OrientedCoordinateArray {
  17278. constructor() {
  17279. OrientedCoordinateArray.constructor_.apply(this, arguments);
  17280. }
  17281. static constructor_() {
  17282. this._pts = null;
  17283. this._orientation = null;
  17284. const pts = arguments[0];
  17285. this._pts = pts;
  17286. this._orientation = OrientedCoordinateArray.orientation(pts);
  17287. }
  17288. static orientation(pts) {
  17289. return CoordinateArrays.increasingDirection(pts) === 1;
  17290. }
  17291. static compareOriented(pts1, orientation1, pts2, orientation2) {
  17292. const dir1 = orientation1 ? 1 : -1;
  17293. const dir2 = orientation2 ? 1 : -1;
  17294. const limit1 = orientation1 ? pts1.length : -1;
  17295. const limit2 = orientation2 ? pts2.length : -1;
  17296. let i1 = orientation1 ? 0 : pts1.length - 1;
  17297. let i2 = orientation2 ? 0 : pts2.length - 1;
  17298. while (true) {
  17299. const compPt = pts1[i1].compareTo(pts2[i2]);
  17300. if (compPt !== 0) return compPt;
  17301. i1 += dir1;
  17302. i2 += dir2;
  17303. const done1 = i1 === limit1;
  17304. const done2 = i2 === limit2;
  17305. if (done1 && !done2) return -1;
  17306. if (!done1 && done2) return 1;
  17307. if (done1 && done2) return 0;
  17308. }
  17309. }
  17310. compareTo(o1) {
  17311. const oca = o1;
  17312. const comp = OrientedCoordinateArray.compareOriented(this._pts, this._orientation, oca._pts, oca._orientation);
  17313. return comp;
  17314. }
  17315. get interfaces_() {
  17316. return [Comparable];
  17317. }
  17318. }
  17319. class EdgeList {
  17320. constructor() {
  17321. EdgeList.constructor_.apply(this, arguments);
  17322. }
  17323. static constructor_() {
  17324. this._edges = new ArrayList();
  17325. this._ocaMap = new TreeMap();
  17326. }
  17327. print(out) {
  17328. out.print('MULTILINESTRING ( ');
  17329. for (let j = 0; j < this._edges.size(); j++) {
  17330. const e = this._edges.get(j);
  17331. if (j > 0) out.print(',');
  17332. out.print('(');
  17333. const pts = e.getCoordinates();
  17334. for (let i = 0; i < pts.length; i++) {
  17335. if (i > 0) out.print(',');
  17336. out.print(pts[i].x + ' ' + pts[i].y);
  17337. }
  17338. out.println(')');
  17339. }
  17340. out.print(') ');
  17341. }
  17342. addAll(edgeColl) {
  17343. for (let i = edgeColl.iterator(); i.hasNext();) this.add(i.next());
  17344. }
  17345. findEdgeIndex(e) {
  17346. for (let i = 0; i < this._edges.size(); i++) if (this._edges.get(i).equals(e)) return i;
  17347. return -1;
  17348. }
  17349. iterator() {
  17350. return this._edges.iterator();
  17351. }
  17352. getEdges() {
  17353. return this._edges;
  17354. }
  17355. get(i) {
  17356. return this._edges.get(i);
  17357. }
  17358. findEqualEdge(e) {
  17359. const oca = new OrientedCoordinateArray(e.getCoordinates());
  17360. const matchEdge = this._ocaMap.get(oca);
  17361. return matchEdge;
  17362. }
  17363. add(e) {
  17364. this._edges.add(e);
  17365. const oca = new OrientedCoordinateArray(e.getCoordinates());
  17366. this._ocaMap.put(oca, e);
  17367. }
  17368. }
  17369. class SegmentIntersector {
  17370. processIntersections(e0, segIndex0, e1, segIndex1) {}
  17371. isDone() {}
  17372. }
  17373. class IntersectionAdder {
  17374. constructor() {
  17375. IntersectionAdder.constructor_.apply(this, arguments);
  17376. }
  17377. static constructor_() {
  17378. this._hasIntersection = false;
  17379. this._hasProper = false;
  17380. this._hasProperInterior = false;
  17381. this._hasInterior = false;
  17382. this._properIntersectionPoint = null;
  17383. this._li = null;
  17384. this._isSelfIntersection = null;
  17385. this.numIntersections = 0;
  17386. this.numInteriorIntersections = 0;
  17387. this.numProperIntersections = 0;
  17388. this.numTests = 0;
  17389. const li = arguments[0];
  17390. this._li = li;
  17391. }
  17392. static isAdjacentSegments(i1, i2) {
  17393. return Math.abs(i1 - i2) === 1;
  17394. }
  17395. isTrivialIntersection(e0, segIndex0, e1, segIndex1) {
  17396. if (e0 === e1) if (this._li.getIntersectionNum() === 1) {
  17397. if (IntersectionAdder.isAdjacentSegments(segIndex0, segIndex1)) return true;
  17398. if (e0.isClosed()) {
  17399. const maxSegIndex = e0.size() - 1;
  17400. if (segIndex0 === 0 && segIndex1 === maxSegIndex || segIndex1 === 0 && segIndex0 === maxSegIndex) return true;
  17401. }
  17402. }
  17403. return false;
  17404. }
  17405. getProperIntersectionPoint() {
  17406. return this._properIntersectionPoint;
  17407. }
  17408. hasProperInteriorIntersection() {
  17409. return this._hasProperInterior;
  17410. }
  17411. getLineIntersector() {
  17412. return this._li;
  17413. }
  17414. hasProperIntersection() {
  17415. return this._hasProper;
  17416. }
  17417. processIntersections(e0, segIndex0, e1, segIndex1) {
  17418. if (e0 === e1 && segIndex0 === segIndex1) return null;
  17419. this.numTests++;
  17420. const p00 = e0.getCoordinates()[segIndex0];
  17421. const p01 = e0.getCoordinates()[segIndex0 + 1];
  17422. const p10 = e1.getCoordinates()[segIndex1];
  17423. const p11 = e1.getCoordinates()[segIndex1 + 1];
  17424. this._li.computeIntersection(p00, p01, p10, p11);
  17425. if (this._li.hasIntersection()) {
  17426. this.numIntersections++;
  17427. if (this._li.isInteriorIntersection()) {
  17428. this.numInteriorIntersections++;
  17429. this._hasInterior = true;
  17430. }
  17431. if (!this.isTrivialIntersection(e0, segIndex0, e1, segIndex1)) {
  17432. this._hasIntersection = true;
  17433. e0.addIntersections(this._li, segIndex0, 0);
  17434. e1.addIntersections(this._li, segIndex1, 1);
  17435. if (this._li.isProper()) {
  17436. this.numProperIntersections++;
  17437. this._hasProper = true;
  17438. this._hasProperInterior = true;
  17439. }
  17440. }
  17441. }
  17442. }
  17443. hasIntersection() {
  17444. return this._hasIntersection;
  17445. }
  17446. isDone() {
  17447. return false;
  17448. }
  17449. hasInteriorIntersection() {
  17450. return this._hasInterior;
  17451. }
  17452. get interfaces_() {
  17453. return [SegmentIntersector];
  17454. }
  17455. }
  17456. class BufferBuilder {
  17457. constructor() {
  17458. BufferBuilder.constructor_.apply(this, arguments);
  17459. }
  17460. static constructor_() {
  17461. this._bufParams = null;
  17462. this._workingPrecisionModel = null;
  17463. this._workingNoder = null;
  17464. this._geomFact = null;
  17465. this._graph = null;
  17466. this._edgeList = new EdgeList();
  17467. const bufParams = arguments[0];
  17468. this._bufParams = bufParams;
  17469. }
  17470. static depthDelta(label) {
  17471. const lLoc = label.getLocation(0, Position.LEFT);
  17472. const rLoc = label.getLocation(0, Position.RIGHT);
  17473. if (lLoc === Location.INTERIOR && rLoc === Location.EXTERIOR) return 1;else if (lLoc === Location.EXTERIOR && rLoc === Location.INTERIOR) return -1;
  17474. return 0;
  17475. }
  17476. static convertSegStrings(it) {
  17477. const fact = new GeometryFactory();
  17478. const lines = new ArrayList();
  17479. while (it.hasNext()) {
  17480. const ss = it.next();
  17481. const line = fact.createLineString(ss.getCoordinates());
  17482. lines.add(line);
  17483. }
  17484. return fact.buildGeometry(lines);
  17485. }
  17486. setWorkingPrecisionModel(pm) {
  17487. this._workingPrecisionModel = pm;
  17488. }
  17489. insertUniqueEdge(e) {
  17490. const existingEdge = this._edgeList.findEqualEdge(e);
  17491. if (existingEdge !== null) {
  17492. const existingLabel = existingEdge.getLabel();
  17493. let labelToMerge = e.getLabel();
  17494. if (!existingEdge.isPointwiseEqual(e)) {
  17495. labelToMerge = new Label(e.getLabel());
  17496. labelToMerge.flip();
  17497. }
  17498. existingLabel.merge(labelToMerge);
  17499. const mergeDelta = BufferBuilder.depthDelta(labelToMerge);
  17500. const existingDelta = existingEdge.getDepthDelta();
  17501. const newDelta = existingDelta + mergeDelta;
  17502. existingEdge.setDepthDelta(newDelta);
  17503. } else {
  17504. this._edgeList.add(e);
  17505. e.setDepthDelta(BufferBuilder.depthDelta(e.getLabel()));
  17506. }
  17507. }
  17508. buildSubgraphs(subgraphList, polyBuilder) {
  17509. const processedGraphs = new ArrayList();
  17510. for (let i = subgraphList.iterator(); i.hasNext();) {
  17511. const subgraph = i.next();
  17512. const p = subgraph.getRightmostCoordinate();
  17513. const locater = new SubgraphDepthLocater(processedGraphs);
  17514. const outsideDepth = locater.getDepth(p);
  17515. subgraph.computeDepth(outsideDepth);
  17516. subgraph.findResultEdges();
  17517. processedGraphs.add(subgraph);
  17518. polyBuilder.add(subgraph.getDirectedEdges(), subgraph.getNodes());
  17519. }
  17520. }
  17521. createSubgraphs(graph) {
  17522. const subgraphList = new ArrayList();
  17523. for (let i = graph.getNodes().iterator(); i.hasNext();) {
  17524. const node = i.next();
  17525. if (!node.isVisited()) {
  17526. const subgraph = new BufferSubgraph();
  17527. subgraph.create(node);
  17528. subgraphList.add(subgraph);
  17529. }
  17530. }
  17531. Collections.sort(subgraphList, Collections.reverseOrder());
  17532. return subgraphList;
  17533. }
  17534. createEmptyResultGeometry() {
  17535. const emptyGeom = this._geomFact.createPolygon();
  17536. return emptyGeom;
  17537. }
  17538. getNoder(precisionModel) {
  17539. if (this._workingNoder !== null) return this._workingNoder;
  17540. const noder = new MCIndexNoder();
  17541. const li = new RobustLineIntersector();
  17542. li.setPrecisionModel(precisionModel);
  17543. noder.setSegmentIntersector(new IntersectionAdder(li));
  17544. return noder;
  17545. }
  17546. buffer(g, distance) {
  17547. let precisionModel = this._workingPrecisionModel;
  17548. if (precisionModel === null) precisionModel = g.getPrecisionModel();
  17549. this._geomFact = g.getFactory();
  17550. const curveBuilder = new OffsetCurveBuilder(precisionModel, this._bufParams);
  17551. const curveSetBuilder = new OffsetCurveSetBuilder(g, distance, curveBuilder);
  17552. const bufferSegStrList = curveSetBuilder.getCurves();
  17553. if (bufferSegStrList.size() <= 0) return this.createEmptyResultGeometry();
  17554. this.computeNodedEdges(bufferSegStrList, precisionModel);
  17555. this._graph = new PlanarGraph$1(new OverlayNodeFactory());
  17556. this._graph.addEdges(this._edgeList.getEdges());
  17557. const subgraphList = this.createSubgraphs(this._graph);
  17558. const polyBuilder = new PolygonBuilder(this._geomFact);
  17559. this.buildSubgraphs(subgraphList, polyBuilder);
  17560. const resultPolyList = polyBuilder.getPolygons();
  17561. if (resultPolyList.size() <= 0) return this.createEmptyResultGeometry();
  17562. const resultGeom = this._geomFact.buildGeometry(resultPolyList);
  17563. return resultGeom;
  17564. }
  17565. computeNodedEdges(bufferSegStrList, precisionModel) {
  17566. const noder = this.getNoder(precisionModel);
  17567. noder.computeNodes(bufferSegStrList);
  17568. const nodedSegStrings = noder.getNodedSubstrings();
  17569. for (let i = nodedSegStrings.iterator(); i.hasNext();) {
  17570. const segStr = i.next();
  17571. const pts = segStr.getCoordinates();
  17572. if (pts.length === 2 && pts[0].equals2D(pts[1])) continue;
  17573. const oldLabel = segStr.getData();
  17574. const edge = new Edge$1(segStr.getCoordinates(), new Label(oldLabel));
  17575. this.insertUniqueEdge(edge);
  17576. }
  17577. }
  17578. setNoder(noder) {
  17579. this._workingNoder = noder;
  17580. }
  17581. }
  17582. class NodingValidator {
  17583. constructor() {
  17584. NodingValidator.constructor_.apply(this, arguments);
  17585. }
  17586. static constructor_() {
  17587. this._li = new RobustLineIntersector();
  17588. this._segStrings = null;
  17589. const segStrings = arguments[0];
  17590. this._segStrings = segStrings;
  17591. }
  17592. checkEndPtVertexIntersections() {
  17593. if (arguments.length === 0) {
  17594. for (let i = this._segStrings.iterator(); i.hasNext();) {
  17595. const ss = i.next();
  17596. const pts = ss.getCoordinates();
  17597. this.checkEndPtVertexIntersections(pts[0], this._segStrings);
  17598. this.checkEndPtVertexIntersections(pts[pts.length - 1], this._segStrings);
  17599. }
  17600. } else if (arguments.length === 2) {
  17601. const testPt = arguments[0],
  17602. segStrings = arguments[1];
  17603. for (let i = segStrings.iterator(); i.hasNext();) {
  17604. const ss = i.next();
  17605. const pts = ss.getCoordinates();
  17606. for (let j = 1; j < pts.length - 1; j++) if (pts[j].equals(testPt)) throw new RuntimeException('found endpt/interior pt intersection at index ' + j + ' :pt ' + testPt);
  17607. }
  17608. }
  17609. }
  17610. checkInteriorIntersections() {
  17611. if (arguments.length === 0) {
  17612. for (let i = this._segStrings.iterator(); i.hasNext();) {
  17613. const ss0 = i.next();
  17614. for (let j = this._segStrings.iterator(); j.hasNext();) {
  17615. const ss1 = j.next();
  17616. this.checkInteriorIntersections(ss0, ss1);
  17617. }
  17618. }
  17619. } else if (arguments.length === 2) {
  17620. const ss0 = arguments[0],
  17621. ss1 = arguments[1];
  17622. const pts0 = ss0.getCoordinates();
  17623. const pts1 = ss1.getCoordinates();
  17624. for (let i0 = 0; i0 < pts0.length - 1; i0++) for (let i1 = 0; i1 < pts1.length - 1; i1++) this.checkInteriorIntersections(ss0, i0, ss1, i1);
  17625. } else if (arguments.length === 4) {
  17626. const e0 = arguments[0],
  17627. segIndex0 = arguments[1],
  17628. e1 = arguments[2],
  17629. segIndex1 = arguments[3];
  17630. if (e0 === e1 && segIndex0 === segIndex1) return null;
  17631. const p00 = e0.getCoordinates()[segIndex0];
  17632. const p01 = e0.getCoordinates()[segIndex0 + 1];
  17633. const p10 = e1.getCoordinates()[segIndex1];
  17634. const p11 = e1.getCoordinates()[segIndex1 + 1];
  17635. this._li.computeIntersection(p00, p01, p10, p11);
  17636. if (this._li.hasIntersection()) if (this._li.isProper() || this.hasInteriorIntersection(this._li, p00, p01) || this.hasInteriorIntersection(this._li, p10, p11)) throw new RuntimeException('found non-noded intersection at ' + p00 + '-' + p01 + ' and ' + p10 + '-' + p11);
  17637. }
  17638. }
  17639. checkValid() {
  17640. this.checkEndPtVertexIntersections();
  17641. this.checkInteriorIntersections();
  17642. this.checkCollapses();
  17643. }
  17644. checkCollapses() {
  17645. if (arguments.length === 0) {
  17646. for (let i = this._segStrings.iterator(); i.hasNext();) {
  17647. const ss = i.next();
  17648. this.checkCollapses(ss);
  17649. }
  17650. } else if (arguments.length === 1) {
  17651. const ss = arguments[0];
  17652. const pts = ss.getCoordinates();
  17653. for (let i = 0; i < pts.length - 2; i++) this.checkCollapse(pts[i], pts[i + 1], pts[i + 2]);
  17654. }
  17655. }
  17656. hasInteriorIntersection(li, p0, p1) {
  17657. for (let i = 0; i < li.getIntersectionNum(); i++) {
  17658. const intPt = li.getIntersection(i);
  17659. if (!(intPt.equals(p0) || intPt.equals(p1))) return true;
  17660. }
  17661. return false;
  17662. }
  17663. checkCollapse(p0, p1, p2) {
  17664. if (p0.equals(p2)) throw new RuntimeException('found non-noded collapse at ' + NodingValidator.fact.createLineString([p0, p1, p2]));
  17665. }
  17666. }
  17667. NodingValidator.fact = new GeometryFactory();
  17668. class HotPixel {
  17669. constructor() {
  17670. HotPixel.constructor_.apply(this, arguments);
  17671. }
  17672. static constructor_() {
  17673. this._li = null;
  17674. this._pt = null;
  17675. this._originalPt = null;
  17676. this._ptScaled = null;
  17677. this._p0Scaled = null;
  17678. this._p1Scaled = null;
  17679. this._scaleFactor = null;
  17680. this._minx = null;
  17681. this._maxx = null;
  17682. this._miny = null;
  17683. this._maxy = null;
  17684. this._corner = new Array(4).fill(null);
  17685. this._safeEnv = null;
  17686. const pt = arguments[0],
  17687. scaleFactor = arguments[1],
  17688. li = arguments[2];
  17689. this._originalPt = pt;
  17690. this._pt = pt;
  17691. this._scaleFactor = scaleFactor;
  17692. this._li = li;
  17693. if (scaleFactor <= 0) throw new IllegalArgumentException('Scale factor must be non-zero');
  17694. if (scaleFactor !== 1.0) {
  17695. this._pt = new Coordinate(this.scale(pt.x), this.scale(pt.y));
  17696. this._p0Scaled = new Coordinate();
  17697. this._p1Scaled = new Coordinate();
  17698. }
  17699. this.initCorners(this._pt);
  17700. }
  17701. intersectsScaled(p0, p1) {
  17702. const segMinx = Math.min(p0.x, p1.x);
  17703. const segMaxx = Math.max(p0.x, p1.x);
  17704. const segMiny = Math.min(p0.y, p1.y);
  17705. const segMaxy = Math.max(p0.y, p1.y);
  17706. const isOutsidePixelEnv = this._maxx < segMinx || this._minx > segMaxx || this._maxy < segMiny || this._miny > segMaxy;
  17707. if (isOutsidePixelEnv) return false;
  17708. const intersects = this.intersectsToleranceSquare(p0, p1);
  17709. Assert.isTrue(!(isOutsidePixelEnv && intersects), 'Found bad envelope test');
  17710. return intersects;
  17711. }
  17712. initCorners(pt) {
  17713. const tolerance = 0.5;
  17714. this._minx = pt.x - tolerance;
  17715. this._maxx = pt.x + tolerance;
  17716. this._miny = pt.y - tolerance;
  17717. this._maxy = pt.y + tolerance;
  17718. this._corner[0] = new Coordinate(this._maxx, this._maxy);
  17719. this._corner[1] = new Coordinate(this._minx, this._maxy);
  17720. this._corner[2] = new Coordinate(this._minx, this._miny);
  17721. this._corner[3] = new Coordinate(this._maxx, this._miny);
  17722. }
  17723. intersects(p0, p1) {
  17724. if (this._scaleFactor === 1.0) return this.intersectsScaled(p0, p1);
  17725. this.copyScaled(p0, this._p0Scaled);
  17726. this.copyScaled(p1, this._p1Scaled);
  17727. return this.intersectsScaled(this._p0Scaled, this._p1Scaled);
  17728. }
  17729. scale(val) {
  17730. return Math.round(val * this._scaleFactor);
  17731. }
  17732. getCoordinate() {
  17733. return this._originalPt;
  17734. }
  17735. copyScaled(p, pScaled) {
  17736. pScaled.x = this.scale(p.x);
  17737. pScaled.y = this.scale(p.y);
  17738. }
  17739. getSafeEnvelope() {
  17740. if (this._safeEnv === null) {
  17741. const safeTolerance = HotPixel.SAFE_ENV_EXPANSION_FACTOR / this._scaleFactor;
  17742. this._safeEnv = new Envelope(this._originalPt.x - safeTolerance, this._originalPt.x + safeTolerance, this._originalPt.y - safeTolerance, this._originalPt.y + safeTolerance);
  17743. }
  17744. return this._safeEnv;
  17745. }
  17746. intersectsPixelClosure(p0, p1) {
  17747. this._li.computeIntersection(p0, p1, this._corner[0], this._corner[1]);
  17748. if (this._li.hasIntersection()) return true;
  17749. this._li.computeIntersection(p0, p1, this._corner[1], this._corner[2]);
  17750. if (this._li.hasIntersection()) return true;
  17751. this._li.computeIntersection(p0, p1, this._corner[2], this._corner[3]);
  17752. if (this._li.hasIntersection()) return true;
  17753. this._li.computeIntersection(p0, p1, this._corner[3], this._corner[0]);
  17754. if (this._li.hasIntersection()) return true;
  17755. return false;
  17756. }
  17757. intersectsToleranceSquare(p0, p1) {
  17758. let intersectsLeft = false;
  17759. let intersectsBottom = false;
  17760. this._li.computeIntersection(p0, p1, this._corner[0], this._corner[1]);
  17761. if (this._li.isProper()) return true;
  17762. this._li.computeIntersection(p0, p1, this._corner[1], this._corner[2]);
  17763. if (this._li.isProper()) return true;
  17764. if (this._li.hasIntersection()) intersectsLeft = true;
  17765. this._li.computeIntersection(p0, p1, this._corner[2], this._corner[3]);
  17766. if (this._li.isProper()) return true;
  17767. if (this._li.hasIntersection()) intersectsBottom = true;
  17768. this._li.computeIntersection(p0, p1, this._corner[3], this._corner[0]);
  17769. if (this._li.isProper()) return true;
  17770. if (intersectsLeft && intersectsBottom) return true;
  17771. if (p0.equals(this._pt)) return true;
  17772. if (p1.equals(this._pt)) return true;
  17773. return false;
  17774. }
  17775. addSnappedNode(segStr, segIndex) {
  17776. const p0 = segStr.getCoordinate(segIndex);
  17777. const p1 = segStr.getCoordinate(segIndex + 1);
  17778. if (this.intersects(p0, p1)) {
  17779. segStr.addIntersection(this.getCoordinate(), segIndex);
  17780. return true;
  17781. }
  17782. return false;
  17783. }
  17784. }
  17785. HotPixel.SAFE_ENV_EXPANSION_FACTOR = 0.75;
  17786. class MonotoneChainSelectAction {
  17787. constructor() {
  17788. MonotoneChainSelectAction.constructor_.apply(this, arguments);
  17789. }
  17790. static constructor_() {
  17791. this.selectedSegment = new LineSegment();
  17792. }
  17793. select() {
  17794. if (arguments.length === 1) ; else if (arguments.length === 2) {
  17795. const mc = arguments[0],
  17796. startIndex = arguments[1];
  17797. mc.getLineSegment(startIndex, this.selectedSegment);
  17798. this.select(this.selectedSegment);
  17799. }
  17800. }
  17801. }
  17802. class MCIndexPointSnapper {
  17803. constructor() {
  17804. MCIndexPointSnapper.constructor_.apply(this, arguments);
  17805. }
  17806. static constructor_() {
  17807. this._index = null;
  17808. const index = arguments[0];
  17809. this._index = index;
  17810. }
  17811. snap() {
  17812. if (arguments.length === 1) {
  17813. const hotPixel = arguments[0];
  17814. return this.snap(hotPixel, null, -1);
  17815. } else if (arguments.length === 3) {
  17816. const hotPixel = arguments[0],
  17817. parentEdge = arguments[1],
  17818. hotPixelVertexIndex = arguments[2];
  17819. const pixelEnv = hotPixel.getSafeEnvelope();
  17820. const hotPixelSnapAction = new HotPixelSnapAction(hotPixel, parentEdge, hotPixelVertexIndex);
  17821. this._index.query(pixelEnv, new class {
  17822. get interfaces_() {
  17823. return [ItemVisitor];
  17824. }
  17825. visitItem(item) {
  17826. const testChain = item;
  17827. testChain.select(pixelEnv, hotPixelSnapAction);
  17828. }
  17829. }());
  17830. return hotPixelSnapAction.isNodeAdded();
  17831. }
  17832. }
  17833. }
  17834. class HotPixelSnapAction extends MonotoneChainSelectAction {
  17835. constructor() {
  17836. super();
  17837. HotPixelSnapAction.constructor_.apply(this, arguments);
  17838. }
  17839. static constructor_() {
  17840. this._hotPixel = null;
  17841. this._parentEdge = null;
  17842. this._hotPixelVertexIndex = null;
  17843. this._isNodeAdded = false;
  17844. const hotPixel = arguments[0],
  17845. parentEdge = arguments[1],
  17846. hotPixelVertexIndex = arguments[2];
  17847. this._hotPixel = hotPixel;
  17848. this._parentEdge = parentEdge;
  17849. this._hotPixelVertexIndex = hotPixelVertexIndex;
  17850. }
  17851. isNodeAdded() {
  17852. return this._isNodeAdded;
  17853. }
  17854. select() {
  17855. if (arguments.length === 2 && Number.isInteger(arguments[1]) && arguments[0] instanceof MonotoneChain) {
  17856. const mc = arguments[0],
  17857. startIndex = arguments[1];
  17858. const ss = mc.getContext();
  17859. if (this._parentEdge === ss) if (startIndex === this._hotPixelVertexIndex || startIndex + 1 === this._hotPixelVertexIndex) return null;
  17860. this._isNodeAdded |= this._hotPixel.addSnappedNode(ss, startIndex);
  17861. } else {
  17862. return super.select.apply(this, arguments);
  17863. }
  17864. }
  17865. }
  17866. MCIndexPointSnapper.HotPixelSnapAction = HotPixelSnapAction;
  17867. class InteriorIntersectionFinderAdder {
  17868. constructor() {
  17869. InteriorIntersectionFinderAdder.constructor_.apply(this, arguments);
  17870. }
  17871. static constructor_() {
  17872. this._li = null;
  17873. this._interiorIntersections = null;
  17874. const li = arguments[0];
  17875. this._li = li;
  17876. this._interiorIntersections = new ArrayList();
  17877. }
  17878. processIntersections(e0, segIndex0, e1, segIndex1) {
  17879. if (e0 === e1 && segIndex0 === segIndex1) return null;
  17880. const p00 = e0.getCoordinates()[segIndex0];
  17881. const p01 = e0.getCoordinates()[segIndex0 + 1];
  17882. const p10 = e1.getCoordinates()[segIndex1];
  17883. const p11 = e1.getCoordinates()[segIndex1 + 1];
  17884. this._li.computeIntersection(p00, p01, p10, p11);
  17885. if (this._li.hasIntersection()) if (this._li.isInteriorIntersection()) {
  17886. for (let intIndex = 0; intIndex < this._li.getIntersectionNum(); intIndex++) this._interiorIntersections.add(this._li.getIntersection(intIndex));
  17887. e0.addIntersections(this._li, segIndex0, 0);
  17888. e1.addIntersections(this._li, segIndex1, 1);
  17889. }
  17890. }
  17891. isDone() {
  17892. return false;
  17893. }
  17894. getInteriorIntersections() {
  17895. return this._interiorIntersections;
  17896. }
  17897. get interfaces_() {
  17898. return [SegmentIntersector];
  17899. }
  17900. }
  17901. class MCIndexSnapRounder {
  17902. constructor() {
  17903. MCIndexSnapRounder.constructor_.apply(this, arguments);
  17904. }
  17905. static constructor_() {
  17906. this._pm = null;
  17907. this._li = null;
  17908. this._scaleFactor = null;
  17909. this._noder = null;
  17910. this._pointSnapper = null;
  17911. this._nodedSegStrings = null;
  17912. const pm = arguments[0];
  17913. this._pm = pm;
  17914. this._li = new RobustLineIntersector();
  17915. this._li.setPrecisionModel(pm);
  17916. this._scaleFactor = pm.getScale();
  17917. }
  17918. checkCorrectness(inputSegmentStrings) {
  17919. const resultSegStrings = NodedSegmentString.getNodedSubstrings(inputSegmentStrings);
  17920. const nv = new NodingValidator(resultSegStrings);
  17921. try {
  17922. nv.checkValid();
  17923. } catch (ex) {
  17924. if (ex instanceof Exception) ex.printStackTrace();else throw ex;
  17925. } finally {}
  17926. }
  17927. getNodedSubstrings() {
  17928. return NodedSegmentString.getNodedSubstrings(this._nodedSegStrings);
  17929. }
  17930. snapRound(segStrings, li) {
  17931. const intersections = this.findInteriorIntersections(segStrings, li);
  17932. this.computeIntersectionSnaps(intersections);
  17933. this.computeVertexSnaps(segStrings);
  17934. }
  17935. findInteriorIntersections(segStrings, li) {
  17936. const intFinderAdder = new InteriorIntersectionFinderAdder(li);
  17937. this._noder.setSegmentIntersector(intFinderAdder);
  17938. this._noder.computeNodes(segStrings);
  17939. return intFinderAdder.getInteriorIntersections();
  17940. }
  17941. computeVertexSnaps() {
  17942. if (hasInterface(arguments[0], Collection)) {
  17943. const edges = arguments[0];
  17944. for (let i0 = edges.iterator(); i0.hasNext();) {
  17945. const edge0 = i0.next();
  17946. this.computeVertexSnaps(edge0);
  17947. }
  17948. } else if (arguments[0] instanceof NodedSegmentString) {
  17949. const e = arguments[0];
  17950. const pts0 = e.getCoordinates();
  17951. for (let i = 0; i < pts0.length; i++) {
  17952. const hotPixel = new HotPixel(pts0[i], this._scaleFactor, this._li);
  17953. const isNodeAdded = this._pointSnapper.snap(hotPixel, e, i);
  17954. if (isNodeAdded) e.addIntersection(pts0[i], i);
  17955. }
  17956. }
  17957. }
  17958. computeNodes(inputSegmentStrings) {
  17959. this._nodedSegStrings = inputSegmentStrings;
  17960. this._noder = new MCIndexNoder();
  17961. this._pointSnapper = new MCIndexPointSnapper(this._noder.getIndex());
  17962. this.snapRound(inputSegmentStrings, this._li);
  17963. }
  17964. computeIntersectionSnaps(snapPts) {
  17965. for (let it = snapPts.iterator(); it.hasNext();) {
  17966. const snapPt = it.next();
  17967. const hotPixel = new HotPixel(snapPt, this._scaleFactor, this._li);
  17968. this._pointSnapper.snap(hotPixel);
  17969. }
  17970. }
  17971. get interfaces_() {
  17972. return [Noder];
  17973. }
  17974. }
  17975. class BufferOp {
  17976. constructor() {
  17977. BufferOp.constructor_.apply(this, arguments);
  17978. }
  17979. static constructor_() {
  17980. this._argGeom = null;
  17981. this._distance = null;
  17982. this._bufParams = new BufferParameters();
  17983. this._resultGeometry = null;
  17984. this._saveException = null;
  17985. if (arguments.length === 1) {
  17986. const g = arguments[0];
  17987. this._argGeom = g;
  17988. } else if (arguments.length === 2) {
  17989. const g = arguments[0],
  17990. bufParams = arguments[1];
  17991. this._argGeom = g;
  17992. this._bufParams = bufParams;
  17993. }
  17994. }
  17995. static bufferOp() {
  17996. if (arguments.length === 2) {
  17997. const g = arguments[0],
  17998. distance = arguments[1];
  17999. const gBuf = new BufferOp(g);
  18000. const geomBuf = gBuf.getResultGeometry(distance);
  18001. return geomBuf;
  18002. } else if (arguments.length === 3) {
  18003. if (Number.isInteger(arguments[2]) && arguments[0] instanceof Geometry && typeof arguments[1] === 'number') {
  18004. const g = arguments[0],
  18005. distance = arguments[1],
  18006. quadrantSegments = arguments[2];
  18007. const bufOp = new BufferOp(g);
  18008. bufOp.setQuadrantSegments(quadrantSegments);
  18009. const geomBuf = bufOp.getResultGeometry(distance);
  18010. return geomBuf;
  18011. } else if (arguments[2] instanceof BufferParameters && arguments[0] instanceof Geometry && typeof arguments[1] === 'number') {
  18012. const g = arguments[0],
  18013. distance = arguments[1],
  18014. params = arguments[2];
  18015. const bufOp = new BufferOp(g, params);
  18016. const geomBuf = bufOp.getResultGeometry(distance);
  18017. return geomBuf;
  18018. }
  18019. } else if (arguments.length === 4) {
  18020. const g = arguments[0],
  18021. distance = arguments[1],
  18022. quadrantSegments = arguments[2],
  18023. endCapStyle = arguments[3];
  18024. const bufOp = new BufferOp(g);
  18025. bufOp.setQuadrantSegments(quadrantSegments);
  18026. bufOp.setEndCapStyle(endCapStyle);
  18027. const geomBuf = bufOp.getResultGeometry(distance);
  18028. return geomBuf;
  18029. }
  18030. }
  18031. static precisionScaleFactor(g, distance, maxPrecisionDigits) {
  18032. const env = g.getEnvelopeInternal();
  18033. const envMax = MathUtil.max(Math.abs(env.getMaxX()), Math.abs(env.getMaxY()), Math.abs(env.getMinX()), Math.abs(env.getMinY()));
  18034. const expandByDistance = distance > 0.0 ? distance : 0.0;
  18035. const bufEnvMax = envMax + 2 * expandByDistance;
  18036. const bufEnvPrecisionDigits = Math.trunc(Math.log(bufEnvMax) / Math.log(10) + 1.0);
  18037. const minUnitLog10 = maxPrecisionDigits - bufEnvPrecisionDigits;
  18038. const scaleFactor = Math.pow(10.0, minUnitLog10);
  18039. return scaleFactor;
  18040. }
  18041. bufferFixedPrecision(fixedPM) {
  18042. const noder = new ScaledNoder(new MCIndexSnapRounder(new PrecisionModel(1.0)), fixedPM.getScale());
  18043. const bufBuilder = new BufferBuilder(this._bufParams);
  18044. bufBuilder.setWorkingPrecisionModel(fixedPM);
  18045. bufBuilder.setNoder(noder);
  18046. this._resultGeometry = bufBuilder.buffer(this._argGeom, this._distance);
  18047. }
  18048. bufferReducedPrecision() {
  18049. if (arguments.length === 0) {
  18050. for (let precDigits = BufferOp.MAX_PRECISION_DIGITS; precDigits >= 0; precDigits--) {
  18051. try {
  18052. this.bufferReducedPrecision(precDigits);
  18053. } catch (ex) {
  18054. if (ex instanceof TopologyException) this._saveException = ex;else throw ex;
  18055. } finally {}
  18056. if (this._resultGeometry !== null) return null;
  18057. }
  18058. throw this._saveException;
  18059. } else if (arguments.length === 1) {
  18060. const precisionDigits = arguments[0];
  18061. const sizeBasedScaleFactor = BufferOp.precisionScaleFactor(this._argGeom, this._distance, precisionDigits);
  18062. const fixedPM = new PrecisionModel(sizeBasedScaleFactor);
  18063. this.bufferFixedPrecision(fixedPM);
  18064. }
  18065. }
  18066. computeGeometry() {
  18067. this.bufferOriginalPrecision();
  18068. if (this._resultGeometry !== null) return null;
  18069. const argPM = this._argGeom.getFactory().getPrecisionModel();
  18070. if (argPM.getType() === PrecisionModel.FIXED) this.bufferFixedPrecision(argPM);else this.bufferReducedPrecision();
  18071. }
  18072. setQuadrantSegments(quadrantSegments) {
  18073. this._bufParams.setQuadrantSegments(quadrantSegments);
  18074. }
  18075. bufferOriginalPrecision() {
  18076. try {
  18077. const bufBuilder = new BufferBuilder(this._bufParams);
  18078. this._resultGeometry = bufBuilder.buffer(this._argGeom, this._distance);
  18079. } catch (ex) {
  18080. if (ex instanceof RuntimeException) this._saveException = ex;else throw ex;
  18081. } finally {}
  18082. }
  18083. getResultGeometry(distance) {
  18084. this._distance = distance;
  18085. this.computeGeometry();
  18086. return this._resultGeometry;
  18087. }
  18088. setEndCapStyle(endCapStyle) {
  18089. this._bufParams.setEndCapStyle(endCapStyle);
  18090. }
  18091. }
  18092. BufferOp.CAP_ROUND = BufferParameters.CAP_ROUND;
  18093. BufferOp.CAP_BUTT = BufferParameters.CAP_FLAT;
  18094. BufferOp.CAP_FLAT = BufferParameters.CAP_FLAT;
  18095. BufferOp.CAP_SQUARE = BufferParameters.CAP_SQUARE;
  18096. BufferOp.MAX_PRECISION_DIGITS = 12;
  18097. var buffer = /*#__PURE__*/Object.freeze({
  18098. __proto__: null,
  18099. BufferOp: BufferOp,
  18100. BufferParameters: BufferParameters
  18101. });
  18102. class GeometryLocation {
  18103. constructor() {
  18104. GeometryLocation.constructor_.apply(this, arguments);
  18105. }
  18106. static constructor_() {
  18107. this._component = null;
  18108. this._segIndex = null;
  18109. this._pt = null;
  18110. if (arguments.length === 2) {
  18111. const component = arguments[0],
  18112. pt = arguments[1];
  18113. GeometryLocation.constructor_.call(this, component, GeometryLocation.INSIDE_AREA, pt);
  18114. } else if (arguments.length === 3) {
  18115. const component = arguments[0],
  18116. segIndex = arguments[1],
  18117. pt = arguments[2];
  18118. this._component = component;
  18119. this._segIndex = segIndex;
  18120. this._pt = pt;
  18121. }
  18122. }
  18123. getSegmentIndex() {
  18124. return this._segIndex;
  18125. }
  18126. getCoordinate() {
  18127. return this._pt;
  18128. }
  18129. isInsideArea() {
  18130. return this._segIndex === GeometryLocation.INSIDE_AREA;
  18131. }
  18132. toString() {
  18133. return this._component.getGeometryType() + '[' + this._segIndex + ']' + '-' + WKTWriter.toPoint(this._pt);
  18134. }
  18135. getGeometryComponent() {
  18136. return this._component;
  18137. }
  18138. }
  18139. GeometryLocation.INSIDE_AREA = -1;
  18140. class ConnectedElementLocationFilter {
  18141. constructor() {
  18142. ConnectedElementLocationFilter.constructor_.apply(this, arguments);
  18143. }
  18144. static constructor_() {
  18145. this._locations = null;
  18146. const locations = arguments[0];
  18147. this._locations = locations;
  18148. }
  18149. static getLocations(geom) {
  18150. const locations = new ArrayList();
  18151. geom.apply(new ConnectedElementLocationFilter(locations));
  18152. return locations;
  18153. }
  18154. filter(geom) {
  18155. if (geom.isEmpty()) return null;
  18156. if (geom instanceof Point || geom instanceof LineString || geom instanceof Polygon) this._locations.add(new GeometryLocation(geom, 0, geom.getCoordinate()));
  18157. }
  18158. get interfaces_() {
  18159. return [GeometryFilter];
  18160. }
  18161. }
  18162. class DistanceOp {
  18163. constructor() {
  18164. DistanceOp.constructor_.apply(this, arguments);
  18165. }
  18166. static constructor_() {
  18167. this._geom = null;
  18168. this._terminateDistance = 0.0;
  18169. this._ptLocator = new PointLocator();
  18170. this._minDistanceLocation = null;
  18171. this._minDistance = Double.MAX_VALUE;
  18172. if (arguments.length === 2) {
  18173. const g0 = arguments[0],
  18174. g1 = arguments[1];
  18175. DistanceOp.constructor_.call(this, g0, g1, 0.0);
  18176. } else if (arguments.length === 3) {
  18177. const g0 = arguments[0],
  18178. g1 = arguments[1],
  18179. terminateDistance = arguments[2];
  18180. this._geom = new Array(2).fill(null);
  18181. this._geom[0] = g0;
  18182. this._geom[1] = g1;
  18183. this._terminateDistance = terminateDistance;
  18184. }
  18185. }
  18186. static distance(g0, g1) {
  18187. const distOp = new DistanceOp(g0, g1);
  18188. return distOp.distance();
  18189. }
  18190. static isWithinDistance(g0, g1, distance) {
  18191. const envDist = g0.getEnvelopeInternal().distance(g1.getEnvelopeInternal());
  18192. if (envDist > distance) return false;
  18193. const distOp = new DistanceOp(g0, g1, distance);
  18194. return distOp.distance() <= distance;
  18195. }
  18196. static nearestPoints(g0, g1) {
  18197. const distOp = new DistanceOp(g0, g1);
  18198. return distOp.nearestPoints();
  18199. }
  18200. computeContainmentDistance() {
  18201. if (arguments.length === 0) {
  18202. const locPtPoly = new Array(2).fill(null);
  18203. this.computeContainmentDistance(0, locPtPoly);
  18204. if (this._minDistance <= this._terminateDistance) return null;
  18205. this.computeContainmentDistance(1, locPtPoly);
  18206. } else if (arguments.length === 2) {
  18207. const polyGeomIndex = arguments[0],
  18208. locPtPoly = arguments[1];
  18209. const polyGeom = this._geom[polyGeomIndex];
  18210. if (polyGeom.getDimension() < 2) return null;
  18211. const locationsIndex = 1 - polyGeomIndex;
  18212. const polys = PolygonExtracter.getPolygons(polyGeom);
  18213. if (polys.size() > 0) {
  18214. const insideLocs = ConnectedElementLocationFilter.getLocations(this._geom[locationsIndex]);
  18215. this.computeContainmentDistance(insideLocs, polys, locPtPoly);
  18216. if (this._minDistance <= this._terminateDistance) {
  18217. this._minDistanceLocation[locationsIndex] = locPtPoly[0];
  18218. this._minDistanceLocation[polyGeomIndex] = locPtPoly[1];
  18219. return null;
  18220. }
  18221. }
  18222. } else if (arguments.length === 3) {
  18223. if (arguments[2] instanceof Array && hasInterface(arguments[0], List) && hasInterface(arguments[1], List)) {
  18224. const locs = arguments[0],
  18225. polys = arguments[1],
  18226. locPtPoly = arguments[2];
  18227. for (let i = 0; i < locs.size(); i++) {
  18228. const loc = locs.get(i);
  18229. for (let j = 0; j < polys.size(); j++) {
  18230. this.computeContainmentDistance(loc, polys.get(j), locPtPoly);
  18231. if (this._minDistance <= this._terminateDistance) return null;
  18232. }
  18233. }
  18234. } else if (arguments[2] instanceof Array && arguments[0] instanceof GeometryLocation && arguments[1] instanceof Polygon) {
  18235. const ptLoc = arguments[0],
  18236. poly = arguments[1],
  18237. locPtPoly = arguments[2];
  18238. const pt = ptLoc.getCoordinate();
  18239. if (Location.EXTERIOR !== this._ptLocator.locate(pt, poly)) {
  18240. this._minDistance = 0.0;
  18241. locPtPoly[0] = ptLoc;
  18242. locPtPoly[1] = new GeometryLocation(poly, pt);
  18243. return null;
  18244. }
  18245. }
  18246. }
  18247. }
  18248. computeMinDistanceLinesPoints(lines, points, locGeom) {
  18249. for (let i = 0; i < lines.size(); i++) {
  18250. const line = lines.get(i);
  18251. for (let j = 0; j < points.size(); j++) {
  18252. const pt = points.get(j);
  18253. this.computeMinDistance(line, pt, locGeom);
  18254. if (this._minDistance <= this._terminateDistance) return null;
  18255. }
  18256. }
  18257. }
  18258. computeFacetDistance() {
  18259. const locGeom = new Array(2).fill(null);
  18260. const lines0 = LinearComponentExtracter.getLines(this._geom[0]);
  18261. const lines1 = LinearComponentExtracter.getLines(this._geom[1]);
  18262. const pts0 = PointExtracter.getPoints(this._geom[0]);
  18263. const pts1 = PointExtracter.getPoints(this._geom[1]);
  18264. this.computeMinDistanceLines(lines0, lines1, locGeom);
  18265. this.updateMinDistance(locGeom, false);
  18266. if (this._minDistance <= this._terminateDistance) return null;
  18267. locGeom[0] = null;
  18268. locGeom[1] = null;
  18269. this.computeMinDistanceLinesPoints(lines0, pts1, locGeom);
  18270. this.updateMinDistance(locGeom, false);
  18271. if (this._minDistance <= this._terminateDistance) return null;
  18272. locGeom[0] = null;
  18273. locGeom[1] = null;
  18274. this.computeMinDistanceLinesPoints(lines1, pts0, locGeom);
  18275. this.updateMinDistance(locGeom, true);
  18276. if (this._minDistance <= this._terminateDistance) return null;
  18277. locGeom[0] = null;
  18278. locGeom[1] = null;
  18279. this.computeMinDistancePoints(pts0, pts1, locGeom);
  18280. this.updateMinDistance(locGeom, false);
  18281. }
  18282. nearestLocations() {
  18283. this.computeMinDistance();
  18284. return this._minDistanceLocation;
  18285. }
  18286. updateMinDistance(locGeom, flip) {
  18287. if (locGeom[0] === null) return null;
  18288. if (flip) {
  18289. this._minDistanceLocation[0] = locGeom[1];
  18290. this._minDistanceLocation[1] = locGeom[0];
  18291. } else {
  18292. this._minDistanceLocation[0] = locGeom[0];
  18293. this._minDistanceLocation[1] = locGeom[1];
  18294. }
  18295. }
  18296. nearestPoints() {
  18297. this.computeMinDistance();
  18298. const nearestPts = [this._minDistanceLocation[0].getCoordinate(), this._minDistanceLocation[1].getCoordinate()];
  18299. return nearestPts;
  18300. }
  18301. computeMinDistance() {
  18302. if (arguments.length === 0) {
  18303. if (this._minDistanceLocation !== null) return null;
  18304. this._minDistanceLocation = new Array(2).fill(null);
  18305. this.computeContainmentDistance();
  18306. if (this._minDistance <= this._terminateDistance) return null;
  18307. this.computeFacetDistance();
  18308. } else if (arguments.length === 3) {
  18309. if (arguments[2] instanceof Array && arguments[0] instanceof LineString && arguments[1] instanceof Point) {
  18310. const line = arguments[0],
  18311. pt = arguments[1],
  18312. locGeom = arguments[2];
  18313. if (line.getEnvelopeInternal().distance(pt.getEnvelopeInternal()) > this._minDistance) return null;
  18314. const coord0 = line.getCoordinates();
  18315. const coord = pt.getCoordinate();
  18316. for (let i = 0; i < coord0.length - 1; i++) {
  18317. const dist = Distance.pointToSegment(coord, coord0[i], coord0[i + 1]);
  18318. if (dist < this._minDistance) {
  18319. this._minDistance = dist;
  18320. const seg = new LineSegment(coord0[i], coord0[i + 1]);
  18321. const segClosestPoint = seg.closestPoint(coord);
  18322. locGeom[0] = new GeometryLocation(line, i, segClosestPoint);
  18323. locGeom[1] = new GeometryLocation(pt, 0, coord);
  18324. }
  18325. if (this._minDistance <= this._terminateDistance) return null;
  18326. }
  18327. } else if (arguments[2] instanceof Array && arguments[0] instanceof LineString && arguments[1] instanceof LineString) {
  18328. const line0 = arguments[0],
  18329. line1 = arguments[1],
  18330. locGeom = arguments[2];
  18331. if (line0.getEnvelopeInternal().distance(line1.getEnvelopeInternal()) > this._minDistance) return null;
  18332. const coord0 = line0.getCoordinates();
  18333. const coord1 = line1.getCoordinates();
  18334. for (let i = 0; i < coord0.length - 1; i++) {
  18335. const segEnv0 = new Envelope(coord0[i], coord0[i + 1]);
  18336. if (segEnv0.distance(line1.getEnvelopeInternal()) > this._minDistance) continue;
  18337. for (let j = 0; j < coord1.length - 1; j++) {
  18338. const segEnv1 = new Envelope(coord1[j], coord1[j + 1]);
  18339. if (segEnv0.distance(segEnv1) > this._minDistance) continue;
  18340. const dist = Distance.segmentToSegment(coord0[i], coord0[i + 1], coord1[j], coord1[j + 1]);
  18341. if (dist < this._minDistance) {
  18342. this._minDistance = dist;
  18343. const seg0 = new LineSegment(coord0[i], coord0[i + 1]);
  18344. const seg1 = new LineSegment(coord1[j], coord1[j + 1]);
  18345. const closestPt = seg0.closestPoints(seg1);
  18346. locGeom[0] = new GeometryLocation(line0, i, closestPt[0]);
  18347. locGeom[1] = new GeometryLocation(line1, j, closestPt[1]);
  18348. }
  18349. if (this._minDistance <= this._terminateDistance) return null;
  18350. }
  18351. }
  18352. }
  18353. }
  18354. }
  18355. computeMinDistancePoints(points0, points1, locGeom) {
  18356. for (let i = 0; i < points0.size(); i++) {
  18357. const pt0 = points0.get(i);
  18358. for (let j = 0; j < points1.size(); j++) {
  18359. const pt1 = points1.get(j);
  18360. const dist = pt0.getCoordinate().distance(pt1.getCoordinate());
  18361. if (dist < this._minDistance) {
  18362. this._minDistance = dist;
  18363. locGeom[0] = new GeometryLocation(pt0, 0, pt0.getCoordinate());
  18364. locGeom[1] = new GeometryLocation(pt1, 0, pt1.getCoordinate());
  18365. }
  18366. if (this._minDistance <= this._terminateDistance) return null;
  18367. }
  18368. }
  18369. }
  18370. distance() {
  18371. if (this._geom[0] === null || this._geom[1] === null) throw new IllegalArgumentException('null geometries are not supported');
  18372. if (this._geom[0].isEmpty() || this._geom[1].isEmpty()) return 0.0;
  18373. this.computeMinDistance();
  18374. return this._minDistance;
  18375. }
  18376. computeMinDistanceLines(lines0, lines1, locGeom) {
  18377. for (let i = 0; i < lines0.size(); i++) {
  18378. const line0 = lines0.get(i);
  18379. for (let j = 0; j < lines1.size(); j++) {
  18380. const line1 = lines1.get(j);
  18381. this.computeMinDistance(line0, line1, locGeom);
  18382. if (this._minDistance <= this._terminateDistance) return null;
  18383. }
  18384. }
  18385. }
  18386. }
  18387. var distance = /*#__PURE__*/Object.freeze({
  18388. __proto__: null,
  18389. DistanceOp: DistanceOp
  18390. });
  18391. class EdgeString {
  18392. constructor() {
  18393. EdgeString.constructor_.apply(this, arguments);
  18394. }
  18395. static constructor_() {
  18396. this._factory = null;
  18397. this._directedEdges = new ArrayList();
  18398. this._coordinates = null;
  18399. const factory = arguments[0];
  18400. this._factory = factory;
  18401. }
  18402. getCoordinates() {
  18403. if (this._coordinates === null) {
  18404. let forwardDirectedEdges = 0;
  18405. let reverseDirectedEdges = 0;
  18406. const coordinateList = new CoordinateList();
  18407. for (let i = this._directedEdges.iterator(); i.hasNext();) {
  18408. const directedEdge = i.next();
  18409. if (directedEdge.getEdgeDirection()) forwardDirectedEdges++;else reverseDirectedEdges++;
  18410. coordinateList.add(directedEdge.getEdge().getLine().getCoordinates(), false, directedEdge.getEdgeDirection());
  18411. }
  18412. this._coordinates = coordinateList.toCoordinateArray();
  18413. if (reverseDirectedEdges > forwardDirectedEdges) CoordinateArrays.reverse(this._coordinates);
  18414. }
  18415. return this._coordinates;
  18416. }
  18417. toLineString() {
  18418. return this._factory.createLineString(this.getCoordinates());
  18419. }
  18420. add(directedEdge) {
  18421. this._directedEdges.add(directedEdge);
  18422. }
  18423. }
  18424. class GraphComponent {
  18425. constructor() {
  18426. GraphComponent.constructor_.apply(this, arguments);
  18427. }
  18428. static constructor_() {
  18429. this._isMarked = false;
  18430. this._isVisited = false;
  18431. this._data = null;
  18432. }
  18433. static getComponentWithVisitedState(i, visitedState) {
  18434. while (i.hasNext()) {
  18435. const comp = i.next();
  18436. if (comp.isVisited() === visitedState) return comp;
  18437. }
  18438. return null;
  18439. }
  18440. static setVisited(i, visited) {
  18441. while (i.hasNext()) {
  18442. const comp = i.next();
  18443. comp.setVisited(visited);
  18444. }
  18445. }
  18446. static setMarked(i, marked) {
  18447. while (i.hasNext()) {
  18448. const comp = i.next();
  18449. comp.setMarked(marked);
  18450. }
  18451. }
  18452. setVisited(isVisited) {
  18453. this._isVisited = isVisited;
  18454. }
  18455. isMarked() {
  18456. return this._isMarked;
  18457. }
  18458. setData(data) {
  18459. this._data = data;
  18460. }
  18461. getData() {
  18462. return this._data;
  18463. }
  18464. setMarked(isMarked) {
  18465. this._isMarked = isMarked;
  18466. }
  18467. getContext() {
  18468. return this._data;
  18469. }
  18470. isVisited() {
  18471. return this._isVisited;
  18472. }
  18473. setContext(data) {
  18474. this._data = data;
  18475. }
  18476. }
  18477. class DirectedEdge extends GraphComponent {
  18478. constructor() {
  18479. super();
  18480. DirectedEdge.constructor_.apply(this, arguments);
  18481. }
  18482. static constructor_() {
  18483. this._parentEdge = null;
  18484. this._from = null;
  18485. this._to = null;
  18486. this._p0 = null;
  18487. this._p1 = null;
  18488. this._sym = null;
  18489. this._edgeDirection = null;
  18490. this._quadrant = null;
  18491. this._angle = null;
  18492. if (arguments.length === 0) ; else if (arguments.length === 4) {
  18493. const from = arguments[0],
  18494. to = arguments[1],
  18495. directionPt = arguments[2],
  18496. edgeDirection = arguments[3];
  18497. this._from = from;
  18498. this._to = to;
  18499. this._edgeDirection = edgeDirection;
  18500. this._p0 = from.getCoordinate();
  18501. this._p1 = directionPt;
  18502. const dx = this._p1.x - this._p0.x;
  18503. const dy = this._p1.y - this._p0.y;
  18504. this._quadrant = Quadrant.quadrant(dx, dy);
  18505. this._angle = Math.atan2(dy, dx);
  18506. }
  18507. }
  18508. static toEdges(dirEdges) {
  18509. const edges = new ArrayList();
  18510. for (let i = dirEdges.iterator(); i.hasNext();) edges.add(i.next()._parentEdge);
  18511. return edges;
  18512. }
  18513. isRemoved() {
  18514. return this._parentEdge === null;
  18515. }
  18516. compareDirection(e) {
  18517. if (this._quadrant > e._quadrant) return 1;
  18518. if (this._quadrant < e._quadrant) return -1;
  18519. return Orientation.index(e._p0, e._p1, this._p1);
  18520. }
  18521. getCoordinate() {
  18522. return this._from.getCoordinate();
  18523. }
  18524. print(out) {
  18525. const className = this.getClass().getName();
  18526. const lastDotPos = className.lastIndexOf('.');
  18527. const name = className.substring(lastDotPos + 1);
  18528. out.print(' ' + name + ': ' + this._p0 + ' - ' + this._p1 + ' ' + this._quadrant + ':' + this._angle);
  18529. }
  18530. getDirectionPt() {
  18531. return this._p1;
  18532. }
  18533. getAngle() {
  18534. return this._angle;
  18535. }
  18536. compareTo(obj) {
  18537. const de = obj;
  18538. return this.compareDirection(de);
  18539. }
  18540. getFromNode() {
  18541. return this._from;
  18542. }
  18543. getSym() {
  18544. return this._sym;
  18545. }
  18546. setEdge(parentEdge) {
  18547. this._parentEdge = parentEdge;
  18548. }
  18549. remove() {
  18550. this._sym = null;
  18551. this._parentEdge = null;
  18552. }
  18553. getEdge() {
  18554. return this._parentEdge;
  18555. }
  18556. getQuadrant() {
  18557. return this._quadrant;
  18558. }
  18559. setSym(sym) {
  18560. this._sym = sym;
  18561. }
  18562. getToNode() {
  18563. return this._to;
  18564. }
  18565. getEdgeDirection() {
  18566. return this._edgeDirection;
  18567. }
  18568. get interfaces_() {
  18569. return [Comparable];
  18570. }
  18571. }
  18572. class LineMergeDirectedEdge extends DirectedEdge {
  18573. constructor() {
  18574. super();
  18575. LineMergeDirectedEdge.constructor_.apply(this, arguments);
  18576. }
  18577. static constructor_() {
  18578. const from = arguments[0],
  18579. to = arguments[1],
  18580. directionPt = arguments[2],
  18581. edgeDirection = arguments[3];
  18582. DirectedEdge.constructor_.call(this, from, to, directionPt, edgeDirection);
  18583. }
  18584. getNext() {
  18585. if (this.getToNode().getDegree() !== 2) return null;
  18586. if (this.getToNode().getOutEdges().getEdges().get(0) === this.getSym()) return this.getToNode().getOutEdges().getEdges().get(1);
  18587. Assert.isTrue(this.getToNode().getOutEdges().getEdges().get(1) === this.getSym());
  18588. return this.getToNode().getOutEdges().getEdges().get(0);
  18589. }
  18590. }
  18591. class Edge extends GraphComponent {
  18592. constructor() {
  18593. super();
  18594. Edge.constructor_.apply(this, arguments);
  18595. }
  18596. static constructor_() {
  18597. this._dirEdge = null;
  18598. if (arguments.length === 0) ; else if (arguments.length === 2) {
  18599. const de0 = arguments[0],
  18600. de1 = arguments[1];
  18601. this.setDirectedEdges(de0, de1);
  18602. }
  18603. }
  18604. isRemoved() {
  18605. return this._dirEdge === null;
  18606. }
  18607. setDirectedEdges(de0, de1) {
  18608. this._dirEdge = [de0, de1];
  18609. de0.setEdge(this);
  18610. de1.setEdge(this);
  18611. de0.setSym(de1);
  18612. de1.setSym(de0);
  18613. de0.getFromNode().addOutEdge(de0);
  18614. de1.getFromNode().addOutEdge(de1);
  18615. }
  18616. getDirEdge() {
  18617. if (Number.isInteger(arguments[0])) {
  18618. const i = arguments[0];
  18619. return this._dirEdge[i];
  18620. } else if (arguments[0] instanceof Node) {
  18621. const fromNode = arguments[0];
  18622. if (this._dirEdge[0].getFromNode() === fromNode) return this._dirEdge[0];
  18623. if (this._dirEdge[1].getFromNode() === fromNode) return this._dirEdge[1];
  18624. return null;
  18625. }
  18626. }
  18627. remove() {
  18628. this._dirEdge = null;
  18629. }
  18630. getOppositeNode(node) {
  18631. if (this._dirEdge[0].getFromNode() === node) return this._dirEdge[0].getToNode();
  18632. if (this._dirEdge[1].getFromNode() === node) return this._dirEdge[1].getToNode();
  18633. return null;
  18634. }
  18635. }
  18636. class DirectedEdgeStar {
  18637. constructor() {
  18638. DirectedEdgeStar.constructor_.apply(this, arguments);
  18639. }
  18640. static constructor_() {
  18641. this._outEdges = new ArrayList();
  18642. this._sorted = false;
  18643. }
  18644. getNextEdge(dirEdge) {
  18645. const i = this.getIndex(dirEdge);
  18646. return this._outEdges.get(this.getIndex(i + 1));
  18647. }
  18648. getCoordinate() {
  18649. const it = this.iterator();
  18650. if (!it.hasNext()) return null;
  18651. const e = it.next();
  18652. return e.getCoordinate();
  18653. }
  18654. iterator() {
  18655. this.sortEdges();
  18656. return this._outEdges.iterator();
  18657. }
  18658. sortEdges() {
  18659. if (!this._sorted) {
  18660. Collections.sort(this._outEdges);
  18661. this._sorted = true;
  18662. }
  18663. }
  18664. remove(de) {
  18665. this._outEdges.remove(de);
  18666. }
  18667. getEdges() {
  18668. this.sortEdges();
  18669. return this._outEdges;
  18670. }
  18671. getNextCWEdge(dirEdge) {
  18672. const i = this.getIndex(dirEdge);
  18673. return this._outEdges.get(this.getIndex(i - 1));
  18674. }
  18675. getIndex() {
  18676. if (arguments[0] instanceof Edge) {
  18677. const edge = arguments[0];
  18678. this.sortEdges();
  18679. for (let i = 0; i < this._outEdges.size(); i++) {
  18680. const de = this._outEdges.get(i);
  18681. if (de.getEdge() === edge) return i;
  18682. }
  18683. return -1;
  18684. } else if (arguments[0] instanceof DirectedEdge) {
  18685. const dirEdge = arguments[0];
  18686. this.sortEdges();
  18687. for (let i = 0; i < this._outEdges.size(); i++) {
  18688. const de = this._outEdges.get(i);
  18689. if (de === dirEdge) return i;
  18690. }
  18691. return -1;
  18692. } else if (Number.isInteger(arguments[0])) {
  18693. const i = arguments[0];
  18694. let modi = i % this._outEdges.size();
  18695. if (modi < 0) modi += this._outEdges.size();
  18696. return modi;
  18697. }
  18698. }
  18699. add(de) {
  18700. this._outEdges.add(de);
  18701. this._sorted = false;
  18702. }
  18703. getDegree() {
  18704. return this._outEdges.size();
  18705. }
  18706. }
  18707. class Node extends GraphComponent {
  18708. constructor() {
  18709. super();
  18710. Node.constructor_.apply(this, arguments);
  18711. }
  18712. static constructor_() {
  18713. this._pt = null;
  18714. this._deStar = null;
  18715. if (arguments.length === 1) {
  18716. const pt = arguments[0];
  18717. Node.constructor_.call(this, pt, new DirectedEdgeStar());
  18718. } else if (arguments.length === 2) {
  18719. const pt = arguments[0],
  18720. deStar = arguments[1];
  18721. this._pt = pt;
  18722. this._deStar = deStar;
  18723. }
  18724. }
  18725. static getEdgesBetween(node0, node1) {
  18726. const edges0 = DirectedEdge.toEdges(node0.getOutEdges().getEdges());
  18727. const commonEdges = new HashSet(edges0);
  18728. const edges1 = DirectedEdge.toEdges(node1.getOutEdges().getEdges());
  18729. commonEdges.retainAll(edges1);
  18730. return commonEdges;
  18731. }
  18732. isRemoved() {
  18733. return this._pt === null;
  18734. }
  18735. addOutEdge(de) {
  18736. this._deStar.add(de);
  18737. }
  18738. getCoordinate() {
  18739. return this._pt;
  18740. }
  18741. getOutEdges() {
  18742. return this._deStar;
  18743. }
  18744. remove() {
  18745. if (arguments.length === 0) {
  18746. this._pt = null;
  18747. } else if (arguments.length === 1) {
  18748. const de = arguments[0];
  18749. this._deStar.remove(de);
  18750. }
  18751. }
  18752. getIndex(edge) {
  18753. return this._deStar.getIndex(edge);
  18754. }
  18755. getDegree() {
  18756. return this._deStar.getDegree();
  18757. }
  18758. }
  18759. class LineMergeEdge extends Edge {
  18760. constructor() {
  18761. super();
  18762. LineMergeEdge.constructor_.apply(this, arguments);
  18763. }
  18764. static constructor_() {
  18765. this._line = null;
  18766. const line = arguments[0];
  18767. this._line = line;
  18768. }
  18769. getLine() {
  18770. return this._line;
  18771. }
  18772. }
  18773. class NodeMap {
  18774. constructor() {
  18775. NodeMap.constructor_.apply(this, arguments);
  18776. }
  18777. static constructor_() {
  18778. this._nodeMap = new TreeMap();
  18779. }
  18780. find(coord) {
  18781. return this._nodeMap.get(coord);
  18782. }
  18783. iterator() {
  18784. return this._nodeMap.values().iterator();
  18785. }
  18786. remove(pt) {
  18787. return this._nodeMap.remove(pt);
  18788. }
  18789. values() {
  18790. return this._nodeMap.values();
  18791. }
  18792. add(n) {
  18793. this._nodeMap.put(n.getCoordinate(), n);
  18794. return n;
  18795. }
  18796. }
  18797. class PlanarGraph {
  18798. constructor() {
  18799. PlanarGraph.constructor_.apply(this, arguments);
  18800. }
  18801. static constructor_() {
  18802. this._edges = new HashSet();
  18803. this._dirEdges = new HashSet();
  18804. this._nodeMap = new NodeMap();
  18805. }
  18806. findNodesOfDegree(degree) {
  18807. const nodesFound = new ArrayList();
  18808. for (let i = this.nodeIterator(); i.hasNext();) {
  18809. const node = i.next();
  18810. if (node.getDegree() === degree) nodesFound.add(node);
  18811. }
  18812. return nodesFound;
  18813. }
  18814. dirEdgeIterator() {
  18815. return this._dirEdges.iterator();
  18816. }
  18817. edgeIterator() {
  18818. return this._edges.iterator();
  18819. }
  18820. remove() {
  18821. if (arguments[0] instanceof Edge) {
  18822. const edge = arguments[0];
  18823. this.remove(edge.getDirEdge(0));
  18824. this.remove(edge.getDirEdge(1));
  18825. this._edges.remove(edge);
  18826. edge.remove();
  18827. } else if (arguments[0] instanceof DirectedEdge) {
  18828. const de = arguments[0];
  18829. const sym = de.getSym();
  18830. if (sym !== null) sym.setSym(null);
  18831. de.getFromNode().remove(de);
  18832. de.remove();
  18833. this._dirEdges.remove(de);
  18834. } else if (arguments[0] instanceof Node) {
  18835. const node = arguments[0];
  18836. const outEdges = node.getOutEdges().getEdges();
  18837. for (let i = outEdges.iterator(); i.hasNext();) {
  18838. const de = i.next();
  18839. const sym = de.getSym();
  18840. if (sym !== null) this.remove(sym);
  18841. this._dirEdges.remove(de);
  18842. const edge = de.getEdge();
  18843. if (edge !== null) this._edges.remove(edge);
  18844. }
  18845. this._nodeMap.remove(node.getCoordinate());
  18846. node.remove();
  18847. }
  18848. }
  18849. findNode(pt) {
  18850. return this._nodeMap.find(pt);
  18851. }
  18852. getEdges() {
  18853. return this._edges;
  18854. }
  18855. nodeIterator() {
  18856. return this._nodeMap.iterator();
  18857. }
  18858. contains() {
  18859. if (arguments[0] instanceof Edge) {
  18860. const e = arguments[0];
  18861. return this._edges.contains(e);
  18862. } else if (arguments[0] instanceof DirectedEdge) {
  18863. const de = arguments[0];
  18864. return this._dirEdges.contains(de);
  18865. }
  18866. }
  18867. add() {
  18868. if (arguments[0] instanceof Node) {
  18869. const node = arguments[0];
  18870. this._nodeMap.add(node);
  18871. } else if (arguments[0] instanceof Edge) {
  18872. const edge = arguments[0];
  18873. this._edges.add(edge);
  18874. this.add(edge.getDirEdge(0));
  18875. this.add(edge.getDirEdge(1));
  18876. } else if (arguments[0] instanceof DirectedEdge) {
  18877. const dirEdge = arguments[0];
  18878. this._dirEdges.add(dirEdge);
  18879. }
  18880. }
  18881. getNodes() {
  18882. return this._nodeMap.values();
  18883. }
  18884. }
  18885. class LineMergeGraph extends PlanarGraph {
  18886. constructor() {
  18887. super();
  18888. }
  18889. addEdge(lineString) {
  18890. if (lineString.isEmpty()) return null;
  18891. const coordinates = CoordinateArrays.removeRepeatedPoints(lineString.getCoordinates());
  18892. if (coordinates.length <= 1) return null;
  18893. const startCoordinate = coordinates[0];
  18894. const endCoordinate = coordinates[coordinates.length - 1];
  18895. const startNode = this.getNode(startCoordinate);
  18896. const endNode = this.getNode(endCoordinate);
  18897. const directedEdge0 = new LineMergeDirectedEdge(startNode, endNode, coordinates[1], true);
  18898. const directedEdge1 = new LineMergeDirectedEdge(endNode, startNode, coordinates[coordinates.length - 2], false);
  18899. const edge = new LineMergeEdge(lineString);
  18900. edge.setDirectedEdges(directedEdge0, directedEdge1);
  18901. this.add(edge);
  18902. }
  18903. getNode(coordinate) {
  18904. let node = this.findNode(coordinate);
  18905. if (node === null) {
  18906. node = new Node(coordinate);
  18907. this.add(node);
  18908. }
  18909. return node;
  18910. }
  18911. }
  18912. class LineMerger {
  18913. constructor() {
  18914. LineMerger.constructor_.apply(this, arguments);
  18915. }
  18916. static constructor_() {
  18917. this._graph = new LineMergeGraph();
  18918. this._mergedLineStrings = null;
  18919. this._factory = null;
  18920. this._edgeStrings = null;
  18921. }
  18922. buildEdgeStringsForUnprocessedNodes() {
  18923. for (let i = this._graph.getNodes().iterator(); i.hasNext();) {
  18924. const node = i.next();
  18925. if (!node.isMarked()) {
  18926. Assert.isTrue(node.getDegree() === 2);
  18927. this.buildEdgeStringsStartingAt(node);
  18928. node.setMarked(true);
  18929. }
  18930. }
  18931. }
  18932. buildEdgeStringsForNonDegree2Nodes() {
  18933. for (let i = this._graph.getNodes().iterator(); i.hasNext();) {
  18934. const node = i.next();
  18935. if (node.getDegree() !== 2) {
  18936. this.buildEdgeStringsStartingAt(node);
  18937. node.setMarked(true);
  18938. }
  18939. }
  18940. }
  18941. buildEdgeStringsForObviousStartNodes() {
  18942. this.buildEdgeStringsForNonDegree2Nodes();
  18943. }
  18944. getMergedLineStrings() {
  18945. this.merge();
  18946. return this._mergedLineStrings;
  18947. }
  18948. buildEdgeStringsStartingAt(node) {
  18949. for (let i = node.getOutEdges().iterator(); i.hasNext();) {
  18950. const directedEdge = i.next();
  18951. if (directedEdge.getEdge().isMarked()) continue;
  18952. this._edgeStrings.add(this.buildEdgeStringStartingWith(directedEdge));
  18953. }
  18954. }
  18955. merge() {
  18956. if (this._mergedLineStrings !== null) return null;
  18957. GraphComponent.setMarked(this._graph.nodeIterator(), false);
  18958. GraphComponent.setMarked(this._graph.edgeIterator(), false);
  18959. this._edgeStrings = new ArrayList();
  18960. this.buildEdgeStringsForObviousStartNodes();
  18961. this.buildEdgeStringsForIsolatedLoops();
  18962. this._mergedLineStrings = new ArrayList();
  18963. for (let i = this._edgeStrings.iterator(); i.hasNext();) {
  18964. const edgeString = i.next();
  18965. this._mergedLineStrings.add(edgeString.toLineString());
  18966. }
  18967. }
  18968. addLineString(lineString) {
  18969. if (this._factory === null) this._factory = lineString.getFactory();
  18970. this._graph.addEdge(lineString);
  18971. }
  18972. buildEdgeStringStartingWith(start) {
  18973. const edgeString = new EdgeString(this._factory);
  18974. let current = start;
  18975. do {
  18976. edgeString.add(current);
  18977. current.getEdge().setMarked(true);
  18978. current = current.getNext();
  18979. } while (current !== null && current !== start);
  18980. return edgeString;
  18981. }
  18982. add() {
  18983. if (arguments[0] instanceof Geometry) {
  18984. const geometry = arguments[0];
  18985. for (let i = 0; i < geometry.getNumGeometries(); i++) {
  18986. const component = geometry.getGeometryN(i);
  18987. if (component instanceof LineString) this.addLineString(component);
  18988. }
  18989. } else if (hasInterface(arguments[0], Collection)) {
  18990. const geometries = arguments[0];
  18991. this._mergedLineStrings = null;
  18992. for (let i = geometries.iterator(); i.hasNext();) {
  18993. const geometry = i.next();
  18994. this.add(geometry);
  18995. }
  18996. }
  18997. }
  18998. buildEdgeStringsForIsolatedLoops() {
  18999. this.buildEdgeStringsForUnprocessedNodes();
  19000. }
  19001. }
  19002. class Subgraph {
  19003. constructor() {
  19004. Subgraph.constructor_.apply(this, arguments);
  19005. }
  19006. static constructor_() {
  19007. this._parentGraph = null;
  19008. this._edges = new HashSet();
  19009. this._dirEdges = new ArrayList();
  19010. this._nodeMap = new NodeMap();
  19011. const parentGraph = arguments[0];
  19012. this._parentGraph = parentGraph;
  19013. }
  19014. dirEdgeIterator() {
  19015. return this._dirEdges.iterator();
  19016. }
  19017. edgeIterator() {
  19018. return this._edges.iterator();
  19019. }
  19020. getParent() {
  19021. return this._parentGraph;
  19022. }
  19023. nodeIterator() {
  19024. return this._nodeMap.iterator();
  19025. }
  19026. contains(e) {
  19027. return this._edges.contains(e);
  19028. }
  19029. add(e) {
  19030. if (this._edges.contains(e)) return null;
  19031. this._edges.add(e);
  19032. this._dirEdges.add(e.getDirEdge(0));
  19033. this._dirEdges.add(e.getDirEdge(1));
  19034. this._nodeMap.add(e.getDirEdge(0).getFromNode());
  19035. this._nodeMap.add(e.getDirEdge(1).getFromNode());
  19036. }
  19037. }
  19038. class ConnectedSubgraphFinder {
  19039. constructor() {
  19040. ConnectedSubgraphFinder.constructor_.apply(this, arguments);
  19041. }
  19042. static constructor_() {
  19043. this._graph = null;
  19044. const graph = arguments[0];
  19045. this._graph = graph;
  19046. }
  19047. addReachable(startNode, subgraph) {
  19048. const nodeStack = new Stack();
  19049. nodeStack.add(startNode);
  19050. while (!nodeStack.empty()) {
  19051. const node = nodeStack.pop();
  19052. this.addEdges(node, nodeStack, subgraph);
  19053. }
  19054. }
  19055. findSubgraph(node) {
  19056. const subgraph = new Subgraph(this._graph);
  19057. this.addReachable(node, subgraph);
  19058. return subgraph;
  19059. }
  19060. getConnectedSubgraphs() {
  19061. const subgraphs = new ArrayList();
  19062. GraphComponent.setVisited(this._graph.nodeIterator(), false);
  19063. for (let i = this._graph.edgeIterator(); i.hasNext();) {
  19064. const e = i.next();
  19065. const node = e.getDirEdge(0).getFromNode();
  19066. if (!node.isVisited()) subgraphs.add(this.findSubgraph(node));
  19067. }
  19068. return subgraphs;
  19069. }
  19070. addEdges(node, nodeStack, subgraph) {
  19071. node.setVisited(true);
  19072. for (let i = node.getOutEdges().iterator(); i.hasNext();) {
  19073. const de = i.next();
  19074. subgraph.add(de.getEdge());
  19075. const toNode = de.getToNode();
  19076. if (!toNode.isVisited()) nodeStack.push(toNode);
  19077. }
  19078. }
  19079. }
  19080. class LineSequencer {
  19081. constructor() {
  19082. LineSequencer.constructor_.apply(this, arguments);
  19083. }
  19084. static constructor_() {
  19085. this._graph = new LineMergeGraph();
  19086. this._factory = new GeometryFactory();
  19087. this._lineCount = 0;
  19088. this._isRun = false;
  19089. this._sequencedGeometry = null;
  19090. this._isSequenceable = false;
  19091. }
  19092. static findUnvisitedBestOrientedDE(node) {
  19093. let wellOrientedDE = null;
  19094. let unvisitedDE = null;
  19095. for (let i = node.getOutEdges().iterator(); i.hasNext();) {
  19096. const de = i.next();
  19097. if (!de.getEdge().isVisited()) {
  19098. unvisitedDE = de;
  19099. if (de.getEdgeDirection()) wellOrientedDE = de;
  19100. }
  19101. }
  19102. if (wellOrientedDE !== null) return wellOrientedDE;
  19103. return unvisitedDE;
  19104. }
  19105. static findLowestDegreeNode(graph) {
  19106. let minDegree = Integer.MAX_VALUE;
  19107. let minDegreeNode = null;
  19108. for (let i = graph.nodeIterator(); i.hasNext();) {
  19109. const node = i.next();
  19110. if (minDegreeNode === null || node.getDegree() < minDegree) {
  19111. minDegree = node.getDegree();
  19112. minDegreeNode = node;
  19113. }
  19114. }
  19115. return minDegreeNode;
  19116. }
  19117. static isSequenced(geom) {
  19118. if (!(geom instanceof MultiLineString)) return true;
  19119. const mls = geom;
  19120. const prevSubgraphNodes = new TreeSet();
  19121. let lastNode = null;
  19122. const currNodes = new ArrayList();
  19123. for (let i = 0; i < mls.getNumGeometries(); i++) {
  19124. const line = mls.getGeometryN(i);
  19125. const startNode = line.getCoordinateN(0);
  19126. const endNode = line.getCoordinateN(line.getNumPoints() - 1);
  19127. if (prevSubgraphNodes.contains(startNode)) return false;
  19128. if (prevSubgraphNodes.contains(endNode)) return false;
  19129. if (lastNode !== null) if (!startNode.equals(lastNode)) {
  19130. prevSubgraphNodes.addAll(currNodes);
  19131. currNodes.clear();
  19132. }
  19133. currNodes.add(startNode);
  19134. currNodes.add(endNode);
  19135. lastNode = endNode;
  19136. }
  19137. return true;
  19138. }
  19139. static reverse(line) {
  19140. const pts = line.getCoordinates();
  19141. const revPts = new Array(pts.length).fill(null);
  19142. const len = pts.length;
  19143. for (let i = 0; i < len; i++) revPts[len - 1 - i] = new Coordinate(pts[i]);
  19144. return line.getFactory().createLineString(revPts);
  19145. }
  19146. static sequence(geom) {
  19147. const sequencer = new LineSequencer();
  19148. sequencer.add(geom);
  19149. return sequencer.getSequencedLineStrings();
  19150. }
  19151. addLine(lineString) {
  19152. if (this._factory === null) this._factory = lineString.getFactory();
  19153. this._graph.addEdge(lineString);
  19154. this._lineCount++;
  19155. }
  19156. hasSequence(graph) {
  19157. let oddDegreeCount = 0;
  19158. for (let i = graph.nodeIterator(); i.hasNext();) {
  19159. const node = i.next();
  19160. if (node.getDegree() % 2 === 1) oddDegreeCount++;
  19161. }
  19162. return oddDegreeCount <= 2;
  19163. }
  19164. computeSequence() {
  19165. if (this._isRun) return null;
  19166. this._isRun = true;
  19167. const sequences = this.findSequences();
  19168. if (sequences === null) return null;
  19169. this._sequencedGeometry = this.buildSequencedGeometry(sequences);
  19170. this._isSequenceable = true;
  19171. const finalLineCount = this._sequencedGeometry.getNumGeometries();
  19172. Assert.isTrue(this._lineCount === finalLineCount, 'Lines were missing from result');
  19173. Assert.isTrue(this._sequencedGeometry instanceof LineString || this._sequencedGeometry instanceof MultiLineString, 'Result is not lineal');
  19174. }
  19175. findSequences() {
  19176. const sequences = new ArrayList();
  19177. const csFinder = new ConnectedSubgraphFinder(this._graph);
  19178. const subgraphs = csFinder.getConnectedSubgraphs();
  19179. for (let i = subgraphs.iterator(); i.hasNext();) {
  19180. const subgraph = i.next();
  19181. if (this.hasSequence(subgraph)) {
  19182. const seq = this.findSequence(subgraph);
  19183. sequences.add(seq);
  19184. } else {
  19185. return null;
  19186. }
  19187. }
  19188. return sequences;
  19189. }
  19190. addReverseSubpath(de, lit, expectedClosed) {
  19191. const endNode = de.getToNode();
  19192. let fromNode = null;
  19193. while (true) {
  19194. lit.add(de.getSym());
  19195. de.getEdge().setVisited(true);
  19196. fromNode = de.getFromNode();
  19197. const unvisitedOutDE = LineSequencer.findUnvisitedBestOrientedDE(fromNode);
  19198. if (unvisitedOutDE === null) break;
  19199. de = unvisitedOutDE.getSym();
  19200. }
  19201. if (expectedClosed) Assert.isTrue(fromNode === endNode, 'path not contiguous');
  19202. }
  19203. findSequence(graph) {
  19204. GraphComponent.setVisited(graph.edgeIterator(), false);
  19205. const startNode = LineSequencer.findLowestDegreeNode(graph);
  19206. const startDE = startNode.getOutEdges().iterator().next();
  19207. const startDESym = startDE.getSym();
  19208. const seq = new LinkedList();
  19209. const lit = seq.listIterator();
  19210. this.addReverseSubpath(startDESym, lit, false);
  19211. while (lit.hasPrevious()) {
  19212. const prev = lit.previous();
  19213. const unvisitedOutDE = LineSequencer.findUnvisitedBestOrientedDE(prev.getFromNode());
  19214. if (unvisitedOutDE !== null) this.addReverseSubpath(unvisitedOutDE.getSym(), lit, true);
  19215. }
  19216. const orientedSeq = this.orient(seq);
  19217. return orientedSeq;
  19218. }
  19219. reverse(seq) {
  19220. const newSeq = new LinkedList();
  19221. for (let i = seq.iterator(); i.hasNext();) {
  19222. const de = i.next();
  19223. newSeq.addFirst(de.getSym());
  19224. }
  19225. return newSeq;
  19226. }
  19227. orient(seq) {
  19228. const startEdge = seq.get(0);
  19229. const endEdge = seq.get(seq.size() - 1);
  19230. const startNode = startEdge.getFromNode();
  19231. const endNode = endEdge.getToNode();
  19232. let flipSeq = false;
  19233. const hasDegree1Node = startNode.getDegree() === 1 || endNode.getDegree() === 1;
  19234. if (hasDegree1Node) {
  19235. let hasObviousStartNode = false;
  19236. if (endEdge.getToNode().getDegree() === 1 && endEdge.getEdgeDirection() === false) {
  19237. hasObviousStartNode = true;
  19238. flipSeq = true;
  19239. }
  19240. if (startEdge.getFromNode().getDegree() === 1 && startEdge.getEdgeDirection() === true) {
  19241. hasObviousStartNode = true;
  19242. flipSeq = false;
  19243. }
  19244. if (!hasObviousStartNode) if (startEdge.getFromNode().getDegree() === 1) flipSeq = true;
  19245. }
  19246. if (flipSeq) return this.reverse(seq);
  19247. return seq;
  19248. }
  19249. buildSequencedGeometry(sequences) {
  19250. const lines = new ArrayList();
  19251. for (let i1 = sequences.iterator(); i1.hasNext();) {
  19252. const seq = i1.next();
  19253. for (let i2 = seq.iterator(); i2.hasNext();) {
  19254. const de = i2.next();
  19255. const e = de.getEdge();
  19256. const line = e.getLine();
  19257. let lineToAdd = line;
  19258. if (!de.getEdgeDirection() && !line.isClosed()) lineToAdd = LineSequencer.reverse(line);
  19259. lines.add(lineToAdd);
  19260. }
  19261. }
  19262. if (lines.size() === 0) return this._factory.createMultiLineString(new Array(0).fill(null));
  19263. return this._factory.buildGeometry(lines);
  19264. }
  19265. getSequencedLineStrings() {
  19266. this.computeSequence();
  19267. return this._sequencedGeometry;
  19268. }
  19269. isSequenceable() {
  19270. this.computeSequence();
  19271. return this._isSequenceable;
  19272. }
  19273. add() {
  19274. if (hasInterface(arguments[0], Collection)) {
  19275. const geometries = arguments[0];
  19276. for (let i = geometries.iterator(); i.hasNext();) {
  19277. const geometry = i.next();
  19278. this.add(geometry);
  19279. }
  19280. } else if (arguments[0] instanceof Geometry) {
  19281. const geometry = arguments[0];
  19282. geometry.apply(new class {
  19283. get interfaces_() {
  19284. return [GeometryComponentFilter];
  19285. }
  19286. filter(component) {
  19287. if (component instanceof LineString) this.addLine(component);
  19288. }
  19289. }());
  19290. }
  19291. }
  19292. }
  19293. var linemerge = /*#__PURE__*/Object.freeze({
  19294. __proto__: null,
  19295. LineMerger: LineMerger,
  19296. LineSequencer: LineSequencer
  19297. });
  19298. class LineStringSnapper {
  19299. constructor() {
  19300. LineStringSnapper.constructor_.apply(this, arguments);
  19301. }
  19302. static constructor_() {
  19303. this._snapTolerance = 0.0;
  19304. this._srcPts = null;
  19305. this._seg = new LineSegment();
  19306. this._allowSnappingToSourceVertices = false;
  19307. this._isClosed = false;
  19308. if (arguments[0] instanceof LineString && typeof arguments[1] === 'number') {
  19309. const srcLine = arguments[0],
  19310. snapTolerance = arguments[1];
  19311. LineStringSnapper.constructor_.call(this, srcLine.getCoordinates(), snapTolerance);
  19312. } else if (arguments[0] instanceof Array && typeof arguments[1] === 'number') {
  19313. const srcPts = arguments[0],
  19314. snapTolerance = arguments[1];
  19315. this._srcPts = srcPts;
  19316. this._isClosed = LineStringSnapper.isClosed(srcPts);
  19317. this._snapTolerance = snapTolerance;
  19318. }
  19319. }
  19320. static isClosed(pts) {
  19321. if (pts.length <= 1) return false;
  19322. return pts[0].equals2D(pts[pts.length - 1]);
  19323. }
  19324. snapVertices(srcCoords, snapPts) {
  19325. const end = this._isClosed ? srcCoords.size() - 1 : srcCoords.size();
  19326. for (let i = 0; i < end; i++) {
  19327. const srcPt = srcCoords.get(i);
  19328. const snapVert = this.findSnapForVertex(srcPt, snapPts);
  19329. if (snapVert !== null) {
  19330. srcCoords.set(i, new Coordinate(snapVert));
  19331. if (i === 0 && this._isClosed) srcCoords.set(srcCoords.size() - 1, new Coordinate(snapVert));
  19332. }
  19333. }
  19334. }
  19335. findSnapForVertex(pt, snapPts) {
  19336. for (let i = 0; i < snapPts.length; i++) {
  19337. if (pt.equals2D(snapPts[i])) return null;
  19338. if (pt.distance(snapPts[i]) < this._snapTolerance) return snapPts[i];
  19339. }
  19340. return null;
  19341. }
  19342. snapTo(snapPts) {
  19343. const coordList = new CoordinateList(this._srcPts);
  19344. this.snapVertices(coordList, snapPts);
  19345. this.snapSegments(coordList, snapPts);
  19346. const newPts = coordList.toCoordinateArray();
  19347. return newPts;
  19348. }
  19349. snapSegments(srcCoords, snapPts) {
  19350. if (snapPts.length === 0) return null;
  19351. let distinctPtCount = snapPts.length;
  19352. if (snapPts[0].equals2D(snapPts[snapPts.length - 1])) distinctPtCount = snapPts.length - 1;
  19353. for (let i = 0; i < distinctPtCount; i++) {
  19354. const snapPt = snapPts[i];
  19355. const index = this.findSegmentIndexToSnap(snapPt, srcCoords);
  19356. if (index >= 0) srcCoords.add(index + 1, new Coordinate(snapPt), false);
  19357. }
  19358. }
  19359. findSegmentIndexToSnap(snapPt, srcCoords) {
  19360. let minDist = Double.MAX_VALUE;
  19361. let snapIndex = -1;
  19362. for (let i = 0; i < srcCoords.size() - 1; i++) {
  19363. this._seg.p0 = srcCoords.get(i);
  19364. this._seg.p1 = srcCoords.get(i + 1);
  19365. if (this._seg.p0.equals2D(snapPt) || this._seg.p1.equals2D(snapPt)) if (this._allowSnappingToSourceVertices) continue;else return -1;
  19366. const dist = this._seg.distance(snapPt);
  19367. if (dist < this._snapTolerance && dist < minDist) {
  19368. minDist = dist;
  19369. snapIndex = i;
  19370. }
  19371. }
  19372. return snapIndex;
  19373. }
  19374. setAllowSnappingToSourceVertices(allowSnappingToSourceVertices) {
  19375. this._allowSnappingToSourceVertices = allowSnappingToSourceVertices;
  19376. }
  19377. }
  19378. class GeometrySnapper {
  19379. constructor() {
  19380. GeometrySnapper.constructor_.apply(this, arguments);
  19381. }
  19382. static constructor_() {
  19383. this._srcGeom = null;
  19384. const srcGeom = arguments[0];
  19385. this._srcGeom = srcGeom;
  19386. }
  19387. static snap(g0, g1, snapTolerance) {
  19388. const snapGeom = new Array(2).fill(null);
  19389. const snapper0 = new GeometrySnapper(g0);
  19390. snapGeom[0] = snapper0.snapTo(g1, snapTolerance);
  19391. const snapper1 = new GeometrySnapper(g1);
  19392. snapGeom[1] = snapper1.snapTo(snapGeom[0], snapTolerance);
  19393. return snapGeom;
  19394. }
  19395. static computeOverlaySnapTolerance() {
  19396. if (arguments.length === 1) {
  19397. const g = arguments[0];
  19398. let snapTolerance = GeometrySnapper.computeSizeBasedSnapTolerance(g);
  19399. const pm = g.getPrecisionModel();
  19400. if (pm.getType() === PrecisionModel.FIXED) {
  19401. const fixedSnapTol = 1 / pm.getScale() * 2 / 1.415;
  19402. if (fixedSnapTol > snapTolerance) snapTolerance = fixedSnapTol;
  19403. }
  19404. return snapTolerance;
  19405. } else if (arguments.length === 2) {
  19406. const g0 = arguments[0],
  19407. g1 = arguments[1];
  19408. return Math.min(GeometrySnapper.computeOverlaySnapTolerance(g0), GeometrySnapper.computeOverlaySnapTolerance(g1));
  19409. }
  19410. }
  19411. static computeSizeBasedSnapTolerance(g) {
  19412. const env = g.getEnvelopeInternal();
  19413. const minDimension = Math.min(env.getHeight(), env.getWidth());
  19414. const snapTol = minDimension * GeometrySnapper.SNAP_PRECISION_FACTOR;
  19415. return snapTol;
  19416. }
  19417. static snapToSelf(geom, snapTolerance, cleanResult) {
  19418. const snapper0 = new GeometrySnapper(geom);
  19419. return snapper0.snapToSelf(snapTolerance, cleanResult);
  19420. }
  19421. snapTo(snapGeom, snapTolerance) {
  19422. const snapPts = this.extractTargetCoordinates(snapGeom);
  19423. const snapTrans = new SnapTransformer(snapTolerance, snapPts);
  19424. return snapTrans.transform(this._srcGeom);
  19425. }
  19426. snapToSelf(snapTolerance, cleanResult) {
  19427. const snapPts = this.extractTargetCoordinates(this._srcGeom);
  19428. const snapTrans = new SnapTransformer(snapTolerance, snapPts, true);
  19429. const snappedGeom = snapTrans.transform(this._srcGeom);
  19430. let result = snappedGeom;
  19431. if (cleanResult && hasInterface(result, Polygonal)) result = snappedGeom.buffer(0);
  19432. return result;
  19433. }
  19434. computeSnapTolerance(ringPts) {
  19435. const minSegLen = this.computeMinimumSegmentLength(ringPts);
  19436. const snapTol = minSegLen / 10;
  19437. return snapTol;
  19438. }
  19439. extractTargetCoordinates(g) {
  19440. const ptSet = new TreeSet();
  19441. const pts = g.getCoordinates();
  19442. for (let i = 0; i < pts.length; i++) ptSet.add(pts[i]);
  19443. return ptSet.toArray(new Array(0).fill(null));
  19444. }
  19445. computeMinimumSegmentLength(pts) {
  19446. let minSegLen = Double.MAX_VALUE;
  19447. for (let i = 0; i < pts.length - 1; i++) {
  19448. const segLen = pts[i].distance(pts[i + 1]);
  19449. if (segLen < minSegLen) minSegLen = segLen;
  19450. }
  19451. return minSegLen;
  19452. }
  19453. }
  19454. GeometrySnapper.SNAP_PRECISION_FACTOR = 1e-9;
  19455. class SnapTransformer extends GeometryTransformer {
  19456. constructor() {
  19457. super();
  19458. SnapTransformer.constructor_.apply(this, arguments);
  19459. }
  19460. static constructor_() {
  19461. this._snapTolerance = null;
  19462. this._snapPts = null;
  19463. this._isSelfSnap = false;
  19464. if (arguments.length === 2) {
  19465. const snapTolerance = arguments[0],
  19466. snapPts = arguments[1];
  19467. this._snapTolerance = snapTolerance;
  19468. this._snapPts = snapPts;
  19469. } else if (arguments.length === 3) {
  19470. const snapTolerance = arguments[0],
  19471. snapPts = arguments[1],
  19472. isSelfSnap = arguments[2];
  19473. this._snapTolerance = snapTolerance;
  19474. this._snapPts = snapPts;
  19475. this._isSelfSnap = isSelfSnap;
  19476. }
  19477. }
  19478. snapLine(srcPts, snapPts) {
  19479. const snapper = new LineStringSnapper(srcPts, this._snapTolerance);
  19480. snapper.setAllowSnappingToSourceVertices(this._isSelfSnap);
  19481. return snapper.snapTo(snapPts);
  19482. }
  19483. transformCoordinates(coords, parent) {
  19484. const srcPts = coords.toCoordinateArray();
  19485. const newPts = this.snapLine(srcPts, this._snapPts);
  19486. return this._factory.getCoordinateSequenceFactory().create(newPts);
  19487. }
  19488. }
  19489. var snap = /*#__PURE__*/Object.freeze({
  19490. __proto__: null,
  19491. GeometrySnapper: GeometrySnapper,
  19492. LineStringSnapper: LineStringSnapper
  19493. });
  19494. class BasicSegmentString {
  19495. constructor() {
  19496. BasicSegmentString.constructor_.apply(this, arguments);
  19497. }
  19498. static constructor_() {
  19499. this._pts = null;
  19500. this._data = null;
  19501. const pts = arguments[0],
  19502. data = arguments[1];
  19503. this._pts = pts;
  19504. this._data = data;
  19505. }
  19506. getCoordinates() {
  19507. return this._pts;
  19508. }
  19509. size() {
  19510. return this._pts.length;
  19511. }
  19512. getCoordinate(i) {
  19513. return this._pts[i];
  19514. }
  19515. isClosed() {
  19516. return this._pts[0].equals(this._pts[this._pts.length - 1]);
  19517. }
  19518. getSegmentOctant(index) {
  19519. if (index === this._pts.length - 1) return -1;
  19520. return Octant.octant(this.getCoordinate(index), this.getCoordinate(index + 1));
  19521. }
  19522. setData(data) {
  19523. this._data = data;
  19524. }
  19525. getData() {
  19526. return this._data;
  19527. }
  19528. toString() {
  19529. return WKTWriter.toLineString(new CoordinateArraySequence(this._pts));
  19530. }
  19531. get interfaces_() {
  19532. return [SegmentString];
  19533. }
  19534. }
  19535. class NodingIntersectionFinder {
  19536. constructor() {
  19537. NodingIntersectionFinder.constructor_.apply(this, arguments);
  19538. }
  19539. static constructor_() {
  19540. this._findAllIntersections = false;
  19541. this._isCheckEndSegmentsOnly = false;
  19542. this._keepIntersections = true;
  19543. this._isInteriorIntersectionsOnly = false;
  19544. this._li = null;
  19545. this._interiorIntersection = null;
  19546. this._intSegments = null;
  19547. this._intersections = new ArrayList();
  19548. this._intersectionCount = 0;
  19549. const li = arguments[0];
  19550. this._li = li;
  19551. this._interiorIntersection = null;
  19552. }
  19553. static createAllIntersectionsFinder(li) {
  19554. const finder = new NodingIntersectionFinder(li);
  19555. finder.setFindAllIntersections(true);
  19556. return finder;
  19557. }
  19558. static isInteriorVertexIntersection() {
  19559. if (arguments.length === 4) {
  19560. const p0 = arguments[0],
  19561. p1 = arguments[1],
  19562. isEnd0 = arguments[2],
  19563. isEnd1 = arguments[3];
  19564. if (isEnd0 && isEnd1) return false;
  19565. if (p0.equals2D(p1)) return true;
  19566. return false;
  19567. } else if (arguments.length === 8) {
  19568. const p00 = arguments[0],
  19569. p01 = arguments[1],
  19570. p10 = arguments[2],
  19571. p11 = arguments[3],
  19572. isEnd00 = arguments[4],
  19573. isEnd01 = arguments[5],
  19574. isEnd10 = arguments[6],
  19575. isEnd11 = arguments[7];
  19576. if (NodingIntersectionFinder.isInteriorVertexIntersection(p00, p10, isEnd00, isEnd10)) return true;
  19577. if (NodingIntersectionFinder.isInteriorVertexIntersection(p00, p11, isEnd00, isEnd11)) return true;
  19578. if (NodingIntersectionFinder.isInteriorVertexIntersection(p01, p10, isEnd01, isEnd10)) return true;
  19579. if (NodingIntersectionFinder.isInteriorVertexIntersection(p01, p11, isEnd01, isEnd11)) return true;
  19580. return false;
  19581. }
  19582. }
  19583. static createInteriorIntersectionCounter(li) {
  19584. const finder = new NodingIntersectionFinder(li);
  19585. finder.setInteriorIntersectionsOnly(true);
  19586. finder.setFindAllIntersections(true);
  19587. finder.setKeepIntersections(false);
  19588. return finder;
  19589. }
  19590. static createIntersectionCounter(li) {
  19591. const finder = new NodingIntersectionFinder(li);
  19592. finder.setFindAllIntersections(true);
  19593. finder.setKeepIntersections(false);
  19594. return finder;
  19595. }
  19596. static isEndSegment(segStr, index) {
  19597. if (index === 0) return true;
  19598. if (index >= segStr.size() - 2) return true;
  19599. return false;
  19600. }
  19601. static createAnyIntersectionFinder(li) {
  19602. return new NodingIntersectionFinder(li);
  19603. }
  19604. static createInteriorIntersectionsFinder(li) {
  19605. const finder = new NodingIntersectionFinder(li);
  19606. finder.setFindAllIntersections(true);
  19607. finder.setInteriorIntersectionsOnly(true);
  19608. return finder;
  19609. }
  19610. setCheckEndSegmentsOnly(isCheckEndSegmentsOnly) {
  19611. this._isCheckEndSegmentsOnly = isCheckEndSegmentsOnly;
  19612. }
  19613. getIntersectionSegments() {
  19614. return this._intSegments;
  19615. }
  19616. count() {
  19617. return this._intersectionCount;
  19618. }
  19619. getIntersections() {
  19620. return this._intersections;
  19621. }
  19622. setFindAllIntersections(findAllIntersections) {
  19623. this._findAllIntersections = findAllIntersections;
  19624. }
  19625. setKeepIntersections(keepIntersections) {
  19626. this._keepIntersections = keepIntersections;
  19627. }
  19628. getIntersection() {
  19629. return this._interiorIntersection;
  19630. }
  19631. processIntersections(e0, segIndex0, e1, segIndex1) {
  19632. if (!this._findAllIntersections && this.hasIntersection()) return null;
  19633. const isSameSegString = e0 === e1;
  19634. const isSameSegment = isSameSegString && segIndex0 === segIndex1;
  19635. if (isSameSegment) return null;
  19636. if (this._isCheckEndSegmentsOnly) {
  19637. const isEndSegPresent = NodingIntersectionFinder.isEndSegment(e0, segIndex0) || NodingIntersectionFinder.isEndSegment(e1, segIndex1);
  19638. if (!isEndSegPresent) return null;
  19639. }
  19640. const p00 = e0.getCoordinate(segIndex0);
  19641. const p01 = e0.getCoordinate(segIndex0 + 1);
  19642. const p10 = e1.getCoordinate(segIndex1);
  19643. const p11 = e1.getCoordinate(segIndex1 + 1);
  19644. const isEnd00 = segIndex0 === 0;
  19645. const isEnd01 = segIndex0 + 2 === e0.size();
  19646. const isEnd10 = segIndex1 === 0;
  19647. const isEnd11 = segIndex1 + 2 === e1.size();
  19648. this._li.computeIntersection(p00, p01, p10, p11);
  19649. const isInteriorInt = this._li.hasIntersection() && this._li.isInteriorIntersection();
  19650. let isInteriorVertexInt = false;
  19651. if (!this._isInteriorIntersectionsOnly) {
  19652. const isAdjacentSegment = isSameSegString && Math.abs(segIndex1 - segIndex0) <= 1;
  19653. isInteriorVertexInt = !isAdjacentSegment && NodingIntersectionFinder.isInteriorVertexIntersection(p00, p01, p10, p11, isEnd00, isEnd01, isEnd10, isEnd11);
  19654. }
  19655. if (isInteriorInt || isInteriorVertexInt) {
  19656. this._intSegments = new Array(4).fill(null);
  19657. this._intSegments[0] = p00;
  19658. this._intSegments[1] = p01;
  19659. this._intSegments[2] = p10;
  19660. this._intSegments[3] = p11;
  19661. this._interiorIntersection = this._li.getIntersection(0);
  19662. if (this._keepIntersections) this._intersections.add(this._interiorIntersection);
  19663. this._intersectionCount++;
  19664. }
  19665. }
  19666. hasIntersection() {
  19667. return this._interiorIntersection !== null;
  19668. }
  19669. isDone() {
  19670. if (this._findAllIntersections) return false;
  19671. return this._interiorIntersection !== null;
  19672. }
  19673. setInteriorIntersectionsOnly(isInteriorIntersectionsOnly) {
  19674. this._isInteriorIntersectionsOnly = isInteriorIntersectionsOnly;
  19675. }
  19676. get interfaces_() {
  19677. return [SegmentIntersector];
  19678. }
  19679. }
  19680. class FastNodingValidator {
  19681. constructor() {
  19682. FastNodingValidator.constructor_.apply(this, arguments);
  19683. }
  19684. static constructor_() {
  19685. this._li = new RobustLineIntersector();
  19686. this._segStrings = null;
  19687. this._findAllIntersections = false;
  19688. this._segInt = null;
  19689. this._isValid = true;
  19690. const segStrings = arguments[0];
  19691. this._segStrings = segStrings;
  19692. }
  19693. static computeIntersections(segStrings) {
  19694. const nv = new FastNodingValidator(segStrings);
  19695. nv.setFindAllIntersections(true);
  19696. nv.isValid();
  19697. return nv.getIntersections();
  19698. }
  19699. execute() {
  19700. if (this._segInt !== null) return null;
  19701. this.checkInteriorIntersections();
  19702. }
  19703. getIntersections() {
  19704. return this._segInt.getIntersections();
  19705. }
  19706. isValid() {
  19707. this.execute();
  19708. return this._isValid;
  19709. }
  19710. setFindAllIntersections(findAllIntersections) {
  19711. this._findAllIntersections = findAllIntersections;
  19712. }
  19713. checkInteriorIntersections() {
  19714. this._isValid = true;
  19715. this._segInt = new NodingIntersectionFinder(this._li);
  19716. this._segInt.setFindAllIntersections(this._findAllIntersections);
  19717. const noder = new MCIndexNoder();
  19718. noder.setSegmentIntersector(this._segInt);
  19719. noder.computeNodes(this._segStrings);
  19720. if (this._segInt.hasIntersection()) {
  19721. this._isValid = false;
  19722. return null;
  19723. }
  19724. }
  19725. checkValid() {
  19726. this.execute();
  19727. if (!this._isValid) throw new TopologyException(this.getErrorMessage(), this._segInt.getIntersection());
  19728. }
  19729. getErrorMessage() {
  19730. if (this._isValid) return 'no intersections found';
  19731. const intSegs = this._segInt.getIntersectionSegments();
  19732. return 'found non-noded intersection between ' + WKTWriter.toLineString(intSegs[0], intSegs[1]) + ' and ' + WKTWriter.toLineString(intSegs[2], intSegs[3]);
  19733. }
  19734. }
  19735. class EdgeNodingValidator {
  19736. constructor() {
  19737. EdgeNodingValidator.constructor_.apply(this, arguments);
  19738. }
  19739. static constructor_() {
  19740. this._nv = null;
  19741. const edges = arguments[0];
  19742. this._nv = new FastNodingValidator(EdgeNodingValidator.toSegmentStrings(edges));
  19743. }
  19744. static toSegmentStrings(edges) {
  19745. const segStrings = new ArrayList();
  19746. for (let i = edges.iterator(); i.hasNext();) {
  19747. const e = i.next();
  19748. segStrings.add(new BasicSegmentString(e.getCoordinates(), e));
  19749. }
  19750. return segStrings;
  19751. }
  19752. static checkValid(edges) {
  19753. const validator = new EdgeNodingValidator(edges);
  19754. validator.checkValid();
  19755. }
  19756. checkValid() {
  19757. this._nv.checkValid();
  19758. }
  19759. }
  19760. class LineBuilder {
  19761. constructor() {
  19762. LineBuilder.constructor_.apply(this, arguments);
  19763. }
  19764. static constructor_() {
  19765. this._op = null;
  19766. this._geometryFactory = null;
  19767. this._ptLocator = null;
  19768. this._lineEdgesList = new ArrayList();
  19769. this._resultLineList = new ArrayList();
  19770. const op = arguments[0],
  19771. geometryFactory = arguments[1],
  19772. ptLocator = arguments[2];
  19773. this._op = op;
  19774. this._geometryFactory = geometryFactory;
  19775. this._ptLocator = ptLocator;
  19776. }
  19777. collectLines(opCode) {
  19778. for (let it = this._op.getGraph().getEdgeEnds().iterator(); it.hasNext();) {
  19779. const de = it.next();
  19780. this.collectLineEdge(de, opCode, this._lineEdgesList);
  19781. this.collectBoundaryTouchEdge(de, opCode, this._lineEdgesList);
  19782. }
  19783. }
  19784. labelIsolatedLine(e, targetIndex) {
  19785. const loc = this._ptLocator.locate(e.getCoordinate(), this._op.getArgGeometry(targetIndex));
  19786. e.getLabel().setLocation(targetIndex, loc);
  19787. }
  19788. build(opCode) {
  19789. this.findCoveredLineEdges();
  19790. this.collectLines(opCode);
  19791. this.buildLines(opCode);
  19792. return this._resultLineList;
  19793. }
  19794. collectLineEdge(de, opCode, edges) {
  19795. const label = de.getLabel();
  19796. const e = de.getEdge();
  19797. if (de.isLineEdge()) if (!de.isVisited() && OverlayOp.isResultOfOp(label, opCode) && !e.isCovered()) {
  19798. edges.add(e);
  19799. de.setVisitedEdge(true);
  19800. }
  19801. }
  19802. findCoveredLineEdges() {
  19803. for (let nodeit = this._op.getGraph().getNodes().iterator(); nodeit.hasNext();) {
  19804. const node = nodeit.next();
  19805. node.getEdges().findCoveredLineEdges();
  19806. }
  19807. for (let it = this._op.getGraph().getEdgeEnds().iterator(); it.hasNext();) {
  19808. const de = it.next();
  19809. const e = de.getEdge();
  19810. if (de.isLineEdge() && !e.isCoveredSet()) {
  19811. const isCovered = this._op.isCoveredByA(de.getCoordinate());
  19812. e.setCovered(isCovered);
  19813. }
  19814. }
  19815. }
  19816. labelIsolatedLines(edgesList) {
  19817. for (let it = edgesList.iterator(); it.hasNext();) {
  19818. const e = it.next();
  19819. const label = e.getLabel();
  19820. if (e.isIsolated()) if (label.isNull(0)) this.labelIsolatedLine(e, 0);else this.labelIsolatedLine(e, 1);
  19821. }
  19822. }
  19823. buildLines(opCode) {
  19824. for (let it = this._lineEdgesList.iterator(); it.hasNext();) {
  19825. const e = it.next();
  19826. const line = this._geometryFactory.createLineString(e.getCoordinates());
  19827. this._resultLineList.add(line);
  19828. e.setInResult(true);
  19829. }
  19830. }
  19831. collectBoundaryTouchEdge(de, opCode, edges) {
  19832. const label = de.getLabel();
  19833. if (de.isLineEdge()) return null;
  19834. if (de.isVisited()) return null;
  19835. if (de.isInteriorAreaEdge()) return null;
  19836. if (de.getEdge().isInResult()) return null;
  19837. Assert.isTrue(!(de.isInResult() || de.getSym().isInResult()) || !de.getEdge().isInResult());
  19838. if (OverlayOp.isResultOfOp(label, opCode) && opCode === OverlayOp.INTERSECTION) {
  19839. edges.add(de.getEdge());
  19840. de.setVisitedEdge(true);
  19841. }
  19842. }
  19843. }
  19844. class PointBuilder {
  19845. constructor() {
  19846. PointBuilder.constructor_.apply(this, arguments);
  19847. }
  19848. static constructor_() {
  19849. this._op = null;
  19850. this._geometryFactory = null;
  19851. this._resultPointList = new ArrayList();
  19852. const op = arguments[0],
  19853. geometryFactory = arguments[1];
  19854. this._op = op;
  19855. this._geometryFactory = geometryFactory;
  19856. }
  19857. filterCoveredNodeToPoint(n) {
  19858. const coord = n.getCoordinate();
  19859. if (!this._op.isCoveredByLA(coord)) {
  19860. const pt = this._geometryFactory.createPoint(coord);
  19861. this._resultPointList.add(pt);
  19862. }
  19863. }
  19864. extractNonCoveredResultNodes(opCode) {
  19865. for (let nodeit = this._op.getGraph().getNodes().iterator(); nodeit.hasNext();) {
  19866. const n = nodeit.next();
  19867. if (n.isInResult()) continue;
  19868. if (n.isIncidentEdgeInResult()) continue;
  19869. if (n.getEdges().getDegree() === 0 || opCode === OverlayOp.INTERSECTION) {
  19870. const label = n.getLabel();
  19871. if (OverlayOp.isResultOfOp(label, opCode)) this.filterCoveredNodeToPoint(n);
  19872. }
  19873. }
  19874. }
  19875. build(opCode) {
  19876. this.extractNonCoveredResultNodes(opCode);
  19877. return this._resultPointList;
  19878. }
  19879. }
  19880. class CommonBits {
  19881. constructor() {
  19882. this._isFirst = true;
  19883. this._commonMantissaBitsCount = 53;
  19884. this._commonBits = new Long();
  19885. this._commonSignExp = null;
  19886. }
  19887. getCommon() {
  19888. return Double.longBitsToDouble(this._commonBits);
  19889. }
  19890. add(num) {
  19891. const numBits = Double.doubleToLongBits(num);
  19892. if (this._isFirst) {
  19893. this._commonBits = numBits;
  19894. this._commonSignExp = CommonBits.signExpBits(this._commonBits);
  19895. this._isFirst = false;
  19896. return null;
  19897. }
  19898. const numSignExp = CommonBits.signExpBits(numBits);
  19899. if (numSignExp !== this._commonSignExp) {
  19900. this._commonBits.high = 0 | 0;
  19901. this._commonBits.low = 0 | 0;
  19902. return null;
  19903. }
  19904. this._commonMantissaBitsCount = CommonBits.numCommonMostSigMantissaBits(this._commonBits, numBits);
  19905. this._commonBits = CommonBits.zeroLowerBits(this._commonBits, 64 - (12 + this._commonMantissaBitsCount));
  19906. }
  19907. toString() {
  19908. if (arguments.length === 1) {
  19909. const bits = arguments[0];
  19910. const x = Double.longBitsToDouble(bits);
  19911. const numStr = Long.toBinaryString(bits);
  19912. const padStr = '0000000000000000000000000000000000000000000000000000000000000000' + numStr;
  19913. const bitStr = padStr.substring(padStr.length - 64);
  19914. const str = bitStr.substring(0, 1) + ' ' + bitStr.substring(1, 12) + '(exp) ' + bitStr.substring(12) + ' [ ' + x + ' ]';
  19915. return str;
  19916. }
  19917. }
  19918. getClass() {
  19919. return CommonBits;
  19920. }
  19921. get interfaces_() {
  19922. return [];
  19923. }
  19924. static getBit(bits, i) {
  19925. const mask = 1 << i % 32;
  19926. if (i < 32) return (bits.low & mask) !== 0 ? 1 : 0;
  19927. return (bits.high & mask) !== 0 ? 1 : 0;
  19928. }
  19929. static signExpBits(num) {
  19930. return num.high >>> 20;
  19931. }
  19932. static zeroLowerBits(bits, nBits) {
  19933. let prop = 'low';
  19934. if (nBits > 32) {
  19935. bits.low = 0 | 0;
  19936. nBits %= 32;
  19937. prop = 'high';
  19938. }
  19939. if (nBits > 0) {
  19940. const mask = nBits < 32 ? ~((1 << nBits) - 1) : 0;
  19941. bits[prop] &= mask;
  19942. }
  19943. return bits;
  19944. }
  19945. static numCommonMostSigMantissaBits(num1, num2) {
  19946. let count = 0;
  19947. for (let i = 52; i >= 0; i--) {
  19948. if (CommonBits.getBit(num1, i) !== CommonBits.getBit(num2, i)) return count;
  19949. count++;
  19950. }
  19951. return 52;
  19952. }
  19953. }
  19954. class CommonBitsRemover {
  19955. constructor() {
  19956. CommonBitsRemover.constructor_.apply(this, arguments);
  19957. }
  19958. static constructor_() {
  19959. this._commonCoord = null;
  19960. this._ccFilter = new CommonCoordinateFilter();
  19961. }
  19962. addCommonBits(geom) {
  19963. const trans = new Translater(this._commonCoord);
  19964. geom.apply(trans);
  19965. geom.geometryChanged();
  19966. }
  19967. removeCommonBits(geom) {
  19968. if (this._commonCoord.x === 0.0 && this._commonCoord.y === 0.0) return geom;
  19969. const invCoord = new Coordinate(this._commonCoord);
  19970. invCoord.x = -invCoord.x;
  19971. invCoord.y = -invCoord.y;
  19972. const trans = new Translater(invCoord);
  19973. geom.apply(trans);
  19974. geom.geometryChanged();
  19975. return geom;
  19976. }
  19977. getCommonCoordinate() {
  19978. return this._commonCoord;
  19979. }
  19980. add(geom) {
  19981. geom.apply(this._ccFilter);
  19982. this._commonCoord = this._ccFilter.getCommonCoordinate();
  19983. }
  19984. }
  19985. class CommonCoordinateFilter {
  19986. constructor() {
  19987. CommonCoordinateFilter.constructor_.apply(this, arguments);
  19988. }
  19989. static constructor_() {
  19990. this._commonBitsX = new CommonBits();
  19991. this._commonBitsY = new CommonBits();
  19992. }
  19993. filter(coord) {
  19994. this._commonBitsX.add(coord.x);
  19995. this._commonBitsY.add(coord.y);
  19996. }
  19997. getCommonCoordinate() {
  19998. return new Coordinate(this._commonBitsX.getCommon(), this._commonBitsY.getCommon());
  19999. }
  20000. get interfaces_() {
  20001. return [CoordinateFilter];
  20002. }
  20003. }
  20004. class Translater {
  20005. constructor() {
  20006. Translater.constructor_.apply(this, arguments);
  20007. }
  20008. static constructor_() {
  20009. this.trans = null;
  20010. const trans = arguments[0];
  20011. this.trans = trans;
  20012. }
  20013. filter(seq, i) {
  20014. const xp = seq.getOrdinate(i, 0) + this.trans.x;
  20015. const yp = seq.getOrdinate(i, 1) + this.trans.y;
  20016. seq.setOrdinate(i, 0, xp);
  20017. seq.setOrdinate(i, 1, yp);
  20018. }
  20019. isDone() {
  20020. return false;
  20021. }
  20022. isGeometryChanged() {
  20023. return true;
  20024. }
  20025. get interfaces_() {
  20026. return [CoordinateSequenceFilter];
  20027. }
  20028. }
  20029. CommonBitsRemover.CommonCoordinateFilter = CommonCoordinateFilter;
  20030. CommonBitsRemover.Translater = Translater;
  20031. class SnapOverlayOp {
  20032. constructor() {
  20033. SnapOverlayOp.constructor_.apply(this, arguments);
  20034. }
  20035. static constructor_() {
  20036. this._geom = new Array(2).fill(null);
  20037. this._snapTolerance = null;
  20038. this._cbr = null;
  20039. const g1 = arguments[0],
  20040. g2 = arguments[1];
  20041. this._geom[0] = g1;
  20042. this._geom[1] = g2;
  20043. this.computeSnapTolerance();
  20044. }
  20045. static overlayOp(g0, g1, opCode) {
  20046. const op = new SnapOverlayOp(g0, g1);
  20047. return op.getResultGeometry(opCode);
  20048. }
  20049. static union(g0, g1) {
  20050. return SnapOverlayOp.overlayOp(g0, g1, OverlayOp.UNION);
  20051. }
  20052. static intersection(g0, g1) {
  20053. return SnapOverlayOp.overlayOp(g0, g1, OverlayOp.INTERSECTION);
  20054. }
  20055. static symDifference(g0, g1) {
  20056. return SnapOverlayOp.overlayOp(g0, g1, OverlayOp.SYMDIFFERENCE);
  20057. }
  20058. static difference(g0, g1) {
  20059. return SnapOverlayOp.overlayOp(g0, g1, OverlayOp.DIFFERENCE);
  20060. }
  20061. selfSnap(geom) {
  20062. const snapper0 = new GeometrySnapper(geom);
  20063. const snapGeom = snapper0.snapTo(geom, this._snapTolerance);
  20064. return snapGeom;
  20065. }
  20066. removeCommonBits(geom) {
  20067. this._cbr = new CommonBitsRemover();
  20068. this._cbr.add(geom[0]);
  20069. this._cbr.add(geom[1]);
  20070. const remGeom = new Array(2).fill(null);
  20071. remGeom[0] = this._cbr.removeCommonBits(geom[0].copy());
  20072. remGeom[1] = this._cbr.removeCommonBits(geom[1].copy());
  20073. return remGeom;
  20074. }
  20075. prepareResult(geom) {
  20076. this._cbr.addCommonBits(geom);
  20077. return geom;
  20078. }
  20079. getResultGeometry(opCode) {
  20080. const prepGeom = this.snap(this._geom);
  20081. const result = OverlayOp.overlayOp(prepGeom[0], prepGeom[1], opCode);
  20082. return this.prepareResult(result);
  20083. }
  20084. checkValid(g) {
  20085. if (!g.isValid()) System.out.println('Snapped geometry is invalid');
  20086. }
  20087. computeSnapTolerance() {
  20088. this._snapTolerance = GeometrySnapper.computeOverlaySnapTolerance(this._geom[0], this._geom[1]);
  20089. }
  20090. snap(geom) {
  20091. const remGeom = this.removeCommonBits(geom);
  20092. const snapGeom = GeometrySnapper.snap(remGeom[0], remGeom[1], this._snapTolerance);
  20093. return snapGeom;
  20094. }
  20095. }
  20096. class SnapIfNeededOverlayOp {
  20097. constructor() {
  20098. SnapIfNeededOverlayOp.constructor_.apply(this, arguments);
  20099. }
  20100. static constructor_() {
  20101. this._geom = new Array(2).fill(null);
  20102. const g1 = arguments[0],
  20103. g2 = arguments[1];
  20104. this._geom[0] = g1;
  20105. this._geom[1] = g2;
  20106. }
  20107. static overlayOp(g0, g1, opCode) {
  20108. const op = new SnapIfNeededOverlayOp(g0, g1);
  20109. return op.getResultGeometry(opCode);
  20110. }
  20111. static union(g0, g1) {
  20112. return SnapIfNeededOverlayOp.overlayOp(g0, g1, OverlayOp.UNION);
  20113. }
  20114. static intersection(g0, g1) {
  20115. return SnapIfNeededOverlayOp.overlayOp(g0, g1, OverlayOp.INTERSECTION);
  20116. }
  20117. static symDifference(g0, g1) {
  20118. return SnapIfNeededOverlayOp.overlayOp(g0, g1, OverlayOp.SYMDIFFERENCE);
  20119. }
  20120. static difference(g0, g1) {
  20121. return SnapIfNeededOverlayOp.overlayOp(g0, g1, OverlayOp.DIFFERENCE);
  20122. }
  20123. getResultGeometry(opCode) {
  20124. let result = null;
  20125. let isSuccess = false;
  20126. let savedException = null;
  20127. try {
  20128. result = OverlayOp.overlayOp(this._geom[0], this._geom[1], opCode);
  20129. const isValid = true;
  20130. if (isValid) isSuccess = true;
  20131. } catch (ex) {
  20132. if (ex instanceof RuntimeException) savedException = ex;else throw ex;
  20133. } finally {}
  20134. if (!isSuccess) try {
  20135. result = SnapOverlayOp.overlayOp(this._geom[0], this._geom[1], opCode);
  20136. } catch (ex) {
  20137. if (ex instanceof RuntimeException) throw savedException;else throw ex;
  20138. } finally {}
  20139. return result;
  20140. }
  20141. }
  20142. class GeometryGraphOperation {
  20143. constructor() {
  20144. GeometryGraphOperation.constructor_.apply(this, arguments);
  20145. }
  20146. static constructor_() {
  20147. this._li = new RobustLineIntersector();
  20148. this._resultPrecisionModel = null;
  20149. this._arg = null;
  20150. if (arguments.length === 1) {
  20151. const g0 = arguments[0];
  20152. this.setComputationPrecision(g0.getPrecisionModel());
  20153. this._arg = new Array(1).fill(null);
  20154. this._arg[0] = new GeometryGraph(0, g0);
  20155. } else if (arguments.length === 2) {
  20156. const g0 = arguments[0],
  20157. g1 = arguments[1];
  20158. GeometryGraphOperation.constructor_.call(this, g0, g1, BoundaryNodeRule.OGC_SFS_BOUNDARY_RULE);
  20159. } else if (arguments.length === 3) {
  20160. const g0 = arguments[0],
  20161. g1 = arguments[1],
  20162. boundaryNodeRule = arguments[2];
  20163. if (g0.getPrecisionModel().compareTo(g1.getPrecisionModel()) >= 0) this.setComputationPrecision(g0.getPrecisionModel());else this.setComputationPrecision(g1.getPrecisionModel());
  20164. this._arg = new Array(2).fill(null);
  20165. this._arg[0] = new GeometryGraph(0, g0, boundaryNodeRule);
  20166. this._arg[1] = new GeometryGraph(1, g1, boundaryNodeRule);
  20167. }
  20168. }
  20169. getArgGeometry(i) {
  20170. return this._arg[i].getGeometry();
  20171. }
  20172. setComputationPrecision(pm) {
  20173. this._resultPrecisionModel = pm;
  20174. this._li.setPrecisionModel(this._resultPrecisionModel);
  20175. }
  20176. }
  20177. class OverlayOp extends GeometryGraphOperation {
  20178. constructor() {
  20179. super();
  20180. OverlayOp.constructor_.apply(this, arguments);
  20181. }
  20182. static constructor_() {
  20183. this._ptLocator = new PointLocator();
  20184. this._geomFact = null;
  20185. this._resultGeom = null;
  20186. this._graph = null;
  20187. this._edgeList = new EdgeList();
  20188. this._resultPolyList = new ArrayList();
  20189. this._resultLineList = new ArrayList();
  20190. this._resultPointList = new ArrayList();
  20191. const g0 = arguments[0],
  20192. g1 = arguments[1];
  20193. GeometryGraphOperation.constructor_.call(this, g0, g1);
  20194. this._graph = new PlanarGraph$1(new OverlayNodeFactory());
  20195. this._geomFact = g0.getFactory();
  20196. }
  20197. static overlayOp(geom0, geom1, opCode) {
  20198. const gov = new OverlayOp(geom0, geom1);
  20199. const geomOv = gov.getResultGeometry(opCode);
  20200. return geomOv;
  20201. }
  20202. static union(geom, other) {
  20203. if (geom.isEmpty() || other.isEmpty()) {
  20204. if (geom.isEmpty() && other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.UNION, geom, other, geom.getFactory());
  20205. if (geom.isEmpty()) return other.copy();
  20206. if (other.isEmpty()) return geom.copy();
  20207. }
  20208. if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments');
  20209. return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.UNION);
  20210. }
  20211. static intersection(geom, other) {
  20212. if (geom.isEmpty() || other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.INTERSECTION, geom, other, geom.getFactory());
  20213. if (geom.isGeometryCollection()) {
  20214. const g2 = other;
  20215. return GeometryCollectionMapper.map(geom, new class {
  20216. get interfaces_() {
  20217. return [MapOp];
  20218. }
  20219. map(g) {
  20220. return OverlayOp.intersection(g, g2);
  20221. }
  20222. }());
  20223. }
  20224. return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.INTERSECTION);
  20225. }
  20226. static symDifference(geom, other) {
  20227. if (geom.isEmpty() || other.isEmpty()) {
  20228. if (geom.isEmpty() && other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.SYMDIFFERENCE, geom, other, geom.getFactory());
  20229. if (geom.isEmpty()) return other.copy();
  20230. if (other.isEmpty()) return geom.copy();
  20231. }
  20232. if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments');
  20233. return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.SYMDIFFERENCE);
  20234. }
  20235. static resultDimension(opCode, g0, g1) {
  20236. const dim0 = g0.getDimension();
  20237. const dim1 = g1.getDimension();
  20238. let resultDimension = -1;
  20239. switch (opCode) {
  20240. case OverlayOp.INTERSECTION:
  20241. resultDimension = Math.min(dim0, dim1);
  20242. break;
  20243. case OverlayOp.UNION:
  20244. resultDimension = Math.max(dim0, dim1);
  20245. break;
  20246. case OverlayOp.DIFFERENCE:
  20247. resultDimension = dim0;
  20248. break;
  20249. case OverlayOp.SYMDIFFERENCE:
  20250. resultDimension = Math.max(dim0, dim1);
  20251. break;
  20252. }
  20253. return resultDimension;
  20254. }
  20255. static createEmptyResult(overlayOpCode, a, b, geomFact) {
  20256. const resultDim = OverlayOp.resultDimension(overlayOpCode, a, b);
  20257. return geomFact.createEmpty(resultDim);
  20258. }
  20259. static difference(geom, other) {
  20260. if (geom.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.DIFFERENCE, geom, other, geom.getFactory());
  20261. if (other.isEmpty()) return geom.copy();
  20262. if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments');
  20263. return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.DIFFERENCE);
  20264. }
  20265. static isResultOfOp() {
  20266. if (arguments.length === 2) {
  20267. const label = arguments[0],
  20268. opCode = arguments[1];
  20269. const loc0 = label.getLocation(0);
  20270. const loc1 = label.getLocation(1);
  20271. return OverlayOp.isResultOfOp(loc0, loc1, opCode);
  20272. } else if (arguments.length === 3) {
  20273. let loc0 = arguments[0],
  20274. loc1 = arguments[1],
  20275. overlayOpCode = arguments[2];
  20276. if (loc0 === Location.BOUNDARY) loc0 = Location.INTERIOR;
  20277. if (loc1 === Location.BOUNDARY) loc1 = Location.INTERIOR;
  20278. switch (overlayOpCode) {
  20279. case OverlayOp.INTERSECTION:
  20280. return loc0 === Location.INTERIOR && loc1 === Location.INTERIOR;
  20281. case OverlayOp.UNION:
  20282. return loc0 === Location.INTERIOR || loc1 === Location.INTERIOR;
  20283. case OverlayOp.DIFFERENCE:
  20284. return loc0 === Location.INTERIOR && loc1 !== Location.INTERIOR;
  20285. case OverlayOp.SYMDIFFERENCE:
  20286. return loc0 === Location.INTERIOR && loc1 !== Location.INTERIOR || loc0 !== Location.INTERIOR && loc1 === Location.INTERIOR;
  20287. }
  20288. return false;
  20289. }
  20290. }
  20291. insertUniqueEdge(e) {
  20292. const existingEdge = this._edgeList.findEqualEdge(e);
  20293. if (existingEdge !== null) {
  20294. const existingLabel = existingEdge.getLabel();
  20295. let labelToMerge = e.getLabel();
  20296. if (!existingEdge.isPointwiseEqual(e)) {
  20297. labelToMerge = new Label(e.getLabel());
  20298. labelToMerge.flip();
  20299. }
  20300. const depth = existingEdge.getDepth();
  20301. if (depth.isNull()) depth.add(existingLabel);
  20302. depth.add(labelToMerge);
  20303. existingLabel.merge(labelToMerge);
  20304. } else {
  20305. this._edgeList.add(e);
  20306. }
  20307. }
  20308. getGraph() {
  20309. return this._graph;
  20310. }
  20311. cancelDuplicateResultEdges() {
  20312. for (let it = this._graph.getEdgeEnds().iterator(); it.hasNext();) {
  20313. const de = it.next();
  20314. const sym = de.getSym();
  20315. if (de.isInResult() && sym.isInResult()) {
  20316. de.setInResult(false);
  20317. sym.setInResult(false);
  20318. }
  20319. }
  20320. }
  20321. isCoveredByLA(coord) {
  20322. if (this.isCovered(coord, this._resultLineList)) return true;
  20323. if (this.isCovered(coord, this._resultPolyList)) return true;
  20324. return false;
  20325. }
  20326. computeGeometry(resultPointList, resultLineList, resultPolyList, opcode) {
  20327. const geomList = new ArrayList();
  20328. geomList.addAll(resultPointList);
  20329. geomList.addAll(resultLineList);
  20330. geomList.addAll(resultPolyList);
  20331. if (geomList.isEmpty()) return OverlayOp.createEmptyResult(opcode, this._arg[0].getGeometry(), this._arg[1].getGeometry(), this._geomFact);
  20332. return this._geomFact.buildGeometry(geomList);
  20333. }
  20334. mergeSymLabels() {
  20335. for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext();) {
  20336. const node = nodeit.next();
  20337. node.getEdges().mergeSymLabels();
  20338. }
  20339. }
  20340. isCovered(coord, geomList) {
  20341. for (let it = geomList.iterator(); it.hasNext();) {
  20342. const geom = it.next();
  20343. const loc = this._ptLocator.locate(coord, geom);
  20344. if (loc !== Location.EXTERIOR) return true;
  20345. }
  20346. return false;
  20347. }
  20348. replaceCollapsedEdges() {
  20349. const newEdges = new ArrayList();
  20350. for (let it = this._edgeList.iterator(); it.hasNext();) {
  20351. const e = it.next();
  20352. if (e.isCollapsed()) {
  20353. it.remove();
  20354. newEdges.add(e.getCollapsedEdge());
  20355. }
  20356. }
  20357. this._edgeList.addAll(newEdges);
  20358. }
  20359. updateNodeLabelling() {
  20360. for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext();) {
  20361. const node = nodeit.next();
  20362. const lbl = node.getEdges().getLabel();
  20363. node.getLabel().merge(lbl);
  20364. }
  20365. }
  20366. getResultGeometry(overlayOpCode) {
  20367. this.computeOverlay(overlayOpCode);
  20368. return this._resultGeom;
  20369. }
  20370. insertUniqueEdges(edges) {
  20371. for (let i = edges.iterator(); i.hasNext();) {
  20372. const e = i.next();
  20373. this.insertUniqueEdge(e);
  20374. }
  20375. }
  20376. computeOverlay(opCode) {
  20377. this.copyPoints(0);
  20378. this.copyPoints(1);
  20379. this._arg[0].computeSelfNodes(this._li, false);
  20380. this._arg[1].computeSelfNodes(this._li, false);
  20381. this._arg[0].computeEdgeIntersections(this._arg[1], this._li, true);
  20382. const baseSplitEdges = new ArrayList();
  20383. this._arg[0].computeSplitEdges(baseSplitEdges);
  20384. this._arg[1].computeSplitEdges(baseSplitEdges);
  20385. this.insertUniqueEdges(baseSplitEdges);
  20386. this.computeLabelsFromDepths();
  20387. this.replaceCollapsedEdges();
  20388. EdgeNodingValidator.checkValid(this._edgeList.getEdges());
  20389. this._graph.addEdges(this._edgeList.getEdges());
  20390. this.computeLabelling();
  20391. this.labelIncompleteNodes();
  20392. this.findResultAreaEdges(opCode);
  20393. this.cancelDuplicateResultEdges();
  20394. const polyBuilder = new PolygonBuilder(this._geomFact);
  20395. polyBuilder.add(this._graph);
  20396. this._resultPolyList = polyBuilder.getPolygons();
  20397. const lineBuilder = new LineBuilder(this, this._geomFact, this._ptLocator);
  20398. this._resultLineList = lineBuilder.build(opCode);
  20399. const pointBuilder = new PointBuilder(this, this._geomFact, this._ptLocator);
  20400. this._resultPointList = pointBuilder.build(opCode);
  20401. this._resultGeom = this.computeGeometry(this._resultPointList, this._resultLineList, this._resultPolyList, opCode);
  20402. }
  20403. labelIncompleteNode(n, targetIndex) {
  20404. const loc = this._ptLocator.locate(n.getCoordinate(), this._arg[targetIndex].getGeometry());
  20405. n.getLabel().setLocation(targetIndex, loc);
  20406. }
  20407. copyPoints(argIndex) {
  20408. for (let i = this._arg[argIndex].getNodeIterator(); i.hasNext();) {
  20409. const graphNode = i.next();
  20410. const newNode = this._graph.addNode(graphNode.getCoordinate());
  20411. newNode.setLabel(argIndex, graphNode.getLabel().getLocation(argIndex));
  20412. }
  20413. }
  20414. findResultAreaEdges(opCode) {
  20415. for (let it = this._graph.getEdgeEnds().iterator(); it.hasNext();) {
  20416. const de = it.next();
  20417. const label = de.getLabel();
  20418. if (label.isArea() && !de.isInteriorAreaEdge() && OverlayOp.isResultOfOp(label.getLocation(0, Position.RIGHT), label.getLocation(1, Position.RIGHT), opCode)) de.setInResult(true);
  20419. }
  20420. }
  20421. computeLabelsFromDepths() {
  20422. for (let it = this._edgeList.iterator(); it.hasNext();) {
  20423. const e = it.next();
  20424. const lbl = e.getLabel();
  20425. const depth = e.getDepth();
  20426. if (!depth.isNull()) {
  20427. depth.normalize();
  20428. for (let i = 0; i < 2; i++) if (!lbl.isNull(i) && lbl.isArea() && !depth.isNull(i)) if (depth.getDelta(i) === 0) {
  20429. lbl.toLine(i);
  20430. } else {
  20431. Assert.isTrue(!depth.isNull(i, Position.LEFT), 'depth of LEFT side has not been initialized');
  20432. lbl.setLocation(i, Position.LEFT, depth.getLocation(i, Position.LEFT));
  20433. Assert.isTrue(!depth.isNull(i, Position.RIGHT), 'depth of RIGHT side has not been initialized');
  20434. lbl.setLocation(i, Position.RIGHT, depth.getLocation(i, Position.RIGHT));
  20435. }
  20436. }
  20437. }
  20438. }
  20439. computeLabelling() {
  20440. for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext();) {
  20441. const node = nodeit.next();
  20442. node.getEdges().computeLabelling(this._arg);
  20443. }
  20444. this.mergeSymLabels();
  20445. this.updateNodeLabelling();
  20446. }
  20447. labelIncompleteNodes() {
  20448. for (let ni = this._graph.getNodes().iterator(); ni.hasNext();) {
  20449. const n = ni.next();
  20450. const label = n.getLabel();
  20451. if (n.isIsolated()) if (label.isNull(0)) this.labelIncompleteNode(n, 0);else this.labelIncompleteNode(n, 1);
  20452. n.getEdges().updateLabelling(label);
  20453. }
  20454. }
  20455. isCoveredByA(coord) {
  20456. if (this.isCovered(coord, this._resultPolyList)) return true;
  20457. return false;
  20458. }
  20459. }
  20460. OverlayOp.INTERSECTION = 1;
  20461. OverlayOp.UNION = 2;
  20462. OverlayOp.DIFFERENCE = 3;
  20463. OverlayOp.SYMDIFFERENCE = 4;
  20464. var overlay = /*#__PURE__*/Object.freeze({
  20465. __proto__: null,
  20466. snap: snap,
  20467. OverlayOp: OverlayOp
  20468. });
  20469. class PolygonizeDirectedEdge extends DirectedEdge {
  20470. constructor() {
  20471. super();
  20472. PolygonizeDirectedEdge.constructor_.apply(this, arguments);
  20473. }
  20474. static constructor_() {
  20475. this._edgeRing = null;
  20476. this._next = null;
  20477. this._label = -1;
  20478. const from = arguments[0],
  20479. to = arguments[1],
  20480. directionPt = arguments[2],
  20481. edgeDirection = arguments[3];
  20482. DirectedEdge.constructor_.call(this, from, to, directionPt, edgeDirection);
  20483. }
  20484. getNext() {
  20485. return this._next;
  20486. }
  20487. isInRing() {
  20488. return this._edgeRing !== null;
  20489. }
  20490. setRing(edgeRing) {
  20491. this._edgeRing = edgeRing;
  20492. }
  20493. setLabel(label) {
  20494. this._label = label;
  20495. }
  20496. getLabel() {
  20497. return this._label;
  20498. }
  20499. setNext(next) {
  20500. this._next = next;
  20501. }
  20502. getRing() {
  20503. return this._edgeRing;
  20504. }
  20505. }
  20506. class PolygonizeEdge extends Edge {
  20507. constructor() {
  20508. super();
  20509. PolygonizeEdge.constructor_.apply(this, arguments);
  20510. }
  20511. static constructor_() {
  20512. this._line = null;
  20513. const line = arguments[0];
  20514. this._line = line;
  20515. }
  20516. getLine() {
  20517. return this._line;
  20518. }
  20519. }
  20520. class ConnectedInteriorTester {
  20521. constructor() {
  20522. ConnectedInteriorTester.constructor_.apply(this, arguments);
  20523. }
  20524. static constructor_() {
  20525. this._geometryFactory = new GeometryFactory();
  20526. this._geomGraph = null;
  20527. this._disconnectedRingcoord = null;
  20528. const geomGraph = arguments[0];
  20529. this._geomGraph = geomGraph;
  20530. }
  20531. static findDifferentPoint(coord, pt) {
  20532. for (let i = 0; i < coord.length; i++) if (!coord[i].equals(pt)) return coord[i];
  20533. return null;
  20534. }
  20535. visitInteriorRing(ring, graph) {
  20536. if (ring.isEmpty()) return null;
  20537. const pts = ring.getCoordinates();
  20538. const pt0 = pts[0];
  20539. const pt1 = ConnectedInteriorTester.findDifferentPoint(pts, pt0);
  20540. const e = graph.findEdgeInSameDirection(pt0, pt1);
  20541. const de = graph.findEdgeEnd(e);
  20542. let intDe = null;
  20543. if (de.getLabel().getLocation(0, Position.RIGHT) === Location.INTERIOR) intDe = de;else if (de.getSym().getLabel().getLocation(0, Position.RIGHT) === Location.INTERIOR) intDe = de.getSym();
  20544. Assert.isTrue(intDe !== null, 'unable to find dirEdge with Interior on RHS');
  20545. this.visitLinkedDirectedEdges(intDe);
  20546. }
  20547. visitShellInteriors(g, graph) {
  20548. if (g instanceof Polygon) {
  20549. const p = g;
  20550. this.visitInteriorRing(p.getExteriorRing(), graph);
  20551. }
  20552. if (g instanceof MultiPolygon) {
  20553. const mp = g;
  20554. for (let i = 0; i < mp.getNumGeometries(); i++) {
  20555. const p = mp.getGeometryN(i);
  20556. this.visitInteriorRing(p.getExteriorRing(), graph);
  20557. }
  20558. }
  20559. }
  20560. getCoordinate() {
  20561. return this._disconnectedRingcoord;
  20562. }
  20563. setInteriorEdgesInResult(graph) {
  20564. for (let it = graph.getEdgeEnds().iterator(); it.hasNext();) {
  20565. const de = it.next();
  20566. if (de.getLabel().getLocation(0, Position.RIGHT) === Location.INTERIOR) de.setInResult(true);
  20567. }
  20568. }
  20569. visitLinkedDirectedEdges(start) {
  20570. const startDe = start;
  20571. let de = start;
  20572. do {
  20573. Assert.isTrue(de !== null, 'found null Directed Edge');
  20574. de.setVisited(true);
  20575. de = de.getNext();
  20576. } while (de !== startDe);
  20577. }
  20578. buildEdgeRings(dirEdges) {
  20579. const edgeRings = new ArrayList();
  20580. for (let it = dirEdges.iterator(); it.hasNext();) {
  20581. const de = it.next();
  20582. if (de.isInResult() && de.getEdgeRing() === null) {
  20583. const er = new MaximalEdgeRing(de, this._geometryFactory);
  20584. er.linkDirectedEdgesForMinimalEdgeRings();
  20585. const minEdgeRings = er.buildMinimalRings();
  20586. edgeRings.addAll(minEdgeRings);
  20587. }
  20588. }
  20589. return edgeRings;
  20590. }
  20591. hasUnvisitedShellEdge(edgeRings) {
  20592. for (let i = 0; i < edgeRings.size(); i++) {
  20593. const er = edgeRings.get(i);
  20594. if (er.isHole()) continue;
  20595. const edges = er.getEdges();
  20596. let de = edges.get(0);
  20597. if (de.getLabel().getLocation(0, Position.RIGHT) !== Location.INTERIOR) continue;
  20598. for (let j = 0; j < edges.size(); j++) {
  20599. de = edges.get(j);
  20600. if (!de.isVisited()) {
  20601. this._disconnectedRingcoord = de.getCoordinate();
  20602. return true;
  20603. }
  20604. }
  20605. }
  20606. return false;
  20607. }
  20608. isInteriorsConnected() {
  20609. const splitEdges = new ArrayList();
  20610. this._geomGraph.computeSplitEdges(splitEdges);
  20611. const graph = new PlanarGraph$1(new OverlayNodeFactory());
  20612. graph.addEdges(splitEdges);
  20613. this.setInteriorEdgesInResult(graph);
  20614. graph.linkResultDirectedEdges();
  20615. const edgeRings = this.buildEdgeRings(graph.getEdgeEnds());
  20616. this.visitShellInteriors(this._geomGraph.getGeometry(), graph);
  20617. return !this.hasUnvisitedShellEdge(edgeRings);
  20618. }
  20619. }
  20620. class EdgeEndBuilder {
  20621. createEdgeEndForNext(edge, l, eiCurr, eiNext) {
  20622. const iNext = eiCurr.segmentIndex + 1;
  20623. if (iNext >= edge.getNumPoints() && eiNext === null) return null;
  20624. let pNext = edge.getCoordinate(iNext);
  20625. if (eiNext !== null && eiNext.segmentIndex === eiCurr.segmentIndex) pNext = eiNext.coord;
  20626. const e = new EdgeEnd(edge, eiCurr.coord, pNext, new Label(edge.getLabel()));
  20627. l.add(e);
  20628. }
  20629. createEdgeEndForPrev(edge, l, eiCurr, eiPrev) {
  20630. let iPrev = eiCurr.segmentIndex;
  20631. if (eiCurr.dist === 0.0) {
  20632. if (iPrev === 0) return null;
  20633. iPrev--;
  20634. }
  20635. let pPrev = edge.getCoordinate(iPrev);
  20636. if (eiPrev !== null && eiPrev.segmentIndex >= iPrev) pPrev = eiPrev.coord;
  20637. const label = new Label(edge.getLabel());
  20638. label.flip();
  20639. const e = new EdgeEnd(edge, eiCurr.coord, pPrev, label);
  20640. l.add(e);
  20641. }
  20642. computeEdgeEnds() {
  20643. if (arguments.length === 1) {
  20644. const edges = arguments[0];
  20645. const l = new ArrayList();
  20646. for (let i = edges; i.hasNext();) {
  20647. const e = i.next();
  20648. this.computeEdgeEnds(e, l);
  20649. }
  20650. return l;
  20651. } else if (arguments.length === 2) {
  20652. const edge = arguments[0],
  20653. l = arguments[1];
  20654. const eiList = edge.getEdgeIntersectionList();
  20655. eiList.addEndpoints();
  20656. const it = eiList.iterator();
  20657. let eiPrev = null;
  20658. let eiCurr = null;
  20659. if (!it.hasNext()) return null;
  20660. let eiNext = it.next();
  20661. do {
  20662. eiPrev = eiCurr;
  20663. eiCurr = eiNext;
  20664. eiNext = null;
  20665. if (it.hasNext()) eiNext = it.next();
  20666. if (eiCurr !== null) {
  20667. this.createEdgeEndForPrev(edge, l, eiCurr, eiPrev);
  20668. this.createEdgeEndForNext(edge, l, eiCurr, eiNext);
  20669. }
  20670. } while (eiCurr !== null);
  20671. }
  20672. }
  20673. }
  20674. class EdgeEndBundle extends EdgeEnd {
  20675. constructor() {
  20676. super();
  20677. EdgeEndBundle.constructor_.apply(this, arguments);
  20678. }
  20679. static constructor_() {
  20680. this._edgeEnds = new ArrayList();
  20681. if (arguments.length === 1) {
  20682. const e = arguments[0];
  20683. EdgeEndBundle.constructor_.call(this, null, e);
  20684. } else if (arguments.length === 2) {
  20685. const e = arguments[1];
  20686. EdgeEnd.constructor_.call(this, e.getEdge(), e.getCoordinate(), e.getDirectedCoordinate(), new Label(e.getLabel()));
  20687. this.insert(e);
  20688. }
  20689. }
  20690. insert(e) {
  20691. this._edgeEnds.add(e);
  20692. }
  20693. print(out) {
  20694. out.println('EdgeEndBundle--> Label: ' + this._label);
  20695. for (let it = this.iterator(); it.hasNext();) {
  20696. const ee = it.next();
  20697. ee.print(out);
  20698. out.println();
  20699. }
  20700. }
  20701. iterator() {
  20702. return this._edgeEnds.iterator();
  20703. }
  20704. getEdgeEnds() {
  20705. return this._edgeEnds;
  20706. }
  20707. computeLabelOn(geomIndex, boundaryNodeRule) {
  20708. let boundaryCount = 0;
  20709. let foundInterior = false;
  20710. for (let it = this.iterator(); it.hasNext();) {
  20711. const e = it.next();
  20712. const loc = e.getLabel().getLocation(geomIndex);
  20713. if (loc === Location.BOUNDARY) boundaryCount++;
  20714. if (loc === Location.INTERIOR) foundInterior = true;
  20715. }
  20716. let loc = Location.NONE;
  20717. if (foundInterior) loc = Location.INTERIOR;
  20718. if (boundaryCount > 0) loc = GeometryGraph.determineBoundary(boundaryNodeRule, boundaryCount);
  20719. this._label.setLocation(geomIndex, loc);
  20720. }
  20721. computeLabelSide(geomIndex, side) {
  20722. for (let it = this.iterator(); it.hasNext();) {
  20723. const e = it.next();
  20724. if (e.getLabel().isArea()) {
  20725. const loc = e.getLabel().getLocation(geomIndex, side);
  20726. if (loc === Location.INTERIOR) {
  20727. this._label.setLocation(geomIndex, side, Location.INTERIOR);
  20728. return null;
  20729. } else if (loc === Location.EXTERIOR) {
  20730. this._label.setLocation(geomIndex, side, Location.EXTERIOR);
  20731. }
  20732. }
  20733. }
  20734. }
  20735. getLabel() {
  20736. return this._label;
  20737. }
  20738. computeLabelSides(geomIndex) {
  20739. this.computeLabelSide(geomIndex, Position.LEFT);
  20740. this.computeLabelSide(geomIndex, Position.RIGHT);
  20741. }
  20742. updateIM(im) {
  20743. Edge$1.updateIM(this._label, im);
  20744. }
  20745. computeLabel(boundaryNodeRule) {
  20746. let isArea = false;
  20747. for (let it = this.iterator(); it.hasNext();) {
  20748. const e = it.next();
  20749. if (e.getLabel().isArea()) isArea = true;
  20750. }
  20751. if (isArea) this._label = new Label(Location.NONE, Location.NONE, Location.NONE);else this._label = new Label(Location.NONE);
  20752. for (let i = 0; i < 2; i++) {
  20753. this.computeLabelOn(i, boundaryNodeRule);
  20754. if (isArea) this.computeLabelSides(i);
  20755. }
  20756. }
  20757. }
  20758. class EdgeEndBundleStar extends EdgeEndStar {
  20759. constructor() {
  20760. super();
  20761. }
  20762. updateIM(im) {
  20763. for (let it = this.iterator(); it.hasNext();) {
  20764. const esb = it.next();
  20765. esb.updateIM(im);
  20766. }
  20767. }
  20768. insert(e) {
  20769. let eb = this._edgeMap.get(e);
  20770. if (eb === null) {
  20771. eb = new EdgeEndBundle(e);
  20772. this.insertEdgeEnd(e, eb);
  20773. } else {
  20774. eb.insert(e);
  20775. }
  20776. }
  20777. }
  20778. class RelateNode extends Node$2 {
  20779. constructor() {
  20780. super();
  20781. RelateNode.constructor_.apply(this, arguments);
  20782. }
  20783. static constructor_() {
  20784. const coord = arguments[0],
  20785. edges = arguments[1];
  20786. Node$2.constructor_.call(this, coord, edges);
  20787. }
  20788. updateIMFromEdges(im) {
  20789. this._edges.updateIM(im);
  20790. }
  20791. computeIM(im) {
  20792. im.setAtLeastIfValid(this._label.getLocation(0), this._label.getLocation(1), 0);
  20793. }
  20794. }
  20795. class RelateNodeFactory extends NodeFactory {
  20796. constructor() {
  20797. super();
  20798. }
  20799. createNode(coord) {
  20800. return new RelateNode(coord, new EdgeEndBundleStar());
  20801. }
  20802. }
  20803. class RelateNodeGraph {
  20804. constructor() {
  20805. RelateNodeGraph.constructor_.apply(this, arguments);
  20806. }
  20807. static constructor_() {
  20808. this._nodes = new NodeMap$1(new RelateNodeFactory());
  20809. }
  20810. insertEdgeEnds(ee) {
  20811. for (let i = ee.iterator(); i.hasNext();) {
  20812. const e = i.next();
  20813. this._nodes.add(e);
  20814. }
  20815. }
  20816. getNodeIterator() {
  20817. return this._nodes.iterator();
  20818. }
  20819. copyNodesAndLabels(geomGraph, argIndex) {
  20820. for (let nodeIt = geomGraph.getNodeIterator(); nodeIt.hasNext();) {
  20821. const graphNode = nodeIt.next();
  20822. const newNode = this._nodes.addNode(graphNode.getCoordinate());
  20823. newNode.setLabel(argIndex, graphNode.getLabel().getLocation(argIndex));
  20824. }
  20825. }
  20826. build(geomGraph) {
  20827. this.computeIntersectionNodes(geomGraph, 0);
  20828. this.copyNodesAndLabels(geomGraph, 0);
  20829. const eeBuilder = new EdgeEndBuilder();
  20830. const eeList = eeBuilder.computeEdgeEnds(geomGraph.getEdgeIterator());
  20831. this.insertEdgeEnds(eeList);
  20832. }
  20833. computeIntersectionNodes(geomGraph, argIndex) {
  20834. for (let edgeIt = geomGraph.getEdgeIterator(); edgeIt.hasNext();) {
  20835. const e = edgeIt.next();
  20836. const eLoc = e.getLabel().getLocation(argIndex);
  20837. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext();) {
  20838. const ei = eiIt.next();
  20839. const n = this._nodes.addNode(ei.coord);
  20840. if (eLoc === Location.BOUNDARY) n.setLabelBoundary(argIndex);else if (n.getLabel().isNull(argIndex)) n.setLabel(argIndex, Location.INTERIOR);
  20841. }
  20842. }
  20843. }
  20844. }
  20845. class ConsistentAreaTester {
  20846. constructor() {
  20847. ConsistentAreaTester.constructor_.apply(this, arguments);
  20848. }
  20849. static constructor_() {
  20850. this._li = new RobustLineIntersector();
  20851. this._geomGraph = null;
  20852. this._nodeGraph = new RelateNodeGraph();
  20853. this._invalidPoint = null;
  20854. const geomGraph = arguments[0];
  20855. this._geomGraph = geomGraph;
  20856. }
  20857. isNodeEdgeAreaLabelsConsistent() {
  20858. for (let nodeIt = this._nodeGraph.getNodeIterator(); nodeIt.hasNext();) {
  20859. const node = nodeIt.next();
  20860. if (!node.getEdges().isAreaLabelsConsistent(this._geomGraph)) {
  20861. this._invalidPoint = node.getCoordinate().copy();
  20862. return false;
  20863. }
  20864. }
  20865. return true;
  20866. }
  20867. getInvalidPoint() {
  20868. return this._invalidPoint;
  20869. }
  20870. hasDuplicateRings() {
  20871. for (let nodeIt = this._nodeGraph.getNodeIterator(); nodeIt.hasNext();) {
  20872. const node = nodeIt.next();
  20873. for (let i = node.getEdges().iterator(); i.hasNext();) {
  20874. const eeb = i.next();
  20875. if (eeb.getEdgeEnds().size() > 1) {
  20876. this._invalidPoint = eeb.getEdge().getCoordinate(0);
  20877. return true;
  20878. }
  20879. }
  20880. }
  20881. return false;
  20882. }
  20883. isNodeConsistentArea() {
  20884. const intersector = this._geomGraph.computeSelfNodes(this._li, true, true);
  20885. if (intersector.hasProperIntersection()) {
  20886. this._invalidPoint = intersector.getProperIntersectionPoint();
  20887. return false;
  20888. }
  20889. this._nodeGraph.build(this._geomGraph);
  20890. return this.isNodeEdgeAreaLabelsConsistent();
  20891. }
  20892. }
  20893. class IndexedNestedRingTester {
  20894. constructor() {
  20895. IndexedNestedRingTester.constructor_.apply(this, arguments);
  20896. }
  20897. static constructor_() {
  20898. this._graph = null;
  20899. this._rings = new ArrayList();
  20900. this._totalEnv = new Envelope();
  20901. this._index = null;
  20902. this._nestedPt = null;
  20903. const graph = arguments[0];
  20904. this._graph = graph;
  20905. }
  20906. buildIndex() {
  20907. this._index = new STRtree();
  20908. for (let i = 0; i < this._rings.size(); i++) {
  20909. const ring = this._rings.get(i);
  20910. const env = ring.getEnvelopeInternal();
  20911. this._index.insert(env, ring);
  20912. }
  20913. }
  20914. getNestedPoint() {
  20915. return this._nestedPt;
  20916. }
  20917. isNonNested() {
  20918. this.buildIndex();
  20919. for (let i = 0; i < this._rings.size(); i++) {
  20920. const innerRing = this._rings.get(i);
  20921. const innerRingPts = innerRing.getCoordinates();
  20922. const results = this._index.query(innerRing.getEnvelopeInternal());
  20923. for (let j = 0; j < results.size(); j++) {
  20924. const searchRing = results.get(j);
  20925. const searchRingPts = searchRing.getCoordinates();
  20926. if (innerRing === searchRing) continue;
  20927. if (!innerRing.getEnvelopeInternal().intersects(searchRing.getEnvelopeInternal())) continue;
  20928. const innerRingPt = IsValidOp.findPtNotNode(innerRingPts, searchRing, this._graph);
  20929. if (innerRingPt === null) continue;
  20930. const isInside = PointLocation.isInRing(innerRingPt, searchRingPts);
  20931. if (isInside) {
  20932. this._nestedPt = innerRingPt;
  20933. return false;
  20934. }
  20935. }
  20936. }
  20937. return true;
  20938. }
  20939. add(ring) {
  20940. this._rings.add(ring);
  20941. this._totalEnv.expandToInclude(ring.getEnvelopeInternal());
  20942. }
  20943. }
  20944. class TopologyValidationError {
  20945. constructor() {
  20946. TopologyValidationError.constructor_.apply(this, arguments);
  20947. }
  20948. static constructor_() {
  20949. this._errorType = null;
  20950. this._pt = null;
  20951. if (arguments.length === 1) {
  20952. const errorType = arguments[0];
  20953. TopologyValidationError.constructor_.call(this, errorType, null);
  20954. } else if (arguments.length === 2) {
  20955. const errorType = arguments[0],
  20956. pt = arguments[1];
  20957. this._errorType = errorType;
  20958. if (pt !== null) this._pt = pt.copy();
  20959. }
  20960. }
  20961. getErrorType() {
  20962. return this._errorType;
  20963. }
  20964. getMessage() {
  20965. return TopologyValidationError.errMsg[this._errorType];
  20966. }
  20967. getCoordinate() {
  20968. return this._pt;
  20969. }
  20970. toString() {
  20971. let locStr = '';
  20972. if (this._pt !== null) locStr = ' at or near point ' + this._pt;
  20973. return this.getMessage() + locStr;
  20974. }
  20975. }
  20976. TopologyValidationError.ERROR = 0;
  20977. TopologyValidationError.REPEATED_POINT = 1;
  20978. TopologyValidationError.HOLE_OUTSIDE_SHELL = 2;
  20979. TopologyValidationError.NESTED_HOLES = 3;
  20980. TopologyValidationError.DISCONNECTED_INTERIOR = 4;
  20981. TopologyValidationError.SELF_INTERSECTION = 5;
  20982. TopologyValidationError.RING_SELF_INTERSECTION = 6;
  20983. TopologyValidationError.NESTED_SHELLS = 7;
  20984. TopologyValidationError.DUPLICATE_RINGS = 8;
  20985. TopologyValidationError.TOO_FEW_POINTS = 9;
  20986. TopologyValidationError.INVALID_COORDINATE = 10;
  20987. TopologyValidationError.RING_NOT_CLOSED = 11;
  20988. TopologyValidationError.errMsg = ['Topology Validation Error', 'Repeated Point', 'Hole lies outside shell', 'Holes are nested', 'Interior is disconnected', 'Self-intersection', 'Ring Self-intersection', 'Nested shells', 'Duplicate Rings', 'Too few distinct points in geometry component', 'Invalid Coordinate', 'Ring is not closed'];
  20989. class IsValidOp {
  20990. constructor() {
  20991. IsValidOp.constructor_.apply(this, arguments);
  20992. }
  20993. static constructor_() {
  20994. this._parentGeometry = null;
  20995. this._isSelfTouchingRingFormingHoleValid = false;
  20996. this._validErr = null;
  20997. const parentGeometry = arguments[0];
  20998. this._parentGeometry = parentGeometry;
  20999. }
  21000. static findPtNotNode(testCoords, searchRing, graph) {
  21001. const searchEdge = graph.findEdge(searchRing);
  21002. const eiList = searchEdge.getEdgeIntersectionList();
  21003. for (let i = 0; i < testCoords.length; i++) {
  21004. const pt = testCoords[i];
  21005. if (!eiList.isIntersection(pt)) return pt;
  21006. }
  21007. return null;
  21008. }
  21009. static isValid() {
  21010. if (arguments[0] instanceof Geometry) {
  21011. const geom = arguments[0];
  21012. const isValidOp = new IsValidOp(geom);
  21013. return isValidOp.isValid();
  21014. } else if (arguments[0] instanceof Coordinate) {
  21015. const coord = arguments[0];
  21016. if (Double.isNaN(coord.x)) return false;
  21017. if (Double.isInfinite(coord.x)) return false;
  21018. if (Double.isNaN(coord.y)) return false;
  21019. if (Double.isInfinite(coord.y)) return false;
  21020. return true;
  21021. }
  21022. }
  21023. checkInvalidCoordinates() {
  21024. if (arguments[0] instanceof Array) {
  21025. const coords = arguments[0];
  21026. for (let i = 0; i < coords.length; i++) if (!IsValidOp.isValid(coords[i])) {
  21027. this._validErr = new TopologyValidationError(TopologyValidationError.INVALID_COORDINATE, coords[i]);
  21028. return null;
  21029. }
  21030. } else if (arguments[0] instanceof Polygon) {
  21031. const poly = arguments[0];
  21032. this.checkInvalidCoordinates(poly.getExteriorRing().getCoordinates());
  21033. if (this._validErr !== null) return null;
  21034. for (let i = 0; i < poly.getNumInteriorRing(); i++) {
  21035. this.checkInvalidCoordinates(poly.getInteriorRingN(i).getCoordinates());
  21036. if (this._validErr !== null) return null;
  21037. }
  21038. }
  21039. }
  21040. checkHolesNotNested(p, graph) {
  21041. if (p.getNumInteriorRing() <= 0) return null;
  21042. const nestedTester = new IndexedNestedRingTester(graph);
  21043. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  21044. const innerHole = p.getInteriorRingN(i);
  21045. if (innerHole.isEmpty()) continue;
  21046. nestedTester.add(innerHole);
  21047. }
  21048. const isNonNested = nestedTester.isNonNested();
  21049. if (!isNonNested) this._validErr = new TopologyValidationError(TopologyValidationError.NESTED_HOLES, nestedTester.getNestedPoint());
  21050. }
  21051. checkConsistentArea(graph) {
  21052. const cat = new ConsistentAreaTester(graph);
  21053. const isValidArea = cat.isNodeConsistentArea();
  21054. if (!isValidArea) {
  21055. this._validErr = new TopologyValidationError(TopologyValidationError.SELF_INTERSECTION, cat.getInvalidPoint());
  21056. return null;
  21057. }
  21058. if (cat.hasDuplicateRings()) this._validErr = new TopologyValidationError(TopologyValidationError.DUPLICATE_RINGS, cat.getInvalidPoint());
  21059. }
  21060. isValid() {
  21061. this.checkValid(this._parentGeometry);
  21062. return this._validErr === null;
  21063. }
  21064. checkShellInsideHole(shell, hole, graph) {
  21065. const shellPts = shell.getCoordinates();
  21066. const holePts = hole.getCoordinates();
  21067. const shellPt = IsValidOp.findPtNotNode(shellPts, hole, graph);
  21068. if (shellPt !== null) {
  21069. const insideHole = PointLocation.isInRing(shellPt, holePts);
  21070. if (!insideHole) return shellPt;
  21071. }
  21072. const holePt = IsValidOp.findPtNotNode(holePts, shell, graph);
  21073. if (holePt !== null) {
  21074. const insideShell = PointLocation.isInRing(holePt, shellPts);
  21075. if (insideShell) return holePt;
  21076. return null;
  21077. }
  21078. Assert.shouldNeverReachHere('points in shell and hole appear to be equal');
  21079. return null;
  21080. }
  21081. checkNoSelfIntersectingRings(graph) {
  21082. for (let i = graph.getEdgeIterator(); i.hasNext();) {
  21083. const e = i.next();
  21084. this.checkNoSelfIntersectingRing(e.getEdgeIntersectionList());
  21085. if (this._validErr !== null) return null;
  21086. }
  21087. }
  21088. checkConnectedInteriors(graph) {
  21089. const cit = new ConnectedInteriorTester(graph);
  21090. if (!cit.isInteriorsConnected()) this._validErr = new TopologyValidationError(TopologyValidationError.DISCONNECTED_INTERIOR, cit.getCoordinate());
  21091. }
  21092. checkNoSelfIntersectingRing(eiList) {
  21093. const nodeSet = new TreeSet();
  21094. let isFirst = true;
  21095. for (let i = eiList.iterator(); i.hasNext();) {
  21096. const ei = i.next();
  21097. if (isFirst) {
  21098. isFirst = false;
  21099. continue;
  21100. }
  21101. if (nodeSet.contains(ei.coord)) {
  21102. this._validErr = new TopologyValidationError(TopologyValidationError.RING_SELF_INTERSECTION, ei.coord);
  21103. return null;
  21104. } else {
  21105. nodeSet.add(ei.coord);
  21106. }
  21107. }
  21108. }
  21109. checkHolesInShell(p, graph) {
  21110. if (p.getNumInteriorRing() <= 0) return null;
  21111. const shell = p.getExteriorRing();
  21112. const isShellEmpty = shell.isEmpty();
  21113. const pir = new IndexedPointInAreaLocator(shell);
  21114. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  21115. const hole = p.getInteriorRingN(i);
  21116. let holePt = null;
  21117. if (hole.isEmpty()) continue;
  21118. holePt = IsValidOp.findPtNotNode(hole.getCoordinates(), shell, graph);
  21119. if (holePt === null) return null;
  21120. const outside = isShellEmpty || Location.EXTERIOR === pir.locate(holePt);
  21121. if (outside) {
  21122. this._validErr = new TopologyValidationError(TopologyValidationError.HOLE_OUTSIDE_SHELL, holePt);
  21123. return null;
  21124. }
  21125. }
  21126. }
  21127. checkTooFewPoints(graph) {
  21128. if (graph.hasTooFewPoints()) {
  21129. this._validErr = new TopologyValidationError(TopologyValidationError.TOO_FEW_POINTS, graph.getInvalidPoint());
  21130. return null;
  21131. }
  21132. }
  21133. getValidationError() {
  21134. this.checkValid(this._parentGeometry);
  21135. return this._validErr;
  21136. }
  21137. checkValid() {
  21138. if (arguments[0] instanceof Point) {
  21139. const g = arguments[0];
  21140. this.checkInvalidCoordinates(g.getCoordinates());
  21141. } else if (arguments[0] instanceof MultiPoint) {
  21142. const g = arguments[0];
  21143. this.checkInvalidCoordinates(g.getCoordinates());
  21144. } else if (arguments[0] instanceof LinearRing) {
  21145. const g = arguments[0];
  21146. this.checkInvalidCoordinates(g.getCoordinates());
  21147. if (this._validErr !== null) return null;
  21148. this.checkClosedRing(g);
  21149. if (this._validErr !== null) return null;
  21150. const graph = new GeometryGraph(0, g);
  21151. this.checkTooFewPoints(graph);
  21152. if (this._validErr !== null) return null;
  21153. const li = new RobustLineIntersector();
  21154. graph.computeSelfNodes(li, true, true);
  21155. this.checkNoSelfIntersectingRings(graph);
  21156. } else if (arguments[0] instanceof LineString) {
  21157. const g = arguments[0];
  21158. this.checkInvalidCoordinates(g.getCoordinates());
  21159. if (this._validErr !== null) return null;
  21160. const graph = new GeometryGraph(0, g);
  21161. this.checkTooFewPoints(graph);
  21162. } else if (arguments[0] instanceof Polygon) {
  21163. const g = arguments[0];
  21164. this.checkInvalidCoordinates(g);
  21165. if (this._validErr !== null) return null;
  21166. this.checkClosedRings(g);
  21167. if (this._validErr !== null) return null;
  21168. const graph = new GeometryGraph(0, g);
  21169. this.checkTooFewPoints(graph);
  21170. if (this._validErr !== null) return null;
  21171. this.checkConsistentArea(graph);
  21172. if (this._validErr !== null) return null;
  21173. if (!this._isSelfTouchingRingFormingHoleValid) {
  21174. this.checkNoSelfIntersectingRings(graph);
  21175. if (this._validErr !== null) return null;
  21176. }
  21177. this.checkHolesInShell(g, graph);
  21178. if (this._validErr !== null) return null;
  21179. this.checkHolesNotNested(g, graph);
  21180. if (this._validErr !== null) return null;
  21181. this.checkConnectedInteriors(graph);
  21182. } else if (arguments[0] instanceof MultiPolygon) {
  21183. const g = arguments[0];
  21184. for (let i = 0; i < g.getNumGeometries(); i++) {
  21185. const p = g.getGeometryN(i);
  21186. this.checkInvalidCoordinates(p);
  21187. if (this._validErr !== null) return null;
  21188. this.checkClosedRings(p);
  21189. if (this._validErr !== null) return null;
  21190. }
  21191. const graph = new GeometryGraph(0, g);
  21192. this.checkTooFewPoints(graph);
  21193. if (this._validErr !== null) return null;
  21194. this.checkConsistentArea(graph);
  21195. if (this._validErr !== null) return null;
  21196. if (!this._isSelfTouchingRingFormingHoleValid) {
  21197. this.checkNoSelfIntersectingRings(graph);
  21198. if (this._validErr !== null) return null;
  21199. }
  21200. for (let i = 0; i < g.getNumGeometries(); i++) {
  21201. const p = g.getGeometryN(i);
  21202. this.checkHolesInShell(p, graph);
  21203. if (this._validErr !== null) return null;
  21204. }
  21205. for (let i = 0; i < g.getNumGeometries(); i++) {
  21206. const p = g.getGeometryN(i);
  21207. this.checkHolesNotNested(p, graph);
  21208. if (this._validErr !== null) return null;
  21209. }
  21210. this.checkShellsNotNested(g, graph);
  21211. if (this._validErr !== null) return null;
  21212. this.checkConnectedInteriors(graph);
  21213. } else if (arguments[0] instanceof GeometryCollection) {
  21214. const gc = arguments[0];
  21215. for (let i = 0; i < gc.getNumGeometries(); i++) {
  21216. const g = gc.getGeometryN(i);
  21217. this.checkValid(g);
  21218. if (this._validErr !== null) return null;
  21219. }
  21220. } else if (arguments[0] instanceof Geometry) {
  21221. const g = arguments[0];
  21222. this._validErr = null;
  21223. if (g.isEmpty()) return null;
  21224. if (g instanceof Point) this.checkValid(g);else if (g instanceof MultiPoint) this.checkValid(g);else if (g instanceof LinearRing) this.checkValid(g);else if (g instanceof LineString) this.checkValid(g);else if (g instanceof Polygon) this.checkValid(g);else if (g instanceof MultiPolygon) this.checkValid(g);else if (g instanceof GeometryCollection) this.checkValid(g);else throw new UnsupportedOperationException(g.getGeometryType());
  21225. }
  21226. }
  21227. setSelfTouchingRingFormingHoleValid(isValid) {
  21228. this._isSelfTouchingRingFormingHoleValid = isValid;
  21229. }
  21230. checkShellNotNested(shell, p, graph) {
  21231. const shellPts = shell.getCoordinates();
  21232. const polyShell = p.getExteriorRing();
  21233. if (polyShell.isEmpty()) return null;
  21234. const polyPts = polyShell.getCoordinates();
  21235. const shellPt = IsValidOp.findPtNotNode(shellPts, polyShell, graph);
  21236. if (shellPt === null) return null;
  21237. const insidePolyShell = PointLocation.isInRing(shellPt, polyPts);
  21238. if (!insidePolyShell) return null;
  21239. if (p.getNumInteriorRing() <= 0) {
  21240. this._validErr = new TopologyValidationError(TopologyValidationError.NESTED_SHELLS, shellPt);
  21241. return null;
  21242. }
  21243. let badNestedPt = null;
  21244. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  21245. const hole = p.getInteriorRingN(i);
  21246. badNestedPt = this.checkShellInsideHole(shell, hole, graph);
  21247. if (badNestedPt === null) return null;
  21248. }
  21249. this._validErr = new TopologyValidationError(TopologyValidationError.NESTED_SHELLS, badNestedPt);
  21250. }
  21251. checkClosedRings(poly) {
  21252. this.checkClosedRing(poly.getExteriorRing());
  21253. if (this._validErr !== null) return null;
  21254. for (let i = 0; i < poly.getNumInteriorRing(); i++) {
  21255. this.checkClosedRing(poly.getInteriorRingN(i));
  21256. if (this._validErr !== null) return null;
  21257. }
  21258. }
  21259. checkClosedRing(ring) {
  21260. if (ring.isEmpty()) return null;
  21261. if (!ring.isClosed()) {
  21262. let pt = null;
  21263. if (ring.getNumPoints() >= 1) pt = ring.getCoordinateN(0);
  21264. this._validErr = new TopologyValidationError(TopologyValidationError.RING_NOT_CLOSED, pt);
  21265. }
  21266. }
  21267. checkShellsNotNested(mp, graph) {
  21268. for (let i = 0; i < mp.getNumGeometries(); i++) {
  21269. const p = mp.getGeometryN(i);
  21270. const shell = p.getExteriorRing();
  21271. for (let j = 0; j < mp.getNumGeometries(); j++) {
  21272. if (i === j) continue;
  21273. const p2 = mp.getGeometryN(j);
  21274. this.checkShellNotNested(shell, p2, graph);
  21275. if (this._validErr !== null) return null;
  21276. }
  21277. }
  21278. }
  21279. }
  21280. class EdgeRing {
  21281. constructor() {
  21282. EdgeRing.constructor_.apply(this, arguments);
  21283. }
  21284. static constructor_() {
  21285. this._factory = null;
  21286. this._deList = new ArrayList();
  21287. this._lowestEdge = null;
  21288. this._ring = null;
  21289. this._locator = null;
  21290. this._ringPts = null;
  21291. this._holes = null;
  21292. this._shell = null;
  21293. this._isHole = null;
  21294. this._isProcessed = false;
  21295. this._isIncludedSet = false;
  21296. this._isIncluded = false;
  21297. const factory = arguments[0];
  21298. this._factory = factory;
  21299. }
  21300. static findDirEdgesInRing(startDE) {
  21301. let de = startDE;
  21302. const edges = new ArrayList();
  21303. do {
  21304. edges.add(de);
  21305. de = de.getNext();
  21306. Assert.isTrue(de !== null, 'found null DE in ring');
  21307. Assert.isTrue(de === startDE || !de.isInRing(), 'found DE already in ring');
  21308. } while (de !== startDE);
  21309. return edges;
  21310. }
  21311. static addEdge(coords, isForward, coordList) {
  21312. if (isForward) for (let i = 0; i < coords.length; i++) coordList.add(coords[i], false);else for (let i = coords.length - 1; i >= 0; i--) coordList.add(coords[i], false);
  21313. }
  21314. static findEdgeRingContaining(testEr, erList) {
  21315. const testRing = testEr.getRing();
  21316. const testEnv = testRing.getEnvelopeInternal();
  21317. let testPt = testRing.getCoordinateN(0);
  21318. let minRing = null;
  21319. let minRingEnv = null;
  21320. for (let it = erList.iterator(); it.hasNext();) {
  21321. const tryEdgeRing = it.next();
  21322. const tryRing = tryEdgeRing.getRing();
  21323. const tryShellEnv = tryRing.getEnvelopeInternal();
  21324. if (tryShellEnv.equals(testEnv)) continue;
  21325. if (!tryShellEnv.contains(testEnv)) continue;
  21326. testPt = CoordinateArrays.ptNotInList(testRing.getCoordinates(), tryEdgeRing.getCoordinates());
  21327. const isContained = tryEdgeRing.isInRing(testPt);
  21328. if (isContained) if (minRing === null || minRingEnv.contains(tryShellEnv)) {
  21329. minRing = tryEdgeRing;
  21330. minRingEnv = minRing.getRing().getEnvelopeInternal();
  21331. }
  21332. }
  21333. return minRing;
  21334. }
  21335. isIncluded() {
  21336. return this._isIncluded;
  21337. }
  21338. getCoordinates() {
  21339. if (this._ringPts === null) {
  21340. const coordList = new CoordinateList();
  21341. for (let i = this._deList.iterator(); i.hasNext();) {
  21342. const de = i.next();
  21343. const edge = de.getEdge();
  21344. EdgeRing.addEdge(edge.getLine().getCoordinates(), de.getEdgeDirection(), coordList);
  21345. }
  21346. this._ringPts = coordList.toCoordinateArray();
  21347. }
  21348. return this._ringPts;
  21349. }
  21350. isIncludedSet() {
  21351. return this._isIncludedSet;
  21352. }
  21353. isValid() {
  21354. this.getCoordinates();
  21355. if (this._ringPts.length <= 3) return false;
  21356. this.getRing();
  21357. return IsValidOp.isValid(this._ring);
  21358. }
  21359. build(startDE) {
  21360. let de = startDE;
  21361. do {
  21362. this.add(de);
  21363. de.setRing(this);
  21364. de = de.getNext();
  21365. Assert.isTrue(de !== null, 'found null DE in ring');
  21366. Assert.isTrue(de === startDE || !de.isInRing(), 'found DE already in ring');
  21367. } while (de !== startDE);
  21368. }
  21369. isInRing(pt) {
  21370. return Location.EXTERIOR !== this.getLocator().locate(pt);
  21371. }
  21372. isOuterHole() {
  21373. if (!this._isHole) return false;
  21374. return !this.hasShell();
  21375. }
  21376. getPolygon() {
  21377. let holeLR = null;
  21378. if (this._holes !== null) {
  21379. holeLR = new Array(this._holes.size()).fill(null);
  21380. for (let i = 0; i < this._holes.size(); i++) holeLR[i] = this._holes.get(i);
  21381. }
  21382. const poly = this._factory.createPolygon(this._ring, holeLR);
  21383. return poly;
  21384. }
  21385. isHole() {
  21386. return this._isHole;
  21387. }
  21388. isProcessed() {
  21389. return this._isProcessed;
  21390. }
  21391. addHole() {
  21392. if (arguments[0] instanceof LinearRing) {
  21393. const hole = arguments[0];
  21394. if (this._holes === null) this._holes = new ArrayList();
  21395. this._holes.add(hole);
  21396. } else if (arguments[0] instanceof EdgeRing) {
  21397. const holeER = arguments[0];
  21398. holeER.setShell(this);
  21399. const hole = holeER.getRing();
  21400. if (this._holes === null) this._holes = new ArrayList();
  21401. this._holes.add(hole);
  21402. }
  21403. }
  21404. setIncluded(isIncluded) {
  21405. this._isIncluded = isIncluded;
  21406. this._isIncludedSet = true;
  21407. }
  21408. getOuterHole() {
  21409. if (this.isHole()) return null;
  21410. for (let i = 0; i < this._deList.size(); i++) {
  21411. const de = this._deList.get(i);
  21412. const adjRing = de.getSym().getRing();
  21413. if (adjRing.isOuterHole()) return adjRing;
  21414. }
  21415. return null;
  21416. }
  21417. computeHole() {
  21418. const ring = this.getRing();
  21419. this._isHole = Orientation.isCCW(ring.getCoordinates());
  21420. }
  21421. hasShell() {
  21422. return this._shell !== null;
  21423. }
  21424. isOuterShell() {
  21425. return this.getOuterHole() !== null;
  21426. }
  21427. getLineString() {
  21428. this.getCoordinates();
  21429. return this._factory.createLineString(this._ringPts);
  21430. }
  21431. toString() {
  21432. return WKTWriter.toLineString(new CoordinateArraySequence(this.getCoordinates()));
  21433. }
  21434. getLocator() {
  21435. if (this._locator === null) this._locator = new IndexedPointInAreaLocator(this.getRing());
  21436. return this._locator;
  21437. }
  21438. getShell() {
  21439. if (this.isHole()) return this._shell;
  21440. return this;
  21441. }
  21442. add(de) {
  21443. this._deList.add(de);
  21444. }
  21445. getRing() {
  21446. if (this._ring !== null) return this._ring;
  21447. this.getCoordinates();
  21448. if (this._ringPts.length < 3) System.out.println(this._ringPts);
  21449. try {
  21450. this._ring = this._factory.createLinearRing(this._ringPts);
  21451. } catch (ex) {
  21452. if (ex instanceof Exception) System.out.println(this._ringPts);else throw ex;
  21453. } finally {}
  21454. return this._ring;
  21455. }
  21456. updateIncluded() {
  21457. if (this.isHole()) return null;
  21458. for (let i = 0; i < this._deList.size(); i++) {
  21459. const de = this._deList.get(i);
  21460. const adjShell = de.getSym().getRing().getShell();
  21461. if (adjShell !== null && adjShell.isIncludedSet()) {
  21462. this.setIncluded(!adjShell.isIncluded());
  21463. return null;
  21464. }
  21465. }
  21466. }
  21467. setShell(shell) {
  21468. this._shell = shell;
  21469. }
  21470. setProcessed(isProcessed) {
  21471. this._isProcessed = isProcessed;
  21472. }
  21473. }
  21474. class EnvelopeComparator {
  21475. compare(obj0, obj1) {
  21476. const r0 = obj0;
  21477. const r1 = obj1;
  21478. return r0.getRing().getEnvelope().compareTo(r1.getRing().getEnvelope());
  21479. }
  21480. get interfaces_() {
  21481. return [Comparator];
  21482. }
  21483. }
  21484. EdgeRing.EnvelopeComparator = EnvelopeComparator;
  21485. class PolygonizeGraph extends PlanarGraph {
  21486. constructor() {
  21487. super();
  21488. PolygonizeGraph.constructor_.apply(this, arguments);
  21489. }
  21490. static constructor_() {
  21491. this._factory = null;
  21492. const factory = arguments[0];
  21493. this._factory = factory;
  21494. }
  21495. static findLabeledEdgeRings(dirEdges) {
  21496. const edgeRingStarts = new ArrayList();
  21497. let currLabel = 1;
  21498. for (let i = dirEdges.iterator(); i.hasNext();) {
  21499. const de = i.next();
  21500. if (de.isMarked()) continue;
  21501. if (de.getLabel() >= 0) continue;
  21502. edgeRingStarts.add(de);
  21503. const edges = EdgeRing.findDirEdgesInRing(de);
  21504. PolygonizeGraph.label(edges, currLabel);
  21505. currLabel++;
  21506. }
  21507. return edgeRingStarts;
  21508. }
  21509. static getDegreeNonDeleted(node) {
  21510. const edges = node.getOutEdges().getEdges();
  21511. let degree = 0;
  21512. for (let i = edges.iterator(); i.hasNext();) {
  21513. const de = i.next();
  21514. if (!de.isMarked()) degree++;
  21515. }
  21516. return degree;
  21517. }
  21518. static deleteAllEdges(node) {
  21519. const edges = node.getOutEdges().getEdges();
  21520. for (let i = edges.iterator(); i.hasNext();) {
  21521. const de = i.next();
  21522. de.setMarked(true);
  21523. const sym = de.getSym();
  21524. if (sym !== null) sym.setMarked(true);
  21525. }
  21526. }
  21527. static label(dirEdges, label) {
  21528. for (let i = dirEdges.iterator(); i.hasNext();) {
  21529. const de = i.next();
  21530. de.setLabel(label);
  21531. }
  21532. }
  21533. static computeNextCWEdges(node) {
  21534. const deStar = node.getOutEdges();
  21535. let startDE = null;
  21536. let prevDE = null;
  21537. for (let i = deStar.getEdges().iterator(); i.hasNext();) {
  21538. const outDE = i.next();
  21539. if (outDE.isMarked()) continue;
  21540. if (startDE === null) startDE = outDE;
  21541. if (prevDE !== null) {
  21542. const sym = prevDE.getSym();
  21543. sym.setNext(outDE);
  21544. }
  21545. prevDE = outDE;
  21546. }
  21547. if (prevDE !== null) {
  21548. const sym = prevDE.getSym();
  21549. sym.setNext(startDE);
  21550. }
  21551. }
  21552. static computeNextCCWEdges(node, label) {
  21553. const deStar = node.getOutEdges();
  21554. let firstOutDE = null;
  21555. let prevInDE = null;
  21556. const edges = deStar.getEdges();
  21557. for (let i = edges.size() - 1; i >= 0; i--) {
  21558. const de = edges.get(i);
  21559. const sym = de.getSym();
  21560. let outDE = null;
  21561. if (de.getLabel() === label) outDE = de;
  21562. let inDE = null;
  21563. if (sym.getLabel() === label) inDE = sym;
  21564. if (outDE === null && inDE === null) continue;
  21565. if (inDE !== null) prevInDE = inDE;
  21566. if (outDE !== null) {
  21567. if (prevInDE !== null) {
  21568. prevInDE.setNext(outDE);
  21569. prevInDE = null;
  21570. }
  21571. if (firstOutDE === null) firstOutDE = outDE;
  21572. }
  21573. }
  21574. if (prevInDE !== null) {
  21575. Assert.isTrue(firstOutDE !== null);
  21576. prevInDE.setNext(firstOutDE);
  21577. }
  21578. }
  21579. static getDegree(node, label) {
  21580. const edges = node.getOutEdges().getEdges();
  21581. let degree = 0;
  21582. for (let i = edges.iterator(); i.hasNext();) {
  21583. const de = i.next();
  21584. if (de.getLabel() === label) degree++;
  21585. }
  21586. return degree;
  21587. }
  21588. static findIntersectionNodes(startDE, label) {
  21589. let de = startDE;
  21590. let intNodes = null;
  21591. do {
  21592. const node = de.getFromNode();
  21593. if (PolygonizeGraph.getDegree(node, label) > 1) {
  21594. if (intNodes === null) intNodes = new ArrayList();
  21595. intNodes.add(node);
  21596. }
  21597. de = de.getNext();
  21598. Assert.isTrue(de !== null, 'found null DE in ring');
  21599. Assert.isTrue(de === startDE || !de.isInRing(), 'found DE already in ring');
  21600. } while (de !== startDE);
  21601. return intNodes;
  21602. }
  21603. findEdgeRing(startDE) {
  21604. const er = new EdgeRing(this._factory);
  21605. er.build(startDE);
  21606. return er;
  21607. }
  21608. computeDepthParity() {
  21609. if (arguments.length === 0) {
  21610. while (true) {
  21611. return null;
  21612. }
  21613. }
  21614. }
  21615. computeNextCWEdges() {
  21616. for (let iNode = this.nodeIterator(); iNode.hasNext();) {
  21617. const node = iNode.next();
  21618. PolygonizeGraph.computeNextCWEdges(node);
  21619. }
  21620. }
  21621. addEdge(line) {
  21622. if (line.isEmpty()) return null;
  21623. const linePts = CoordinateArrays.removeRepeatedPoints(line.getCoordinates());
  21624. if (linePts.length < 2) return null;
  21625. const startPt = linePts[0];
  21626. const endPt = linePts[linePts.length - 1];
  21627. const nStart = this.getNode(startPt);
  21628. const nEnd = this.getNode(endPt);
  21629. const de0 = new PolygonizeDirectedEdge(nStart, nEnd, linePts[1], true);
  21630. const de1 = new PolygonizeDirectedEdge(nEnd, nStart, linePts[linePts.length - 2], false);
  21631. const edge = new PolygonizeEdge(line);
  21632. edge.setDirectedEdges(de0, de1);
  21633. this.add(edge);
  21634. }
  21635. deleteCutEdges() {
  21636. this.computeNextCWEdges();
  21637. PolygonizeGraph.findLabeledEdgeRings(this._dirEdges);
  21638. const cutLines = new ArrayList();
  21639. for (let i = this._dirEdges.iterator(); i.hasNext();) {
  21640. const de = i.next();
  21641. if (de.isMarked()) continue;
  21642. const sym = de.getSym();
  21643. if (de.getLabel() === sym.getLabel()) {
  21644. de.setMarked(true);
  21645. sym.setMarked(true);
  21646. const e = de.getEdge();
  21647. cutLines.add(e.getLine());
  21648. }
  21649. }
  21650. return cutLines;
  21651. }
  21652. getEdgeRings() {
  21653. this.computeNextCWEdges();
  21654. PolygonizeGraph.label(this._dirEdges, -1);
  21655. const maximalRings = PolygonizeGraph.findLabeledEdgeRings(this._dirEdges);
  21656. this.convertMaximalToMinimalEdgeRings(maximalRings);
  21657. const edgeRingList = new ArrayList();
  21658. for (let i = this._dirEdges.iterator(); i.hasNext();) {
  21659. const de = i.next();
  21660. if (de.isMarked()) continue;
  21661. if (de.isInRing()) continue;
  21662. const er = this.findEdgeRing(de);
  21663. edgeRingList.add(er);
  21664. }
  21665. return edgeRingList;
  21666. }
  21667. getNode(pt) {
  21668. let node = this.findNode(pt);
  21669. if (node === null) {
  21670. node = new Node(pt);
  21671. this.add(node);
  21672. }
  21673. return node;
  21674. }
  21675. convertMaximalToMinimalEdgeRings(ringEdges) {
  21676. for (let i = ringEdges.iterator(); i.hasNext();) {
  21677. const de = i.next();
  21678. const label = de.getLabel();
  21679. const intNodes = PolygonizeGraph.findIntersectionNodes(de, label);
  21680. if (intNodes === null) continue;
  21681. for (let iNode = intNodes.iterator(); iNode.hasNext();) {
  21682. const node = iNode.next();
  21683. PolygonizeGraph.computeNextCCWEdges(node, label);
  21684. }
  21685. }
  21686. }
  21687. deleteDangles() {
  21688. const nodesToRemove = this.findNodesOfDegree(1);
  21689. const dangleLines = new HashSet();
  21690. const nodeStack = new Stack();
  21691. for (let i = nodesToRemove.iterator(); i.hasNext();) nodeStack.push(i.next());
  21692. while (!nodeStack.isEmpty()) {
  21693. const node = nodeStack.pop();
  21694. PolygonizeGraph.deleteAllEdges(node);
  21695. const nodeOutEdges = node.getOutEdges().getEdges();
  21696. for (let i = nodeOutEdges.iterator(); i.hasNext();) {
  21697. const de = i.next();
  21698. de.setMarked(true);
  21699. const sym = de.getSym();
  21700. if (sym !== null) sym.setMarked(true);
  21701. const e = de.getEdge();
  21702. dangleLines.add(e.getLine());
  21703. const toNode = de.getToNode();
  21704. if (PolygonizeGraph.getDegreeNonDeleted(toNode) === 1) nodeStack.push(toNode);
  21705. }
  21706. }
  21707. return dangleLines;
  21708. }
  21709. }
  21710. class HoleAssigner {
  21711. constructor() {
  21712. HoleAssigner.constructor_.apply(this, arguments);
  21713. }
  21714. static constructor_() {
  21715. this._shells = null;
  21716. this._shellIndex = null;
  21717. const shells = arguments[0];
  21718. this._shells = shells;
  21719. this.buildIndex();
  21720. }
  21721. static assignHolesToShells(holes, shells) {
  21722. const assigner = new HoleAssigner(shells);
  21723. assigner.assignHolesToShells(holes);
  21724. }
  21725. assignHolesToShells(holeList) {
  21726. for (let i = holeList.iterator(); i.hasNext();) {
  21727. const holeER = i.next();
  21728. this.assignHoleToShell(holeER);
  21729. }
  21730. }
  21731. buildIndex() {
  21732. this._shellIndex = new STRtree();
  21733. for (const shell of this._shells) this._shellIndex.insert(shell.getRing().getEnvelopeInternal(), shell);
  21734. }
  21735. queryOverlappingShells(ringEnv) {
  21736. return this._shellIndex.query(ringEnv);
  21737. }
  21738. findShellContaining(testEr) {
  21739. const testEnv = testEr.getRing().getEnvelopeInternal();
  21740. const candidateShells = this.queryOverlappingShells(testEnv);
  21741. return EdgeRing.findEdgeRingContaining(testEr, candidateShells);
  21742. }
  21743. assignHoleToShell(holeER) {
  21744. const shell = this.findShellContaining(holeER);
  21745. if (shell !== null) shell.addHole(holeER);
  21746. }
  21747. }
  21748. class Polygonizer {
  21749. constructor() {
  21750. Polygonizer.constructor_.apply(this, arguments);
  21751. }
  21752. static constructor_() {
  21753. this._lineStringAdder = new LineStringAdder(this);
  21754. this._graph = null;
  21755. this._dangles = new ArrayList();
  21756. this._cutEdges = new ArrayList();
  21757. this._invalidRingLines = new ArrayList();
  21758. this._holeList = null;
  21759. this._shellList = null;
  21760. this._polyList = null;
  21761. this._isCheckingRingsValid = true;
  21762. this._extractOnlyPolygonal = null;
  21763. this._geomFactory = null;
  21764. if (arguments.length === 0) {
  21765. Polygonizer.constructor_.call(this, false);
  21766. } else if (arguments.length === 1) {
  21767. const extractOnlyPolygonal = arguments[0];
  21768. this._extractOnlyPolygonal = extractOnlyPolygonal;
  21769. }
  21770. }
  21771. static extractPolygons(shellList, includeAll) {
  21772. const polyList = new ArrayList();
  21773. for (let i = shellList.iterator(); i.hasNext();) {
  21774. const er = i.next();
  21775. if (includeAll || er.isIncluded()) polyList.add(er.getPolygon());
  21776. }
  21777. return polyList;
  21778. }
  21779. static findOuterShells(shellList) {
  21780. for (let i = shellList.iterator(); i.hasNext();) {
  21781. const er = i.next();
  21782. const outerHoleER = er.getOuterHole();
  21783. if (outerHoleER !== null && !outerHoleER.isProcessed()) {
  21784. er.setIncluded(true);
  21785. outerHoleER.setProcessed(true);
  21786. }
  21787. }
  21788. }
  21789. static findDisjointShells(shellList) {
  21790. Polygonizer.findOuterShells(shellList);
  21791. let isMoreToScan = null;
  21792. do {
  21793. isMoreToScan = false;
  21794. for (let i = shellList.iterator(); i.hasNext();) {
  21795. const er = i.next();
  21796. if (er.isIncludedSet()) continue;
  21797. er.updateIncluded();
  21798. if (!er.isIncludedSet()) isMoreToScan = true;
  21799. }
  21800. } while (isMoreToScan);
  21801. }
  21802. getGeometry() {
  21803. if (this._geomFactory === null) this._geomFactory = new GeometryFactory();
  21804. this.polygonize();
  21805. if (this._extractOnlyPolygonal) return this._geomFactory.buildGeometry(this._polyList);
  21806. return this._geomFactory.createGeometryCollection(GeometryFactory.toGeometryArray(this._polyList));
  21807. }
  21808. getInvalidRingLines() {
  21809. this.polygonize();
  21810. return this._invalidRingLines;
  21811. }
  21812. findValidRings(edgeRingList, validEdgeRingList, invalidRingList) {
  21813. for (let i = edgeRingList.iterator(); i.hasNext();) {
  21814. const er = i.next();
  21815. if (er.isValid()) validEdgeRingList.add(er);else invalidRingList.add(er.getLineString());
  21816. }
  21817. }
  21818. polygonize() {
  21819. if (this._polyList !== null) return null;
  21820. this._polyList = new ArrayList();
  21821. if (this._graph === null) return null;
  21822. this._dangles = this._graph.deleteDangles();
  21823. this._cutEdges = this._graph.deleteCutEdges();
  21824. const edgeRingList = this._graph.getEdgeRings();
  21825. let validEdgeRingList = new ArrayList();
  21826. this._invalidRingLines = new ArrayList();
  21827. if (this._isCheckingRingsValid) this.findValidRings(edgeRingList, validEdgeRingList, this._invalidRingLines);else validEdgeRingList = edgeRingList;
  21828. this.findShellsAndHoles(validEdgeRingList);
  21829. HoleAssigner.assignHolesToShells(this._holeList, this._shellList);
  21830. Collections.sort(this._shellList, new EdgeRing.EnvelopeComparator());
  21831. let includeAll = true;
  21832. if (this._extractOnlyPolygonal) {
  21833. Polygonizer.findDisjointShells(this._shellList);
  21834. includeAll = false;
  21835. }
  21836. this._polyList = Polygonizer.extractPolygons(this._shellList, includeAll);
  21837. }
  21838. getDangles() {
  21839. this.polygonize();
  21840. return this._dangles;
  21841. }
  21842. getCutEdges() {
  21843. this.polygonize();
  21844. return this._cutEdges;
  21845. }
  21846. getPolygons() {
  21847. this.polygonize();
  21848. return this._polyList;
  21849. }
  21850. add() {
  21851. if (hasInterface(arguments[0], Collection)) {
  21852. const geomList = arguments[0];
  21853. for (let i = geomList.iterator(); i.hasNext();) {
  21854. const geometry = i.next();
  21855. this.add(geometry);
  21856. }
  21857. } else if (arguments[0] instanceof LineString) {
  21858. const line = arguments[0];
  21859. this._geomFactory = line.getFactory();
  21860. if (this._graph === null) this._graph = new PolygonizeGraph(this._geomFactory);
  21861. this._graph.addEdge(line);
  21862. } else if (arguments[0] instanceof Geometry) {
  21863. const g = arguments[0];
  21864. g.apply(this._lineStringAdder);
  21865. }
  21866. }
  21867. setCheckRingsValid(isCheckingRingsValid) {
  21868. this._isCheckingRingsValid = isCheckingRingsValid;
  21869. }
  21870. findShellsAndHoles(edgeRingList) {
  21871. this._holeList = new ArrayList();
  21872. this._shellList = new ArrayList();
  21873. for (let i = edgeRingList.iterator(); i.hasNext();) {
  21874. const er = i.next();
  21875. er.computeHole();
  21876. if (er.isHole()) this._holeList.add(er);else this._shellList.add(er);
  21877. }
  21878. }
  21879. }
  21880. class LineStringAdder {
  21881. constructor() {
  21882. LineStringAdder.constructor_.apply(this, arguments);
  21883. }
  21884. static constructor_() {
  21885. this.p = null;
  21886. const p = arguments[0];
  21887. this.p = p;
  21888. }
  21889. filter(g) {
  21890. if (g instanceof LineString) this.p.add(g);
  21891. }
  21892. get interfaces_() {
  21893. return [GeometryComponentFilter];
  21894. }
  21895. }
  21896. Polygonizer.LineStringAdder = LineStringAdder;
  21897. var polygonize = /*#__PURE__*/Object.freeze({
  21898. __proto__: null,
  21899. Polygonizer: Polygonizer
  21900. });
  21901. class RelateComputer {
  21902. constructor() {
  21903. RelateComputer.constructor_.apply(this, arguments);
  21904. }
  21905. static constructor_() {
  21906. this._li = new RobustLineIntersector();
  21907. this._ptLocator = new PointLocator();
  21908. this._arg = null;
  21909. this._nodes = new NodeMap$1(new RelateNodeFactory());
  21910. this._im = null;
  21911. this._isolatedEdges = new ArrayList();
  21912. this._invalidPoint = null;
  21913. const arg = arguments[0];
  21914. this._arg = arg;
  21915. }
  21916. insertEdgeEnds(ee) {
  21917. for (let i = ee.iterator(); i.hasNext();) {
  21918. const e = i.next();
  21919. this._nodes.add(e);
  21920. }
  21921. }
  21922. computeProperIntersectionIM(intersector, im) {
  21923. const dimA = this._arg[0].getGeometry().getDimension();
  21924. const dimB = this._arg[1].getGeometry().getDimension();
  21925. const hasProper = intersector.hasProperIntersection();
  21926. const hasProperInterior = intersector.hasProperInteriorIntersection();
  21927. if (dimA === 2 && dimB === 2) {
  21928. if (hasProper) im.setAtLeast('212101212');
  21929. } else if (dimA === 2 && dimB === 1) {
  21930. if (hasProper) im.setAtLeast('FFF0FFFF2');
  21931. if (hasProperInterior) im.setAtLeast('1FFFFF1FF');
  21932. } else if (dimA === 1 && dimB === 2) {
  21933. if (hasProper) im.setAtLeast('F0FFFFFF2');
  21934. if (hasProperInterior) im.setAtLeast('1F1FFFFFF');
  21935. } else if (dimA === 1 && dimB === 1) {
  21936. if (hasProperInterior) im.setAtLeast('0FFFFFFFF');
  21937. }
  21938. }
  21939. labelIsolatedEdges(thisIndex, targetIndex) {
  21940. for (let ei = this._arg[thisIndex].getEdgeIterator(); ei.hasNext();) {
  21941. const e = ei.next();
  21942. if (e.isIsolated()) {
  21943. this.labelIsolatedEdge(e, targetIndex, this._arg[targetIndex].getGeometry());
  21944. this._isolatedEdges.add(e);
  21945. }
  21946. }
  21947. }
  21948. labelIsolatedEdge(e, targetIndex, target) {
  21949. if (target.getDimension() > 0) {
  21950. const loc = this._ptLocator.locate(e.getCoordinate(), target);
  21951. e.getLabel().setAllLocations(targetIndex, loc);
  21952. } else {
  21953. e.getLabel().setAllLocations(targetIndex, Location.EXTERIOR);
  21954. }
  21955. }
  21956. computeIM() {
  21957. const im = new IntersectionMatrix();
  21958. im.set(Location.EXTERIOR, Location.EXTERIOR, 2);
  21959. if (!this._arg[0].getGeometry().getEnvelopeInternal().intersects(this._arg[1].getGeometry().getEnvelopeInternal())) {
  21960. this.computeDisjointIM(im);
  21961. return im;
  21962. }
  21963. this._arg[0].computeSelfNodes(this._li, false);
  21964. this._arg[1].computeSelfNodes(this._li, false);
  21965. const intersector = this._arg[0].computeEdgeIntersections(this._arg[1], this._li, false);
  21966. this.computeIntersectionNodes(0);
  21967. this.computeIntersectionNodes(1);
  21968. this.copyNodesAndLabels(0);
  21969. this.copyNodesAndLabels(1);
  21970. this.labelIsolatedNodes();
  21971. this.computeProperIntersectionIM(intersector, im);
  21972. const eeBuilder = new EdgeEndBuilder();
  21973. const ee0 = eeBuilder.computeEdgeEnds(this._arg[0].getEdgeIterator());
  21974. this.insertEdgeEnds(ee0);
  21975. const ee1 = eeBuilder.computeEdgeEnds(this._arg[1].getEdgeIterator());
  21976. this.insertEdgeEnds(ee1);
  21977. this.labelNodeEdges();
  21978. this.labelIsolatedEdges(0, 1);
  21979. this.labelIsolatedEdges(1, 0);
  21980. this.updateIM(im);
  21981. return im;
  21982. }
  21983. labelNodeEdges() {
  21984. for (let ni = this._nodes.iterator(); ni.hasNext();) {
  21985. const node = ni.next();
  21986. node.getEdges().computeLabelling(this._arg);
  21987. }
  21988. }
  21989. copyNodesAndLabels(argIndex) {
  21990. for (let i = this._arg[argIndex].getNodeIterator(); i.hasNext();) {
  21991. const graphNode = i.next();
  21992. const newNode = this._nodes.addNode(graphNode.getCoordinate());
  21993. newNode.setLabel(argIndex, graphNode.getLabel().getLocation(argIndex));
  21994. }
  21995. }
  21996. labelIntersectionNodes(argIndex) {
  21997. for (let i = this._arg[argIndex].getEdgeIterator(); i.hasNext();) {
  21998. const e = i.next();
  21999. const eLoc = e.getLabel().getLocation(argIndex);
  22000. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext();) {
  22001. const ei = eiIt.next();
  22002. const n = this._nodes.find(ei.coord);
  22003. if (n.getLabel().isNull(argIndex)) if (eLoc === Location.BOUNDARY) n.setLabelBoundary(argIndex);else n.setLabel(argIndex, Location.INTERIOR);
  22004. }
  22005. }
  22006. }
  22007. labelIsolatedNode(n, targetIndex) {
  22008. const loc = this._ptLocator.locate(n.getCoordinate(), this._arg[targetIndex].getGeometry());
  22009. n.getLabel().setAllLocations(targetIndex, loc);
  22010. }
  22011. computeIntersectionNodes(argIndex) {
  22012. for (let i = this._arg[argIndex].getEdgeIterator(); i.hasNext();) {
  22013. const e = i.next();
  22014. const eLoc = e.getLabel().getLocation(argIndex);
  22015. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext();) {
  22016. const ei = eiIt.next();
  22017. const n = this._nodes.addNode(ei.coord);
  22018. if (eLoc === Location.BOUNDARY) n.setLabelBoundary(argIndex);else if (n.getLabel().isNull(argIndex)) n.setLabel(argIndex, Location.INTERIOR);
  22019. }
  22020. }
  22021. }
  22022. labelIsolatedNodes() {
  22023. for (let ni = this._nodes.iterator(); ni.hasNext();) {
  22024. const n = ni.next();
  22025. const label = n.getLabel();
  22026. Assert.isTrue(label.getGeometryCount() > 0, 'node with empty label found');
  22027. if (n.isIsolated()) if (label.isNull(0)) this.labelIsolatedNode(n, 0);else this.labelIsolatedNode(n, 1);
  22028. }
  22029. }
  22030. updateIM(im) {
  22031. for (let ei = this._isolatedEdges.iterator(); ei.hasNext();) {
  22032. const e = ei.next();
  22033. e.updateIM(im);
  22034. }
  22035. for (let ni = this._nodes.iterator(); ni.hasNext();) {
  22036. const node = ni.next();
  22037. node.updateIM(im);
  22038. node.updateIMFromEdges(im);
  22039. }
  22040. }
  22041. computeDisjointIM(im) {
  22042. const ga = this._arg[0].getGeometry();
  22043. if (!ga.isEmpty()) {
  22044. im.set(Location.INTERIOR, Location.EXTERIOR, ga.getDimension());
  22045. im.set(Location.BOUNDARY, Location.EXTERIOR, ga.getBoundaryDimension());
  22046. }
  22047. const gb = this._arg[1].getGeometry();
  22048. if (!gb.isEmpty()) {
  22049. im.set(Location.EXTERIOR, Location.INTERIOR, gb.getDimension());
  22050. im.set(Location.EXTERIOR, Location.BOUNDARY, gb.getBoundaryDimension());
  22051. }
  22052. }
  22053. }
  22054. class RectangleContains {
  22055. constructor() {
  22056. RectangleContains.constructor_.apply(this, arguments);
  22057. }
  22058. static constructor_() {
  22059. this._rectEnv = null;
  22060. const rectangle = arguments[0];
  22061. this._rectEnv = rectangle.getEnvelopeInternal();
  22062. }
  22063. static contains(rectangle, b) {
  22064. const rc = new RectangleContains(rectangle);
  22065. return rc.contains(b);
  22066. }
  22067. isContainedInBoundary(geom) {
  22068. if (geom instanceof Polygon) return false;
  22069. if (geom instanceof Point) return this.isPointContainedInBoundary(geom);
  22070. if (geom instanceof LineString) return this.isLineStringContainedInBoundary(geom);
  22071. for (let i = 0; i < geom.getNumGeometries(); i++) {
  22072. const comp = geom.getGeometryN(i);
  22073. if (!this.isContainedInBoundary(comp)) return false;
  22074. }
  22075. return true;
  22076. }
  22077. isLineSegmentContainedInBoundary(p0, p1) {
  22078. if (p0.equals(p1)) return this.isPointContainedInBoundary(p0);
  22079. if (p0.x === p1.x) {
  22080. if (p0.x === this._rectEnv.getMinX() || p0.x === this._rectEnv.getMaxX()) return true;
  22081. } else if (p0.y === p1.y) {
  22082. if (p0.y === this._rectEnv.getMinY() || p0.y === this._rectEnv.getMaxY()) return true;
  22083. }
  22084. return false;
  22085. }
  22086. isLineStringContainedInBoundary(line) {
  22087. const seq = line.getCoordinateSequence();
  22088. const p0 = new Coordinate();
  22089. const p1 = new Coordinate();
  22090. for (let i = 0; i < seq.size() - 1; i++) {
  22091. seq.getCoordinate(i, p0);
  22092. seq.getCoordinate(i + 1, p1);
  22093. if (!this.isLineSegmentContainedInBoundary(p0, p1)) return false;
  22094. }
  22095. return true;
  22096. }
  22097. isPointContainedInBoundary() {
  22098. if (arguments[0] instanceof Point) {
  22099. const point = arguments[0];
  22100. return this.isPointContainedInBoundary(point.getCoordinate());
  22101. } else if (arguments[0] instanceof Coordinate) {
  22102. const pt = arguments[0];
  22103. return pt.x === this._rectEnv.getMinX() || pt.x === this._rectEnv.getMaxX() || pt.y === this._rectEnv.getMinY() || pt.y === this._rectEnv.getMaxY();
  22104. }
  22105. }
  22106. contains(geom) {
  22107. if (!this._rectEnv.contains(geom.getEnvelopeInternal())) return false;
  22108. if (this.isContainedInBoundary(geom)) return false;
  22109. return true;
  22110. }
  22111. }
  22112. class RectangleLineIntersector {
  22113. constructor() {
  22114. RectangleLineIntersector.constructor_.apply(this, arguments);
  22115. }
  22116. static constructor_() {
  22117. this._li = new RobustLineIntersector();
  22118. this._rectEnv = null;
  22119. this._diagUp0 = null;
  22120. this._diagUp1 = null;
  22121. this._diagDown0 = null;
  22122. this._diagDown1 = null;
  22123. const rectEnv = arguments[0];
  22124. this._rectEnv = rectEnv;
  22125. this._diagUp0 = new Coordinate(rectEnv.getMinX(), rectEnv.getMinY());
  22126. this._diagUp1 = new Coordinate(rectEnv.getMaxX(), rectEnv.getMaxY());
  22127. this._diagDown0 = new Coordinate(rectEnv.getMinX(), rectEnv.getMaxY());
  22128. this._diagDown1 = new Coordinate(rectEnv.getMaxX(), rectEnv.getMinY());
  22129. }
  22130. intersects(p0, p1) {
  22131. const segEnv = new Envelope(p0, p1);
  22132. if (!this._rectEnv.intersects(segEnv)) return false;
  22133. if (this._rectEnv.intersects(p0)) return true;
  22134. if (this._rectEnv.intersects(p1)) return true;
  22135. if (p0.compareTo(p1) > 0) {
  22136. const tmp = p0;
  22137. p0 = p1;
  22138. p1 = tmp;
  22139. }
  22140. let isSegUpwards = false;
  22141. if (p1.y > p0.y) isSegUpwards = true;
  22142. if (isSegUpwards) this._li.computeIntersection(p0, p1, this._diagDown0, this._diagDown1);else this._li.computeIntersection(p0, p1, this._diagUp0, this._diagUp1);
  22143. if (this._li.hasIntersection()) return true;
  22144. return false;
  22145. }
  22146. }
  22147. class RectangleIntersects {
  22148. constructor() {
  22149. RectangleIntersects.constructor_.apply(this, arguments);
  22150. }
  22151. static constructor_() {
  22152. this._rectangle = null;
  22153. this._rectEnv = null;
  22154. const rectangle = arguments[0];
  22155. this._rectangle = rectangle;
  22156. this._rectEnv = rectangle.getEnvelopeInternal();
  22157. }
  22158. static intersects(rectangle, b) {
  22159. const rp = new RectangleIntersects(rectangle);
  22160. return rp.intersects(b);
  22161. }
  22162. intersects(geom) {
  22163. if (!this._rectEnv.intersects(geom.getEnvelopeInternal())) return false;
  22164. const visitor = new EnvelopeIntersectsVisitor(this._rectEnv);
  22165. visitor.applyTo(geom);
  22166. if (visitor.intersects()) return true;
  22167. const ecpVisitor = new GeometryContainsPointVisitor(this._rectangle);
  22168. ecpVisitor.applyTo(geom);
  22169. if (ecpVisitor.containsPoint()) return true;
  22170. const riVisitor = new RectangleIntersectsSegmentVisitor(this._rectangle);
  22171. riVisitor.applyTo(geom);
  22172. if (riVisitor.intersects()) return true;
  22173. return false;
  22174. }
  22175. }
  22176. class EnvelopeIntersectsVisitor extends ShortCircuitedGeometryVisitor {
  22177. constructor() {
  22178. super();
  22179. EnvelopeIntersectsVisitor.constructor_.apply(this, arguments);
  22180. }
  22181. static constructor_() {
  22182. this._rectEnv = null;
  22183. this._intersects = false;
  22184. const rectEnv = arguments[0];
  22185. this._rectEnv = rectEnv;
  22186. }
  22187. isDone() {
  22188. return this._intersects === true;
  22189. }
  22190. visit(element) {
  22191. const elementEnv = element.getEnvelopeInternal();
  22192. if (!this._rectEnv.intersects(elementEnv)) return null;
  22193. if (this._rectEnv.contains(elementEnv)) {
  22194. this._intersects = true;
  22195. return null;
  22196. }
  22197. if (elementEnv.getMinX() >= this._rectEnv.getMinX() && elementEnv.getMaxX() <= this._rectEnv.getMaxX()) {
  22198. this._intersects = true;
  22199. return null;
  22200. }
  22201. if (elementEnv.getMinY() >= this._rectEnv.getMinY() && elementEnv.getMaxY() <= this._rectEnv.getMaxY()) {
  22202. this._intersects = true;
  22203. return null;
  22204. }
  22205. }
  22206. intersects() {
  22207. return this._intersects;
  22208. }
  22209. }
  22210. class GeometryContainsPointVisitor extends ShortCircuitedGeometryVisitor {
  22211. constructor() {
  22212. super();
  22213. GeometryContainsPointVisitor.constructor_.apply(this, arguments);
  22214. }
  22215. static constructor_() {
  22216. this._rectSeq = null;
  22217. this._rectEnv = null;
  22218. this._containsPoint = false;
  22219. const rectangle = arguments[0];
  22220. this._rectSeq = rectangle.getExteriorRing().getCoordinateSequence();
  22221. this._rectEnv = rectangle.getEnvelopeInternal();
  22222. }
  22223. isDone() {
  22224. return this._containsPoint === true;
  22225. }
  22226. visit(geom) {
  22227. if (!(geom instanceof Polygon)) return null;
  22228. const elementEnv = geom.getEnvelopeInternal();
  22229. if (!this._rectEnv.intersects(elementEnv)) return null;
  22230. const rectPt = new Coordinate();
  22231. for (let i = 0; i < 4; i++) {
  22232. this._rectSeq.getCoordinate(i, rectPt);
  22233. if (!elementEnv.contains(rectPt)) continue;
  22234. if (SimplePointInAreaLocator.containsPointInPolygon(rectPt, geom)) {
  22235. this._containsPoint = true;
  22236. return null;
  22237. }
  22238. }
  22239. }
  22240. containsPoint() {
  22241. return this._containsPoint;
  22242. }
  22243. }
  22244. class RectangleIntersectsSegmentVisitor extends ShortCircuitedGeometryVisitor {
  22245. constructor() {
  22246. super();
  22247. RectangleIntersectsSegmentVisitor.constructor_.apply(this, arguments);
  22248. }
  22249. static constructor_() {
  22250. this._rectEnv = null;
  22251. this._rectIntersector = null;
  22252. this._hasIntersection = false;
  22253. this._p0 = new Coordinate();
  22254. this._p1 = new Coordinate();
  22255. const rectangle = arguments[0];
  22256. this._rectEnv = rectangle.getEnvelopeInternal();
  22257. this._rectIntersector = new RectangleLineIntersector(this._rectEnv);
  22258. }
  22259. intersects() {
  22260. return this._hasIntersection;
  22261. }
  22262. isDone() {
  22263. return this._hasIntersection === true;
  22264. }
  22265. visit(geom) {
  22266. const elementEnv = geom.getEnvelopeInternal();
  22267. if (!this._rectEnv.intersects(elementEnv)) return null;
  22268. const lines = LinearComponentExtracter.getLines(geom);
  22269. this.checkIntersectionWithLineStrings(lines);
  22270. }
  22271. checkIntersectionWithLineStrings(lines) {
  22272. for (let i = lines.iterator(); i.hasNext();) {
  22273. const testLine = i.next();
  22274. this.checkIntersectionWithSegments(testLine);
  22275. if (this._hasIntersection) return null;
  22276. }
  22277. }
  22278. checkIntersectionWithSegments(testLine) {
  22279. const seq1 = testLine.getCoordinateSequence();
  22280. for (let j = 1; j < seq1.size(); j++) {
  22281. seq1.getCoordinate(j - 1, this._p0);
  22282. seq1.getCoordinate(j, this._p1);
  22283. if (this._rectIntersector.intersects(this._p0, this._p1)) {
  22284. this._hasIntersection = true;
  22285. return null;
  22286. }
  22287. }
  22288. }
  22289. }
  22290. class RelateOp extends GeometryGraphOperation {
  22291. constructor() {
  22292. super();
  22293. RelateOp.constructor_.apply(this, arguments);
  22294. }
  22295. static constructor_() {
  22296. this._relate = null;
  22297. if (arguments.length === 2) {
  22298. const g0 = arguments[0],
  22299. g1 = arguments[1];
  22300. GeometryGraphOperation.constructor_.call(this, g0, g1);
  22301. this._relate = new RelateComputer(this._arg);
  22302. } else if (arguments.length === 3) {
  22303. const g0 = arguments[0],
  22304. g1 = arguments[1],
  22305. boundaryNodeRule = arguments[2];
  22306. GeometryGraphOperation.constructor_.call(this, g0, g1, boundaryNodeRule);
  22307. this._relate = new RelateComputer(this._arg);
  22308. }
  22309. }
  22310. static covers(g1, g2) {
  22311. if (g2.getDimension() === 2 && g1.getDimension() < 2) return false;
  22312. if (g2.getDimension() === 1 && g1.getDimension() < 1 && g2.getLength() > 0.0) return false;
  22313. if (!g1.getEnvelopeInternal().covers(g2.getEnvelopeInternal())) return false;
  22314. if (g1.isRectangle()) return true;
  22315. return new RelateOp(g1, g2).getIntersectionMatrix().isCovers();
  22316. }
  22317. static intersects(g1, g2) {
  22318. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false;
  22319. if (g1.isRectangle()) return RectangleIntersects.intersects(g1, g2);
  22320. if (g2.isRectangle()) return RectangleIntersects.intersects(g2, g1);
  22321. if (g1.isGeometryCollection() || g2.isGeometryCollection()) {
  22322. for (let i = 0; i < g1.getNumGeometries(); i++) for (let j = 0; j < g2.getNumGeometries(); j++) if (g1.getGeometryN(i).intersects(g2.getGeometryN(j))) return true;
  22323. return false;
  22324. }
  22325. return new RelateOp(g1, g2).getIntersectionMatrix().isIntersects();
  22326. }
  22327. static touches(g1, g2) {
  22328. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false;
  22329. return new RelateOp(g1, g2).getIntersectionMatrix().isTouches(g1.getDimension(), g2.getDimension());
  22330. }
  22331. static equalsTopo(g1, g2) {
  22332. if (!g1.getEnvelopeInternal().equals(g2.getEnvelopeInternal())) return false;
  22333. return RelateOp.relate(g1, g2).isEquals(g1.getDimension(), g2.getDimension());
  22334. }
  22335. static relate() {
  22336. if (arguments.length === 2) {
  22337. const a = arguments[0],
  22338. b = arguments[1];
  22339. const relOp = new RelateOp(a, b);
  22340. const im = relOp.getIntersectionMatrix();
  22341. return im;
  22342. } else if (arguments.length === 3) {
  22343. const a = arguments[0],
  22344. b = arguments[1],
  22345. boundaryNodeRule = arguments[2];
  22346. const relOp = new RelateOp(a, b, boundaryNodeRule);
  22347. const im = relOp.getIntersectionMatrix();
  22348. return im;
  22349. }
  22350. }
  22351. static overlaps(g1, g2) {
  22352. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false;
  22353. return new RelateOp(g1, g2).getIntersectionMatrix().isOverlaps(g1.getDimension(), g2.getDimension());
  22354. }
  22355. static crosses(g1, g2) {
  22356. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false;
  22357. return new RelateOp(g1, g2).getIntersectionMatrix().isCrosses(g1.getDimension(), g2.getDimension());
  22358. }
  22359. static contains(g1, g2) {
  22360. if (g2.getDimension() === 2 && g1.getDimension() < 2) return false;
  22361. if (g2.getDimension() === 1 && g1.getDimension() < 1 && g2.getLength() > 0.0) return false;
  22362. if (!g1.getEnvelopeInternal().contains(g2.getEnvelopeInternal())) return false;
  22363. if (g1.isRectangle()) return RectangleContains.contains(g1, g2);
  22364. return new RelateOp(g1, g2).getIntersectionMatrix().isContains();
  22365. }
  22366. getIntersectionMatrix() {
  22367. return this._relate.computeIM();
  22368. }
  22369. }
  22370. var relate = /*#__PURE__*/Object.freeze({
  22371. __proto__: null,
  22372. RelateOp: RelateOp
  22373. });
  22374. class PointGeometryUnion {
  22375. constructor() {
  22376. PointGeometryUnion.constructor_.apply(this, arguments);
  22377. }
  22378. static constructor_() {
  22379. this._pointGeom = null;
  22380. this._otherGeom = null;
  22381. this._geomFact = null;
  22382. const pointGeom = arguments[0],
  22383. otherGeom = arguments[1];
  22384. this._pointGeom = pointGeom;
  22385. this._otherGeom = otherGeom;
  22386. this._geomFact = otherGeom.getFactory();
  22387. }
  22388. static union(pointGeom, otherGeom) {
  22389. const unioner = new PointGeometryUnion(pointGeom, otherGeom);
  22390. return unioner.union();
  22391. }
  22392. union() {
  22393. const locater = new PointLocator();
  22394. const exteriorCoords = new TreeSet();
  22395. for (let i = 0; i < this._pointGeom.getNumGeometries(); i++) {
  22396. const point = this._pointGeom.getGeometryN(i);
  22397. const coord = point.getCoordinate();
  22398. const loc = locater.locate(coord, this._otherGeom);
  22399. if (loc === Location.EXTERIOR) exteriorCoords.add(coord);
  22400. }
  22401. if (exteriorCoords.size() === 0) return this._otherGeom;
  22402. let ptComp = null;
  22403. const coords = CoordinateArrays.toCoordinateArray(exteriorCoords);
  22404. if (coords.length === 1) ptComp = this._geomFact.createPoint(coords[0]);else ptComp = this._geomFact.createMultiPointFromCoords(coords);
  22405. return GeometryCombiner.combine(ptComp, this._otherGeom);
  22406. }
  22407. }
  22408. class InputExtracter {
  22409. constructor() {
  22410. InputExtracter.constructor_.apply(this, arguments);
  22411. }
  22412. static constructor_() {
  22413. this._geomFactory = null;
  22414. this._polygons = new ArrayList();
  22415. this._lines = new ArrayList();
  22416. this._points = new ArrayList();
  22417. this._dimension = Dimension.FALSE;
  22418. }
  22419. static extract() {
  22420. if (hasInterface(arguments[0], Collection)) {
  22421. const geoms = arguments[0];
  22422. const extracter = new InputExtracter();
  22423. extracter.add(geoms);
  22424. return extracter;
  22425. } else if (arguments[0] instanceof Geometry) {
  22426. const geom = arguments[0];
  22427. const extracter = new InputExtracter();
  22428. extracter.add(geom);
  22429. return extracter;
  22430. }
  22431. }
  22432. getFactory() {
  22433. return this._geomFactory;
  22434. }
  22435. recordDimension(dim) {
  22436. if (dim > this._dimension) this._dimension = dim;
  22437. }
  22438. getDimension() {
  22439. return this._dimension;
  22440. }
  22441. filter(geom) {
  22442. this.recordDimension(geom.getDimension());
  22443. if (geom instanceof GeometryCollection) return null;
  22444. if (geom.isEmpty()) return null;
  22445. if (geom instanceof Polygon) {
  22446. this._polygons.add(geom);
  22447. return null;
  22448. } else if (geom instanceof LineString) {
  22449. this._lines.add(geom);
  22450. return null;
  22451. } else if (geom instanceof Point) {
  22452. this._points.add(geom);
  22453. return null;
  22454. }
  22455. Assert.shouldNeverReachHere('Unhandled geometry type: ' + geom.getGeometryType());
  22456. }
  22457. getExtract(dim) {
  22458. switch (dim) {
  22459. case 0:
  22460. return this._points;
  22461. case 1:
  22462. return this._lines;
  22463. case 2:
  22464. return this._polygons;
  22465. }
  22466. Assert.shouldNeverReachHere('Invalid dimension: ' + dim);
  22467. return null;
  22468. }
  22469. isEmpty() {
  22470. return this._polygons.isEmpty() && this._lines.isEmpty() && this._points.isEmpty();
  22471. }
  22472. add() {
  22473. if (hasInterface(arguments[0], Collection)) {
  22474. const geoms = arguments[0];
  22475. for (const geom of geoms) this.add(geom);
  22476. } else if (arguments[0] instanceof Geometry) {
  22477. const geom = arguments[0];
  22478. if (this._geomFactory === null) this._geomFactory = geom.getFactory();
  22479. geom.apply(this);
  22480. }
  22481. }
  22482. get interfaces_() {
  22483. return [GeometryFilter];
  22484. }
  22485. }
  22486. class OverlapUnion {
  22487. constructor() {
  22488. OverlapUnion.constructor_.apply(this, arguments);
  22489. }
  22490. static constructor_() {
  22491. this._geomFactory = null;
  22492. this._g0 = null;
  22493. this._g1 = null;
  22494. this._isUnionSafe = null;
  22495. const g0 = arguments[0],
  22496. g1 = arguments[1];
  22497. this._g0 = g0;
  22498. this._g1 = g1;
  22499. this._geomFactory = g0.getFactory();
  22500. }
  22501. static containsProperly() {
  22502. if (arguments.length === 2) {
  22503. const env = arguments[0],
  22504. p = arguments[1];
  22505. if (env.isNull()) return false;
  22506. return p.getX() > env.getMinX() && p.getX() < env.getMaxX() && p.getY() > env.getMinY() && p.getY() < env.getMaxY();
  22507. } else if (arguments.length === 3) {
  22508. const env = arguments[0],
  22509. p0 = arguments[1],
  22510. p1 = arguments[2];
  22511. return OverlapUnion.containsProperly(env, p0) && OverlapUnion.containsProperly(env, p1);
  22512. }
  22513. }
  22514. static union(g0, g1) {
  22515. const union = new OverlapUnion(g0, g1);
  22516. return union.union();
  22517. }
  22518. static intersects(env, p0, p1) {
  22519. return env.intersects(p0) || env.intersects(p1);
  22520. }
  22521. static overlapEnvelope(g0, g1) {
  22522. const g0Env = g0.getEnvelopeInternal();
  22523. const g1Env = g1.getEnvelopeInternal();
  22524. const overlapEnv = g0Env.intersection(g1Env);
  22525. return overlapEnv;
  22526. }
  22527. static extractBorderSegments(geom, env, segs) {
  22528. geom.apply(new class {
  22529. get interfaces_() {
  22530. return [CoordinateSequenceFilter];
  22531. }
  22532. filter(seq, i) {
  22533. if (i <= 0) return null;
  22534. const p0 = seq.getCoordinate(i - 1);
  22535. const p1 = seq.getCoordinate(i);
  22536. const isBorder = OverlapUnion.intersects(env, p0, p1) && !OverlapUnion.containsProperly(env, p0, p1);
  22537. if (isBorder) {
  22538. const seg = new LineSegment(p0, p1);
  22539. segs.add(seg);
  22540. }
  22541. }
  22542. isDone() {
  22543. return false;
  22544. }
  22545. isGeometryChanged() {
  22546. return false;
  22547. }
  22548. }());
  22549. }
  22550. static unionBuffer(g0, g1) {
  22551. const factory = g0.getFactory();
  22552. const gColl = factory.createGeometryCollection([g0, g1]);
  22553. const union = gColl.buffer(0.0);
  22554. return union;
  22555. }
  22556. isBorderSegmentsSame(result, env) {
  22557. const segsBefore = this.extractBorderSegments(this._g0, this._g1, env);
  22558. const segsAfter = new ArrayList();
  22559. OverlapUnion.extractBorderSegments(result, env, segsAfter);
  22560. return this.isEqual(segsBefore, segsAfter);
  22561. }
  22562. extractByEnvelope(env, geom, disjointGeoms) {
  22563. const intersectingGeoms = new ArrayList();
  22564. for (let i = 0; i < geom.getNumGeometries(); i++) {
  22565. const elem = geom.getGeometryN(i);
  22566. if (elem.getEnvelopeInternal().intersects(env)) {
  22567. intersectingGeoms.add(elem);
  22568. } else {
  22569. const copy = elem.copy();
  22570. disjointGeoms.add(copy);
  22571. }
  22572. }
  22573. return this._geomFactory.buildGeometry(intersectingGeoms);
  22574. }
  22575. isEqual(segs0, segs1) {
  22576. if (segs0.size() !== segs1.size()) return false;
  22577. const segIndex = new HashSet(segs0);
  22578. for (const seg of segs1) if (!segIndex.contains(seg)) return false;
  22579. return true;
  22580. }
  22581. union() {
  22582. const overlapEnv = OverlapUnion.overlapEnvelope(this._g0, this._g1);
  22583. if (overlapEnv.isNull()) {
  22584. const g0Copy = this._g0.copy();
  22585. const g1Copy = this._g1.copy();
  22586. return GeometryCombiner.combine(g0Copy, g1Copy);
  22587. }
  22588. const disjointPolys = new ArrayList();
  22589. const g0Overlap = this.extractByEnvelope(overlapEnv, this._g0, disjointPolys);
  22590. const g1Overlap = this.extractByEnvelope(overlapEnv, this._g1, disjointPolys);
  22591. const unionGeom = this.unionFull(g0Overlap, g1Overlap);
  22592. let result = null;
  22593. this._isUnionSafe = this.isBorderSegmentsSame(unionGeom, overlapEnv);
  22594. if (!this._isUnionSafe) result = this.unionFull(this._g0, this._g1);else result = this.combine(unionGeom, disjointPolys);
  22595. return result;
  22596. }
  22597. combine(unionGeom, disjointPolys) {
  22598. if (disjointPolys.size() <= 0) return unionGeom;
  22599. disjointPolys.add(unionGeom);
  22600. const result = GeometryCombiner.combine(disjointPolys);
  22601. return result;
  22602. }
  22603. unionFull(geom0, geom1) {
  22604. try {
  22605. return geom0.union(geom1);
  22606. } catch (ex) {
  22607. if (ex instanceof TopologyException) return OverlapUnion.unionBuffer(geom0, geom1);else throw ex;
  22608. } finally {}
  22609. }
  22610. extractBorderSegments(geom0, geom1, env) {
  22611. const segs = new ArrayList();
  22612. OverlapUnion.extractBorderSegments(geom0, env, segs);
  22613. if (geom1 !== null) OverlapUnion.extractBorderSegments(geom1, env, segs);
  22614. return segs;
  22615. }
  22616. isUnionOptimized() {
  22617. return this._isUnionSafe;
  22618. }
  22619. }
  22620. class CascadedPolygonUnion {
  22621. constructor() {
  22622. CascadedPolygonUnion.constructor_.apply(this, arguments);
  22623. }
  22624. static constructor_() {
  22625. this._inputPolys = null;
  22626. this._geomFactory = null;
  22627. const polys = arguments[0];
  22628. this._inputPolys = polys;
  22629. if (this._inputPolys === null) this._inputPolys = new ArrayList();
  22630. }
  22631. static restrictToPolygons(g) {
  22632. if (hasInterface(g, Polygonal)) return g;
  22633. const polygons = PolygonExtracter.getPolygons(g);
  22634. if (polygons.size() === 1) return polygons.get(0);
  22635. return g.getFactory().createMultiPolygon(GeometryFactory.toPolygonArray(polygons));
  22636. }
  22637. static getGeometry(list, index) {
  22638. if (index >= list.size()) return null;
  22639. return list.get(index);
  22640. }
  22641. static union(polys) {
  22642. const op = new CascadedPolygonUnion(polys);
  22643. return op.union();
  22644. }
  22645. reduceToGeometries(geomTree) {
  22646. const geoms = new ArrayList();
  22647. for (let i = geomTree.iterator(); i.hasNext();) {
  22648. const o = i.next();
  22649. let geom = null;
  22650. if (hasInterface(o, List)) geom = this.unionTree(o);else if (o instanceof Geometry) geom = o;
  22651. geoms.add(geom);
  22652. }
  22653. return geoms;
  22654. }
  22655. union() {
  22656. if (this._inputPolys === null) throw new IllegalStateException('union() method cannot be called twice');
  22657. if (this._inputPolys.isEmpty()) return null;
  22658. this._geomFactory = this._inputPolys.iterator().next().getFactory();
  22659. const index = new STRtree(CascadedPolygonUnion.STRTREE_NODE_CAPACITY);
  22660. for (let i = this._inputPolys.iterator(); i.hasNext();) {
  22661. const item = i.next();
  22662. index.insert(item.getEnvelopeInternal(), item);
  22663. }
  22664. this._inputPolys = null;
  22665. const itemTree = index.itemsTree();
  22666. const unionAll = this.unionTree(itemTree);
  22667. return unionAll;
  22668. }
  22669. binaryUnion() {
  22670. if (arguments.length === 1) {
  22671. const geoms = arguments[0];
  22672. return this.binaryUnion(geoms, 0, geoms.size());
  22673. } else if (arguments.length === 3) {
  22674. const geoms = arguments[0],
  22675. start = arguments[1],
  22676. end = arguments[2];
  22677. if (end - start <= 1) {
  22678. const g0 = CascadedPolygonUnion.getGeometry(geoms, start);
  22679. return this.unionSafe(g0, null);
  22680. } else if (end - start === 2) {
  22681. return this.unionSafe(CascadedPolygonUnion.getGeometry(geoms, start), CascadedPolygonUnion.getGeometry(geoms, start + 1));
  22682. } else {
  22683. const mid = Math.trunc((end + start) / 2);
  22684. const g0 = this.binaryUnion(geoms, start, mid);
  22685. const g1 = this.binaryUnion(geoms, mid, end);
  22686. return this.unionSafe(g0, g1);
  22687. }
  22688. }
  22689. }
  22690. repeatedUnion(geoms) {
  22691. let union = null;
  22692. for (let i = geoms.iterator(); i.hasNext();) {
  22693. const g = i.next();
  22694. if (union === null) union = g.copy();else union = union.union(g);
  22695. }
  22696. return union;
  22697. }
  22698. unionSafe(g0, g1) {
  22699. if (g0 === null && g1 === null) return null;
  22700. if (g0 === null) return g1.copy();
  22701. if (g1 === null) return g0.copy();
  22702. return this.unionActual(g0, g1);
  22703. }
  22704. unionActual(g0, g1) {
  22705. const union = OverlapUnion.union(g0, g1);
  22706. return CascadedPolygonUnion.restrictToPolygons(union);
  22707. }
  22708. unionTree(geomTree) {
  22709. const geoms = this.reduceToGeometries(geomTree);
  22710. const union = this.binaryUnion(geoms);
  22711. return union;
  22712. }
  22713. bufferUnion() {
  22714. if (arguments.length === 1) {
  22715. const geoms = arguments[0];
  22716. const factory = geoms.get(0).getFactory();
  22717. const gColl = factory.buildGeometry(geoms);
  22718. const unionAll = gColl.buffer(0.0);
  22719. return unionAll;
  22720. } else if (arguments.length === 2) {
  22721. const g0 = arguments[0],
  22722. g1 = arguments[1];
  22723. const factory = g0.getFactory();
  22724. const gColl = factory.createGeometryCollection([g0, g1]);
  22725. const unionAll = gColl.buffer(0.0);
  22726. return unionAll;
  22727. }
  22728. }
  22729. }
  22730. CascadedPolygonUnion.STRTREE_NODE_CAPACITY = 4;
  22731. class UnaryUnionOp {
  22732. constructor() {
  22733. UnaryUnionOp.constructor_.apply(this, arguments);
  22734. }
  22735. static constructor_() {
  22736. this._geomFact = null;
  22737. this._extracter = null;
  22738. if (arguments.length === 1) {
  22739. if (hasInterface(arguments[0], Collection)) {
  22740. const geoms = arguments[0];
  22741. this.extract(geoms);
  22742. } else if (arguments[0] instanceof Geometry) {
  22743. const geom = arguments[0];
  22744. this.extract(geom);
  22745. }
  22746. } else if (arguments.length === 2) {
  22747. const geoms = arguments[0],
  22748. geomFact = arguments[1];
  22749. this._geomFact = geomFact;
  22750. this.extract(geoms);
  22751. }
  22752. }
  22753. static union() {
  22754. if (arguments.length === 1) {
  22755. if (hasInterface(arguments[0], Collection)) {
  22756. const geoms = arguments[0];
  22757. const op = new UnaryUnionOp(geoms);
  22758. return op.union();
  22759. } else if (arguments[0] instanceof Geometry) {
  22760. const geom = arguments[0];
  22761. const op = new UnaryUnionOp(geom);
  22762. return op.union();
  22763. }
  22764. } else if (arguments.length === 2) {
  22765. const geoms = arguments[0],
  22766. geomFact = arguments[1];
  22767. const op = new UnaryUnionOp(geoms, geomFact);
  22768. return op.union();
  22769. }
  22770. }
  22771. unionNoOpt(g0) {
  22772. const empty = this._geomFact.createPoint();
  22773. return SnapIfNeededOverlayOp.overlayOp(g0, empty, OverlayOp.UNION);
  22774. }
  22775. unionWithNull(g0, g1) {
  22776. if (g0 === null && g1 === null) return null;
  22777. if (g1 === null) return g0;
  22778. if (g0 === null) return g1;
  22779. return g0.union(g1);
  22780. }
  22781. extract() {
  22782. if (hasInterface(arguments[0], Collection)) {
  22783. const geoms = arguments[0];
  22784. this._extracter = InputExtracter.extract(geoms);
  22785. } else if (arguments[0] instanceof Geometry) {
  22786. const geom = arguments[0];
  22787. this._extracter = InputExtracter.extract(geom);
  22788. }
  22789. }
  22790. union() {
  22791. if (this._geomFact === null) this._geomFact = this._extracter.getFactory();
  22792. if (this._geomFact === null) return null;
  22793. if (this._extracter.isEmpty()) return this._geomFact.createEmpty(this._extracter.getDimension());
  22794. const points = this._extracter.getExtract(0);
  22795. const lines = this._extracter.getExtract(1);
  22796. const polygons = this._extracter.getExtract(2);
  22797. let unionPoints = null;
  22798. if (points.size() > 0) {
  22799. const ptGeom = this._geomFact.buildGeometry(points);
  22800. unionPoints = this.unionNoOpt(ptGeom);
  22801. }
  22802. let unionLines = null;
  22803. if (lines.size() > 0) {
  22804. const lineGeom = this._geomFact.buildGeometry(lines);
  22805. unionLines = this.unionNoOpt(lineGeom);
  22806. }
  22807. let unionPolygons = null;
  22808. if (polygons.size() > 0) unionPolygons = CascadedPolygonUnion.union(polygons);
  22809. const unionLA = this.unionWithNull(unionLines, unionPolygons);
  22810. let union = null;
  22811. if (unionPoints === null) union = unionLA;else if (unionLA === null) union = unionPoints;else union = PointGeometryUnion.union(unionPoints, unionLA);
  22812. if (union === null) return this._geomFact.createGeometryCollection();
  22813. return union;
  22814. }
  22815. }
  22816. var union = /*#__PURE__*/Object.freeze({
  22817. __proto__: null,
  22818. UnaryUnionOp: UnaryUnionOp
  22819. });
  22820. var valid = /*#__PURE__*/Object.freeze({
  22821. __proto__: null,
  22822. IsValidOp: IsValidOp,
  22823. ConsistentAreaTester: ConsistentAreaTester
  22824. });
  22825. var operation = /*#__PURE__*/Object.freeze({
  22826. __proto__: null,
  22827. BoundaryOp: BoundaryOp,
  22828. IsSimpleOp: IsSimpleOp,
  22829. buffer: buffer,
  22830. distance: distance,
  22831. linemerge: linemerge,
  22832. overlay: overlay,
  22833. polygonize: polygonize,
  22834. relate: relate,
  22835. union: union,
  22836. valid: valid
  22837. });
  22838. class CommonBitsOp {
  22839. constructor() {
  22840. CommonBitsOp.constructor_.apply(this, arguments);
  22841. }
  22842. static constructor_() {
  22843. this._returnToOriginalPrecision = true;
  22844. this._cbr = null;
  22845. if (arguments.length === 0) {
  22846. CommonBitsOp.constructor_.call(this, true);
  22847. } else if (arguments.length === 1) {
  22848. const returnToOriginalPrecision = arguments[0];
  22849. this._returnToOriginalPrecision = returnToOriginalPrecision;
  22850. }
  22851. }
  22852. computeResultPrecision(result) {
  22853. if (this._returnToOriginalPrecision) this._cbr.addCommonBits(result);
  22854. return result;
  22855. }
  22856. union(geom0, geom1) {
  22857. const geom = this.removeCommonBits(geom0, geom1);
  22858. return this.computeResultPrecision(geom[0].union(geom[1]));
  22859. }
  22860. intersection(geom0, geom1) {
  22861. const geom = this.removeCommonBits(geom0, geom1);
  22862. return this.computeResultPrecision(geom[0].intersection(geom[1]));
  22863. }
  22864. removeCommonBits() {
  22865. if (arguments.length === 1) {
  22866. const geom0 = arguments[0];
  22867. this._cbr = new CommonBitsRemover();
  22868. this._cbr.add(geom0);
  22869. const geom = this._cbr.removeCommonBits(geom0.copy());
  22870. return geom;
  22871. } else if (arguments.length === 2) {
  22872. const geom0 = arguments[0],
  22873. geom1 = arguments[1];
  22874. this._cbr = new CommonBitsRemover();
  22875. this._cbr.add(geom0);
  22876. this._cbr.add(geom1);
  22877. const geom = new Array(2).fill(null);
  22878. geom[0] = this._cbr.removeCommonBits(geom0.copy());
  22879. geom[1] = this._cbr.removeCommonBits(geom1.copy());
  22880. return geom;
  22881. }
  22882. }
  22883. buffer(geom0, distance) {
  22884. const geom = this.removeCommonBits(geom0);
  22885. return this.computeResultPrecision(geom.buffer(distance));
  22886. }
  22887. symDifference(geom0, geom1) {
  22888. const geom = this.removeCommonBits(geom0, geom1);
  22889. return this.computeResultPrecision(geom[0].symDifference(geom[1]));
  22890. }
  22891. difference(geom0, geom1) {
  22892. const geom = this.removeCommonBits(geom0, geom1);
  22893. return this.computeResultPrecision(geom[0].difference(geom[1]));
  22894. }
  22895. }
  22896. class EnhancedPrecisionOp {
  22897. static union(geom0, geom1) {
  22898. let originalEx = null;
  22899. try {
  22900. const result = geom0.union(geom1);
  22901. return result;
  22902. } catch (ex) {
  22903. if (ex instanceof RuntimeException) originalEx = ex;else throw ex;
  22904. } finally {}
  22905. try {
  22906. const cbo = new CommonBitsOp(true);
  22907. const resultEP = cbo.union(geom0, geom1);
  22908. if (!resultEP.isValid()) throw originalEx;
  22909. return resultEP;
  22910. } catch (ex2) {
  22911. if (ex2 instanceof RuntimeException) throw originalEx;else throw ex2;
  22912. } finally {}
  22913. }
  22914. static intersection(geom0, geom1) {
  22915. let originalEx = null;
  22916. try {
  22917. const result = geom0.intersection(geom1);
  22918. return result;
  22919. } catch (ex) {
  22920. if (ex instanceof RuntimeException) originalEx = ex;else throw ex;
  22921. } finally {}
  22922. try {
  22923. const cbo = new CommonBitsOp(true);
  22924. const resultEP = cbo.intersection(geom0, geom1);
  22925. if (!resultEP.isValid()) throw originalEx;
  22926. return resultEP;
  22927. } catch (ex2) {
  22928. if (ex2 instanceof RuntimeException) throw originalEx;else throw ex2;
  22929. } finally {}
  22930. }
  22931. static buffer(geom, distance) {
  22932. let originalEx = null;
  22933. try {
  22934. const result = geom.buffer(distance);
  22935. return result;
  22936. } catch (ex) {
  22937. if (ex instanceof RuntimeException) originalEx = ex;else throw ex;
  22938. } finally {}
  22939. try {
  22940. const cbo = new CommonBitsOp(true);
  22941. const resultEP = cbo.buffer(geom, distance);
  22942. if (!resultEP.isValid()) throw originalEx;
  22943. return resultEP;
  22944. } catch (ex2) {
  22945. if (ex2 instanceof RuntimeException) throw originalEx;else throw ex2;
  22946. } finally {}
  22947. }
  22948. static symDifference(geom0, geom1) {
  22949. let originalEx = null;
  22950. try {
  22951. const result = geom0.symDifference(geom1);
  22952. return result;
  22953. } catch (ex) {
  22954. if (ex instanceof RuntimeException) originalEx = ex;else throw ex;
  22955. } finally {}
  22956. try {
  22957. const cbo = new CommonBitsOp(true);
  22958. const resultEP = cbo.symDifference(geom0, geom1);
  22959. if (!resultEP.isValid()) throw originalEx;
  22960. return resultEP;
  22961. } catch (ex2) {
  22962. if (ex2 instanceof RuntimeException) throw originalEx;else throw ex2;
  22963. } finally {}
  22964. }
  22965. static difference(geom0, geom1) {
  22966. let originalEx = null;
  22967. try {
  22968. const result = geom0.difference(geom1);
  22969. return result;
  22970. } catch (ex) {
  22971. if (ex instanceof RuntimeException) originalEx = ex;else throw ex;
  22972. } finally {}
  22973. try {
  22974. const cbo = new CommonBitsOp(true);
  22975. const resultEP = cbo.difference(geom0, geom1);
  22976. if (!resultEP.isValid()) throw originalEx;
  22977. return resultEP;
  22978. } catch (ex2) {
  22979. if (ex2 instanceof RuntimeException) throw originalEx;else throw ex2;
  22980. } finally {}
  22981. }
  22982. }
  22983. class PrecisionReducerCoordinateOperation extends GeometryEditor.CoordinateOperation {
  22984. constructor() {
  22985. super();
  22986. PrecisionReducerCoordinateOperation.constructor_.apply(this, arguments);
  22987. }
  22988. static constructor_() {
  22989. this._targetPM = null;
  22990. this._removeCollapsed = true;
  22991. const targetPM = arguments[0],
  22992. removeCollapsed = arguments[1];
  22993. this._targetPM = targetPM;
  22994. this._removeCollapsed = removeCollapsed;
  22995. }
  22996. edit() {
  22997. if (arguments.length === 2 && arguments[1] instanceof Geometry && arguments[0] instanceof Array) {
  22998. const coordinates = arguments[0],
  22999. geom = arguments[1];
  23000. if (coordinates.length === 0) return null;
  23001. const reducedCoords = new Array(coordinates.length).fill(null);
  23002. for (let i = 0; i < coordinates.length; i++) {
  23003. const coord = new Coordinate(coordinates[i]);
  23004. this._targetPM.makePrecise(coord);
  23005. reducedCoords[i] = coord;
  23006. }
  23007. const noRepeatedCoordList = new CoordinateList(reducedCoords, false);
  23008. const noRepeatedCoords = noRepeatedCoordList.toCoordinateArray();
  23009. let minLength = 0;
  23010. if (geom instanceof LineString) minLength = 2;
  23011. if (geom instanceof LinearRing) minLength = 4;
  23012. let collapsedCoords = reducedCoords;
  23013. if (this._removeCollapsed) collapsedCoords = null;
  23014. if (noRepeatedCoords.length < minLength) return collapsedCoords;
  23015. return noRepeatedCoords;
  23016. } else {
  23017. return super.edit.apply(this, arguments);
  23018. }
  23019. }
  23020. }
  23021. class GeometryPrecisionReducer {
  23022. constructor() {
  23023. GeometryPrecisionReducer.constructor_.apply(this, arguments);
  23024. }
  23025. static constructor_() {
  23026. this._targetPM = null;
  23027. this._removeCollapsed = true;
  23028. this._changePrecisionModel = false;
  23029. this._isPointwise = false;
  23030. const pm = arguments[0];
  23031. this._targetPM = pm;
  23032. }
  23033. static reduce(g, precModel) {
  23034. const reducer = new GeometryPrecisionReducer(precModel);
  23035. return reducer.reduce(g);
  23036. }
  23037. static reducePointwise(g, precModel) {
  23038. const reducer = new GeometryPrecisionReducer(precModel);
  23039. reducer.setPointwise(true);
  23040. return reducer.reduce(g);
  23041. }
  23042. fixPolygonalTopology(geom) {
  23043. let geomToBuffer = geom;
  23044. if (!this._changePrecisionModel) geomToBuffer = this.changePM(geom, this._targetPM);
  23045. const bufGeom = BufferOp.bufferOp(geomToBuffer, 0);
  23046. return bufGeom;
  23047. }
  23048. reducePointwise(geom) {
  23049. let geomEdit = null;
  23050. if (this._changePrecisionModel) {
  23051. const newFactory = this.createFactory(geom.getFactory(), this._targetPM);
  23052. geomEdit = new GeometryEditor(newFactory);
  23053. } else {
  23054. geomEdit = new GeometryEditor();
  23055. }
  23056. let finalRemoveCollapsed = this._removeCollapsed;
  23057. if (geom.getDimension() >= 2) finalRemoveCollapsed = true;
  23058. const reduceGeom = geomEdit.edit(geom, new PrecisionReducerCoordinateOperation(this._targetPM, finalRemoveCollapsed));
  23059. return reduceGeom;
  23060. }
  23061. changePM(geom, newPM) {
  23062. const geomEditor = this.createEditor(geom.getFactory(), newPM);
  23063. return geomEditor.edit(geom, new GeometryEditor.NoOpGeometryOperation());
  23064. }
  23065. setRemoveCollapsedComponents(removeCollapsed) {
  23066. this._removeCollapsed = removeCollapsed;
  23067. }
  23068. createFactory(inputFactory, pm) {
  23069. const newFactory = new GeometryFactory(pm, inputFactory.getSRID(), inputFactory.getCoordinateSequenceFactory());
  23070. return newFactory;
  23071. }
  23072. setChangePrecisionModel(changePrecisionModel) {
  23073. this._changePrecisionModel = changePrecisionModel;
  23074. }
  23075. reduce(geom) {
  23076. const reducePW = this.reducePointwise(geom);
  23077. if (this._isPointwise) return reducePW;
  23078. if (!hasInterface(reducePW, Polygonal)) return reducePW;
  23079. if (IsValidOp.isValid(reducePW)) return reducePW;
  23080. return this.fixPolygonalTopology(reducePW);
  23081. }
  23082. setPointwise(isPointwise) {
  23083. this._isPointwise = isPointwise;
  23084. }
  23085. createEditor(geomFactory, newPM) {
  23086. if (geomFactory.getPrecisionModel() === newPM) return new GeometryEditor();
  23087. const newFactory = this.createFactory(geomFactory, newPM);
  23088. const geomEdit = new GeometryEditor(newFactory);
  23089. return geomEdit;
  23090. }
  23091. }
  23092. class FacetSequence {
  23093. constructor() {
  23094. FacetSequence.constructor_.apply(this, arguments);
  23095. }
  23096. static constructor_() {
  23097. this._geom = null;
  23098. this._pts = null;
  23099. this._start = null;
  23100. this._end = null;
  23101. if (arguments.length === 2) {
  23102. const pts = arguments[0],
  23103. start = arguments[1];
  23104. this._pts = pts;
  23105. this._start = start;
  23106. this._end = start + 1;
  23107. } else if (arguments.length === 3) {
  23108. const pts = arguments[0],
  23109. start = arguments[1],
  23110. end = arguments[2];
  23111. this._pts = pts;
  23112. this._start = start;
  23113. this._end = end;
  23114. } else if (arguments.length === 4) {
  23115. const geom = arguments[0],
  23116. pts = arguments[1],
  23117. start = arguments[2],
  23118. end = arguments[3];
  23119. this._geom = geom;
  23120. this._pts = pts;
  23121. this._start = start;
  23122. this._end = end;
  23123. }
  23124. }
  23125. computeDistanceLineLine(facetSeq, locs) {
  23126. let minDistance = Double.MAX_VALUE;
  23127. for (let i = this._start; i < this._end - 1; i++) {
  23128. const p0 = this._pts.getCoordinate(i);
  23129. const p1 = this._pts.getCoordinate(i + 1);
  23130. for (let j = facetSeq._start; j < facetSeq._end - 1; j++) {
  23131. const q0 = facetSeq._pts.getCoordinate(j);
  23132. const q1 = facetSeq._pts.getCoordinate(j + 1);
  23133. const dist = Distance.segmentToSegment(p0, p1, q0, q1);
  23134. if (dist < minDistance) {
  23135. minDistance = dist;
  23136. if (locs !== null) this.updateNearestLocationsLineLine(i, p0, p1, facetSeq, j, q0, q1, locs);
  23137. if (minDistance <= 0.0) return minDistance;
  23138. }
  23139. }
  23140. }
  23141. return minDistance;
  23142. }
  23143. updateNearestLocationsPointLine(pt, facetSeq, i, q0, q1, locs) {
  23144. locs[0] = new GeometryLocation(this._geom, this._start, new Coordinate(pt));
  23145. const seg = new LineSegment(q0, q1);
  23146. const segClosestPoint = seg.closestPoint(pt);
  23147. locs[1] = new GeometryLocation(facetSeq._geom, i, new Coordinate(segClosestPoint));
  23148. }
  23149. size() {
  23150. return this._end - this._start;
  23151. }
  23152. getCoordinate(index) {
  23153. return this._pts.getCoordinate(this._start + index);
  23154. }
  23155. nearestLocations(facetSeq) {
  23156. const isPoint = this.isPoint();
  23157. const isPointOther = facetSeq.isPoint();
  23158. const locs = new Array(2).fill(null);
  23159. if (isPoint && isPointOther) {
  23160. const pt = this._pts.getCoordinate(this._start);
  23161. const seqPt = facetSeq._pts.getCoordinate(facetSeq._start);
  23162. locs[0] = new GeometryLocation(this._geom, this._start, new Coordinate(pt));
  23163. locs[1] = new GeometryLocation(facetSeq._geom, facetSeq._start, new Coordinate(seqPt));
  23164. } else if (isPoint) {
  23165. const pt = this._pts.getCoordinate(this._start);
  23166. this.computeDistancePointLine(pt, facetSeq, locs);
  23167. } else if (isPointOther) {
  23168. const seqPt = facetSeq._pts.getCoordinate(facetSeq._start);
  23169. this.computeDistancePointLine(seqPt, this, locs);
  23170. const tmp = locs[0];
  23171. locs[0] = locs[1];
  23172. locs[1] = tmp;
  23173. } else {
  23174. this.computeDistanceLineLine(facetSeq, locs);
  23175. }
  23176. return locs;
  23177. }
  23178. getEnvelope() {
  23179. const env = new Envelope();
  23180. for (let i = this._start; i < this._end; i++) env.expandToInclude(this._pts.getX(i), this._pts.getY(i));
  23181. return env;
  23182. }
  23183. updateNearestLocationsLineLine(i, p0, p1, facetSeq, j, q0, q1, locs) {
  23184. const seg0 = new LineSegment(p0, p1);
  23185. const seg1 = new LineSegment(q0, q1);
  23186. const closestPt = seg0.closestPoints(seg1);
  23187. locs[0] = new GeometryLocation(this._geom, i, new Coordinate(closestPt[0]));
  23188. locs[1] = new GeometryLocation(facetSeq._geom, j, new Coordinate(closestPt[1]));
  23189. }
  23190. toString() {
  23191. const buf = new StringBuffer();
  23192. buf.append('LINESTRING ( ');
  23193. const p = new Coordinate();
  23194. for (let i = this._start; i < this._end; i++) {
  23195. if (i > this._start) buf.append(', ');
  23196. this._pts.getCoordinate(i, p);
  23197. buf.append(p.x + ' ' + p.y);
  23198. }
  23199. buf.append(' )');
  23200. return buf.toString();
  23201. }
  23202. computeDistancePointLine(pt, facetSeq, locs) {
  23203. let minDistance = Double.MAX_VALUE;
  23204. for (let i = facetSeq._start; i < facetSeq._end - 1; i++) {
  23205. const q0 = facetSeq._pts.getCoordinate(i);
  23206. const q1 = facetSeq._pts.getCoordinate(i + 1);
  23207. const dist = Distance.pointToSegment(pt, q0, q1);
  23208. if (dist < minDistance) {
  23209. minDistance = dist;
  23210. if (locs !== null) this.updateNearestLocationsPointLine(pt, facetSeq, i, q0, q1, locs);
  23211. if (minDistance <= 0.0) return minDistance;
  23212. }
  23213. }
  23214. return minDistance;
  23215. }
  23216. isPoint() {
  23217. return this._end - this._start === 1;
  23218. }
  23219. distance(facetSeq) {
  23220. const isPoint = this.isPoint();
  23221. const isPointOther = facetSeq.isPoint();
  23222. let distance = null;
  23223. if (isPoint && isPointOther) {
  23224. const pt = this._pts.getCoordinate(this._start);
  23225. const seqPt = facetSeq._pts.getCoordinate(facetSeq._start);
  23226. distance = pt.distance(seqPt);
  23227. } else if (isPoint) {
  23228. const pt = this._pts.getCoordinate(this._start);
  23229. distance = this.computeDistancePointLine(pt, facetSeq, null);
  23230. } else if (isPointOther) {
  23231. const seqPt = facetSeq._pts.getCoordinate(facetSeq._start);
  23232. distance = this.computeDistancePointLine(seqPt, this, null);
  23233. } else {
  23234. distance = this.computeDistanceLineLine(facetSeq, null);
  23235. }
  23236. return distance;
  23237. }
  23238. }
  23239. class FacetSequenceTreeBuilder {
  23240. static addFacetSequences(geom, pts, sections) {
  23241. let i = 0;
  23242. const size = pts.size();
  23243. while (i <= size - 1) {
  23244. let end = i + FacetSequenceTreeBuilder.FACET_SEQUENCE_SIZE + 1;
  23245. if (end >= size - 1) end = size;
  23246. const sect = new FacetSequence(geom, pts, i, end);
  23247. sections.add(sect);
  23248. i = i + FacetSequenceTreeBuilder.FACET_SEQUENCE_SIZE;
  23249. }
  23250. }
  23251. static computeFacetSequences(g) {
  23252. const sections = new ArrayList();
  23253. g.apply(new class {
  23254. get interfaces_() {
  23255. return [GeometryComponentFilter];
  23256. }
  23257. filter(geom) {
  23258. let seq = null;
  23259. if (geom instanceof LineString) {
  23260. seq = geom.getCoordinateSequence();
  23261. FacetSequenceTreeBuilder.addFacetSequences(geom, seq, sections);
  23262. } else if (geom instanceof Point) {
  23263. seq = geom.getCoordinateSequence();
  23264. FacetSequenceTreeBuilder.addFacetSequences(geom, seq, sections);
  23265. }
  23266. }
  23267. }());
  23268. return sections;
  23269. }
  23270. static build(g) {
  23271. const tree = new STRtree(FacetSequenceTreeBuilder.STR_TREE_NODE_CAPACITY);
  23272. const sections = FacetSequenceTreeBuilder.computeFacetSequences(g);
  23273. for (let i = sections.iterator(); i.hasNext();) {
  23274. const section = i.next();
  23275. tree.insert(section.getEnvelope(), section);
  23276. }
  23277. tree.build();
  23278. return tree;
  23279. }
  23280. }
  23281. FacetSequenceTreeBuilder.FACET_SEQUENCE_SIZE = 6;
  23282. FacetSequenceTreeBuilder.STR_TREE_NODE_CAPACITY = 4;
  23283. class MinimumClearance {
  23284. constructor() {
  23285. MinimumClearance.constructor_.apply(this, arguments);
  23286. }
  23287. static constructor_() {
  23288. this._inputGeom = null;
  23289. this._minClearance = null;
  23290. this._minClearancePts = null;
  23291. const geom = arguments[0];
  23292. this._inputGeom = geom;
  23293. }
  23294. static getLine(g) {
  23295. const rp = new MinimumClearance(g);
  23296. return rp.getLine();
  23297. }
  23298. static getDistance(g) {
  23299. const rp = new MinimumClearance(g);
  23300. return rp.getDistance();
  23301. }
  23302. getLine() {
  23303. this.compute();
  23304. if (this._minClearancePts === null || this._minClearancePts[0] === null) return this._inputGeom.getFactory().createLineString();
  23305. return this._inputGeom.getFactory().createLineString(this._minClearancePts);
  23306. }
  23307. compute() {
  23308. if (this._minClearancePts !== null) return null;
  23309. this._minClearancePts = new Array(2).fill(null);
  23310. this._minClearance = Double.MAX_VALUE;
  23311. if (this._inputGeom.isEmpty()) return null;
  23312. const geomTree = FacetSequenceTreeBuilder.build(this._inputGeom);
  23313. const nearest = geomTree.nearestNeighbour(new MinClearanceDistance());
  23314. const mcd = new MinClearanceDistance();
  23315. this._minClearance = mcd.distance(nearest[0], nearest[1]);
  23316. this._minClearancePts = mcd.getCoordinates();
  23317. }
  23318. getDistance() {
  23319. this.compute();
  23320. return this._minClearance;
  23321. }
  23322. }
  23323. class MinClearanceDistance {
  23324. constructor() {
  23325. MinClearanceDistance.constructor_.apply(this, arguments);
  23326. }
  23327. static constructor_() {
  23328. this._minDist = Double.MAX_VALUE;
  23329. this._minPts = new Array(2).fill(null);
  23330. }
  23331. vertexDistance(fs1, fs2) {
  23332. for (let i1 = 0; i1 < fs1.size(); i1++) for (let i2 = 0; i2 < fs2.size(); i2++) {
  23333. const p1 = fs1.getCoordinate(i1);
  23334. const p2 = fs2.getCoordinate(i2);
  23335. if (!p1.equals2D(p2)) {
  23336. const d = p1.distance(p2);
  23337. if (d < this._minDist) {
  23338. this._minDist = d;
  23339. this._minPts[0] = p1;
  23340. this._minPts[1] = p2;
  23341. if (d === 0.0) return d;
  23342. }
  23343. }
  23344. }
  23345. return this._minDist;
  23346. }
  23347. getCoordinates() {
  23348. return this._minPts;
  23349. }
  23350. segmentDistance(fs1, fs2) {
  23351. for (let i1 = 0; i1 < fs1.size(); i1++) for (let i2 = 1; i2 < fs2.size(); i2++) {
  23352. const p = fs1.getCoordinate(i1);
  23353. const seg0 = fs2.getCoordinate(i2 - 1);
  23354. const seg1 = fs2.getCoordinate(i2);
  23355. if (!(p.equals2D(seg0) || p.equals2D(seg1))) {
  23356. const d = Distance.pointToSegment(p, seg0, seg1);
  23357. if (d < this._minDist) {
  23358. this._minDist = d;
  23359. this.updatePts(p, seg0, seg1);
  23360. if (d === 0.0) return d;
  23361. }
  23362. }
  23363. }
  23364. return this._minDist;
  23365. }
  23366. distance() {
  23367. if (arguments[0] instanceof ItemBoundable && arguments[1] instanceof ItemBoundable) {
  23368. const b1 = arguments[0],
  23369. b2 = arguments[1];
  23370. const fs1 = b1.getItem();
  23371. const fs2 = b2.getItem();
  23372. this._minDist = Double.MAX_VALUE;
  23373. return this.distance(fs1, fs2);
  23374. } else if (arguments[0] instanceof FacetSequence && arguments[1] instanceof FacetSequence) {
  23375. const fs1 = arguments[0],
  23376. fs2 = arguments[1];
  23377. this.vertexDistance(fs1, fs2);
  23378. if (fs1.size() === 1 && fs2.size() === 1) return this._minDist;
  23379. if (this._minDist <= 0.0) return this._minDist;
  23380. this.segmentDistance(fs1, fs2);
  23381. if (this._minDist <= 0.0) return this._minDist;
  23382. this.segmentDistance(fs2, fs1);
  23383. return this._minDist;
  23384. }
  23385. }
  23386. updatePts(p, seg0, seg1) {
  23387. this._minPts[0] = p;
  23388. const seg = new LineSegment(seg0, seg1);
  23389. this._minPts[1] = new Coordinate(seg.closestPoint(p));
  23390. }
  23391. get interfaces_() {
  23392. return [ItemDistance];
  23393. }
  23394. }
  23395. MinimumClearance.MinClearanceDistance = MinClearanceDistance;
  23396. class SimpleMinimumClearance {
  23397. constructor() {
  23398. SimpleMinimumClearance.constructor_.apply(this, arguments);
  23399. }
  23400. static constructor_() {
  23401. this._inputGeom = null;
  23402. this._minClearance = null;
  23403. this._minClearancePts = null;
  23404. const geom = arguments[0];
  23405. this._inputGeom = geom;
  23406. }
  23407. static getLine(g) {
  23408. const rp = new SimpleMinimumClearance(g);
  23409. return rp.getLine();
  23410. }
  23411. static getDistance(g) {
  23412. const rp = new SimpleMinimumClearance(g);
  23413. return rp.getDistance();
  23414. }
  23415. getLine() {
  23416. this.compute();
  23417. return this._inputGeom.getFactory().createLineString(this._minClearancePts);
  23418. }
  23419. updateClearance() {
  23420. if (arguments.length === 3) {
  23421. const candidateValue = arguments[0],
  23422. p0 = arguments[1],
  23423. p1 = arguments[2];
  23424. if (candidateValue < this._minClearance) {
  23425. this._minClearance = candidateValue;
  23426. this._minClearancePts[0] = new Coordinate(p0);
  23427. this._minClearancePts[1] = new Coordinate(p1);
  23428. }
  23429. } else if (arguments.length === 4) {
  23430. const candidateValue = arguments[0],
  23431. p = arguments[1],
  23432. seg0 = arguments[2],
  23433. seg1 = arguments[3];
  23434. if (candidateValue < this._minClearance) {
  23435. this._minClearance = candidateValue;
  23436. this._minClearancePts[0] = new Coordinate(p);
  23437. const seg = new LineSegment(seg0, seg1);
  23438. this._minClearancePts[1] = new Coordinate(seg.closestPoint(p));
  23439. }
  23440. }
  23441. }
  23442. compute() {
  23443. if (this._minClearancePts !== null) return null;
  23444. this._minClearancePts = new Array(2).fill(null);
  23445. this._minClearance = Double.MAX_VALUE;
  23446. this._inputGeom.apply(new VertexCoordinateFilter(this));
  23447. }
  23448. getDistance() {
  23449. this.compute();
  23450. return this._minClearance;
  23451. }
  23452. }
  23453. class VertexCoordinateFilter {
  23454. constructor() {
  23455. VertexCoordinateFilter.constructor_.apply(this, arguments);
  23456. }
  23457. static constructor_() {
  23458. this.smc = null;
  23459. const smc = arguments[0];
  23460. this.smc = smc;
  23461. }
  23462. filter(coord) {
  23463. this.smc._inputGeom.apply(new ComputeMCCoordinateSequenceFilter(this.smc, coord));
  23464. }
  23465. get interfaces_() {
  23466. return [CoordinateFilter];
  23467. }
  23468. }
  23469. class ComputeMCCoordinateSequenceFilter {
  23470. constructor() {
  23471. ComputeMCCoordinateSequenceFilter.constructor_.apply(this, arguments);
  23472. }
  23473. static constructor_() {
  23474. this.smc = null;
  23475. this._queryPt = null;
  23476. const smc = arguments[0],
  23477. queryPt = arguments[1];
  23478. this.smc = smc;
  23479. this._queryPt = queryPt;
  23480. }
  23481. isGeometryChanged() {
  23482. return false;
  23483. }
  23484. checkVertexDistance(vertex) {
  23485. const vertexDist = vertex.distance(this._queryPt);
  23486. if (vertexDist > 0) this.smc.updateClearance(vertexDist, this._queryPt, vertex);
  23487. }
  23488. filter(seq, i) {
  23489. this.checkVertexDistance(seq.getCoordinate(i));
  23490. if (i > 0) this.checkSegmentDistance(seq.getCoordinate(i - 1), seq.getCoordinate(i));
  23491. }
  23492. checkSegmentDistance(seg0, seg1) {
  23493. if (this._queryPt.equals2D(seg0) || this._queryPt.equals2D(seg1)) return null;
  23494. const segDist = Distance.pointToSegment(this._queryPt, seg1, seg0);
  23495. if (segDist > 0) this.smc.updateClearance(segDist, this._queryPt, seg1, seg0);
  23496. }
  23497. isDone() {
  23498. return false;
  23499. }
  23500. get interfaces_() {
  23501. return [CoordinateSequenceFilter];
  23502. }
  23503. }
  23504. SimpleMinimumClearance.VertexCoordinateFilter = VertexCoordinateFilter;
  23505. SimpleMinimumClearance.ComputeMCCoordinateSequenceFilter = ComputeMCCoordinateSequenceFilter;
  23506. var precision = /*#__PURE__*/Object.freeze({
  23507. __proto__: null,
  23508. CommonBits: CommonBits,
  23509. CommonBitsOp: CommonBitsOp,
  23510. CommonBitsRemover: CommonBitsRemover,
  23511. EnhancedPrecisionOp: EnhancedPrecisionOp,
  23512. GeometryPrecisionReducer: GeometryPrecisionReducer,
  23513. MinimumClearance: MinimumClearance,
  23514. SimpleMinimumClearance: SimpleMinimumClearance
  23515. });
  23516. class DouglasPeuckerLineSimplifier {
  23517. constructor() {
  23518. DouglasPeuckerLineSimplifier.constructor_.apply(this, arguments);
  23519. }
  23520. static constructor_() {
  23521. this._pts = null;
  23522. this._usePt = null;
  23523. this._distanceTolerance = null;
  23524. this._seg = new LineSegment();
  23525. const pts = arguments[0];
  23526. this._pts = pts;
  23527. }
  23528. static simplify(pts, distanceTolerance) {
  23529. const simp = new DouglasPeuckerLineSimplifier(pts);
  23530. simp.setDistanceTolerance(distanceTolerance);
  23531. return simp.simplify();
  23532. }
  23533. simplifySection(i, j) {
  23534. if (i + 1 === j) return null;
  23535. this._seg.p0 = this._pts[i];
  23536. this._seg.p1 = this._pts[j];
  23537. let maxDistance = -1.0;
  23538. let maxIndex = i;
  23539. for (let k = i + 1; k < j; k++) {
  23540. const distance = this._seg.distance(this._pts[k]);
  23541. if (distance > maxDistance) {
  23542. maxDistance = distance;
  23543. maxIndex = k;
  23544. }
  23545. }
  23546. if (maxDistance <= this._distanceTolerance) {
  23547. for (let k = i + 1; k < j; k++) this._usePt[k] = false;
  23548. } else {
  23549. this.simplifySection(i, maxIndex);
  23550. this.simplifySection(maxIndex, j);
  23551. }
  23552. }
  23553. setDistanceTolerance(distanceTolerance) {
  23554. this._distanceTolerance = distanceTolerance;
  23555. }
  23556. simplify() {
  23557. this._usePt = new Array(this._pts.length).fill(null);
  23558. for (let i = 0; i < this._pts.length; i++) this._usePt[i] = true;
  23559. this.simplifySection(0, this._pts.length - 1);
  23560. const coordList = new CoordinateList();
  23561. for (let i = 0; i < this._pts.length; i++) if (this._usePt[i]) coordList.add(new Coordinate(this._pts[i]));
  23562. return coordList.toCoordinateArray();
  23563. }
  23564. }
  23565. class DouglasPeuckerSimplifier {
  23566. constructor() {
  23567. DouglasPeuckerSimplifier.constructor_.apply(this, arguments);
  23568. }
  23569. static constructor_() {
  23570. this._inputGeom = null;
  23571. this._distanceTolerance = null;
  23572. this._isEnsureValidTopology = true;
  23573. const inputGeom = arguments[0];
  23574. this._inputGeom = inputGeom;
  23575. }
  23576. static simplify(geom, distanceTolerance) {
  23577. const tss = new DouglasPeuckerSimplifier(geom);
  23578. tss.setDistanceTolerance(distanceTolerance);
  23579. return tss.getResultGeometry();
  23580. }
  23581. setEnsureValid(isEnsureValidTopology) {
  23582. this._isEnsureValidTopology = isEnsureValidTopology;
  23583. }
  23584. getResultGeometry() {
  23585. if (this._inputGeom.isEmpty()) return this._inputGeom.copy();
  23586. return new DPTransformer(this._isEnsureValidTopology, this._distanceTolerance).transform(this._inputGeom);
  23587. }
  23588. setDistanceTolerance(distanceTolerance) {
  23589. if (distanceTolerance < 0.0) throw new IllegalArgumentException('Tolerance must be non-negative');
  23590. this._distanceTolerance = distanceTolerance;
  23591. }
  23592. }
  23593. class DPTransformer extends GeometryTransformer {
  23594. constructor() {
  23595. super();
  23596. DPTransformer.constructor_.apply(this, arguments);
  23597. }
  23598. static constructor_() {
  23599. this._isEnsureValidTopology = true;
  23600. this._distanceTolerance = null;
  23601. const isEnsureValidTopology = arguments[0],
  23602. distanceTolerance = arguments[1];
  23603. this._isEnsureValidTopology = isEnsureValidTopology;
  23604. this._distanceTolerance = distanceTolerance;
  23605. }
  23606. transformPolygon(geom, parent) {
  23607. if (geom.isEmpty()) return null;
  23608. const rawGeom = super.transformPolygon.call(this, geom, parent);
  23609. if (parent instanceof MultiPolygon) return rawGeom;
  23610. return this.createValidArea(rawGeom);
  23611. }
  23612. createValidArea(rawAreaGeom) {
  23613. if (this._isEnsureValidTopology) return rawAreaGeom.buffer(0.0);
  23614. return rawAreaGeom;
  23615. }
  23616. transformCoordinates(coords, parent) {
  23617. const inputPts = coords.toCoordinateArray();
  23618. let newPts = null;
  23619. if (inputPts.length === 0) newPts = new Array(0).fill(null);else newPts = DouglasPeuckerLineSimplifier.simplify(inputPts, this._distanceTolerance);
  23620. return this._factory.getCoordinateSequenceFactory().create(newPts);
  23621. }
  23622. transformMultiPolygon(geom, parent) {
  23623. const rawGeom = super.transformMultiPolygon.call(this, geom, parent);
  23624. return this.createValidArea(rawGeom);
  23625. }
  23626. transformLinearRing(geom, parent) {
  23627. const removeDegenerateRings = parent instanceof Polygon;
  23628. const simpResult = super.transformLinearRing.call(this, geom, parent);
  23629. if (removeDegenerateRings && !(simpResult instanceof LinearRing)) return null;
  23630. return simpResult;
  23631. }
  23632. }
  23633. DouglasPeuckerSimplifier.DPTransformer = DPTransformer;
  23634. class TaggedLineSegment extends LineSegment {
  23635. constructor() {
  23636. super();
  23637. TaggedLineSegment.constructor_.apply(this, arguments);
  23638. }
  23639. static constructor_() {
  23640. this._parent = null;
  23641. this._index = null;
  23642. if (arguments.length === 2) {
  23643. const p0 = arguments[0],
  23644. p1 = arguments[1];
  23645. TaggedLineSegment.constructor_.call(this, p0, p1, null, -1);
  23646. } else if (arguments.length === 4) {
  23647. const p0 = arguments[0],
  23648. p1 = arguments[1],
  23649. parent = arguments[2],
  23650. index = arguments[3];
  23651. LineSegment.constructor_.call(this, p0, p1);
  23652. this._parent = parent;
  23653. this._index = index;
  23654. }
  23655. }
  23656. getIndex() {
  23657. return this._index;
  23658. }
  23659. getParent() {
  23660. return this._parent;
  23661. }
  23662. }
  23663. class TaggedLineString {
  23664. constructor() {
  23665. TaggedLineString.constructor_.apply(this, arguments);
  23666. }
  23667. static constructor_() {
  23668. this._parentLine = null;
  23669. this._segs = null;
  23670. this._resultSegs = new ArrayList();
  23671. this._minimumSize = null;
  23672. if (arguments.length === 1) {
  23673. const parentLine = arguments[0];
  23674. TaggedLineString.constructor_.call(this, parentLine, 2);
  23675. } else if (arguments.length === 2) {
  23676. const parentLine = arguments[0],
  23677. minimumSize = arguments[1];
  23678. this._parentLine = parentLine;
  23679. this._minimumSize = minimumSize;
  23680. this.init();
  23681. }
  23682. }
  23683. static extractCoordinates(segs) {
  23684. const pts = new Array(segs.size() + 1).fill(null);
  23685. let seg = null;
  23686. for (let i = 0; i < segs.size(); i++) {
  23687. seg = segs.get(i);
  23688. pts[i] = seg.p0;
  23689. }
  23690. pts[pts.length - 1] = seg.p1;
  23691. return pts;
  23692. }
  23693. addToResult(seg) {
  23694. this._resultSegs.add(seg);
  23695. }
  23696. asLineString() {
  23697. return this._parentLine.getFactory().createLineString(TaggedLineString.extractCoordinates(this._resultSegs));
  23698. }
  23699. getResultSize() {
  23700. const resultSegsSize = this._resultSegs.size();
  23701. return resultSegsSize === 0 ? 0 : resultSegsSize + 1;
  23702. }
  23703. getParent() {
  23704. return this._parentLine;
  23705. }
  23706. getSegment(i) {
  23707. return this._segs[i];
  23708. }
  23709. getParentCoordinates() {
  23710. return this._parentLine.getCoordinates();
  23711. }
  23712. getMinimumSize() {
  23713. return this._minimumSize;
  23714. }
  23715. asLinearRing() {
  23716. return this._parentLine.getFactory().createLinearRing(TaggedLineString.extractCoordinates(this._resultSegs));
  23717. }
  23718. getSegments() {
  23719. return this._segs;
  23720. }
  23721. init() {
  23722. const pts = this._parentLine.getCoordinates();
  23723. this._segs = new Array(pts.length - 1).fill(null);
  23724. for (let i = 0; i < pts.length - 1; i++) {
  23725. const seg = new TaggedLineSegment(pts[i], pts[i + 1], this._parentLine, i);
  23726. this._segs[i] = seg;
  23727. }
  23728. }
  23729. getResultCoordinates() {
  23730. return TaggedLineString.extractCoordinates(this._resultSegs);
  23731. }
  23732. }
  23733. class LineSegmentIndex {
  23734. constructor() {
  23735. LineSegmentIndex.constructor_.apply(this, arguments);
  23736. }
  23737. static constructor_() {
  23738. this._index = new Quadtree();
  23739. }
  23740. remove(seg) {
  23741. this._index.remove(new Envelope(seg.p0, seg.p1), seg);
  23742. }
  23743. add() {
  23744. if (arguments[0] instanceof TaggedLineString) {
  23745. const line = arguments[0];
  23746. const segs = line.getSegments();
  23747. for (let i = 0; i < segs.length; i++) {
  23748. const seg = segs[i];
  23749. this.add(seg);
  23750. }
  23751. } else if (arguments[0] instanceof LineSegment) {
  23752. const seg = arguments[0];
  23753. this._index.insert(new Envelope(seg.p0, seg.p1), seg);
  23754. }
  23755. }
  23756. query(querySeg) {
  23757. const env = new Envelope(querySeg.p0, querySeg.p1);
  23758. const visitor = new LineSegmentVisitor(querySeg);
  23759. this._index.query(env, visitor);
  23760. const itemsFound = visitor.getItems();
  23761. return itemsFound;
  23762. }
  23763. }
  23764. class LineSegmentVisitor {
  23765. constructor() {
  23766. LineSegmentVisitor.constructor_.apply(this, arguments);
  23767. }
  23768. static constructor_() {
  23769. this._querySeg = null;
  23770. this._items = new ArrayList();
  23771. const querySeg = arguments[0];
  23772. this._querySeg = querySeg;
  23773. }
  23774. visitItem(item) {
  23775. const seg = item;
  23776. if (Envelope.intersects(seg.p0, seg.p1, this._querySeg.p0, this._querySeg.p1)) this._items.add(item);
  23777. }
  23778. getItems() {
  23779. return this._items;
  23780. }
  23781. get interfaces_() {
  23782. return [ItemVisitor];
  23783. }
  23784. }
  23785. class TaggedLineStringSimplifier {
  23786. constructor() {
  23787. TaggedLineStringSimplifier.constructor_.apply(this, arguments);
  23788. }
  23789. static constructor_() {
  23790. this._li = new RobustLineIntersector();
  23791. this._inputIndex = new LineSegmentIndex();
  23792. this._outputIndex = new LineSegmentIndex();
  23793. this._line = null;
  23794. this._linePts = null;
  23795. this._distanceTolerance = 0.0;
  23796. const inputIndex = arguments[0],
  23797. outputIndex = arguments[1];
  23798. this._inputIndex = inputIndex;
  23799. this._outputIndex = outputIndex;
  23800. }
  23801. static isInLineSection(line, sectionIndex, seg) {
  23802. if (seg.getParent() !== line.getParent()) return false;
  23803. const segIndex = seg.getIndex();
  23804. if (segIndex >= sectionIndex[0] && segIndex < sectionIndex[1]) return true;
  23805. return false;
  23806. }
  23807. flatten(start, end) {
  23808. const p0 = this._linePts[start];
  23809. const p1 = this._linePts[end];
  23810. const newSeg = new LineSegment(p0, p1);
  23811. this.remove(this._line, start, end);
  23812. this._outputIndex.add(newSeg);
  23813. return newSeg;
  23814. }
  23815. hasBadIntersection(parentLine, sectionIndex, candidateSeg) {
  23816. if (this.hasBadOutputIntersection(candidateSeg)) return true;
  23817. if (this.hasBadInputIntersection(parentLine, sectionIndex, candidateSeg)) return true;
  23818. return false;
  23819. }
  23820. setDistanceTolerance(distanceTolerance) {
  23821. this._distanceTolerance = distanceTolerance;
  23822. }
  23823. simplifySection(i, j, depth) {
  23824. depth += 1;
  23825. const sectionIndex = new Array(2).fill(null);
  23826. if (i + 1 === j) {
  23827. const newSeg = this._line.getSegment(i);
  23828. this._line.addToResult(newSeg);
  23829. return null;
  23830. }
  23831. let isValidToSimplify = true;
  23832. if (this._line.getResultSize() < this._line.getMinimumSize()) {
  23833. const worstCaseSize = depth + 1;
  23834. if (worstCaseSize < this._line.getMinimumSize()) isValidToSimplify = false;
  23835. }
  23836. const distance = new Array(1).fill(null);
  23837. const furthestPtIndex = this.findFurthestPoint(this._linePts, i, j, distance);
  23838. if (distance[0] > this._distanceTolerance) isValidToSimplify = false;
  23839. const candidateSeg = new LineSegment();
  23840. candidateSeg.p0 = this._linePts[i];
  23841. candidateSeg.p1 = this._linePts[j];
  23842. sectionIndex[0] = i;
  23843. sectionIndex[1] = j;
  23844. if (this.hasBadIntersection(this._line, sectionIndex, candidateSeg)) isValidToSimplify = false;
  23845. if (isValidToSimplify) {
  23846. const newSeg = this.flatten(i, j);
  23847. this._line.addToResult(newSeg);
  23848. return null;
  23849. }
  23850. this.simplifySection(i, furthestPtIndex, depth);
  23851. this.simplifySection(furthestPtIndex, j, depth);
  23852. }
  23853. hasBadOutputIntersection(candidateSeg) {
  23854. const querySegs = this._outputIndex.query(candidateSeg);
  23855. for (let i = querySegs.iterator(); i.hasNext();) {
  23856. const querySeg = i.next();
  23857. if (this.hasInteriorIntersection(querySeg, candidateSeg)) return true;
  23858. }
  23859. return false;
  23860. }
  23861. findFurthestPoint(pts, i, j, maxDistance) {
  23862. const seg = new LineSegment();
  23863. seg.p0 = pts[i];
  23864. seg.p1 = pts[j];
  23865. let maxDist = -1.0;
  23866. let maxIndex = i;
  23867. for (let k = i + 1; k < j; k++) {
  23868. const midPt = pts[k];
  23869. const distance = seg.distance(midPt);
  23870. if (distance > maxDist) {
  23871. maxDist = distance;
  23872. maxIndex = k;
  23873. }
  23874. }
  23875. maxDistance[0] = maxDist;
  23876. return maxIndex;
  23877. }
  23878. simplify(line) {
  23879. this._line = line;
  23880. this._linePts = line.getParentCoordinates();
  23881. this.simplifySection(0, this._linePts.length - 1, 0);
  23882. }
  23883. remove(line, start, end) {
  23884. for (let i = start; i < end; i++) {
  23885. const seg = line.getSegment(i);
  23886. this._inputIndex.remove(seg);
  23887. }
  23888. }
  23889. hasInteriorIntersection(seg0, seg1) {
  23890. this._li.computeIntersection(seg0.p0, seg0.p1, seg1.p0, seg1.p1);
  23891. return this._li.isInteriorIntersection();
  23892. }
  23893. hasBadInputIntersection(parentLine, sectionIndex, candidateSeg) {
  23894. const querySegs = this._inputIndex.query(candidateSeg);
  23895. for (let i = querySegs.iterator(); i.hasNext();) {
  23896. const querySeg = i.next();
  23897. if (this.hasInteriorIntersection(querySeg, candidateSeg)) {
  23898. if (TaggedLineStringSimplifier.isInLineSection(parentLine, sectionIndex, querySeg)) continue;
  23899. return true;
  23900. }
  23901. }
  23902. return false;
  23903. }
  23904. }
  23905. class TaggedLinesSimplifier {
  23906. constructor() {
  23907. TaggedLinesSimplifier.constructor_.apply(this, arguments);
  23908. }
  23909. static constructor_() {
  23910. this._inputIndex = new LineSegmentIndex();
  23911. this._outputIndex = new LineSegmentIndex();
  23912. this._distanceTolerance = 0.0;
  23913. }
  23914. setDistanceTolerance(distanceTolerance) {
  23915. this._distanceTolerance = distanceTolerance;
  23916. }
  23917. simplify(taggedLines) {
  23918. for (let i = taggedLines.iterator(); i.hasNext();) this._inputIndex.add(i.next());
  23919. for (let i = taggedLines.iterator(); i.hasNext();) {
  23920. const tlss = new TaggedLineStringSimplifier(this._inputIndex, this._outputIndex);
  23921. tlss.setDistanceTolerance(this._distanceTolerance);
  23922. tlss.simplify(i.next());
  23923. }
  23924. }
  23925. }
  23926. class TopologyPreservingSimplifier {
  23927. constructor() {
  23928. TopologyPreservingSimplifier.constructor_.apply(this, arguments);
  23929. }
  23930. static constructor_() {
  23931. this._inputGeom = null;
  23932. this._lineSimplifier = new TaggedLinesSimplifier();
  23933. this._linestringMap = null;
  23934. const inputGeom = arguments[0];
  23935. this._inputGeom = inputGeom;
  23936. }
  23937. static simplify(geom, distanceTolerance) {
  23938. const tss = new TopologyPreservingSimplifier(geom);
  23939. tss.setDistanceTolerance(distanceTolerance);
  23940. return tss.getResultGeometry();
  23941. }
  23942. getResultGeometry() {
  23943. if (this._inputGeom.isEmpty()) return this._inputGeom.copy();
  23944. this._linestringMap = new HashMap();
  23945. this._inputGeom.apply(new LineStringMapBuilderFilter(this));
  23946. this._lineSimplifier.simplify(this._linestringMap.values());
  23947. const result = new LineStringTransformer(this._linestringMap).transform(this._inputGeom);
  23948. return result;
  23949. }
  23950. setDistanceTolerance(distanceTolerance) {
  23951. if (distanceTolerance < 0.0) throw new IllegalArgumentException('Tolerance must be non-negative');
  23952. this._lineSimplifier.setDistanceTolerance(distanceTolerance);
  23953. }
  23954. }
  23955. class LineStringTransformer extends GeometryTransformer {
  23956. constructor() {
  23957. super();
  23958. LineStringTransformer.constructor_.apply(this, arguments);
  23959. }
  23960. static constructor_() {
  23961. this._linestringMap = null;
  23962. const linestringMap = arguments[0];
  23963. this._linestringMap = linestringMap;
  23964. }
  23965. transformCoordinates(coords, parent) {
  23966. if (coords.size() === 0) return null;
  23967. if (parent instanceof LineString) {
  23968. const taggedLine = this._linestringMap.get(parent);
  23969. return this.createCoordinateSequence(taggedLine.getResultCoordinates());
  23970. }
  23971. return super.transformCoordinates.call(this, coords, parent);
  23972. }
  23973. }
  23974. class LineStringMapBuilderFilter {
  23975. constructor() {
  23976. LineStringMapBuilderFilter.constructor_.apply(this, arguments);
  23977. }
  23978. static constructor_() {
  23979. this.tps = null;
  23980. const tps = arguments[0];
  23981. this.tps = tps;
  23982. }
  23983. filter(geom) {
  23984. if (geom instanceof LineString) {
  23985. const line = geom;
  23986. if (line.isEmpty()) return null;
  23987. const minSize = line.isClosed() ? 4 : 2;
  23988. const taggedLine = new TaggedLineString(line, minSize);
  23989. this.tps._linestringMap.put(line, taggedLine);
  23990. }
  23991. }
  23992. get interfaces_() {
  23993. return [GeometryComponentFilter];
  23994. }
  23995. }
  23996. TopologyPreservingSimplifier.LineStringTransformer = LineStringTransformer;
  23997. TopologyPreservingSimplifier.LineStringMapBuilderFilter = LineStringMapBuilderFilter;
  23998. class VWLineSimplifier {
  23999. constructor() {
  24000. VWLineSimplifier.constructor_.apply(this, arguments);
  24001. }
  24002. static constructor_() {
  24003. this._pts = null;
  24004. this._tolerance = null;
  24005. const pts = arguments[0],
  24006. distanceTolerance = arguments[1];
  24007. this._pts = pts;
  24008. this._tolerance = distanceTolerance * distanceTolerance;
  24009. }
  24010. static simplify(pts, distanceTolerance) {
  24011. const simp = new VWLineSimplifier(pts, distanceTolerance);
  24012. return simp.simplify();
  24013. }
  24014. simplifyVertex(vwLine) {
  24015. let curr = vwLine;
  24016. let minArea = curr.getArea();
  24017. let minVertex = null;
  24018. while (curr !== null) {
  24019. const area = curr.getArea();
  24020. if (area < minArea) {
  24021. minArea = area;
  24022. minVertex = curr;
  24023. }
  24024. curr = curr._next;
  24025. }
  24026. if (minVertex !== null && minArea < this._tolerance) minVertex.remove();
  24027. if (!vwLine.isLive()) return -1;
  24028. return minArea;
  24029. }
  24030. simplify() {
  24031. const vwLine = VWVertex.buildLine(this._pts);
  24032. let minArea = this._tolerance;
  24033. do minArea = this.simplifyVertex(vwLine); while (minArea < this._tolerance);
  24034. const simp = vwLine.getCoordinates();
  24035. if (simp.length < 2) return [simp[0], new Coordinate(simp[0])];
  24036. return simp;
  24037. }
  24038. }
  24039. class VWVertex {
  24040. constructor() {
  24041. VWVertex.constructor_.apply(this, arguments);
  24042. }
  24043. static constructor_() {
  24044. this._pt = null;
  24045. this._prev = null;
  24046. this._next = null;
  24047. this._area = VWVertex.MAX_AREA;
  24048. this._isLive = true;
  24049. const pt = arguments[0];
  24050. this._pt = pt;
  24051. }
  24052. static buildLine(pts) {
  24053. let first = null;
  24054. let prev = null;
  24055. for (let i = 0; i < pts.length; i++) {
  24056. const v = new VWVertex(pts[i]);
  24057. if (first === null) first = v;
  24058. v.setPrev(prev);
  24059. if (prev !== null) {
  24060. prev.setNext(v);
  24061. prev.updateArea();
  24062. }
  24063. prev = v;
  24064. }
  24065. return first;
  24066. }
  24067. getCoordinates() {
  24068. const coords = new CoordinateList();
  24069. let curr = this;
  24070. do {
  24071. coords.add(curr._pt, false);
  24072. curr = curr._next;
  24073. } while (curr !== null);
  24074. return coords.toCoordinateArray();
  24075. }
  24076. getArea() {
  24077. return this._area;
  24078. }
  24079. updateArea() {
  24080. if (this._prev === null || this._next === null) {
  24081. this._area = VWVertex.MAX_AREA;
  24082. return null;
  24083. }
  24084. this._area = Math.abs(Triangle.area(this._prev._pt, this._pt, this._next._pt));
  24085. }
  24086. remove() {
  24087. const tmpPrev = this._prev;
  24088. const tmpNext = this._next;
  24089. let result = null;
  24090. if (this._prev !== null) {
  24091. this._prev.setNext(tmpNext);
  24092. this._prev.updateArea();
  24093. result = this._prev;
  24094. }
  24095. if (this._next !== null) {
  24096. this._next.setPrev(tmpPrev);
  24097. this._next.updateArea();
  24098. if (result === null) result = this._next;
  24099. }
  24100. this._isLive = false;
  24101. return result;
  24102. }
  24103. isLive() {
  24104. return this._isLive;
  24105. }
  24106. setPrev(prev) {
  24107. this._prev = prev;
  24108. }
  24109. setNext(next) {
  24110. this._next = next;
  24111. }
  24112. }
  24113. VWVertex.MAX_AREA = Double.MAX_VALUE;
  24114. VWLineSimplifier.VWVertex = VWVertex;
  24115. class VWSimplifier {
  24116. constructor() {
  24117. VWSimplifier.constructor_.apply(this, arguments);
  24118. }
  24119. static constructor_() {
  24120. this._inputGeom = null;
  24121. this._distanceTolerance = null;
  24122. this._isEnsureValidTopology = true;
  24123. const inputGeom = arguments[0];
  24124. this._inputGeom = inputGeom;
  24125. }
  24126. static simplify(geom, distanceTolerance) {
  24127. const simp = new VWSimplifier(geom);
  24128. simp.setDistanceTolerance(distanceTolerance);
  24129. return simp.getResultGeometry();
  24130. }
  24131. setEnsureValid(isEnsureValidTopology) {
  24132. this._isEnsureValidTopology = isEnsureValidTopology;
  24133. }
  24134. getResultGeometry() {
  24135. if (this._inputGeom.isEmpty()) return this._inputGeom.copy();
  24136. return new VWTransformer(this._isEnsureValidTopology, this._distanceTolerance).transform(this._inputGeom);
  24137. }
  24138. setDistanceTolerance(distanceTolerance) {
  24139. if (distanceTolerance < 0.0) throw new IllegalArgumentException('Tolerance must be non-negative');
  24140. this._distanceTolerance = distanceTolerance;
  24141. }
  24142. }
  24143. class VWTransformer extends GeometryTransformer {
  24144. constructor() {
  24145. super();
  24146. VWTransformer.constructor_.apply(this, arguments);
  24147. }
  24148. static constructor_() {
  24149. this._isEnsureValidTopology = true;
  24150. this._distanceTolerance = null;
  24151. const isEnsureValidTopology = arguments[0],
  24152. distanceTolerance = arguments[1];
  24153. this._isEnsureValidTopology = isEnsureValidTopology;
  24154. this._distanceTolerance = distanceTolerance;
  24155. }
  24156. transformPolygon(geom, parent) {
  24157. if (geom.isEmpty()) return null;
  24158. const rawGeom = super.transformPolygon.call(this, geom, parent);
  24159. if (parent instanceof MultiPolygon) return rawGeom;
  24160. return this.createValidArea(rawGeom);
  24161. }
  24162. createValidArea(rawAreaGeom) {
  24163. if (this._isEnsureValidTopology) return rawAreaGeom.buffer(0.0);
  24164. return rawAreaGeom;
  24165. }
  24166. transformCoordinates(coords, parent) {
  24167. const inputPts = coords.toCoordinateArray();
  24168. let newPts = null;
  24169. if (inputPts.length === 0) newPts = new Array(0).fill(null);else newPts = VWLineSimplifier.simplify(inputPts, this._distanceTolerance);
  24170. return this._factory.getCoordinateSequenceFactory().create(newPts);
  24171. }
  24172. transformMultiPolygon(geom, parent) {
  24173. const rawGeom = super.transformMultiPolygon.call(this, geom, parent);
  24174. return this.createValidArea(rawGeom);
  24175. }
  24176. transformLinearRing(geom, parent) {
  24177. const removeDegenerateRings = parent instanceof Polygon;
  24178. const simpResult = super.transformLinearRing.call(this, geom, parent);
  24179. if (removeDegenerateRings && !(simpResult instanceof LinearRing)) return null;
  24180. return simpResult;
  24181. }
  24182. }
  24183. VWSimplifier.VWTransformer = VWTransformer;
  24184. var simplify = /*#__PURE__*/Object.freeze({
  24185. __proto__: null,
  24186. DouglasPeuckerSimplifier: DouglasPeuckerSimplifier,
  24187. TopologyPreservingSimplifier: TopologyPreservingSimplifier,
  24188. VWSimplifier: VWSimplifier
  24189. });
  24190. class SplitSegment {
  24191. constructor() {
  24192. SplitSegment.constructor_.apply(this, arguments);
  24193. }
  24194. static constructor_() {
  24195. this._seg = null;
  24196. this._segLen = null;
  24197. this._splitPt = null;
  24198. this._minimumLen = 0.0;
  24199. const seg = arguments[0];
  24200. this._seg = seg;
  24201. this._segLen = seg.getLength();
  24202. }
  24203. static pointAlongReverse(seg, segmentLengthFraction) {
  24204. const coord = new Coordinate();
  24205. coord.x = seg.p1.x - segmentLengthFraction * (seg.p1.x - seg.p0.x);
  24206. coord.y = seg.p1.y - segmentLengthFraction * (seg.p1.y - seg.p0.y);
  24207. return coord;
  24208. }
  24209. splitAt() {
  24210. if (arguments.length === 1) {
  24211. const pt = arguments[0];
  24212. const minFrac = this._minimumLen / this._segLen;
  24213. if (pt.distance(this._seg.p0) < this._minimumLen) {
  24214. this._splitPt = this._seg.pointAlong(minFrac);
  24215. return null;
  24216. }
  24217. if (pt.distance(this._seg.p1) < this._minimumLen) {
  24218. this._splitPt = SplitSegment.pointAlongReverse(this._seg, minFrac);
  24219. return null;
  24220. }
  24221. this._splitPt = pt;
  24222. } else if (arguments.length === 2) {
  24223. const length = arguments[0],
  24224. endPt = arguments[1];
  24225. const actualLen = this.getConstrainedLength(length);
  24226. const frac = actualLen / this._segLen;
  24227. if (endPt.equals2D(this._seg.p0)) this._splitPt = this._seg.pointAlong(frac);else this._splitPt = SplitSegment.pointAlongReverse(this._seg, frac);
  24228. }
  24229. }
  24230. setMinimumLength(minLen) {
  24231. this._minimumLen = minLen;
  24232. }
  24233. getConstrainedLength(len) {
  24234. if (len < this._minimumLen) return this._minimumLen;
  24235. return len;
  24236. }
  24237. getSplitPoint() {
  24238. return this._splitPt;
  24239. }
  24240. }
  24241. class ConstraintSplitPointFinder {
  24242. findSplitPoint(seg, encroachPt) {}
  24243. }
  24244. class NonEncroachingSplitPointFinder {
  24245. static projectedSplitPoint(seg, encroachPt) {
  24246. const lineSeg = seg.getLineSegment();
  24247. const projPt = lineSeg.project(encroachPt);
  24248. return projPt;
  24249. }
  24250. findSplitPoint(seg, encroachPt) {
  24251. const lineSeg = seg.getLineSegment();
  24252. const segLen = lineSeg.getLength();
  24253. const midPtLen = segLen / 2;
  24254. const splitSeg = new SplitSegment(lineSeg);
  24255. const projPt = NonEncroachingSplitPointFinder.projectedSplitPoint(seg, encroachPt);
  24256. const nonEncroachDiam = projPt.distance(encroachPt) * 2 * 0.8;
  24257. let maxSplitLen = nonEncroachDiam;
  24258. if (maxSplitLen > midPtLen) maxSplitLen = midPtLen;
  24259. splitSeg.setMinimumLength(maxSplitLen);
  24260. splitSeg.splitAt(projPt);
  24261. return splitSeg.getSplitPoint();
  24262. }
  24263. get interfaces_() {
  24264. return [ConstraintSplitPointFinder];
  24265. }
  24266. }
  24267. class TrianglePredicate {
  24268. static triArea(a, b, c) {
  24269. return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  24270. }
  24271. static isInCircleDDNormalized(a, b, c, p) {
  24272. const adx = DD.valueOf(a.x).selfSubtract(p.x);
  24273. const ady = DD.valueOf(a.y).selfSubtract(p.y);
  24274. const bdx = DD.valueOf(b.x).selfSubtract(p.x);
  24275. const bdy = DD.valueOf(b.y).selfSubtract(p.y);
  24276. const cdx = DD.valueOf(c.x).selfSubtract(p.x);
  24277. const cdy = DD.valueOf(c.y).selfSubtract(p.y);
  24278. const abdet = adx.multiply(bdy).selfSubtract(bdx.multiply(ady));
  24279. const bcdet = bdx.multiply(cdy).selfSubtract(cdx.multiply(bdy));
  24280. const cadet = cdx.multiply(ady).selfSubtract(adx.multiply(cdy));
  24281. const alift = adx.multiply(adx).selfAdd(ady.multiply(ady));
  24282. const blift = bdx.multiply(bdx).selfAdd(bdy.multiply(bdy));
  24283. const clift = cdx.multiply(cdx).selfAdd(cdy.multiply(cdy));
  24284. const sum = alift.selfMultiply(bcdet).selfAdd(blift.selfMultiply(cadet)).selfAdd(clift.selfMultiply(abdet));
  24285. const isInCircle = sum.doubleValue() > 0;
  24286. return isInCircle;
  24287. }
  24288. static checkRobustInCircle(a, b, c, p) {
  24289. const nonRobustInCircle = TrianglePredicate.isInCircleNonRobust(a, b, c, p);
  24290. const isInCircleDD = TrianglePredicate.isInCircleDDSlow(a, b, c, p);
  24291. const isInCircleCC = TrianglePredicate.isInCircleCC(a, b, c, p);
  24292. const circumCentre = Triangle.circumcentre(a, b, c);
  24293. System.out.println('p radius diff a = ' + Math.abs(p.distance(circumCentre) - a.distance(circumCentre)) / a.distance(circumCentre));
  24294. if (nonRobustInCircle !== isInCircleDD || nonRobustInCircle !== isInCircleCC) {
  24295. System.out.println('inCircle robustness failure (double result = ' + nonRobustInCircle + ', DD result = ' + isInCircleDD + ', CC result = ' + isInCircleCC + ')');
  24296. System.out.println(WKTWriter.toLineString(new CoordinateArraySequence([a, b, c, p])));
  24297. System.out.println('Circumcentre = ' + WKTWriter.toPoint(circumCentre) + ' radius = ' + a.distance(circumCentre));
  24298. System.out.println('p radius diff a = ' + Math.abs(p.distance(circumCentre) / a.distance(circumCentre) - 1));
  24299. System.out.println('p radius diff b = ' + Math.abs(p.distance(circumCentre) / b.distance(circumCentre) - 1));
  24300. System.out.println('p radius diff c = ' + Math.abs(p.distance(circumCentre) / c.distance(circumCentre) - 1));
  24301. System.out.println();
  24302. }
  24303. }
  24304. static isInCircleDDFast(a, b, c, p) {
  24305. const aTerm = DD.sqr(a.x).selfAdd(DD.sqr(a.y)).selfMultiply(TrianglePredicate.triAreaDDFast(b, c, p));
  24306. const bTerm = DD.sqr(b.x).selfAdd(DD.sqr(b.y)).selfMultiply(TrianglePredicate.triAreaDDFast(a, c, p));
  24307. const cTerm = DD.sqr(c.x).selfAdd(DD.sqr(c.y)).selfMultiply(TrianglePredicate.triAreaDDFast(a, b, p));
  24308. const pTerm = DD.sqr(p.x).selfAdd(DD.sqr(p.y)).selfMultiply(TrianglePredicate.triAreaDDFast(a, b, c));
  24309. const sum = aTerm.selfSubtract(bTerm).selfAdd(cTerm).selfSubtract(pTerm);
  24310. const isInCircle = sum.doubleValue() > 0;
  24311. return isInCircle;
  24312. }
  24313. static isInCircleCC(a, b, c, p) {
  24314. const cc = Triangle.circumcentre(a, b, c);
  24315. const ccRadius = a.distance(cc);
  24316. const pRadiusDiff = p.distance(cc) - ccRadius;
  24317. return pRadiusDiff <= 0;
  24318. }
  24319. static isInCircleNormalized(a, b, c, p) {
  24320. const adx = a.x - p.x;
  24321. const ady = a.y - p.y;
  24322. const bdx = b.x - p.x;
  24323. const bdy = b.y - p.y;
  24324. const cdx = c.x - p.x;
  24325. const cdy = c.y - p.y;
  24326. const abdet = adx * bdy - bdx * ady;
  24327. const bcdet = bdx * cdy - cdx * bdy;
  24328. const cadet = cdx * ady - adx * cdy;
  24329. const alift = adx * adx + ady * ady;
  24330. const blift = bdx * bdx + bdy * bdy;
  24331. const clift = cdx * cdx + cdy * cdy;
  24332. const disc = alift * bcdet + blift * cadet + clift * abdet;
  24333. return disc > 0;
  24334. }
  24335. static isInCircleDDSlow(a, b, c, p) {
  24336. const px = DD.valueOf(p.x);
  24337. const py = DD.valueOf(p.y);
  24338. const ax = DD.valueOf(a.x);
  24339. const ay = DD.valueOf(a.y);
  24340. const bx = DD.valueOf(b.x);
  24341. const by = DD.valueOf(b.y);
  24342. const cx = DD.valueOf(c.x);
  24343. const cy = DD.valueOf(c.y);
  24344. const aTerm = ax.multiply(ax).add(ay.multiply(ay)).multiply(TrianglePredicate.triAreaDDSlow(bx, by, cx, cy, px, py));
  24345. const bTerm = bx.multiply(bx).add(by.multiply(by)).multiply(TrianglePredicate.triAreaDDSlow(ax, ay, cx, cy, px, py));
  24346. const cTerm = cx.multiply(cx).add(cy.multiply(cy)).multiply(TrianglePredicate.triAreaDDSlow(ax, ay, bx, by, px, py));
  24347. const pTerm = px.multiply(px).add(py.multiply(py)).multiply(TrianglePredicate.triAreaDDSlow(ax, ay, bx, by, cx, cy));
  24348. const sum = aTerm.subtract(bTerm).add(cTerm).subtract(pTerm);
  24349. const isInCircle = sum.doubleValue() > 0;
  24350. return isInCircle;
  24351. }
  24352. static isInCircleNonRobust(a, b, c, p) {
  24353. const isInCircle = (a.x * a.x + a.y * a.y) * TrianglePredicate.triArea(b, c, p) - (b.x * b.x + b.y * b.y) * TrianglePredicate.triArea(a, c, p) + (c.x * c.x + c.y * c.y) * TrianglePredicate.triArea(a, b, p) - (p.x * p.x + p.y * p.y) * TrianglePredicate.triArea(a, b, c) > 0;
  24354. return isInCircle;
  24355. }
  24356. static isInCircleRobust(a, b, c, p) {
  24357. return TrianglePredicate.isInCircleNormalized(a, b, c, p);
  24358. }
  24359. static triAreaDDSlow(ax, ay, bx, by, cx, cy) {
  24360. return bx.subtract(ax).multiply(cy.subtract(ay)).subtract(by.subtract(ay).multiply(cx.subtract(ax)));
  24361. }
  24362. static triAreaDDFast(a, b, c) {
  24363. const t1 = DD.valueOf(b.x).selfSubtract(a.x).selfMultiply(DD.valueOf(c.y).selfSubtract(a.y));
  24364. const t2 = DD.valueOf(b.y).selfSubtract(a.y).selfMultiply(DD.valueOf(c.x).selfSubtract(a.x));
  24365. return t1.selfSubtract(t2);
  24366. }
  24367. }
  24368. class Vertex {
  24369. constructor() {
  24370. Vertex.constructor_.apply(this, arguments);
  24371. }
  24372. static constructor_() {
  24373. this._p = null;
  24374. if (arguments.length === 1) {
  24375. const _p = arguments[0];
  24376. this._p = new Coordinate(_p);
  24377. } else if (arguments.length === 2) {
  24378. const _x = arguments[0],
  24379. _y = arguments[1];
  24380. this._p = new Coordinate(_x, _y);
  24381. } else if (arguments.length === 3) {
  24382. const _x = arguments[0],
  24383. _y = arguments[1],
  24384. _z = arguments[2];
  24385. this._p = new Coordinate(_x, _y, _z);
  24386. }
  24387. }
  24388. static interpolateZ() {
  24389. if (arguments.length === 3) {
  24390. const p = arguments[0],
  24391. p0 = arguments[1],
  24392. p1 = arguments[2];
  24393. const segLen = p0.distance(p1);
  24394. const ptLen = p.distance(p0);
  24395. const dz = p1.getZ() - p0.getZ();
  24396. const pz = p0.getZ() + dz * (ptLen / segLen);
  24397. return pz;
  24398. } else if (arguments.length === 4) {
  24399. const p = arguments[0],
  24400. v0 = arguments[1],
  24401. v1 = arguments[2],
  24402. v2 = arguments[3];
  24403. const x0 = v0.x;
  24404. const y0 = v0.y;
  24405. const a = v1.x - x0;
  24406. const b = v2.x - x0;
  24407. const c = v1.y - y0;
  24408. const d = v2.y - y0;
  24409. const det = a * d - b * c;
  24410. const dx = p.x - x0;
  24411. const dy = p.y - y0;
  24412. const t = (d * dx - b * dy) / det;
  24413. const u = (-c * dx + a * dy) / det;
  24414. const z = v0.getZ() + t * (v1.getZ() - v0.getZ()) + u * (v2.getZ() - v0.getZ());
  24415. return z;
  24416. }
  24417. }
  24418. circleCenter(b, c) {
  24419. const a = new Vertex(this.getX(), this.getY());
  24420. const cab = this.bisector(a, b);
  24421. const cbc = this.bisector(b, c);
  24422. const hcc = new HCoordinate(cab, cbc);
  24423. let cc = null;
  24424. try {
  24425. cc = new Vertex(hcc.getX(), hcc.getY());
  24426. } catch (nre) {
  24427. if (nre instanceof NotRepresentableException) {
  24428. System.err.println('a: ' + a + ' b: ' + b + ' c: ' + c);
  24429. System.err.println(nre);
  24430. } else {
  24431. throw nre;
  24432. }
  24433. } finally {}
  24434. return cc;
  24435. }
  24436. dot(v) {
  24437. return this._p.x * v.getX() + this._p.y * v.getY();
  24438. }
  24439. magn() {
  24440. return Math.sqrt(this._p.x * this._p.x + this._p.y * this._p.y);
  24441. }
  24442. getZ() {
  24443. return this._p.getZ();
  24444. }
  24445. bisector(a, b) {
  24446. const dx = b.getX() - a.getX();
  24447. const dy = b.getY() - a.getY();
  24448. const l1 = new HCoordinate(a.getX() + dx / 2.0, a.getY() + dy / 2.0, 1.0);
  24449. const l2 = new HCoordinate(a.getX() - dy + dx / 2.0, a.getY() + dx + dy / 2.0, 1.0);
  24450. return new HCoordinate(l1, l2);
  24451. }
  24452. equals() {
  24453. if (arguments.length === 1) {
  24454. const _x = arguments[0];
  24455. if (this._p.x === _x.getX() && this._p.y === _x.getY()) return true;else return false;
  24456. } else if (arguments.length === 2) {
  24457. const _x = arguments[0],
  24458. tolerance = arguments[1];
  24459. if (this._p.distance(_x.getCoordinate()) < tolerance) return true;else return false;
  24460. }
  24461. }
  24462. getCoordinate() {
  24463. return this._p;
  24464. }
  24465. isInCircle(a, b, c) {
  24466. return TrianglePredicate.isInCircleRobust(a._p, b._p, c._p, this._p);
  24467. }
  24468. interpolateZValue(v0, v1, v2) {
  24469. const x0 = v0.getX();
  24470. const y0 = v0.getY();
  24471. const a = v1.getX() - x0;
  24472. const b = v2.getX() - x0;
  24473. const c = v1.getY() - y0;
  24474. const d = v2.getY() - y0;
  24475. const det = a * d - b * c;
  24476. const dx = this.getX() - x0;
  24477. const dy = this.getY() - y0;
  24478. const t = (d * dx - b * dy) / det;
  24479. const u = (-c * dx + a * dy) / det;
  24480. const z = v0.getZ() + t * (v1.getZ() - v0.getZ()) + u * (v2.getZ() - v0.getZ());
  24481. return z;
  24482. }
  24483. midPoint(a) {
  24484. const xm = (this._p.x + a.getX()) / 2.0;
  24485. const ym = (this._p.y + a.getY()) / 2.0;
  24486. const zm = (this._p.getZ() + a.getZ()) / 2.0;
  24487. return new Vertex(xm, ym, zm);
  24488. }
  24489. rightOf(e) {
  24490. return this.isCCW(e.dest(), e.orig());
  24491. }
  24492. isCCW(b, c) {
  24493. return (b._p.x - this._p.x) * (c._p.y - this._p.y) - (b._p.y - this._p.y) * (c._p.x - this._p.x) > 0;
  24494. }
  24495. getX() {
  24496. return this._p.x;
  24497. }
  24498. crossProduct(v) {
  24499. return this._p.x * v.getY() - this._p.y * v.getX();
  24500. }
  24501. setZ(_z) {
  24502. this._p.setZ(_z);
  24503. }
  24504. times(c) {
  24505. return new Vertex(c * this._p.x, c * this._p.y);
  24506. }
  24507. cross() {
  24508. return new Vertex(this._p.y, -this._p.x);
  24509. }
  24510. leftOf(e) {
  24511. return this.isCCW(e.orig(), e.dest());
  24512. }
  24513. toString() {
  24514. return 'POINT (' + this._p.x + ' ' + this._p.y + ')';
  24515. }
  24516. sub(v) {
  24517. return new Vertex(this._p.x - v.getX(), this._p.y - v.getY());
  24518. }
  24519. getY() {
  24520. return this._p.y;
  24521. }
  24522. classify(p0, p1) {
  24523. const p2 = this;
  24524. const a = p1.sub(p0);
  24525. const b = p2.sub(p0);
  24526. const sa = a.crossProduct(b);
  24527. if (sa > 0.0) return Vertex.LEFT;
  24528. if (sa < 0.0) return Vertex.RIGHT;
  24529. if (a.getX() * b.getX() < 0.0 || a.getY() * b.getY() < 0.0) return Vertex.BEHIND;
  24530. if (a.magn() < b.magn()) return Vertex.BEYOND;
  24531. if (p0.equals(p2)) return Vertex.ORIGIN;
  24532. if (p1.equals(p2)) return Vertex.DESTINATION;
  24533. return Vertex.BETWEEN;
  24534. }
  24535. sum(v) {
  24536. return new Vertex(this._p.x + v.getX(), this._p.y + v.getY());
  24537. }
  24538. distance(v1, v2) {
  24539. return Math.sqrt(Math.pow(v2.getX() - v1.getX(), 2.0) + Math.pow(v2.getY() - v1.getY(), 2.0));
  24540. }
  24541. circumRadiusRatio(b, c) {
  24542. const x = this.circleCenter(b, c);
  24543. const radius = this.distance(x, b);
  24544. let edgeLength = this.distance(this, b);
  24545. let el = this.distance(b, c);
  24546. if (el < edgeLength) edgeLength = el;
  24547. el = this.distance(c, this);
  24548. if (el < edgeLength) edgeLength = el;
  24549. return radius / edgeLength;
  24550. }
  24551. }
  24552. Vertex.LEFT = 0;
  24553. Vertex.RIGHT = 1;
  24554. Vertex.BEYOND = 2;
  24555. Vertex.BEHIND = 3;
  24556. Vertex.BETWEEN = 4;
  24557. Vertex.ORIGIN = 5;
  24558. Vertex.DESTINATION = 6;
  24559. class ConstraintVertex extends Vertex {
  24560. constructor() {
  24561. super();
  24562. ConstraintVertex.constructor_.apply(this, arguments);
  24563. }
  24564. static constructor_() {
  24565. this._isOnConstraint = null;
  24566. this._constraint = null;
  24567. const p = arguments[0];
  24568. Vertex.constructor_.call(this, p);
  24569. }
  24570. getConstraint() {
  24571. return this._constraint;
  24572. }
  24573. setOnConstraint(isOnConstraint) {
  24574. this._isOnConstraint = isOnConstraint;
  24575. }
  24576. merge(other) {
  24577. if (other._isOnConstraint) {
  24578. this._isOnConstraint = true;
  24579. this._constraint = other._constraint;
  24580. }
  24581. }
  24582. isOnConstraint() {
  24583. return this._isOnConstraint;
  24584. }
  24585. setConstraint(constraint) {
  24586. this._isOnConstraint = true;
  24587. this._constraint = constraint;
  24588. }
  24589. }
  24590. class QuadEdge {
  24591. constructor() {
  24592. QuadEdge.constructor_.apply(this, arguments);
  24593. }
  24594. static constructor_() {
  24595. this._rot = null;
  24596. this._vertex = null;
  24597. this._next = null;
  24598. this._data = null;
  24599. }
  24600. static makeEdge(o, d) {
  24601. const q0 = new QuadEdge();
  24602. const q1 = new QuadEdge();
  24603. const q2 = new QuadEdge();
  24604. const q3 = new QuadEdge();
  24605. q0._rot = q1;
  24606. q1._rot = q2;
  24607. q2._rot = q3;
  24608. q3._rot = q0;
  24609. q0.setNext(q0);
  24610. q1.setNext(q3);
  24611. q2.setNext(q2);
  24612. q3.setNext(q1);
  24613. const base = q0;
  24614. base.setOrig(o);
  24615. base.setDest(d);
  24616. return base;
  24617. }
  24618. static swap(e) {
  24619. const a = e.oPrev();
  24620. const b = e.sym().oPrev();
  24621. QuadEdge.splice(e, a);
  24622. QuadEdge.splice(e.sym(), b);
  24623. QuadEdge.splice(e, a.lNext());
  24624. QuadEdge.splice(e.sym(), b.lNext());
  24625. e.setOrig(a.dest());
  24626. e.setDest(b.dest());
  24627. }
  24628. static splice(a, b) {
  24629. const alpha = a.oNext().rot();
  24630. const beta = b.oNext().rot();
  24631. const t1 = b.oNext();
  24632. const t2 = a.oNext();
  24633. const t3 = beta.oNext();
  24634. const t4 = alpha.oNext();
  24635. a.setNext(t1);
  24636. b.setNext(t2);
  24637. alpha.setNext(t3);
  24638. beta.setNext(t4);
  24639. }
  24640. static connect(a, b) {
  24641. const e = QuadEdge.makeEdge(a.dest(), b.orig());
  24642. QuadEdge.splice(e, a.lNext());
  24643. QuadEdge.splice(e.sym(), b);
  24644. return e;
  24645. }
  24646. equalsNonOriented(qe) {
  24647. if (this.equalsOriented(qe)) return true;
  24648. if (this.equalsOriented(qe.sym())) return true;
  24649. return false;
  24650. }
  24651. toLineSegment() {
  24652. return new LineSegment(this._vertex.getCoordinate(), this.dest().getCoordinate());
  24653. }
  24654. dest() {
  24655. return this.sym().orig();
  24656. }
  24657. oNext() {
  24658. return this._next;
  24659. }
  24660. equalsOriented(qe) {
  24661. if (this.orig().getCoordinate().equals2D(qe.orig().getCoordinate()) && this.dest().getCoordinate().equals2D(qe.dest().getCoordinate())) return true;
  24662. return false;
  24663. }
  24664. dNext() {
  24665. return this.sym().oNext().sym();
  24666. }
  24667. lPrev() {
  24668. return this._next.sym();
  24669. }
  24670. rPrev() {
  24671. return this.sym().oNext();
  24672. }
  24673. rot() {
  24674. return this._rot;
  24675. }
  24676. oPrev() {
  24677. return this._rot._next._rot;
  24678. }
  24679. sym() {
  24680. return this._rot._rot;
  24681. }
  24682. setOrig(o) {
  24683. this._vertex = o;
  24684. }
  24685. lNext() {
  24686. return this.invRot().oNext().rot();
  24687. }
  24688. getLength() {
  24689. return this.orig().getCoordinate().distance(this.dest().getCoordinate());
  24690. }
  24691. invRot() {
  24692. return this._rot.sym();
  24693. }
  24694. setDest(d) {
  24695. this.sym().setOrig(d);
  24696. }
  24697. setData(data) {
  24698. this._data = data;
  24699. }
  24700. getData() {
  24701. return this._data;
  24702. }
  24703. delete() {
  24704. this._rot = null;
  24705. }
  24706. orig() {
  24707. return this._vertex;
  24708. }
  24709. rNext() {
  24710. return this._rot._next.invRot();
  24711. }
  24712. toString() {
  24713. const p0 = this._vertex.getCoordinate();
  24714. const p1 = this.dest().getCoordinate();
  24715. return WKTWriter.toLineString(p0, p1);
  24716. }
  24717. isLive() {
  24718. return this._rot !== null;
  24719. }
  24720. getPrimary() {
  24721. if (this.orig().getCoordinate().compareTo(this.dest().getCoordinate()) <= 0) return this;else return this.sym();
  24722. }
  24723. dPrev() {
  24724. return this.invRot().oNext().invRot();
  24725. }
  24726. setNext(next) {
  24727. this._next = next;
  24728. }
  24729. }
  24730. class IncrementalDelaunayTriangulator {
  24731. constructor() {
  24732. IncrementalDelaunayTriangulator.constructor_.apply(this, arguments);
  24733. }
  24734. static constructor_() {
  24735. this._subdiv = null;
  24736. this._isUsingTolerance = false;
  24737. const subdiv = arguments[0];
  24738. this._subdiv = subdiv;
  24739. this._isUsingTolerance = subdiv.getTolerance() > 0.0;
  24740. }
  24741. insertSite(v) {
  24742. let e = this._subdiv.locate(v);
  24743. if (this._subdiv.isVertexOfEdge(e, v)) {
  24744. return e;
  24745. } else if (this._subdiv.isOnEdge(e, v.getCoordinate())) {
  24746. e = e.oPrev();
  24747. this._subdiv.delete(e.oNext());
  24748. }
  24749. let base = this._subdiv.makeEdge(e.orig(), v);
  24750. QuadEdge.splice(base, e);
  24751. const startEdge = base;
  24752. do {
  24753. base = this._subdiv.connect(e, base.sym());
  24754. e = base.oPrev();
  24755. } while (e.lNext() !== startEdge);
  24756. do {
  24757. const t = e.oPrev();
  24758. if (t.dest().rightOf(e) && v.isInCircle(e.orig(), t.dest(), e.dest())) {
  24759. QuadEdge.swap(e);
  24760. e = e.oPrev();
  24761. } else if (e.oNext() === startEdge) {
  24762. return base;
  24763. } else {
  24764. e = e.oNext().lPrev();
  24765. }
  24766. } while (true);
  24767. }
  24768. insertSites(vertices) {
  24769. for (let i = vertices.iterator(); i.hasNext();) {
  24770. const v = i.next();
  24771. this.insertSite(v);
  24772. }
  24773. }
  24774. }
  24775. class QuadEdgeLocator {
  24776. locate(v) {}
  24777. }
  24778. class LastFoundQuadEdgeLocator {
  24779. constructor() {
  24780. LastFoundQuadEdgeLocator.constructor_.apply(this, arguments);
  24781. }
  24782. static constructor_() {
  24783. this._subdiv = null;
  24784. this._lastEdge = null;
  24785. const subdiv = arguments[0];
  24786. this._subdiv = subdiv;
  24787. this.init();
  24788. }
  24789. init() {
  24790. this._lastEdge = this.findEdge();
  24791. }
  24792. locate(v) {
  24793. if (!this._lastEdge.isLive()) this.init();
  24794. const e = this._subdiv.locateFromEdge(v, this._lastEdge);
  24795. this._lastEdge = e;
  24796. return e;
  24797. }
  24798. findEdge() {
  24799. const edges = this._subdiv.getEdges();
  24800. return edges.iterator().next();
  24801. }
  24802. get interfaces_() {
  24803. return [QuadEdgeLocator];
  24804. }
  24805. }
  24806. class LocateFailureException extends RuntimeException {
  24807. constructor() {
  24808. super();
  24809. LocateFailureException.constructor_.apply(this, arguments);
  24810. }
  24811. static constructor_() {
  24812. this._seg = null;
  24813. if (arguments.length === 1) {
  24814. if (typeof arguments[0] === 'string') {
  24815. const msg = arguments[0];
  24816. RuntimeException.constructor_.call(this, msg);
  24817. } else if (arguments[0] instanceof LineSegment) {
  24818. const seg = arguments[0];
  24819. RuntimeException.constructor_.call(this, 'Locate failed to converge (at edge: ' + seg + '). Possible causes include invalid Subdivision topology or very close sites');
  24820. this._seg = new LineSegment(seg);
  24821. }
  24822. } else if (arguments.length === 2) {
  24823. const msg = arguments[0],
  24824. seg = arguments[1];
  24825. RuntimeException.constructor_.call(this, LocateFailureException.msgWithSpatial(msg, seg));
  24826. this._seg = new LineSegment(seg);
  24827. }
  24828. }
  24829. static msgWithSpatial(msg, seg) {
  24830. if (seg !== null) return msg + ' [ ' + seg + ' ]';
  24831. return msg;
  24832. }
  24833. getSegment() {
  24834. return this._seg;
  24835. }
  24836. }
  24837. class TriangleVisitor {
  24838. visit(triEdges) {}
  24839. }
  24840. class QuadEdgeSubdivision {
  24841. constructor() {
  24842. QuadEdgeSubdivision.constructor_.apply(this, arguments);
  24843. }
  24844. static constructor_() {
  24845. this._visitedKey = 0;
  24846. this._quadEdges = new ArrayList();
  24847. this._startingEdge = null;
  24848. this._tolerance = null;
  24849. this._edgeCoincidenceTolerance = null;
  24850. this._frameVertex = new Array(3).fill(null);
  24851. this._frameEnv = null;
  24852. this._locator = null;
  24853. this._seg = new LineSegment();
  24854. this._triEdges = new Array(3).fill(null);
  24855. const env = arguments[0],
  24856. tolerance = arguments[1];
  24857. this._tolerance = tolerance;
  24858. this._edgeCoincidenceTolerance = tolerance / QuadEdgeSubdivision.EDGE_COINCIDENCE_TOL_FACTOR;
  24859. this.createFrame(env);
  24860. this._startingEdge = this.initSubdiv();
  24861. this._locator = new LastFoundQuadEdgeLocator(this);
  24862. }
  24863. static getTriangleEdges(startQE, triEdge) {
  24864. triEdge[0] = startQE;
  24865. triEdge[1] = triEdge[0].lNext();
  24866. triEdge[2] = triEdge[1].lNext();
  24867. if (triEdge[2].lNext() !== triEdge[0]) throw new IllegalArgumentException('Edges do not form a triangle');
  24868. }
  24869. getTriangleVertices(includeFrame) {
  24870. const visitor = new TriangleVertexListVisitor();
  24871. this.visitTriangles(visitor, includeFrame);
  24872. return visitor.getTriangleVertices();
  24873. }
  24874. isFrameVertex(v) {
  24875. if (v.equals(this._frameVertex[0])) return true;
  24876. if (v.equals(this._frameVertex[1])) return true;
  24877. if (v.equals(this._frameVertex[2])) return true;
  24878. return false;
  24879. }
  24880. isVertexOfEdge(e, v) {
  24881. if (v.equals(e.orig(), this._tolerance) || v.equals(e.dest(), this._tolerance)) return true;
  24882. return false;
  24883. }
  24884. connect(a, b) {
  24885. const q = QuadEdge.connect(a, b);
  24886. this._quadEdges.add(q);
  24887. return q;
  24888. }
  24889. getVoronoiCellPolygon(qe, geomFact) {
  24890. const cellPts = new ArrayList();
  24891. const startQE = qe;
  24892. do {
  24893. const cc = qe.rot().orig().getCoordinate();
  24894. cellPts.add(cc);
  24895. qe = qe.oPrev();
  24896. } while (qe !== startQE);
  24897. const coordList = new CoordinateList();
  24898. coordList.addAll(cellPts, false);
  24899. coordList.closeRing();
  24900. if (coordList.size() < 4) {
  24901. System.out.println(coordList);
  24902. coordList.add(coordList.get(coordList.size() - 1), true);
  24903. }
  24904. const pts = coordList.toCoordinateArray();
  24905. const cellPoly = geomFact.createPolygon(geomFact.createLinearRing(pts));
  24906. const v = startQE.orig();
  24907. cellPoly.setUserData(v.getCoordinate());
  24908. return cellPoly;
  24909. }
  24910. setLocator(locator) {
  24911. this._locator = locator;
  24912. }
  24913. initSubdiv() {
  24914. const ea = this.makeEdge(this._frameVertex[0], this._frameVertex[1]);
  24915. const eb = this.makeEdge(this._frameVertex[1], this._frameVertex[2]);
  24916. QuadEdge.splice(ea.sym(), eb);
  24917. const ec = this.makeEdge(this._frameVertex[2], this._frameVertex[0]);
  24918. QuadEdge.splice(eb.sym(), ec);
  24919. QuadEdge.splice(ec.sym(), ea);
  24920. return ea;
  24921. }
  24922. isFrameBorderEdge(e) {
  24923. const leftTri = new Array(3).fill(null);
  24924. QuadEdgeSubdivision.getTriangleEdges(e, leftTri);
  24925. const rightTri = new Array(3).fill(null);
  24926. QuadEdgeSubdivision.getTriangleEdges(e.sym(), rightTri);
  24927. const vLeftTriOther = e.lNext().dest();
  24928. if (this.isFrameVertex(vLeftTriOther)) return true;
  24929. const vRightTriOther = e.sym().lNext().dest();
  24930. if (this.isFrameVertex(vRightTriOther)) return true;
  24931. return false;
  24932. }
  24933. makeEdge(o, d) {
  24934. const q = QuadEdge.makeEdge(o, d);
  24935. this._quadEdges.add(q);
  24936. return q;
  24937. }
  24938. visitTriangles(triVisitor, includeFrame) {
  24939. this._visitedKey++;
  24940. const edgeStack = new Stack();
  24941. edgeStack.push(this._startingEdge);
  24942. const visitedEdges = new HashSet();
  24943. while (!edgeStack.empty()) {
  24944. const edge = edgeStack.pop();
  24945. if (!visitedEdges.contains(edge)) {
  24946. const triEdges = this.fetchTriangleToVisit(edge, edgeStack, includeFrame, visitedEdges);
  24947. if (triEdges !== null) triVisitor.visit(triEdges);
  24948. }
  24949. }
  24950. }
  24951. isFrameEdge(e) {
  24952. if (this.isFrameVertex(e.orig()) || this.isFrameVertex(e.dest())) return true;
  24953. return false;
  24954. }
  24955. isOnEdge(e, p) {
  24956. this._seg.setCoordinates(e.orig().getCoordinate(), e.dest().getCoordinate());
  24957. const dist = this._seg.distance(p);
  24958. return dist < this._edgeCoincidenceTolerance;
  24959. }
  24960. getEnvelope() {
  24961. return new Envelope(this._frameEnv);
  24962. }
  24963. createFrame(env) {
  24964. const deltaX = env.getWidth();
  24965. const deltaY = env.getHeight();
  24966. let offset = 0.0;
  24967. if (deltaX > deltaY) offset = deltaX * 10.0;else offset = deltaY * 10.0;
  24968. this._frameVertex[0] = new Vertex((env.getMaxX() + env.getMinX()) / 2.0, env.getMaxY() + offset);
  24969. this._frameVertex[1] = new Vertex(env.getMinX() - offset, env.getMinY() - offset);
  24970. this._frameVertex[2] = new Vertex(env.getMaxX() + offset, env.getMinY() - offset);
  24971. this._frameEnv = new Envelope(this._frameVertex[0].getCoordinate(), this._frameVertex[1].getCoordinate());
  24972. this._frameEnv.expandToInclude(this._frameVertex[2].getCoordinate());
  24973. }
  24974. getTriangleCoordinates(includeFrame) {
  24975. const visitor = new TriangleCoordinatesVisitor();
  24976. this.visitTriangles(visitor, includeFrame);
  24977. return visitor.getTriangles();
  24978. }
  24979. getVertices(includeFrame) {
  24980. const vertices = new HashSet();
  24981. for (let i = this._quadEdges.iterator(); i.hasNext();) {
  24982. const qe = i.next();
  24983. const v = qe.orig();
  24984. if (includeFrame || !this.isFrameVertex(v)) vertices.add(v);
  24985. const vd = qe.dest();
  24986. if (includeFrame || !this.isFrameVertex(vd)) vertices.add(vd);
  24987. }
  24988. return vertices;
  24989. }
  24990. fetchTriangleToVisit(edge, edgeStack, includeFrame, visitedEdges) {
  24991. let curr = edge;
  24992. let edgeCount = 0;
  24993. let isFrame = false;
  24994. do {
  24995. this._triEdges[edgeCount] = curr;
  24996. if (this.isFrameEdge(curr)) isFrame = true;
  24997. const sym = curr.sym();
  24998. if (!visitedEdges.contains(sym)) edgeStack.push(sym);
  24999. visitedEdges.add(curr);
  25000. edgeCount++;
  25001. curr = curr.lNext();
  25002. } while (curr !== edge);
  25003. if (isFrame && !includeFrame) return null;
  25004. return this._triEdges;
  25005. }
  25006. getEdges() {
  25007. if (arguments.length === 0) {
  25008. return this._quadEdges;
  25009. } else if (arguments.length === 1) {
  25010. const geomFact = arguments[0];
  25011. const quadEdges = this.getPrimaryEdges(false);
  25012. const edges = new Array(quadEdges.size()).fill(null);
  25013. let i = 0;
  25014. for (let it = quadEdges.iterator(); it.hasNext();) {
  25015. const qe = it.next();
  25016. edges[i++] = geomFact.createLineString([qe.orig().getCoordinate(), qe.dest().getCoordinate()]);
  25017. }
  25018. return geomFact.createMultiLineString(edges);
  25019. }
  25020. }
  25021. getVertexUniqueEdges(includeFrame) {
  25022. const edges = new ArrayList();
  25023. const visitedVertices = new HashSet();
  25024. for (let i = this._quadEdges.iterator(); i.hasNext();) {
  25025. const qe = i.next();
  25026. const v = qe.orig();
  25027. if (!visitedVertices.contains(v)) {
  25028. visitedVertices.add(v);
  25029. if (includeFrame || !this.isFrameVertex(v)) edges.add(qe);
  25030. }
  25031. const qd = qe.sym();
  25032. const vd = qd.orig();
  25033. if (!visitedVertices.contains(vd)) {
  25034. visitedVertices.add(vd);
  25035. if (includeFrame || !this.isFrameVertex(vd)) edges.add(qd);
  25036. }
  25037. }
  25038. return edges;
  25039. }
  25040. getTriangleEdges(includeFrame) {
  25041. const visitor = new TriangleEdgesListVisitor();
  25042. this.visitTriangles(visitor, includeFrame);
  25043. return visitor.getTriangleEdges();
  25044. }
  25045. getPrimaryEdges(includeFrame) {
  25046. this._visitedKey++;
  25047. const edges = new ArrayList();
  25048. const edgeStack = new Stack();
  25049. edgeStack.push(this._startingEdge);
  25050. const visitedEdges = new HashSet();
  25051. while (!edgeStack.empty()) {
  25052. const edge = edgeStack.pop();
  25053. if (!visitedEdges.contains(edge)) {
  25054. const priQE = edge.getPrimary();
  25055. if (includeFrame || !this.isFrameEdge(priQE)) edges.add(priQE);
  25056. edgeStack.push(edge.oNext());
  25057. edgeStack.push(edge.sym().oNext());
  25058. visitedEdges.add(edge);
  25059. visitedEdges.add(edge.sym());
  25060. }
  25061. }
  25062. return edges;
  25063. }
  25064. delete(e) {
  25065. QuadEdge.splice(e, e.oPrev());
  25066. QuadEdge.splice(e.sym(), e.sym().oPrev());
  25067. const eSym = e.sym();
  25068. const eRot = e.rot();
  25069. const eRotSym = e.rot().sym();
  25070. this._quadEdges.remove(e);
  25071. this._quadEdges.remove(eSym);
  25072. this._quadEdges.remove(eRot);
  25073. this._quadEdges.remove(eRotSym);
  25074. e.delete();
  25075. eSym.delete();
  25076. eRot.delete();
  25077. eRotSym.delete();
  25078. }
  25079. locateFromEdge(v, startEdge) {
  25080. let iter = 0;
  25081. const maxIter = this._quadEdges.size();
  25082. let e = startEdge;
  25083. while (true) {
  25084. iter++;
  25085. if (iter > maxIter) throw new LocateFailureException(e.toLineSegment());
  25086. if (v.equals(e.orig()) || v.equals(e.dest())) break;else if (v.rightOf(e)) e = e.sym();else if (!v.rightOf(e.oNext())) e = e.oNext();else if (!v.rightOf(e.dPrev())) e = e.dPrev();else break;
  25087. }
  25088. return e;
  25089. }
  25090. getTolerance() {
  25091. return this._tolerance;
  25092. }
  25093. getVoronoiCellPolygons(geomFact) {
  25094. this.visitTriangles(new TriangleCircumcentreVisitor(), true);
  25095. const cells = new ArrayList();
  25096. const edges = this.getVertexUniqueEdges(false);
  25097. for (let i = edges.iterator(); i.hasNext();) {
  25098. const qe = i.next();
  25099. cells.add(this.getVoronoiCellPolygon(qe, geomFact));
  25100. }
  25101. return cells;
  25102. }
  25103. getVoronoiDiagram(geomFact) {
  25104. const vorCells = this.getVoronoiCellPolygons(geomFact);
  25105. return geomFact.createGeometryCollection(GeometryFactory.toGeometryArray(vorCells));
  25106. }
  25107. getTriangles(geomFact) {
  25108. const triPtsList = this.getTriangleCoordinates(false);
  25109. const tris = new Array(triPtsList.size()).fill(null);
  25110. let i = 0;
  25111. for (let it = triPtsList.iterator(); it.hasNext();) {
  25112. const triPt = it.next();
  25113. tris[i++] = geomFact.createPolygon(geomFact.createLinearRing(triPt));
  25114. }
  25115. return geomFact.createGeometryCollection(tris);
  25116. }
  25117. insertSite(v) {
  25118. let e = this.locate(v);
  25119. if (v.equals(e.orig(), this._tolerance) || v.equals(e.dest(), this._tolerance)) return e;
  25120. let base = this.makeEdge(e.orig(), v);
  25121. QuadEdge.splice(base, e);
  25122. const startEdge = base;
  25123. do {
  25124. base = this.connect(e, base.sym());
  25125. e = base.oPrev();
  25126. } while (e.lNext() !== startEdge);
  25127. return startEdge;
  25128. }
  25129. locate() {
  25130. if (arguments.length === 1) {
  25131. if (arguments[0] instanceof Vertex) {
  25132. const v = arguments[0];
  25133. return this._locator.locate(v);
  25134. } else if (arguments[0] instanceof Coordinate) {
  25135. const p = arguments[0];
  25136. return this._locator.locate(new Vertex(p));
  25137. }
  25138. } else if (arguments.length === 2) {
  25139. const p0 = arguments[0],
  25140. p1 = arguments[1];
  25141. const e = this._locator.locate(new Vertex(p0));
  25142. if (e === null) return null;
  25143. let base = e;
  25144. if (e.dest().getCoordinate().equals2D(p0)) base = e.sym();
  25145. let locEdge = base;
  25146. do {
  25147. if (locEdge.dest().getCoordinate().equals2D(p1)) return locEdge;
  25148. locEdge = locEdge.oNext();
  25149. } while (locEdge !== base);
  25150. return null;
  25151. }
  25152. }
  25153. }
  25154. class TriangleCircumcentreVisitor {
  25155. visit(triEdges) {
  25156. const a = triEdges[0].orig().getCoordinate();
  25157. const b = triEdges[1].orig().getCoordinate();
  25158. const c = triEdges[2].orig().getCoordinate();
  25159. const cc = Triangle.circumcentreDD(a, b, c);
  25160. const ccVertex = new Vertex(cc);
  25161. for (let i = 0; i < 3; i++) triEdges[i].rot().setOrig(ccVertex);
  25162. }
  25163. get interfaces_() {
  25164. return [TriangleVisitor];
  25165. }
  25166. }
  25167. class TriangleEdgesListVisitor {
  25168. constructor() {
  25169. TriangleEdgesListVisitor.constructor_.apply(this, arguments);
  25170. }
  25171. static constructor_() {
  25172. this._triList = new ArrayList();
  25173. }
  25174. getTriangleEdges() {
  25175. return this._triList;
  25176. }
  25177. visit(triEdges) {
  25178. this._triList.add(triEdges);
  25179. }
  25180. get interfaces_() {
  25181. return [TriangleVisitor];
  25182. }
  25183. }
  25184. class TriangleVertexListVisitor {
  25185. constructor() {
  25186. TriangleVertexListVisitor.constructor_.apply(this, arguments);
  25187. }
  25188. static constructor_() {
  25189. this._triList = new ArrayList();
  25190. }
  25191. visit(triEdges) {
  25192. this._triList.add([triEdges[0].orig(), triEdges[1].orig(), triEdges[2].orig()]);
  25193. }
  25194. getTriangleVertices() {
  25195. return this._triList;
  25196. }
  25197. get interfaces_() {
  25198. return [TriangleVisitor];
  25199. }
  25200. }
  25201. class TriangleCoordinatesVisitor {
  25202. constructor() {
  25203. TriangleCoordinatesVisitor.constructor_.apply(this, arguments);
  25204. }
  25205. static constructor_() {
  25206. this._coordList = new CoordinateList();
  25207. this._triCoords = new ArrayList();
  25208. }
  25209. checkTriangleSize(pts) {
  25210. if (pts.length >= 2) WKTWriter.toLineString(pts[0], pts[1]);else if (pts.length >= 1) WKTWriter.toPoint(pts[0]);
  25211. }
  25212. visit(triEdges) {
  25213. this._coordList.clear();
  25214. for (let i = 0; i < 3; i++) {
  25215. const v = triEdges[i].orig();
  25216. this._coordList.add(v.getCoordinate());
  25217. }
  25218. if (this._coordList.size() > 0) {
  25219. this._coordList.closeRing();
  25220. const pts = this._coordList.toCoordinateArray();
  25221. if (pts.length !== 4) return null;
  25222. this._triCoords.add(pts);
  25223. }
  25224. }
  25225. getTriangles() {
  25226. return this._triCoords;
  25227. }
  25228. get interfaces_() {
  25229. return [TriangleVisitor];
  25230. }
  25231. }
  25232. QuadEdgeSubdivision.TriangleCircumcentreVisitor = TriangleCircumcentreVisitor;
  25233. QuadEdgeSubdivision.TriangleEdgesListVisitor = TriangleEdgesListVisitor;
  25234. QuadEdgeSubdivision.TriangleVertexListVisitor = TriangleVertexListVisitor;
  25235. QuadEdgeSubdivision.TriangleCoordinatesVisitor = TriangleCoordinatesVisitor;
  25236. QuadEdgeSubdivision.EDGE_COINCIDENCE_TOL_FACTOR = 1000;
  25237. class Segment {
  25238. constructor() {
  25239. Segment.constructor_.apply(this, arguments);
  25240. }
  25241. static constructor_() {
  25242. this._ls = null;
  25243. this._data = null;
  25244. if (arguments.length === 2) {
  25245. const p0 = arguments[0],
  25246. p1 = arguments[1];
  25247. this._ls = new LineSegment(p0, p1);
  25248. } else if (arguments.length === 3) {
  25249. const p0 = arguments[0],
  25250. p1 = arguments[1],
  25251. data = arguments[2];
  25252. this._ls = new LineSegment(p0, p1);
  25253. this._data = data;
  25254. } else if (arguments.length === 6) {
  25255. const x1 = arguments[0],
  25256. y1 = arguments[1],
  25257. z1 = arguments[2],
  25258. x2 = arguments[3],
  25259. y2 = arguments[4],
  25260. z2 = arguments[5];
  25261. Segment.constructor_.call(this, new Coordinate(x1, y1, z1), new Coordinate(x2, y2, z2));
  25262. } else if (arguments.length === 7) {
  25263. const x1 = arguments[0],
  25264. y1 = arguments[1],
  25265. z1 = arguments[2],
  25266. x2 = arguments[3],
  25267. y2 = arguments[4],
  25268. z2 = arguments[5],
  25269. data = arguments[6];
  25270. Segment.constructor_.call(this, new Coordinate(x1, y1, z1), new Coordinate(x2, y2, z2), data);
  25271. }
  25272. }
  25273. getLineSegment() {
  25274. return this._ls;
  25275. }
  25276. getEndZ() {
  25277. const p = this._ls.getCoordinate(1);
  25278. return p.getZ();
  25279. }
  25280. getStartZ() {
  25281. const p = this._ls.getCoordinate(0);
  25282. return p.getZ();
  25283. }
  25284. intersection(s) {
  25285. return this._ls.intersection(s.getLineSegment());
  25286. }
  25287. getStart() {
  25288. return this._ls.getCoordinate(0);
  25289. }
  25290. getEnd() {
  25291. return this._ls.getCoordinate(1);
  25292. }
  25293. getEndY() {
  25294. const p = this._ls.getCoordinate(1);
  25295. return p.y;
  25296. }
  25297. getStartX() {
  25298. const p = this._ls.getCoordinate(0);
  25299. return p.x;
  25300. }
  25301. equalsTopo(s) {
  25302. return this._ls.equalsTopo(s.getLineSegment());
  25303. }
  25304. getStartY() {
  25305. const p = this._ls.getCoordinate(0);
  25306. return p.y;
  25307. }
  25308. setData(data) {
  25309. this._data = data;
  25310. }
  25311. getData() {
  25312. return this._data;
  25313. }
  25314. getEndX() {
  25315. const p = this._ls.getCoordinate(1);
  25316. return p.x;
  25317. }
  25318. toString() {
  25319. return this._ls.toString();
  25320. }
  25321. }
  25322. class ConstraintEnforcementException extends RuntimeException {
  25323. constructor() {
  25324. super();
  25325. ConstraintEnforcementException.constructor_.apply(this, arguments);
  25326. }
  25327. static constructor_() {
  25328. this._pt = null;
  25329. if (arguments.length === 1) {
  25330. const msg = arguments[0];
  25331. RuntimeException.constructor_.call(this, msg);
  25332. } else if (arguments.length === 2) {
  25333. const msg = arguments[0],
  25334. pt = arguments[1];
  25335. RuntimeException.constructor_.call(this, ConstraintEnforcementException.msgWithCoord(msg, pt));
  25336. this._pt = new Coordinate(pt);
  25337. }
  25338. }
  25339. static msgWithCoord(msg, pt) {
  25340. if (pt !== null) return msg + ' [ ' + WKTWriter.toPoint(pt) + ' ]';
  25341. return msg;
  25342. }
  25343. getCoordinate() {
  25344. return this._pt;
  25345. }
  25346. }
  25347. class ConformingDelaunayTriangulator {
  25348. constructor() {
  25349. ConformingDelaunayTriangulator.constructor_.apply(this, arguments);
  25350. }
  25351. static constructor_() {
  25352. this._initialVertices = null;
  25353. this._segVertices = null;
  25354. this._segments = new ArrayList();
  25355. this._subdiv = null;
  25356. this._incDel = null;
  25357. this._convexHull = null;
  25358. this._splitFinder = new NonEncroachingSplitPointFinder();
  25359. this._kdt = null;
  25360. this._vertexFactory = null;
  25361. this._computeAreaEnv = null;
  25362. this._splitPt = null;
  25363. this._tolerance = null;
  25364. const initialVertices = arguments[0],
  25365. tolerance = arguments[1];
  25366. this._initialVertices = new ArrayList(initialVertices);
  25367. this._tolerance = tolerance;
  25368. this._kdt = new KdTree(tolerance);
  25369. }
  25370. static computeVertexEnvelope(vertices) {
  25371. const env = new Envelope();
  25372. for (let i = vertices.iterator(); i.hasNext();) {
  25373. const v = i.next();
  25374. env.expandToInclude(v.getCoordinate());
  25375. }
  25376. return env;
  25377. }
  25378. getInitialVertices() {
  25379. return this._initialVertices;
  25380. }
  25381. getKDT() {
  25382. return this._kdt;
  25383. }
  25384. enforceConstraints() {
  25385. this.addConstraintVertices();
  25386. let count = 0;
  25387. let splits = 0;
  25388. do {
  25389. splits = this.enforceGabriel(this._segments);
  25390. count++;
  25391. } while (splits > 0 && count < ConformingDelaunayTriangulator.MAX_SPLIT_ITER);
  25392. if (count === ConformingDelaunayTriangulator.MAX_SPLIT_ITER) throw new ConstraintEnforcementException('Too many splitting iterations while enforcing constraints. Last split point was at: ', this._splitPt);
  25393. }
  25394. insertSites(vertices) {
  25395. for (let i = vertices.iterator(); i.hasNext();) {
  25396. const v = i.next();
  25397. this.insertSite(v);
  25398. }
  25399. }
  25400. getVertexFactory() {
  25401. return this._vertexFactory;
  25402. }
  25403. getPointArray() {
  25404. const pts = new Array(this._initialVertices.size() + this._segVertices.size()).fill(null);
  25405. let index = 0;
  25406. for (let i = this._initialVertices.iterator(); i.hasNext();) {
  25407. const v = i.next();
  25408. pts[index++] = v.getCoordinate();
  25409. }
  25410. for (let i2 = this._segVertices.iterator(); i2.hasNext();) {
  25411. const v = i2.next();
  25412. pts[index++] = v.getCoordinate();
  25413. }
  25414. return pts;
  25415. }
  25416. setConstraints(segments, segVertices) {
  25417. this._segments = segments;
  25418. this._segVertices = segVertices;
  25419. }
  25420. computeConvexHull() {
  25421. const fact = new GeometryFactory();
  25422. const coords = this.getPointArray();
  25423. const hull = new ConvexHull(coords, fact);
  25424. this._convexHull = hull.getConvexHull();
  25425. }
  25426. addConstraintVertices() {
  25427. this.computeConvexHull();
  25428. this.insertSites(this._segVertices);
  25429. }
  25430. findNonGabrielPoint(seg) {
  25431. const p = seg.getStart();
  25432. const q = seg.getEnd();
  25433. const midPt = new Coordinate((p.x + q.x) / 2.0, (p.y + q.y) / 2.0);
  25434. const segRadius = p.distance(midPt);
  25435. const env = new Envelope(midPt);
  25436. env.expandBy(segRadius);
  25437. const result = this._kdt.query(env);
  25438. let closestNonGabriel = null;
  25439. let minDist = Double.MAX_VALUE;
  25440. for (let i = result.iterator(); i.hasNext();) {
  25441. const nextNode = i.next();
  25442. const testPt = nextNode.getCoordinate();
  25443. if (testPt.equals2D(p) || testPt.equals2D(q)) continue;
  25444. const testRadius = midPt.distance(testPt);
  25445. if (testRadius < segRadius) {
  25446. const testDist = testRadius;
  25447. if (closestNonGabriel === null || testDist < minDist) {
  25448. closestNonGabriel = testPt;
  25449. minDist = testDist;
  25450. }
  25451. }
  25452. }
  25453. return closestNonGabriel;
  25454. }
  25455. getConstraintSegments() {
  25456. return this._segments;
  25457. }
  25458. setSplitPointFinder(splitFinder) {
  25459. this._splitFinder = splitFinder;
  25460. }
  25461. getConvexHull() {
  25462. return this._convexHull;
  25463. }
  25464. getTolerance() {
  25465. return this._tolerance;
  25466. }
  25467. enforceGabriel(segsToInsert) {
  25468. const newSegments = new ArrayList();
  25469. let splits = 0;
  25470. const segsToRemove = new ArrayList();
  25471. for (let i = segsToInsert.iterator(); i.hasNext();) {
  25472. const seg = i.next();
  25473. const encroachPt = this.findNonGabrielPoint(seg);
  25474. if (encroachPt === null) continue;
  25475. this._splitPt = this._splitFinder.findSplitPoint(seg, encroachPt);
  25476. const splitVertex = this.createVertex(this._splitPt, seg);
  25477. const insertedVertex = this.insertSite(splitVertex);
  25478. if (!insertedVertex.getCoordinate().equals2D(this._splitPt)) ;
  25479. const s1 = new Segment(seg.getStartX(), seg.getStartY(), seg.getStartZ(), splitVertex.getX(), splitVertex.getY(), splitVertex.getZ(), seg.getData());
  25480. const s2 = new Segment(splitVertex.getX(), splitVertex.getY(), splitVertex.getZ(), seg.getEndX(), seg.getEndY(), seg.getEndZ(), seg.getData());
  25481. newSegments.add(s1);
  25482. newSegments.add(s2);
  25483. segsToRemove.add(seg);
  25484. splits = splits + 1;
  25485. }
  25486. segsToInsert.removeAll(segsToRemove);
  25487. segsToInsert.addAll(newSegments);
  25488. return splits;
  25489. }
  25490. createVertex() {
  25491. if (arguments.length === 1) {
  25492. const p = arguments[0];
  25493. let v = null;
  25494. if (this._vertexFactory !== null) v = this._vertexFactory.createVertex(p, null);else v = new ConstraintVertex(p);
  25495. return v;
  25496. } else if (arguments.length === 2) {
  25497. const p = arguments[0],
  25498. seg = arguments[1];
  25499. let v = null;
  25500. if (this._vertexFactory !== null) v = this._vertexFactory.createVertex(p, seg);else v = new ConstraintVertex(p);
  25501. v.setOnConstraint(true);
  25502. return v;
  25503. }
  25504. }
  25505. getSubdivision() {
  25506. return this._subdiv;
  25507. }
  25508. computeBoundingBox() {
  25509. const vertexEnv = ConformingDelaunayTriangulator.computeVertexEnvelope(this._initialVertices);
  25510. const segEnv = ConformingDelaunayTriangulator.computeVertexEnvelope(this._segVertices);
  25511. const allPointsEnv = new Envelope(vertexEnv);
  25512. allPointsEnv.expandToInclude(segEnv);
  25513. const deltaX = allPointsEnv.getWidth() * 0.2;
  25514. const deltaY = allPointsEnv.getHeight() * 0.2;
  25515. const delta = Math.max(deltaX, deltaY);
  25516. this._computeAreaEnv = new Envelope(allPointsEnv);
  25517. this._computeAreaEnv.expandBy(delta);
  25518. }
  25519. setVertexFactory(vertexFactory) {
  25520. this._vertexFactory = vertexFactory;
  25521. }
  25522. formInitialDelaunay() {
  25523. this.computeBoundingBox();
  25524. this._subdiv = new QuadEdgeSubdivision(this._computeAreaEnv, this._tolerance);
  25525. this._subdiv.setLocator(new LastFoundQuadEdgeLocator(this._subdiv));
  25526. this._incDel = new IncrementalDelaunayTriangulator(this._subdiv);
  25527. this.insertSites(this._initialVertices);
  25528. }
  25529. insertSite() {
  25530. if (arguments[0] instanceof ConstraintVertex) {
  25531. const v = arguments[0];
  25532. const kdnode = this._kdt.insert(v.getCoordinate(), v);
  25533. if (!kdnode.isRepeated()) {
  25534. this._incDel.insertSite(v);
  25535. } else {
  25536. const snappedV = kdnode.getData();
  25537. snappedV.merge(v);
  25538. return snappedV;
  25539. }
  25540. return v;
  25541. } else if (arguments[0] instanceof Coordinate) {
  25542. const p = arguments[0];
  25543. this.insertSite(this.createVertex(p));
  25544. }
  25545. }
  25546. }
  25547. ConformingDelaunayTriangulator.MAX_SPLIT_ITER = 99;
  25548. class DelaunayTriangulationBuilder {
  25549. constructor() {
  25550. DelaunayTriangulationBuilder.constructor_.apply(this, arguments);
  25551. }
  25552. static constructor_() {
  25553. this._siteCoords = null;
  25554. this._tolerance = 0.0;
  25555. this._subdiv = null;
  25556. }
  25557. static extractUniqueCoordinates(geom) {
  25558. if (geom === null) return new CoordinateList();
  25559. const coords = geom.getCoordinates();
  25560. return DelaunayTriangulationBuilder.unique(coords);
  25561. }
  25562. static envelope(coords) {
  25563. const env = new Envelope();
  25564. for (let i = coords.iterator(); i.hasNext();) {
  25565. const coord = i.next();
  25566. env.expandToInclude(coord);
  25567. }
  25568. return env;
  25569. }
  25570. static unique(coords) {
  25571. const coordsCopy = CoordinateArrays.copyDeep(coords);
  25572. Arrays.sort(coordsCopy);
  25573. const coordList = new CoordinateList(coordsCopy, false);
  25574. return coordList;
  25575. }
  25576. static toVertices(coords) {
  25577. const verts = new ArrayList();
  25578. for (let i = coords.iterator(); i.hasNext();) {
  25579. const coord = i.next();
  25580. verts.add(new Vertex(coord));
  25581. }
  25582. return verts;
  25583. }
  25584. create() {
  25585. if (this._subdiv !== null) return null;
  25586. const siteEnv = DelaunayTriangulationBuilder.envelope(this._siteCoords);
  25587. const vertices = DelaunayTriangulationBuilder.toVertices(this._siteCoords);
  25588. this._subdiv = new QuadEdgeSubdivision(siteEnv, this._tolerance);
  25589. const triangulator = new IncrementalDelaunayTriangulator(this._subdiv);
  25590. triangulator.insertSites(vertices);
  25591. }
  25592. setTolerance(tolerance) {
  25593. this._tolerance = tolerance;
  25594. }
  25595. setSites() {
  25596. if (arguments[0] instanceof Geometry) {
  25597. const geom = arguments[0];
  25598. this._siteCoords = DelaunayTriangulationBuilder.extractUniqueCoordinates(geom);
  25599. } else if (hasInterface(arguments[0], Collection)) {
  25600. const coords = arguments[0];
  25601. this._siteCoords = DelaunayTriangulationBuilder.unique(CoordinateArrays.toCoordinateArray(coords));
  25602. }
  25603. }
  25604. getEdges(geomFact) {
  25605. this.create();
  25606. return this._subdiv.getEdges(geomFact);
  25607. }
  25608. getSubdivision() {
  25609. this.create();
  25610. return this._subdiv;
  25611. }
  25612. getTriangles(geomFact) {
  25613. this.create();
  25614. return this._subdiv.getTriangles(geomFact);
  25615. }
  25616. }
  25617. class ConformingDelaunayTriangulationBuilder {
  25618. constructor() {
  25619. ConformingDelaunayTriangulationBuilder.constructor_.apply(this, arguments);
  25620. }
  25621. static constructor_() {
  25622. this._siteCoords = null;
  25623. this._constraintLines = null;
  25624. this._tolerance = 0.0;
  25625. this._subdiv = null;
  25626. this._constraintVertexMap = new TreeMap();
  25627. }
  25628. static createConstraintSegments() {
  25629. if (arguments.length === 1) {
  25630. const geom = arguments[0];
  25631. const lines = LinearComponentExtracter.getLines(geom);
  25632. const constraintSegs = new ArrayList();
  25633. for (let i = lines.iterator(); i.hasNext();) {
  25634. const line = i.next();
  25635. ConformingDelaunayTriangulationBuilder.createConstraintSegments(line, constraintSegs);
  25636. }
  25637. return constraintSegs;
  25638. } else if (arguments.length === 2) {
  25639. const line = arguments[0],
  25640. constraintSegs = arguments[1];
  25641. const coords = line.getCoordinates();
  25642. for (let i = 1; i < coords.length; i++) constraintSegs.add(new Segment(coords[i - 1], coords[i]));
  25643. }
  25644. }
  25645. createSiteVertices(coords) {
  25646. const verts = new ArrayList();
  25647. for (let i = coords.iterator(); i.hasNext();) {
  25648. const coord = i.next();
  25649. if (this._constraintVertexMap.containsKey(coord)) continue;
  25650. verts.add(new ConstraintVertex(coord));
  25651. }
  25652. return verts;
  25653. }
  25654. create() {
  25655. if (this._subdiv !== null) return null;
  25656. const siteEnv = DelaunayTriangulationBuilder.envelope(this._siteCoords);
  25657. let segments = new ArrayList();
  25658. if (this._constraintLines !== null) {
  25659. siteEnv.expandToInclude(this._constraintLines.getEnvelopeInternal());
  25660. this.createVertices(this._constraintLines);
  25661. segments = ConformingDelaunayTriangulationBuilder.createConstraintSegments(this._constraintLines);
  25662. }
  25663. const sites = this.createSiteVertices(this._siteCoords);
  25664. const cdt = new ConformingDelaunayTriangulator(sites, this._tolerance);
  25665. cdt.setConstraints(segments, new ArrayList(this._constraintVertexMap.values()));
  25666. cdt.formInitialDelaunay();
  25667. cdt.enforceConstraints();
  25668. this._subdiv = cdt.getSubdivision();
  25669. }
  25670. setTolerance(tolerance) {
  25671. this._tolerance = tolerance;
  25672. }
  25673. setConstraints(constraintLines) {
  25674. this._constraintLines = constraintLines;
  25675. }
  25676. setSites(geom) {
  25677. this._siteCoords = DelaunayTriangulationBuilder.extractUniqueCoordinates(geom);
  25678. }
  25679. getEdges(geomFact) {
  25680. this.create();
  25681. return this._subdiv.getEdges(geomFact);
  25682. }
  25683. getSubdivision() {
  25684. this.create();
  25685. return this._subdiv;
  25686. }
  25687. getTriangles(geomFact) {
  25688. this.create();
  25689. return this._subdiv.getTriangles(geomFact);
  25690. }
  25691. createVertices(geom) {
  25692. const coords = geom.getCoordinates();
  25693. for (let i = 0; i < coords.length; i++) {
  25694. const v = new ConstraintVertex(coords[i]);
  25695. this._constraintVertexMap.put(coords[i], v);
  25696. }
  25697. }
  25698. }
  25699. class VoronoiDiagramBuilder {
  25700. constructor() {
  25701. VoronoiDiagramBuilder.constructor_.apply(this, arguments);
  25702. }
  25703. static constructor_() {
  25704. this._siteCoords = null;
  25705. this._tolerance = 0.0;
  25706. this._subdiv = null;
  25707. this._clipEnv = null;
  25708. this._diagramEnv = null;
  25709. }
  25710. static clipGeometryCollection(geom, clipEnv) {
  25711. const clipPoly = geom.getFactory().toGeometry(clipEnv);
  25712. const clipped = new ArrayList();
  25713. for (let i = 0; i < geom.getNumGeometries(); i++) {
  25714. const g = geom.getGeometryN(i);
  25715. let result = null;
  25716. if (clipEnv.contains(g.getEnvelopeInternal())) {
  25717. result = g;
  25718. } else if (clipEnv.intersects(g.getEnvelopeInternal())) {
  25719. result = OverlayOp.intersection(clipPoly, g);
  25720. result.setUserData(g.getUserData());
  25721. }
  25722. if (result !== null && !result.isEmpty()) clipped.add(result);
  25723. }
  25724. return geom.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(clipped));
  25725. }
  25726. create() {
  25727. if (this._subdiv !== null) return null;
  25728. const siteEnv = DelaunayTriangulationBuilder.envelope(this._siteCoords);
  25729. this._diagramEnv = this._clipEnv;
  25730. if (this._diagramEnv === null) {
  25731. this._diagramEnv = siteEnv;
  25732. const expandBy = this._diagramEnv.getDiameter();
  25733. this._diagramEnv.expandBy(expandBy);
  25734. }
  25735. const vertices = DelaunayTriangulationBuilder.toVertices(this._siteCoords);
  25736. this._subdiv = new QuadEdgeSubdivision(siteEnv, this._tolerance);
  25737. const triangulator = new IncrementalDelaunayTriangulator(this._subdiv);
  25738. triangulator.insertSites(vertices);
  25739. }
  25740. getDiagram(geomFact) {
  25741. this.create();
  25742. const polys = this._subdiv.getVoronoiDiagram(geomFact);
  25743. return VoronoiDiagramBuilder.clipGeometryCollection(polys, this._diagramEnv);
  25744. }
  25745. setTolerance(tolerance) {
  25746. this._tolerance = tolerance;
  25747. }
  25748. setSites() {
  25749. if (arguments[0] instanceof Geometry) {
  25750. const geom = arguments[0];
  25751. this._siteCoords = DelaunayTriangulationBuilder.extractUniqueCoordinates(geom);
  25752. } else if (hasInterface(arguments[0], Collection)) {
  25753. const coords = arguments[0];
  25754. this._siteCoords = DelaunayTriangulationBuilder.unique(CoordinateArrays.toCoordinateArray(coords));
  25755. }
  25756. }
  25757. setClipEnvelope(clipEnv) {
  25758. this._clipEnv = clipEnv;
  25759. }
  25760. getSubdivision() {
  25761. this.create();
  25762. return this._subdiv;
  25763. }
  25764. }
  25765. var quadedge = /*#__PURE__*/Object.freeze({
  25766. __proto__: null,
  25767. Vertex: Vertex
  25768. });
  25769. var triangulate = /*#__PURE__*/Object.freeze({
  25770. __proto__: null,
  25771. ConformingDelaunayTriangulationBuilder: ConformingDelaunayTriangulationBuilder,
  25772. DelaunayTriangulationBuilder: DelaunayTriangulationBuilder,
  25773. VoronoiDiagramBuilder: VoronoiDiagramBuilder,
  25774. quadedge: quadedge
  25775. });
  25776. class LinearIterator {
  25777. constructor() {
  25778. LinearIterator.constructor_.apply(this, arguments);
  25779. }
  25780. static constructor_() {
  25781. this._linearGeom = null;
  25782. this._numLines = null;
  25783. this._currentLine = null;
  25784. this._componentIndex = 0;
  25785. this._vertexIndex = 0;
  25786. if (arguments.length === 1) {
  25787. const linear = arguments[0];
  25788. LinearIterator.constructor_.call(this, linear, 0, 0);
  25789. } else if (arguments.length === 2) {
  25790. const linear = arguments[0],
  25791. start = arguments[1];
  25792. LinearIterator.constructor_.call(this, linear, start.getComponentIndex(), LinearIterator.segmentEndVertexIndex(start));
  25793. } else if (arguments.length === 3) {
  25794. const linearGeom = arguments[0],
  25795. componentIndex = arguments[1],
  25796. vertexIndex = arguments[2];
  25797. if (!hasInterface(linearGeom, Lineal)) throw new IllegalArgumentException('Lineal geometry is required');
  25798. this._linearGeom = linearGeom;
  25799. this._numLines = linearGeom.getNumGeometries();
  25800. this._componentIndex = componentIndex;
  25801. this._vertexIndex = vertexIndex;
  25802. this.loadCurrentLine();
  25803. }
  25804. }
  25805. static segmentEndVertexIndex(loc) {
  25806. if (loc.getSegmentFraction() > 0.0) return loc.getSegmentIndex() + 1;
  25807. return loc.getSegmentIndex();
  25808. }
  25809. getComponentIndex() {
  25810. return this._componentIndex;
  25811. }
  25812. getLine() {
  25813. return this._currentLine;
  25814. }
  25815. getVertexIndex() {
  25816. return this._vertexIndex;
  25817. }
  25818. getSegmentEnd() {
  25819. if (this._vertexIndex < this.getLine().getNumPoints() - 1) return this._currentLine.getCoordinateN(this._vertexIndex + 1);
  25820. return null;
  25821. }
  25822. next() {
  25823. if (!this.hasNext()) return null;
  25824. this._vertexIndex++;
  25825. if (this._vertexIndex >= this._currentLine.getNumPoints()) {
  25826. this._componentIndex++;
  25827. this.loadCurrentLine();
  25828. this._vertexIndex = 0;
  25829. }
  25830. }
  25831. loadCurrentLine() {
  25832. if (this._componentIndex >= this._numLines) {
  25833. this._currentLine = null;
  25834. return null;
  25835. }
  25836. this._currentLine = this._linearGeom.getGeometryN(this._componentIndex);
  25837. }
  25838. getSegmentStart() {
  25839. return this._currentLine.getCoordinateN(this._vertexIndex);
  25840. }
  25841. isEndOfLine() {
  25842. if (this._componentIndex >= this._numLines) return false;
  25843. if (this._vertexIndex < this._currentLine.getNumPoints() - 1) return false;
  25844. return true;
  25845. }
  25846. hasNext() {
  25847. if (this._componentIndex >= this._numLines) return false;
  25848. if (this._componentIndex === this._numLines - 1 && this._vertexIndex >= this._currentLine.getNumPoints()) return false;
  25849. return true;
  25850. }
  25851. }
  25852. class LengthIndexOfPoint {
  25853. constructor() {
  25854. LengthIndexOfPoint.constructor_.apply(this, arguments);
  25855. }
  25856. static constructor_() {
  25857. this._linearGeom = null;
  25858. const linearGeom = arguments[0];
  25859. this._linearGeom = linearGeom;
  25860. }
  25861. static indexOf(linearGeom, inputPt) {
  25862. const locater = new LengthIndexOfPoint(linearGeom);
  25863. return locater.indexOf(inputPt);
  25864. }
  25865. static indexOfAfter(linearGeom, inputPt, minIndex) {
  25866. const locater = new LengthIndexOfPoint(linearGeom);
  25867. return locater.indexOfAfter(inputPt, minIndex);
  25868. }
  25869. indexOf(inputPt) {
  25870. return this.indexOfFromStart(inputPt, -1.0);
  25871. }
  25872. indexOfFromStart(inputPt, minIndex) {
  25873. let minDistance = Double.MAX_VALUE;
  25874. let ptMeasure = minIndex;
  25875. let segmentStartMeasure = 0.0;
  25876. const seg = new LineSegment();
  25877. const it = new LinearIterator(this._linearGeom);
  25878. while (it.hasNext()) {
  25879. if (!it.isEndOfLine()) {
  25880. seg.p0 = it.getSegmentStart();
  25881. seg.p1 = it.getSegmentEnd();
  25882. const segDistance = seg.distance(inputPt);
  25883. const segMeasureToPt = this.segmentNearestMeasure(seg, inputPt, segmentStartMeasure);
  25884. if (segDistance < minDistance && segMeasureToPt > minIndex) {
  25885. ptMeasure = segMeasureToPt;
  25886. minDistance = segDistance;
  25887. }
  25888. segmentStartMeasure += seg.getLength();
  25889. }
  25890. it.next();
  25891. }
  25892. return ptMeasure;
  25893. }
  25894. indexOfAfter(inputPt, minIndex) {
  25895. if (minIndex < 0.0) return this.indexOf(inputPt);
  25896. const endIndex = this._linearGeom.getLength();
  25897. if (endIndex < minIndex) return endIndex;
  25898. const closestAfter = this.indexOfFromStart(inputPt, minIndex);
  25899. Assert.isTrue(closestAfter >= minIndex, 'computed index is before specified minimum index');
  25900. return closestAfter;
  25901. }
  25902. segmentNearestMeasure(seg, inputPt, segmentStartMeasure) {
  25903. const projFactor = seg.projectionFactor(inputPt);
  25904. if (projFactor <= 0.0) return segmentStartMeasure;
  25905. if (projFactor <= 1.0) return segmentStartMeasure + projFactor * seg.getLength();
  25906. return segmentStartMeasure + seg.getLength();
  25907. }
  25908. }
  25909. class LinearLocation {
  25910. constructor() {
  25911. LinearLocation.constructor_.apply(this, arguments);
  25912. }
  25913. static constructor_() {
  25914. this._componentIndex = 0;
  25915. this._segmentIndex = 0;
  25916. this._segmentFraction = 0.0;
  25917. if (arguments.length === 0) ; else if (arguments.length === 1) {
  25918. const loc = arguments[0];
  25919. this._componentIndex = loc._componentIndex;
  25920. this._segmentIndex = loc._segmentIndex;
  25921. this._segmentFraction = loc._segmentFraction;
  25922. } else if (arguments.length === 2) {
  25923. const segmentIndex = arguments[0],
  25924. segmentFraction = arguments[1];
  25925. LinearLocation.constructor_.call(this, 0, segmentIndex, segmentFraction);
  25926. } else if (arguments.length === 3) {
  25927. const componentIndex = arguments[0],
  25928. segmentIndex = arguments[1],
  25929. segmentFraction = arguments[2];
  25930. this._componentIndex = componentIndex;
  25931. this._segmentIndex = segmentIndex;
  25932. this._segmentFraction = segmentFraction;
  25933. this.normalize();
  25934. } else if (arguments.length === 4) {
  25935. const componentIndex = arguments[0],
  25936. segmentIndex = arguments[1],
  25937. segmentFraction = arguments[2],
  25938. doNormalize = arguments[3];
  25939. this._componentIndex = componentIndex;
  25940. this._segmentIndex = segmentIndex;
  25941. this._segmentFraction = segmentFraction;
  25942. if (doNormalize) this.normalize();
  25943. }
  25944. }
  25945. static getEndLocation(linear) {
  25946. const loc = new LinearLocation();
  25947. loc.setToEnd(linear);
  25948. return loc;
  25949. }
  25950. static pointAlongSegmentByFraction(p0, p1, frac) {
  25951. if (frac <= 0.0) return p0;
  25952. if (frac >= 1.0) return p1;
  25953. const x = (p1.x - p0.x) * frac + p0.x;
  25954. const y = (p1.y - p0.y) * frac + p0.y;
  25955. const z = (p1.getZ() - p0.getZ()) * frac + p0.getZ();
  25956. return new Coordinate(x, y, z);
  25957. }
  25958. static compareLocationValues(componentIndex0, segmentIndex0, segmentFraction0, componentIndex1, segmentIndex1, segmentFraction1) {
  25959. if (componentIndex0 < componentIndex1) return -1;
  25960. if (componentIndex0 > componentIndex1) return 1;
  25961. if (segmentIndex0 < segmentIndex1) return -1;
  25962. if (segmentIndex0 > segmentIndex1) return 1;
  25963. if (segmentFraction0 < segmentFraction1) return -1;
  25964. if (segmentFraction0 > segmentFraction1) return 1;
  25965. return 0;
  25966. }
  25967. static numSegments(line) {
  25968. const npts = line.getNumPoints();
  25969. if (npts <= 1) return 0;
  25970. return npts - 1;
  25971. }
  25972. getSegmentIndex() {
  25973. return this._segmentIndex;
  25974. }
  25975. getComponentIndex() {
  25976. return this._componentIndex;
  25977. }
  25978. isEndpoint(linearGeom) {
  25979. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  25980. const nseg = LinearLocation.numSegments(lineComp);
  25981. return this._segmentIndex >= nseg || this._segmentIndex === nseg - 1 && this._segmentFraction >= 1.0;
  25982. }
  25983. isValid(linearGeom) {
  25984. if (this._componentIndex < 0 || this._componentIndex >= linearGeom.getNumGeometries()) return false;
  25985. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  25986. if (this._segmentIndex < 0 || this._segmentIndex > lineComp.getNumPoints()) return false;
  25987. if (this._segmentIndex === lineComp.getNumPoints() && this._segmentFraction !== 0.0) return false;
  25988. if (this._segmentFraction < 0.0 || this._segmentFraction > 1.0) return false;
  25989. return true;
  25990. }
  25991. normalize() {
  25992. if (this._segmentFraction < 0.0) this._segmentFraction = 0.0;
  25993. if (this._segmentFraction > 1.0) this._segmentFraction = 1.0;
  25994. if (this._componentIndex < 0) {
  25995. this._componentIndex = 0;
  25996. this._segmentIndex = 0;
  25997. this._segmentFraction = 0.0;
  25998. }
  25999. if (this._segmentIndex < 0) {
  26000. this._segmentIndex = 0;
  26001. this._segmentFraction = 0.0;
  26002. }
  26003. if (this._segmentFraction === 1.0) {
  26004. this._segmentFraction = 0.0;
  26005. this._segmentIndex += 1;
  26006. }
  26007. }
  26008. toLowest(linearGeom) {
  26009. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  26010. const nseg = LinearLocation.numSegments(lineComp);
  26011. if (this._segmentIndex < nseg) return this;
  26012. return new LinearLocation(this._componentIndex, nseg - 1, 1.0, false);
  26013. }
  26014. getCoordinate(linearGeom) {
  26015. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  26016. const p0 = lineComp.getCoordinateN(this._segmentIndex);
  26017. if (this._segmentIndex >= LinearLocation.numSegments(lineComp)) return p0;
  26018. const p1 = lineComp.getCoordinateN(this._segmentIndex + 1);
  26019. return LinearLocation.pointAlongSegmentByFraction(p0, p1, this._segmentFraction);
  26020. }
  26021. getSegmentFraction() {
  26022. return this._segmentFraction;
  26023. }
  26024. getSegment(linearGeom) {
  26025. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  26026. const p0 = lineComp.getCoordinateN(this._segmentIndex);
  26027. if (this._segmentIndex >= LinearLocation.numSegments(lineComp)) {
  26028. const prev = lineComp.getCoordinateN(lineComp.getNumPoints() - 2);
  26029. return new LineSegment(prev, p0);
  26030. }
  26031. const p1 = lineComp.getCoordinateN(this._segmentIndex + 1);
  26032. return new LineSegment(p0, p1);
  26033. }
  26034. clamp(linear) {
  26035. if (this._componentIndex >= linear.getNumGeometries()) {
  26036. this.setToEnd(linear);
  26037. return null;
  26038. }
  26039. if (this._segmentIndex >= linear.getNumPoints()) {
  26040. const line = linear.getGeometryN(this._componentIndex);
  26041. this._segmentIndex = LinearLocation.numSegments(line);
  26042. this._segmentFraction = 1.0;
  26043. }
  26044. }
  26045. setToEnd(linear) {
  26046. this._componentIndex = linear.getNumGeometries() - 1;
  26047. const lastLine = linear.getGeometryN(this._componentIndex);
  26048. this._segmentIndex = LinearLocation.numSegments(lastLine);
  26049. this._segmentFraction = 0.0;
  26050. }
  26051. compareTo(o) {
  26052. const other = o;
  26053. if (this._componentIndex < other._componentIndex) return -1;
  26054. if (this._componentIndex > other._componentIndex) return 1;
  26055. if (this._segmentIndex < other._segmentIndex) return -1;
  26056. if (this._segmentIndex > other._segmentIndex) return 1;
  26057. if (this._segmentFraction < other._segmentFraction) return -1;
  26058. if (this._segmentFraction > other._segmentFraction) return 1;
  26059. return 0;
  26060. }
  26061. copy() {
  26062. return new LinearLocation(this._componentIndex, this._segmentIndex, this._segmentFraction);
  26063. }
  26064. toString() {
  26065. return 'LinearLoc[' + this._componentIndex + ', ' + this._segmentIndex + ', ' + this._segmentFraction + ']';
  26066. }
  26067. isOnSameSegment(loc) {
  26068. if (this._componentIndex !== loc._componentIndex) return false;
  26069. if (this._segmentIndex === loc._segmentIndex) return true;
  26070. if (loc._segmentIndex - this._segmentIndex === 1 && loc._segmentFraction === 0.0) return true;
  26071. if (this._segmentIndex - loc._segmentIndex === 1 && this._segmentFraction === 0.0) return true;
  26072. return false;
  26073. }
  26074. snapToVertex(linearGeom, minDistance) {
  26075. if (this._segmentFraction <= 0.0 || this._segmentFraction >= 1.0) return null;
  26076. const segLen = this.getSegmentLength(linearGeom);
  26077. const lenToStart = this._segmentFraction * segLen;
  26078. const lenToEnd = segLen - lenToStart;
  26079. if (lenToStart <= lenToEnd && lenToStart < minDistance) this._segmentFraction = 0.0;else if (lenToEnd <= lenToStart && lenToEnd < minDistance) this._segmentFraction = 1.0;
  26080. }
  26081. compareLocationValues(componentIndex1, segmentIndex1, segmentFraction1) {
  26082. if (this._componentIndex < componentIndex1) return -1;
  26083. if (this._componentIndex > componentIndex1) return 1;
  26084. if (this._segmentIndex < segmentIndex1) return -1;
  26085. if (this._segmentIndex > segmentIndex1) return 1;
  26086. if (this._segmentFraction < segmentFraction1) return -1;
  26087. if (this._segmentFraction > segmentFraction1) return 1;
  26088. return 0;
  26089. }
  26090. getSegmentLength(linearGeom) {
  26091. const lineComp = linearGeom.getGeometryN(this._componentIndex);
  26092. let segIndex = this._segmentIndex;
  26093. if (this._segmentIndex >= LinearLocation.numSegments(lineComp)) segIndex = lineComp.getNumPoints() - 2;
  26094. const p0 = lineComp.getCoordinateN(segIndex);
  26095. const p1 = lineComp.getCoordinateN(segIndex + 1);
  26096. return p0.distance(p1);
  26097. }
  26098. isVertex() {
  26099. return this._segmentFraction <= 0.0 || this._segmentFraction >= 1.0;
  26100. }
  26101. get interfaces_() {
  26102. return [Comparable];
  26103. }
  26104. }
  26105. class LocationIndexOfPoint {
  26106. constructor() {
  26107. LocationIndexOfPoint.constructor_.apply(this, arguments);
  26108. }
  26109. static constructor_() {
  26110. this._linearGeom = null;
  26111. const linearGeom = arguments[0];
  26112. this._linearGeom = linearGeom;
  26113. }
  26114. static indexOf(linearGeom, inputPt) {
  26115. const locater = new LocationIndexOfPoint(linearGeom);
  26116. return locater.indexOf(inputPt);
  26117. }
  26118. static indexOfAfter(linearGeom, inputPt, minIndex) {
  26119. const locater = new LocationIndexOfPoint(linearGeom);
  26120. return locater.indexOfAfter(inputPt, minIndex);
  26121. }
  26122. indexOf(inputPt) {
  26123. return this.indexOfFromStart(inputPt, null);
  26124. }
  26125. indexOfFromStart(inputPt, minIndex) {
  26126. let minDistance = Double.MAX_VALUE;
  26127. let minComponentIndex = 0;
  26128. let minSegmentIndex = 0;
  26129. let minFrac = -1.0;
  26130. const seg = new LineSegment();
  26131. for (let it = new LinearIterator(this._linearGeom); it.hasNext(); it.next()) if (!it.isEndOfLine()) {
  26132. seg.p0 = it.getSegmentStart();
  26133. seg.p1 = it.getSegmentEnd();
  26134. const segDistance = seg.distance(inputPt);
  26135. const segFrac = seg.segmentFraction(inputPt);
  26136. const candidateComponentIndex = it.getComponentIndex();
  26137. const candidateSegmentIndex = it.getVertexIndex();
  26138. if (segDistance < minDistance) if (minIndex === null || minIndex.compareLocationValues(candidateComponentIndex, candidateSegmentIndex, segFrac) < 0) {
  26139. minComponentIndex = candidateComponentIndex;
  26140. minSegmentIndex = candidateSegmentIndex;
  26141. minFrac = segFrac;
  26142. minDistance = segDistance;
  26143. }
  26144. }
  26145. if (minDistance === Double.MAX_VALUE) return new LinearLocation(minIndex);
  26146. const loc = new LinearLocation(minComponentIndex, minSegmentIndex, minFrac);
  26147. return loc;
  26148. }
  26149. indexOfAfter(inputPt, minIndex) {
  26150. if (minIndex === null) return this.indexOf(inputPt);
  26151. const endLoc = LinearLocation.getEndLocation(this._linearGeom);
  26152. if (endLoc.compareTo(minIndex) <= 0) return endLoc;
  26153. const closestAfter = this.indexOfFromStart(inputPt, minIndex);
  26154. Assert.isTrue(closestAfter.compareTo(minIndex) >= 0, 'computed location is before specified minimum location');
  26155. return closestAfter;
  26156. }
  26157. }
  26158. class LocationIndexOfLine {
  26159. constructor() {
  26160. LocationIndexOfLine.constructor_.apply(this, arguments);
  26161. }
  26162. static constructor_() {
  26163. this._linearGeom = null;
  26164. const linearGeom = arguments[0];
  26165. this._linearGeom = linearGeom;
  26166. }
  26167. static indicesOf(linearGeom, subLine) {
  26168. const locater = new LocationIndexOfLine(linearGeom);
  26169. return locater.indicesOf(subLine);
  26170. }
  26171. indicesOf(subLine) {
  26172. const startPt = subLine.getGeometryN(0).getCoordinateN(0);
  26173. const lastLine = subLine.getGeometryN(subLine.getNumGeometries() - 1);
  26174. const endPt = lastLine.getCoordinateN(lastLine.getNumPoints() - 1);
  26175. const locPt = new LocationIndexOfPoint(this._linearGeom);
  26176. const subLineLoc = new Array(2).fill(null);
  26177. subLineLoc[0] = locPt.indexOf(startPt);
  26178. if (subLine.getLength() === 0.0) subLineLoc[1] = subLineLoc[0].copy();else subLineLoc[1] = locPt.indexOfAfter(endPt, subLineLoc[0]);
  26179. return subLineLoc;
  26180. }
  26181. }
  26182. class LengthLocationMap {
  26183. constructor() {
  26184. LengthLocationMap.constructor_.apply(this, arguments);
  26185. }
  26186. static constructor_() {
  26187. this._linearGeom = null;
  26188. const linearGeom = arguments[0];
  26189. this._linearGeom = linearGeom;
  26190. }
  26191. static getLength(linearGeom, loc) {
  26192. const locater = new LengthLocationMap(linearGeom);
  26193. return locater.getLength(loc);
  26194. }
  26195. static getLocation() {
  26196. if (arguments.length === 2) {
  26197. const linearGeom = arguments[0],
  26198. length = arguments[1];
  26199. const locater = new LengthLocationMap(linearGeom);
  26200. return locater.getLocation(length);
  26201. } else if (arguments.length === 3) {
  26202. const linearGeom = arguments[0],
  26203. length = arguments[1],
  26204. resolveLower = arguments[2];
  26205. const locater = new LengthLocationMap(linearGeom);
  26206. return locater.getLocation(length, resolveLower);
  26207. }
  26208. }
  26209. getLength(loc) {
  26210. let totalLength = 0.0;
  26211. const it = new LinearIterator(this._linearGeom);
  26212. while (it.hasNext()) {
  26213. if (!it.isEndOfLine()) {
  26214. const p0 = it.getSegmentStart();
  26215. const p1 = it.getSegmentEnd();
  26216. const segLen = p1.distance(p0);
  26217. if (loc.getComponentIndex() === it.getComponentIndex() && loc.getSegmentIndex() === it.getVertexIndex()) return totalLength + segLen * loc.getSegmentFraction();
  26218. totalLength += segLen;
  26219. }
  26220. it.next();
  26221. }
  26222. return totalLength;
  26223. }
  26224. resolveHigher(loc) {
  26225. if (!loc.isEndpoint(this._linearGeom)) return loc;
  26226. let compIndex = loc.getComponentIndex();
  26227. if (compIndex >= this._linearGeom.getNumGeometries() - 1) return loc;
  26228. do compIndex++; while (compIndex < this._linearGeom.getNumGeometries() - 1 && this._linearGeom.getGeometryN(compIndex).getLength() === 0);
  26229. return new LinearLocation(compIndex, 0, 0.0);
  26230. }
  26231. getLocation() {
  26232. if (arguments.length === 1) {
  26233. const length = arguments[0];
  26234. return this.getLocation(length, true);
  26235. } else if (arguments.length === 2) {
  26236. const length = arguments[0],
  26237. resolveLower = arguments[1];
  26238. let forwardLength = length;
  26239. if (length < 0.0) {
  26240. const lineLen = this._linearGeom.getLength();
  26241. forwardLength = lineLen + length;
  26242. }
  26243. const loc = this.getLocationForward(forwardLength);
  26244. if (resolveLower) return loc;
  26245. return this.resolveHigher(loc);
  26246. }
  26247. }
  26248. getLocationForward(length) {
  26249. if (length <= 0.0) return new LinearLocation();
  26250. let totalLength = 0.0;
  26251. const it = new LinearIterator(this._linearGeom);
  26252. while (it.hasNext()) {
  26253. if (it.isEndOfLine()) {
  26254. if (totalLength === length) {
  26255. const compIndex = it.getComponentIndex();
  26256. const segIndex = it.getVertexIndex();
  26257. return new LinearLocation(compIndex, segIndex, 0.0);
  26258. }
  26259. } else {
  26260. const p0 = it.getSegmentStart();
  26261. const p1 = it.getSegmentEnd();
  26262. const segLen = p1.distance(p0);
  26263. if (totalLength + segLen > length) {
  26264. const frac = (length - totalLength) / segLen;
  26265. const compIndex = it.getComponentIndex();
  26266. const segIndex = it.getVertexIndex();
  26267. return new LinearLocation(compIndex, segIndex, frac);
  26268. }
  26269. totalLength += segLen;
  26270. }
  26271. it.next();
  26272. }
  26273. return LinearLocation.getEndLocation(this._linearGeom);
  26274. }
  26275. }
  26276. class LinearGeometryBuilder {
  26277. constructor() {
  26278. LinearGeometryBuilder.constructor_.apply(this, arguments);
  26279. }
  26280. static constructor_() {
  26281. this._geomFact = null;
  26282. this._lines = new ArrayList();
  26283. this._coordList = null;
  26284. this._ignoreInvalidLines = false;
  26285. this._fixInvalidLines = false;
  26286. this._lastPt = null;
  26287. const geomFact = arguments[0];
  26288. this._geomFact = geomFact;
  26289. }
  26290. getGeometry() {
  26291. this.endLine();
  26292. return this._geomFact.buildGeometry(this._lines);
  26293. }
  26294. getLastCoordinate() {
  26295. return this._lastPt;
  26296. }
  26297. endLine() {
  26298. if (this._coordList === null) return null;
  26299. if (this._ignoreInvalidLines && this._coordList.size() < 2) {
  26300. this._coordList = null;
  26301. return null;
  26302. }
  26303. const rawPts = this._coordList.toCoordinateArray();
  26304. let pts = rawPts;
  26305. if (this._fixInvalidLines) pts = this.validCoordinateSequence(rawPts);
  26306. this._coordList = null;
  26307. let line = null;
  26308. try {
  26309. line = this._geomFact.createLineString(pts);
  26310. } catch (ex) {
  26311. if (ex instanceof IllegalArgumentException) {
  26312. if (!this._ignoreInvalidLines) throw ex;
  26313. } else {
  26314. throw ex;
  26315. }
  26316. } finally {}
  26317. if (line !== null) this._lines.add(line);
  26318. }
  26319. setFixInvalidLines(fixInvalidLines) {
  26320. this._fixInvalidLines = fixInvalidLines;
  26321. }
  26322. add() {
  26323. if (arguments.length === 1) {
  26324. const pt = arguments[0];
  26325. this.add(pt, true);
  26326. } else if (arguments.length === 2) {
  26327. const pt = arguments[0],
  26328. allowRepeatedPoints = arguments[1];
  26329. if (this._coordList === null) this._coordList = new CoordinateList();
  26330. this._coordList.add(pt, allowRepeatedPoints);
  26331. this._lastPt = pt;
  26332. }
  26333. }
  26334. setIgnoreInvalidLines(ignoreInvalidLines) {
  26335. this._ignoreInvalidLines = ignoreInvalidLines;
  26336. }
  26337. validCoordinateSequence(pts) {
  26338. if (pts.length >= 2) return pts;
  26339. const validPts = [pts[0], pts[0]];
  26340. return validPts;
  26341. }
  26342. }
  26343. class ExtractLineByLocation {
  26344. constructor() {
  26345. ExtractLineByLocation.constructor_.apply(this, arguments);
  26346. }
  26347. static constructor_() {
  26348. this._line = null;
  26349. const line = arguments[0];
  26350. this._line = line;
  26351. }
  26352. static extract(line, start, end) {
  26353. const ls = new ExtractLineByLocation(line);
  26354. return ls.extract(start, end);
  26355. }
  26356. computeLinear(start, end) {
  26357. const builder = new LinearGeometryBuilder(this._line.getFactory());
  26358. builder.setFixInvalidLines(true);
  26359. if (!start.isVertex()) builder.add(start.getCoordinate(this._line));
  26360. for (let it = new LinearIterator(this._line, start); it.hasNext(); it.next()) {
  26361. if (end.compareLocationValues(it.getComponentIndex(), it.getVertexIndex(), 0.0) < 0) break;
  26362. const pt = it.getSegmentStart();
  26363. builder.add(pt);
  26364. if (it.isEndOfLine()) builder.endLine();
  26365. }
  26366. if (!end.isVertex()) builder.add(end.getCoordinate(this._line));
  26367. return builder.getGeometry();
  26368. }
  26369. computeLine(start, end) {
  26370. const coordinates = this._line.getCoordinates();
  26371. const newCoordinates = new CoordinateList();
  26372. let startSegmentIndex = start.getSegmentIndex();
  26373. if (start.getSegmentFraction() > 0.0) startSegmentIndex += 1;
  26374. let lastSegmentIndex = end.getSegmentIndex();
  26375. if (end.getSegmentFraction() === 1.0) lastSegmentIndex += 1;
  26376. if (lastSegmentIndex >= coordinates.length) lastSegmentIndex = coordinates.length - 1;
  26377. if (!start.isVertex()) newCoordinates.add(start.getCoordinate(this._line));
  26378. for (let i = startSegmentIndex; i <= lastSegmentIndex; i++) newCoordinates.add(coordinates[i]);
  26379. if (!end.isVertex()) newCoordinates.add(end.getCoordinate(this._line));
  26380. if (newCoordinates.size() <= 0) newCoordinates.add(start.getCoordinate(this._line));
  26381. let newCoordinateArray = newCoordinates.toCoordinateArray();
  26382. if (newCoordinateArray.length <= 1) newCoordinateArray = [newCoordinateArray[0], newCoordinateArray[0]];
  26383. return this._line.getFactory().createLineString(newCoordinateArray);
  26384. }
  26385. extract(start, end) {
  26386. if (end.compareTo(start) < 0) return this.reverse(this.computeLinear(end, start));
  26387. return this.computeLinear(start, end);
  26388. }
  26389. reverse(linear) {
  26390. if (hasInterface(linear, Lineal)) return linear.reverse();
  26391. Assert.shouldNeverReachHere('non-linear geometry encountered');
  26392. return null;
  26393. }
  26394. }
  26395. class LengthIndexedLine {
  26396. constructor() {
  26397. LengthIndexedLine.constructor_.apply(this, arguments);
  26398. }
  26399. static constructor_() {
  26400. this._linearGeom = null;
  26401. const linearGeom = arguments[0];
  26402. this._linearGeom = linearGeom;
  26403. }
  26404. clampIndex(index) {
  26405. const posIndex = this.positiveIndex(index);
  26406. const startIndex = this.getStartIndex();
  26407. if (posIndex < startIndex) return startIndex;
  26408. const endIndex = this.getEndIndex();
  26409. if (posIndex > endIndex) return endIndex;
  26410. return posIndex;
  26411. }
  26412. locationOf() {
  26413. if (arguments.length === 1) {
  26414. const index = arguments[0];
  26415. return LengthLocationMap.getLocation(this._linearGeom, index);
  26416. } else if (arguments.length === 2) {
  26417. const index = arguments[0],
  26418. resolveLower = arguments[1];
  26419. return LengthLocationMap.getLocation(this._linearGeom, index, resolveLower);
  26420. }
  26421. }
  26422. project(pt) {
  26423. return LengthIndexOfPoint.indexOf(this._linearGeom, pt);
  26424. }
  26425. positiveIndex(index) {
  26426. if (index >= 0.0) return index;
  26427. return this._linearGeom.getLength() + index;
  26428. }
  26429. extractPoint() {
  26430. if (arguments.length === 1) {
  26431. const index = arguments[0];
  26432. const loc = LengthLocationMap.getLocation(this._linearGeom, index);
  26433. return loc.getCoordinate(this._linearGeom);
  26434. } else if (arguments.length === 2) {
  26435. const index = arguments[0],
  26436. offsetDistance = arguments[1];
  26437. const loc = LengthLocationMap.getLocation(this._linearGeom, index);
  26438. const locLow = loc.toLowest(this._linearGeom);
  26439. return locLow.getSegment(this._linearGeom).pointAlongOffset(locLow.getSegmentFraction(), offsetDistance);
  26440. }
  26441. }
  26442. isValidIndex(index) {
  26443. return index >= this.getStartIndex() && index <= this.getEndIndex();
  26444. }
  26445. getEndIndex() {
  26446. return this._linearGeom.getLength();
  26447. }
  26448. getStartIndex() {
  26449. return 0.0;
  26450. }
  26451. indexOfAfter(pt, minIndex) {
  26452. return LengthIndexOfPoint.indexOfAfter(this._linearGeom, pt, minIndex);
  26453. }
  26454. extractLine(startIndex, endIndex) {
  26455. const startIndex2 = this.clampIndex(startIndex);
  26456. const endIndex2 = this.clampIndex(endIndex);
  26457. const resolveStartLower = startIndex2 === endIndex2;
  26458. const startLoc = this.locationOf(startIndex2, resolveStartLower);
  26459. const endLoc = this.locationOf(endIndex2);
  26460. return ExtractLineByLocation.extract(this._linearGeom, startLoc, endLoc);
  26461. }
  26462. indexOf(pt) {
  26463. return LengthIndexOfPoint.indexOf(this._linearGeom, pt);
  26464. }
  26465. indicesOf(subLine) {
  26466. const locIndex = LocationIndexOfLine.indicesOf(this._linearGeom, subLine);
  26467. const index = [LengthLocationMap.getLength(this._linearGeom, locIndex[0]), LengthLocationMap.getLength(this._linearGeom, locIndex[1])];
  26468. return index;
  26469. }
  26470. }
  26471. class LocationIndexedLine {
  26472. constructor() {
  26473. LocationIndexedLine.constructor_.apply(this, arguments);
  26474. }
  26475. static constructor_() {
  26476. this._linearGeom = null;
  26477. const linearGeom = arguments[0];
  26478. this._linearGeom = linearGeom;
  26479. this.checkGeometryType();
  26480. }
  26481. clampIndex(index) {
  26482. const loc = index.copy();
  26483. loc.clamp(this._linearGeom);
  26484. return loc;
  26485. }
  26486. project(pt) {
  26487. return LocationIndexOfPoint.indexOf(this._linearGeom, pt);
  26488. }
  26489. checkGeometryType() {
  26490. if (!(this._linearGeom instanceof LineString || this._linearGeom instanceof MultiLineString)) throw new IllegalArgumentException('Input geometry must be linear');
  26491. }
  26492. extractPoint() {
  26493. if (arguments.length === 1) {
  26494. const index = arguments[0];
  26495. return index.getCoordinate(this._linearGeom);
  26496. } else if (arguments.length === 2) {
  26497. const index = arguments[0],
  26498. offsetDistance = arguments[1];
  26499. const indexLow = index.toLowest(this._linearGeom);
  26500. return indexLow.getSegment(this._linearGeom).pointAlongOffset(indexLow.getSegmentFraction(), offsetDistance);
  26501. }
  26502. }
  26503. isValidIndex(index) {
  26504. return index.isValid(this._linearGeom);
  26505. }
  26506. getEndIndex() {
  26507. return LinearLocation.getEndLocation(this._linearGeom);
  26508. }
  26509. getStartIndex() {
  26510. return new LinearLocation();
  26511. }
  26512. indexOfAfter(pt, minIndex) {
  26513. return LocationIndexOfPoint.indexOfAfter(this._linearGeom, pt, minIndex);
  26514. }
  26515. extractLine(startIndex, endIndex) {
  26516. return ExtractLineByLocation.extract(this._linearGeom, startIndex, endIndex);
  26517. }
  26518. indexOf(pt) {
  26519. return LocationIndexOfPoint.indexOf(this._linearGeom, pt);
  26520. }
  26521. indicesOf(subLine) {
  26522. return LocationIndexOfLine.indicesOf(this._linearGeom, subLine);
  26523. }
  26524. }
  26525. var linearref = /*#__PURE__*/Object.freeze({
  26526. __proto__: null,
  26527. LengthIndexedLine: LengthIndexedLine,
  26528. LengthLocationMap: LengthLocationMap,
  26529. LinearGeometryBuilder: LinearGeometryBuilder,
  26530. LinearIterator: LinearIterator,
  26531. LinearLocation: LinearLocation,
  26532. LocationIndexedLine: LocationIndexedLine
  26533. });
  26534. class CollectionUtil {
  26535. static transform(coll, func) {
  26536. const result = new ArrayList();
  26537. for (let i = coll.iterator(); i.hasNext();) result.add(func.execute(i.next()));
  26538. return result;
  26539. }
  26540. static select(collection, func) {
  26541. const result = new ArrayList();
  26542. for (let i = collection.iterator(); i.hasNext();) {
  26543. const item = i.next();
  26544. if (Boolean.TRUE.equals(func.execute(item))) result.add(item);
  26545. }
  26546. return result;
  26547. }
  26548. static apply(coll, func) {
  26549. for (let i = coll.iterator(); i.hasNext();) func.execute(i.next());
  26550. }
  26551. }
  26552. function Function() {}
  26553. CollectionUtil.Function = Function;
  26554. class CoordinateArrayFilter {
  26555. constructor() {
  26556. CoordinateArrayFilter.constructor_.apply(this, arguments);
  26557. }
  26558. static constructor_() {
  26559. this.pts = null;
  26560. this.n = 0;
  26561. const size = arguments[0];
  26562. this.pts = new Array(size).fill(null);
  26563. }
  26564. filter(coord) {
  26565. this.pts[this.n++] = coord;
  26566. }
  26567. getCoordinates() {
  26568. return this.pts;
  26569. }
  26570. get interfaces_() {
  26571. return [CoordinateFilter];
  26572. }
  26573. }
  26574. class CoordinateCountFilter {
  26575. constructor() {
  26576. CoordinateCountFilter.constructor_.apply(this, arguments);
  26577. }
  26578. static constructor_() {
  26579. this._n = 0;
  26580. }
  26581. filter(coord) {
  26582. this._n++;
  26583. }
  26584. getCount() {
  26585. return this._n;
  26586. }
  26587. get interfaces_() {
  26588. return [CoordinateFilter];
  26589. }
  26590. }
  26591. class ObjectCounter {
  26592. constructor() {
  26593. ObjectCounter.constructor_.apply(this, arguments);
  26594. }
  26595. static constructor_() {
  26596. this._counts = new HashMap();
  26597. }
  26598. count(o) {
  26599. const counter = this._counts.get(o);
  26600. if (counter === null) return 0;else return counter.count();
  26601. }
  26602. add(o) {
  26603. const counter = this._counts.get(o);
  26604. if (counter === null) this._counts.put(o, new Counter(1));else counter.increment();
  26605. }
  26606. }
  26607. class Counter {
  26608. constructor() {
  26609. Counter.constructor_.apply(this, arguments);
  26610. }
  26611. static constructor_() {
  26612. this.count = 0;
  26613. if (arguments.length === 0) ; else if (arguments.length === 1) {
  26614. const count = arguments[0];
  26615. this.count = count;
  26616. }
  26617. }
  26618. count() {
  26619. return this.count;
  26620. }
  26621. increment() {
  26622. this.count++;
  26623. }
  26624. }
  26625. ObjectCounter.Counter = Counter;
  26626. function PrintStream() {}
  26627. function StringReader() {}
  26628. function ByteArrayOutputStream() {}
  26629. class IOException extends Exception {}
  26630. function LineNumberReader() {}
  26631. class StringUtil {
  26632. static chars(c, n) {
  26633. const ch = new Array(n).fill(null);
  26634. for (let i = 0; i < n; i++) ch[i] = c;
  26635. return new String(ch);
  26636. }
  26637. static getStackTrace() {
  26638. if (arguments.length === 1) {
  26639. const t = arguments[0];
  26640. const os = new ByteArrayOutputStream();
  26641. const ps = new PrintStream(os);
  26642. t.printStackTrace(ps);
  26643. return os.toString();
  26644. } else if (arguments.length === 2) {
  26645. const t = arguments[0],
  26646. depth = arguments[1];
  26647. let stackTrace = '';
  26648. const stringReader = new StringReader(StringUtil.getStackTrace(t));
  26649. const lineNumberReader = new LineNumberReader(stringReader);
  26650. for (let i = 0; i < depth; i++) try {
  26651. stackTrace += lineNumberReader.readLine() + StringUtil.NEWLINE;
  26652. } catch (e) {
  26653. if (e instanceof IOException) Assert.shouldNeverReachHere();else throw e;
  26654. } finally {}
  26655. return stackTrace;
  26656. }
  26657. }
  26658. static spaces(n) {
  26659. return StringUtil.chars(' ', n);
  26660. }
  26661. static split(s, separator) {
  26662. const separatorlen = separator.length;
  26663. const tokenList = new ArrayList();
  26664. let tmpString = '' + s;
  26665. let pos = tmpString.indexOf(separator);
  26666. while (pos >= 0) {
  26667. const token = tmpString.substring(0, pos);
  26668. tokenList.add(token);
  26669. tmpString = tmpString.substring(pos + separatorlen);
  26670. pos = tmpString.indexOf(separator);
  26671. }
  26672. if (tmpString.length > 0) tokenList.add(tmpString);
  26673. const res = new Array(tokenList.size()).fill(null);
  26674. for (let i = 0; i < res.length; i++) res[i] = tokenList.get(i);
  26675. return res;
  26676. }
  26677. }
  26678. StringUtil.NEWLINE = System.getProperty('line.separator');
  26679. var util = /*#__PURE__*/Object.freeze({
  26680. __proto__: null,
  26681. CollectionUtil: CollectionUtil,
  26682. CoordinateArrayFilter: CoordinateArrayFilter,
  26683. CoordinateCountFilter: CoordinateCountFilter,
  26684. GeometricShapeFactory: GeometricShapeFactory,
  26685. NumberUtil: NumberUtil,
  26686. ObjectCounter: ObjectCounter,
  26687. PriorityQueue: PriorityQueue,
  26688. StringUtil: StringUtil,
  26689. UniqueCoordinateArrayFilter: UniqueCoordinateArrayFilter
  26690. });
  26691. class UnionOp {
  26692. get interfaces_() {
  26693. return [];
  26694. }
  26695. getClass() {
  26696. return UnionOp;
  26697. }
  26698. static union(g, other) {
  26699. if (g.isEmpty() || other.isEmpty()) {
  26700. if (g.isEmpty() && other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.UNION, g, other, g.getFactory());
  26701. if (g.isEmpty()) return other.copy();
  26702. if (other.isEmpty()) return g.copy();
  26703. }
  26704. g.checkNotGeometryCollection(g);
  26705. g.checkNotGeometryCollection(other);
  26706. return SnapIfNeededOverlayOp.overlayOp(g, other, OverlayOp.UNION);
  26707. }
  26708. }
  26709. LineString.prototype.getBoundary = function () {
  26710. return BoundaryOp.getBoundary(this);
  26711. };
  26712. MultiLineString.prototype.getBoundary = function () {
  26713. return BoundaryOp.getBoundary(this);
  26714. };
  26715. Geometry.prototype.equalsTopo = function (g) {
  26716. return RelateOp.equalsTopo(this, g);
  26717. };
  26718. Geometry.prototype.equals = function (g) {
  26719. if (g === null) return false;
  26720. return RelateOp.equalsTopo(this, g);
  26721. };
  26722. Geometry.prototype.union = function () {
  26723. if (arguments.length === 0) {
  26724. return UnaryUnionOp.union(this);
  26725. } else if (arguments.length === 1) {
  26726. const other = arguments[0];
  26727. return UnionOp.union(this, other);
  26728. }
  26729. };
  26730. Geometry.prototype.isValid = function () {
  26731. return IsValidOp.isValid(this);
  26732. };
  26733. Geometry.prototype.intersection = function (other) {
  26734. return OverlayOp.intersection(this, other);
  26735. };
  26736. Geometry.prototype.covers = function (g) {
  26737. return RelateOp.covers(this, g);
  26738. };
  26739. Geometry.prototype.coveredBy = function (g) {
  26740. return RelateOp.covers(g, this);
  26741. };
  26742. Geometry.prototype.touches = function (g) {
  26743. return RelateOp.touches(this, g);
  26744. };
  26745. Geometry.prototype.intersects = function (g) {
  26746. return RelateOp.intersects(this, g);
  26747. };
  26748. Geometry.prototype.within = function (g) {
  26749. return RelateOp.contains(g, this);
  26750. };
  26751. Geometry.prototype.overlaps = function (g) {
  26752. return RelateOp.overlaps(this, g);
  26753. };
  26754. Geometry.prototype.disjoint = function (g) {
  26755. return RelateOp.disjoint(this, g);
  26756. };
  26757. Geometry.prototype.crosses = function (g) {
  26758. return RelateOp.crosses(this, g);
  26759. };
  26760. Geometry.prototype.buffer = function () {
  26761. if (arguments.length === 1) {
  26762. const distance = arguments[0];
  26763. return BufferOp.bufferOp(this, distance);
  26764. } else if (arguments.length === 2) {
  26765. const distance = arguments[0];
  26766. const quadrantSegments = arguments[1];
  26767. return BufferOp.bufferOp(this, distance, quadrantSegments);
  26768. } else if (arguments.length === 3) {
  26769. const distance = arguments[0];
  26770. const quadrantSegments = arguments[1];
  26771. const endCapStyle = arguments[2];
  26772. return BufferOp.bufferOp(this, distance, quadrantSegments, endCapStyle);
  26773. }
  26774. };
  26775. Geometry.prototype.convexHull = function () {
  26776. return new ConvexHull(this).getConvexHull();
  26777. };
  26778. Geometry.prototype.relate = function () {
  26779. if (arguments.length === 1) {
  26780. const geometry = arguments[0];
  26781. return RelateOp.relate(this, geometry);
  26782. } else if (arguments.length === 2) {
  26783. const geometry = arguments[0];
  26784. const intersectionPattern = arguments[1];
  26785. return RelateOp.relate(this, geometry).matches(intersectionPattern);
  26786. }
  26787. };
  26788. Geometry.prototype.getCentroid = function () {
  26789. if (this.isEmpty()) return this._factory.createPoint();
  26790. const centPt = Centroid.getCentroid(this);
  26791. return this.createPointFromInternalCoord(centPt, this);
  26792. };
  26793. Geometry.prototype.getInteriorPoint = function () {
  26794. if (this.isEmpty()) return this._factory.createPoint();
  26795. let intPt = null;
  26796. const dim = this.getDimension();
  26797. if (dim === 0) intPt = new InteriorPointPoint(this);else if (dim === 1) intPt = new InteriorPointLine(this);else intPt = new InteriorPointArea(this);
  26798. const interiorPt = intPt.getInteriorPoint();
  26799. return this.createPointFromInternalCoord(interiorPt, this);
  26800. };
  26801. Geometry.prototype.symDifference = function (other) {
  26802. return OverlayOp.symDifference(this, other);
  26803. };
  26804. Geometry.prototype.createPointFromInternalCoord = function (coord, exemplar) {
  26805. exemplar.getPrecisionModel().makePrecise(coord);
  26806. return exemplar.getFactory().createPoint(coord);
  26807. };
  26808. Geometry.prototype.toText = function () {
  26809. const writer = new WKTWriter();
  26810. return writer.write(this);
  26811. };
  26812. Geometry.prototype.toString = function () {
  26813. this.toText();
  26814. };
  26815. Geometry.prototype.contains = function (g) {
  26816. return RelateOp.contains(this, g);
  26817. };
  26818. Geometry.prototype.difference = function (other) {
  26819. return OverlayOp.difference(this, other);
  26820. };
  26821. Geometry.prototype.isSimple = function () {
  26822. const op = new IsSimpleOp(this);
  26823. return op.isSimple();
  26824. };
  26825. Geometry.prototype.isWithinDistance = function (geom, distance) {
  26826. const envDist = this.getEnvelopeInternal().distance(geom.getEnvelopeInternal());
  26827. if (envDist > distance) return false;
  26828. return DistanceOp.isWithinDistance(this, geom, distance);
  26829. };
  26830. Geometry.prototype.distance = function (g) {
  26831. return DistanceOp.distance(this, g);
  26832. };
  26833. const version = '2.7.1 (16652a2)';
  26834. exports.algorithm = algorithm;
  26835. exports.densify = densify;
  26836. exports.dissolve = dissolve;
  26837. exports.geom = geom;
  26838. exports.geomgraph = geomgraph;
  26839. exports.index = index;
  26840. exports.io = io;
  26841. exports.linearref = linearref;
  26842. exports.noding = noding;
  26843. exports.operation = operation;
  26844. exports.precision = precision;
  26845. exports.simplify = simplify;
  26846. exports.triangulate = triangulate;
  26847. exports.util = util;
  26848. exports.version = version;
  26849. Object.defineProperty(exports, '__esModule', { value: true });
  26850. })));
  26851. //# sourceMappingURL=jsts.js.map