leaflet-shadow-simulator 0.61.0 → 0.61.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Copyright Ted Piotrowski 2025
3
3
  * Package: leaflet-shadow-simulator
4
- * Version: 0.61.0
4
+ * Version: 0.61.1
5
5
  * For licensing visit: https://shademap.app/about/
6
6
  */
7
7
 
8
- import{ImageOverlay as t,DomUtil as e,Util as r}from"leaflet";class o extends t{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const t="CANVAS"===this._url.tagName,o=this._image=t?this._url:e.create("canvas");return e.addClass(o,"leaflet-image-layer"),this._zoomAnimated&&e.addClass(o,"leaflet-zoom-animated"),this.options.className&&e.addClass(o,this.options.className),o.onselectstart=r.falseFn,o.onmousemove=r.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function i(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())})}class n{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}}function a(t,e,r){r=r||2;var o,i,n,a,u,h,c,f=e&&e.length,d=f?e[0]*r:t.length,g=s(t,0,d,r,!0),x=[];if(!g||g.next===g.prev)return x;if(f&&(g=function(t,e,r,o){var i,n,a,u=[];for(i=0,n=e.length;i<n;i++)(a=s(t,e[i]*o,i<n-1?e[i+1]*o:t.length,o,!1))===a.next&&(a.steiner=!0),u.push(p(a));for(u.sort(m),i=0;i<u.length;i++)r=_(u[i],r);return r}(t,e,g,r)),t.length>80*r){o=n=t[0],i=a=t[1];for(var E=r;E<d;E+=r)(u=t[E])<o&&(o=u),(h=t[E+1])<i&&(i=h),u>n&&(n=u),h>a&&(a=h);c=0!==(c=Math.max(n-o,a-i))?32767/c:0}return l(g,x,r,o,i,c,0),x}function s(t,e,r,o,i){var n,a;if(i===D(t,e,r,o)>0)for(n=e;n<r;n+=o)a=F(n,t[n],t[n+1],a);else for(n=r-o;n>=e;n-=o)a=F(n,t[n],t[n+1],a);return a&&y(a,a.next)&&(L(a),a=a.next),a}function u(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!y(o,o.next)&&0!==T(o.prev,o,o.next))o=o.next;else{if(L(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function l(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=x(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;e<l&&(s++,o=o.nextZ);e++);for(u=l;s>0||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,m,_=t;t.prev!==t.next;)if(s=t.prev,m=t.next,n?c(t,o,i,n):h(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),L(t),t=m.next,_=m.next;else if((t=m)===_){a?1===a?l(t=f(u(t),e,r),e,r,o,i,n,2):2===a&&d(t,e,r,o,i,n):l(u(t),e,r,o,i,n,1);break}}}function h(t){var e=t.prev,r=t,o=t.next;if(T(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=i<n?i<a?i:a:n<a?n:a,c=s<u?s<l?s:l:u<l?u:l,f=i>n?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&&E(i,s,n,u,a,l,m.x,m.y)&&T(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function c(t,e,r,o){var i=t.prev,n=t,a=t.next;if(T(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=s<u?s<l?s:l:u<l?u:l,m=h<c?h<f?h:f:c<f?c:f,_=s>u?s>l?s:l:u>l?u:l,g=h>c?h>f?h:f:c>f?c:f,p=x(d,m,e,r,o),v=x(_,g,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=p&&b&&b.z<=v;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==i&&y!==a&&E(s,h,u,c,l,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,b.x>=d&&b.x<=_&&b.y>=m&&b.y<=g&&b!==i&&b!==a&&E(s,h,u,c,l,f,b.x,b.y)&&T(b.prev,b,b.next)>=0)return!1;b=b.nextZ}for(;y&&y.z>=p;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==i&&y!==a&&E(s,h,u,c,l,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;b&&b.z<=v;){if(b.x>=d&&b.x<=_&&b.y>=m&&b.y<=g&&b!==i&&b!==a&&E(s,h,u,c,l,f,b.x,b.y)&&T(b.prev,b,b.next)>=0)return!1;b=b.nextZ}return!0}function f(t,e,r){var o=t;do{var i=o.prev,n=o.next.next;!y(i,n)&&b(i,o,o.next,n)&&M(i,n)&&M(n,i)&&(e.push(i.i/r|0),e.push(o.i/r|0),e.push(n.i/r|0),L(o),L(o.next),o=t=n),o=o.next}while(o!==t);return u(o)}function d(t,e,r,o,i,n){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&v(a,s)){var h=w(a,s);return a=u(a,a.next),h=u(h,h.next),l(a,e,r,o,i,n,0),void l(h,e,r,o,i,n,0)}s=s.next}a=a.next}while(a!==t)}function m(t,e){return t.x-e.x}function _(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.next.x?o:o.next,s===i))return r}o=o.next}while(o!==e);if(!r)return null;var u,l=r,h=r.x,c=r.y,f=1/0;o=r;do{i>=o.x&&o.x>=h&&i!==o.x&&E(n<c?i:a,n,h,c,n<c?a:i,n,o.x,o.y)&&(u=Math.abs(n-o.y)/(i-o.x),M(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&g(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 u(o,o.next),u(r,r.next)}function g(t,e){return T(t.prev,t,e.prev)<0&&T(e.next,t,t.next)<0}function x(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 p(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function E(t,e,r,o,i,n,a,s){return(i-a)*(e-s)>=(t-a)*(n-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(n-s)>=(i-a)*(o-s)}function v(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&&b(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(M(t,e)&&M(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)&&(T(t.prev,t,e.prev)||T(t,e.prev,e))||y(t,e)&&T(t.prev,t,t.next)>0&&T(e.prev,e,e.next)>0)}function T(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function y(t,e){return t.x===e.x&&t.y===e.y}function b(t,e,r,o){var i=R(T(t,e,r)),n=R(T(t,e,o)),a=R(T(r,o,t)),s=R(T(r,o,e));return i!==n&&a!==s||(!(0!==i||!A(t,r,e))||(!(0!==n||!A(t,o,e))||(!(0!==a||!A(r,t,o))||!(0!==s||!A(r,e,o)))))}function A(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 R(t){return t>0?1:t<0?-1:0}function M(t,e){return T(t.prev,t,t.next)<0?T(t,e,t.next)>=0&&T(t,t.prev,e)>=0:T(t,e,t.prev)<0||T(t,t.next,e)<0}function w(t,e){var r=new U(t.i,t.x,t.y),o=new U(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 F(t,e,r,o){var i=new U(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 L(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 U(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 D(t,e,r,o){for(var i=0,n=e,a=r-o;n<r;n+=o)i+=(t[a]-t[n])*(t[n+1]+t[a+1]),a=n;return i}function P(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 S(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}a.deviation=function(t,e,r,o){var i=e&&e.length,n=i?e[0]*r:t.length,a=Math.abs(D(t,0,n,r));if(i)for(var s=0,u=e.length;s<u;s++){var l=e[s]*r,h=s<u-1?e[s+1]*r:t.length;a-=Math.abs(D(t,l,h,r))}var c=0;for(s=0;s<o.length;s+=3){var f=o[s]*r,d=o[s+1]*r,m=o[s+2]*r;c+=Math.abs((t[f]-t[m])*(t[d+1]-t[f+1])-(t[f]-t[d])*(t[m+1]-t[f+1]))}return 0===a&&0===c?0:Math.abs((c-a)/a)},a.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},o=0,i=0;i<t.length;i++){for(var n=0;n<t[i].length;n++)for(var a=0;a<e;a++)r.vertices.push(t[i][n][a]);i>0&&(o+=t[i-1].length,r.holes.push(o))}return r};var C=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function B(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,r){return t instanceof B?t:C(t)?new B(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new B(t.x,t.y):new B(t,e,r)}function O(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function z(t,e){return!t||t instanceof O?t:new O(t,e)}function X(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function G(t,e){return t instanceof X?t:new X(t,e)}B.prototype={clone:function(){return new B(this.x,this.y)},add:function(t){return this.clone()._add(N(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(N(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new B(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new B(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=I(this.x),this.y=I(this.y),this},distanceTo:function(t){var e=(t=N(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=N(t)).x===this.x&&t.y===this.y},contains:function(t){return t=N(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+S(this.x)+", "+S(this.y)+")"}},O.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof B||"number"==typeof t[0]||"x"in t)e=r=N(t);else if(e=(t=z(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return N((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return N(this.min.x,this.max.y)},getTopRight:function(){return N(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof B?N(t):z(t))instanceof O?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=z(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=z(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},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,o=Math.abs(e.x-r.x)*t,i=Math.abs(e.y-r.y)*t;return z(N(e.x-o,e.y-i),N(r.x+o,r.y+i))},equals:function(t){return!!t&&(t=z(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},X.prototype={extend:function(t){var e,r,o=this._southWest,i=this._northEast;if(t instanceof W)e=t,r=t;else{if(!(t instanceof X))return t?this.extend(j(t)||G(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return o||i?(o.lat=Math.min(e.lat,o.lat),o.lng=Math.min(e.lng,o.lng),i.lat=Math.max(r.lat,i.lat),i.lng=Math.max(r.lng,i.lng)):(this._southWest=new W(e.lat,e.lng),this._northEast=new W(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,o=Math.abs(e.lat-r.lat)*t,i=Math.abs(e.lng-r.lng)*t;return new X(new W(e.lat-o,e.lng-i),new W(r.lat+o,r.lng+i))},getCenter:function(){return new W((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new W(this.getNorth(),this.getWest())},getSouthEast:function(){return new W(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof W||"lat"in t?j(t):G(t);var e,r,o=this._southWest,i=this._northEast;return t instanceof X?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=o.lat&&r.lat<=i.lat&&e.lng>=o.lng&&r.lng<=i.lng},intersects:function(t){t=G(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=G(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},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=G(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var H=function(t){var e,r,o,i;for(r=1,o=arguments.length;r<o;r++)for(e in i=arguments[r])t[e]=i[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),o=this.scale(e);return this.transformation._transform(r,o)},pointToLatLng:function(t,e){var r=this.scale(e),o=this.transformation.untransform(t,r);return this.projection.unproject(o)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new O(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?P(t.lng,this.wrapLng,!0):t.lng;return new W(this.wrapLat?P(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),o=e.lat-r.lat,i=e.lng-r.lng;if(0===o&&0===i)return t;var n=t.getSouthWest(),a=t.getNorthEast();return new X(new W(n.lat-o,n.lng-i),new W(a.lat-o,a.lng-i))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,o=t.lat*r,i=e.lat*r,n=Math.sin((e.lat-t.lat)*r/2),a=Math.sin((e.lng-t.lng)*r/2),s=n*n+Math.cos(o)*Math.cos(i)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function W(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function j(t,e,r){return t instanceof W?t:C(t)&&"object"!=typeof t[0]?3===t.length?new W(t[0],t[1],t[2]):2===t.length?new W(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new W(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new W(t,e,r)}W.prototype={equals:function(t,e){return!!t&&(t=j(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+S(this.lat,t)+", "+S(this.lng,t)+")"},distanceTo:function(t){return H.distance(this,j(t))},wrap:function(){return H.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return G([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new W(this.lat,this.lng,this.alt)}};const Z=256,Y=.40909994067971484,K=t=>{const e=t.valueOf();return k(e)},k=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(Y),Math.cos(o));return{dec:Math.asin(Math.sin(Y)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},V=(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},q=(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()};function $(t){t.filter(t=>"MultiPolygon"===t.geometry.type).forEach(e=>{const{geometry:r,properties:o,type:i}=e;if("MultiPolygon"===r.type)for(let o=0;o<r.coordinates.length;o++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[o]})}))});return t.filter(t=>"Polygon"===t.geometry.type).map(t=>{const{geometry:e,properties:r}=t,o=a.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=a(o.vertices,o.holes,o.dimensions),s=n.length>256?new Uint16Array(n):new Uint8Array(n),u=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return o||e}(r),l=r.highlight||!1;let h=0,c=0,f=0;for(let t=0;t<i.length;t+=2)h+=i[t],c+=i[t+1],f++;return{aPosition:i,cuts:s,buildingHeight:u,centroid:[h/f,c/f],highlight:l}})}const J=(t,e)=>{const{lat:r,lng:o}=t;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*Z)(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)*Z)(r,e))},Q=(t,e)=>{return new W((r=t.y,o=e,i=Math.PI-2*Math.PI*r/Z/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/Z/Math.pow(2,e)*360-180}(t.x,e));var r,o,i};let tt,et={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new O(new B(0,0),new B(0,0)),DEMPixelBounds:new O(new B(0,0),new B(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const rt=t=>i(void 0,void 0,void 0,function*(){const{demZoom:e,getFeatures:r,terrainSource:o,dsmSource:n,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:l,tileMerger:h,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:d,maxZoom:m,tileSize:_,_overzoom:g}=o,x=(p={getFeatures:r},i(void 0,void 0,void 0,function*(){const{getFeatures:t}=p;try{return $(yield t())}catch(t){console.log("Error merging buildings",t)}return[]}));var p;try{const t=u,r=t.getNorthWest(),o=t.getSouthEast(),i=new O(J(r,e),J(o,e));let g=new B(i.min.x,i.min.y),p=i.max.x-i.min.x;const E=i.max.y-i.min.y,v=i.max.subtract(i.min);v.y>v.x&&(g.x-=Z,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,i=e.divideBy(Z).floor().multiplyBy(Z),n=(Math.ceil(r/Z)+1)*Z,a=(Math.ceil(o/Z)+1)*Z,s=i.add([n,a]);return new O(i,s)})({upperLeft:g,width:p,height:E}),{x:y,y:b}=T.max.subtract(T.min),A=(t=>{const{upperLeft:e,width:r,height:o,zoom:i}=t,n=e.divideBy(Z).floor(),a=n.x+r/Z,s=Math.min(n.y+o/Z,Math.pow(2,i)-1),u=[];for(var l=n.x;l<a;l++)for(var h=n.y;h<s;h++)u.push({x:l,y:h,z:i});return u})({upperLeft:T.min,width:y,height:b,zoom:e}),R=(t=>{t.sort((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x);const e=t.reduce((t,e)=>e.x<t.x?e:t).x,r=t.reduce((t,e)=>e.y<t.y?e:t).y;return t.map(t=>{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)*Z,yOffset:(t.y-r)*Z}})})(A),M=i.max.x-i.min.x,w=i.max.y-i.min.y,F=Math.round(M),L=Math.round(w),U=JSON.stringify(R);if(!c&&U===tt&&e<m)return et=Object.assign(Object.assign({},et),{outputWidth:F,outputHeight:L,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),et;const D=yield h.merge(R,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===D)return et=Object.assign(Object.assign({},et),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),et;const P=new O(J(new X(n.bounds).getNorthWest(),e),J(new X(n.bounds).getSouthEast(),e)),S=P.min.subtract(T.min),C=P.max.subtract(T.min),I=[S.x/y,S.y/b,C.x/y,C.y/b].map(t=>2*t-1),N=yield x,{maxHeight:z,heightMapTex:G}=l.raster({upperLeftTile:R[0],width:y,height:b,mapZoom:e,features:N,imageData:D,gl:s,dsmSource:n,dsmCoords:I}),H=Math.max(n.maxHeight,D.maxHeight+z);tt=U,et={heightMapTex:G,maxHeight:H,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:i,raster:R,demZoom:e,dirty:!0,outputWidth:F,outputHeight:L}}catch(t){console.error("Could not decode height map",t)}return et}),ot=(t,e)=>{const{r:r,g:o,b:i}=t;return[r/255,o/255,i/255,e]},it=(t,e)=>{const{date:r}=e,{dec:o,Hi:i}=K(r);t.updateDate({dec:o,Hi:i})},nt=(t,e)=>i(void 0,void 0,void 0,function*(){return yield t.updateDateRange(Object.assign({},e))}),at=(t,e)=>{const{color:r,opacity:o}=e,i=ot(r,o);t.updateColor({colorVec:i})},st=t=>{const{kernel:e,map:r,heightMap:o,now:i,color:n,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:h,maxHeight:c,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:g}=o;if(0===f||0===d)return;const{min:x,max:p}=_;if(!x||!p)return;const{x:E,y:v}=p.subtract(x),T=m.min,y=r.getPixelDimensions(),b=r.screenUnproject([0,0]),A=r.screenUnproject([y.width,0]),R=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,R,b,A].map(t=>r.project(t,g)),F=w.map(t=>[(t.x-T.x)/f,(t.y-T.y)/d]).flat(),L=w.map(t=>[(t.x-x.x)/E,(t.y-x.y)/v]).map(t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)]).flat(),U=T.y/Z/Math.pow(2,g),D=d/Z/Math.pow(2,g),P=new X(Q(m.getTopLeft(),g),Q(m.getBottomRight(),g)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:i}=e,n=ot(r,o),{dec:a,Hi:s}=K(i);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:g,cornerTextureCoords:F,cornerClipCoords:L,topYCoord:U,ySize:D,west:S,dLng:C,date:i,color:n,opacity:a,outputHeight:l,outputWidth:h,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const ut=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*Z)(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)*Z)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new W((i=r.y,n=o,a=Math.PI-2*Math.PI*i/Z/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/Z/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 X(e,r)}}};class lt extends n{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}},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=>i(this,void 0,void 0,function*(){if(200!==t.status)throw new Error(yield t.text())})).catch(t=>i(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&&(it(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=q(this.options.date)-q(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&&(at(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&&(at(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 i(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)it(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()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield nt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this})}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:o,height:i}=this._heightMap,n=e.min;return t.map(t=>{const e=J(t,r);return[(e.x-n.x)/o,(e.y-n.y)/i]})}return[]}_getBounds(t,e){const{width:r,height:o}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(o))return t.getBounds();const i=t.getCenter(),n=J(i,e),a=Q(new B(n.x-r/2,n.y-o/2),e),s=Q(new B(n.x+r/2,n.y+o/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return i(this,void 0,void 0,function*(){if(this.options.debug("_reset()"),this._map){const t=ut(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),i=r.filter(t=>t.canonical.z===o).map(t=>t.canonical),[n,a,s,u]=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]),l=Q(new B(512*n,512*a),o+1),h=Q(new B(512*(s+1),512*(u+1)),o+1);e=o+1,this._bounds=t.createBounds({nw:l,se:h})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield rt({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 i(this,void 0,void 0,function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(st({kernel:this._compiledKernel,map:ut(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 nt(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<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),i=V(o,.5,u)/1e3/60,n=Math.min(Math.floor(i/6),255);e=new Uint8Array([n])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!ut(this._map).isLeaflet())return a;for(let t=0;t<o;t+=i)s.set(a.subarray(t,t+i),n-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,o=t(e,r),i=ut(this._map),{lat:n,lng:a}=i.getBounds().getNorthWest(),s=i.getBounds().getSouthEast(),u=[0,0,0,a,n,0],l=[(s.lng-a)/e,(n-s.lat)/r,0];return this.options.sunExposure.enabled,{data:o,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:l,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations),r=((t,e)=>{const r=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=ot({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 r}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=q(t.startDate)-q(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=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=ot({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<a;r++){const o=[];for(let e=s-1;e>=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=V(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}}const ht=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 ct(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 ft{constructor(t){this.gl=t,this.program=ct({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 bool is_negative;\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) * (is_negative ? -1.0 : 1.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.isNegative=t.getUniformLocation(this.program,"is_negative"),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:i,height:n,imageData:a,dsmCoords:s,dsmSource:u,gl:l}=t,{x:h,y:c,z:f}=r;let d=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1),l.bindTexture(l.TEXTURE_2D,a);const m=i,_=n;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,m,_,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,m,_);const g=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,g);const x=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,x,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 p=h/Math.pow(2,f),E=c/Math.pow(2,f),v=new B(p,E),T=i/Z/Math.pow(2,f),y=n/Z/Math.pow(2,f);l.uniform3f(this.xyzUniformLocation,p,E,f),l.uniform2f(this.dimensionsUniformLocation,T,y),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const b=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,b,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:a,highlight:s}=t,u=new B(a[0],a[1]).subtract(v).unscaleBy(new B(T,y)).scaleBy(new B(i,n)).floor();if(u.x<0||u.y<0||u.x>i||u.y>n)return;const h=u.x/i,c=u.y/n,f=5*Math.abs(e),m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),l.uniform1i(this.isNegative,e<0?1:0),l.uniform4f(this.colorUniformLocation,m,_,m,_),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!==u.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,u.width,u.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,u.data),l.pixelStorei(l.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[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(g),{maxHeight:d,heightMapTex:this.targetTexture}}}const dt=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:o===Z?i:Math.floor(i/2),y:o===Z?n:Math.floor(n/2),z:o===Z?a:a-1}};class mt{constructor(t){this.gl=t,this.program=ct({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 i(this,void 0,void 0,function*(){const r=this.gl;let o=0;const{width:n,height:a,crossOrigin:s,getSourceUrl:u,getElevation:l,tileSize:h,tileLoaded:c,maxZoom:f}=e;if(r){this.inProgress.forEach(t=>t.src=""),this.inProgress=[],this.finished=0;const e=new Set;t.forEach(t=>{e.add(u(dt({tile:t,maxZoom:f,tileSize:h})))}),r.useProgram(this.program);let d=0;-32768===l({r:0,g:0,b:0,a:0})&&(d=0),-1e4===l({r:0,g:0,b:0,a:0})&&(d=1),4808===l({r:256,g:0,b:0,a:0})&&(d=2),r.uniform1f(this.encodingUniformLocation,d),ht({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:n,height:a,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=>i(this,void 0,void 0,function*(){return new Promise((i,l)=>{const c=new Image;this.inProgress.push(c),c.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,c),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer);const s=t.filter(t=>e===u(dt({tile:t,maxZoom:f,tileSize:h}))),l=s.reduce((t,e)=>Math.min(t,e.xOffset),1/0),d=s.reduce((t,e)=>Math.min(t,e.yOffset),1/0),m=s.reduce((t,e)=>Math.max(t,e.xOffset),0),_=s.reduce((t,e)=>Math.max(t,e.yOffset),0),g=s.reduce((t,e)=>Math.min(t,e.x),1/0),x=s.reduce((t,e)=>Math.min(t,e.y),1/0),p=s.reduce((t,e)=>Math.max(t,e.x),0),E=s.reduce((t,e)=>Math.max(t,e.y),0),v=s[0].z,T=l/n,y=d/a,b=(m+Z)/n,A=(_+Z)/a,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-f)+(h!==Z?1:0)),F=514===h?1/h:0,L=1/w,U=g%w/w,D=x%w/w,P=p%w/w,S=E%w/w;M=[U,D,P+L,D,U,S+L,P+L,S+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,n,a),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const C=Math.floor(T*n),B=Math.floor(y*a),I=Math.ceil((b-T)*n),N=Math.ceil((A-y)*a),O=new Uint8Array(4*I*N);r.readPixels(C,B,I,N,r.RGBA,r.UNSIGNED_BYTE,O);for(let t=0;t<O.length;t+=4){const e=(256*O[t+2]+O[t+3])/5;o=Math.max(e,o)}i(null)},c.onerror=t=>{if(c.src!==c.originalSource)return l("new tiles requested");i(null)},c.crossOrigin=s||null,c.src=e,c.originalSource=c.src}).then(()=>{this.finished++,c(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")})}}class _t extends lt{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,o=e,n=ct({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_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){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 a=o.createBuffer(),s=o.getAttribLocation(n,"a_pos"),u=o.createBuffer(),l=o.getAttribLocation(n,"a_tex_pos"),h=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,h),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW);const c=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,c),o.bufferData(o.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),o.STATIC_DRAW);const f=o.getUniformLocation(n,"user_a");o.uniform1i(f,0);const d=o.getUniformLocation(n,"user_width"),m=o.getUniformLocation(n,"user_height"),_=o.getUniformLocation(n,"user_maxHeight"),g=o.getUniformLocation(n,"user_zoom"),x=o.getUniformLocation(n,"user_topYCoord"),p=o.getUniformLocation(n,"user_ySize"),E=o.getUniformLocation(n,"user_step"),v=o.getUniformLocation(n,"user_west"),T=o.getUniformLocation(n,"user_dLng"),y=o.getUniformLocation(n,"user_dec"),b=o.getUniformLocation(n,"user_Hi"),A=o.getUniformLocation(n,"user_color"),R=o.getUniformLocation(n,"u_below_canopy"),M=o.getUniformLocation(n,"user_sunExposureTexture"),w=o.getUniformLocation(n,"user_outputSunExposure"),F=o.getUniformLocation(n,"u_outputShadeProfile"),L=o.getUniformLocation(n,"u_gpxTexture"),U=o.getUniformLocation(n,"u_decHiTexture"),D=o.getUniformLocation(n,"u_sunColor"),P=o.getUniformLocation(n,"u_outputLocationShadeProfile"),S=o.getUniformLocation(n,"u_shadeProfileLocation"),C=o.createTexture();let B=0,I=0,N=0,O=null;const z=()=>{I&&N&&(o.useProgram(n),o.bindBuffer(o.ARRAY_BUFFER,a),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,u),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),r(o,I,N),o.viewport(0,0,I,N),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4))};let X=0,G=0,H=0,W="";const j=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(o.activeTexture(o.TEXTURE3),o.uniform1i(U,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const i=[],n=828e5,a=q(new Date(G),W);for(let t=0;t<e;t++){const e=q(new Date(G+86400*t*1e3+n),W)-a;i[t]=e}const s=new Float32Array(r*e*2);for(let t=0;t<r;t++)for(let r=0;r<e;r++){const o=G+86400*r*1e3+60*t*1e3,{dec:n,Hi:a}=k(o+i[r]);s[2*(t*e+r)]=-n,s[2*(t*e+r)+1]=a/10}ht({gl:o,texture:C,imageData:s,width:e,height:r,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT})}else o.bindTexture(o.TEXTURE_2D,C);return{outputWidth:e,outputHeight:r}};return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:i,maxHeight:s,heightMapZoom:l,topYCoord:h,ySize:c,colorVec:f,step:M,west:w,dLng:F,dec:L,Hi:U,cornerClipCoords:D,cornerTextureCoords:P,outputWidth:S,outputHeight:C,belowCanopy:X,skipRender:G}=t;o.useProgram(n),I=S,N=C,O=e,o.bindBuffer(o.ARRAY_BUFFER,a),o.bufferData(o.ARRAY_BUFFER,new Float32Array(D),o.STATIC_DRAW),o.bindBuffer(o.ARRAY_BUFFER,u),o.bufferData(o.ARRAY_BUFFER,new Float32Array(P),o.STATIC_DRAW),o.uniform1f(d,r),o.uniform1f(m,i),o.uniform1f(_,s),o.uniform1f(g,l),o.uniform1f(x,h),o.uniform1f(p,c),o.uniform4fv(A,f),o.uniform1f(E,M),o.uniform1f(v,w),o.uniform1f(T,F),o.uniform1f(y,L),o.uniform1f(b,U),o.uniform1f(R,X?1:0),G||(window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z))},updateDate:t=>{const{dec:e,Hi:r}=t;o.useProgram(n),o.uniform1f(y,e),o.uniform1f(b,r),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},updateDateRange:t=>i(this,void 0,void 0,function*(){const{startDate:e,endDate:r,iterations:i,emit:h}=t;o.useProgram(n);const c=X=Date.now(),f=Math.floor((r.getTime()-e.getTime())/i),d=o.getUniform(n,A),m=I,_=N,g=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,g),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,m,_,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 p=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,p,o.TEXTURE_2D,g,0);for(let t=0;t<i;t++){if(o.useProgram(n),h("tileloaded",t,i-1),c!==X)return o.deleteFramebuffer(x),o.deleteTexture(g),o.uniform4fv(A,d),!0;yield new Promise((r,h)=>{window.requestAnimationFrame(()=>{o.useProgram(n),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),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}=K(new Date(e.getTime()+f*t));o.uniform1f(y,c),o.uniform1f(b,d),o.uniform4fv(A,[1/i,0,0,1]),o.bindBuffer(o.ARRAY_BUFFER,a),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,u),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),o.viewport(0,0,m,_),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!==X)return o.deleteTexture(g),o.uniform4fv(A,d),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,g),o.uniform1i(M,2),o.uniform1i(w,1),z(),o.uniform1i(w,0),o.uniform1i(M,0),o.deleteTexture(g),o.uniform4fv(A,d),t(!1)})})}),updateColor:t=>{const{colorVec:e}=t;o.useProgram(n),o.uniform4fv(A,e),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},generateShadeProfile:t=>{o.useProgram(n);const{dates:e,texCoords:r,shadeColor:i,sunColor:a}=t,u=r.length,f=e.length,d=o.getUniform(n,A);o.uniform4fv(A,i),o.uniform4fv(D,a),o.uniform1i(F,1);const m=o.createTexture();o.activeTexture(o.TEXTURE3),ht({gl:o,imageData:null,width:u,height:f,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:m});const _=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,_),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,m,0);const g=o.checkFramebufferStatus(o.FRAMEBUFFER);g!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+g);const x=r.map(t=>[t[0],t[1]]).flat(),p=o.createTexture();o.activeTexture(o.TEXTURE2),ht({gl:o,texture:p,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(L,2);const E=e.map(t=>{const{dec:e,Hi:r}=K(t);return[-e,r/10]}).flat(),v=o.createTexture();o.activeTexture(o.TEXTURE1),ht({gl:o,texture:v,imageData:new Float32Array(E),width:1,height:e.length,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT}),o.uniform1i(U,1),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,c),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),o.viewport(0,0,u,f),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const T=new Uint8Array(u*f*4);return o.readPixels(0,0,u,f,o.RGBA,o.UNSIGNED_BYTE,T),o.deleteTexture(m),o.deleteTexture(v),o.deleteTexture(p),o.deleteFramebuffer(_),o.uniform1i(U,0),o.uniform1i(L,0),o.uniform1i(F,0),o.uniform4fv(A,d),o.uniform4fv(D,[0,0,0,0]),T},generateLocationShadeProfile:t=>{o.useProgram(n);const{startTime:e,endTime:r,tzId:i,texCoord:a,shadeColor:u,sunColor:f}=t,d=o.getUniform(n,A),{outputWidth:m,outputHeight:_}=j({startTime:e,endTime:r,tzId:i});o.uniform4fv(A,u),o.uniform4fv(D,f),o.uniform1i(P,1),o.uniform2fv(S,[a[0],a[1]]);const g=o.createTexture();o.activeTexture(o.TEXTURE2),ht({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:g});const x=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,x),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,g,0);const p=o.checkFramebufferStatus(o.FRAMEBUFFER);p!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,c),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,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(x),o.uniform1i(U,0),o.uniform1i(P,0),o.uniform4fv(A,d),o.uniform4fv(D,[0,0,0,0]),{output:E,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new ft(this._gl),this._tileMerger=new mt(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new o(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">&copy; ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}void 0!==window.L&&(window.L.shadeMap=(...t)=>new _t(...t));export{_t as default};
8
+ import{ImageOverlay as t,DomUtil as e,Util as r}from"leaflet";class o extends t{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const t="CANVAS"===this._url.tagName,o=this._image=t?this._url:e.create("canvas");return e.addClass(o,"leaflet-image-layer"),this._zoomAnimated&&e.addClass(o,"leaflet-zoom-animated"),this.options.className&&e.addClass(o,this.options.className),o.onselectstart=r.falseFn,o.onmousemove=r.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function n(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())})}class i{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}}function a(t,e,r){r=r||2;var o,n,i,a,u,h,c,f=e&&e.length,d=f?e[0]*r:t.length,g=s(t,0,d,r,!0),x=[];if(!g||g.next===g.prev)return x;if(f&&(g=function(t,e,r,o){var n,i,a,u=[];for(n=0,i=e.length;n<i;n++)(a=s(t,e[n]*o,n<i-1?e[n+1]*o:t.length,o,!1))===a.next&&(a.steiner=!0),u.push(p(a));for(u.sort(m),n=0;n<u.length;n++)r=_(u[n],r);return r}(t,e,g,r)),t.length>80*r){o=i=t[0],n=a=t[1];for(var v=r;v<d;v+=r)(u=t[v])<o&&(o=u),(h=t[v+1])<n&&(n=h),u>i&&(i=u),h>a&&(a=h);c=0!==(c=Math.max(i-o,a-n))?32767/c:0}return l(g,x,r,o,n,c,0),x}function s(t,e,r,o,n){var i,a;if(n===L(t,e,r,o)>0)for(i=e;i<r;i+=o)a=F(i,t[i],t[i+1],a);else for(i=r-o;i>=e;i-=o)a=F(i,t[i],t[i+1],a);return a&&y(a,a.next)&&(U(a),a=a.next),a}function u(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!y(o,o.next)&&0!==T(o.prev,o,o.next))o=o.next;else{if(U(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function l(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=x(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;e<l&&(s++,o=o.nextZ);e++);for(u=l;s>0||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,m,_=t;t.prev!==t.next;)if(s=t.prev,m=t.next,i?c(t,o,n,i):h(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),U(t),t=m.next,_=m.next;else if((t=m)===_){a?1===a?l(t=f(u(t),e,r),e,r,o,n,i,2):2===a&&d(t,e,r,o,n,i):l(u(t),e,r,o,n,i,1);break}}}function h(t){var e=t.prev,r=t,o=t.next;if(T(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=n<i?n<a?n:a:i<a?i:a,c=s<u?s<l?s:l:u<l?u:l,f=n>i?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&&v(n,s,i,u,a,l,m.x,m.y)&&T(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function c(t,e,r,o){var n=t.prev,i=t,a=t.next;if(T(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=s<u?s<l?s:l:u<l?u:l,m=h<c?h<f?h:f:c<f?c:f,_=s>u?s>l?s:l:u>l?u:l,g=h>c?h>f?h:f:c>f?c:f,p=x(d,m,e,r,o),E=x(_,g,e,r,o),y=t.prevZ,A=t.nextZ;y&&y.z>=p&&A&&A.z<=E;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&v(s,h,u,c,l,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,A.x>=d&&A.x<=_&&A.y>=m&&A.y<=g&&A!==n&&A!==a&&v(s,h,u,c,l,f,A.x,A.y)&&T(A.prev,A,A.next)>=0)return!1;A=A.nextZ}for(;y&&y.z>=p;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&v(s,h,u,c,l,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;A&&A.z<=E;){if(A.x>=d&&A.x<=_&&A.y>=m&&A.y<=g&&A!==n&&A!==a&&v(s,h,u,c,l,f,A.x,A.y)&&T(A.prev,A,A.next)>=0)return!1;A=A.nextZ}return!0}function f(t,e,r){var o=t;do{var n=o.prev,i=o.next.next;!y(n,i)&&A(n,o,o.next,i)&&M(n,i)&&M(i,n)&&(e.push(n.i/r|0),e.push(o.i/r|0),e.push(i.i/r|0),U(o),U(o.next),o=t=i),o=o.next}while(o!==t);return u(o)}function d(t,e,r,o,n,i){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&E(a,s)){var h=w(a,s);return a=u(a,a.next),h=u(h,h.next),l(a,e,r,o,n,i,0),void l(h,e,r,o,n,i,0)}s=s.next}a=a.next}while(a!==t)}function m(t,e){return t.x-e.x}function _(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.next.x?o:o.next,s===n))return r}o=o.next}while(o!==e);if(!r)return null;var u,l=r,h=r.x,c=r.y,f=1/0;o=r;do{n>=o.x&&o.x>=h&&n!==o.x&&v(i<c?n:a,i,h,c,i<c?a:n,i,o.x,o.y)&&(u=Math.abs(i-o.y)/(n-o.x),M(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&g(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 u(o,o.next),u(r,r.next)}function g(t,e){return T(t.prev,t,e.prev)<0&&T(e.next,t,t.next)<0}function x(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 p(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function v(t,e,r,o,n,i,a,s){return(n-a)*(e-s)>=(t-a)*(i-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(i-s)>=(n-a)*(o-s)}function E(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&&A(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(M(t,e)&&M(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)&&(T(t.prev,t,e.prev)||T(t,e.prev,e))||y(t,e)&&T(t.prev,t,t.next)>0&&T(e.prev,e,e.next)>0)}function T(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function y(t,e){return t.x===e.x&&t.y===e.y}function A(t,e,r,o){var n=b(T(t,e,r)),i=b(T(t,e,o)),a=b(T(r,o,t)),s=b(T(r,o,e));return n!==i&&a!==s||(!(0!==n||!R(t,r,e))||(!(0!==i||!R(t,o,e))||(!(0!==a||!R(r,t,o))||!(0!==s||!R(r,e,o)))))}function R(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 b(t){return t>0?1:t<0?-1:0}function M(t,e){return T(t.prev,t,t.next)<0?T(t,e,t.next)>=0&&T(t,t.prev,e)>=0:T(t,e,t.prev)<0||T(t,t.next,e)<0}function w(t,e){var r=new D(t.i,t.x,t.y),o=new D(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 F(t,e,r,o){var n=new D(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 U(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 D(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 L(t,e,r,o){for(var n=0,i=e,a=r-o;i<r;i+=o)n+=(t[a]-t[i])*(t[i+1]+t[a+1]),a=i;return n}function C(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 P(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}a.deviation=function(t,e,r,o){var n=e&&e.length,i=n?e[0]*r:t.length,a=Math.abs(L(t,0,i,r));if(n)for(var s=0,u=e.length;s<u;s++){var l=e[s]*r,h=s<u-1?e[s+1]*r:t.length;a-=Math.abs(L(t,l,h,r))}var c=0;for(s=0;s<o.length;s+=3){var f=o[s]*r,d=o[s+1]*r,m=o[s+2]*r;c+=Math.abs((t[f]-t[m])*(t[d+1]-t[f+1])-(t[f]-t[d])*(t[m+1]-t[f+1]))}return 0===a&&0===c?0:Math.abs((c-a)/a)},a.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},o=0,n=0;n<t.length;n++){for(var i=0;i<t[n].length;i++)for(var a=0;a<e;a++)r.vertices.push(t[n][i][a]);n>0&&(o+=t[n-1].length,r.holes.push(o))}return r};var S=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function B(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,r){return t instanceof B?t:S(t)?new B(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new B(t.x,t.y):new B(t,e,r)}function O(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function z(t,e){return!t||t instanceof O?t:new O(t,e)}function X(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function G(t,e){return t instanceof X?t:new X(t,e)}B.prototype={clone:function(){return new B(this.x,this.y)},add:function(t){return this.clone()._add(N(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(N(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new B(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new B(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=I(this.x),this.y=I(this.y),this},distanceTo:function(t){var e=(t=N(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=N(t)).x===this.x&&t.y===this.y},contains:function(t){return t=N(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+P(this.x)+", "+P(this.y)+")"}},O.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof B||"number"==typeof t[0]||"x"in t)e=r=N(t);else if(e=(t=z(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return N((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return N(this.min.x,this.max.y)},getTopRight:function(){return N(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof B?N(t):z(t))instanceof O?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=z(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=z(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},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,o=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return z(N(e.x-o,e.y-n),N(r.x+o,r.y+n))},equals:function(t){return!!t&&(t=z(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},X.prototype={extend:function(t){var e,r,o=this._southWest,n=this._northEast;if(t instanceof W)e=t,r=t;else{if(!(t instanceof X))return t?this.extend(Y(t)||G(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return o||n?(o.lat=Math.min(e.lat,o.lat),o.lng=Math.min(e.lng,o.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new W(e.lat,e.lng),this._northEast=new W(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,o=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new X(new W(e.lat-o,e.lng-n),new W(r.lat+o,r.lng+n))},getCenter:function(){return new W((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new W(this.getNorth(),this.getWest())},getSouthEast:function(){return new W(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof W||"lat"in t?Y(t):G(t);var e,r,o=this._southWest,n=this._northEast;return t instanceof X?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=o.lat&&r.lat<=n.lat&&e.lng>=o.lng&&r.lng<=n.lng},intersects:function(t){t=G(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=G(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},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=G(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var H=function(t){var e,r,o,n;for(r=1,o=arguments.length;r<o;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),o=this.scale(e);return this.transformation._transform(r,o)},pointToLatLng:function(t,e){var r=this.scale(e),o=this.transformation.untransform(t,r);return this.projection.unproject(o)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new O(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?C(t.lng,this.wrapLng,!0):t.lng;return new W(this.wrapLat?C(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),o=e.lat-r.lat,n=e.lng-r.lng;if(0===o&&0===n)return t;var i=t.getSouthWest(),a=t.getNorthEast();return new X(new W(i.lat-o,i.lng-n),new W(a.lat-o,a.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,o=t.lat*r,n=e.lat*r,i=Math.sin((e.lat-t.lat)*r/2),a=Math.sin((e.lng-t.lng)*r/2),s=i*i+Math.cos(o)*Math.cos(n)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function W(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function Y(t,e,r){return t instanceof W?t:S(t)&&"object"!=typeof t[0]?3===t.length?new W(t[0],t[1],t[2]):2===t.length?new W(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new W(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new W(t,e,r)}W.prototype={equals:function(t,e){return!!t&&(t=Y(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+P(this.lat,t)+", "+P(this.lng,t)+")"},distanceTo:function(t){return H.distance(this,Y(t))},wrap:function(){return H.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return G([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new W(this.lat,this.lng,this.alt)}};const Z=256,j=.40909994067971484,K=t=>{const e=t.valueOf();return k(e)},k=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(j),Math.cos(o));return{dec:Math.asin(Math.sin(j)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},V=(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},q=(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()};function $(t){t.filter(t=>"MultiPolygon"===t.geometry.type).forEach(e=>{const{geometry:r,properties:o,type:n}=e;if("MultiPolygon"===r.type)for(let o=0;o<r.coordinates.length;o++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[o]})}))});return t.filter(t=>"Polygon"===t.geometry.type).map(t=>{const{geometry:e,properties:r}=t,o=a.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=a(o.vertices,o.holes,o.dimensions),s=i.length>256?new Uint16Array(i):new Uint8Array(i),u=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return o||e}(r),l=r.highlight||!1;let h=0,c=0,f=0;for(let t=0;t<n.length;t+=2)h+=n[t],c+=n[t+1],f++;return{aPosition:n,cuts:s,buildingHeight:u,centroid:[h/f,c/f],highlight:l}})}const J=(t,e)=>{const{lat:r,lng:o}=t;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*Z)(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)*Z)(r,e))},Q=(t,e)=>{return new W((r=t.y,o=e,n=Math.PI-2*Math.PI*r/Z/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/Z/Math.pow(2,e)*360-180}(t.x,e));var r,o,n};let tt,et={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new O(new B(0,0),new B(0,0)),DEMPixelBounds:new O(new B(0,0),new B(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const rt=t=>n(void 0,void 0,void 0,function*(){const{demZoom:e,getFeatures:r,terrainSource:o,canopySource:i,dsmSource:a,tileLoaded:s,gl:u,bounds:l,buildingRasterizer:h,tileMerger:c,canopyMerger:f,forceUpdate:d=!1}=t,{getSourceUrl:m,getElevation:_,maxZoom:g,tileSize:x,_overzoom:p}=o,v=(E={getFeatures:r},n(void 0,void 0,void 0,function*(){const{getFeatures:t}=E;try{return $(yield t())}catch(t){console.log("Error merging buildings",t)}return[]}));var E;try{const t=l,r=t.getNorthWest(),o=t.getSouthEast(),n=new O(J(r,e),J(o,e));let p=new B(n.min.x,n.min.y),E=n.max.x-n.min.x;const T=n.max.y-n.min.y,y=n.max.subtract(n.min);y.y>y.x&&(p.x-=Z,E+=512);const A=(t=>{const{upperLeft:e,width:r,height:o}=t,n=e.divideBy(Z).floor().multiplyBy(Z),i=(Math.ceil(r/Z)+1)*Z,a=(Math.ceil(o/Z)+1)*Z,s=n.add([i,a]);return new O(n,s)})({upperLeft:p,width:E,height:T}),{x:R,y:b}=A.max.subtract(A.min),M=(t=>{const{upperLeft:e,width:r,height:o,zoom:n}=t,i=e.divideBy(Z).floor(),a=i.x+r/Z,s=Math.min(i.y+o/Z,Math.pow(2,n)-1),u=[];for(var l=i.x;l<a;l++)for(var h=i.y;h<s;h++)u.push({x:l,y:h,z:n});return u})({upperLeft:A.min,width:R,height:b,zoom:e}),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.x<t.x?e:t).x,r=t.reduce((t,e)=>e.y<t.y?e:t).y;return t.map(t=>{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)*Z,yOffset:(t.y-r)*Z}})})(M),F=n.max.x-n.min.x,U=n.max.y-n.min.y,D=Math.round(F),L=Math.round(U),C=JSON.stringify(w);if(!d&&C===tt&&e<g)return et=Object.assign(Object.assign({},et),{outputWidth:D,outputHeight:L,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),et;const P=[c.merge(w,{maxZoom:g,width:R,height:b,crossOrigin:"Anonymous",getSourceUrl:m,getElevation:_,tileSize:x,tileLoaded:s})];if(void 0!==i){const t=f.merge(w,{maxZoom:i.maxZoom,width:R,height:b,crossOrigin:"Anonymous",getSourceUrl:i.getSourceUrl,getElevation:i.getElevation,tileSize:i.tileSize,tileLoaded:s});P.push(t)}else P.push(new Promise(t=>{const e=u.createTexture();u.activeTexture(u.TEXTURE2),u.bindTexture(u.TEXTURE_2D,e),u.texImage2D(u.TEXTURE_2D,0,u.RGBA,1,1,0,u.RGBA,u.UNSIGNED_BYTE,new Uint8ClampedArray([0,0,0,0])),t(e)}));const[S,I]=yield Promise.all(P);if(null===S||null===I)return et=Object.assign(Object.assign({},et),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),et;const N=new O(J(new X(a.bounds).getNorthWest(),e),J(new X(a.bounds).getSouthEast(),e)),z=N.min.subtract(A.min),G=N.max.subtract(A.min),H=[z.x/R,z.y/b,G.x/R,G.y/b].map(t=>2*t-1),W=yield v,{maxHeight:Y,heightMapTex:j}=h.raster({upperLeftTile:w[0],width:R,height:b,mapZoom:e,features:W,imageData:S,gl:u,dsmSource:a,dsmCoords:H,canopyData:I}),K=Math.max(a.maxHeight,S.maxHeight+Y);tt=C,et={heightMapTex:j,maxHeight:K,width:R,height:b,DEMPixelBounds:A,visibleDEMPixelBounds:n,raster:w,demZoom:e,dirty:!0,outputWidth:D,outputHeight:L}}catch(t){console.error("Could not decode height map",t)}return et}),ot=(t,e)=>{const{r:r,g:o,b:n}=t;return[r/255,o/255,n/255,e]},nt=(t,e)=>{const{date:r}=e,{dec:o,Hi:n}=K(r);t.updateDate({dec:o,Hi:n})},it=(t,e)=>n(void 0,void 0,void 0,function*(){return yield t.updateDateRange(Object.assign({},e))}),at=(t,e)=>{const{color:r,opacity:o}=e,n=ot(r,o);t.updateColor({colorVec:n})},st=t=>{const{kernel:e,map:r,heightMap:o,now:n,color:i,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:h,maxHeight:c,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:g}=o;if(0===f||0===d)return;const{min:x,max:p}=_;if(!x||!p)return;const{x:v,y:E}=p.subtract(x),T=m.min,y=r.getPixelDimensions(),A=r.screenUnproject([0,0]),R=r.screenUnproject([y.width,0]),b=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,b,A,R].map(t=>r.project(t,g)),F=w.map(t=>[(t.x-T.x)/f,(t.y-T.y)/d]).flat(),U=w.map(t=>[(t.x-x.x)/v,(t.y-x.y)/E]).map(t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)]).flat(),D=T.y/Z/Math.pow(2,g),L=d/Z/Math.pow(2,g),C=new X(Q(m.getTopLeft(),g),Q(m.getBottomRight(),g)),P=C.getWest(),S=Math.abs(C.getWest()-C.getEast());((t,e)=>{const{color:r,opacity:o,date:n}=e,i=ot(r,o),{dec:a,Hi:s}=K(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:i,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:g,cornerTextureCoords:F,cornerClipCoords:U,topYCoord:D,ySize:L,west:P,dLng:S,date:n,color:i,opacity:a,outputHeight:l,outputWidth:h,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const ut=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*Z)(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)*Z)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new W((n=r.y,i=o,a=Math.PI-2*Math.PI*n/Z/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/Z/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 X(e,r)}}};class lt extends i{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=>n(this,void 0,void 0,function*(){if(200!==t.status)throw new Error(yield t.text())})).catch(t=>n(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&&(nt(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=q(this.options.date)-q(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&&(at(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&&(at(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 n(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)nt(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()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield it(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this})}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:o,height:n}=this._heightMap,i=e.min;return t.map(t=>{const e=J(t,r);return[(e.x-i.x)/o,(e.y-i.y)/n]})}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(),i=J(n,e),a=Q(new B(i.x-r/2,i.y-o/2),e),s=Q(new B(i.x+r/2,i.y+o/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return n(this,void 0,void 0,function*(){if(this.options.debug("_reset()"),this._map){const t=ut(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),[i,a,s,u]=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 B(512*i,512*a),o+1),h=Q(new B(512*(s+1),512*(u+1)),o+1);e=o+1,this._bounds=t.createBounds({nw:l,se:h})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield rt({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 n(this,void 0,void 0,function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(st({kernel:this._compiledKernel,map:ut(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 it(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<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),n=V(o,.5,u)/1e3/60,i=Math.min(Math.floor(n/6),255);e=new Uint8Array([i])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!ut(this._map).isLeaflet())return a;for(let t=0;t<o;t+=n)s.set(a.subarray(t,t+n),i-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,o=t(e,r),n=ut(this._map),{lat:i,lng:a}=n.getBounds().getNorthWest(),s=n.getBounds().getSouthEast(),u=[0,0,0,a,i,0],l=[(s.lng-a)/e,(i-s.lat)/r,0];return this.options.sunExposure.enabled,{data:o,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:l,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations),r=((t,e)=>{const r=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=ot({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 r}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=q(t.startDate)-q(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=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=ot({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<a;r++){const o=[];for(let e=s-1;e>=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=V(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}}const ht=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 ct(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 ft{constructor(t){this.gl=t,this.program=ct({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 vCoord = normalized;\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 bool is_negative;\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) * (is_negative ? -1.0 : 1.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(1,1,1,1)) {\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.r + canopyColor.r;\n float combined = textureColor.g + canopyColor.g;\n if (combined > 1.0) {\n combined = combined - floor(combined);\n upper += 1.0 / 255.0;\n }\n gl_FragColor = vec4(textureColor.r, textureColor.g, upper, combined);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n }\n if (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n gl_FragColor = vec4(textureColor.r, textureColor.g, textureColor.r, textureColor.g);\n return;\n }\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.isNegative=t.getUniformLocation(this.program,"is_negative"),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:o,width:n,height:i,imageData:a,dsmCoords:s,dsmSource:u,canopyData:l,gl:h}=t,{x:c,y:f,z:d}=r;let m=0;h.useProgram(this.program),h.activeTexture(h.TEXTURE1),h.bindTexture(h.TEXTURE_2D,a),h.activeTexture(h.TEXTURE2),h.bindTexture(h.TEXTURE_2D,l);const _=n,g=i;h.activeTexture(h.TEXTURE0),h.bindTexture(h.TEXTURE_2D,this.targetTexture),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,_,g,0,h.RGBA,h.UNSIGNED_BYTE,null),o>15&&e.length>5?(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST)):(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.LINEAR),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.LINEAR)),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.viewport(0,0,_,g);const x=h.createFramebuffer();h.bindFramebuffer(h.FRAMEBUFFER,x);const p=h.COLOR_ATTACHMENT0;h.framebufferTexture2D(h.FRAMEBUFFER,p,h.TEXTURE_2D,this.targetTexture,0),h.disable(h.BLEND),h.bindBuffer(h.ARRAY_BUFFER,this.positionBuffer),h.bindBuffer(h.ELEMENT_ARRAY_BUFFER,this.indexBuffer),h.clearColor(0,0,0,0),h.clear(h.COLOR_BUFFER_BIT);const v=c/Math.pow(2,d),E=f/Math.pow(2,d),T=new B(v,E),y=n/Z/Math.pow(2,d),A=i/Z/Math.pow(2,d);h.uniform3f(this.xyzUniformLocation,v,E,d),h.uniform2f(this.dimensionsUniformLocation,y,A),h.uniform1i(this.heightMapUniformLocation,1),h.uniform1i(this.canopyMapUniformLocation,2),h.uniform1i(this.useDSMUniformLocation,0);const R=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,R,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,1,1,1,1),h.drawArrays(h.TRIANGLE_STRIP,0,4),e.filter(t=>!t.highlight).forEach(t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:a,highlight:s}=t,u=new B(a[0],a[1]).subtract(T).unscaleBy(new B(y,A)).scaleBy(new B(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const l=u.x/n,c=u.y/i,f=5*Math.abs(e),d=Math.floor(f/255)/255,_=Math.floor(f%255)/255;m=Math.max(m,e),h.uniform2f(this.centroidUniformLocation,l,c),h.uniform1i(this.isNegative,e<0?1:0),h.uniform4f(this.colorUniformLocation,d,_,d,_),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ELEMENT_ARRAY_BUFFER,o,h.DYNAMIC_DRAW),h.drawElements(h.TRIANGLES,o.length,o.length>256?h.UNSIGNED_SHORT:h.UNSIGNED_BYTE,0)}),0!==u.data.length){h.activeTexture(h.TEXTURE1);const t=h.createTexture();h.bindTexture(h.TEXTURE_2D,t),h.pixelStorei(h.UNPACK_ALIGNMENT,2),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST),h.texImage2D(h.TEXTURE_2D,0,h.LUMINANCE_ALPHA,u.width,u.height,0,h.LUMINANCE_ALPHA,h.UNSIGNED_BYTE,u.data),h.pixelStorei(h.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[1]],r=new Float32Array(e);h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,r,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.enableVertexAttribArray(this.dsmAttributeLocation),h.bindBuffer(h.ARRAY_BUFFER,this.dsmBuffer),h.bufferData(h.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),h.STATIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,1,1,1,1),h.uniform1i(this.useDSMUniformLocation,1),h.colorMask(!1,!1,!0,!0),h.drawArrays(h.TRIANGLE_STRIP,0,4),h.deleteTexture(t)}return h.colorMask(!0,!0,!0,!0),h.activeTexture(h.TEXTURE1),h.bindTexture(h.TEXTURE_2D,a),h.activeTexture(h.TEXTURE2),h.bindTexture(h.TEXTURE_2D,l),h.uniform1i(this.useDSMUniformLocation,0),e.filter(t=>t.highlight).forEach(t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:a,highlight:s}=t,u=new B(a[0],a[1]).subtract(T).unscaleBy(new B(y,A)).scaleBy(new B(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const l=u.x/n,c=u.y/i,f=5*e,d=Math.floor(f/255)/255,_=Math.floor(f%255)/255;m=Math.max(m,e),h.uniform2f(this.centroidUniformLocation,l,c),h.uniform4f(this.colorUniformLocation,d,_,d,_),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ELEMENT_ARRAY_BUFFER,o,h.DYNAMIC_DRAW),h.drawElements(h.TRIANGLES,o.length,o.length>256?h.UNSIGNED_SHORT:h.UNSIGNED_BYTE,0)}),h.deleteFramebuffer(x),{maxHeight:m,heightMapTex:this.targetTexture}}}const dt=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===Z?n:Math.floor(n/2),y:o===Z?i:Math.floor(i/2),z:o===Z?a:a-1}};class mt{constructor(t){this.gl=t,this.program=ct({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 #ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n #else\n precision mediump float;\n #endif\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 if (closestColor < 3) {\n closestColor = 0;\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(r, g, 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.inProgress=[],this.finished=0}merge(t,e){return n(this,void 0,void 0,function*(){const r=this.gl;let o=0;const{width:i,height:a,crossOrigin:s,getSourceUrl:u,getElevation:l,tileSize:h,tileLoaded:c,maxZoom:f}=e;if(r){this.inProgress.forEach(t=>t.src=""),this.inProgress=[],this.finished=0;const e=new Set;t.forEach(t=>{e.add(u(dt({tile:t,maxZoom:f,tileSize:h})))}),r.useProgram(this.program);let d=0;-32768===l({r:0,g:0,b:0,a:0})&&(d=0),-1e4===l({r:0,g:0,b:0,a:0})&&(d=1),4808===l({r:256,g:0,b:0,a:0})&&(d=2),0===l({r:51,g:0,b:66,a:0})&&(d=3),r.uniform1f(this.encodingUniformLocation,d),ht({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:i,height:a,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST});const m=r.createFramebuffer();r.bindFramebuffer(r.FRAMEBUFFER,m),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const _=r.checkFramebufferStatus(r.FRAMEBUFFER);_!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+_),r.clear(r.COLOR_BUFFER_BIT);const g=Array.from(e).map(e=>n(this,void 0,void 0,function*(){return new Promise((n,l)=>{const c=new Image;this.inProgress.push(c),c.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,c),r.bindFramebuffer(r.FRAMEBUFFER,m);const s=t.filter(t=>e===u(dt({tile:t,maxZoom:f,tileSize:h}))),l=s.reduce((t,e)=>Math.min(t,e.xOffset),1/0),d=s.reduce((t,e)=>Math.min(t,e.yOffset),1/0),_=s.reduce((t,e)=>Math.max(t,e.xOffset),0),g=s.reduce((t,e)=>Math.max(t,e.yOffset),0),x=s.reduce((t,e)=>Math.min(t,e.x),1/0),p=s.reduce((t,e)=>Math.min(t,e.y),1/0),v=s.reduce((t,e)=>Math.max(t,e.x),0),E=s.reduce((t,e)=>Math.max(t,e.y),0),T=s[0].z,y=l/i,A=d/a,R=(_+Z)/i,b=(g+Z)/a,M=[y,A,R,A,y,b,R,b].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-f)+(h!==Z?1:0)),U=514===h?1/h:0,D=1/F,L=x%F/F,C=p%F/F,P=v%F/F,S=E%F/F;w=[L,C,P+D,C,L,S+D,P+D,S+D].map(t=>t*(514===h?512/514:1)+U),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,i,a),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const B=Math.floor(y*i),I=Math.floor(A*a),N=Math.ceil((R-y)*i),O=Math.ceil((b-A)*a),z=new Uint8Array(4*N*O);r.readPixels(B,I,N,O,r.RGBA,r.UNSIGNED_BYTE,z);for(let t=0;t<z.length;t+=4){const e=(256*z[t+2]+z[t+3])/5;o=Math.max(e,o)}n(null)},c.onerror=t=>{if(c.src!==c.originalSource)return l("new tiles requested");n(null)},c.crossOrigin=s||null,c.src=e,c.originalSource=c.src}).then(()=>{this.finished++,c(this.finished,this.inProgress.length)})}));try{yield Promise.all(g)}catch(t){return console.log(`${g.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=o,r.deleteFramebuffer(m),this.outputTexture}throw new Error("Could not get canvas context for merging tile images")})}}class _t extends lt{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,o=e,i=ct({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_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){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 a=o.createBuffer(),s=o.getAttribLocation(i,"a_pos"),u=o.createBuffer(),l=o.getAttribLocation(i,"a_tex_pos"),h=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,h),o.bufferData(o.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),o.STATIC_DRAW);const c=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,c),o.bufferData(o.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),o.STATIC_DRAW);const f=o.getUniformLocation(i,"user_a");o.uniform1i(f,0);const d=o.getUniformLocation(i,"user_width"),m=o.getUniformLocation(i,"user_height"),_=o.getUniformLocation(i,"user_maxHeight"),g=o.getUniformLocation(i,"user_zoom"),x=o.getUniformLocation(i,"user_topYCoord"),p=o.getUniformLocation(i,"user_ySize"),v=o.getUniformLocation(i,"user_step"),E=o.getUniformLocation(i,"user_west"),T=o.getUniformLocation(i,"user_dLng"),y=o.getUniformLocation(i,"user_dec"),A=o.getUniformLocation(i,"user_Hi"),R=o.getUniformLocation(i,"user_color"),b=o.getUniformLocation(i,"u_below_canopy"),M=o.getUniformLocation(i,"user_sunExposureTexture"),w=o.getUniformLocation(i,"user_outputSunExposure"),F=o.getUniformLocation(i,"u_outputShadeProfile"),U=o.getUniformLocation(i,"u_gpxTexture"),D=o.getUniformLocation(i,"u_decHiTexture"),L=o.getUniformLocation(i,"u_sunColor"),C=o.getUniformLocation(i,"u_outputLocationShadeProfile"),P=o.getUniformLocation(i,"u_shadeProfileLocation"),S=o.createTexture();let B=0,I=0,N=0,O=null;const z=()=>{I&&N&&(o.useProgram(i),o.bindBuffer(o.ARRAY_BUFFER,a),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,u),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),r(o,I,N),o.viewport(0,0,I,N),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4))};let X=0,G=0,H=0,W="";const Y=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(o.activeTexture(o.TEXTURE3),o.uniform1i(D,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const n=[],i=828e5,a=q(new Date(G),W);for(let t=0;t<e;t++){const e=q(new Date(G+86400*t*1e3+i),W)-a;n[t]=e}const s=new Float32Array(r*e*2);for(let t=0;t<r;t++)for(let r=0;r<e;r++){const o=G+86400*r*1e3+60*t*1e3,{dec:i,Hi:a}=k(o+n[r]);s[2*(t*e+r)]=-i,s[2*(t*e+r)+1]=a/10}ht({gl:o,texture:S,imageData:s,width:e,height:r,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RG,internalFormat:o.RG32F,type:o.FLOAT})}else o.bindTexture(o.TEXTURE_2D,S);return{outputWidth:e,outputHeight:r}};return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:n,maxHeight:s,heightMapZoom:l,topYCoord:h,ySize:c,colorVec:f,step:M,west:w,dLng:F,dec:U,Hi:D,cornerClipCoords:L,cornerTextureCoords:C,outputWidth:P,outputHeight:S,belowCanopy:X,skipRender:G}=t;o.useProgram(i),I=P,N=S,O=e,o.bindBuffer(o.ARRAY_BUFFER,a),o.bufferData(o.ARRAY_BUFFER,new Float32Array(L),o.STATIC_DRAW),o.bindBuffer(o.ARRAY_BUFFER,u),o.bufferData(o.ARRAY_BUFFER,new Float32Array(C),o.STATIC_DRAW),o.uniform1f(d,r),o.uniform1f(m,n),o.uniform1f(_,s),o.uniform1f(g,l),o.uniform1f(x,h),o.uniform1f(p,c),o.uniform4fv(R,f),o.uniform1f(v,M),o.uniform1f(E,w),o.uniform1f(T,F),o.uniform1f(y,U),o.uniform1f(A,D),o.uniform1f(b,X?1:0),G||(window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z))},updateDate:t=>{const{dec:e,Hi:r}=t;o.useProgram(i),o.uniform1f(y,e),o.uniform1f(A,r),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},updateDateRange:t=>n(this,void 0,void 0,function*(){const{startDate:e,endDate:r,iterations:n,emit:h}=t;o.useProgram(i);const c=X=Date.now(),f=Math.floor((r.getTime()-e.getTime())/n),d=o.getUniform(i,R),m=I,_=N,g=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,g),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,m,_,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 p=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,p,o.TEXTURE_2D,g,0);for(let t=0;t<n;t++){if(o.useProgram(i),h("tileloaded",t,n-1),c!==X)return o.deleteFramebuffer(x),o.deleteTexture(g),o.uniform4fv(R,d),!0;yield new Promise((r,h)=>{window.requestAnimationFrame(()=>{o.useProgram(i),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),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}=K(new Date(e.getTime()+f*t));o.uniform1f(y,c),o.uniform1f(A,d),o.uniform4fv(R,[1/n,0,0,1]),o.bindBuffer(o.ARRAY_BUFFER,a),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,u),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),o.viewport(0,0,m,_),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!==X)return o.deleteTexture(g),o.uniform4fv(R,d),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,g),o.uniform1i(M,2),o.uniform1i(w,1),z(),o.uniform1i(w,0),o.uniform1i(M,0),o.deleteTexture(g),o.uniform4fv(R,d),t(!1)})})}),updateColor:t=>{const{colorVec:e}=t;o.useProgram(i),o.uniform4fv(R,e),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},generateShadeProfile:t=>{o.useProgram(i);const{dates:e,texCoords:r,shadeColor:n,sunColor:a}=t,u=r.length,f=e.length,d=o.getUniform(i,R);o.uniform4fv(R,n),o.uniform4fv(L,a),o.uniform1i(F,1);const m=o.createTexture();o.activeTexture(o.TEXTURE3),ht({gl:o,imageData:null,width:u,height:f,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:m});const _=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,_),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,m,0);const g=o.checkFramebufferStatus(o.FRAMEBUFFER);g!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+g);const x=r.map(t=>[t[0],t[1]]).flat(),p=o.createTexture();o.activeTexture(o.TEXTURE2),ht({gl:o,texture:p,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(U,2);const v=e.map(t=>{const{dec:e,Hi:r}=K(t);return[-e,r/10]}).flat(),E=o.createTexture();o.activeTexture(o.TEXTURE1),ht({gl:o,texture:E,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(D,1),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,c),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,2,o.FLOAT,!1,0,0),o.viewport(0,0,u,f),o.clear(o.COLOR_BUFFER_BIT),o.drawArrays(o.TRIANGLE_STRIP,0,4);const T=new Uint8Array(u*f*4);return o.readPixels(0,0,u,f,o.RGBA,o.UNSIGNED_BYTE,T),o.deleteTexture(m),o.deleteTexture(E),o.deleteTexture(p),o.deleteFramebuffer(_),o.uniform1i(D,0),o.uniform1i(U,0),o.uniform1i(F,0),o.uniform4fv(R,d),o.uniform4fv(L,[0,0,0,0]),T},generateLocationShadeProfile:t=>{o.useProgram(i);const{startTime:e,endTime:r,tzId:n,texCoord:a,shadeColor:u,sunColor:f}=t,d=o.getUniform(i,R),{outputWidth:m,outputHeight:_}=Y({startTime:e,endTime:r,tzId:n});o.uniform4fv(R,u),o.uniform4fv(L,f),o.uniform1i(C,1),o.uniform2fv(P,[a[0],a[1]]);const g=o.createTexture();o.activeTexture(o.TEXTURE2),ht({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:g});const x=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,x),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,g,0);const p=o.checkFramebufferStatus(o.FRAMEBUFFER);p!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p),null!==O&&(o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,O)),o.bindBuffer(o.ARRAY_BUFFER,h),o.enableVertexAttribArray(s),o.vertexAttribPointer(s,2,o.FLOAT,!1,0,0),o.bindBuffer(o.ARRAY_BUFFER,c),o.enableVertexAttribArray(l),o.vertexAttribPointer(l,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 v=new Uint8Array(m*_*4);return o.readPixels(0,0,m,_,o.RGBA,o.UNSIGNED_BYTE,v),o.deleteTexture(g),o.deleteFramebuffer(x),o.uniform1i(D,0),o.uniform1i(C,0),o.uniform4fv(R,d),o.uniform4fv(L,[0,0,0,0]),{output:v,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new ft(this._gl),this._tileMerger=new mt(this._gl),this._canopyMerger=new mt(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new o(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">&copy; ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}void 0!==window.L&&(window.L.shadeMap=(...t)=>new _t(...t));export{_t as default};
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Copyright Ted Piotrowski 2025
3
3
  * Package: leaflet-shadow-simulator
4
- * Version: 0.61.0
4
+ * Version: 0.61.1
5
5
  * For licensing visit: https://shademap.app/about/
6
6
  */
7
7
 
8
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).ShadeMap=e(t.L)}(this,function(t){"use strict";class e extends t.ImageOverlay{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const e="CANVAS"===this._url.tagName,r=this._image=e?this._url:t.DomUtil.create("canvas");return t.DomUtil.addClass(r,"leaflet-image-layer"),this._zoomAnimated&&t.DomUtil.addClass(r,"leaflet-zoom-animated"),this.options.className&&t.DomUtil.addClass(r,this.options.className),r.onselectstart=t.Util.falseFn,r.onmousemove=t.Util.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function r(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())})}class o{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}}function i(t,e,r){r=r||2;var o,i,a,u,l,h,c,m=e&&e.length,_=m?e[0]*r:t.length,x=n(t,0,_,r,!0),p=[];if(!x||x.next===x.prev)return p;if(m&&(x=function(t,e,r,o){var i,a,s,u=[];for(i=0,a=e.length;i<a;i++)(s=n(t,e[i]*o,i<a-1?e[i+1]*o:t.length,o,!1))===s.next&&(s.steiner=!0),u.push(g(s));for(u.sort(f),i=0;i<u.length;i++)r=d(u[i],r);return r}(t,e,x,r)),t.length>80*r){o=a=t[0],i=u=t[1];for(var v=r;v<_;v+=r)(l=t[v])<o&&(o=l),(h=t[v+1])<i&&(i=h),l>a&&(a=l),h>u&&(u=h);c=0!==(c=Math.max(a-o,u-i))?32767/c:0}return s(x,p,r,o,i,c,0),p}function n(t,e,r,o,i){var n,a;if(i===U(t,e,r,o)>0)for(n=e;n<r;n+=o)a=M(n,t[n],t[n+1],a);else for(n=r-o;n>=e;n-=o)a=M(n,t[n],t[n+1],a);return a&&E(a,a.next)&&(w(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||!E(o,o.next)&&0!==v(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 s(t,e,r,o,i,n,f){if(t){!f&&n&&function(t,e,r,o){var i=t;do{0===i.z&&(i.z=_(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;e<l&&(s++,o=o.nextZ);e++);for(u=l;s>0||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 d,m,g=t;t.prev!==t.next;)if(d=t.prev,m=t.next,n?l(t,o,i,n):u(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),w(t),t=m.next,g=m.next;else if((t=m)===g){f?1===f?s(t=h(a(t),e,r),e,r,o,i,n,2):2===f&&c(t,e,r,o,i,n):s(a(t),e,r,o,i,n,1);break}}}function u(t){var e=t.prev,r=t,o=t.next;if(v(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=i<n?i<a?i:a:n<a?n:a,c=s<u?s<l?s:l:u<l?u:l,f=i>n?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&&x(i,s,n,u,a,l,m.x,m.y)&&v(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function l(t,e,r,o){var i=t.prev,n=t,a=t.next;if(v(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=s<u?s<l?s:l:u<l?u:l,m=h<c?h<f?h:f:c<f?c:f,g=s>u?s>l?s:l:u>l?u:l,p=h>c?h>f?h:f:c>f?c:f,E=_(d,m,e,r,o),T=_(g,p,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=E&&b&&b.z<=T;){if(y.x>=d&&y.x<=g&&y.y>=m&&y.y<=p&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&v(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,b.x>=d&&b.x<=g&&b.y>=m&&b.y<=p&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&v(b.prev,b,b.next)>=0)return!1;b=b.nextZ}for(;y&&y.z>=E;){if(y.x>=d&&y.x<=g&&y.y>=m&&y.y<=p&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&v(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;b&&b.z<=T;){if(b.x>=d&&b.x<=g&&b.y>=m&&b.y<=p&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&v(b.prev,b,b.next)>=0)return!1;b=b.nextZ}return!0}function h(t,e,r){var o=t;do{var i=o.prev,n=o.next.next;!E(i,n)&&T(i,o,o.next,n)&&A(i,n)&&A(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 a(o)}function c(t,e,r,o,i,n){var u=t;do{for(var l=u.next.next;l!==u.prev;){if(u.i!==l.i&&p(u,l)){var h=R(u,l);return u=a(u,u.next),h=a(h,h.next),s(u,e,r,o,i,n,0),void s(h,e,r,o,i,n,0)}l=l.next}u=u.next}while(u!==t)}function f(t,e){return t.x-e.x}function d(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.next.x?o:o.next,s===i))return r}o=o.next}while(o!==e);if(!r)return null;var u,l=r,h=r.x,c=r.y,f=1/0;o=r;do{i>=o.x&&o.x>=h&&i!==o.x&&x(n<c?i:a,n,h,c,n<c?a:i,n,o.x,o.y)&&(u=Math.abs(n-o.y)/(i-o.x),A(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&m(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=R(r,t);return a(o,o.next),a(r,r.next)}function m(t,e){return v(t.prev,t,e.prev)<0&&v(e.next,t,t.next)<0}function _(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 g(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function x(t,e,r,o,i,n,a,s){return(i-a)*(e-s)>=(t-a)*(n-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(n-s)>=(i-a)*(o-s)}function p(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&&T(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(A(t,e)&&A(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)&&(v(t.prev,t,e.prev)||v(t,e.prev,e))||E(t,e)&&v(t.prev,t,t.next)>0&&v(e.prev,e,e.next)>0)}function v(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function E(t,e){return t.x===e.x&&t.y===e.y}function T(t,e,r,o){var i=b(v(t,e,r)),n=b(v(t,e,o)),a=b(v(r,o,t)),s=b(v(r,o,e));return i!==n&&a!==s||(!(0!==i||!y(t,r,e))||(!(0!==n||!y(t,o,e))||(!(0!==a||!y(r,t,o))||!(0!==s||!y(r,e,o)))))}function y(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 b(t){return t>0?1:t<0?-1:0}function A(t,e){return v(t.prev,t,t.next)<0?v(t,e,t.next)>=0&&v(t,t.prev,e)>=0:v(t,e,t.prev)<0||v(t,t.next,e)<0}function R(t,e){var r=new F(t.i,t.x,t.y),o=new F(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 M(t,e,r,o){var i=new F(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 F(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 U(t,e,r,o){for(var i=0,n=e,a=r-o;n<r;n+=o)i+=(t[a]-t[n])*(t[n+1]+t[a+1]),a=n;return i}function L(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 D(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}i.deviation=function(t,e,r,o){var i=e&&e.length,n=i?e[0]*r:t.length,a=Math.abs(U(t,0,n,r));if(i)for(var s=0,u=e.length;s<u;s++){var l=e[s]*r,h=s<u-1?e[s+1]*r:t.length;a-=Math.abs(U(t,l,h,r))}var c=0;for(s=0;s<o.length;s+=3){var f=o[s]*r,d=o[s+1]*r,m=o[s+2]*r;c+=Math.abs((t[f]-t[m])*(t[d+1]-t[f+1])-(t[f]-t[d])*(t[m+1]-t[f+1]))}return 0===a&&0===c?0:Math.abs((c-a)/a)},i.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},o=0,i=0;i<t.length;i++){for(var n=0;n<t[i].length;n++)for(var a=0;a<e;a++)r.vertices.push(t[i][n][a]);i>0&&(o+=t[i-1].length,r.holes.push(o))}return r};var P=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function S(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var C=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function B(t,e,r){return t instanceof S?t:P(t)?new S(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new S(t.x,t.y):new S(t,e,r)}function I(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function N(t,e){return!t||t instanceof I?t:new I(t,e)}function O(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function z(t,e){return t instanceof O?t:new O(t,e)}S.prototype={clone:function(){return new S(this.x,this.y)},add:function(t){return this.clone()._add(B(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(B(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new S(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new S(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=C(this.x),this.y=C(this.y),this},distanceTo:function(t){var e=(t=B(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=B(t)).x===this.x&&t.y===this.y},contains:function(t){return t=B(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+D(this.x)+", "+D(this.y)+")"}},I.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof S||"number"==typeof t[0]||"x"in t)e=r=B(t);else if(e=(t=N(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return B((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return B(this.min.x,this.max.y)},getTopRight:function(){return B(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof S?B(t):N(t))instanceof I?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=N(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=N(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},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,o=Math.abs(e.x-r.x)*t,i=Math.abs(e.y-r.y)*t;return N(B(e.x-o,e.y-i),B(r.x+o,r.y+i))},equals:function(t){return!!t&&(t=N(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},O.prototype={extend:function(t){var e,r,o=this._southWest,i=this._northEast;if(t instanceof G)e=t,r=t;else{if(!(t instanceof O))return t?this.extend(H(t)||z(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return o||i?(o.lat=Math.min(e.lat,o.lat),o.lng=Math.min(e.lng,o.lng),i.lat=Math.max(r.lat,i.lat),i.lng=Math.max(r.lng,i.lng)):(this._southWest=new G(e.lat,e.lng),this._northEast=new G(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,o=Math.abs(e.lat-r.lat)*t,i=Math.abs(e.lng-r.lng)*t;return new O(new G(e.lat-o,e.lng-i),new G(r.lat+o,r.lng+i))},getCenter:function(){return new G((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new G(this.getNorth(),this.getWest())},getSouthEast:function(){return new G(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof G||"lat"in t?H(t):z(t);var e,r,o=this._southWest,i=this._northEast;return t instanceof O?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=o.lat&&r.lat<=i.lat&&e.lng>=o.lng&&r.lng<=i.lng},intersects:function(t){t=z(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=z(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},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=z(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var X=function(t){var e,r,o,i;for(r=1,o=arguments.length;r<o;r++)for(e in i=arguments[r])t[e]=i[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),o=this.scale(e);return this.transformation._transform(r,o)},pointToLatLng:function(t,e){var r=this.scale(e),o=this.transformation.untransform(t,r);return this.projection.unproject(o)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new I(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?L(t.lng,this.wrapLng,!0):t.lng;return new G(this.wrapLat?L(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),o=e.lat-r.lat,i=e.lng-r.lng;if(0===o&&0===i)return t;var n=t.getSouthWest(),a=t.getNorthEast();return new O(new G(n.lat-o,n.lng-i),new G(a.lat-o,a.lng-i))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,o=t.lat*r,i=e.lat*r,n=Math.sin((e.lat-t.lat)*r/2),a=Math.sin((e.lng-t.lng)*r/2),s=n*n+Math.cos(o)*Math.cos(i)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function G(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function H(t,e,r){return t instanceof G?t:P(t)&&"object"!=typeof t[0]?3===t.length?new G(t[0],t[1],t[2]):2===t.length?new G(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new G(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new G(t,e,r)}G.prototype={equals:function(t,e){return!!t&&(t=H(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+D(this.lat,t)+", "+D(this.lng,t)+")"},distanceTo:function(t){return X.distance(this,H(t))},wrap:function(){return X.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return z([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new G(this.lat,this.lng,this.alt)}};const W=256,j=.40909994067971484,Z=t=>{const e=t.valueOf();return Y(e)},Y=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(j),Math.cos(o));return{dec:Math.asin(Math.sin(j)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},K=(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},k=(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()};function V(t){t.filter(t=>"MultiPolygon"===t.geometry.type).forEach(e=>{const{geometry:r,properties:o,type:i}=e;if("MultiPolygon"===r.type)for(let o=0;o<r.coordinates.length;o++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[o]})}))});return t.filter(t=>"Polygon"===t.geometry.type).map(t=>{const{geometry:e,properties:r}=t,o=i.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})),a=i(o.vertices,o.holes,o.dimensions),s=a.length>256?new Uint16Array(a):new Uint8Array(a),u=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return o||e}(r),l=r.highlight||!1;let h=0,c=0,f=0;for(let t=0;t<n.length;t+=2)h+=n[t],c+=n[t+1],f++;return{aPosition:n,cuts:s,buildingHeight:u,centroid:[h/f,c/f],highlight:l}})}const q=(t,e)=>{const{lat:r,lng:o}=t;return new S(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(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)*W)(r,e))},$=(t,e)=>{return new G((r=t.y,o=e,i=Math.PI-2*Math.PI*r/W/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/W/Math.pow(2,e)*360-180}(t.x,e));var r,o,i};let J,Q={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new I(new S(0,0),new S(0,0)),DEMPixelBounds:new I(new S(0,0),new S(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const tt=t=>r(void 0,void 0,void 0,function*(){const{demZoom:e,getFeatures:o,terrainSource:i,dsmSource:n,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:l,tileMerger:h,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:d,maxZoom:m,tileSize:_,_overzoom:g}=i,x=(p={getFeatures:o},r(void 0,void 0,void 0,function*(){const{getFeatures:t}=p;try{return V(yield t())}catch(t){console.log("Error merging buildings",t)}return[]}));var p;try{const t=u,r=t.getNorthWest(),o=t.getSouthEast(),i=new I(q(r,e),q(o,e));let g=new S(i.min.x,i.min.y),p=i.max.x-i.min.x;const v=i.max.y-i.min.y,E=i.max.subtract(i.min);E.y>E.x&&(g.x-=W,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,i=e.divideBy(W).floor().multiplyBy(W),n=(Math.ceil(r/W)+1)*W,a=(Math.ceil(o/W)+1)*W,s=i.add([n,a]);return new I(i,s)})({upperLeft:g,width:p,height:v}),{x:y,y:b}=T.max.subtract(T.min),A=(t=>{const{upperLeft:e,width:r,height:o,zoom:i}=t,n=e.divideBy(W).floor(),a=n.x+r/W,s=Math.min(n.y+o/W,Math.pow(2,i)-1),u=[];for(var l=n.x;l<a;l++)for(var h=n.y;h<s;h++)u.push({x:l,y:h,z:i});return u})({upperLeft:T.min,width:y,height:b,zoom:e}),R=(t=>{t.sort((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x);const e=t.reduce((t,e)=>e.x<t.x?e:t).x,r=t.reduce((t,e)=>e.y<t.y?e:t).y;return t.map(t=>{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)*W,yOffset:(t.y-r)*W}})})(A),M=i.max.x-i.min.x,w=i.max.y-i.min.y,F=Math.round(M),U=Math.round(w),L=JSON.stringify(R);if(!c&&L===J&&e<m)return Q=Object.assign(Object.assign({},Q),{outputWidth:F,outputHeight:U,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),Q;const D=yield h.merge(R,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===D)return Q=Object.assign(Object.assign({},Q),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),Q;const P=new I(q(new O(n.bounds).getNorthWest(),e),q(new O(n.bounds).getSouthEast(),e)),C=P.min.subtract(T.min),B=P.max.subtract(T.min),N=[C.x/y,C.y/b,B.x/y,B.y/b].map(t=>2*t-1),z=yield x,{maxHeight:X,heightMapTex:G}=l.raster({upperLeftTile:R[0],width:y,height:b,mapZoom:e,features:z,imageData:D,gl:s,dsmSource:n,dsmCoords:N}),H=Math.max(n.maxHeight,D.maxHeight+X);J=L,Q={heightMapTex:G,maxHeight:H,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:i,raster:R,demZoom:e,dirty:!0,outputWidth:F,outputHeight:U}}catch(t){console.error("Could not decode height map",t)}return Q}),et=(t,e)=>{const{r:r,g:o,b:i}=t;return[r/255,o/255,i/255,e]},rt=(t,e)=>{const{date:r}=e,{dec:o,Hi:i}=Z(r);t.updateDate({dec:o,Hi:i})},ot=(t,e)=>r(void 0,void 0,void 0,function*(){return yield t.updateDateRange(Object.assign({},e))}),it=(t,e)=>{const{color:r,opacity:o}=e,i=et(r,o);t.updateColor({colorVec:i})},nt=t=>{const{kernel:e,map:r,heightMap:o,now:i,color:n,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:h,maxHeight:c,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:g}=o;if(0===f||0===d)return;const{min:x,max:p}=_;if(!x||!p)return;const{x:v,y:E}=p.subtract(x),T=m.min,y=r.getPixelDimensions(),b=r.screenUnproject([0,0]),A=r.screenUnproject([y.width,0]),R=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,R,b,A].map(t=>r.project(t,g)),F=w.map(t=>[(t.x-T.x)/f,(t.y-T.y)/d]).flat(),U=w.map(t=>[(t.x-x.x)/v,(t.y-x.y)/E]).map(t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)]).flat(),L=T.y/W/Math.pow(2,g),D=d/W/Math.pow(2,g),P=new O($(m.getTopLeft(),g),$(m.getBottomRight(),g)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:i}=e,n=et(r,o),{dec:a,Hi:s}=Z(i);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:g,cornerTextureCoords:F,cornerClipCoords:U,topYCoord:L,ySize:D,west:S,dLng:C,date:i,color:n,opacity:a,outputHeight:l,outputWidth:h,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const at=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new S(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(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)*W)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new G((i=r.y,n=o,a=Math.PI-2*Math.PI*i/W/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/W/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 O(e,r)}}};class st extends o{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}},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=>r(this,void 0,void 0,function*(){if(200!==t.status)throw new Error(yield t.text())})).catch(t=>r(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&&(rt(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=k(this.options.date)-k(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 r(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)rt(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()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield ot(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this})}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:o,height:i}=this._heightMap,n=e.min;return t.map(t=>{const e=q(t,r);return[(e.x-n.x)/o,(e.y-n.y)/i]})}return[]}_getBounds(t,e){const{width:r,height:o}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(o))return t.getBounds();const i=t.getCenter(),n=q(i,e),a=$(new S(n.x-r/2,n.y-o/2),e),s=$(new S(n.x+r/2,n.y+o/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return r(this,void 0,void 0,function*(){if(this.options.debug("_reset()"),this._map){const t=at(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),i=r.filter(t=>t.canonical.z===o).map(t=>t.canonical),[n,a,s,u]=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]),l=$(new S(512*n,512*a),o+1),h=$(new S(512*(s+1),512*(u+1)),o+1);e=o+1,this._bounds=t.createBounds({nw:l,se:h})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield tt({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 r(this,void 0,void 0,function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(nt({kernel:this._compiledKernel,map:at(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 ot(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<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),i=K(o,.5,u)/1e3/60,n=Math.min(Math.floor(i/6),255);e=new Uint8Array([n])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!at(this._map).isLeaflet())return a;for(let t=0;t<o;t+=i)s.set(a.subarray(t,t+i),n-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,o=t(e,r),i=at(this._map),{lat:n,lng:a}=i.getBounds().getNorthWest(),s=i.getBounds().getSouthEast(),u=[0,0,0,a,n,0],l=[(s.lng-a)/e,(n-s.lat)/r,0];return this.options.sunExposure.enabled,{data:o,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:l,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations),r=((t,e)=>{const r=et({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=et({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 r}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=k(t.startDate)-k(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=et({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=et({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<a;r++){const o=[];for(let e=s-1;e>=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=K(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}}const ut=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 lt(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 ht{constructor(t){this.gl=t,this.program=lt({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 bool is_negative;\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) * (is_negative ? -1.0 : 1.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.isNegative=t.getUniformLocation(this.program,"is_negative"),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:i,height:n,imageData:a,dsmCoords:s,dsmSource:u,gl:l}=t,{x:h,y:c,z:f}=r;let d=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1),l.bindTexture(l.TEXTURE_2D,a);const m=i,_=n;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,m,_,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,m,_);const g=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,g);const x=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,x,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 p=h/Math.pow(2,f),v=c/Math.pow(2,f),E=new S(p,v),T=i/W/Math.pow(2,f),y=n/W/Math.pow(2,f);l.uniform3f(this.xyzUniformLocation,p,v,f),l.uniform2f(this.dimensionsUniformLocation,T,y),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const b=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,b,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:a,highlight:s}=t,u=new S(a[0],a[1]).subtract(E).unscaleBy(new S(T,y)).scaleBy(new S(i,n)).floor();if(u.x<0||u.y<0||u.x>i||u.y>n)return;const h=u.x/i,c=u.y/n,f=5*Math.abs(e),m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),l.uniform1i(this.isNegative,e<0?1:0),l.uniform4f(this.colorUniformLocation,m,_,m,_),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!==u.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,u.width,u.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,u.data),l.pixelStorei(l.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[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(g),{maxHeight:d,heightMapTex:this.targetTexture}}}const ct=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:o===W?i:Math.floor(i/2),y:o===W?n:Math.floor(n/2),z:o===W?a:a-1}};class ft{constructor(t){this.gl=t,this.program=lt({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 r(this,void 0,void 0,function*(){const o=this.gl;let i=0;const{width:n,height:a,crossOrigin:s,getSourceUrl:u,getElevation:l,tileSize:h,tileLoaded:c,maxZoom:f}=e;if(o){this.inProgress.forEach(t=>t.src=""),this.inProgress=[],this.finished=0;const e=new Set;t.forEach(t=>{e.add(u(ct({tile:t,maxZoom:f,tileSize:h})))}),o.useProgram(this.program);let d=0;-32768===l({r:0,g:0,b:0,a:0})&&(d=0),-1e4===l({r:0,g:0,b:0,a:0})&&(d=1),4808===l({r:256,g:0,b:0,a:0})&&(d=2),o.uniform1f(this.encodingUniformLocation,d),ut({gl:o,texture:this.outputTexture,imageData:null,format:o.RGBA,width:n,height:a,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST}),o.bindFramebuffer(o.FRAMEBUFFER,this.framebuffer),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,this.outputTexture,0);const m=o.checkFramebufferStatus(o.FRAMEBUFFER);m!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+m),o.clear(o.COLOR_BUFFER_BIT);const _=Array.from(e).map(e=>r(this,void 0,void 0,function*(){return new Promise((r,l)=>{const c=new Image;this.inProgress.push(c),c.onload=()=>{o.useProgram(this.program),o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,this.tileTexture),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,o.RGBA,o.UNSIGNED_BYTE,c),o.bindFramebuffer(o.FRAMEBUFFER,this.framebuffer);const s=t.filter(t=>e===u(ct({tile:t,maxZoom:f,tileSize:h}))),l=s.reduce((t,e)=>Math.min(t,e.xOffset),1/0),d=s.reduce((t,e)=>Math.min(t,e.yOffset),1/0),m=s.reduce((t,e)=>Math.max(t,e.xOffset),0),_=s.reduce((t,e)=>Math.max(t,e.yOffset),0),g=s.reduce((t,e)=>Math.min(t,e.x),1/0),x=s.reduce((t,e)=>Math.min(t,e.y),1/0),p=s.reduce((t,e)=>Math.max(t,e.x),0),v=s.reduce((t,e)=>Math.max(t,e.y),0),E=s[0].z,T=l/n,y=d/a,b=(m+W)/n,A=(_+W)/a,R=[T,y,b,y,T,A,b,A].map(t=>2*t-1);o.bindBuffer(o.ARRAY_BUFFER,this.tilePositionBuffer),o.bufferData(o.ARRAY_BUFFER,new Float32Array(R),o.STATIC_DRAW),o.enableVertexAttribArray(this.tilePositionAttributeLocation),o.vertexAttribPointer(this.tilePositionAttributeLocation,2,o.FLOAT,!1,0,0);let M=[0,0,1,0,0,1,1,1];const w=Math.pow(2,Math.max(0,E-f)+(h!==W?1:0)),F=514===h?1/h:0,U=1/w,L=g%w/w,D=x%w/w,P=p%w/w,S=v%w/w;M=[L,D,P+U,D,L,S+U,P+U,S+U].map(t=>t*(514===h?512/514:1)+F),o.bindBuffer(o.ARRAY_BUFFER,this.texPositionBuffer),o.bufferData(o.ARRAY_BUFFER,new Float32Array(M),o.STATIC_DRAW),o.enableVertexAttribArray(this.texPositionAttributeLocation),o.vertexAttribPointer(this.texPositionAttributeLocation,2,o.FLOAT,!1,0,0),o.uniform1f(this.tileSizeUniformLocation,h),o.viewport(0,0,n,a),o.disable(o.BLEND),o.drawArrays(o.TRIANGLE_STRIP,0,4);const C=Math.floor(T*n),B=Math.floor(y*a),I=Math.ceil((b-T)*n),N=Math.ceil((A-y)*a),O=new Uint8Array(4*I*N);o.readPixels(C,B,I,N,o.RGBA,o.UNSIGNED_BYTE,O);for(let t=0;t<O.length;t+=4){const e=(256*O[t+2]+O[t+3])/5;i=Math.max(e,i)}r(null)},c.onerror=t=>{if(c.src!==c.originalSource)return l("new tiles requested");r(null)},c.crossOrigin=s||null,c.src=e,c.originalSource=c.src}).then(()=>{this.finished++,c(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")})}}class dt extends st{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:o}=t,i=e,n=lt({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_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){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(n);const a=i.createBuffer(),s=i.getAttribLocation(n,"a_pos"),u=i.createBuffer(),l=i.getAttribLocation(n,"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 c=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,c),i.bufferData(i.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),i.STATIC_DRAW);const f=i.getUniformLocation(n,"user_a");i.uniform1i(f,0);const d=i.getUniformLocation(n,"user_width"),m=i.getUniformLocation(n,"user_height"),_=i.getUniformLocation(n,"user_maxHeight"),g=i.getUniformLocation(n,"user_zoom"),x=i.getUniformLocation(n,"user_topYCoord"),p=i.getUniformLocation(n,"user_ySize"),v=i.getUniformLocation(n,"user_step"),E=i.getUniformLocation(n,"user_west"),T=i.getUniformLocation(n,"user_dLng"),y=i.getUniformLocation(n,"user_dec"),b=i.getUniformLocation(n,"user_Hi"),A=i.getUniformLocation(n,"user_color"),R=i.getUniformLocation(n,"u_below_canopy"),M=i.getUniformLocation(n,"user_sunExposureTexture"),w=i.getUniformLocation(n,"user_outputSunExposure"),F=i.getUniformLocation(n,"u_outputShadeProfile"),U=i.getUniformLocation(n,"u_gpxTexture"),L=i.getUniformLocation(n,"u_decHiTexture"),D=i.getUniformLocation(n,"u_sunColor"),P=i.getUniformLocation(n,"u_outputLocationShadeProfile"),S=i.getUniformLocation(n,"u_shadeProfileLocation"),C=i.createTexture();let B=0,I=0,N=0,O=null;const z=()=>{I&&N&&(i.useProgram(n),i.bindBuffer(i.ARRAY_BUFFER,a),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,u),i.enableVertexAttribArray(l),i.vertexAttribPointer(l,2,i.FLOAT,!1,0,0),null!==O&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,O)),o(i,I,N),i.viewport(0,0,I,N),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4))};let X=0,G=0,H=0,W="";const j=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(i.activeTexture(i.TEXTURE3),i.uniform1i(L,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const o=[],n=828e5,a=k(new Date(G),W);for(let t=0;t<e;t++){const e=k(new Date(G+86400*t*1e3+n),W)-a;o[t]=e}const s=new Float32Array(r*e*2);for(let t=0;t<r;t++)for(let r=0;r<e;r++){const i=G+86400*r*1e3+60*t*1e3,{dec:n,Hi:a}=Y(i+o[r]);s[2*(t*e+r)]=-n,s[2*(t*e+r)+1]=a/10}ut({gl:i,texture:C,imageData:s,width:e,height:r,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT})}else i.bindTexture(i.TEXTURE_2D,C);return{outputWidth:e,outputHeight:r}};return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:o,maxHeight:s,heightMapZoom:l,topYCoord:h,ySize:c,colorVec:f,step:M,west:w,dLng:F,dec:U,Hi:L,cornerClipCoords:D,cornerTextureCoords:P,outputWidth:S,outputHeight:C,belowCanopy:X,skipRender:G}=t;i.useProgram(n),I=S,N=C,O=e,i.bindBuffer(i.ARRAY_BUFFER,a),i.bufferData(i.ARRAY_BUFFER,new Float32Array(D),i.STATIC_DRAW),i.bindBuffer(i.ARRAY_BUFFER,u),i.bufferData(i.ARRAY_BUFFER,new Float32Array(P),i.STATIC_DRAW),i.uniform1f(d,r),i.uniform1f(m,o),i.uniform1f(_,s),i.uniform1f(g,l),i.uniform1f(x,h),i.uniform1f(p,c),i.uniform4fv(A,f),i.uniform1f(v,M),i.uniform1f(E,w),i.uniform1f(T,F),i.uniform1f(y,U),i.uniform1f(b,L),i.uniform1f(R,X?1:0),G||(window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z))},updateDate:t=>{const{dec:e,Hi:r}=t;i.useProgram(n),i.uniform1f(y,e),i.uniform1f(b,r),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},updateDateRange:t=>r(this,void 0,void 0,function*(){const{startDate:e,endDate:r,iterations:o,emit:h}=t;i.useProgram(n);const c=X=Date.now(),f=Math.floor((r.getTime()-e.getTime())/o),d=i.getUniform(n,A),m=I,_=N,g=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,g),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,m,_,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 x=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,x);const p=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,p,i.TEXTURE_2D,g,0);for(let t=0;t<o;t++){if(i.useProgram(n),h("tileloaded",t,o-1),c!==X)return i.deleteFramebuffer(x),i.deleteTexture(g),i.uniform4fv(A,d),!0;yield new Promise((r,h)=>{window.requestAnimationFrame(()=>{i.useProgram(n),null!==O&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,O)),i.bindFramebuffer(i.FRAMEBUFFER,x);const h=i.checkFramebufferStatus(i.FRAMEBUFFER);h!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=Z(new Date(e.getTime()+f*t));i.uniform1f(y,c),i.uniform1f(b,d),i.uniform4fv(A,[1/o,0,0,1]),i.bindBuffer(i.ARRAY_BUFFER,a),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,u),i.enableVertexAttribArray(l),i.vertexAttribPointer(l,2,i.FLOAT,!1,0,0),i.viewport(0,0,m,_),i.enable(i.BLEND),i.blendFunc(i.ONE,i.ONE),i.drawArrays(i.TRIANGLE_STRIP,0,4),r()})})}return i.deleteFramebuffer(x),yield new Promise((t,e)=>{window.requestAnimationFrame(()=>{if(i.useProgram(n),c!==X)return i.deleteTexture(g),i.uniform4fv(A,d),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,g),i.uniform1i(M,2),i.uniform1i(w,1),z(),i.uniform1i(w,0),i.uniform1i(M,0),i.deleteTexture(g),i.uniform4fv(A,d),t(!1)})})}),updateColor:t=>{const{colorVec:e}=t;i.useProgram(n),i.uniform4fv(A,e),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},generateShadeProfile:t=>{i.useProgram(n);const{dates:e,texCoords:r,shadeColor:o,sunColor:a}=t,u=r.length,f=e.length,d=i.getUniform(n,A);i.uniform4fv(A,o),i.uniform4fv(D,a),i.uniform1i(F,1);const m=i.createTexture();i.activeTexture(i.TEXTURE3),ut({gl:i,imageData:null,width:u,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:m});const _=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,_),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,m,0);const g=i.checkFramebufferStatus(i.FRAMEBUFFER);g!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+g);const x=r.map(t=>[t[0],t[1]]).flat(),p=i.createTexture();i.activeTexture(i.TEXTURE2),ut({gl:i,texture:p,imageData:new Float32Array(x),width:x.length/2,height:1,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RG,internalFormat:i.RG32F,type:i.FLOAT}),i.uniform1i(U,2);const v=e.map(t=>{const{dec:e,Hi:r}=Z(t);return[-e,r/10]}).flat(),E=i.createTexture();i.activeTexture(i.TEXTURE1),ut({gl:i,texture:E,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(L,1),null!==O&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,O)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,c),i.enableVertexAttribArray(l),i.vertexAttribPointer(l,2,i.FLOAT,!1,0,0),i.viewport(0,0,u,f),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const T=new Uint8Array(u*f*4);return i.readPixels(0,0,u,f,i.RGBA,i.UNSIGNED_BYTE,T),i.deleteTexture(m),i.deleteTexture(E),i.deleteTexture(p),i.deleteFramebuffer(_),i.uniform1i(L,0),i.uniform1i(U,0),i.uniform1i(F,0),i.uniform4fv(A,d),i.uniform4fv(D,[0,0,0,0]),T},generateLocationShadeProfile:t=>{i.useProgram(n);const{startTime:e,endTime:r,tzId:o,texCoord:a,shadeColor:u,sunColor:f}=t,d=i.getUniform(n,A),{outputWidth:m,outputHeight:_}=j({startTime:e,endTime:r,tzId:o});i.uniform4fv(A,u),i.uniform4fv(D,f),i.uniform1i(P,1),i.uniform2fv(S,[a[0],a[1]]);const g=i.createTexture();i.activeTexture(i.TEXTURE2),ut({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!==O&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,O)),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,c),i.enableVertexAttribArray(l),i.vertexAttribPointer(l,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 v=new Uint8Array(m*_*4);return i.readPixels(0,0,m,_,i.RGBA,i.UNSIGNED_BYTE,v),i.deleteTexture(g),i.deleteFramebuffer(x),i.uniform1i(L,0),i.uniform1i(P,0),i.uniform4fv(A,d),i.uniform4fv(D,[0,0,0,0]),{output:v,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new ht(this._gl),this._tileMerger=new ft(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new e(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">&copy; ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}return void 0!==window.L&&(window.L.shadeMap=(...t)=>new dt(...t)),dt});
8
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).ShadeMap=e(t.L)}(this,function(t){"use strict";class e extends t.ImageOverlay{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const e="CANVAS"===this._url.tagName,r=this._image=e?this._url:t.DomUtil.create("canvas");return t.DomUtil.addClass(r,"leaflet-image-layer"),this._zoomAnimated&&t.DomUtil.addClass(r,"leaflet-zoom-animated"),this.options.className&&t.DomUtil.addClass(r,this.options.className),r.onselectstart=t.Util.falseFn,r.onmousemove=t.Util.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function r(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())})}class o{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}}function n(t,e,r){r=r||2;var o,n,a,u,l,c,h,m=e&&e.length,_=m?e[0]*r:t.length,x=i(t,0,_,r,!0),p=[];if(!x||x.next===x.prev)return p;if(m&&(x=function(t,e,r,o){var n,a,s,u=[];for(n=0,a=e.length;n<a;n++)(s=i(t,e[n]*o,n<a-1?e[n+1]*o:t.length,o,!1))===s.next&&(s.steiner=!0),u.push(g(s));for(u.sort(f),n=0;n<u.length;n++)r=d(u[n],r);return r}(t,e,x,r)),t.length>80*r){o=a=t[0],n=u=t[1];for(var v=r;v<_;v+=r)(l=t[v])<o&&(o=l),(c=t[v+1])<n&&(n=c),l>a&&(a=l),c>u&&(u=c);h=0!==(h=Math.max(a-o,u-n))?32767/h:0}return s(x,p,r,o,n,h,0),p}function i(t,e,r,o,n){var i,a;if(n===U(t,e,r,o)>0)for(i=e;i<r;i+=o)a=M(i,t[i],t[i+1],a);else for(i=r-o;i>=e;i-=o)a=M(i,t[i],t[i+1],a);return a&&E(a,a.next)&&(w(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||!E(o,o.next)&&0!==v(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 s(t,e,r,o,n,i,f){if(t){!f&&i&&function(t,e,r,o){var n=t;do{0===n.z&&(n.z=_(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;e<l&&(s++,o=o.nextZ);e++);for(u=l;s>0||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 d,m,g=t;t.prev!==t.next;)if(d=t.prev,m=t.next,i?l(t,o,n,i):u(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),w(t),t=m.next,g=m.next;else if((t=m)===g){f?1===f?s(t=c(a(t),e,r),e,r,o,n,i,2):2===f&&h(t,e,r,o,n,i):s(a(t),e,r,o,n,i,1);break}}}function u(t){var e=t.prev,r=t,o=t.next;if(v(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,c=n<i?n<a?n:a:i<a?i:a,h=s<u?s<l?s:l:u<l?u:l,f=n>i?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>=c&&m.x<=f&&m.y>=h&&m.y<=d&&x(n,s,i,u,a,l,m.x,m.y)&&v(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function l(t,e,r,o){var n=t.prev,i=t,a=t.next;if(v(n,i,a)>=0)return!1;for(var s=n.x,u=i.x,l=a.x,c=n.y,h=i.y,f=a.y,d=s<u?s<l?s:l:u<l?u:l,m=c<h?c<f?c:f:h<f?h:f,g=s>u?s>l?s:l:u>l?u:l,p=c>h?c>f?c:f:h>f?h:f,E=_(d,m,e,r,o),T=_(g,p,e,r,o),y=t.prevZ,A=t.nextZ;y&&y.z>=E&&A&&A.z<=T;){if(y.x>=d&&y.x<=g&&y.y>=m&&y.y<=p&&y!==n&&y!==a&&x(s,c,u,h,l,f,y.x,y.y)&&v(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,A.x>=d&&A.x<=g&&A.y>=m&&A.y<=p&&A!==n&&A!==a&&x(s,c,u,h,l,f,A.x,A.y)&&v(A.prev,A,A.next)>=0)return!1;A=A.nextZ}for(;y&&y.z>=E;){if(y.x>=d&&y.x<=g&&y.y>=m&&y.y<=p&&y!==n&&y!==a&&x(s,c,u,h,l,f,y.x,y.y)&&v(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;A&&A.z<=T;){if(A.x>=d&&A.x<=g&&A.y>=m&&A.y<=p&&A!==n&&A!==a&&x(s,c,u,h,l,f,A.x,A.y)&&v(A.prev,A,A.next)>=0)return!1;A=A.nextZ}return!0}function c(t,e,r){var o=t;do{var n=o.prev,i=o.next.next;!E(n,i)&&T(n,o,o.next,i)&&R(n,i)&&R(i,n)&&(e.push(n.i/r|0),e.push(o.i/r|0),e.push(i.i/r|0),w(o),w(o.next),o=t=i),o=o.next}while(o!==t);return a(o)}function h(t,e,r,o,n,i){var u=t;do{for(var l=u.next.next;l!==u.prev;){if(u.i!==l.i&&p(u,l)){var c=b(u,l);return u=a(u,u.next),c=a(c,c.next),s(u,e,r,o,n,i,0),void s(c,e,r,o,n,i,0)}l=l.next}u=u.next}while(u!==t)}function f(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.next.x?o:o.next,s===n))return r}o=o.next}while(o!==e);if(!r)return null;var u,l=r,c=r.x,h=r.y,f=1/0;o=r;do{n>=o.x&&o.x>=c&&n!==o.x&&x(i<h?n:a,i,c,h,i<h?a:n,i,o.x,o.y)&&(u=Math.abs(i-o.y)/(n-o.x),R(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&m(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=b(r,t);return a(o,o.next),a(r,r.next)}function m(t,e){return v(t.prev,t,e.prev)<0&&v(e.next,t,t.next)<0}function _(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 g(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function x(t,e,r,o,n,i,a,s){return(n-a)*(e-s)>=(t-a)*(i-s)&&(t-a)*(o-s)>=(r-a)*(e-s)&&(r-a)*(i-s)>=(n-a)*(o-s)}function p(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&&T(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(R(t,e)&&R(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)&&(v(t.prev,t,e.prev)||v(t,e.prev,e))||E(t,e)&&v(t.prev,t,t.next)>0&&v(e.prev,e,e.next)>0)}function v(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function E(t,e){return t.x===e.x&&t.y===e.y}function T(t,e,r,o){var n=A(v(t,e,r)),i=A(v(t,e,o)),a=A(v(r,o,t)),s=A(v(r,o,e));return n!==i&&a!==s||(!(0!==n||!y(t,r,e))||(!(0!==i||!y(t,o,e))||(!(0!==a||!y(r,t,o))||!(0!==s||!y(r,e,o)))))}function y(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 A(t){return t>0?1:t<0?-1:0}function R(t,e){return v(t.prev,t,t.next)<0?v(t,e,t.next)>=0&&v(t,t.prev,e)>=0:v(t,e,t.prev)<0||v(t,t.next,e)<0}function b(t,e){var r=new F(t.i,t.x,t.y),o=new F(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 M(t,e,r,o){var n=new F(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 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 F(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 U(t,e,r,o){for(var n=0,i=e,a=r-o;i<r;i+=o)n+=(t[a]-t[i])*(t[i+1]+t[a+1]),a=i;return n}function D(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 L(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}n.deviation=function(t,e,r,o){var n=e&&e.length,i=n?e[0]*r:t.length,a=Math.abs(U(t,0,i,r));if(n)for(var s=0,u=e.length;s<u;s++){var l=e[s]*r,c=s<u-1?e[s+1]*r:t.length;a-=Math.abs(U(t,l,c,r))}var h=0;for(s=0;s<o.length;s+=3){var f=o[s]*r,d=o[s+1]*r,m=o[s+2]*r;h+=Math.abs((t[f]-t[m])*(t[d+1]-t[f+1])-(t[f]-t[d])*(t[m+1]-t[f+1]))}return 0===a&&0===h?0:Math.abs((h-a)/a)},n.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},o=0,n=0;n<t.length;n++){for(var i=0;i<t[n].length;i++)for(var a=0;a<e;a++)r.vertices.push(t[n][i][a]);n>0&&(o+=t[n-1].length,r.holes.push(o))}return r};var C=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function P(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var S=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function B(t,e,r){return t instanceof P?t:C(t)?new P(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new P(t.x,t.y):new P(t,e,r)}function I(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function N(t,e){return!t||t instanceof I?t:new I(t,e)}function O(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function z(t,e){return t instanceof O?t:new O(t,e)}P.prototype={clone:function(){return new P(this.x,this.y)},add:function(t){return this.clone()._add(B(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(B(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new P(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new P(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=S(this.x),this.y=S(this.y),this},distanceTo:function(t){var e=(t=B(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=B(t)).x===this.x&&t.y===this.y},contains:function(t){return t=B(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+L(this.x)+", "+L(this.y)+")"}},I.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof P||"number"==typeof t[0]||"x"in t)e=r=B(t);else if(e=(t=N(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return B((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return B(this.min.x,this.max.y)},getTopRight:function(){return B(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof P?B(t):N(t))instanceof I?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=N(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=N(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},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,o=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return N(B(e.x-o,e.y-n),B(r.x+o,r.y+n))},equals:function(t){return!!t&&(t=N(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},O.prototype={extend:function(t){var e,r,o=this._southWest,n=this._northEast;if(t instanceof G)e=t,r=t;else{if(!(t instanceof O))return t?this.extend(H(t)||z(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return o||n?(o.lat=Math.min(e.lat,o.lat),o.lng=Math.min(e.lng,o.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new G(e.lat,e.lng),this._northEast=new G(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,o=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new O(new G(e.lat-o,e.lng-n),new G(r.lat+o,r.lng+n))},getCenter:function(){return new G((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new G(this.getNorth(),this.getWest())},getSouthEast:function(){return new G(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof G||"lat"in t?H(t):z(t);var e,r,o=this._southWest,n=this._northEast;return t instanceof O?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=o.lat&&r.lat<=n.lat&&e.lng>=o.lng&&r.lng<=n.lng},intersects:function(t){t=z(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=z(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},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=z(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var X=function(t){var e,r,o,n;for(r=1,o=arguments.length;r<o;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),o=this.scale(e);return this.transformation._transform(r,o)},pointToLatLng:function(t,e){var r=this.scale(e),o=this.transformation.untransform(t,r);return this.projection.unproject(o)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new I(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?D(t.lng,this.wrapLng,!0):t.lng;return new G(this.wrapLat?D(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),o=e.lat-r.lat,n=e.lng-r.lng;if(0===o&&0===n)return t;var i=t.getSouthWest(),a=t.getNorthEast();return new O(new G(i.lat-o,i.lng-n),new G(a.lat-o,a.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,o=t.lat*r,n=e.lat*r,i=Math.sin((e.lat-t.lat)*r/2),a=Math.sin((e.lng-t.lng)*r/2),s=i*i+Math.cos(o)*Math.cos(n)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function G(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function H(t,e,r){return t instanceof G?t:C(t)&&"object"!=typeof t[0]?3===t.length?new G(t[0],t[1],t[2]):2===t.length?new G(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new G(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new G(t,e,r)}G.prototype={equals:function(t,e){return!!t&&(t=H(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+L(this.lat,t)+", "+L(this.lng,t)+")"},distanceTo:function(t){return X.distance(this,H(t))},wrap:function(){return X.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return z([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new G(this.lat,this.lng,this.alt)}};const W=256,j=.40909994067971484,Y=t=>{const e=t.valueOf();return Z(e)},Z=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(j),Math.cos(o));return{dec:Math.asin(Math.sin(j)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},K=(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},k=(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()};function V(t){t.filter(t=>"MultiPolygon"===t.geometry.type).forEach(e=>{const{geometry:r,properties:o,type:n}=e;if("MultiPolygon"===r.type)for(let o=0;o<r.coordinates.length;o++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[o]})}))});return t.filter(t=>"Polygon"===t.geometry.type).map(t=>{const{geometry:e,properties:r}=t,o=n.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})),a=n(o.vertices,o.holes,o.dimensions),s=a.length>256?new Uint16Array(a):new Uint8Array(a),u=function(t){const{height:e=0,levels:r=0,render_height:o=0}=t;if(r)return 3.04*r;return o||e}(r),l=r.highlight||!1;let c=0,h=0,f=0;for(let t=0;t<i.length;t+=2)c+=i[t],h+=i[t+1],f++;return{aPosition:i,cuts:s,buildingHeight:u,centroid:[c/f,h/f],highlight:l}})}const q=(t,e)=>{const{lat:r,lng:o}=t;return new P(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(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)*W)(r,e))},$=(t,e)=>{return new G((r=t.y,o=e,n=Math.PI-2*Math.PI*r/W/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/W/Math.pow(2,e)*360-180}(t.x,e));var r,o,n};let J,Q={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new I(new P(0,0),new P(0,0)),DEMPixelBounds:new I(new P(0,0),new P(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const tt=t=>r(void 0,void 0,void 0,function*(){const{demZoom:e,getFeatures:o,terrainSource:n,canopySource:i,dsmSource:a,tileLoaded:s,gl:u,bounds:l,buildingRasterizer:c,tileMerger:h,canopyMerger:f,forceUpdate:d=!1}=t,{getSourceUrl:m,getElevation:_,maxZoom:g,tileSize:x,_overzoom:p}=n,v=(E={getFeatures:o},r(void 0,void 0,void 0,function*(){const{getFeatures:t}=E;try{return V(yield t())}catch(t){console.log("Error merging buildings",t)}return[]}));var E;try{const t=l,r=t.getNorthWest(),o=t.getSouthEast(),n=new I(q(r,e),q(o,e));let p=new P(n.min.x,n.min.y),E=n.max.x-n.min.x;const T=n.max.y-n.min.y,y=n.max.subtract(n.min);y.y>y.x&&(p.x-=W,E+=512);const A=(t=>{const{upperLeft:e,width:r,height:o}=t,n=e.divideBy(W).floor().multiplyBy(W),i=(Math.ceil(r/W)+1)*W,a=(Math.ceil(o/W)+1)*W,s=n.add([i,a]);return new I(n,s)})({upperLeft:p,width:E,height:T}),{x:R,y:b}=A.max.subtract(A.min),M=(t=>{const{upperLeft:e,width:r,height:o,zoom:n}=t,i=e.divideBy(W).floor(),a=i.x+r/W,s=Math.min(i.y+o/W,Math.pow(2,n)-1),u=[];for(var l=i.x;l<a;l++)for(var c=i.y;c<s;c++)u.push({x:l,y:c,z:n});return u})({upperLeft:A.min,width:R,height:b,zoom:e}),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.x<t.x?e:t).x,r=t.reduce((t,e)=>e.y<t.y?e:t).y;return t.map(t=>{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)*W,yOffset:(t.y-r)*W}})})(M),F=n.max.x-n.min.x,U=n.max.y-n.min.y,D=Math.round(F),L=Math.round(U),C=JSON.stringify(w);if(!d&&C===J&&e<g)return Q=Object.assign(Object.assign({},Q),{outputWidth:D,outputHeight:L,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),Q;const S=[h.merge(w,{maxZoom:g,width:R,height:b,crossOrigin:"Anonymous",getSourceUrl:m,getElevation:_,tileSize:x,tileLoaded:s})];if(void 0!==i){const t=f.merge(w,{maxZoom:i.maxZoom,width:R,height:b,crossOrigin:"Anonymous",getSourceUrl:i.getSourceUrl,getElevation:i.getElevation,tileSize:i.tileSize,tileLoaded:s});S.push(t)}else S.push(new Promise(t=>{const e=u.createTexture();u.activeTexture(u.TEXTURE2),u.bindTexture(u.TEXTURE_2D,e),u.texImage2D(u.TEXTURE_2D,0,u.RGBA,1,1,0,u.RGBA,u.UNSIGNED_BYTE,new Uint8ClampedArray([0,0,0,0])),t(e)}));const[B,N]=yield Promise.all(S);if(null===B||null===N)return Q=Object.assign(Object.assign({},Q),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),Q;const z=new I(q(new O(a.bounds).getNorthWest(),e),q(new O(a.bounds).getSouthEast(),e)),X=z.min.subtract(A.min),G=z.max.subtract(A.min),H=[X.x/R,X.y/b,G.x/R,G.y/b].map(t=>2*t-1),j=yield v,{maxHeight:Y,heightMapTex:Z}=c.raster({upperLeftTile:w[0],width:R,height:b,mapZoom:e,features:j,imageData:B,gl:u,dsmSource:a,dsmCoords:H,canopyData:N}),K=Math.max(a.maxHeight,B.maxHeight+Y);J=C,Q={heightMapTex:Z,maxHeight:K,width:R,height:b,DEMPixelBounds:A,visibleDEMPixelBounds:n,raster:w,demZoom:e,dirty:!0,outputWidth:D,outputHeight:L}}catch(t){console.error("Could not decode height map",t)}return Q}),et=(t,e)=>{const{r:r,g:o,b:n}=t;return[r/255,o/255,n/255,e]},rt=(t,e)=>{const{date:r}=e,{dec:o,Hi:n}=Y(r);t.updateDate({dec:o,Hi:n})},ot=(t,e)=>r(void 0,void 0,void 0,function*(){return yield t.updateDateRange(Object.assign({},e))}),nt=(t,e)=>{const{color:r,opacity:o}=e,n=et(r,o);t.updateColor({colorVec:n})},it=t=>{const{kernel:e,map:r,heightMap:o,now:n,color:i,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:c,maxHeight:h,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:g}=o;if(0===f||0===d)return;const{min:x,max:p}=_;if(!x||!p)return;const{x:v,y:E}=p.subtract(x),T=m.min,y=r.getPixelDimensions(),A=r.screenUnproject([0,0]),R=r.screenUnproject([y.width,0]),b=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,b,A,R].map(t=>r.project(t,g)),F=w.map(t=>[(t.x-T.x)/f,(t.y-T.y)/d]).flat(),U=w.map(t=>[(t.x-x.x)/v,(t.y-x.y)/E]).map(t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)]).flat(),D=T.y/W/Math.pow(2,g),L=d/W/Math.pow(2,g),C=new O($(m.getTopLeft(),g),$(m.getBottomRight(),g)),P=C.getWest(),S=Math.abs(C.getWest()-C.getEast());((t,e)=>{const{color:r,opacity:o,date:n}=e,i=et(r,o),{dec:a,Hi:s}=Y(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:i,step:1},e))})(e,{heightMapTex:t,maxHeight:h,width:f,height:d,heightMapZoom:g,cornerTextureCoords:F,cornerClipCoords:U,topYCoord:D,ySize:L,west:P,dLng:S,date:n,color:i,opacity:a,outputHeight:l,outputWidth:c,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const at=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new P(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(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)*W)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new G((n=r.y,i=o,a=Math.PI-2*Math.PI*n/W/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/W/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 O(e,r)}}};class st extends o{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=>r(this,void 0,void 0,function*(){if(200!==t.status)throw new Error(yield t.text())})).catch(t=>r(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&&(rt(this._compiledKernel,{date:this.options.date}),this._heightMap&&this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=k(this.options.date)-k(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&&(nt(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&&(nt(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 r(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)rt(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()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield ot(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this})}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:o,height:n}=this._heightMap,i=e.min;return t.map(t=>{const e=q(t,r);return[(e.x-i.x)/o,(e.y-i.y)/n]})}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(),i=q(n,e),a=$(new P(i.x-r/2,i.y-o/2),e),s=$(new P(i.x+r/2,i.y+o/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return r(this,void 0,void 0,function*(){if(this.options.debug("_reset()"),this._map){const t=at(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),[i,a,s,u]=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=$(new P(512*i,512*a),o+1),c=$(new P(512*(s+1),512*(u+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 tt({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 r(this,void 0,void 0,function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(it({kernel:this._compiledKernel,map:at(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 ot(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:c}=this.options.sunExposure;this.options.sunExposure.enabled&&l&&c&&(u=c.getTime()-l.getTime());for(let t=0;t<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),n=K(o,.5,u)/1e3/60,i=Math.min(Math.floor(n/6),255);e=new Uint8Array([i])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!at(this._map).isLeaflet())return a;for(let t=0;t<o;t+=n)s.set(a.subarray(t,t+n),i-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,o=t(e,r),n=at(this._map),{lat:i,lng:a}=n.getBounds().getNorthWest(),s=n.getBounds().getSouthEast(),u=[0,0,0,a,i,0],l=[(s.lng-a)/e,(i-s.lat)/r,0];return this.options.sunExposure.enabled,{data:o,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:l,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations),r=((t,e)=>{const r=et({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=et({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 r}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=k(t.startDate)-k(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=et({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=et({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<a;r++){const o=[];for(let e=s-1;e>=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=K(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}}const ut=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:c=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,c,i,a,0,n,l,o)};function lt(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 ct{constructor(t){this.gl=t,this.program=lt({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 vCoord = normalized;\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 bool is_negative;\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) * (is_negative ? -1.0 : 1.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(1,1,1,1)) {\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.r + canopyColor.r;\n float combined = textureColor.g + canopyColor.g;\n if (combined > 1.0) {\n combined = combined - floor(combined);\n upper += 1.0 / 255.0;\n }\n gl_FragColor = vec4(textureColor.r, textureColor.g, upper, combined);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n }\n if (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n gl_FragColor = vec4(textureColor.r, textureColor.g, textureColor.r, textureColor.g);\n return;\n }\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.isNegative=t.getUniformLocation(this.program,"is_negative"),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:o,width:n,height:i,imageData:a,dsmCoords:s,dsmSource:u,canopyData:l,gl:c}=t,{x:h,y:f,z:d}=r;let m=0;c.useProgram(this.program),c.activeTexture(c.TEXTURE1),c.bindTexture(c.TEXTURE_2D,a),c.activeTexture(c.TEXTURE2),c.bindTexture(c.TEXTURE_2D,l);const _=n,g=i;c.activeTexture(c.TEXTURE0),c.bindTexture(c.TEXTURE_2D,this.targetTexture),c.texImage2D(c.TEXTURE_2D,0,c.RGBA,_,g,0,c.RGBA,c.UNSIGNED_BYTE,null),o>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);const x=c.createFramebuffer();c.bindFramebuffer(c.FRAMEBUFFER,x);const p=c.COLOR_ATTACHMENT0;c.framebufferTexture2D(c.FRAMEBUFFER,p,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=h/Math.pow(2,d),E=f/Math.pow(2,d),T=new P(v,E),y=n/W/Math.pow(2,d),A=i/W/Math.pow(2,d);c.uniform3f(this.xyzUniformLocation,v,E,d),c.uniform2f(this.dimensionsUniformLocation,y,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,1,1,1,1),c.drawArrays(c.TRIANGLE_STRIP,0,4),e.filter(t=>!t.highlight).forEach(t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:a,highlight:s}=t,u=new P(a[0],a[1]).subtract(T).unscaleBy(new P(y,A)).scaleBy(new P(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const l=u.x/n,h=u.y/i,f=5*Math.abs(e),d=Math.floor(f/255)/255,_=Math.floor(f%255)/255;m=Math.max(m,e),c.uniform2f(this.centroidUniformLocation,l,h),c.uniform1i(this.isNegative,e<0?1:0),c.uniform4f(this.colorUniformLocation,d,_,d,_),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,o,c.DYNAMIC_DRAW),c.drawElements(c.TRIANGLES,o.length,o.length>256?c.UNSIGNED_SHORT:c.UNSIGNED_BYTE,0)}),0!==u.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,u.width,u.height,0,c.LUMINANCE_ALPHA,c.UNSIGNED_BYTE,u.data),c.pixelStorei(c.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[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,1,1,1,1),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.activeTexture(c.TEXTURE1),c.bindTexture(c.TEXTURE_2D,a),c.activeTexture(c.TEXTURE2),c.bindTexture(c.TEXTURE_2D,l),c.uniform1i(this.useDSMUniformLocation,0),e.filter(t=>t.highlight).forEach(t=>{const{buildingHeight:e,aPosition:r,cuts:o,centroid:a,highlight:s}=t,u=new P(a[0],a[1]).subtract(T).unscaleBy(new P(y,A)).scaleBy(new P(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const l=u.x/n,h=u.y/i,f=5*e,d=Math.floor(f/255)/255,_=Math.floor(f%255)/255;m=Math.max(m,e),c.uniform2f(this.centroidUniformLocation,l,h),c.uniform4f(this.colorUniformLocation,d,_,d,_),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,o,c.DYNAMIC_DRAW),c.drawElements(c.TRIANGLES,o.length,o.length>256?c.UNSIGNED_SHORT:c.UNSIGNED_BYTE,0)}),c.deleteFramebuffer(x),{maxHeight:m,heightMapTex:this.targetTexture}}}const ht=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===W?n:Math.floor(n/2),y:o===W?i:Math.floor(i/2),z:o===W?a:a-1}};class ft{constructor(t){this.gl=t,this.program=lt({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 #ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n #else\n precision mediump float;\n #endif\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 if (closestColor < 3) {\n closestColor = 0;\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(r, g, 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.inProgress=[],this.finished=0}merge(t,e){return r(this,void 0,void 0,function*(){const o=this.gl;let n=0;const{width:i,height:a,crossOrigin:s,getSourceUrl:u,getElevation:l,tileSize:c,tileLoaded:h,maxZoom:f}=e;if(o){this.inProgress.forEach(t=>t.src=""),this.inProgress=[],this.finished=0;const e=new Set;t.forEach(t=>{e.add(u(ht({tile:t,maxZoom:f,tileSize:c})))}),o.useProgram(this.program);let d=0;-32768===l({r:0,g:0,b:0,a:0})&&(d=0),-1e4===l({r:0,g:0,b:0,a:0})&&(d=1),4808===l({r:256,g:0,b:0,a:0})&&(d=2),0===l({r:51,g:0,b:66,a:0})&&(d=3),o.uniform1f(this.encodingUniformLocation,d),ut({gl:o,texture:this.outputTexture,imageData:null,format:o.RGBA,width:i,height:a,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST});const m=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,m),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,this.outputTexture,0);const _=o.checkFramebufferStatus(o.FRAMEBUFFER);_!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+_),o.clear(o.COLOR_BUFFER_BIT);const g=Array.from(e).map(e=>r(this,void 0,void 0,function*(){return new Promise((r,l)=>{const h=new Image;this.inProgress.push(h),h.onload=()=>{o.useProgram(this.program),o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,this.tileTexture),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,o.RGBA,o.UNSIGNED_BYTE,h),o.bindFramebuffer(o.FRAMEBUFFER,m);const s=t.filter(t=>e===u(ht({tile:t,maxZoom:f,tileSize:c}))),l=s.reduce((t,e)=>Math.min(t,e.xOffset),1/0),d=s.reduce((t,e)=>Math.min(t,e.yOffset),1/0),_=s.reduce((t,e)=>Math.max(t,e.xOffset),0),g=s.reduce((t,e)=>Math.max(t,e.yOffset),0),x=s.reduce((t,e)=>Math.min(t,e.x),1/0),p=s.reduce((t,e)=>Math.min(t,e.y),1/0),v=s.reduce((t,e)=>Math.max(t,e.x),0),E=s.reduce((t,e)=>Math.max(t,e.y),0),T=s[0].z,y=l/i,A=d/a,R=(_+W)/i,b=(g+W)/a,M=[y,A,R,A,y,b,R,b].map(t=>2*t-1);o.bindBuffer(o.ARRAY_BUFFER,this.tilePositionBuffer),o.bufferData(o.ARRAY_BUFFER,new Float32Array(M),o.STATIC_DRAW),o.enableVertexAttribArray(this.tilePositionAttributeLocation),o.vertexAttribPointer(this.tilePositionAttributeLocation,2,o.FLOAT,!1,0,0);let w=[0,0,1,0,0,1,1,1];const F=Math.pow(2,Math.max(0,T-f)+(c!==W?1:0)),U=514===c?1/c:0,D=1/F,L=x%F/F,C=p%F/F,P=v%F/F,S=E%F/F;w=[L,C,P+D,C,L,S+D,P+D,S+D].map(t=>t*(514===c?512/514:1)+U),o.bindBuffer(o.ARRAY_BUFFER,this.texPositionBuffer),o.bufferData(o.ARRAY_BUFFER,new Float32Array(w),o.STATIC_DRAW),o.enableVertexAttribArray(this.texPositionAttributeLocation),o.vertexAttribPointer(this.texPositionAttributeLocation,2,o.FLOAT,!1,0,0),o.uniform1f(this.tileSizeUniformLocation,c),o.viewport(0,0,i,a),o.disable(o.BLEND),o.drawArrays(o.TRIANGLE_STRIP,0,4);const B=Math.floor(y*i),I=Math.floor(A*a),N=Math.ceil((R-y)*i),O=Math.ceil((b-A)*a),z=new Uint8Array(4*N*O);o.readPixels(B,I,N,O,o.RGBA,o.UNSIGNED_BYTE,z);for(let t=0;t<z.length;t+=4){const e=(256*z[t+2]+z[t+3])/5;n=Math.max(e,n)}r(null)},h.onerror=t=>{if(h.src!==h.originalSource)return l("new tiles requested");r(null)},h.crossOrigin=s||null,h.src=e,h.originalSource=h.src}).then(()=>{this.finished++,h(this.finished,this.inProgress.length)})}));try{yield Promise.all(g)}catch(t){return console.log(`${g.length} requests aborted`,t),null}return this.inProgress=[],this.outputTexture.maxHeight=n,o.deleteFramebuffer(m),this.outputTexture}throw new Error("Could not get canvas context for merging tile images")})}}class dt extends st{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:o}=t,n=e,i=lt({gl:n,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_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){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;}"});n.useProgram(i);const a=n.createBuffer(),s=n.getAttribLocation(i,"a_pos"),u=n.createBuffer(),l=n.getAttribLocation(i,"a_tex_pos"),c=n.createBuffer();n.bindBuffer(n.ARRAY_BUFFER,c),n.bufferData(n.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),n.STATIC_DRAW);const h=n.createBuffer();n.bindBuffer(n.ARRAY_BUFFER,h),n.bufferData(n.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),n.STATIC_DRAW);const f=n.getUniformLocation(i,"user_a");n.uniform1i(f,0);const d=n.getUniformLocation(i,"user_width"),m=n.getUniformLocation(i,"user_height"),_=n.getUniformLocation(i,"user_maxHeight"),g=n.getUniformLocation(i,"user_zoom"),x=n.getUniformLocation(i,"user_topYCoord"),p=n.getUniformLocation(i,"user_ySize"),v=n.getUniformLocation(i,"user_step"),E=n.getUniformLocation(i,"user_west"),T=n.getUniformLocation(i,"user_dLng"),y=n.getUniformLocation(i,"user_dec"),A=n.getUniformLocation(i,"user_Hi"),R=n.getUniformLocation(i,"user_color"),b=n.getUniformLocation(i,"u_below_canopy"),M=n.getUniformLocation(i,"user_sunExposureTexture"),w=n.getUniformLocation(i,"user_outputSunExposure"),F=n.getUniformLocation(i,"u_outputShadeProfile"),U=n.getUniformLocation(i,"u_gpxTexture"),D=n.getUniformLocation(i,"u_decHiTexture"),L=n.getUniformLocation(i,"u_sunColor"),C=n.getUniformLocation(i,"u_outputLocationShadeProfile"),P=n.getUniformLocation(i,"u_shadeProfileLocation"),S=n.createTexture();let B=0,I=0,N=0,O=null;const z=()=>{I&&N&&(n.useProgram(i),n.bindBuffer(n.ARRAY_BUFFER,a),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,u),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,0,0),null!==O&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,O)),o(n,I,N),n.viewport(0,0,I,N),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4))};let X=0,G=0,H=0,W="";const j=t=>{const e=(t.endTime-t.startTime)/86400/1e3,r=1440;if(n.activeTexture(n.TEXTURE3),n.uniform1i(D,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const o=[],i=828e5,a=k(new Date(G),W);for(let t=0;t<e;t++){const e=k(new Date(G+86400*t*1e3+i),W)-a;o[t]=e}const s=new Float32Array(r*e*2);for(let t=0;t<r;t++)for(let r=0;r<e;r++){const n=G+86400*r*1e3+60*t*1e3,{dec:i,Hi:a}=Z(n+o[r]);s[2*(t*e+r)]=-i,s[2*(t*e+r)+1]=a/10}ut({gl:n,texture:S,imageData:s,width:e,height:r,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RG,internalFormat:n.RG32F,type:n.FLOAT})}else n.bindTexture(n.TEXTURE_2D,S);return{outputWidth:e,outputHeight:r}};return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:o,maxHeight:s,heightMapZoom:l,topYCoord:c,ySize:h,colorVec:f,step:M,west:w,dLng:F,dec:U,Hi:D,cornerClipCoords:L,cornerTextureCoords:C,outputWidth:P,outputHeight:S,belowCanopy:X,skipRender:G}=t;n.useProgram(i),I=P,N=S,O=e,n.bindBuffer(n.ARRAY_BUFFER,a),n.bufferData(n.ARRAY_BUFFER,new Float32Array(L),n.STATIC_DRAW),n.bindBuffer(n.ARRAY_BUFFER,u),n.bufferData(n.ARRAY_BUFFER,new Float32Array(C),n.STATIC_DRAW),n.uniform1f(d,r),n.uniform1f(m,o),n.uniform1f(_,s),n.uniform1f(g,l),n.uniform1f(x,c),n.uniform1f(p,h),n.uniform4fv(R,f),n.uniform1f(v,M),n.uniform1f(E,w),n.uniform1f(T,F),n.uniform1f(y,U),n.uniform1f(A,D),n.uniform1f(b,X?1:0),G||(window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z))},updateDate:t=>{const{dec:e,Hi:r}=t;n.useProgram(i),n.uniform1f(y,e),n.uniform1f(A,r),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},updateDateRange:t=>r(this,void 0,void 0,function*(){const{startDate:e,endDate:r,iterations:o,emit:c}=t;n.useProgram(i);const h=X=Date.now(),f=Math.floor((r.getTime()-e.getTime())/o),d=n.getUniform(i,R),m=I,_=N,g=n.createTexture();n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,g),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,m,_,0,n.RGBA,n.UNSIGNED_BYTE,null),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST);const x=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,x);const p=n.COLOR_ATTACHMENT0;n.framebufferTexture2D(n.FRAMEBUFFER,p,n.TEXTURE_2D,g,0);for(let t=0;t<o;t++){if(n.useProgram(i),c("tileloaded",t,o-1),h!==X)return n.deleteFramebuffer(x),n.deleteTexture(g),n.uniform4fv(R,d),!0;yield new Promise((r,c)=>{window.requestAnimationFrame(()=>{n.useProgram(i),null!==O&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,O)),n.bindFramebuffer(n.FRAMEBUFFER,x);const c=n.checkFramebufferStatus(n.FRAMEBUFFER);c!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+c);const{dec:h,Hi:d}=Y(new Date(e.getTime()+f*t));n.uniform1f(y,h),n.uniform1f(A,d),n.uniform4fv(R,[1/o,0,0,1]),n.bindBuffer(n.ARRAY_BUFFER,a),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,u),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,0,0),n.viewport(0,0,m,_),n.enable(n.BLEND),n.blendFunc(n.ONE,n.ONE),n.drawArrays(n.TRIANGLE_STRIP,0,4),r()})})}return n.deleteFramebuffer(x),yield new Promise((t,e)=>{window.requestAnimationFrame(()=>{if(n.useProgram(i),h!==X)return n.deleteTexture(g),n.uniform4fv(R,d),void t(!0);n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,g),n.uniform1i(M,2),n.uniform1i(w,1),z(),n.uniform1i(w,0),n.uniform1i(M,0),n.deleteTexture(g),n.uniform4fv(R,d),t(!1)})})}),updateColor:t=>{const{colorVec:e}=t;n.useProgram(i),n.uniform4fv(R,e),window.cancelAnimationFrame(B),B=window.requestAnimationFrame(z)},generateShadeProfile:t=>{n.useProgram(i);const{dates:e,texCoords:r,shadeColor:o,sunColor:a}=t,u=r.length,f=e.length,d=n.getUniform(i,R);n.uniform4fv(R,o),n.uniform4fv(L,a),n.uniform1i(F,1);const m=n.createTexture();n.activeTexture(n.TEXTURE3),ut({gl:n,imageData:null,width:u,height:f,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,texture:m});const _=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,_),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,m,0);const g=n.checkFramebufferStatus(n.FRAMEBUFFER);g!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+g);const x=r.map(t=>[t[0],t[1]]).flat(),p=n.createTexture();n.activeTexture(n.TEXTURE2),ut({gl:n,texture:p,imageData:new Float32Array(x),width:x.length/2,height:1,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RG,internalFormat:n.RG32F,type:n.FLOAT}),n.uniform1i(U,2);const v=e.map(t=>{const{dec:e,Hi:r}=Y(t);return[-e,r/10]}).flat(),E=n.createTexture();n.activeTexture(n.TEXTURE1),ut({gl:n,texture:E,imageData:new Float32Array(v),width:1,height:e.length,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RG,internalFormat:n.RG32F,type:n.FLOAT}),n.uniform1i(D,1),null!==O&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,O)),n.bindBuffer(n.ARRAY_BUFFER,c),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,h),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,0,0),n.viewport(0,0,u,f),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4);const T=new Uint8Array(u*f*4);return n.readPixels(0,0,u,f,n.RGBA,n.UNSIGNED_BYTE,T),n.deleteTexture(m),n.deleteTexture(E),n.deleteTexture(p),n.deleteFramebuffer(_),n.uniform1i(D,0),n.uniform1i(U,0),n.uniform1i(F,0),n.uniform4fv(R,d),n.uniform4fv(L,[0,0,0,0]),T},generateLocationShadeProfile:t=>{n.useProgram(i);const{startTime:e,endTime:r,tzId:o,texCoord:a,shadeColor:u,sunColor:f}=t,d=n.getUniform(i,R),{outputWidth:m,outputHeight:_}=j({startTime:e,endTime:r,tzId:o});n.uniform4fv(R,u),n.uniform4fv(L,f),n.uniform1i(C,1),n.uniform2fv(P,[a[0],a[1]]);const g=n.createTexture();n.activeTexture(n.TEXTURE2),ut({gl:n,imageData:null,width:m,height:_,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,texture:g});const x=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,x),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,g,0);const p=n.checkFramebufferStatus(n.FRAMEBUFFER);p!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+p),null!==O&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,O)),n.bindBuffer(n.ARRAY_BUFFER,c),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,h),n.enableVertexAttribArray(l),n.vertexAttribPointer(l,2,n.FLOAT,!1,0,0),n.viewport(0,0,m,_),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4);const v=new Uint8Array(m*_*4);return n.readPixels(0,0,m,_,n.RGBA,n.UNSIGNED_BYTE,v),n.deleteTexture(g),n.deleteFramebuffer(x),n.uniform1i(D,0),n.uniform1i(C,0),n.uniform4fv(R,d),n.uniform4fv(L,[0,0,0,0]),{output:v,outputWidth:m,outputHeight:_}}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new ct(this._gl),this._tileMerger=new ft(this._gl),this._canopyMerger=new ft(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new e(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">&copy; ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}return void 0!==window.L&&(window.L.shadeMap=(...t)=>new dt(...t)),dt});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leaflet-shadow-simulator",
3
- "version": "0.61.0",
3
+ "version": "0.61.1",
4
4
  "description": "Display terrain and structure shadows cast by the sun in a custom Leaflet layer",
5
5
  "main": "dist/leaflet-shadow-simulator.umd.min.js",
6
6
  "browser": "dist/leaflet-shadow-simulator.umd.min.js",