From 5488ba5db2ceb3a5ff05b27ba9480787415c2b3b Mon Sep 17 00:00:00 2001 From: Ted Piotrowski Date: Sun, 12 May 2024 16:50:26 -0400 Subject: [PATCH] Add canopy source --- dist/mapbox-gl-shadow-simulator.esm.js | 4 ++-- dist/mapbox-gl-shadow-simulator.umd.min.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/mapbox-gl-shadow-simulator.esm.js b/dist/mapbox-gl-shadow-simulator.esm.js index fa6aca3..d5e4868 100644 --- a/dist/mapbox-gl-shadow-simulator.esm.js +++ b/dist/mapbox-gl-shadow-simulator.esm.js @@ -1,8 +1,8 @@ /** * Copyright Ted Piotrowski 2024 * Package: mapbox-gl-shadow-simulator - * Version: 0.57.1 + * Version: 0.58.0 * For licensing visit: https://shademap.app/about/ */ -function t(t,e,r){var i=e[1],o=e[0],n=i-o;return t===i&&r?t:((t-o)%n+n)%n+o}function e(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var r=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function i(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var o=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function n(t,e,o){return t instanceof i?t:r(t)?new i(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new i(t.x,t.y):new i(t,e,o)}function a(t,e){if(t)for(var r=e?[t,e]:t,i=0,o=r.length;i=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=s(t);var e=this.min,r=this.max,i=t.min,o=t.max,n=o.x>=e.x&&i.x<=r.x,a=o.y>=e.y&&i.y<=r.y;return n&&a},overlaps:function(t){t=s(t);var e=this.min,r=this.max,i=t.min,o=t.max,n=o.x>e.x&&i.xe.y&&i.y=i.lat&&r.lat<=o.lat&&e.lng>=i.lng&&r.lng<=o.lng},intersects:function(t){t=h(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),o=t.getNorthEast(),n=o.lat>=e.lat&&i.lat<=r.lat,a=o.lng>=e.lng&&i.lng<=r.lng;return n&&a},overlaps:function(t){t=h(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),o=t.getNorthEast(),n=o.lat>e.lat&&i.late.lng&&i.lng{const e=t.valueOf();return m(e)},m=t=>{const e=t/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,o=Math.atan2(Math.sin(i)*Math.cos(.40909994067971484),Math.cos(i));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-o)%(2*Math.PI)+2*Math.PI}},_=()=>Math.floor(1e7*Math.random()),g=(t,e,r)=>{const i=1/e,o=Math.min(t[0]*i,255),n=Math.min(t[1]*i,255),a=Math.min(t[2]*i,255);let s=0;return o+n+a!==0&&(s=o>0?o/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},x=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};const p=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new i(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,o),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,o))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new c((o=r.y,n=i,a=Math.PI-2*Math.PI*o/256/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,i));var o,n,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new u(e,r)}}};function E(t,e,r,i){return new(r||(r=Promise))((function(o,n){function a(t){try{u(i.next(t))}catch(t){n(t)}}function s(t){try{u(i.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((i=i.apply(t,e||[])).next())}))}function v(t,e,r){r=r||2;var i,o,n,a,s,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=T(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(l&&(f=function(t,e,r,i){var o,n,a,s=[];for(o=0,n=e.length;o80*r){i=n=t[0],o=a=t[1];for(var m=r;mn&&(n=s),u>a&&(a=u);h=0!==(h=Math.max(n-i,a-o))?32767/h:0}return b(f,d,r,i,o,h,0),d}function T(t,e,r,i,o){var n,a;if(o===Z(t,e,r,i)>0)for(n=e;n=e;n-=i)a=H(n,t[n],t[n+1],a);return a&&B(a,a.next)&&(W(a),a=a.next),a}function y(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!B(i,i.next)&&0!==I(i.prev,i,i.next))i=i.next;else{if(W(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function b(t,e,r,i,o,n,a){if(t){!a&&n&&function(t,e,r,i){var o=t;do{0===o.z&&(o.z=D(o.x,o.y,e,r,i)),o.prevZ=o.prev,o.nextZ=o.next,o=o.next}while(o!==t);o.prevZ.nextZ=null,o.prevZ=null,function(t){var e,r,i,o,n,a,s,u,h=1;do{for(r=t,t=null,n=null,a=0;r;){for(a++,i=r,s=0,e=0;e0||u>0&&i;)0!==s&&(0===u||!i||r.z<=i.z)?(o=r,r=r.nextZ,s--):(o=i,i=i.nextZ,u--),n?n.nextZ=o:t=o,o.prevZ=n,n=o;r=i}n.nextZ=null,h*=2}while(a>1)}(o)}(t,i,o,n);for(var s,u,h=t;t.prev!==t.next;)if(s=t.prev,u=t.next,n?A(t,i,o,n):R(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),W(t),t=u.next,h=u.next;else if((t=u)===h){a?1===a?b(t=M(y(t),e,r),e,r,i,o,n,2):2===a&&w(t,e,r,i,o,n):b(y(t),e,r,i,o,n,1);break}}}function R(t){var e=t.prev,r=t,i=t.next;if(I(e,r,i)>=0)return!1;for(var o=e.x,n=r.x,a=i.x,s=e.y,u=r.y,h=i.y,l=on?o>a?o:a:n>a?n:a,d=s>u?s>h?s:h:u>h?u:h,m=i.next;m!==e;){if(m.x>=l&&m.x<=f&&m.y>=c&&m.y<=d&&P(o,s,n,u,a,h,m.x,m.y)&&I(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function A(t,e,r,i){var o=t.prev,n=t,a=t.next;if(I(o,n,a)>=0)return!1;for(var s=o.x,u=n.x,h=a.x,l=o.y,c=n.y,f=a.y,d=su?s>h?s:h:u>h?u:h,g=l>c?l>f?l:f:c>f?c:f,x=D(d,m,e,r,i),p=D(_,g,e,r,i),E=t.prevZ,v=t.nextZ;E&&E.z>=x&&v&&v.z<=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==o&&E!==a&&P(s,l,u,c,h,f,E.x,E.y)&&I(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==o&&v!==a&&P(s,l,u,c,h,f,v.x,v.y)&&I(v.prev,v,v.next)>=0)return!1;v=v.nextZ}for(;E&&E.z>=x;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==o&&E!==a&&P(s,l,u,c,h,f,E.x,E.y)&&I(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;v&&v.z<=p;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==o&&v!==a&&P(s,l,u,c,h,f,v.x,v.y)&&I(v.prev,v,v.next)>=0)return!1;v=v.nextZ}return!0}function M(t,e,r){var i=t;do{var o=i.prev,n=i.next.next;!B(o,n)&&N(o,i,i.next,n)&&X(o,n)&&X(n,o)&&(e.push(o.i/r|0),e.push(i.i/r|0),e.push(n.i/r|0),W(i),W(i.next),i=t=n),i=i.next}while(i!==t);return y(i)}function w(t,e,r,i,o,n){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&C(a,s)){var u=G(a,s);return a=y(a,a.next),u=y(u,u.next),b(a,e,r,i,o,n,0),void b(u,e,r,i,o,n,0)}s=s.next}a=a.next}while(a!==t)}function F(t,e){return t.x-e.x}function L(t,e){var r=function(t,e){var r,i=e,o=t.x,n=t.y,a=-1/0;do{if(n<=i.y&&n>=i.next.y&&i.next.y!==i.y){var s=i.x+(n-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(s<=o&&s>a&&(a=s,r=i.x=i.x&&i.x>=l&&o!==i.x&&P(nr.x||i.x===r.x&&U(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=G(r,t);return y(i,i.next),y(r,r.next)}function U(t,e){return I(t.prev,t,e.prev)<0&&I(e.next,t,t.next)<0}function D(t,e,r,i,o){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*o|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*o|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function S(t){var e=t,r=t;do{(e.x=(t-a)*(n-s)&&(t-a)*(i-s)>=(r-a)*(e-s)&&(r-a)*(n-s)>=(o-a)*(i-s)}function C(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&N(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(X(t,e)&&X(e,t)&&function(t,e){var r=t,i=!1,o=(t.x+e.x)/2,n=(t.y+e.y)/2;do{r.y>n!=r.next.y>n&&r.next.y!==r.y&&o<(r.next.x-r.x)*(n-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(I(t.prev,t,e.prev)||I(t,e.prev,e))||B(t,e)&&I(t.prev,t,t.next)>0&&I(e.prev,e,e.next)>0)}function I(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function B(t,e){return t.x===e.x&&t.y===e.y}function N(t,e,r,i){var o=z(I(t,e,r)),n=z(I(t,e,i)),a=z(I(r,i,t)),s=z(I(r,i,e));return o!==n&&a!==s||(!(0!==o||!O(t,r,e))||(!(0!==n||!O(t,i,e))||(!(0!==a||!O(r,t,i))||!(0!==s||!O(r,e,i)))))}function O(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function z(t){return t>0?1:t<0?-1:0}function X(t,e){return I(t.prev,t,t.next)<0?I(t,e,t.next)>=0&&I(t,t.prev,e)>=0:I(t,e,t.prev)<0||I(t,t.next,e)<0}function G(t,e){var r=new j(t.i,t.x,t.y),i=new j(e.i,e.x,e.y),o=t.next,n=e.prev;return t.next=e,e.prev=t,r.next=o,o.prev=r,i.next=r,r.prev=i,n.next=i,i.prev=n,i}function H(t,e,r,i){var o=new j(t,e,r);return i?(o.next=i.next,o.prev=i,i.next.prev=o,i.next=o):(o.prev=o,o.next=o),o}function W(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function j(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function Z(t,e,r,i){for(var o=0,n=e,a=r-i;n"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:o}=e;if("MultiPolygon"===r.type)for(let i=0;i"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,i=v.flatten(e.coordinates),o=new Float32Array(i.vertices.map(((t,e)=>{return e%2==1?(r=t,(180-180/Math.PI*Math.log(Math.tan(Math.PI/4+r*Math.PI/360)))/360):(180+t)/360;var r}))),n=v(i.vertices,i.holes,i.dimensions),a=n.length>256?new Uint16Array(n):new Uint8Array(n),s=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),u=r.highlight||!1;let h=0,l=0,c=0;for(let t=0;t0&&(i+=t[o-1].length,r.holes.push(i))}return r};const K=(t,e)=>{const{lat:r,lng:o}=t;return new i(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(o,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},k=(t,e)=>{return new c((r=t.y,i=e,o=Math.PI-2*Math.PI*r/256/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(o)-Math.exp(-o)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,i,o};let V,q={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new a(new i(0,0),new i(0,0)),DEMPixelBounds:new a(new i(0,0),new i(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const $=t=>E(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:o,dsmSource:n,tileLoaded:s,gl:h,bounds:l,buildingRasterizer:c,tileMerger:f,forceUpdate:d=!1}=t,{getSourceUrl:m,getElevation:_,maxZoom:g,tileSize:x,_overzoom:p}=o,v=(T={getFeatures:r},E(void 0,void 0,void 0,(function*(){const{getFeatures:t}=T;try{return Y(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var T;try{const t=l,r=t.getNorthWest(),o=t.getSouthEast(),p=new a(K(r,e),K(o,e));let E=new i(p.min.x,p.min.y),T=p.max.x-p.min.x;const y=p.max.y-p.min.y,b=p.max.subtract(p.min);b.y>b.x&&(E.x-=256,T+=512);const R=(t=>{const{upperLeft:e,width:r,height:i}=t,o=e.divideBy(256).floor().multiplyBy(256),n=256*(Math.ceil(r/256)+1),s=256*(Math.ceil(i/256)+1),u=o.add([n,s]);return new a(o,u)})({upperLeft:E,width:T,height:y}),{x:A,y:M}=R.max.subtract(R.min),w=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.xe.y{const i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:o}=t,n=e.divideBy(256).floor(),a=n.x+r/256,s=Math.min(n.y+i/256,Math.pow(2,o)-1),u=[];for(var h=n.x;h2*t-1)),O=yield v,{maxHeight:z,heightMapTex:X}=c.raster({upperLeftTile:w[0],width:A,height:M,mapZoom:e,features:O,imageData:P,gl:h,dsmSource:n,dsmCoords:N}),G=Math.max(n.maxHeight,P.maxHeight+z);V=S,q={heightMapTex:X,maxHeight:G,width:A,height:M,DEMPixelBounds:R,visibleDEMPixelBounds:p,raster:w,demZoom:e,dirty:!0,outputWidth:U,outputHeight:D}}catch(t){console.error("Could not decode height map",t)}return q})),J=(t,e)=>{const{r:r,g:i,b:o}=t;return[r/255,i/255,o/255,e]},Q=(t,e)=>{const{date:r}=e,{dec:i,Hi:o}=d(r);t.updateDate({dec:i,Hi:o})},tt=(t,e)=>E(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),et=(t,e)=>{const{color:r,opacity:i}=e,o=J(r,i);t.updateColor({colorVec:o})},rt=t=>{const{kernel:e,map:r,heightMap:i,now:o,color:n,opacity:a,belowCanopy:s,skipRender:h}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:c,maxHeight:f,width:m,height:_,DEMPixelBounds:g,visibleDEMPixelBounds:x,demZoom:p}=i;if(0===m||0===_)return;const{min:E,max:v}=x;if(!E||!v)return;const{x:T,y:y}=v.subtract(E),b=g.min,R=r.getPixelDimensions(),A=r.screenUnproject([0,0]),M=r.screenUnproject([R.width,0]),w=r.screenUnproject([R.width,R.height]),F=r.screenUnproject([0,R.height]),L=[F,w,A,M].map((t=>r.project(t,p))),U=L.map((t=>[(t.x-b.x)/m,(t.y-b.y)/_])).flat(),D=L.map((t=>[(t.x-E.x)/T,(t.y-E.y)/y])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),S=b.y/256/Math.pow(2,p),P=_/256/Math.pow(2,p),C=new u(k(g.getTopLeft(),p),k(g.getBottomRight(),p)),I=C.getWest(),B=Math.abs(C.getWest()-C.getEast());((t,e)=>{const{color:r,opacity:i,date:o}=e,n=J(r,i),{dec:a,Hi:s}=d(o);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:f,width:m,height:_,heightMapZoom:p,cornerTextureCoords:U,cornerClipCoords:D,topYCoord:S,ySize:P,west:I,dLng:B,date:o,color:n,opacity:a,outputHeight:l,outputWidth:c,belowCanopy:s,skipRender:h})}catch(t){console.error("EXCEPTION",t)}};const it=t=>{const{gl:e}=t,{texture:r,imageData:i=null,format:o=e.RGBA,width:n,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:h=e.UNSIGNED_BYTE,internalFormat:l=o}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,l,n,a,0,o,h,i)};function ot(t){const{gl:e,vSrc:r,fSrc:i}=t,o=e.createShader(e.VERTEX_SHADER);e.shaderSource(o,r),e.compileShader(o);const n=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(n,i),e.compileShader(n);const a=e.createProgram();return e.attachShader(a,o),e.attachShader(a,n),e.linkProgram(a),a}class nt{constructor(t){this.gl=t,this.program=ot({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width:n,height:a,imageData:s,dsmCoords:u,dsmSource:h,gl:l}=t,{x:c,y:f,z:d}=r;let m=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1),l.bindTexture(l.TEXTURE_2D,s);const _=n,g=a;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,_,g,0,l.RGBA,l.UNSIGNED_BYTE,null),o>15&&e.length>5?(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST)):(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.LINEAR),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.LINEAR)),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.viewport(0,0,_,g);const x=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,x);const p=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,p,l.TEXTURE_2D,this.targetTexture,0),l.disable(l.BLEND),l.bindBuffer(l.ARRAY_BUFFER,this.positionBuffer),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,this.indexBuffer),l.clearColor(0,0,0,0),l.clear(l.COLOR_BUFFER_BIT);const E=c/Math.pow(2,d),v=f/Math.pow(2,d),T=new i(E,v),y=n/256/Math.pow(2,d),b=a/256/Math.pow(2,d);l.uniform3f(this.xyzUniformLocation,E,v,d),l.uniform2f(this.dimensionsUniformLocation,y,b),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const R=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,R,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.drawArrays(l.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:s,highlight:u}=t,h=new i(s[0],s[1]).subtract(T).unscaleBy(new i(y,b)).scaleBy(new i(n,a)).floor();if(h.x<0||h.y<0||h.x>n||h.y>a)return;const c=h.x/n,f=h.y/a,d=5*e,_=Math.floor(d/255)/255,g=Math.floor(d%255)/255;m=Math.max(m,e),l.uniform2f(this.centroidUniformLocation,c,f),l.uniform4f(this.colorUniformLocation,_,g,_,g),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ELEMENT_ARRAY_BUFFER,o,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,o.length,o.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),0!==h.data.length){l.activeTexture(l.TEXTURE1);const t=l.createTexture();l.bindTexture(l.TEXTURE_2D,t),l.pixelStorei(l.UNPACK_ALIGNMENT,2),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST),l.texImage2D(l.TEXTURE_2D,0,l.LUMINANCE_ALPHA,h.width,h.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,h.data),l.pixelStorei(l.UNPACK_ALIGNMENT,4);const e=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],r=new Float32Array(e);l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,r,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.enableVertexAttribArray(this.dsmAttributeLocation),l.bindBuffer(l.ARRAY_BUFFER,this.dsmBuffer),l.bufferData(l.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),l.STATIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.uniform1i(this.useDSMUniformLocation,1),l.colorMask(!1,!1,!0,!0),l.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.colorMask(!0,!0,!0,!0),l.deleteFramebuffer(x),{maxHeight:m,heightMapTex:this.targetTexture}}}const at=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:o,y:n,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);o=Math.floor(o/t),n=Math.floor(n/t),a=r}return{x:256===i?o:Math.floor(o/2),y:256===i?n:Math.floor(n/2),z:256===i?a:a-1}};class st{constructor(t){this.gl=t,this.program=ot({vSrc:"\n attribute vec4 a_tex_position;\n attribute vec4 a_tile_position;\n varying vec2 v_tex_pos;\n void main() {\n v_tex_pos = a_tex_position.xy;\n gl_Position = a_tile_position;\n }\n ",fSrc:"\n precision mediump float;\n uniform sampler2D u_texture;\n uniform float u_encoding;\n uniform float u_tile_size;\n varying vec2 v_tex_pos;\n\n float getElevationTerrarium(vec4 color) {\n float decoded = color.r * 255.0 * 256.0 + color.g * 255.0 + color.b * 255.0 / 256.0 - 32768.0;\n return decoded;\n }\n\n float getElevationMapbox(vec4 color) {\n float decoded = -10000.0 + (color.r * 255.0 * 256.0 * 256.0 + color.g * 255.0 * 256.0 + color.b * 255.0) * .1;\n return decoded;\n }\n\n float getElevationSwitzerland(vec4 color) {\n float decoded = ((color.r * 255.0 * 256.0 + color.g * 255.0) / pow(2.0, 16.0)) * 4808.0;\n return decoded;\n }\n\n float getElevation(vec4 color, float encoding) {\n float decoded;\n if (encoding == 1.0) {\n decoded = getElevationMapbox(color);\n } else if (encoding == 2.0) {\n decoded = getElevationSwitzerland(color);\n } else {\n decoded = getElevationTerrarium(color);\n }\n float scaled = decoded * 5.0;\n float elevation = max(scaled, 0.0);\n return elevation;\n }\n\n float textureSampleLinear(sampler2D sampler, vec2 texCoord) {\n vec2 texelSize = vec2(1.0 / u_tile_size, 1.0 / u_tile_size);\n \n // Calculate interpolation weights based on texCoord fract (fractional part)\n vec2 uv = fract(texCoord * u_tile_size);\n \n // Sample four nearest texels\n vec4 bottomLeft = texture2D(sampler, texCoord);\n vec4 bottomRight = texture2D(sampler, texCoord + vec2(texelSize.x, 0.0));\n vec4 topLeft = texture2D(sampler, texCoord + vec2(0.0, texelSize.y));\n vec4 topRight = texture2D(sampler, texCoord + texelSize);\n\n float bottomLeftEl = getElevation(bottomLeft, u_encoding);\n float bottomRightEl = getElevation(bottomRight, u_encoding);\n float topLeftEl = getElevation(topLeft, u_encoding);\n float topRightEl = getElevation(topRight, u_encoding);\n \n // Interpolate between bottom and top rows\n float bottom = mix(bottomLeftEl, bottomRightEl, uv.x);\n float top = mix(topLeftEl, topRightEl, uv.x);\n \n // Interpolate final color\n return mix(bottom, top, uv.y);\n }\n\n void main() {\n float elevation = textureSampleLinear(u_texture, v_tex_pos);\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n gl_FragColor = vec4(0, 0, r, g);\n\n // vec4 color = texture2D(u_texture, v_tex_pos);\n // float elevation = getElevation(color, u_encoding);\n // float r = floor(elevation / 255.0) / 255.0;\n // float g = floor(mod(elevation, 255.0)) / 255.0;\n // gl_FragColor = vec4(0, 0, r, g);\n // gl_FragColor = color;\n }\n ",gl:t}),t.useProgram(this.program),this.texPositionAttributeLocation=t.getAttribLocation(this.program,"a_tex_position"),this.tilePositionAttributeLocation=t.getAttribLocation(this.program,"a_tile_position"),this.encodingUniformLocation=t.getUniformLocation(this.program,"u_encoding"),this.tileSizeUniformLocation=t.getUniformLocation(this.program,"u_tile_size"),this.texPositionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.texPositionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.texPositionAttributeLocation),t.vertexAttribPointer(this.texPositionAttributeLocation,2,t.FLOAT,!1,0,0),this.tilePositionBuffer=t.createBuffer(),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return E(this,void 0,void 0,(function*(){const r=this.gl;let i=0;const{width:o,height:n,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:h,tileLoaded:l,maxZoom:c}=e;if(r){this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const e=new Set;t.forEach((t=>{e.add(s(at({tile:t,maxZoom:c,tileSize:h})))})),r.useProgram(this.program);let f=0;-32768===u({r:0,g:0,b:0,a:0})&&(f=0),-1e4===u({r:0,g:0,b:0,a:0})&&(f=1),4808===u({r:256,g:0,b:0,a:0})&&(f=2),r.uniform1f(this.encodingUniformLocation,f),it({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:o,height:n,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST}),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const d=r.checkFramebufferStatus(r.FRAMEBUFFER);d!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+d),r.clear(r.COLOR_BUFFER_BIT);const m=Array.from(e).map((e=>E(this,void 0,void 0,(function*(){return new Promise(((u,l)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{r.useProgram(this.program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.tileTexture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,f),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer);const a=t.filter((t=>e===s(at({tile:t,maxZoom:c,tileSize:h})))),l=a.reduce(((t,e)=>Math.min(t,e.xOffset)),1/0),d=a.reduce(((t,e)=>Math.min(t,e.yOffset)),1/0),m=a.reduce(((t,e)=>Math.max(t,e.xOffset)),0),_=a.reduce(((t,e)=>Math.max(t,e.yOffset)),0),g=a.reduce(((t,e)=>Math.min(t,e.x)),1/0),x=a.reduce(((t,e)=>Math.min(t,e.y)),1/0),p=a.reduce(((t,e)=>Math.max(t,e.x)),0),E=a.reduce(((t,e)=>Math.max(t,e.y)),0),v=a[0].z,T=l/o,y=d/n,b=(m+256)/o,R=(_+256)/n,A=[T,y,b,y,T,R,b,R].map((t=>2*t-1));r.bindBuffer(r.ARRAY_BUFFER,this.tilePositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(A),r.STATIC_DRAW),r.enableVertexAttribArray(this.tilePositionAttributeLocation),r.vertexAttribPointer(this.tilePositionAttributeLocation,2,r.FLOAT,!1,0,0);let M=[0,0,1,0,0,1,1,1];const w=Math.pow(2,Math.max(0,v-c)+(256!==h?1:0)),F=514===h?1/h:0,L=1/w,U=g%w/w,D=x%w/w,S=p%w/w,P=E%w/w;M=[U,D,S+L,D,U,P+L,S+L,P+L].map((t=>t*(514===h?512/514:1)+F)),r.bindBuffer(r.ARRAY_BUFFER,this.texPositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(M),r.STATIC_DRAW),r.enableVertexAttribArray(this.texPositionAttributeLocation),r.vertexAttribPointer(this.texPositionAttributeLocation,2,r.FLOAT,!1,0,0),r.uniform1f(this.tileSizeUniformLocation,h),r.viewport(0,0,o,n),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const C=Math.floor(T*o),I=Math.floor(y*n),B=Math.ceil((b-T)*o),N=Math.ceil((R-y)*n),O=new Uint8Array(4*B*N);r.readPixels(C,I,B,N,r.RGBA,r.UNSIGNED_BYTE,O);for(let t=0;t{if(f.src!==f.originalSource)return l("new tiles requested");u(null)},f.crossOrigin=a||null,f.src=e,f.originalSource=f.src})).then((()=>{this.finished++,l(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(m)}catch(t){return console.log(`${m.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=i,this.outputTexture}throw new Error("Could not get canvas context for merging tile images")}))}}class ut extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>E(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>E(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(Q(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=x(this.options.date)-x(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(et(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(et(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return E(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:i=new Date,iterations:o=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:o}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)Q(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime(){const e=K(t,r);return[(e.x-n.x)/i,(e.y-n.y)/o]}))}return[]}_getBounds(t,e){const{width:r,height:o}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(o))return t.getBounds();const n=t.getCenter(),a=K(n,e),s=k(new i(a.x-r/2,a.y-o/2),e),u=k(new i(a.x+r/2,a.y+o/2),e);return t.createBounds({nw:s,se:u})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return E(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=p(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),o=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===o)).map((t=>t.canonical)),[a,s,u,h]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),l=k(new i(512*a,512*s),o+1),c=k(new i(512*(u+1),512*(h+1)),o+1);e=o+1,this._bounds=t.createBounds({nw:l,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield $({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return E(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(rt({kernel:this._compiledKernel,map:p(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield tt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,i){const o=new Uint8Array(r*i*4);return this._gl&&this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,o),o}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),i=t*e*1,o=1*t,n=(e-1)*o,a=new Uint8Array(i),s=new Uint8Array(i);let u=0;const{startDate:h,endDate:l}=this.options.sunExposure;this.options.sunExposure.enabled&&h&&l&&(u=l.getTime()-h.getTime());for(let t=0;t{const r=J({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=J({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=x(t.startDate)-x(t.startDate,t.tzId),r=t.startDate.getTime()-e,i=t.endDate.getTime()-e,o=this._lngLatToTextureCoords([t.location])[0],{output:n,outputWidth:a,outputHeight:s}=((t,e)=>{const r=J({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=J({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:i,texCoord:o}));return n.toArray=function(){const e=new Array;for(let r=0;r=0;e--){const o=e*a*4+4*r,n=this.slice(o,o+4);i.push(n.join("")===t.sunColor.join("")?1:0)}e.push(i)}return e},{data:n,width:a,height:s}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:i,endDate:o}=this.options.sunExposure,n=o.getTime()-i.getTime(),a=g(r,.5,n);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t),this.id="shademap-layer",this.type="custom",this.canvasSourceId="canvas-source",this.attributionSourceId="attribution-source",this.canvasLayerId="canvas-layer",this.attributionLayerId="attribution-layer",this._refreshing=0,this._raf=0,this.id=this.id+_(),this.canvasSourceId=this.canvasSourceId+_(),this.attributionSourceId=this.attributionSourceId+_(),this.canvasLayerId=this.canvasLayerId+_(),this.attributionLayerId=this.attributionLayerId+_(),this.options.terrainSource.tileSize>256&&(this.options.terrainSource.maxZoom=this.options.terrainSource.maxZoom+1),this._moveEndHandler=()=>{this._map&&this._reset()}}render(t,e){}addTo(t){return t.addLayer(this),this}onAdd(t){this._map=t,this._gl=this._map.painter.context.gl,this._framebuffer=this._gl.createFramebuffer();this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,i=e,o=ot({gl:i,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_outputSunExposure;uniform bool u_outputShadeProfile;uniform sampler2D u_decHiTexture;uniform sampler2D u_gpxTexture;uniform bool u_outputLocationShadeProfile;uniform vec2 u_shadeProfileLocation;float kernelResult;void kernel(){float sunDec=user_dec;float sunHi=user_Hi;vec4 shade_color=user_color;float maxHeight=user_maxHeight/1000.0;float user_x=vTexCoordCropped.x;float user_y=vTexCoordCropped.y;if(u_outputShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;vec4 gpx=texture2D(u_gpxTexture,vec2(vTexCoordFull.x,.5));user_x=gpx.x;user_y=gpx.y;}if(u_outputLocationShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;user_x=u_shadeProfileLocation.x;user_y=u_shadeProfileLocation.y;}float user_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitudeiter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_outputSunExposure){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));float timeInSun=1.0-aggregateColor.r;float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,0.5);}}void main(void){kernel();gl_FragColor=actualColor;}"});i.useProgram(o);const n=i.createBuffer(),a=i.getAttribLocation(o,"a_pos"),s=i.createBuffer(),u=i.getAttribLocation(o,"a_tex_pos"),h=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,h),i.bufferData(i.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),i.STATIC_DRAW);const l=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,l),i.bufferData(i.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),i.STATIC_DRAW);const c=i.getUniformLocation(o,"user_a");i.uniform1i(c,0);const f=i.getUniformLocation(o,"user_width"),_=i.getUniformLocation(o,"user_height"),g=i.getUniformLocation(o,"user_maxHeight"),p=i.getUniformLocation(o,"user_zoom"),v=i.getUniformLocation(o,"user_topYCoord"),T=i.getUniformLocation(o,"user_ySize"),y=i.getUniformLocation(o,"user_step"),b=i.getUniformLocation(o,"user_west"),R=i.getUniformLocation(o,"user_dLng"),A=i.getUniformLocation(o,"user_dec"),M=i.getUniformLocation(o,"user_Hi"),w=i.getUniformLocation(o,"user_color"),F=i.getUniformLocation(o,"u_below_canopy"),L=i.getUniformLocation(o,"user_sunExposureTexture"),U=i.getUniformLocation(o,"user_outputSunExposure"),D=i.getUniformLocation(o,"u_outputShadeProfile"),S=i.getUniformLocation(o,"u_gpxTexture"),P=i.getUniformLocation(o,"u_decHiTexture"),C=i.getUniformLocation(o,"u_sunColor"),I=i.getUniformLocation(o,"u_outputLocationShadeProfile"),B=i.getUniformLocation(o,"u_shadeProfileLocation"),N=i.createTexture();let O=0,z=0,X=0,G=null;const H=()=>{z&&X&&(i.useProgram(o),i.bindBuffer(i.ARRAY_BUFFER,n),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,s),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),r(i,z,X),i.viewport(0,0,z,X),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4))};let W=0,j=0,Z=0,Y="";const K=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(i.activeTexture(i.TEXTURE3),i.uniform1i(P,3),t.startTime!==j||t.endTime!==Z||t.tzId!==Y){j=t.startTime,Z=t.endTime,Y=t.tzId;const o=[],n=828e5,a=x(new Date(j),Y);for(let t=0;t{const{dec:e,Hi:r}=t;i.useProgram(o),i.uniform1f(A,e),i.uniform1f(M,r),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},updateDateRange:t=>E(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:h,emit:l}=t;i.useProgram(o);const c=W=Date.now(),f=Math.floor((r.getTime()-e.getTime())/h),m=i.getUniform(o,w),_=z,g=X,x=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,_,g,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST);const p=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,p);const E=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,E,i.TEXTURE_2D,x,0);for(let t=0;t{window.requestAnimationFrame((()=>{i.useProgram(o),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindFramebuffer(i.FRAMEBUFFER,p);const l=i.checkFramebufferStatus(i.FRAMEBUFFER);l!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+l);const{dec:c,Hi:m}=d(new Date(e.getTime()+f*t));i.uniform1f(A,c),i.uniform1f(M,m),i.uniform4fv(w,[1/h,0,0,1]),i.bindBuffer(i.ARRAY_BUFFER,n),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,s),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,_,g),i.enable(i.BLEND),i.blendFunc(i.ONE,i.ONE),i.drawArrays(i.TRIANGLE_STRIP,0,4),r()}))}))}return i.deleteFramebuffer(p),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(i.useProgram(o),c!==W)return i.deleteTexture(x),i.uniform4fv(w,m),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.uniform1i(L,2),i.uniform1i(U,1),H(),i.uniform1i(U,0),i.uniform1i(L,0),i.deleteTexture(x),i.uniform4fv(w,m),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;i.useProgram(o),i.uniform4fv(w,e),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},generateShadeProfile:t=>{i.useProgram(o);const{dates:e,texCoords:r,shadeColor:n,sunColor:s}=t,c=r.length,f=e.length,m=i.getUniform(o,w);i.uniform4fv(w,n),i.uniform4fv(C,s),i.uniform1i(D,1);const _=i.createTexture();i.activeTexture(i.TEXTURE3),it({gl:i,imageData:null,width:c,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:_});const g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,_,0);const x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const p=r.map((t=>[t[0],t[1]])).flat(),E=i.createTexture();i.activeTexture(i.TEXTURE2),it({gl:i,texture:E,imageData:new Float32Array(p),width:p.length/2,height:1,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT}),i.uniform1i(S,2);const v=e.map((t=>{const{dec:e,Hi:r}=d(t);return[-e,r/10]})).flat(),T=i.createTexture();i.activeTexture(i.TEXTURE1),it({gl:i,texture:T,imageData:new Float32Array(v),width:1,height:e.length,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT}),i.uniform1i(P,1),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,c,f),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const y=new Uint8Array(c*f*4);return i.readPixels(0,0,c,f,i.RGBA,i.UNSIGNED_BYTE,y),i.deleteTexture(_),i.deleteTexture(T),i.deleteTexture(E),i.deleteFramebuffer(g),i.uniform1i(P,0),i.uniform1i(S,0),i.uniform1i(D,0),i.uniform4fv(w,m),i.uniform4fv(C,[0,0,0,0]),y},generateLocationShadeProfile:t=>{i.useProgram(o);const{startTime:e,endTime:r,tzId:n,texCoord:s,shadeColor:c,sunColor:f}=t,d=i.getUniform(o,w),{outputWidth:m,outputHeight:_}=K({startTime:e,endTime:r,tzId:n});i.uniform4fv(w,c),i.uniform4fv(C,f),i.uniform1i(I,1),i.uniform2fv(B,[s[0],s[1]]);const g=i.createTexture();i.activeTexture(i.TEXTURE2),it({gl:i,imageData:null,width:m,height:_,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:g});const x=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,x),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,g,0);const p=i.checkFramebufferStatus(i.FRAMEBUFFER);p!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,m,_),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const E=new Uint8Array(m*_*4);return i.readPixels(0,0,m,_,i.RGBA,i.UNSIGNED_BYTE,E),i.deleteTexture(g),i.deleteFramebuffer(x),i.uniform1i(P,0),i.uniform1i(I,0),i.uniform4fv(w,d),i.uniform4fv(C,[0,0,0,0]),{output:E,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:(e,r,i)=>{const o=t.getSource(this.canvasSourceId).texture;e.activeTexture(e.TEXTURE1),o.bind(e.LINEAR,e.CLAMP_TO_EDGE),o.size=[r,i],e.texImage2D(e.TEXTURE_2D,0,e.RGBA,r,i,0,e.RGBA,e.UNSIGNED_BYTE,null),e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,o.texture,0),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA)}}),this._tileMerger=new st(this._gl),this._buildingRasterizer=new nt(this._gl),document.body.appendChild(this._canvas),this._canvas.style.display="none";const e=t.getBounds(),r=e.getNorthWest(),i=e.getNorthEast(),o=e.getSouthEast(),n=e.getSouthWest(),a=[[r.lng,r.lat],[i.lng,i.lat],[o.lng,o.lat],[n.lng,n.lat]];return t.addSource(this.canvasSourceId,{type:"canvas",canvas:this._canvas,coordinates:a,animate:!1}),t.addLayer({id:this.canvasLayerId,type:"raster",source:this.canvasSourceId,paint:{"raster-fade-duration":0}}),t.addSource(this.attributionSourceId,{type:"geojson",data:{type:"Feature",properties:{},geometry:{type:"Point",coordinates:[-122.210598,47.769799]}},attribution:'© ShadeMap'}),t.addLayer({id:this.attributionLayerId,type:"fill",source:this.attributionSourceId}),this._map.on("moveend",this._moveEndHandler),this._moveEndHandler(),this}onRemove(){return this._map&&(this._map.off("moveend",this._moveEndHandler),this._map.removeLayer(this.attributionLayerId),this._map.removeLayer(this.canvasLayerId),this._map.removeSource(this.attributionSourceId),this._map.removeSource(this.canvasSourceId)),this._gl&&this._framebuffer&&this._gl.deleteFramebuffer(this._framebuffer),document.body.removeChild(this._canvas),this.options.debug("onRemove called"),this}_getHeightMapCoords(t,e){if(this._map&&this.options.sunExposure.enabled&&this._bounds&&this._heightMap){const r=this._map.unproject([t,e]);if(r.toString()===this._map.unproject([t,e+1]).toString())return new i(-1,-1);const{visibleDEMPixelBounds:o,demZoom:n}=this._heightMap;return p(this._map).project(r,n).subtract(o.min)}return new i(-1,-1)}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this._getHeightMapCoords(t,e),i=this.readPixel(r.x,r.y),{startDate:o,endDate:n}=this.options.sunExposure,a=n.getTime()-o.getTime(),s=g(i,.5,a);return Math.abs(s/1e3/3600)}return 0}remove(){this._map&&this._map.removeLayer(this.id)}readPixel(t,e){const r=new Uint8Array(4);if(this._map&&this._gl&&this._framebuffer){const i=this._gl,o=this._map.getSource(this.canvasSourceId).texture;i.activeTexture(i.TEXTURE1),o.bind(i.LINEAR,i.CLAMP_TO_EDGE),i.bindFramebuffer(i.FRAMEBUFFER,this._framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,o.texture,0),this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r)}return r}readPixels(t,e,r,i){const o=new Uint8Array(r*i*4);if(this._map&&this._gl&&this._framebuffer){const n=this._gl,a=this._map.getSource(this.canvasSourceId).texture;n.activeTexture(n.TEXTURE1),a.bind(n.LINEAR,n.CLAMP_TO_EDGE),n.bindFramebuffer(n.FRAMEBUFFER,this._framebuffer),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,a.texture,0),this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,o)}return o}_flush(){if(this._map){this._map.getSource(this.canvasSourceId).fire({type:"data",dataType:"source",sourceDataType:"content"})}this.emit("idle")}_repositionCanvas(t){if(this._map){const e=this._map.getSource(this.canvasSourceId);if(e){const r=t.getNorthWest(),i=t.getNorthEast(),o=t.getSouthEast(),n=t.getSouthWest(),a=[[r.lng,r.lat],[i.lng,i.lat],[o.lng,o.lat],[n.lng,n.lat]];e.setCoordinates(a),this.emit("idle")}}return this}}export{ut as default}; +function t(t,e,r){var o=e[1],i=e[0],n=o-i;return t===o&&r?t:((t-i)%n+n)%n+i}function e(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var r=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function o(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var i=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function n(t,e,i){return t instanceof o?t:r(t)?new o(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new o(t.x,t.y):new o(t,e,i)}function a(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=s(t);var e=this.min,r=this.max,o=t.min,i=t.max,n=i.x>=e.x&&o.x<=r.x,a=i.y>=e.y&&o.y<=r.y;return n&&a},overlaps:function(t){t=s(t);var e=this.min,r=this.max,o=t.min,i=t.max,n=i.x>e.x&&o.xe.y&&o.y=o.lat&&r.lat<=i.lat&&e.lng>=o.lng&&r.lng<=i.lng},intersects:function(t){t=l(t);var e=this._southWest,r=this._northEast,o=t.getSouthWest(),i=t.getNorthEast(),n=i.lat>=e.lat&&o.lat<=r.lat,a=i.lng>=e.lng&&o.lng<=r.lng;return n&&a},overlaps:function(t){t=l(t);var e=this._southWest,r=this._northEast,o=t.getSouthWest(),i=t.getNorthEast(),n=i.lat>e.lat&&o.late.lng&&o.lng{const e=t.valueOf();return m(e)},m=t=>{const e=t/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,o=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,i=Math.atan2(Math.sin(o)*Math.cos(.40909994067971484),Math.cos(o));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},_=()=>Math.floor(1e7*Math.random()),g=(t,e,r)=>{const o=1/e,i=Math.min(t[0]*o,255),n=Math.min(t[1]*o,255),a=Math.min(t[2]*o,255);let s=0;return i+n+a!==0&&(s=i>0?i/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},p=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),o=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-o.getTime()};const x=t=>{const e=()=>!t.getPitch;return{project:(r,i)=>{if(e())return t.project(r,i);{const{lat:t,lng:e}=r;return new o(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,i),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,i))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new c((i=r.y,n=o,a=Math.PI-2*Math.PI*i/256/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,o));var i,n,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new u(e,r)}}};function E(t,e,r,o){return new(r||(r=Promise))((function(i,n){function a(t){try{u(o.next(t))}catch(t){n(t)}}function s(t){try{u(o.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((o=o.apply(t,e||[])).next())}))}function v(t,e,r){r=r||2;var o,i,n,a,s,u,l,h=e&&e.length,c=h?e[0]*r:t.length,f=T(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(h&&(f=function(t,e,r,o){var i,n,a,s=[];for(i=0,n=e.length;i80*r){o=n=t[0],i=a=t[1];for(var m=r;mn&&(n=s),u>a&&(a=u);l=0!==(l=Math.max(n-o,a-i))?32767/l:0}return b(f,d,r,o,i,l,0),d}function T(t,e,r,o,i){var n,a;if(i===Z(t,e,r,o)>0)for(n=e;n=e;n-=o)a=H(n,t[n],t[n+1],a);return a&&B(a,a.next)&&(W(a),a=a.next),a}function y(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!B(o,o.next)&&0!==I(o.prev,o,o.next))o=o.next;else{if(W(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function b(t,e,r,o,i,n,a){if(t){!a&&n&&function(t,e,r,o){var i=t;do{0===i.z&&(i.z=D(i.x,i.y,e,r,o)),i.prevZ=i.prev,i.nextZ=i.next,i=i.next}while(i!==t);i.prevZ.nextZ=null,i.prevZ=null,function(t){var e,r,o,i,n,a,s,u,l=1;do{for(r=t,t=null,n=null,a=0;r;){for(a++,o=r,s=0,e=0;e0||u>0&&o;)0!==s&&(0===u||!o||r.z<=o.z)?(i=r,r=r.nextZ,s--):(i=o,o=o.nextZ,u--),n?n.nextZ=i:t=i,i.prevZ=n,n=i;r=o}n.nextZ=null,l*=2}while(a>1)}(i)}(t,o,i,n);for(var s,u,l=t;t.prev!==t.next;)if(s=t.prev,u=t.next,n?R(t,o,i,n):A(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),W(t),t=u.next,l=u.next;else if((t=u)===l){a?1===a?b(t=M(y(t),e,r),e,r,o,i,n,2):2===a&&w(t,e,r,o,i,n):b(y(t),e,r,o,i,n,1);break}}}function A(t){var e=t.prev,r=t,o=t.next;if(I(e,r,o)>=0)return!1;for(var i=e.x,n=r.x,a=o.x,s=e.y,u=r.y,l=o.y,h=in?i>a?i:a:n>a?n:a,d=s>u?s>l?s:l:u>l?u:l,m=o.next;m!==e;){if(m.x>=h&&m.x<=f&&m.y>=c&&m.y<=d&&P(i,s,n,u,a,l,m.x,m.y)&&I(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function R(t,e,r,o){var i=t.prev,n=t,a=t.next;if(I(i,n,a)>=0)return!1;for(var s=i.x,u=n.x,l=a.x,h=i.y,c=n.y,f=a.y,d=su?s>l?s:l:u>l?u:l,g=h>c?h>f?h:f:c>f?c:f,p=D(d,m,e,r,o),x=D(_,g,e,r,o),E=t.prevZ,v=t.nextZ;E&&E.z>=p&&v&&v.z<=x;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==i&&E!==a&&P(s,h,u,c,l,f,E.x,E.y)&&I(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==i&&v!==a&&P(s,h,u,c,l,f,v.x,v.y)&&I(v.prev,v,v.next)>=0)return!1;v=v.nextZ}for(;E&&E.z>=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==i&&E!==a&&P(s,h,u,c,l,f,E.x,E.y)&&I(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;v&&v.z<=x;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==i&&v!==a&&P(s,h,u,c,l,f,v.x,v.y)&&I(v.prev,v,v.next)>=0)return!1;v=v.nextZ}return!0}function M(t,e,r){var o=t;do{var i=o.prev,n=o.next.next;!B(i,n)&&N(i,o,o.next,n)&&X(i,n)&&X(n,i)&&(e.push(i.i/r|0),e.push(o.i/r|0),e.push(n.i/r|0),W(o),W(o.next),o=t=n),o=o.next}while(o!==t);return y(o)}function w(t,e,r,o,i,n){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&C(a,s)){var u=G(a,s);return a=y(a,a.next),u=y(u,u.next),b(a,e,r,o,i,n,0),void b(u,e,r,o,i,n,0)}s=s.next}a=a.next}while(a!==t)}function F(t,e){return t.x-e.x}function L(t,e){var r=function(t,e){var r,o=e,i=t.x,n=t.y,a=-1/0;do{if(n<=o.y&&n>=o.next.y&&o.next.y!==o.y){var s=o.x+(n-o.y)*(o.next.x-o.x)/(o.next.y-o.y);if(s<=i&&s>a&&(a=s,r=o.x=o.x&&o.x>=h&&i!==o.x&&P(nr.x||o.x===r.x&&U(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=G(r,t);return y(o,o.next),y(r,r.next)}function U(t,e){return I(t.prev,t,e.prev)<0&&I(e.next,t,t.next)<0}function D(t,e,r,o,i){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*i|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-o)*i|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function S(t){var e=t,r=t;do{(e.x=(t-a)*(n-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(n-s)>=(i-a)*(o-s)}function C(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&N(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(X(t,e)&&X(e,t)&&function(t,e){var r=t,o=!1,i=(t.x+e.x)/2,n=(t.y+e.y)/2;do{r.y>n!=r.next.y>n&&r.next.y!==r.y&&i<(r.next.x-r.x)*(n-r.y)/(r.next.y-r.y)+r.x&&(o=!o),r=r.next}while(r!==t);return o}(t,e)&&(I(t.prev,t,e.prev)||I(t,e.prev,e))||B(t,e)&&I(t.prev,t,t.next)>0&&I(e.prev,e,e.next)>0)}function I(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function B(t,e){return t.x===e.x&&t.y===e.y}function N(t,e,r,o){var i=z(I(t,e,r)),n=z(I(t,e,o)),a=z(I(r,o,t)),s=z(I(r,o,e));return i!==n&&a!==s||(!(0!==i||!O(t,r,e))||(!(0!==n||!O(t,o,e))||(!(0!==a||!O(r,t,o))||!(0!==s||!O(r,e,o)))))}function O(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function z(t){return t>0?1:t<0?-1:0}function X(t,e){return I(t.prev,t,t.next)<0?I(t,e,t.next)>=0&&I(t,t.prev,e)>=0:I(t,e,t.prev)<0||I(t,t.next,e)<0}function G(t,e){var r=new j(t.i,t.x,t.y),o=new j(e.i,e.x,e.y),i=t.next,n=e.prev;return t.next=e,e.prev=t,r.next=i,i.prev=r,o.next=r,r.prev=o,n.next=o,o.prev=n,o}function H(t,e,r,o){var i=new j(t,e,r);return o?(i.next=o.next,i.prev=o,o.next.prev=i,o.next=i):(i.prev=i,i.next=i),i}function W(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function j(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function Z(t,e,r,o){for(var i=0,n=e,a=r-o;n"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:o,type:i}=e;if("MultiPolygon"===r.type)for(let o=0;o"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,o=v.flatten(e.coordinates),i=new Float32Array(o.vertices.map(((t,e)=>{return e%2==1?(r=t,(180-180/Math.PI*Math.log(Math.tan(Math.PI/4+r*Math.PI/360)))/360):(180+t)/360;var r}))),n=v(o.vertices,o.holes,o.dimensions),a=n.length>256?new Uint16Array(n):new Uint8Array(n),s=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return Math.max(e,o)}(r),u=r.highlight||!1;let l=0,h=0,c=0;for(let t=0;t0&&(o+=t[i-1].length,r.holes.push(o))}return r};const K=(t,e)=>{const{lat:r,lng:i}=t;return new o(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(i,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},k=(t,e)=>{return new c((r=t.y,o=e,i=Math.PI-2*Math.PI*r/256/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,o,i};let V,q={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new a(new o(0,0),new o(0,0)),DEMPixelBounds:new a(new o(0,0),new o(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const $=t=>E(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:i,canopySource:n,dsmSource:s,tileLoaded:l,gl:h,bounds:c,buildingRasterizer:f,tileMerger:d,canopyMerger:m,forceUpdate:_=!1}=t,{getSourceUrl:g,getElevation:p,maxZoom:x,tileSize:v,_overzoom:T}=i,y=(b={getFeatures:r},E(void 0,void 0,void 0,(function*(){const{getFeatures:t}=b;try{return Y(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var b;try{const t=c,r=t.getNorthWest(),i=t.getSouthEast(),E=new a(K(r,e),K(i,e));let T=new o(E.min.x,E.min.y),b=E.max.x-E.min.x;const A=E.max.y-E.min.y,R=E.max.subtract(E.min);R.y>R.x&&(T.x-=256,b+=512);const M=(t=>{const{upperLeft:e,width:r,height:o}=t,i=e.divideBy(256).floor().multiplyBy(256),n=256*(Math.ceil(r/256)+1),s=256*(Math.ceil(o/256)+1),u=i.add([n,s]);return new a(i,u)})({upperLeft:T,width:b,height:A}),{x:w,y:F}=M.max.subtract(M.min),L=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.xe.y{const o=Math.pow(2,t.z);return{x:(t.x%o+o)%o,y:(t.y%o+o)%o,z:t.z,xOffset:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:i}=t,n=e.divideBy(256).floor(),a=n.x+r/256,s=Math.min(n.y+o/256,Math.pow(2,i)-1),u=[];for(var l=n.x;l{const e=h.createTexture();h.activeTexture(h.TEXTURE2),h.bindTexture(h.TEXTURE_2D,e),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,1,1,0,h.RGBA,h.UNSIGNED_BYTE,new Uint8ClampedArray([0,0,0,0])),t(e)})));const[B,N]=yield Promise.all(I);if(null===B||null===N)return q=Object.assign(Object.assign({},q),{visibleDEMPixelBounds:E,demZoom:e,dirty:!1}),q;const O=new a(K(new u(s.bounds).getNorthWest(),e),K(new u(s.bounds).getSouthEast(),e)),z=O.min.subtract(M.min),X=O.max.subtract(M.min),G=[z.x/w,z.y/F,X.x/w,X.y/F].map((t=>2*t-1)),H=yield y,{maxHeight:W,heightMapTex:j}=f.raster({upperLeftTile:L[0],width:w,height:F,mapZoom:e,features:H,imageData:B,gl:h,dsmSource:s,dsmCoords:G,canopyData:N}),Z=Math.max(s.maxHeight,B.maxHeight+W);V=C,q={heightMapTex:j,maxHeight:Z,width:w,height:F,DEMPixelBounds:M,visibleDEMPixelBounds:E,raster:L,demZoom:e,dirty:!0,outputWidth:S,outputHeight:P}}catch(t){console.error("Could not decode height map",t)}return q})),J=(t,e)=>{const{r:r,g:o,b:i}=t;return[r/255,o/255,i/255,e]},Q=(t,e)=>{const{date:r}=e,{dec:o,Hi:i}=d(r);t.updateDate({dec:o,Hi:i})},tt=(t,e)=>E(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),et=(t,e)=>{const{color:r,opacity:o}=e,i=J(r,o);t.updateColor({colorVec:i})},rt=t=>{const{kernel:e,map:r,heightMap:o,now:i,color:n,opacity:a,belowCanopy:s,skipRender:l}=t;try{const{heightMapTex:t,outputHeight:h,outputWidth:c,maxHeight:f,width:m,height:_,DEMPixelBounds:g,visibleDEMPixelBounds:p,demZoom:x}=o;if(0===m||0===_)return;const{min:E,max:v}=p;if(!E||!v)return;const{x:T,y:y}=v.subtract(E),b=g.min,A=r.getPixelDimensions(),R=r.screenUnproject([0,0]),M=r.screenUnproject([A.width,0]),w=r.screenUnproject([A.width,A.height]),F=r.screenUnproject([0,A.height]),L=[F,w,R,M].map((t=>r.project(t,x))),U=L.map((t=>[(t.x-b.x)/m,(t.y-b.y)/_])).flat(),D=L.map((t=>[(t.x-E.x)/T,(t.y-E.y)/y])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),S=b.y/256/Math.pow(2,x),P=_/256/Math.pow(2,x),C=new u(k(g.getTopLeft(),x),k(g.getBottomRight(),x)),I=C.getWest(),B=Math.abs(C.getWest()-C.getEast());((t,e)=>{const{color:r,opacity:o,date:i}=e,n=J(r,o),{dec:a,Hi:s}=d(i);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:f,width:m,height:_,heightMapZoom:x,cornerTextureCoords:U,cornerClipCoords:D,topYCoord:S,ySize:P,west:I,dLng:B,date:i,color:n,opacity:a,outputHeight:h,outputWidth:c,belowCanopy:s,skipRender:l})}catch(t){console.error("EXCEPTION",t)}};const ot=t=>{const{gl:e}=t,{texture:r,imageData:o=null,format:i=e.RGBA,width:n,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:l=e.UNSIGNED_BYTE,internalFormat:h=i}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,h,n,a,0,i,l,o)};function it(t){const{gl:e,vSrc:r,fSrc:o}=t,i=e.createShader(e.VERTEX_SHADER);e.shaderSource(i,r),e.compileShader(i);const n=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(n,o),e.compileShader(n);const a=e.createProgram();return e.attachShader(a,i),e.attachShader(a,n),e.linkProgram(a),a}class nt{constructor(t){this.gl=t,this.program=it({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nuniform sampler2D canopy_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n\t \tvec4 canopyColor = texture2D(canopy_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n float upper = textureColor.b + canopyColor.b;\n float combined = textureColor.a + canopyColor.a;\n if (combined > 1.0) {\n combined = combined - floor(combined);\n upper += 1.0 / 255.0;\n }\n gl_FragColor = vec4(textureColor.b, textureColor.a, upper, combined);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.canopyMapUniformLocation=t.getUniformLocation(this.program,"canopy_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:i,width:n,height:a,imageData:s,dsmCoords:u,dsmSource:l,canopyData:h,gl:c}=t,{x:f,y:d,z:m}=r;let _=0;c.useProgram(this.program),c.activeTexture(c.TEXTURE1),c.bindTexture(c.TEXTURE_2D,s),c.activeTexture(c.TEXTURE2),c.bindTexture(c.TEXTURE_2D,h);const g=n,p=a;c.activeTexture(c.TEXTURE0),c.bindTexture(c.TEXTURE_2D,this.targetTexture),c.texImage2D(c.TEXTURE_2D,0,c.RGBA,g,p,0,c.RGBA,c.UNSIGNED_BYTE,null),i>15&&e.length>5?(c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.NEAREST),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.NEAREST)):(c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.LINEAR),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.LINEAR)),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_S,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_T,c.CLAMP_TO_EDGE),c.viewport(0,0,g,p);const x=c.createFramebuffer();c.bindFramebuffer(c.FRAMEBUFFER,x);const E=c.COLOR_ATTACHMENT0;c.framebufferTexture2D(c.FRAMEBUFFER,E,c.TEXTURE_2D,this.targetTexture,0),c.disable(c.BLEND),c.bindBuffer(c.ARRAY_BUFFER,this.positionBuffer),c.bindBuffer(c.ELEMENT_ARRAY_BUFFER,this.indexBuffer),c.clearColor(0,0,0,0),c.clear(c.COLOR_BUFFER_BIT);const v=f/Math.pow(2,m),T=d/Math.pow(2,m),y=new o(v,T),b=n/256/Math.pow(2,m),A=a/256/Math.pow(2,m);c.uniform3f(this.xyzUniformLocation,v,T,m),c.uniform2f(this.dimensionsUniformLocation,b,A),c.uniform1i(this.heightMapUniformLocation,1),c.uniform1i(this.canopyMapUniformLocation,2),c.uniform1i(this.useDSMUniformLocation,0);const R=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(c.enableVertexAttribArray(this.positionAttributeLocation),c.bufferData(c.ARRAY_BUFFER,R,c.STATIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.uniform4f(this.colorUniformLocation,0,0,0,0),c.drawArrays(c.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:i,centroid:s,highlight:u}=t,l=new o(s[0],s[1]).subtract(y).unscaleBy(new o(b,A)).scaleBy(new o(n,a)).floor();if(l.x<0||l.y<0||l.x>n||l.y>a)return;const h=l.x/n,f=l.y/a,d=5*e,m=Math.floor(d/255)/255,g=Math.floor(d%255)/255;_=Math.max(_,e),c.uniform2f(this.centroidUniformLocation,h,f),c.uniform4f(this.colorUniformLocation,m,g,m,g),c.bufferData(c.ARRAY_BUFFER,r,c.DYNAMIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.bufferData(c.ARRAY_BUFFER,r,c.DYNAMIC_DRAW),c.vertexAttribPointer(this.dsmAttributeLocation,2,c.FLOAT,!1,0,0),c.bufferData(c.ELEMENT_ARRAY_BUFFER,i,c.DYNAMIC_DRAW),c.drawElements(c.TRIANGLES,i.length,i.length>256?c.UNSIGNED_SHORT:c.UNSIGNED_BYTE,0)})),0!==l.data.length){c.activeTexture(c.TEXTURE1);const t=c.createTexture();c.bindTexture(c.TEXTURE_2D,t),c.pixelStorei(c.UNPACK_ALIGNMENT,2),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_S,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_T,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.NEAREST),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.NEAREST),c.texImage2D(c.TEXTURE_2D,0,c.LUMINANCE_ALPHA,l.width,l.height,0,c.LUMINANCE_ALPHA,c.UNSIGNED_BYTE,l.data),c.pixelStorei(c.UNPACK_ALIGNMENT,4);const e=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],r=new Float32Array(e);c.enableVertexAttribArray(this.positionAttributeLocation),c.bufferData(c.ARRAY_BUFFER,r,c.STATIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.enableVertexAttribArray(this.dsmAttributeLocation),c.bindBuffer(c.ARRAY_BUFFER,this.dsmBuffer),c.bufferData(c.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),c.STATIC_DRAW),c.vertexAttribPointer(this.dsmAttributeLocation,2,c.FLOAT,!1,0,0),c.uniform4f(this.colorUniformLocation,0,0,0,0),c.uniform1i(this.useDSMUniformLocation,1),c.colorMask(!1,!1,!0,!0),c.drawArrays(c.TRIANGLE_STRIP,0,4),c.deleteTexture(t)}return c.colorMask(!0,!0,!0,!0),c.deleteFramebuffer(x),{maxHeight:_,heightMapTex:this.targetTexture}}}const at=t=>{const{tile:e,maxZoom:r,tileSize:o}=t;let{x:i,y:n,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);i=Math.floor(i/t),n=Math.floor(n/t),a=r}return{x:256===o?i:Math.floor(i/2),y:256===o?n:Math.floor(n/2),z:256===o?a:a-1}};class st{constructor(t){this.gl=t,this.program=it({vSrc:"\n attribute vec4 a_tex_position;\n attribute vec4 a_tile_position;\n varying vec2 v_tex_pos;\n void main() {\n v_tex_pos = a_tex_position.xy;\n gl_Position = a_tile_position;\n }\n ",fSrc:"\n precision mediump float;\n uniform sampler2D u_texture;\n uniform float u_encoding;\n uniform float u_tile_size;\n varying vec2 v_tex_pos;\n\n float getElevationTerrarium(vec4 color) {\n float decoded = color.r * 255.0 * 256.0 + color.g * 255.0 + color.b * 255.0 / 256.0 - 32768.0;\n return decoded;\n }\n\n float getElevationMapbox(vec4 color) {\n float decoded = -10000.0 + (color.r * 255.0 * 256.0 * 256.0 + color.g * 255.0 * 256.0 + color.b * 255.0) * .1;\n return decoded;\n }\n\n float getElevationSwitzerland(vec4 color) {\n float decoded = ((color.r * 255.0 * 256.0 + color.g * 255.0) / pow(2.0, 16.0)) * 4808.0;\n return decoded;\n }\n\n float getElevationCanopy(vec4 color) {\n float closestDistance = 1000.0;\n int closestColor = 100;\n const int colorCount = 26;\n vec3 colorArray[colorCount];\n\n colorArray[0] = vec3(51.0 / 255.0, 0, 66.0 / 255.0);\n colorArray[1] = vec3(51.0 / 255.0, 16.0/255.0, 86.0 / 255.0);\n colorArray[2] = vec3(51.0 / 255.0, 24.0/255.0, 86.0 / 255.0);\n colorArray[3] = vec3(51.0 / 255.0, 24.0/255.0, 98.0 / 255.0);\n colorArray[4] = vec3(51.0 / 255.0, 37.0/255.0, 108.0 / 255.0);\n colorArray[5] = vec3(48.0 / 255.0, 47.0/255.0, 114.0 / 255.0);\n colorArray[6] = vec3(44.0 / 255.0, 57.0/255.0, 116.0 / 255.0);\n colorArray[7] = vec3(42.0 / 255.0, 68.0/255.0, 117.0 / 255.0);\n colorArray[8] = vec3(39.0 / 255.0, 79.0/255.0, 120.0 / 255.0);\n colorArray[9] = vec3(37.0 / 255.0, 89.0/255.0, 123.0 / 255.0);\n colorArray[10] = vec3(34.0 / 255.0, 100.0/255.0, 120.0 / 255.0);\n colorArray[11] = vec3(34.0 / 255.0, 110.0/255.0, 121.0 / 255.0);\n colorArray[12] = vec3(30.0 / 255.0, 120.0/255.0, 120.0 / 255.0);\n colorArray[13] = vec3(32.0 / 255.0, 132.0/255.0, 117.0 / 255.0);\n colorArray[14] = vec3(36.0 / 255.0, 141.0/255.0, 112.0 / 255.0);\n colorArray[15] = vec3(40.0 / 255.0, 152.0/255.0, 108.0 / 255.0);\n colorArray[16] = vec3(44.0 / 255.0, 161.0/255.0, 101.0 / 255.0);\n colorArray[17] = vec3(52.0 / 255.0, 173.0/255.0, 95.0 / 255.0);\n colorArray[18] = vec3(69.0 / 255.0, 182.0/255.0, 84.0 / 255.0);\n colorArray[19] = vec3(88.0 / 255.0, 191.0/255.0, 72.0 / 255.0);\n colorArray[20] = vec3(109.0 / 255.0, 201.0/255.0, 61.0 / 255.0);\n colorArray[21] = vec3(130.0 / 255.0, 209.0/255.0, 51.0 / 255.0);\n colorArray[22] = vec3(158.0 / 255.0, 213.0/255.0, 45.0 / 255.0);\n colorArray[23] = vec3(189.0 / 255.0, 218.0/255.0, 40.0 / 255.0);\n colorArray[24] = vec3(219.0 / 255.0, 224.0/255.0, 36.0 / 255.0);\n colorArray[25] = vec3(252.0 / 255.0, 228.0/255.0, 30.0 / 255.0);\n\n for (int i = 0; i < colorCount; i++) {\n float distance = sqrt(pow(color.x - colorArray[i].x, 2.0) + pow(color.y - colorArray[i].y, 2.0) + pow(color.z - colorArray[i].z, 2.0));\n if (distance < closestDistance) {\n closestDistance = distance;\n closestColor = i;\n }\n }\n\n return float(closestColor);\n }\n\n float getElevation(vec4 color, float encoding) {\n float decoded;\n if (encoding == 1.0) {\n decoded = getElevationMapbox(color);\n } else if (encoding == 2.0) {\n decoded = getElevationSwitzerland(color);\n } else if (encoding == 3.0) {\n decoded = getElevationCanopy(color);\n } else {\n decoded = getElevationTerrarium(color);\n }\n float scaled = decoded * 5.0;\n float elevation = max(scaled, 0.0);\n return elevation;\n }\n\n float textureSampleLinear(sampler2D sampler, vec2 texCoord) {\n vec2 texelSize = vec2(1.0 / u_tile_size, 1.0 / u_tile_size);\n \n // Calculate interpolation weights based on texCoord fract (fractional part)\n vec2 uv = fract(texCoord * u_tile_size);\n \n // Sample four nearest texels\n vec4 bottomLeft = texture2D(sampler, texCoord);\n vec4 bottomRight = texture2D(sampler, texCoord + vec2(texelSize.x, 0.0));\n vec4 topLeft = texture2D(sampler, texCoord + vec2(0.0, texelSize.y));\n vec4 topRight = texture2D(sampler, texCoord + texelSize);\n\n float bottomLeftEl = getElevation(bottomLeft, u_encoding);\n float bottomRightEl = getElevation(bottomRight, u_encoding);\n float topLeftEl = getElevation(topLeft, u_encoding);\n float topRightEl = getElevation(topRight, u_encoding);\n \n // Interpolate between bottom and top rows\n float bottom = mix(bottomLeftEl, bottomRightEl, uv.x);\n float top = mix(topLeftEl, topRightEl, uv.x);\n \n // Interpolate final color\n return mix(bottom, top, uv.y);\n }\n\n void main() {\n float elevation = textureSampleLinear(u_texture, v_tex_pos);\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n gl_FragColor = vec4(0, 0, r, g);\n\n // vec4 color = texture2D(u_texture, v_tex_pos);\n // float elevation = getElevation(color, u_encoding);\n // float r = floor(elevation / 255.0) / 255.0;\n // float g = floor(mod(elevation, 255.0)) / 255.0;\n // gl_FragColor = vec4(0, 0, r, g);\n // gl_FragColor = color;\n }\n ",gl:t}),t.useProgram(this.program),this.texPositionAttributeLocation=t.getAttribLocation(this.program,"a_tex_position"),this.tilePositionAttributeLocation=t.getAttribLocation(this.program,"a_tile_position"),this.encodingUniformLocation=t.getUniformLocation(this.program,"u_encoding"),this.tileSizeUniformLocation=t.getUniformLocation(this.program,"u_tile_size"),this.texPositionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.texPositionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.texPositionAttributeLocation),t.vertexAttribPointer(this.texPositionAttributeLocation,2,t.FLOAT,!1,0,0),this.tilePositionBuffer=t.createBuffer(),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return E(this,void 0,void 0,(function*(){const r=this.gl;let o=0;const{width:i,height:n,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:l,tileLoaded:h,maxZoom:c}=e;if(r){this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const e=new Set;t.forEach((t=>{e.add(s(at({tile:t,maxZoom:c,tileSize:l})))})),r.useProgram(this.program);let f=0;-32768===u({r:0,g:0,b:0,a:0})&&(f=0),-1e4===u({r:0,g:0,b:0,a:0})&&(f=1),4808===u({r:256,g:0,b:0,a:0})&&(f=2),0===u({r:51,g:0,b:66,a:0})&&(f=3),r.uniform1f(this.encodingUniformLocation,f),ot({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:i,height:n,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST}),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const d=r.checkFramebufferStatus(r.FRAMEBUFFER);d!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+d),r.clear(r.COLOR_BUFFER_BIT);const m=Array.from(e).map((e=>E(this,void 0,void 0,(function*(){return new Promise(((u,h)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{r.useProgram(this.program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.tileTexture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,f),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer);const a=t.filter((t=>e===s(at({tile:t,maxZoom:c,tileSize:l})))),h=a.reduce(((t,e)=>Math.min(t,e.xOffset)),1/0),d=a.reduce(((t,e)=>Math.min(t,e.yOffset)),1/0),m=a.reduce(((t,e)=>Math.max(t,e.xOffset)),0),_=a.reduce(((t,e)=>Math.max(t,e.yOffset)),0),g=a.reduce(((t,e)=>Math.min(t,e.x)),1/0),p=a.reduce(((t,e)=>Math.min(t,e.y)),1/0),x=a.reduce(((t,e)=>Math.max(t,e.x)),0),E=a.reduce(((t,e)=>Math.max(t,e.y)),0),v=a[0].z,T=h/i,y=d/n,b=(m+256)/i,A=(_+256)/n,R=[T,y,b,y,T,A,b,A].map((t=>2*t-1));r.bindBuffer(r.ARRAY_BUFFER,this.tilePositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(R),r.STATIC_DRAW),r.enableVertexAttribArray(this.tilePositionAttributeLocation),r.vertexAttribPointer(this.tilePositionAttributeLocation,2,r.FLOAT,!1,0,0);let M=[0,0,1,0,0,1,1,1];const w=Math.pow(2,Math.max(0,v-c)+(256!==l?1:0)),F=514===l?1/l:0,L=1/w,U=g%w/w,D=p%w/w,S=x%w/w,P=E%w/w;M=[U,D,S+L,D,U,P+L,S+L,P+L].map((t=>t*(514===l?512/514:1)+F)),r.bindBuffer(r.ARRAY_BUFFER,this.texPositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(M),r.STATIC_DRAW),r.enableVertexAttribArray(this.texPositionAttributeLocation),r.vertexAttribPointer(this.texPositionAttributeLocation,2,r.FLOAT,!1,0,0),r.uniform1f(this.tileSizeUniformLocation,l),r.viewport(0,0,i,n),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const C=Math.floor(T*i),I=Math.floor(y*n),B=Math.ceil((b-T)*i),N=Math.ceil((A-y)*n),O=new Uint8Array(4*B*N);r.readPixels(C,I,B,N,r.RGBA,r.UNSIGNED_BYTE,O);for(let t=0;t{if(f.src!==f.originalSource)return h("new tiles requested");u(null)},f.crossOrigin=a||null,f.src=e,f.originalSource=f.src})).then((()=>{this.finished++,h(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(m)}catch(t){return console.log(`${m.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=o,this.outputTexture}throw new Error("Could not get canvas context for merging tile images")}))}}class ut extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},canopySource:void 0,dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>E(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>E(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(Q(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=p(this.options.date)-p(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(et(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(et(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setCanopySource(t){return this.options.canopySource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return E(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:o=new Date,iterations:i=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:o,iterations:i}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)Q(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime(){const e=K(t,r);return[(e.x-n.x)/o,(e.y-n.y)/i]}))}return[]}_getBounds(t,e){const{width:r,height:i}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(i))return t.getBounds();const n=t.getCenter(),a=K(n,e),s=k(new o(a.x-r/2,a.y-i/2),e),u=k(new o(a.x+r/2,a.y+i/2),e);return t.createBounds({nw:s,se:u})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return E(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=x(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),i=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===i)).map((t=>t.canonical)),[a,s,u,l]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),h=k(new o(512*a,512*s),i+1),c=k(new o(512*(u+1),512*(l+1)),i+1);e=i+1,this._bounds=t.createBounds({nw:h,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield $({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,canopySource:this.options.canopySource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,canopyMerger:this._canopyMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return E(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(rt({kernel:this._compiledKernel,map:x(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield tt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const i=new Uint8Array(r*o*4);return this._gl&&this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,i),i}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),o=t*e*1,i=1*t,n=(e-1)*i,a=new Uint8Array(o),s=new Uint8Array(o);let u=0;const{startDate:l,endDate:h}=this.options.sunExposure;this.options.sunExposure.enabled&&l&&h&&(u=h.getTime()-l.getTime());for(let t=0;t{const r=J({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=J({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=p(t.startDate)-p(t.startDate,t.tzId),r=t.startDate.getTime()-e,o=t.endDate.getTime()-e,i=this._lngLatToTextureCoords([t.location])[0],{output:n,outputWidth:a,outputHeight:s}=((t,e)=>{const r=J({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=J({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord:i}));return n.toArray=function(){const e=new Array;for(let r=0;r=0;e--){const i=e*a*4+4*r,n=this.slice(i,i+4);o.push(n.join("")===t.sunColor.join("")?1:0)}e.push(o)}return e},{data:n,width:a,height:s}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:o,endDate:i}=this.options.sunExposure,n=i.getTime()-o.getTime(),a=g(r,.5,n);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t),this.id="shademap-layer",this.type="custom",this.canvasSourceId="canvas-source",this.attributionSourceId="attribution-source",this.canvasLayerId="canvas-layer",this.attributionLayerId="attribution-layer",this._refreshing=0,this._raf=0,this.id=this.id+_(),this.canvasSourceId=this.canvasSourceId+_(),this.attributionSourceId=this.attributionSourceId+_(),this.canvasLayerId=this.canvasLayerId+_(),this.attributionLayerId=this.attributionLayerId+_(),this.options.terrainSource.tileSize>256&&(this.options.terrainSource.maxZoom=this.options.terrainSource.maxZoom+1),this._moveEndHandler=()=>{this._map&&this._reset()}}render(t,e){}addTo(t){return t.addLayer(this),this}onAdd(t){this._map=t,this._gl=this._map.painter.context.gl,this._framebuffer=this._gl.createFramebuffer();this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,o=e,i=it({gl:o,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_outputSunExposure;uniform bool u_outputShadeProfile;uniform sampler2D u_decHiTexture;uniform sampler2D u_gpxTexture;uniform bool u_outputLocationShadeProfile;uniform vec2 u_shadeProfileLocation;float kernelResult;void kernel(){float sunDec=user_dec;float sunHi=user_Hi;vec4 shade_color=user_color;float maxHeight=user_maxHeight/1000.0;float user_x=vTexCoordCropped.x;float user_y=vTexCoordCropped.y;if(u_outputShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;vec4 gpx=texture2D(u_gpxTexture,vec2(vTexCoordFull.x,.5));user_x=gpx.x;user_y=gpx.y;}if(u_outputLocationShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;user_x=u_shadeProfileLocation.x;user_y=u_shadeProfileLocation.y;}float user_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitudeiter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_outputSunExposure){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));float timeInSun=1.0-aggregateColor.r;float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,0.5);}}void main(void){kernel();gl_FragColor=actualColor;}"});o.useProgram(i);const n=o.createBuffer(),a=o.getAttribLocation(i,"a_pos"),s=o.createBuffer(),u=o.getAttribLocation(i,"a_tex_pos"),l=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,l),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW);const h=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,h),o.bufferData(o.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),o.STATIC_DRAW);const c=o.getUniformLocation(i,"user_a");o.uniform1i(c,0);const f=o.getUniformLocation(i,"user_width"),_=o.getUniformLocation(i,"user_height"),g=o.getUniformLocation(i,"user_maxHeight"),x=o.getUniformLocation(i,"user_zoom"),v=o.getUniformLocation(i,"user_topYCoord"),T=o.getUniformLocation(i,"user_ySize"),y=o.getUniformLocation(i,"user_step"),b=o.getUniformLocation(i,"user_west"),A=o.getUniformLocation(i,"user_dLng"),R=o.getUniformLocation(i,"user_dec"),M=o.getUniformLocation(i,"user_Hi"),w=o.getUniformLocation(i,"user_color"),F=o.getUniformLocation(i,"u_below_canopy"),L=o.getUniformLocation(i,"user_sunExposureTexture"),U=o.getUniformLocation(i,"user_outputSunExposure"),D=o.getUniformLocation(i,"u_outputShadeProfile"),S=o.getUniformLocation(i,"u_gpxTexture"),P=o.getUniformLocation(i,"u_decHiTexture"),C=o.getUniformLocation(i,"u_sunColor"),I=o.getUniformLocation(i,"u_outputLocationShadeProfile"),B=o.getUniformLocation(i,"u_shadeProfileLocation"),N=o.createTexture();let O=0,z=0,X=0,G=null;const H=()=>{z&&X&&(o.useProgram(i),o.bindBuffer(o.ARRAY_BUFFER,n),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,s),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),r(o,z,X),o.viewport(0,0,z,X),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4))};let W=0,j=0,Z=0,Y="";const K=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(o.activeTexture(o.TEXTURE3),o.uniform1i(P,3),t.startTime!==j||t.endTime!==Z||t.tzId!==Y){j=t.startTime,Z=t.endTime,Y=t.tzId;const i=[],n=828e5,a=p(new Date(j),Y);for(let t=0;t{const{dec:e,Hi:r}=t;o.useProgram(i),o.uniform1f(R,e),o.uniform1f(M,r),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},updateDateRange:t=>E(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:l,emit:h}=t;o.useProgram(i);const c=W=Date.now(),f=Math.floor((r.getTime()-e.getTime())/l),m=o.getUniform(i,w),_=z,g=X,p=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,p),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,_,g,0,o.RGBA,o.UNSIGNED_BYTE,null),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MIN_FILTER,o.NEAREST),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MAG_FILTER,o.NEAREST);const x=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,x);const E=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,E,o.TEXTURE_2D,p,0);for(let t=0;t{window.requestAnimationFrame((()=>{o.useProgram(i),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindFramebuffer(o.FRAMEBUFFER,x);const h=o.checkFramebufferStatus(o.FRAMEBUFFER);h!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:m}=d(new Date(e.getTime()+f*t));o.uniform1f(R,c),o.uniform1f(M,m),o.uniform4fv(w,[1/l,0,0,1]),o.bindBuffer(o.ARRAY_BUFFER,n),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,s),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,_,g),o.enable(o.BLEND),o.blendFunc(o.ONE,o.ONE),o.drawArrays(o.TRIANGLE_STRIP,0,4),r()}))}))}return o.deleteFramebuffer(x),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(o.useProgram(i),c!==W)return o.deleteTexture(p),o.uniform4fv(w,m),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,p),o.uniform1i(L,2),o.uniform1i(U,1),H(),o.uniform1i(U,0),o.uniform1i(L,0),o.deleteTexture(p),o.uniform4fv(w,m),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;o.useProgram(i),o.uniform4fv(w,e),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},generateShadeProfile:t=>{o.useProgram(i);const{dates:e,texCoords:r,shadeColor:n,sunColor:s}=t,c=r.length,f=e.length,m=o.getUniform(i,w);o.uniform4fv(w,n),o.uniform4fv(C,s),o.uniform1i(D,1);const _=o.createTexture();o.activeTexture(o.TEXTURE3),ot({gl:o,imageData:null,width:c,height:f,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:_});const g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,_,0);const p=o.checkFramebufferStatus(o.FRAMEBUFFER);p!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p);const x=r.map((t=>[t[0],t[1]])).flat(),E=o.createTexture();o.activeTexture(o.TEXTURE2),ot({gl:o,texture:E,imageData:new Float32Array(x),width:x.length/2,height:1,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT}),o.uniform1i(S,2);const v=e.map((t=>{const{dec:e,Hi:r}=d(t);return[-e,r/10]})).flat(),T=o.createTexture();o.activeTexture(o.TEXTURE1),ot({gl:o,texture:T,imageData:new Float32Array(v),width:1,height:e.length,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT}),o.uniform1i(P,1),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindBuffer(o.ARRAY_BUFFER,l),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,c,f),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const y=new Uint8Array(c*f*4);return o.readPixels(0,0,c,f,o.RGBA,o.UNSIGNED_BYTE,y),o.deleteTexture(_),o.deleteTexture(T),o.deleteTexture(E),o.deleteFramebuffer(g),o.uniform1i(P,0),o.uniform1i(S,0),o.uniform1i(D,0),o.uniform4fv(w,m),o.uniform4fv(C,[0,0,0,0]),y},generateLocationShadeProfile:t=>{o.useProgram(i);const{startTime:e,endTime:r,tzId:n,texCoord:s,shadeColor:c,sunColor:f}=t,d=o.getUniform(i,w),{outputWidth:m,outputHeight:_}=K({startTime:e,endTime:r,tzId:n});o.uniform4fv(w,c),o.uniform4fv(C,f),o.uniform1i(I,1),o.uniform2fv(B,[s[0],s[1]]);const g=o.createTexture();o.activeTexture(o.TEXTURE2),ot({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:g});const p=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,p),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,g,0);const x=o.checkFramebufferStatus(o.FRAMEBUFFER);x!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindBuffer(o.ARRAY_BUFFER,l),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,m,_),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const E=new Uint8Array(m*_*4);return o.readPixels(0,0,m,_,o.RGBA,o.UNSIGNED_BYTE,E),o.deleteTexture(g),o.deleteFramebuffer(p),o.uniform1i(P,0),o.uniform1i(I,0),o.uniform4fv(w,d),o.uniform4fv(C,[0,0,0,0]),{output:E,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:(e,r,o)=>{const i=t.getSource(this.canvasSourceId).texture;e.activeTexture(e.TEXTURE1),i.bind(e.LINEAR,e.CLAMP_TO_EDGE),i.size=[r,o],e.texImage2D(e.TEXTURE_2D,0,e.RGBA,r,o,0,e.RGBA,e.UNSIGNED_BYTE,null),e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,i.texture,0),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA)}}),this._tileMerger=new st(this._gl),this._canopyMerger=new st(this._gl),this._buildingRasterizer=new nt(this._gl),document.body.appendChild(this._canvas),this._canvas.style.display="none";const e=t.getBounds(),r=e.getNorthWest(),o=e.getNorthEast(),i=e.getSouthEast(),n=e.getSouthWest(),a=[[r.lng,r.lat],[o.lng,o.lat],[i.lng,i.lat],[n.lng,n.lat]];return t.addSource(this.canvasSourceId,{type:"canvas",canvas:this._canvas,coordinates:a,animate:!1}),t.addLayer({id:this.canvasLayerId,type:"raster",source:this.canvasSourceId,paint:{"raster-fade-duration":0}}),t.addSource(this.attributionSourceId,{type:"geojson",data:{type:"Feature",properties:{},geometry:{type:"Point",coordinates:[-122.210598,47.769799]}},attribution:'© ShadeMap'}),t.addLayer({id:this.attributionLayerId,type:"fill",source:this.attributionSourceId}),this._map.on("moveend",this._moveEndHandler),this._moveEndHandler(),this}onRemove(){return this._map&&(this._map.off("moveend",this._moveEndHandler),this._map.removeLayer(this.attributionLayerId),this._map.removeLayer(this.canvasLayerId),this._map.removeSource(this.attributionSourceId),this._map.removeSource(this.canvasSourceId)),this._gl&&this._framebuffer&&this._gl.deleteFramebuffer(this._framebuffer),document.body.removeChild(this._canvas),this.options.debug("onRemove called"),this}_getHeightMapCoords(t,e){if(this._map&&this.options.sunExposure.enabled&&this._bounds&&this._heightMap){const r=this._map.unproject([t,e]);if(r.toString()===this._map.unproject([t,e+1]).toString())return new o(-1,-1);const{visibleDEMPixelBounds:i,demZoom:n}=this._heightMap;return x(this._map).project(r,n).subtract(i.min)}return new o(-1,-1)}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this._getHeightMapCoords(t,e),o=this.readPixel(r.x,r.y),{startDate:i,endDate:n}=this.options.sunExposure,a=n.getTime()-i.getTime(),s=g(o,.5,a);return Math.abs(s/1e3/3600)}return 0}remove(){this._map&&this._map.removeLayer(this.id)}readPixel(t,e){const r=new Uint8Array(4);if(this._map&&this._gl&&this._framebuffer){const o=this._gl,i=this._map.getSource(this.canvasSourceId).texture;o.activeTexture(o.TEXTURE1),i.bind(o.LINEAR,o.CLAMP_TO_EDGE),o.bindFramebuffer(o.FRAMEBUFFER,this._framebuffer),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,i.texture,0),this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r)}return r}readPixels(t,e,r,o){const i=new Uint8Array(r*o*4);if(this._map&&this._gl&&this._framebuffer){const n=this._gl,a=this._map.getSource(this.canvasSourceId).texture;n.activeTexture(n.TEXTURE1),a.bind(n.LINEAR,n.CLAMP_TO_EDGE),n.bindFramebuffer(n.FRAMEBUFFER,this._framebuffer),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,a.texture,0),this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,i)}return i}_flush(){if(this._map){this._map.getSource(this.canvasSourceId).fire({type:"data",dataType:"source",sourceDataType:"content"})}this.emit("idle")}_repositionCanvas(t){if(this._map){const e=this._map.getSource(this.canvasSourceId);if(e){const r=t.getNorthWest(),o=t.getNorthEast(),i=t.getSouthEast(),n=t.getSouthWest(),a=[[r.lng,r.lat],[o.lng,o.lat],[i.lng,i.lat],[n.lng,n.lat]];e.setCoordinates(a),this.emit("idle")}}return this}}export{ut as default}; diff --git a/dist/mapbox-gl-shadow-simulator.umd.min.js b/dist/mapbox-gl-shadow-simulator.umd.min.js index add1b22..c8f12aa 100644 --- a/dist/mapbox-gl-shadow-simulator.umd.min.js +++ b/dist/mapbox-gl-shadow-simulator.umd.min.js @@ -1,8 +1,8 @@ /** * Copyright Ted Piotrowski 2024 * Package: mapbox-gl-shadow-simulator - * Version: 0.57.1 + * Version: 0.58.0 * For licensing visit: https://shademap.app/about/ */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ShadeMap=e()}(this,(function(){"use strict";function t(t,e,r){var i=e[1],o=e[0],n=i-o;return t===i&&r?t:((t-o)%n+n)%n+o}function e(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var r=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function i(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var o=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function n(t,e,o){return t instanceof i?t:r(t)?new i(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new i(t.x,t.y):new i(t,e,o)}function a(t,e){if(t)for(var r=e?[t,e]:t,i=0,o=r.length;i=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=s(t);var e=this.min,r=this.max,i=t.min,o=t.max,n=o.x>=e.x&&i.x<=r.x,a=o.y>=e.y&&i.y<=r.y;return n&&a},overlaps:function(t){t=s(t);var e=this.min,r=this.max,i=t.min,o=t.max,n=o.x>e.x&&i.xe.y&&i.y=i.lat&&r.lat<=o.lat&&e.lng>=i.lng&&r.lng<=o.lng},intersects:function(t){t=h(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),o=t.getNorthEast(),n=o.lat>=e.lat&&i.lat<=r.lat,a=o.lng>=e.lng&&i.lng<=r.lng;return n&&a},overlaps:function(t){t=h(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),o=t.getNorthEast(),n=o.lat>e.lat&&i.late.lng&&i.lng{const e=t.valueOf();return g(e)},g=t=>{const e=t/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,o=Math.atan2(Math.sin(i)*Math.cos(m),Math.cos(i));return{dec:Math.asin(Math.sin(m)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-o)%(2*Math.PI)+2*Math.PI}},x=()=>Math.floor(1e7*Math.random()),p=(t,e,r)=>{const i=1/e,o=Math.min(t[0]*i,255),n=Math.min(t[1]*i,255),a=Math.min(t[2]*i,255);let s=0;return o+n+a!==0&&(s=o>0?o/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},E=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};const v=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new i(((t,e)=>(t+180)/360*Math.pow(2,e)*d)(e,o),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*d)(t,o))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new c((o=r.y,n=i,a=Math.PI-2*Math.PI*o/d/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/d/Math.pow(2,e)*360-180}(r.x,i));var o,n,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new u(e,r)}}};function T(t,e,r,i){return new(r||(r=Promise))((function(o,n){function a(t){try{u(i.next(t))}catch(t){n(t)}}function s(t){try{u(i.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((i=i.apply(t,e||[])).next())}))}function y(t,e,r){r=r||2;var i,o,n,a,s,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=b(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(l&&(f=function(t,e,r,i){var o,n,a,s=[];for(o=0,n=e.length;o80*r){i=n=t[0],o=a=t[1];for(var m=r;mn&&(n=s),u>a&&(a=u);h=0!==(h=Math.max(n-i,a-o))?32767/h:0}return A(f,d,r,i,o,h,0),d}function b(t,e,r,i,o){var n,a;if(o===K(t,e,r,i)>0)for(n=e;n=e;n-=i)a=j(n,t[n],t[n+1],a);return a&&O(a,a.next)&&(Z(a),a=a.next),a}function R(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!O(i,i.next)&&0!==N(i.prev,i,i.next))i=i.next;else{if(Z(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function A(t,e,r,i,o,n,a){if(t){!a&&n&&function(t,e,r,i){var o=t;do{0===o.z&&(o.z=P(o.x,o.y,e,r,i)),o.prevZ=o.prev,o.nextZ=o.next,o=o.next}while(o!==t);o.prevZ.nextZ=null,o.prevZ=null,function(t){var e,r,i,o,n,a,s,u,h=1;do{for(r=t,t=null,n=null,a=0;r;){for(a++,i=r,s=0,e=0;e0||u>0&&i;)0!==s&&(0===u||!i||r.z<=i.z)?(o=r,r=r.nextZ,s--):(o=i,i=i.nextZ,u--),n?n.nextZ=o:t=o,o.prevZ=n,n=o;r=i}n.nextZ=null,h*=2}while(a>1)}(o)}(t,i,o,n);for(var s,u,h=t;t.prev!==t.next;)if(s=t.prev,u=t.next,n?w(t,i,o,n):M(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),Z(t),t=u.next,h=u.next;else if((t=u)===h){a?1===a?A(t=F(R(t),e,r),e,r,i,o,n,2):2===a&&L(t,e,r,i,o,n):A(R(t),e,r,i,o,n,1);break}}}function M(t){var e=t.prev,r=t,i=t.next;if(N(e,r,i)>=0)return!1;for(var o=e.x,n=r.x,a=i.x,s=e.y,u=r.y,h=i.y,l=on?o>a?o:a:n>a?n:a,d=s>u?s>h?s:h:u>h?u:h,m=i.next;m!==e;){if(m.x>=l&&m.x<=f&&m.y>=c&&m.y<=d&&I(o,s,n,u,a,h,m.x,m.y)&&N(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function w(t,e,r,i){var o=t.prev,n=t,a=t.next;if(N(o,n,a)>=0)return!1;for(var s=o.x,u=n.x,h=a.x,l=o.y,c=n.y,f=a.y,d=su?s>h?s:h:u>h?u:h,g=l>c?l>f?l:f:c>f?c:f,x=P(d,m,e,r,i),p=P(_,g,e,r,i),E=t.prevZ,v=t.nextZ;E&&E.z>=x&&v&&v.z<=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==o&&E!==a&&I(s,l,u,c,h,f,E.x,E.y)&&N(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==o&&v!==a&&I(s,l,u,c,h,f,v.x,v.y)&&N(v.prev,v,v.next)>=0)return!1;v=v.nextZ}for(;E&&E.z>=x;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==o&&E!==a&&I(s,l,u,c,h,f,E.x,E.y)&&N(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;v&&v.z<=p;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==o&&v!==a&&I(s,l,u,c,h,f,v.x,v.y)&&N(v.prev,v,v.next)>=0)return!1;v=v.nextZ}return!0}function F(t,e,r){var i=t;do{var o=i.prev,n=i.next.next;!O(o,n)&&z(o,i,i.next,n)&&H(o,n)&&H(n,o)&&(e.push(o.i/r|0),e.push(i.i/r|0),e.push(n.i/r|0),Z(i),Z(i.next),i=t=n),i=i.next}while(i!==t);return R(i)}function L(t,e,r,i,o,n){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&B(a,s)){var u=W(a,s);return a=R(a,a.next),u=R(u,u.next),A(a,e,r,i,o,n,0),void A(u,e,r,i,o,n,0)}s=s.next}a=a.next}while(a!==t)}function U(t,e){return t.x-e.x}function D(t,e){var r=function(t,e){var r,i=e,o=t.x,n=t.y,a=-1/0;do{if(n<=i.y&&n>=i.next.y&&i.next.y!==i.y){var s=i.x+(n-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(s<=o&&s>a&&(a=s,r=i.x=i.x&&i.x>=l&&o!==i.x&&I(nr.x||i.x===r.x&&S(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=W(r,t);return R(i,i.next),R(r,r.next)}function S(t,e){return N(t.prev,t,e.prev)<0&&N(e.next,t,t.next)<0}function P(t,e,r,i,o){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*o|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*o|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function C(t){var e=t,r=t;do{(e.x=(t-a)*(n-s)&&(t-a)*(i-s)>=(r-a)*(e-s)&&(r-a)*(n-s)>=(o-a)*(i-s)}function B(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&z(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(H(t,e)&&H(e,t)&&function(t,e){var r=t,i=!1,o=(t.x+e.x)/2,n=(t.y+e.y)/2;do{r.y>n!=r.next.y>n&&r.next.y!==r.y&&o<(r.next.x-r.x)*(n-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(N(t.prev,t,e.prev)||N(t,e.prev,e))||O(t,e)&&N(t.prev,t,t.next)>0&&N(e.prev,e,e.next)>0)}function N(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function O(t,e){return t.x===e.x&&t.y===e.y}function z(t,e,r,i){var o=G(N(t,e,r)),n=G(N(t,e,i)),a=G(N(r,i,t)),s=G(N(r,i,e));return o!==n&&a!==s||(!(0!==o||!X(t,r,e))||(!(0!==n||!X(t,i,e))||(!(0!==a||!X(r,t,i))||!(0!==s||!X(r,e,i)))))}function X(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function G(t){return t>0?1:t<0?-1:0}function H(t,e){return N(t.prev,t,t.next)<0?N(t,e,t.next)>=0&&N(t,t.prev,e)>=0:N(t,e,t.prev)<0||N(t,t.next,e)<0}function W(t,e){var r=new Y(t.i,t.x,t.y),i=new Y(e.i,e.x,e.y),o=t.next,n=e.prev;return t.next=e,e.prev=t,r.next=o,o.prev=r,i.next=r,r.prev=i,n.next=i,i.prev=n,i}function j(t,e,r,i){var o=new Y(t,e,r);return i?(o.next=i.next,o.prev=i,i.next.prev=o,i.next=o):(o.prev=o,o.next=o),o}function Z(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function Y(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function K(t,e,r,i){for(var o=0,n=e,a=r-i;n"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:o}=e;if("MultiPolygon"===r.type)for(let i=0;i"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,i=y.flatten(e.coordinates),o=new Float32Array(i.vertices.map(((t,e)=>{return e%2==1?(r=t,(180-180/Math.PI*Math.log(Math.tan(Math.PI/4+r*Math.PI/360)))/360):(180+t)/360;var r}))),n=y(i.vertices,i.holes,i.dimensions),a=n.length>256?new Uint16Array(n):new Uint8Array(n),s=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),u=r.highlight||!1;let h=0,l=0,c=0;for(let t=0;t0&&(i+=t[o-1].length,r.holes.push(i))}return r};const V=(t,e)=>{const{lat:r,lng:o}=t;return new i(((t,e)=>(t+180)/360*Math.pow(2,e)*d)(o,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*d)(r,e))},q=(t,e)=>{return new c((r=t.y,i=e,o=Math.PI-2*Math.PI*r/d/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(o)-Math.exp(-o)))),function(t,e){return t/d/Math.pow(2,e)*360-180}(t.x,e));var r,i,o};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new a(new i(0,0),new i(0,0)),DEMPixelBounds:new a(new i(0,0),new i(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=t=>T(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:o,dsmSource:n,tileLoaded:s,gl:h,bounds:l,buildingRasterizer:c,tileMerger:f,forceUpdate:m=!1}=t,{getSourceUrl:_,getElevation:g,maxZoom:x,tileSize:p,_overzoom:E}=o,v=(y={getFeatures:r},T(void 0,void 0,void 0,(function*(){const{getFeatures:t}=y;try{return k(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var y;try{const t=l,r=t.getNorthWest(),o=t.getSouthEast(),E=new a(V(r,e),V(o,e));let T=new i(E.min.x,E.min.y),y=E.max.x-E.min.x;const b=E.max.y-E.min.y,R=E.max.subtract(E.min);R.y>R.x&&(T.x-=d,y+=512);const A=(t=>{const{upperLeft:e,width:r,height:i}=t,o=e.divideBy(d).floor().multiplyBy(d),n=(Math.ceil(r/d)+1)*d,s=(Math.ceil(i/d)+1)*d,u=o.add([n,s]);return new a(o,u)})({upperLeft:T,width:y,height:b}),{x:M,y:w}=A.max.subtract(A.min),F=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.xe.y{const i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:(t.x-e)*d,yOffset:(t.y-r)*d}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:o}=t,n=e.divideBy(d).floor(),a=n.x+r/d,s=Math.min(n.y+i/d,Math.pow(2,o)-1),u=[];for(var h=n.x;h2*t-1)),z=yield v,{maxHeight:X,heightMapTex:G}=c.raster({upperLeftTile:F[0],width:M,height:w,mapZoom:e,features:z,imageData:C,gl:h,dsmSource:n,dsmCoords:O}),H=Math.max(n.maxHeight,C.maxHeight+X);$=P,J={heightMapTex:G,maxHeight:H,width:M,height:w,DEMPixelBounds:A,visibleDEMPixelBounds:E,raster:F,demZoom:e,dirty:!0,outputWidth:D,outputHeight:S}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:i,b:o}=t;return[r/255,i/255,o/255,e]},et=(t,e)=>{const{date:r}=e,{dec:i,Hi:o}=_(r);t.updateDate({dec:i,Hi:o})},rt=(t,e)=>T(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),it=(t,e)=>{const{color:r,opacity:i}=e,o=tt(r,i);t.updateColor({colorVec:o})},ot=t=>{const{kernel:e,map:r,heightMap:i,now:o,color:n,opacity:a,belowCanopy:s,skipRender:h}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:c,maxHeight:f,width:m,height:g,DEMPixelBounds:x,visibleDEMPixelBounds:p,demZoom:E}=i;if(0===m||0===g)return;const{min:v,max:T}=p;if(!v||!T)return;const{x:y,y:b}=T.subtract(v),R=x.min,A=r.getPixelDimensions(),M=r.screenUnproject([0,0]),w=r.screenUnproject([A.width,0]),F=r.screenUnproject([A.width,A.height]),L=r.screenUnproject([0,A.height]),U=[L,F,M,w].map((t=>r.project(t,E))),D=U.map((t=>[(t.x-R.x)/m,(t.y-R.y)/g])).flat(),S=U.map((t=>[(t.x-v.x)/y,(t.y-v.y)/b])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),P=R.y/d/Math.pow(2,E),C=g/d/Math.pow(2,E),I=new u(q(x.getTopLeft(),E),q(x.getBottomRight(),E)),B=I.getWest(),N=Math.abs(I.getWest()-I.getEast());((t,e)=>{const{color:r,opacity:i,date:o}=e,n=tt(r,i),{dec:a,Hi:s}=_(o);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:f,width:m,height:g,heightMapZoom:E,cornerTextureCoords:D,cornerClipCoords:S,topYCoord:P,ySize:C,west:B,dLng:N,date:o,color:n,opacity:a,outputHeight:l,outputWidth:c,belowCanopy:s,skipRender:h})}catch(t){console.error("EXCEPTION",t)}};const nt=t=>{const{gl:e}=t,{texture:r,imageData:i=null,format:o=e.RGBA,width:n,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:h=e.UNSIGNED_BYTE,internalFormat:l=o}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,l,n,a,0,o,h,i)};function at(t){const{gl:e,vSrc:r,fSrc:i}=t,o=e.createShader(e.VERTEX_SHADER);e.shaderSource(o,r),e.compileShader(o);const n=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(n,i),e.compileShader(n);const a=e.createProgram();return e.attachShader(a,o),e.attachShader(a,n),e.linkProgram(a),a}class st{constructor(t){this.gl=t,this.program=at({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width:n,height:a,imageData:s,dsmCoords:u,dsmSource:h,gl:l}=t,{x:c,y:f,z:m}=r;let _=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1),l.bindTexture(l.TEXTURE_2D,s);const g=n,x=a;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,g,x,0,l.RGBA,l.UNSIGNED_BYTE,null),o>15&&e.length>5?(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST)):(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.LINEAR),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.LINEAR)),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.viewport(0,0,g,x);const p=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,p);const E=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,E,l.TEXTURE_2D,this.targetTexture,0),l.disable(l.BLEND),l.bindBuffer(l.ARRAY_BUFFER,this.positionBuffer),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,this.indexBuffer),l.clearColor(0,0,0,0),l.clear(l.COLOR_BUFFER_BIT);const v=c/Math.pow(2,m),T=f/Math.pow(2,m),y=new i(v,T),b=n/d/Math.pow(2,m),R=a/d/Math.pow(2,m);l.uniform3f(this.xyzUniformLocation,v,T,m),l.uniform2f(this.dimensionsUniformLocation,b,R),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const A=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,A,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.drawArrays(l.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:s,highlight:u}=t,h=new i(s[0],s[1]).subtract(y).unscaleBy(new i(b,R)).scaleBy(new i(n,a)).floor();if(h.x<0||h.y<0||h.x>n||h.y>a)return;const c=h.x/n,f=h.y/a,d=5*e,m=Math.floor(d/255)/255,g=Math.floor(d%255)/255;_=Math.max(_,e),l.uniform2f(this.centroidUniformLocation,c,f),l.uniform4f(this.colorUniformLocation,m,g,m,g),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ELEMENT_ARRAY_BUFFER,o,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,o.length,o.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),0!==h.data.length){l.activeTexture(l.TEXTURE1);const t=l.createTexture();l.bindTexture(l.TEXTURE_2D,t),l.pixelStorei(l.UNPACK_ALIGNMENT,2),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST),l.texImage2D(l.TEXTURE_2D,0,l.LUMINANCE_ALPHA,h.width,h.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,h.data),l.pixelStorei(l.UNPACK_ALIGNMENT,4);const e=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],r=new Float32Array(e);l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,r,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.enableVertexAttribArray(this.dsmAttributeLocation),l.bindBuffer(l.ARRAY_BUFFER,this.dsmBuffer),l.bufferData(l.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),l.STATIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.uniform1i(this.useDSMUniformLocation,1),l.colorMask(!1,!1,!0,!0),l.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.colorMask(!0,!0,!0,!0),l.deleteFramebuffer(p),{maxHeight:_,heightMapTex:this.targetTexture}}}const ut=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:o,y:n,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);o=Math.floor(o/t),n=Math.floor(n/t),a=r}return{x:i===d?o:Math.floor(o/2),y:i===d?n:Math.floor(n/2),z:i===d?a:a-1}};class ht{constructor(t){this.gl=t,this.program=at({vSrc:"\n attribute vec4 a_tex_position;\n attribute vec4 a_tile_position;\n varying vec2 v_tex_pos;\n void main() {\n v_tex_pos = a_tex_position.xy;\n gl_Position = a_tile_position;\n }\n ",fSrc:"\n precision mediump float;\n uniform sampler2D u_texture;\n uniform float u_encoding;\n uniform float u_tile_size;\n varying vec2 v_tex_pos;\n\n float getElevationTerrarium(vec4 color) {\n float decoded = color.r * 255.0 * 256.0 + color.g * 255.0 + color.b * 255.0 / 256.0 - 32768.0;\n return decoded;\n }\n\n float getElevationMapbox(vec4 color) {\n float decoded = -10000.0 + (color.r * 255.0 * 256.0 * 256.0 + color.g * 255.0 * 256.0 + color.b * 255.0) * .1;\n return decoded;\n }\n\n float getElevationSwitzerland(vec4 color) {\n float decoded = ((color.r * 255.0 * 256.0 + color.g * 255.0) / pow(2.0, 16.0)) * 4808.0;\n return decoded;\n }\n\n float getElevation(vec4 color, float encoding) {\n float decoded;\n if (encoding == 1.0) {\n decoded = getElevationMapbox(color);\n } else if (encoding == 2.0) {\n decoded = getElevationSwitzerland(color);\n } else {\n decoded = getElevationTerrarium(color);\n }\n float scaled = decoded * 5.0;\n float elevation = max(scaled, 0.0);\n return elevation;\n }\n\n float textureSampleLinear(sampler2D sampler, vec2 texCoord) {\n vec2 texelSize = vec2(1.0 / u_tile_size, 1.0 / u_tile_size);\n \n // Calculate interpolation weights based on texCoord fract (fractional part)\n vec2 uv = fract(texCoord * u_tile_size);\n \n // Sample four nearest texels\n vec4 bottomLeft = texture2D(sampler, texCoord);\n vec4 bottomRight = texture2D(sampler, texCoord + vec2(texelSize.x, 0.0));\n vec4 topLeft = texture2D(sampler, texCoord + vec2(0.0, texelSize.y));\n vec4 topRight = texture2D(sampler, texCoord + texelSize);\n\n float bottomLeftEl = getElevation(bottomLeft, u_encoding);\n float bottomRightEl = getElevation(bottomRight, u_encoding);\n float topLeftEl = getElevation(topLeft, u_encoding);\n float topRightEl = getElevation(topRight, u_encoding);\n \n // Interpolate between bottom and top rows\n float bottom = mix(bottomLeftEl, bottomRightEl, uv.x);\n float top = mix(topLeftEl, topRightEl, uv.x);\n \n // Interpolate final color\n return mix(bottom, top, uv.y);\n }\n\n void main() {\n float elevation = textureSampleLinear(u_texture, v_tex_pos);\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n gl_FragColor = vec4(0, 0, r, g);\n\n // vec4 color = texture2D(u_texture, v_tex_pos);\n // float elevation = getElevation(color, u_encoding);\n // float r = floor(elevation / 255.0) / 255.0;\n // float g = floor(mod(elevation, 255.0)) / 255.0;\n // gl_FragColor = vec4(0, 0, r, g);\n // gl_FragColor = color;\n }\n ",gl:t}),t.useProgram(this.program),this.texPositionAttributeLocation=t.getAttribLocation(this.program,"a_tex_position"),this.tilePositionAttributeLocation=t.getAttribLocation(this.program,"a_tile_position"),this.encodingUniformLocation=t.getUniformLocation(this.program,"u_encoding"),this.tileSizeUniformLocation=t.getUniformLocation(this.program,"u_tile_size"),this.texPositionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.texPositionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.texPositionAttributeLocation),t.vertexAttribPointer(this.texPositionAttributeLocation,2,t.FLOAT,!1,0,0),this.tilePositionBuffer=t.createBuffer(),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return T(this,void 0,void 0,(function*(){const r=this.gl;let i=0;const{width:o,height:n,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:h,tileLoaded:l,maxZoom:c}=e;if(r){this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const e=new Set;t.forEach((t=>{e.add(s(ut({tile:t,maxZoom:c,tileSize:h})))})),r.useProgram(this.program);let f=0;-32768===u({r:0,g:0,b:0,a:0})&&(f=0),-1e4===u({r:0,g:0,b:0,a:0})&&(f=1),4808===u({r:256,g:0,b:0,a:0})&&(f=2),r.uniform1f(this.encodingUniformLocation,f),nt({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:o,height:n,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST}),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const m=r.checkFramebufferStatus(r.FRAMEBUFFER);m!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+m),r.clear(r.COLOR_BUFFER_BIT);const _=Array.from(e).map((e=>T(this,void 0,void 0,(function*(){return new Promise(((u,l)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{r.useProgram(this.program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.tileTexture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,f),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer);const a=t.filter((t=>e===s(ut({tile:t,maxZoom:c,tileSize:h})))),l=a.reduce(((t,e)=>Math.min(t,e.xOffset)),1/0),m=a.reduce(((t,e)=>Math.min(t,e.yOffset)),1/0),_=a.reduce(((t,e)=>Math.max(t,e.xOffset)),0),g=a.reduce(((t,e)=>Math.max(t,e.yOffset)),0),x=a.reduce(((t,e)=>Math.min(t,e.x)),1/0),p=a.reduce(((t,e)=>Math.min(t,e.y)),1/0),E=a.reduce(((t,e)=>Math.max(t,e.x)),0),v=a.reduce(((t,e)=>Math.max(t,e.y)),0),T=a[0].z,y=l/o,b=m/n,R=(_+d)/o,A=(g+d)/n,M=[y,b,R,b,y,A,R,A].map((t=>2*t-1));r.bindBuffer(r.ARRAY_BUFFER,this.tilePositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(M),r.STATIC_DRAW),r.enableVertexAttribArray(this.tilePositionAttributeLocation),r.vertexAttribPointer(this.tilePositionAttributeLocation,2,r.FLOAT,!1,0,0);let w=[0,0,1,0,0,1,1,1];const F=Math.pow(2,Math.max(0,T-c)+(h!==d?1:0)),L=514===h?1/h:0,U=1/F,D=x%F/F,S=p%F/F,P=E%F/F,C=v%F/F;w=[D,S,P+U,S,D,C+U,P+U,C+U].map((t=>t*(514===h?512/514:1)+L)),r.bindBuffer(r.ARRAY_BUFFER,this.texPositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(w),r.STATIC_DRAW),r.enableVertexAttribArray(this.texPositionAttributeLocation),r.vertexAttribPointer(this.texPositionAttributeLocation,2,r.FLOAT,!1,0,0),r.uniform1f(this.tileSizeUniformLocation,h),r.viewport(0,0,o,n),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const I=Math.floor(y*o),B=Math.floor(b*n),N=Math.ceil((R-y)*o),O=Math.ceil((A-b)*n),z=new Uint8Array(4*N*O);r.readPixels(I,B,N,O,r.RGBA,r.UNSIGNED_BYTE,z);for(let t=0;t{if(f.src!==f.originalSource)return l("new tiles requested");u(null)},f.crossOrigin=a||null,f.src=e,f.originalSource=f.src})).then((()=>{this.finished++,l(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(_)}catch(t){return console.log(`${_.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=i,this.outputTexture}throw new Error("Could not get canvas context for merging tile images")}))}}return class extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>T(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>T(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(et(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=E(this.options.date)-E(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(it(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(it(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return T(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:i=new Date,iterations:o=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:o}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime(){const e=V(t,r);return[(e.x-n.x)/i,(e.y-n.y)/o]}))}return[]}_getBounds(t,e){const{width:r,height:o}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(o))return t.getBounds();const n=t.getCenter(),a=V(n,e),s=q(new i(a.x-r/2,a.y-o/2),e),u=q(new i(a.x+r/2,a.y+o/2),e);return t.createBounds({nw:s,se:u})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return T(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=v(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),o=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===o)).map((t=>t.canonical)),[a,s,u,h]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),l=q(new i(512*a,512*s),o+1),c=q(new i(512*(u+1),512*(h+1)),o+1);e=o+1,this._bounds=t.createBounds({nw:l,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return T(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(ot({kernel:this._compiledKernel,map:v(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,i){const o=new Uint8Array(r*i*4);return this._gl&&this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,o),o}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),i=t*e*1,o=1*t,n=(e-1)*o,a=new Uint8Array(i),s=new Uint8Array(i);let u=0;const{startDate:h,endDate:l}=this.options.sunExposure;this.options.sunExposure.enabled&&h&&l&&(u=l.getTime()-h.getTime());for(let t=0;t{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=E(t.startDate)-E(t.startDate,t.tzId),r=t.startDate.getTime()-e,i=t.endDate.getTime()-e,o=this._lngLatToTextureCoords([t.location])[0],{output:n,outputWidth:a,outputHeight:s}=((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:i,texCoord:o}));return n.toArray=function(){const e=new Array;for(let r=0;r=0;e--){const o=e*a*4+4*r,n=this.slice(o,o+4);i.push(n.join("")===t.sunColor.join("")?1:0)}e.push(i)}return e},{data:n,width:a,height:s}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:i,endDate:o}=this.options.sunExposure,n=o.getTime()-i.getTime(),a=p(r,.5,n);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t),this.id="shademap-layer",this.type="custom",this.canvasSourceId="canvas-source",this.attributionSourceId="attribution-source",this.canvasLayerId="canvas-layer",this.attributionLayerId="attribution-layer",this._refreshing=0,this._raf=0,this.id=this.id+x(),this.canvasSourceId=this.canvasSourceId+x(),this.attributionSourceId=this.attributionSourceId+x(),this.canvasLayerId=this.canvasLayerId+x(),this.attributionLayerId=this.attributionLayerId+x(),this.options.terrainSource.tileSize>256&&(this.options.terrainSource.maxZoom=this.options.terrainSource.maxZoom+1),this._moveEndHandler=()=>{this._map&&this._reset()}}render(t,e){}addTo(t){return t.addLayer(this),this}onAdd(t){this._map=t,this._gl=this._map.painter.context.gl,this._framebuffer=this._gl.createFramebuffer();this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,i=e,o=at({gl:i,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_outputSunExposure;uniform bool u_outputShadeProfile;uniform sampler2D u_decHiTexture;uniform sampler2D u_gpxTexture;uniform bool u_outputLocationShadeProfile;uniform vec2 u_shadeProfileLocation;float kernelResult;void kernel(){float sunDec=user_dec;float sunHi=user_Hi;vec4 shade_color=user_color;float maxHeight=user_maxHeight/1000.0;float user_x=vTexCoordCropped.x;float user_y=vTexCoordCropped.y;if(u_outputShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;vec4 gpx=texture2D(u_gpxTexture,vec2(vTexCoordFull.x,.5));user_x=gpx.x;user_y=gpx.y;}if(u_outputLocationShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;user_x=u_shadeProfileLocation.x;user_y=u_shadeProfileLocation.y;}float user_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitudeiter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_outputSunExposure){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));float timeInSun=1.0-aggregateColor.r;float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,0.5);}}void main(void){kernel();gl_FragColor=actualColor;}"});i.useProgram(o);const n=i.createBuffer(),a=i.getAttribLocation(o,"a_pos"),s=i.createBuffer(),u=i.getAttribLocation(o,"a_tex_pos"),h=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,h),i.bufferData(i.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),i.STATIC_DRAW);const l=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,l),i.bufferData(i.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),i.STATIC_DRAW);const c=i.getUniformLocation(o,"user_a");i.uniform1i(c,0);const f=i.getUniformLocation(o,"user_width"),d=i.getUniformLocation(o,"user_height"),m=i.getUniformLocation(o,"user_maxHeight"),x=i.getUniformLocation(o,"user_zoom"),p=i.getUniformLocation(o,"user_topYCoord"),v=i.getUniformLocation(o,"user_ySize"),y=i.getUniformLocation(o,"user_step"),b=i.getUniformLocation(o,"user_west"),R=i.getUniformLocation(o,"user_dLng"),A=i.getUniformLocation(o,"user_dec"),M=i.getUniformLocation(o,"user_Hi"),w=i.getUniformLocation(o,"user_color"),F=i.getUniformLocation(o,"u_below_canopy"),L=i.getUniformLocation(o,"user_sunExposureTexture"),U=i.getUniformLocation(o,"user_outputSunExposure"),D=i.getUniformLocation(o,"u_outputShadeProfile"),S=i.getUniformLocation(o,"u_gpxTexture"),P=i.getUniformLocation(o,"u_decHiTexture"),C=i.getUniformLocation(o,"u_sunColor"),I=i.getUniformLocation(o,"u_outputLocationShadeProfile"),B=i.getUniformLocation(o,"u_shadeProfileLocation"),N=i.createTexture();let O=0,z=0,X=0,G=null;const H=()=>{z&&X&&(i.useProgram(o),i.bindBuffer(i.ARRAY_BUFFER,n),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,s),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),r(i,z,X),i.viewport(0,0,z,X),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4))};let W=0,j=0,Z=0,Y="";const K=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(i.activeTexture(i.TEXTURE3),i.uniform1i(P,3),t.startTime!==j||t.endTime!==Z||t.tzId!==Y){j=t.startTime,Z=t.endTime,Y=t.tzId;const o=[],n=828e5,a=E(new Date(j),Y);for(let t=0;t{const{dec:e,Hi:r}=t;i.useProgram(o),i.uniform1f(A,e),i.uniform1f(M,r),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},updateDateRange:t=>T(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:h,emit:l}=t;i.useProgram(o);const c=W=Date.now(),f=Math.floor((r.getTime()-e.getTime())/h),d=i.getUniform(o,w),m=z,g=X,x=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,m,g,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST);const p=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,p);const E=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,E,i.TEXTURE_2D,x,0);for(let t=0;t{window.requestAnimationFrame((()=>{i.useProgram(o),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindFramebuffer(i.FRAMEBUFFER,p);const l=i.checkFramebufferStatus(i.FRAMEBUFFER);l!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+l);const{dec:c,Hi:d}=_(new Date(e.getTime()+f*t));i.uniform1f(A,c),i.uniform1f(M,d),i.uniform4fv(w,[1/h,0,0,1]),i.bindBuffer(i.ARRAY_BUFFER,n),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,s),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,m,g),i.enable(i.BLEND),i.blendFunc(i.ONE,i.ONE),i.drawArrays(i.TRIANGLE_STRIP,0,4),r()}))}))}return i.deleteFramebuffer(p),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(i.useProgram(o),c!==W)return i.deleteTexture(x),i.uniform4fv(w,d),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.uniform1i(L,2),i.uniform1i(U,1),H(),i.uniform1i(U,0),i.uniform1i(L,0),i.deleteTexture(x),i.uniform4fv(w,d),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;i.useProgram(o),i.uniform4fv(w,e),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},generateShadeProfile:t=>{i.useProgram(o);const{dates:e,texCoords:r,shadeColor:n,sunColor:s}=t,c=r.length,f=e.length,d=i.getUniform(o,w);i.uniform4fv(w,n),i.uniform4fv(C,s),i.uniform1i(D,1);const m=i.createTexture();i.activeTexture(i.TEXTURE3),nt({gl:i,imageData:null,width:c,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:m});const g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,m,0);const x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const p=r.map((t=>[t[0],t[1]])).flat(),E=i.createTexture();i.activeTexture(i.TEXTURE2),nt({gl:i,texture:E,imageData:new Float32Array(p),width:p.length/2,height:1,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT}),i.uniform1i(S,2);const v=e.map((t=>{const{dec:e,Hi:r}=_(t);return[-e,r/10]})).flat(),T=i.createTexture();i.activeTexture(i.TEXTURE1),nt({gl:i,texture:T,imageData:new Float32Array(v),width:1,height:e.length,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT}),i.uniform1i(P,1),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,c,f),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const y=new Uint8Array(c*f*4);return i.readPixels(0,0,c,f,i.RGBA,i.UNSIGNED_BYTE,y),i.deleteTexture(m),i.deleteTexture(T),i.deleteTexture(E),i.deleteFramebuffer(g),i.uniform1i(P,0),i.uniform1i(S,0),i.uniform1i(D,0),i.uniform4fv(w,d),i.uniform4fv(C,[0,0,0,0]),y},generateLocationShadeProfile:t=>{i.useProgram(o);const{startTime:e,endTime:r,tzId:n,texCoord:s,shadeColor:c,sunColor:f}=t,d=i.getUniform(o,w),{outputWidth:m,outputHeight:_}=K({startTime:e,endTime:r,tzId:n});i.uniform4fv(w,c),i.uniform4fv(C,f),i.uniform1i(I,1),i.uniform2fv(B,[s[0],s[1]]);const g=i.createTexture();i.activeTexture(i.TEXTURE2),nt({gl:i,imageData:null,width:m,height:_,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:g});const x=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,x),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,g,0);const p=i.checkFramebufferStatus(i.FRAMEBUFFER);p!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p),null!==G&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,G)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(a),i.vertexAttribPointer(a,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.viewport(0,0,m,_),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const E=new Uint8Array(m*_*4);return i.readPixels(0,0,m,_,i.RGBA,i.UNSIGNED_BYTE,E),i.deleteTexture(g),i.deleteFramebuffer(x),i.uniform1i(P,0),i.uniform1i(I,0),i.uniform4fv(w,d),i.uniform4fv(C,[0,0,0,0]),{output:E,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:(e,r,i)=>{const o=t.getSource(this.canvasSourceId).texture;e.activeTexture(e.TEXTURE1),o.bind(e.LINEAR,e.CLAMP_TO_EDGE),o.size=[r,i],e.texImage2D(e.TEXTURE_2D,0,e.RGBA,r,i,0,e.RGBA,e.UNSIGNED_BYTE,null),e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,o.texture,0),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA)}}),this._tileMerger=new ht(this._gl),this._buildingRasterizer=new st(this._gl),document.body.appendChild(this._canvas),this._canvas.style.display="none";const e=t.getBounds(),r=e.getNorthWest(),i=e.getNorthEast(),o=e.getSouthEast(),n=e.getSouthWest(),a=[[r.lng,r.lat],[i.lng,i.lat],[o.lng,o.lat],[n.lng,n.lat]];return t.addSource(this.canvasSourceId,{type:"canvas",canvas:this._canvas,coordinates:a,animate:!1}),t.addLayer({id:this.canvasLayerId,type:"raster",source:this.canvasSourceId,paint:{"raster-fade-duration":0}}),t.addSource(this.attributionSourceId,{type:"geojson",data:{type:"Feature",properties:{},geometry:{type:"Point",coordinates:[-122.210598,47.769799]}},attribution:'© ShadeMap'}),t.addLayer({id:this.attributionLayerId,type:"fill",source:this.attributionSourceId}),this._map.on("moveend",this._moveEndHandler),this._moveEndHandler(),this}onRemove(){return this._map&&(this._map.off("moveend",this._moveEndHandler),this._map.removeLayer(this.attributionLayerId),this._map.removeLayer(this.canvasLayerId),this._map.removeSource(this.attributionSourceId),this._map.removeSource(this.canvasSourceId)),this._gl&&this._framebuffer&&this._gl.deleteFramebuffer(this._framebuffer),document.body.removeChild(this._canvas),this.options.debug("onRemove called"),this}_getHeightMapCoords(t,e){if(this._map&&this.options.sunExposure.enabled&&this._bounds&&this._heightMap){const r=this._map.unproject([t,e]);if(r.toString()===this._map.unproject([t,e+1]).toString())return new i(-1,-1);const{visibleDEMPixelBounds:o,demZoom:n}=this._heightMap;return v(this._map).project(r,n).subtract(o.min)}return new i(-1,-1)}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this._getHeightMapCoords(t,e),i=this.readPixel(r.x,r.y),{startDate:o,endDate:n}=this.options.sunExposure,a=n.getTime()-o.getTime(),s=p(i,.5,a);return Math.abs(s/1e3/3600)}return 0}remove(){this._map&&this._map.removeLayer(this.id)}readPixel(t,e){const r=new Uint8Array(4);if(this._map&&this._gl&&this._framebuffer){const i=this._gl,o=this._map.getSource(this.canvasSourceId).texture;i.activeTexture(i.TEXTURE1),o.bind(i.LINEAR,i.CLAMP_TO_EDGE),i.bindFramebuffer(i.FRAMEBUFFER,this._framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,o.texture,0),this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r)}return r}readPixels(t,e,r,i){const o=new Uint8Array(r*i*4);if(this._map&&this._gl&&this._framebuffer){const n=this._gl,a=this._map.getSource(this.canvasSourceId).texture;n.activeTexture(n.TEXTURE1),a.bind(n.LINEAR,n.CLAMP_TO_EDGE),n.bindFramebuffer(n.FRAMEBUFFER,this._framebuffer),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,a.texture,0),this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,o)}return o}_flush(){if(this._map){this._map.getSource(this.canvasSourceId).fire({type:"data",dataType:"source",sourceDataType:"content"})}this.emit("idle")}_repositionCanvas(t){if(this._map){const e=this._map.getSource(this.canvasSourceId);if(e){const r=t.getNorthWest(),i=t.getNorthEast(),o=t.getSouthEast(),n=t.getSouthWest(),a=[[r.lng,r.lat],[i.lng,i.lat],[o.lng,o.lat],[n.lng,n.lat]];e.setCoordinates(a),this.emit("idle")}}return this}}})); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ShadeMap=e()}(this,(function(){"use strict";function t(t,e,r){var o=e[1],n=e[0],i=o-n;return t===o&&r?t:((t-n)%i+i)%i+n}function e(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var r=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function o(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var n=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function i(t,e,n){return t instanceof o?t:r(t)?new o(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new o(t.x,t.y):new o(t,e,n)}function a(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=s(t);var e=this.min,r=this.max,o=t.min,n=t.max,i=n.x>=e.x&&o.x<=r.x,a=n.y>=e.y&&o.y<=r.y;return i&&a},overlaps:function(t){t=s(t);var e=this.min,r=this.max,o=t.min,n=t.max,i=n.x>e.x&&o.xe.y&&o.y=o.lat&&r.lat<=n.lat&&e.lng>=o.lng&&r.lng<=n.lng},intersects:function(t){t=l(t);var e=this._southWest,r=this._northEast,o=t.getSouthWest(),n=t.getNorthEast(),i=n.lat>=e.lat&&o.lat<=r.lat,a=n.lng>=e.lng&&o.lng<=r.lng;return i&&a},overlaps:function(t){t=l(t);var e=this._southWest,r=this._northEast,o=t.getSouthWest(),n=t.getNorthEast(),i=n.lat>e.lat&&o.late.lng&&o.lng{const e=t.valueOf();return g(e)},g=t=>{const e=t/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,o=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,n=Math.atan2(Math.sin(o)*Math.cos(m),Math.cos(o));return{dec:Math.asin(Math.sin(m)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},p=()=>Math.floor(1e7*Math.random()),x=(t,e,r)=>{const o=1/e,n=Math.min(t[0]*o,255),i=Math.min(t[1]*o,255),a=Math.min(t[2]*o,255);let s=0;return n+i+a!==0&&(s=n>0?n/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},E=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),o=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-o.getTime()};const v=t=>{const e=()=>!t.getPitch;return{project:(r,n)=>{if(e())return t.project(r,n);{const{lat:t,lng:e}=r;return new o(((t,e)=>(t+180)/360*Math.pow(2,e)*d)(e,n),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*d)(t,n))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new c((n=r.y,i=o,a=Math.PI-2*Math.PI*n/d/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/d/Math.pow(2,e)*360-180}(r.x,o));var n,i,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new u(e,r)}}};function T(t,e,r,o){return new(r||(r=Promise))((function(n,i){function a(t){try{u(o.next(t))}catch(t){i(t)}}function s(t){try{u(o.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((o=o.apply(t,e||[])).next())}))}function y(t,e,r){r=r||2;var o,n,i,a,s,u,l,h=e&&e.length,c=h?e[0]*r:t.length,f=b(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(h&&(f=function(t,e,r,o){var n,i,a,s=[];for(n=0,i=e.length;n80*r){o=i=t[0],n=a=t[1];for(var m=r;mi&&(i=s),u>a&&(a=u);l=0!==(l=Math.max(i-o,a-n))?32767/l:0}return R(f,d,r,o,n,l,0),d}function b(t,e,r,o,n){var i,a;if(n===K(t,e,r,o)>0)for(i=e;i=e;i-=o)a=j(i,t[i],t[i+1],a);return a&&O(a,a.next)&&(Z(a),a=a.next),a}function A(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!O(o,o.next)&&0!==N(o.prev,o,o.next))o=o.next;else{if(Z(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function R(t,e,r,o,n,i,a){if(t){!a&&i&&function(t,e,r,o){var n=t;do{0===n.z&&(n.z=P(n.x,n.y,e,r,o)),n.prevZ=n.prev,n.nextZ=n.next,n=n.next}while(n!==t);n.prevZ.nextZ=null,n.prevZ=null,function(t){var e,r,o,n,i,a,s,u,l=1;do{for(r=t,t=null,i=null,a=0;r;){for(a++,o=r,s=0,e=0;e0||u>0&&o;)0!==s&&(0===u||!o||r.z<=o.z)?(n=r,r=r.nextZ,s--):(n=o,o=o.nextZ,u--),i?i.nextZ=n:t=n,n.prevZ=i,i=n;r=o}i.nextZ=null,l*=2}while(a>1)}(n)}(t,o,n,i);for(var s,u,l=t;t.prev!==t.next;)if(s=t.prev,u=t.next,i?w(t,o,n,i):M(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),Z(t),t=u.next,l=u.next;else if((t=u)===l){a?1===a?R(t=F(A(t),e,r),e,r,o,n,i,2):2===a&&L(t,e,r,o,n,i):R(A(t),e,r,o,n,i,1);break}}}function M(t){var e=t.prev,r=t,o=t.next;if(N(e,r,o)>=0)return!1;for(var n=e.x,i=r.x,a=o.x,s=e.y,u=r.y,l=o.y,h=ni?n>a?n:a:i>a?i:a,d=s>u?s>l?s:l:u>l?u:l,m=o.next;m!==e;){if(m.x>=h&&m.x<=f&&m.y>=c&&m.y<=d&&I(n,s,i,u,a,l,m.x,m.y)&&N(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function w(t,e,r,o){var n=t.prev,i=t,a=t.next;if(N(n,i,a)>=0)return!1;for(var s=n.x,u=i.x,l=a.x,h=n.y,c=i.y,f=a.y,d=su?s>l?s:l:u>l?u:l,g=h>c?h>f?h:f:c>f?c:f,p=P(d,m,e,r,o),x=P(_,g,e,r,o),E=t.prevZ,v=t.nextZ;E&&E.z>=p&&v&&v.z<=x;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==n&&E!==a&&I(s,h,u,c,l,f,E.x,E.y)&&N(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==n&&v!==a&&I(s,h,u,c,l,f,v.x,v.y)&&N(v.prev,v,v.next)>=0)return!1;v=v.nextZ}for(;E&&E.z>=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=g&&E!==n&&E!==a&&I(s,h,u,c,l,f,E.x,E.y)&&N(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;v&&v.z<=x;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=g&&v!==n&&v!==a&&I(s,h,u,c,l,f,v.x,v.y)&&N(v.prev,v,v.next)>=0)return!1;v=v.nextZ}return!0}function F(t,e,r){var o=t;do{var n=o.prev,i=o.next.next;!O(n,i)&&z(n,o,o.next,i)&&H(n,i)&&H(i,n)&&(e.push(n.i/r|0),e.push(o.i/r|0),e.push(i.i/r|0),Z(o),Z(o.next),o=t=i),o=o.next}while(o!==t);return A(o)}function L(t,e,r,o,n,i){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&B(a,s)){var u=W(a,s);return a=A(a,a.next),u=A(u,u.next),R(a,e,r,o,n,i,0),void R(u,e,r,o,n,i,0)}s=s.next}a=a.next}while(a!==t)}function U(t,e){return t.x-e.x}function D(t,e){var r=function(t,e){var r,o=e,n=t.x,i=t.y,a=-1/0;do{if(i<=o.y&&i>=o.next.y&&o.next.y!==o.y){var s=o.x+(i-o.y)*(o.next.x-o.x)/(o.next.y-o.y);if(s<=n&&s>a&&(a=s,r=o.x=o.x&&o.x>=h&&n!==o.x&&I(ir.x||o.x===r.x&&S(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=W(r,t);return A(o,o.next),A(r,r.next)}function S(t,e){return N(t.prev,t,e.prev)<0&&N(e.next,t,t.next)<0}function P(t,e,r,o,n){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*n|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-o)*n|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function C(t){var e=t,r=t;do{(e.x=(t-a)*(i-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(i-s)>=(n-a)*(o-s)}function B(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&z(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(H(t,e)&&H(e,t)&&function(t,e){var r=t,o=!1,n=(t.x+e.x)/2,i=(t.y+e.y)/2;do{r.y>i!=r.next.y>i&&r.next.y!==r.y&&n<(r.next.x-r.x)*(i-r.y)/(r.next.y-r.y)+r.x&&(o=!o),r=r.next}while(r!==t);return o}(t,e)&&(N(t.prev,t,e.prev)||N(t,e.prev,e))||O(t,e)&&N(t.prev,t,t.next)>0&&N(e.prev,e,e.next)>0)}function N(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function O(t,e){return t.x===e.x&&t.y===e.y}function z(t,e,r,o){var n=G(N(t,e,r)),i=G(N(t,e,o)),a=G(N(r,o,t)),s=G(N(r,o,e));return n!==i&&a!==s||(!(0!==n||!X(t,r,e))||(!(0!==i||!X(t,o,e))||(!(0!==a||!X(r,t,o))||!(0!==s||!X(r,e,o)))))}function X(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function G(t){return t>0?1:t<0?-1:0}function H(t,e){return N(t.prev,t,t.next)<0?N(t,e,t.next)>=0&&N(t,t.prev,e)>=0:N(t,e,t.prev)<0||N(t,t.next,e)<0}function W(t,e){var r=new Y(t.i,t.x,t.y),o=new Y(e.i,e.x,e.y),n=t.next,i=e.prev;return t.next=e,e.prev=t,r.next=n,n.prev=r,o.next=r,r.prev=o,i.next=o,o.prev=i,o}function j(t,e,r,o){var n=new Y(t,e,r);return o?(n.next=o.next,n.prev=o,o.next.prev=n,o.next=n):(n.prev=n,n.next=n),n}function Z(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function Y(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function K(t,e,r,o){for(var n=0,i=e,a=r-o;i"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:o,type:n}=e;if("MultiPolygon"===r.type)for(let o=0;o"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,o=y.flatten(e.coordinates),n=new Float32Array(o.vertices.map(((t,e)=>{return e%2==1?(r=t,(180-180/Math.PI*Math.log(Math.tan(Math.PI/4+r*Math.PI/360)))/360):(180+t)/360;var r}))),i=y(o.vertices,o.holes,o.dimensions),a=i.length>256?new Uint16Array(i):new Uint8Array(i),s=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return Math.max(e,o)}(r),u=r.highlight||!1;let l=0,h=0,c=0;for(let t=0;t0&&(o+=t[n-1].length,r.holes.push(o))}return r};const V=(t,e)=>{const{lat:r,lng:n}=t;return new o(((t,e)=>(t+180)/360*Math.pow(2,e)*d)(n,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*d)(r,e))},q=(t,e)=>{return new c((r=t.y,o=e,n=Math.PI-2*Math.PI*r/d/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/d/Math.pow(2,e)*360-180}(t.x,e));var r,o,n};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new a(new o(0,0),new o(0,0)),DEMPixelBounds:new a(new o(0,0),new o(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=t=>T(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:n,canopySource:i,dsmSource:s,tileLoaded:l,gl:h,bounds:c,buildingRasterizer:f,tileMerger:m,canopyMerger:_,forceUpdate:g=!1}=t,{getSourceUrl:p,getElevation:x,maxZoom:E,tileSize:v,_overzoom:y}=n,b=(A={getFeatures:r},T(void 0,void 0,void 0,(function*(){const{getFeatures:t}=A;try{return k(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var A;try{const t=c,r=t.getNorthWest(),n=t.getSouthEast(),T=new a(V(r,e),V(n,e));let y=new o(T.min.x,T.min.y),A=T.max.x-T.min.x;const R=T.max.y-T.min.y,M=T.max.subtract(T.min);M.y>M.x&&(y.x-=d,A+=512);const w=(t=>{const{upperLeft:e,width:r,height:o}=t,n=e.divideBy(d).floor().multiplyBy(d),i=(Math.ceil(r/d)+1)*d,s=(Math.ceil(o/d)+1)*d,u=n.add([i,s]);return new a(n,u)})({upperLeft:y,width:A,height:R}),{x:F,y:L}=w.max.subtract(w.min),U=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.xe.y{const o=Math.pow(2,t.z);return{x:(t.x%o+o)%o,y:(t.y%o+o)%o,z:t.z,xOffset:(t.x-e)*d,yOffset:(t.y-r)*d}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:n}=t,i=e.divideBy(d).floor(),a=i.x+r/d,s=Math.min(i.y+o/d,Math.pow(2,n)-1),u=[];for(var l=i.x;l{const e=h.createTexture();h.activeTexture(h.TEXTURE2),h.bindTexture(h.TEXTURE_2D,e),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,1,1,0,h.RGBA,h.UNSIGNED_BYTE,new Uint8ClampedArray([0,0,0,0])),t(e)})));const[N,O]=yield Promise.all(B);if(null===N||null===O)return J=Object.assign(Object.assign({},J),{visibleDEMPixelBounds:T,demZoom:e,dirty:!1}),J;const z=new a(V(new u(s.bounds).getNorthWest(),e),V(new u(s.bounds).getSouthEast(),e)),X=z.min.subtract(w.min),G=z.max.subtract(w.min),H=[X.x/F,X.y/L,G.x/F,G.y/L].map((t=>2*t-1)),W=yield b,{maxHeight:j,heightMapTex:Z}=f.raster({upperLeftTile:U[0],width:F,height:L,mapZoom:e,features:W,imageData:N,gl:h,dsmSource:s,dsmCoords:H,canopyData:O}),Y=Math.max(s.maxHeight,N.maxHeight+j);$=I,J={heightMapTex:Z,maxHeight:Y,width:F,height:L,DEMPixelBounds:w,visibleDEMPixelBounds:T,raster:U,demZoom:e,dirty:!0,outputWidth:P,outputHeight:C}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:o,b:n}=t;return[r/255,o/255,n/255,e]},et=(t,e)=>{const{date:r}=e,{dec:o,Hi:n}=_(r);t.updateDate({dec:o,Hi:n})},rt=(t,e)=>T(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ot=(t,e)=>{const{color:r,opacity:o}=e,n=tt(r,o);t.updateColor({colorVec:n})},nt=t=>{const{kernel:e,map:r,heightMap:o,now:n,color:i,opacity:a,belowCanopy:s,skipRender:l}=t;try{const{heightMapTex:t,outputHeight:h,outputWidth:c,maxHeight:f,width:m,height:g,DEMPixelBounds:p,visibleDEMPixelBounds:x,demZoom:E}=o;if(0===m||0===g)return;const{min:v,max:T}=x;if(!v||!T)return;const{x:y,y:b}=T.subtract(v),A=p.min,R=r.getPixelDimensions(),M=r.screenUnproject([0,0]),w=r.screenUnproject([R.width,0]),F=r.screenUnproject([R.width,R.height]),L=r.screenUnproject([0,R.height]),U=[L,F,M,w].map((t=>r.project(t,E))),D=U.map((t=>[(t.x-A.x)/m,(t.y-A.y)/g])).flat(),S=U.map((t=>[(t.x-v.x)/y,(t.y-v.y)/b])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),P=A.y/d/Math.pow(2,E),C=g/d/Math.pow(2,E),I=new u(q(p.getTopLeft(),E),q(p.getBottomRight(),E)),B=I.getWest(),N=Math.abs(I.getWest()-I.getEast());((t,e)=>{const{color:r,opacity:o,date:n}=e,i=tt(r,o),{dec:a,Hi:s}=_(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:i,step:1},e))})(e,{heightMapTex:t,maxHeight:f,width:m,height:g,heightMapZoom:E,cornerTextureCoords:D,cornerClipCoords:S,topYCoord:P,ySize:C,west:B,dLng:N,date:n,color:i,opacity:a,outputHeight:h,outputWidth:c,belowCanopy:s,skipRender:l})}catch(t){console.error("EXCEPTION",t)}};const it=t=>{const{gl:e}=t,{texture:r,imageData:o=null,format:n=e.RGBA,width:i,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:l=e.UNSIGNED_BYTE,internalFormat:h=n}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,h,i,a,0,n,l,o)};function at(t){const{gl:e,vSrc:r,fSrc:o}=t,n=e.createShader(e.VERTEX_SHADER);e.shaderSource(n,r),e.compileShader(n);const i=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(i,o),e.compileShader(i);const a=e.createProgram();return e.attachShader(a,n),e.attachShader(a,i),e.linkProgram(a),a}class st{constructor(t){this.gl=t,this.program=at({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nuniform sampler2D canopy_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n\t \tvec4 canopyColor = texture2D(canopy_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n float upper = textureColor.b + canopyColor.b;\n float combined = textureColor.a + canopyColor.a;\n if (combined > 1.0) {\n combined = combined - floor(combined);\n upper += 1.0 / 255.0;\n }\n gl_FragColor = vec4(textureColor.b, textureColor.a, upper, combined);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.canopyMapUniformLocation=t.getUniformLocation(this.program,"canopy_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:n,width:i,height:a,imageData:s,dsmCoords:u,dsmSource:l,canopyData:h,gl:c}=t,{x:f,y:m,z:_}=r;let g=0;c.useProgram(this.program),c.activeTexture(c.TEXTURE1),c.bindTexture(c.TEXTURE_2D,s),c.activeTexture(c.TEXTURE2),c.bindTexture(c.TEXTURE_2D,h);const p=i,x=a;c.activeTexture(c.TEXTURE0),c.bindTexture(c.TEXTURE_2D,this.targetTexture),c.texImage2D(c.TEXTURE_2D,0,c.RGBA,p,x,0,c.RGBA,c.UNSIGNED_BYTE,null),n>15&&e.length>5?(c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.NEAREST),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.NEAREST)):(c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.LINEAR),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.LINEAR)),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_S,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_T,c.CLAMP_TO_EDGE),c.viewport(0,0,p,x);const E=c.createFramebuffer();c.bindFramebuffer(c.FRAMEBUFFER,E);const v=c.COLOR_ATTACHMENT0;c.framebufferTexture2D(c.FRAMEBUFFER,v,c.TEXTURE_2D,this.targetTexture,0),c.disable(c.BLEND),c.bindBuffer(c.ARRAY_BUFFER,this.positionBuffer),c.bindBuffer(c.ELEMENT_ARRAY_BUFFER,this.indexBuffer),c.clearColor(0,0,0,0),c.clear(c.COLOR_BUFFER_BIT);const T=f/Math.pow(2,_),y=m/Math.pow(2,_),b=new o(T,y),A=i/d/Math.pow(2,_),R=a/d/Math.pow(2,_);c.uniform3f(this.xyzUniformLocation,T,y,_),c.uniform2f(this.dimensionsUniformLocation,A,R),c.uniform1i(this.heightMapUniformLocation,1),c.uniform1i(this.canopyMapUniformLocation,2),c.uniform1i(this.useDSMUniformLocation,0);const M=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(c.enableVertexAttribArray(this.positionAttributeLocation),c.bufferData(c.ARRAY_BUFFER,M,c.STATIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.uniform4f(this.colorUniformLocation,0,0,0,0),c.drawArrays(c.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:n,centroid:s,highlight:u}=t,l=new o(s[0],s[1]).subtract(b).unscaleBy(new o(A,R)).scaleBy(new o(i,a)).floor();if(l.x<0||l.y<0||l.x>i||l.y>a)return;const h=l.x/i,f=l.y/a,d=5*e,m=Math.floor(d/255)/255,_=Math.floor(d%255)/255;g=Math.max(g,e),c.uniform2f(this.centroidUniformLocation,h,f),c.uniform4f(this.colorUniformLocation,m,_,m,_),c.bufferData(c.ARRAY_BUFFER,r,c.DYNAMIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.bufferData(c.ARRAY_BUFFER,r,c.DYNAMIC_DRAW),c.vertexAttribPointer(this.dsmAttributeLocation,2,c.FLOAT,!1,0,0),c.bufferData(c.ELEMENT_ARRAY_BUFFER,n,c.DYNAMIC_DRAW),c.drawElements(c.TRIANGLES,n.length,n.length>256?c.UNSIGNED_SHORT:c.UNSIGNED_BYTE,0)})),0!==l.data.length){c.activeTexture(c.TEXTURE1);const t=c.createTexture();c.bindTexture(c.TEXTURE_2D,t),c.pixelStorei(c.UNPACK_ALIGNMENT,2),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_S,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_WRAP_T,c.CLAMP_TO_EDGE),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.NEAREST),c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.NEAREST),c.texImage2D(c.TEXTURE_2D,0,c.LUMINANCE_ALPHA,l.width,l.height,0,c.LUMINANCE_ALPHA,c.UNSIGNED_BYTE,l.data),c.pixelStorei(c.UNPACK_ALIGNMENT,4);const e=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],r=new Float32Array(e);c.enableVertexAttribArray(this.positionAttributeLocation),c.bufferData(c.ARRAY_BUFFER,r,c.STATIC_DRAW),c.vertexAttribPointer(this.positionAttributeLocation,2,c.FLOAT,!1,0,0),c.enableVertexAttribArray(this.dsmAttributeLocation),c.bindBuffer(c.ARRAY_BUFFER,this.dsmBuffer),c.bufferData(c.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),c.STATIC_DRAW),c.vertexAttribPointer(this.dsmAttributeLocation,2,c.FLOAT,!1,0,0),c.uniform4f(this.colorUniformLocation,0,0,0,0),c.uniform1i(this.useDSMUniformLocation,1),c.colorMask(!1,!1,!0,!0),c.drawArrays(c.TRIANGLE_STRIP,0,4),c.deleteTexture(t)}return c.colorMask(!0,!0,!0,!0),c.deleteFramebuffer(E),{maxHeight:g,heightMapTex:this.targetTexture}}}const ut=t=>{const{tile:e,maxZoom:r,tileSize:o}=t;let{x:n,y:i,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);n=Math.floor(n/t),i=Math.floor(i/t),a=r}return{x:o===d?n:Math.floor(n/2),y:o===d?i:Math.floor(i/2),z:o===d?a:a-1}};class lt{constructor(t){this.gl=t,this.program=at({vSrc:"\n attribute vec4 a_tex_position;\n attribute vec4 a_tile_position;\n varying vec2 v_tex_pos;\n void main() {\n v_tex_pos = a_tex_position.xy;\n gl_Position = a_tile_position;\n }\n ",fSrc:"\n precision mediump float;\n uniform sampler2D u_texture;\n uniform float u_encoding;\n uniform float u_tile_size;\n varying vec2 v_tex_pos;\n\n float getElevationTerrarium(vec4 color) {\n float decoded = color.r * 255.0 * 256.0 + color.g * 255.0 + color.b * 255.0 / 256.0 - 32768.0;\n return decoded;\n }\n\n float getElevationMapbox(vec4 color) {\n float decoded = -10000.0 + (color.r * 255.0 * 256.0 * 256.0 + color.g * 255.0 * 256.0 + color.b * 255.0) * .1;\n return decoded;\n }\n\n float getElevationSwitzerland(vec4 color) {\n float decoded = ((color.r * 255.0 * 256.0 + color.g * 255.0) / pow(2.0, 16.0)) * 4808.0;\n return decoded;\n }\n\n float getElevationCanopy(vec4 color) {\n float closestDistance = 1000.0;\n int closestColor = 100;\n const int colorCount = 26;\n vec3 colorArray[colorCount];\n\n colorArray[0] = vec3(51.0 / 255.0, 0, 66.0 / 255.0);\n colorArray[1] = vec3(51.0 / 255.0, 16.0/255.0, 86.0 / 255.0);\n colorArray[2] = vec3(51.0 / 255.0, 24.0/255.0, 86.0 / 255.0);\n colorArray[3] = vec3(51.0 / 255.0, 24.0/255.0, 98.0 / 255.0);\n colorArray[4] = vec3(51.0 / 255.0, 37.0/255.0, 108.0 / 255.0);\n colorArray[5] = vec3(48.0 / 255.0, 47.0/255.0, 114.0 / 255.0);\n colorArray[6] = vec3(44.0 / 255.0, 57.0/255.0, 116.0 / 255.0);\n colorArray[7] = vec3(42.0 / 255.0, 68.0/255.0, 117.0 / 255.0);\n colorArray[8] = vec3(39.0 / 255.0, 79.0/255.0, 120.0 / 255.0);\n colorArray[9] = vec3(37.0 / 255.0, 89.0/255.0, 123.0 / 255.0);\n colorArray[10] = vec3(34.0 / 255.0, 100.0/255.0, 120.0 / 255.0);\n colorArray[11] = vec3(34.0 / 255.0, 110.0/255.0, 121.0 / 255.0);\n colorArray[12] = vec3(30.0 / 255.0, 120.0/255.0, 120.0 / 255.0);\n colorArray[13] = vec3(32.0 / 255.0, 132.0/255.0, 117.0 / 255.0);\n colorArray[14] = vec3(36.0 / 255.0, 141.0/255.0, 112.0 / 255.0);\n colorArray[15] = vec3(40.0 / 255.0, 152.0/255.0, 108.0 / 255.0);\n colorArray[16] = vec3(44.0 / 255.0, 161.0/255.0, 101.0 / 255.0);\n colorArray[17] = vec3(52.0 / 255.0, 173.0/255.0, 95.0 / 255.0);\n colorArray[18] = vec3(69.0 / 255.0, 182.0/255.0, 84.0 / 255.0);\n colorArray[19] = vec3(88.0 / 255.0, 191.0/255.0, 72.0 / 255.0);\n colorArray[20] = vec3(109.0 / 255.0, 201.0/255.0, 61.0 / 255.0);\n colorArray[21] = vec3(130.0 / 255.0, 209.0/255.0, 51.0 / 255.0);\n colorArray[22] = vec3(158.0 / 255.0, 213.0/255.0, 45.0 / 255.0);\n colorArray[23] = vec3(189.0 / 255.0, 218.0/255.0, 40.0 / 255.0);\n colorArray[24] = vec3(219.0 / 255.0, 224.0/255.0, 36.0 / 255.0);\n colorArray[25] = vec3(252.0 / 255.0, 228.0/255.0, 30.0 / 255.0);\n\n for (int i = 0; i < colorCount; i++) {\n float distance = sqrt(pow(color.x - colorArray[i].x, 2.0) + pow(color.y - colorArray[i].y, 2.0) + pow(color.z - colorArray[i].z, 2.0));\n if (distance < closestDistance) {\n closestDistance = distance;\n closestColor = i;\n }\n }\n\n return float(closestColor);\n }\n\n float getElevation(vec4 color, float encoding) {\n float decoded;\n if (encoding == 1.0) {\n decoded = getElevationMapbox(color);\n } else if (encoding == 2.0) {\n decoded = getElevationSwitzerland(color);\n } else if (encoding == 3.0) {\n decoded = getElevationCanopy(color);\n } else {\n decoded = getElevationTerrarium(color);\n }\n float scaled = decoded * 5.0;\n float elevation = max(scaled, 0.0);\n return elevation;\n }\n\n float textureSampleLinear(sampler2D sampler, vec2 texCoord) {\n vec2 texelSize = vec2(1.0 / u_tile_size, 1.0 / u_tile_size);\n \n // Calculate interpolation weights based on texCoord fract (fractional part)\n vec2 uv = fract(texCoord * u_tile_size);\n \n // Sample four nearest texels\n vec4 bottomLeft = texture2D(sampler, texCoord);\n vec4 bottomRight = texture2D(sampler, texCoord + vec2(texelSize.x, 0.0));\n vec4 topLeft = texture2D(sampler, texCoord + vec2(0.0, texelSize.y));\n vec4 topRight = texture2D(sampler, texCoord + texelSize);\n\n float bottomLeftEl = getElevation(bottomLeft, u_encoding);\n float bottomRightEl = getElevation(bottomRight, u_encoding);\n float topLeftEl = getElevation(topLeft, u_encoding);\n float topRightEl = getElevation(topRight, u_encoding);\n \n // Interpolate between bottom and top rows\n float bottom = mix(bottomLeftEl, bottomRightEl, uv.x);\n float top = mix(topLeftEl, topRightEl, uv.x);\n \n // Interpolate final color\n return mix(bottom, top, uv.y);\n }\n\n void main() {\n float elevation = textureSampleLinear(u_texture, v_tex_pos);\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n gl_FragColor = vec4(0, 0, r, g);\n\n // vec4 color = texture2D(u_texture, v_tex_pos);\n // float elevation = getElevation(color, u_encoding);\n // float r = floor(elevation / 255.0) / 255.0;\n // float g = floor(mod(elevation, 255.0)) / 255.0;\n // gl_FragColor = vec4(0, 0, r, g);\n // gl_FragColor = color;\n }\n ",gl:t}),t.useProgram(this.program),this.texPositionAttributeLocation=t.getAttribLocation(this.program,"a_tex_position"),this.tilePositionAttributeLocation=t.getAttribLocation(this.program,"a_tile_position"),this.encodingUniformLocation=t.getUniformLocation(this.program,"u_encoding"),this.tileSizeUniformLocation=t.getUniformLocation(this.program,"u_tile_size"),this.texPositionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.texPositionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.texPositionAttributeLocation),t.vertexAttribPointer(this.texPositionAttributeLocation,2,t.FLOAT,!1,0,0),this.tilePositionBuffer=t.createBuffer(),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return T(this,void 0,void 0,(function*(){const r=this.gl;let o=0;const{width:n,height:i,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:l,tileLoaded:h,maxZoom:c}=e;if(r){this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const e=new Set;t.forEach((t=>{e.add(s(ut({tile:t,maxZoom:c,tileSize:l})))})),r.useProgram(this.program);let f=0;-32768===u({r:0,g:0,b:0,a:0})&&(f=0),-1e4===u({r:0,g:0,b:0,a:0})&&(f=1),4808===u({r:256,g:0,b:0,a:0})&&(f=2),0===u({r:51,g:0,b:66,a:0})&&(f=3),r.uniform1f(this.encodingUniformLocation,f),it({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:n,height:i,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST}),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const m=r.checkFramebufferStatus(r.FRAMEBUFFER);m!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+m),r.clear(r.COLOR_BUFFER_BIT);const _=Array.from(e).map((e=>T(this,void 0,void 0,(function*(){return new Promise(((u,h)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{r.useProgram(this.program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.tileTexture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,f),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer);const a=t.filter((t=>e===s(ut({tile:t,maxZoom:c,tileSize:l})))),h=a.reduce(((t,e)=>Math.min(t,e.xOffset)),1/0),m=a.reduce(((t,e)=>Math.min(t,e.yOffset)),1/0),_=a.reduce(((t,e)=>Math.max(t,e.xOffset)),0),g=a.reduce(((t,e)=>Math.max(t,e.yOffset)),0),p=a.reduce(((t,e)=>Math.min(t,e.x)),1/0),x=a.reduce(((t,e)=>Math.min(t,e.y)),1/0),E=a.reduce(((t,e)=>Math.max(t,e.x)),0),v=a.reduce(((t,e)=>Math.max(t,e.y)),0),T=a[0].z,y=h/n,b=m/i,A=(_+d)/n,R=(g+d)/i,M=[y,b,A,b,y,R,A,R].map((t=>2*t-1));r.bindBuffer(r.ARRAY_BUFFER,this.tilePositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(M),r.STATIC_DRAW),r.enableVertexAttribArray(this.tilePositionAttributeLocation),r.vertexAttribPointer(this.tilePositionAttributeLocation,2,r.FLOAT,!1,0,0);let w=[0,0,1,0,0,1,1,1];const F=Math.pow(2,Math.max(0,T-c)+(l!==d?1:0)),L=514===l?1/l:0,U=1/F,D=p%F/F,S=x%F/F,P=E%F/F,C=v%F/F;w=[D,S,P+U,S,D,C+U,P+U,C+U].map((t=>t*(514===l?512/514:1)+L)),r.bindBuffer(r.ARRAY_BUFFER,this.texPositionBuffer),r.bufferData(r.ARRAY_BUFFER,new Float32Array(w),r.STATIC_DRAW),r.enableVertexAttribArray(this.texPositionAttributeLocation),r.vertexAttribPointer(this.texPositionAttributeLocation,2,r.FLOAT,!1,0,0),r.uniform1f(this.tileSizeUniformLocation,l),r.viewport(0,0,n,i),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const I=Math.floor(y*n),B=Math.floor(b*i),N=Math.ceil((A-y)*n),O=Math.ceil((R-b)*i),z=new Uint8Array(4*N*O);r.readPixels(I,B,N,O,r.RGBA,r.UNSIGNED_BYTE,z);for(let t=0;t{if(f.src!==f.originalSource)return h("new tiles requested");u(null)},f.crossOrigin=a||null,f.src=e,f.originalSource=f.src})).then((()=>{this.finished++,h(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(_)}catch(t){return console.log(`${_.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=o,this.outputTexture}throw new Error("Could not get canvas context for merging tile images")}))}}return class extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},canopySource:void 0,dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>T(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>T(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(et(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=E(this.options.date)-E(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setCanopySource(t){return this.options.canopySource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return T(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:o=new Date,iterations:n=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:o,iterations:n}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime(){const e=V(t,r);return[(e.x-i.x)/o,(e.y-i.y)/n]}))}return[]}_getBounds(t,e){const{width:r,height:n}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(n))return t.getBounds();const i=t.getCenter(),a=V(i,e),s=q(new o(a.x-r/2,a.y-n/2),e),u=q(new o(a.x+r/2,a.y+n/2),e);return t.createBounds({nw:s,se:u})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return T(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=v(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),n=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),i=r.filter((t=>t.canonical.z===n)).map((t=>t.canonical)),[a,s,u,l]=i.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),h=q(new o(512*a,512*s),n+1),c=q(new o(512*(u+1),512*(l+1)),n+1);e=n+1,this._bounds=t.createBounds({nw:h,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,canopySource:this.options.canopySource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,canopyMerger:this._canopyMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return T(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(nt({kernel:this._compiledKernel,map:v(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const n=new Uint8Array(r*o*4);return this._gl&&this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n),n}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),o=t*e*1,n=1*t,i=(e-1)*n,a=new Uint8Array(o),s=new Uint8Array(o);let u=0;const{startDate:l,endDate:h}=this.options.sunExposure;this.options.sunExposure.enabled&&l&&h&&(u=h.getTime()-l.getTime());for(let t=0;t{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=E(t.startDate)-E(t.startDate,t.tzId),r=t.startDate.getTime()-e,o=t.endDate.getTime()-e,n=this._lngLatToTextureCoords([t.location])[0],{output:i,outputWidth:a,outputHeight:s}=((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord:n}));return i.toArray=function(){const e=new Array;for(let r=0;r=0;e--){const n=e*a*4+4*r,i=this.slice(n,n+4);o.push(i.join("")===t.sunColor.join("")?1:0)}e.push(o)}return e},{data:i,width:a,height:s}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:o,endDate:n}=this.options.sunExposure,i=n.getTime()-o.getTime(),a=x(r,.5,i);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t),this.id="shademap-layer",this.type="custom",this.canvasSourceId="canvas-source",this.attributionSourceId="attribution-source",this.canvasLayerId="canvas-layer",this.attributionLayerId="attribution-layer",this._refreshing=0,this._raf=0,this.id=this.id+p(),this.canvasSourceId=this.canvasSourceId+p(),this.attributionSourceId=this.attributionSourceId+p(),this.canvasLayerId=this.canvasLayerId+p(),this.attributionLayerId=this.attributionLayerId+p(),this.options.terrainSource.tileSize>256&&(this.options.terrainSource.maxZoom=this.options.terrainSource.maxZoom+1),this._moveEndHandler=()=>{this._map&&this._reset()}}render(t,e){}addTo(t){return t.addLayer(this),this}onAdd(t){this._map=t,this._gl=this._map.painter.context.gl,this._framebuffer=this._gl.createFramebuffer();this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,o=e,n=at({gl:o,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_outputSunExposure;uniform bool u_outputShadeProfile;uniform sampler2D u_decHiTexture;uniform sampler2D u_gpxTexture;uniform bool u_outputLocationShadeProfile;uniform vec2 u_shadeProfileLocation;float kernelResult;void kernel(){float sunDec=user_dec;float sunHi=user_Hi;vec4 shade_color=user_color;float maxHeight=user_maxHeight/1000.0;float user_x=vTexCoordCropped.x;float user_y=vTexCoordCropped.y;if(u_outputShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;vec4 gpx=texture2D(u_gpxTexture,vec2(vTexCoordFull.x,.5));user_x=gpx.x;user_y=gpx.y;}if(u_outputLocationShadeProfile==true){vec4 decHi=texture2D(u_decHiTexture,vec2(vTexCoordFull.x,1.0-vTexCoordFull.y));sunDec=-decHi.r;sunHi=decHi.g*10.0;user_x=u_shadeProfileLocation.x;user_y=u_shadeProfileLocation.y;}float user_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitudeiter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_outputSunExposure){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));float timeInSun=1.0-aggregateColor.r;float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,0.5);}}void main(void){kernel();gl_FragColor=actualColor;}"});o.useProgram(n);const i=o.createBuffer(),a=o.getAttribLocation(n,"a_pos"),s=o.createBuffer(),u=o.getAttribLocation(n,"a_tex_pos"),l=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,l),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW);const h=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,h),o.bufferData(o.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),o.STATIC_DRAW);const c=o.getUniformLocation(n,"user_a");o.uniform1i(c,0);const f=o.getUniformLocation(n,"user_width"),d=o.getUniformLocation(n,"user_height"),m=o.getUniformLocation(n,"user_maxHeight"),p=o.getUniformLocation(n,"user_zoom"),x=o.getUniformLocation(n,"user_topYCoord"),v=o.getUniformLocation(n,"user_ySize"),y=o.getUniformLocation(n,"user_step"),b=o.getUniformLocation(n,"user_west"),A=o.getUniformLocation(n,"user_dLng"),R=o.getUniformLocation(n,"user_dec"),M=o.getUniformLocation(n,"user_Hi"),w=o.getUniformLocation(n,"user_color"),F=o.getUniformLocation(n,"u_below_canopy"),L=o.getUniformLocation(n,"user_sunExposureTexture"),U=o.getUniformLocation(n,"user_outputSunExposure"),D=o.getUniformLocation(n,"u_outputShadeProfile"),S=o.getUniformLocation(n,"u_gpxTexture"),P=o.getUniformLocation(n,"u_decHiTexture"),C=o.getUniformLocation(n,"u_sunColor"),I=o.getUniformLocation(n,"u_outputLocationShadeProfile"),B=o.getUniformLocation(n,"u_shadeProfileLocation"),N=o.createTexture();let O=0,z=0,X=0,G=null;const H=()=>{z&&X&&(o.useProgram(n),o.bindBuffer(o.ARRAY_BUFFER,i),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,s),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),r(o,z,X),o.viewport(0,0,z,X),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4))};let W=0,j=0,Z=0,Y="";const K=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(o.activeTexture(o.TEXTURE3),o.uniform1i(P,3),t.startTime!==j||t.endTime!==Z||t.tzId!==Y){j=t.startTime,Z=t.endTime,Y=t.tzId;const n=[],i=828e5,a=E(new Date(j),Y);for(let t=0;t{const{dec:e,Hi:r}=t;o.useProgram(n),o.uniform1f(R,e),o.uniform1f(M,r),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},updateDateRange:t=>T(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:l,emit:h}=t;o.useProgram(n);const c=W=Date.now(),f=Math.floor((r.getTime()-e.getTime())/l),d=o.getUniform(n,w),m=z,g=X,p=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,p),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,m,g,0,o.RGBA,o.UNSIGNED_BYTE,null),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MIN_FILTER,o.NEAREST),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MAG_FILTER,o.NEAREST);const x=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,x);const E=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,E,o.TEXTURE_2D,p,0);for(let t=0;t{window.requestAnimationFrame((()=>{o.useProgram(n),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindFramebuffer(o.FRAMEBUFFER,x);const h=o.checkFramebufferStatus(o.FRAMEBUFFER);h!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=_(new Date(e.getTime()+f*t));o.uniform1f(R,c),o.uniform1f(M,d),o.uniform4fv(w,[1/l,0,0,1]),o.bindBuffer(o.ARRAY_BUFFER,i),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,s),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,m,g),o.enable(o.BLEND),o.blendFunc(o.ONE,o.ONE),o.drawArrays(o.TRIANGLE_STRIP,0,4),r()}))}))}return o.deleteFramebuffer(x),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(o.useProgram(n),c!==W)return o.deleteTexture(p),o.uniform4fv(w,d),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,p),o.uniform1i(L,2),o.uniform1i(U,1),H(),o.uniform1i(U,0),o.uniform1i(L,0),o.deleteTexture(p),o.uniform4fv(w,d),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;o.useProgram(n),o.uniform4fv(w,e),window.cancelAnimationFrame(O),O=window.requestAnimationFrame(H)},generateShadeProfile:t=>{o.useProgram(n);const{dates:e,texCoords:r,shadeColor:i,sunColor:s}=t,c=r.length,f=e.length,d=o.getUniform(n,w);o.uniform4fv(w,i),o.uniform4fv(C,s),o.uniform1i(D,1);const m=o.createTexture();o.activeTexture(o.TEXTURE3),it({gl:o,imageData:null,width:c,height:f,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:m});const g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,m,0);const p=o.checkFramebufferStatus(o.FRAMEBUFFER);p!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p);const x=r.map((t=>[t[0],t[1]])).flat(),E=o.createTexture();o.activeTexture(o.TEXTURE2),it({gl:o,texture:E,imageData:new Float32Array(x),width:x.length/2,height:1,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT}),o.uniform1i(S,2);const v=e.map((t=>{const{dec:e,Hi:r}=_(t);return[-e,r/10]})).flat(),T=o.createTexture();o.activeTexture(o.TEXTURE1),it({gl:o,texture:T,imageData:new Float32Array(v),width:1,height:e.length,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT}),o.uniform1i(P,1),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindBuffer(o.ARRAY_BUFFER,l),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,c,f),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const y=new Uint8Array(c*f*4);return o.readPixels(0,0,c,f,o.RGBA,o.UNSIGNED_BYTE,y),o.deleteTexture(m),o.deleteTexture(T),o.deleteTexture(E),o.deleteFramebuffer(g),o.uniform1i(P,0),o.uniform1i(S,0),o.uniform1i(D,0),o.uniform4fv(w,d),o.uniform4fv(C,[0,0,0,0]),y},generateLocationShadeProfile:t=>{o.useProgram(n);const{startTime:e,endTime:r,tzId:i,texCoord:s,shadeColor:c,sunColor:f}=t,d=o.getUniform(n,w),{outputWidth:m,outputHeight:_}=K({startTime:e,endTime:r,tzId:i});o.uniform4fv(w,c),o.uniform4fv(C,f),o.uniform1i(I,1),o.uniform2fv(B,[s[0],s[1]]);const g=o.createTexture();o.activeTexture(o.TEXTURE2),it({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:g});const p=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,p),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,g,0);const x=o.checkFramebufferStatus(o.FRAMEBUFFER);x!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x),null!==G&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,G)),o.bindBuffer(o.ARRAY_BUFFER,l),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,2,o.FLOAT,!1,0,0),o.viewport(0,0,m,_),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const E=new Uint8Array(m*_*4);return o.readPixels(0,0,m,_,o.RGBA,o.UNSIGNED_BYTE,E),o.deleteTexture(g),o.deleteFramebuffer(p),o.uniform1i(P,0),o.uniform1i(I,0),o.uniform4fv(w,d),o.uniform4fv(C,[0,0,0,0]),{output:E,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:(e,r,o)=>{const n=t.getSource(this.canvasSourceId).texture;e.activeTexture(e.TEXTURE1),n.bind(e.LINEAR,e.CLAMP_TO_EDGE),n.size=[r,o],e.texImage2D(e.TEXTURE_2D,0,e.RGBA,r,o,0,e.RGBA,e.UNSIGNED_BYTE,null),e.bindFramebuffer(e.FRAMEBUFFER,this._framebuffer),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,n.texture,0),e.enable(e.BLEND),e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA)}}),this._tileMerger=new lt(this._gl),this._canopyMerger=new lt(this._gl),this._buildingRasterizer=new st(this._gl),document.body.appendChild(this._canvas),this._canvas.style.display="none";const e=t.getBounds(),r=e.getNorthWest(),o=e.getNorthEast(),n=e.getSouthEast(),i=e.getSouthWest(),a=[[r.lng,r.lat],[o.lng,o.lat],[n.lng,n.lat],[i.lng,i.lat]];return t.addSource(this.canvasSourceId,{type:"canvas",canvas:this._canvas,coordinates:a,animate:!1}),t.addLayer({id:this.canvasLayerId,type:"raster",source:this.canvasSourceId,paint:{"raster-fade-duration":0}}),t.addSource(this.attributionSourceId,{type:"geojson",data:{type:"Feature",properties:{},geometry:{type:"Point",coordinates:[-122.210598,47.769799]}},attribution:'© ShadeMap'}),t.addLayer({id:this.attributionLayerId,type:"fill",source:this.attributionSourceId}),this._map.on("moveend",this._moveEndHandler),this._moveEndHandler(),this}onRemove(){return this._map&&(this._map.off("moveend",this._moveEndHandler),this._map.removeLayer(this.attributionLayerId),this._map.removeLayer(this.canvasLayerId),this._map.removeSource(this.attributionSourceId),this._map.removeSource(this.canvasSourceId)),this._gl&&this._framebuffer&&this._gl.deleteFramebuffer(this._framebuffer),document.body.removeChild(this._canvas),this.options.debug("onRemove called"),this}_getHeightMapCoords(t,e){if(this._map&&this.options.sunExposure.enabled&&this._bounds&&this._heightMap){const r=this._map.unproject([t,e]);if(r.toString()===this._map.unproject([t,e+1]).toString())return new o(-1,-1);const{visibleDEMPixelBounds:n,demZoom:i}=this._heightMap;return v(this._map).project(r,i).subtract(n.min)}return new o(-1,-1)}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this._getHeightMapCoords(t,e),o=this.readPixel(r.x,r.y),{startDate:n,endDate:i}=this.options.sunExposure,a=i.getTime()-n.getTime(),s=x(o,.5,a);return Math.abs(s/1e3/3600)}return 0}remove(){this._map&&this._map.removeLayer(this.id)}readPixel(t,e){const r=new Uint8Array(4);if(this._map&&this._gl&&this._framebuffer){const o=this._gl,n=this._map.getSource(this.canvasSourceId).texture;o.activeTexture(o.TEXTURE1),n.bind(o.LINEAR,o.CLAMP_TO_EDGE),o.bindFramebuffer(o.FRAMEBUFFER,this._framebuffer),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,n.texture,0),this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r)}return r}readPixels(t,e,r,o){const n=new Uint8Array(r*o*4);if(this._map&&this._gl&&this._framebuffer){const i=this._gl,a=this._map.getSource(this.canvasSourceId).texture;i.activeTexture(i.TEXTURE1),a.bind(i.LINEAR,i.CLAMP_TO_EDGE),i.bindFramebuffer(i.FRAMEBUFFER,this._framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,a.texture,0),this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n)}return n}_flush(){if(this._map){this._map.getSource(this.canvasSourceId).fire({type:"data",dataType:"source",sourceDataType:"content"})}this.emit("idle")}_repositionCanvas(t){if(this._map){const e=this._map.getSource(this.canvasSourceId);if(e){const r=t.getNorthWest(),o=t.getNorthEast(),n=t.getSouthEast(),i=t.getSouthWest(),a=[[r.lng,r.lat],[o.lng,o.lat],[n.lng,n.lat],[i.lng,i.lat]];e.setCoordinates(a),this.emit("idle")}}return this}}}));