leaflet-shadow-simulator 0.57.0 → 0.57.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 2024
3
3
  * Package: leaflet-shadow-simulator
4
- * Version: 0.57.0
4
+ * Version: 0.57.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 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())}))}function i(t,e,r){r=r||2;var o,n,i,s,l,h,c,f=e&&e.length,_=f?e[0]*r:t.length,x=a(t,0,_,r,!0),p=[];if(!x||x.next===x.prev)return p;if(f&&(x=function(t,e,r,o){var n,i,s,u=[];for(n=0,i=e.length;n<i;n++)(s=a(t,e[n]*o,n<i-1?e[n+1]*o:t.length,o,!1))===s.next&&(s.steiner=!0),u.push(g(s));for(u.sort(d),n=0;n<u.length;n++)r=m(u[n],r);return r}(t,e,x,r)),t.length>80*r){o=i=t[0],n=s=t[1];for(var E=r;E<_;E+=r)(l=t[E])<o&&(o=l),(h=t[E+1])<n&&(n=h),l>i&&(i=l),h>s&&(s=h);c=0!==(c=Math.max(i-o,s-n))?32767/c:0}return u(x,p,r,o,n,c,0),p}function a(t,e,r,o,n){var i,a;if(n===U(t,e,r,o)>0)for(i=e;i<r;i+=o)a=w(i,t[i],t[i+1],a);else for(i=r-o;i>=e;i-=o)a=w(i,t[i],t[i+1],a);return a&&T(a,a.next)&&(F(a),a=a.next),a}function s(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!T(o,o.next)&&0!==v(o.prev,o,o.next))o=o.next;else{if(F(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function u(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 d,m,_=t;t.prev!==t.next;)if(d=t.prev,m=t.next,i?h(t,o,n,i):l(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),F(t),t=m.next,_=m.next;else if((t=m)===_){a?1===a?u(t=c(s(t),e,r),e,r,o,n,i,2):2===a&&f(t,e,r,o,n,i):u(s(t),e,r,o,n,i,1);break}}}function l(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,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&&p(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 h(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,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,E=x(d,m,e,r,o),T=x(_,g,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=E&&b&&b.z<=T;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&p(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<=_&&b.y>=m&&b.y<=g&&b!==n&&b!==a&&p(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<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&p(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<=_&&b.y>=m&&b.y<=g&&b!==n&&b!==a&&p(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 c(t,e,r){var o=t;do{var n=o.prev,i=o.next.next;!T(n,i)&&y(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),F(o),F(o.next),o=t=i),o=o.next}while(o!==t);return s(o)}function f(t,e,r,o,n,i){var a=t;do{for(var l=a.next.next;l!==a.prev;){if(a.i!==l.i&&E(a,l)){var h=M(a,l);return a=s(a,a.next),h=s(h,h.next),u(a,e,r,o,n,i,0),void u(h,e,r,o,n,i,0)}l=l.next}a=a.next}while(a!==t)}function d(t,e){return t.x-e.x}function m(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&&p(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),R(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&_(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=M(r,t);return s(o,o.next),s(r,r.next)}function _(t,e){return v(t.prev,t,e.prev)<0&&v(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 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 p(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&&y(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))||T(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 T(t,e){return t.x===e.x&&t.y===e.y}function y(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||!b(t,r,e))||(!(0!==i||!b(t,o,e))||(!(0!==a||!b(r,t,o))||!(0!==s||!b(r,e,o)))))}function b(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 M(t,e){var r=new L(t.i,t.x,t.y),o=new L(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 w(t,e,r,o){var n=new L(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 F(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 L(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 P(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 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,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,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 C(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var B=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function I(t,e,r){return t instanceof C?t:S(t)?new C(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new C(t.x,t.y):new C(t,e,r)}function N(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function O(t,e){return!t||t instanceof N?t:new N(t,e)}function z(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function X(t,e){return t instanceof z?t:new z(t,e)}C.prototype={clone:function(){return new C(this.x,this.y)},add:function(t){return this.clone()._add(I(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(I(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 C(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new C(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=B(this.x),this.y=B(this.y),this},distanceTo:function(t){var e=(t=I(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=I(t)).x===this.x&&t.y===this.y},contains:function(t){return t=I(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)+")"}},N.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof C||"number"==typeof t[0]||"x"in t)e=r=I(t);else if(e=(t=O(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 I((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return I(this.min.x,this.max.y)},getTopRight:function(){return I(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 C?I(t):O(t))instanceof N?(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=O(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=O(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 O(I(e.x-o,e.y-n),I(r.x+o,r.y+n))},equals:function(t){return!!t&&(t=O(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},z.prototype={extend:function(t){var e,r,o=this._southWest,n=this._northEast;if(t instanceof H)e=t,r=t;else{if(!(t instanceof z))return t?this.extend(W(t)||X(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 H(e.lat,e.lng),this._northEast=new H(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 z(new H(e.lat-o,e.lng-n),new H(r.lat+o,r.lng+n))},getCenter:function(){return new H((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 H(this.getNorth(),this.getWest())},getSouthEast:function(){return new H(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 H||"lat"in t?W(t):X(t);var e,r,o=this._southWest,n=this._northEast;return t instanceof z?(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=X(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=X(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=X(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var G=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 N(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 H(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 z(new H(i.lat-o,i.lng-n),new H(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 H(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 W(t,e,r){return t instanceof H?t:S(t)&&"object"!=typeof t[0]?3===t.length?new H(t[0],t[1],t[2]):2===t.length?new H(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new H(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new H(t,e,r)}H.prototype={equals:function(t,e){return!!t&&(t=W(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 G.distance(this,W(t))},wrap:function(){return G.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return X([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new H(this.lat,this.lng,this.alt)}};const j=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(.40909994067971484),Math.cos(o));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},Y=(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 k(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,{vertices:o}=i.flatten(e.coordinates),n=new Float32Array(o.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(n),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 Math.max(e,o)}(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 V=(t,e)=>{const{lat:r,lng:o}=t;return new C(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(o,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},q=(t,e)=>{return new H((r=t.y,o=e,n=Math.PI-2*Math.PI*r/256/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,o,n};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new N(new C(0,0),new C(0,0)),DEMPixelBounds:new N(new C(0,0),new C(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=t=>n(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:o,dsmSource:i,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:l,tileMerger:h,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:d,maxZoom:m,tileSize:_,_overzoom:x}=o,g=(p={getFeatures:r},n(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return k(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var p;try{const t=u,r=t.getNorthWest(),o=t.getSouthEast(),n=new N(V(r,e),V(o,e));let x=new C(n.min.x,n.min.y),p=n.max.x-n.min.x;const E=n.max.y-n.min.y,v=n.max.subtract(n.min);v.y>v.x&&(x.x-=256,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,n=e.divideBy(256).floor().multiplyBy(256),i=256*(Math.ceil(r/256)+1),a=256*(Math.ceil(o/256)+1),s=n.add([i,a]);return new N(n,s)})({upperLeft:x,width:p,height:E}),{x:y,y:b}=T.max.subtract(T.min),A=(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:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:n}=t,i=e.divideBy(256).floor(),a=i.x+r/256,s=Math.min(i.y+o/256,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:T.min,width:y,height:b,zoom:e})),R=n.max.x-n.min.x,M=n.max.y-n.min.y,w=Math.round(R),F=Math.round(M),L=JSON.stringify(A);if(!c&&L===$&&e<m)return J=Object.assign(Object.assign({},J),{outputWidth:w,outputHeight:F,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),J;const U=yield h.merge(A,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===U)return J=Object.assign(Object.assign({},J),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),J;const D=new N(V(new z(i.bounds).getNorthWest(),e),V(new z(i.bounds).getSouthEast(),e)),P=D.min.subtract(T.min),S=D.max.subtract(T.min),B=[P.x/y,P.y/b,S.x/y,S.y/b].map((t=>2*t-1)),I=yield g,{maxHeight:O,heightMapTex:X}=l.raster({upperLeftTile:A[0],width:y,height:b,mapZoom:e,features:I,imageData:U,gl:s,dsmSource:i,dsmCoords:B}),G=Math.max(i.maxHeight,U.maxHeight+O);$=L,J={heightMapTex:X,maxHeight:G,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:n,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:F}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:o,b:n}=t;return[r/255,o/255,n/255,e]},et=(t,e)=>{const{date:r}=e,{dec:o,Hi:n}=j(r);t.updateDate({dec:o,Hi:n})},rt=(t,e)=>n(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ot=(t,e)=>{const{color:r,opacity:o}=e,n=tt(r,o);t.updateColor({colorVec:n})},nt=t=>{const{kernel:e,map:r,heightMap:o,now:n,color:i,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:h,maxHeight:c,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:x}=o;if(0===f||0===d)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:E,y:v}=p.subtract(g),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,x))),F=w.map((t=>[(t.x-T.x)/f,(t.y-T.y)/d])).flat(),L=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/v])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),U=T.y/256/Math.pow(2,x),D=d/256/Math.pow(2,x),P=new z(q(m.getTopLeft(),x),q(m.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:n}=e,i=tt(r,o),{dec:a,Hi:s}=j(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:i,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:x,cornerTextureCoords:F,cornerClipCoords:L,topYCoord:U,ySize:D,west:S,dLng:C,date:n,color:i,opacity:a,outputHeight:l,outputWidth:h,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const it=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new C(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,o),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new H((n=r.y,i=o,a=Math.PI-2*Math.PI*n/256/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,o));var 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 z(e,r)}}};const at=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 st(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 ut{constructor(t){this.gl=t,this.program=st({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width:n,height:i,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=n,_=i;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 x=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,x);const g=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,g,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 C(p,E),T=n/256/Math.pow(2,f),y=i/256/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 C(a[0],a[1]).subtract(v).unscaleBy(new C(T,y)).scaleBy(new C(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const h=u.x/n,c=u.y/i,f=5*e,m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),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(x),{maxHeight:d,heightMapTex:this.targetTexture}}}const lt=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:256===o?n:Math.floor(n/2),y:256===o?i:Math.floor(i/2),z:256===o?a:a-1}};class ht{constructor(t){this.gl=t,this.program=st({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 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(lt({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),at({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:i,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=>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,this.framebuffer);const s=t.filter((t=>e===u(lt({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),x=s.reduce(((t,e)=>Math.min(t,e.x)),1/0),g=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/i,y=d/a,b=(m+256)/i,A=(_+256)/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)+(256!==h?1:0)),F=514===h?1/h:0,L=1/w,U=x%w/w,D=g%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,i,a),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const C=Math.floor(T*i),B=Math.floor(y*a),I=Math.ceil((b-T)*i),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)}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(_)}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 ct extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},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&&(et(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&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}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)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield rt(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=V(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=V(n,e),a=q(new C(i.x-r/2,i.y-o/2),e),s=q(new C(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=it(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 C(512*i,512*a),o+1),h=q(new C(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 Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return n(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(nt({kernel:this._compiledKernel,map:it(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const n=new Uint8Array(r*o*4);return this._gl&&this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n),n}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),o=t*e*1,n=1*t,i=(e-1)*n,a=new Uint8Array(o),s=new Uint8Array(o);let u=0;const{startDate:l,endDate:h}=this.options.sunExposure;this.options.sunExposure.enabled&&l&&h&&(u=h.getTime()-l.getTime());for(let t=0;t<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),n=Y(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&&!it(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=it(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);return((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=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=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord:n}));return i.toArray=function(){const e=new Array;for(let r=0;r<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=Y(r,.5,i);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);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=st({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"),x=o.getUniformLocation(i,"user_zoom"),g=o.getUniformLocation(i,"user_topYCoord"),p=o.getUniformLocation(i,"user_ySize"),E=o.getUniformLocation(i,"user_step"),v=o.getUniformLocation(i,"user_west"),T=o.getUniformLocation(i,"user_dLng"),y=o.getUniformLocation(i,"user_dec"),b=o.getUniformLocation(i,"user_Hi"),A=o.getUniformLocation(i,"user_color"),R=o.getUniformLocation(i,"u_below_canopy"),M=o.getUniformLocation(i,"user_sunExposureTexture"),w=o.getUniformLocation(i,"user_outputSunExposure"),F=o.getUniformLocation(i,"u_outputShadeProfile"),L=o.getUniformLocation(i,"u_gpxTexture"),U=o.getUniformLocation(i,"u_decHiTexture"),D=o.getUniformLocation(i,"u_sunColor"),P=o.getUniformLocation(i,"u_outputLocationShadeProfile"),S=o.getUniformLocation(i,"u_shadeProfileLocation"),C=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(U,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const n=[],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;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}=Z(o+n[r]);s[2*(t*e+r)]=-i,s[2*(t*e+r)+1]=a/10}at({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:n,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(i),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,n),o.uniform1f(_,s),o.uniform1f(x,l),o.uniform1f(g,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(i),o.uniform1f(y,e),o.uniform1f(b,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,A),m=I,_=N,x=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,x),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 g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g);const p=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,p,o.TEXTURE_2D,x,0);for(let t=0;t<n;t++){if(o.useProgram(i),h("tileloaded",t,n-1),c!==X)return o.deleteFramebuffer(g),o.deleteTexture(x),o.uniform4fv(A,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,g);const h=o.checkFramebufferStatus(o.FRAMEBUFFER);h!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=j(new Date(e.getTime()+f*t));o.uniform1f(y,c),o.uniform1f(b,d),o.uniform4fv(A,[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(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(o.useProgram(i),c!==X)return o.deleteTexture(x),o.uniform4fv(A,d),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,x),o.uniform1i(M,2),o.uniform1i(w,1),z(),o.uniform1i(w,0),o.uniform1i(M,0),o.deleteTexture(x),o.uniform4fv(A,d),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;o.useProgram(i),o.uniform4fv(A,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,A);o.uniform4fv(A,n),o.uniform4fv(D,a),o.uniform1i(F,1);const m=o.createTexture();o.activeTexture(o.TEXTURE3),at({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 x=o.checkFramebufferStatus(o.FRAMEBUFFER);x!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1]])).flat(),p=o.createTexture();o.activeTexture(o.TEXTURE2),at({gl:o,texture:p,imageData:new Float32Array(g),width:g.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}=j(t);return[-e,r/10]})).flat(),v=o.createTexture();o.activeTexture(o.TEXTURE1),at({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(i);const{startTime:e,endTime:r,tzId:n,texCoord:a,shadeColor:u,sunColor:f}=t,d=o.getUniform(i,A),{outputWidth:m,outputHeight:_}=Y({startTime:e,endTime:r,tzId:n});o.uniform4fv(A,u),o.uniform4fv(D,f),o.uniform1i(P,1),o.uniform2fv(S,[a[0],a[1]]);const x=o.createTexture();o.activeTexture(o.TEXTURE2),at({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:x});const g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,x,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(x),o.deleteFramebuffer(g),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 ut(this._gl),this._tileMerger=new ht(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 ct(...t));export{ct 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())}))}function i(t,e,r){r=r||2;var o,n,i,s,l,h,c,f=e&&e.length,_=f?e[0]*r:t.length,x=a(t,0,_,r,!0),p=[];if(!x||x.next===x.prev)return p;if(f&&(x=function(t,e,r,o){var n,i,s,u=[];for(n=0,i=e.length;n<i;n++)(s=a(t,e[n]*o,n<i-1?e[n+1]*o:t.length,o,!1))===s.next&&(s.steiner=!0),u.push(g(s));for(u.sort(d),n=0;n<u.length;n++)r=m(u[n],r);return r}(t,e,x,r)),t.length>80*r){o=i=t[0],n=s=t[1];for(var E=r;E<_;E+=r)(l=t[E])<o&&(o=l),(h=t[E+1])<n&&(n=h),l>i&&(i=l),h>s&&(s=h);c=0!==(c=Math.max(i-o,s-n))?32767/c:0}return u(x,p,r,o,n,c,0),p}function a(t,e,r,o,n){var i,a;if(n===U(t,e,r,o)>0)for(i=e;i<r;i+=o)a=w(i,t[i],t[i+1],a);else for(i=r-o;i>=e;i-=o)a=w(i,t[i],t[i+1],a);return a&&T(a,a.next)&&(F(a),a=a.next),a}function s(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!T(o,o.next)&&0!==v(o.prev,o,o.next))o=o.next;else{if(F(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function u(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 d,m,_=t;t.prev!==t.next;)if(d=t.prev,m=t.next,i?h(t,o,n,i):l(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(m.i/r|0),F(t),t=m.next,_=m.next;else if((t=m)===_){a?1===a?u(t=c(s(t),e,r),e,r,o,n,i,2):2===a&&f(t,e,r,o,n,i):u(s(t),e,r,o,n,i,1);break}}}function l(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,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&&p(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 h(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,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,E=x(d,m,e,r,o),T=x(_,g,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=E&&b&&b.z<=T;){if(y.x>=d&&y.x<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&p(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<=_&&b.y>=m&&b.y<=g&&b!==n&&b!==a&&p(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<=_&&y.y>=m&&y.y<=g&&y!==n&&y!==a&&p(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<=_&&b.y>=m&&b.y<=g&&b!==n&&b!==a&&p(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 c(t,e,r){var o=t;do{var n=o.prev,i=o.next.next;!T(n,i)&&y(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),F(o),F(o.next),o=t=i),o=o.next}while(o!==t);return s(o)}function f(t,e,r,o,n,i){var a=t;do{for(var l=a.next.next;l!==a.prev;){if(a.i!==l.i&&E(a,l)){var h=M(a,l);return a=s(a,a.next),h=s(h,h.next),u(a,e,r,o,n,i,0),void u(h,e,r,o,n,i,0)}l=l.next}a=a.next}while(a!==t)}function d(t,e){return t.x-e.x}function m(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&&p(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),R(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&_(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=M(r,t);return s(o,o.next),s(r,r.next)}function _(t,e){return v(t.prev,t,e.prev)<0&&v(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 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 p(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&&y(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))||T(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 T(t,e){return t.x===e.x&&t.y===e.y}function y(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||!b(t,r,e))||(!(0!==i||!b(t,o,e))||(!(0!==a||!b(r,t,o))||!(0!==s||!b(r,e,o)))))}function b(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 M(t,e){var r=new L(t.i,t.x,t.y),o=new L(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 w(t,e,r,o){var n=new L(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 F(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 L(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 P(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 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,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,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 C(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var B=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function I(t,e,r){return t instanceof C?t:S(t)?new C(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new C(t.x,t.y):new C(t,e,r)}function N(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function O(t,e){return!t||t instanceof N?t:new N(t,e)}function z(t,e){if(t)for(var r=e?[t,e]:t,o=0,n=r.length;o<n;o++)this.extend(r[o])}function X(t,e){return t instanceof z?t:new z(t,e)}C.prototype={clone:function(){return new C(this.x,this.y)},add:function(t){return this.clone()._add(I(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(I(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 C(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new C(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=B(this.x),this.y=B(this.y),this},distanceTo:function(t){var e=(t=I(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=I(t)).x===this.x&&t.y===this.y},contains:function(t){return t=I(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)+")"}},N.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof C||"number"==typeof t[0]||"x"in t)e=r=I(t);else if(e=(t=O(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 I((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return I(this.min.x,this.max.y)},getTopRight:function(){return I(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 C?I(t):O(t))instanceof N?(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=O(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=O(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 O(I(e.x-o,e.y-n),I(r.x+o,r.y+n))},equals:function(t){return!!t&&(t=O(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},z.prototype={extend:function(t){var e,r,o=this._southWest,n=this._northEast;if(t instanceof H)e=t,r=t;else{if(!(t instanceof z))return t?this.extend(W(t)||X(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 H(e.lat,e.lng),this._northEast=new H(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 z(new H(e.lat-o,e.lng-n),new H(r.lat+o,r.lng+n))},getCenter:function(){return new H((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 H(this.getNorth(),this.getWest())},getSouthEast:function(){return new H(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 H||"lat"in t?W(t):X(t);var e,r,o=this._southWest,n=this._northEast;return t instanceof z?(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=X(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=X(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=X(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var G=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 N(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 H(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 z(new H(i.lat-o,i.lng-n),new H(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 H(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 W(t,e,r){return t instanceof H?t:S(t)&&"object"!=typeof t[0]?3===t.length?new H(t[0],t[1],t[2]):2===t.length?new H(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new H(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new H(t,e,r)}H.prototype={equals:function(t,e){return!!t&&(t=W(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 G.distance(this,W(t))},wrap:function(){return G.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return X([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new H(this.lat,this.lng,this.alt)}};const j=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(.40909994067971484),Math.cos(o));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},Y=(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 k(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=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 Math.max(e,o)}(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 V=(t,e)=>{const{lat:r,lng:o}=t;return new C(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(o,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},q=(t,e)=>{return new H((r=t.y,o=e,n=Math.PI-2*Math.PI*r/256/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,o,n};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new N(new C(0,0),new C(0,0)),DEMPixelBounds:new N(new C(0,0),new C(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=t=>n(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:o,dsmSource:i,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:l,tileMerger:h,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:d,maxZoom:m,tileSize:_,_overzoom:x}=o,g=(p={getFeatures:r},n(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return k(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var p;try{const t=u,r=t.getNorthWest(),o=t.getSouthEast(),n=new N(V(r,e),V(o,e));let x=new C(n.min.x,n.min.y),p=n.max.x-n.min.x;const E=n.max.y-n.min.y,v=n.max.subtract(n.min);v.y>v.x&&(x.x-=256,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,n=e.divideBy(256).floor().multiplyBy(256),i=256*(Math.ceil(r/256)+1),a=256*(Math.ceil(o/256)+1),s=n.add([i,a]);return new N(n,s)})({upperLeft:x,width:p,height:E}),{x:y,y:b}=T.max.subtract(T.min),A=(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:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:n}=t,i=e.divideBy(256).floor(),a=i.x+r/256,s=Math.min(i.y+o/256,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:T.min,width:y,height:b,zoom:e})),R=n.max.x-n.min.x,M=n.max.y-n.min.y,w=Math.round(R),F=Math.round(M),L=JSON.stringify(A);if(!c&&L===$&&e<m)return J=Object.assign(Object.assign({},J),{outputWidth:w,outputHeight:F,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),J;const U=yield h.merge(A,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===U)return J=Object.assign(Object.assign({},J),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),J;const D=new N(V(new z(i.bounds).getNorthWest(),e),V(new z(i.bounds).getSouthEast(),e)),P=D.min.subtract(T.min),S=D.max.subtract(T.min),B=[P.x/y,P.y/b,S.x/y,S.y/b].map((t=>2*t-1)),I=yield g,{maxHeight:O,heightMapTex:X}=l.raster({upperLeftTile:A[0],width:y,height:b,mapZoom:e,features:I,imageData:U,gl:s,dsmSource:i,dsmCoords:B}),G=Math.max(i.maxHeight,U.maxHeight+O);$=L,J={heightMapTex:X,maxHeight:G,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:n,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:F}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:o,b:n}=t;return[r/255,o/255,n/255,e]},et=(t,e)=>{const{date:r}=e,{dec:o,Hi:n}=j(r);t.updateDate({dec:o,Hi:n})},rt=(t,e)=>n(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ot=(t,e)=>{const{color:r,opacity:o}=e,n=tt(r,o);t.updateColor({colorVec:n})},nt=t=>{const{kernel:e,map:r,heightMap:o,now:n,color:i,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:l,outputWidth:h,maxHeight:c,width:f,height:d,DEMPixelBounds:m,visibleDEMPixelBounds:_,demZoom:x}=o;if(0===f||0===d)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:E,y:v}=p.subtract(g),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,x))),F=w.map((t=>[(t.x-T.x)/f,(t.y-T.y)/d])).flat(),L=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/v])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),U=T.y/256/Math.pow(2,x),D=d/256/Math.pow(2,x),P=new z(q(m.getTopLeft(),x),q(m.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:n}=e,i=tt(r,o),{dec:a,Hi:s}=j(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:i,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:x,cornerTextureCoords:F,cornerClipCoords:L,topYCoord:U,ySize:D,west:S,dLng:C,date:n,color:i,opacity:a,outputHeight:l,outputWidth:h,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const it=t=>{const e=()=>!t.getPitch;return{project:(r,o)=>{if(e())return t.project(r,o);{const{lat:t,lng:e}=r;return new C(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,o),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new H((n=r.y,i=o,a=Math.PI-2*Math.PI*n/256/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,o));var 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 z(e,r)}}};const at=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 st(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 ut{constructor(t){this.gl=t,this.program=st({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width:n,height:i,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=n,_=i;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 x=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,x);const g=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,g,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 C(p,E),T=n/256/Math.pow(2,f),y=i/256/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 C(a[0],a[1]).subtract(v).unscaleBy(new C(T,y)).scaleBy(new C(n,i)).floor();if(u.x<0||u.y<0||u.x>n||u.y>i)return;const h=u.x/n,c=u.y/i,f=5*e,m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),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(x),{maxHeight:d,heightMapTex:this.targetTexture}}}const lt=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:256===o?n:Math.floor(n/2),y:256===o?i:Math.floor(i/2),z:256===o?a:a-1}};class ht{constructor(t){this.gl=t,this.program=st({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 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(lt({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),at({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:i,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=>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,this.framebuffer);const s=t.filter((t=>e===u(lt({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),x=s.reduce(((t,e)=>Math.min(t,e.x)),1/0),g=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/i,y=d/a,b=(m+256)/i,A=(_+256)/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)+(256!==h?1:0)),F=514===h?1/h:0,L=1/w,U=x%w/w,D=g%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,i,a),r.disable(r.BLEND),r.drawArrays(r.TRIANGLE_STRIP,0,4);const C=Math.floor(T*i),B=Math.floor(y*a),I=Math.ceil((b-T)*i),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)}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(_)}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 ct extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},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&&(et(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&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}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)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield rt(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=V(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=V(n,e),a=q(new C(i.x-r/2,i.y-o/2),e),s=q(new C(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=it(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 C(512*i,512*a),o+1),h=q(new C(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 Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return n(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(nt({kernel:this._compiledKernel,map:it(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const n=new Uint8Array(r*o*4);return this._gl&&this._gl.readPixels(t,e,r,o,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n),n}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),o=t*e*1,n=1*t,i=(e-1)*n,a=new Uint8Array(o),s=new Uint8Array(o);let u=0;const{startDate:l,endDate:h}=this.options.sunExposure;this.options.sunExposure.enabled&&l&&h&&(u=h.getTime()-l.getTime());for(let t=0;t<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const o=r.subarray(t,t+3),n=Y(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&&!it(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=it(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);return((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=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=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord:n}));return i.toArray=function(){const e=new Array;for(let r=0;r<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=Y(r,.5,i);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);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=st({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"),x=o.getUniformLocation(i,"user_zoom"),g=o.getUniformLocation(i,"user_topYCoord"),p=o.getUniformLocation(i,"user_ySize"),E=o.getUniformLocation(i,"user_step"),v=o.getUniformLocation(i,"user_west"),T=o.getUniformLocation(i,"user_dLng"),y=o.getUniformLocation(i,"user_dec"),b=o.getUniformLocation(i,"user_Hi"),A=o.getUniformLocation(i,"user_color"),R=o.getUniformLocation(i,"u_below_canopy"),M=o.getUniformLocation(i,"user_sunExposureTexture"),w=o.getUniformLocation(i,"user_outputSunExposure"),F=o.getUniformLocation(i,"u_outputShadeProfile"),L=o.getUniformLocation(i,"u_gpxTexture"),U=o.getUniformLocation(i,"u_decHiTexture"),D=o.getUniformLocation(i,"u_sunColor"),P=o.getUniformLocation(i,"u_outputLocationShadeProfile"),S=o.getUniformLocation(i,"u_shadeProfileLocation"),C=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(U,3),t.startTime!==G||t.endTime!==H||t.tzId!==W){G=t.startTime,H=t.endTime,W=t.tzId;const n=[],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;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}=Z(o+n[r]);s[2*(t*e+r)]=-i,s[2*(t*e+r)+1]=a/10}at({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:n,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(i),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,n),o.uniform1f(_,s),o.uniform1f(x,l),o.uniform1f(g,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(i),o.uniform1f(y,e),o.uniform1f(b,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,A),m=I,_=N,x=o.createTexture();o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,x),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 g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g);const p=o.COLOR_ATTACHMENT0;o.framebufferTexture2D(o.FRAMEBUFFER,p,o.TEXTURE_2D,x,0);for(let t=0;t<n;t++){if(o.useProgram(i),h("tileloaded",t,n-1),c!==X)return o.deleteFramebuffer(g),o.deleteTexture(x),o.uniform4fv(A,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,g);const h=o.checkFramebufferStatus(o.FRAMEBUFFER);h!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=j(new Date(e.getTime()+f*t));o.uniform1f(y,c),o.uniform1f(b,d),o.uniform4fv(A,[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(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(o.useProgram(i),c!==X)return o.deleteTexture(x),o.uniform4fv(A,d),void t(!0);o.activeTexture(o.TEXTURE2),o.bindTexture(o.TEXTURE_2D,x),o.uniform1i(M,2),o.uniform1i(w,1),z(),o.uniform1i(w,0),o.uniform1i(M,0),o.deleteTexture(x),o.uniform4fv(A,d),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;o.useProgram(i),o.uniform4fv(A,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,A);o.uniform4fv(A,n),o.uniform4fv(D,a),o.uniform1i(F,1);const m=o.createTexture();o.activeTexture(o.TEXTURE3),at({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 x=o.checkFramebufferStatus(o.FRAMEBUFFER);x!==o.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1]])).flat(),p=o.createTexture();o.activeTexture(o.TEXTURE2),at({gl:o,texture:p,imageData:new Float32Array(g),width:g.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}=j(t);return[-e,r/10]})).flat(),v=o.createTexture();o.activeTexture(o.TEXTURE1),at({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(i);const{startTime:e,endTime:r,tzId:n,texCoord:a,shadeColor:u,sunColor:f}=t,d=o.getUniform(i,A),{outputWidth:m,outputHeight:_}=Y({startTime:e,endTime:r,tzId:n});o.uniform4fv(A,u),o.uniform4fv(D,f),o.uniform1i(P,1),o.uniform2fv(S,[a[0],a[1]]);const x=o.createTexture();o.activeTexture(o.TEXTURE2),at({gl:o,imageData:null,width:m,height:_,wrap:o.CLAMP_TO_EDGE,filter:o.NEAREST,format:o.RGBA,texture:x});const g=o.createFramebuffer();o.bindFramebuffer(o.FRAMEBUFFER,g),o.framebufferTexture2D(o.FRAMEBUFFER,o.COLOR_ATTACHMENT0,o.TEXTURE_2D,x,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(x),o.deleteFramebuffer(g),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 ut(this._gl),this._tileMerger=new ht(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 ct(...t));export{ct as default};
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Copyright Ted Piotrowski 2024
3
3
  * Package: leaflet-shadow-simulator
4
- * Version: 0.57.0
4
+ * Version: 0.57.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())}))}function o(t,e,r){r=r||2;var o,n,s,u,l,h,d,m=e&&e.length,x=m?e[0]*r:t.length,g=i(t,0,x,r,!0),p=[];if(!g||g.next===g.prev)return p;if(m&&(g=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(_(s));for(u.sort(c),n=0;n<u.length;n++)r=f(u[n],r);return r}(t,e,g,r)),t.length>80*r){o=s=t[0],n=u=t[1];for(var E=r;E<x;E+=r)(l=t[E])<o&&(o=l),(h=t[E+1])<n&&(n=h),l>s&&(s=l),h>u&&(u=h);d=0!==(d=Math.max(s-o,u-n))?32767/d:0}return a(g,p,r,o,n,d,0),p}function i(t,e,r,o,i){var n,a;if(i===F(t,e,r,o)>0)for(n=e;n<r;n+=o)a=R(n,t[n],t[n+1],a);else for(n=r-o;n>=e;n-=o)a=R(n,t[n],t[n+1],a);return a&&E(a,a.next)&&(M(a),a=a.next),a}function n(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!E(o,o.next)&&0!==p(o.prev,o,o.next))o=o.next;else{if(M(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function a(t,e,r,o,i,c,f){if(t){!f&&c&&function(t,e,r,o){var i=t;do{0===i.z&&(i.z=m(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,c);for(var d,_,x=t;t.prev!==t.next;)if(d=t.prev,_=t.next,c?u(t,o,i,c):s(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(_.i/r|0),M(t),t=_.next,x=_.next;else if((t=_)===x){f?1===f?a(t=l(n(t),e,r),e,r,o,i,c,2):2===f&&h(t,e,r,o,i,c):a(n(t),e,r,o,i,c,1);break}}}function s(t){var e=t.prev,r=t,o=t.next;if(p(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)&&p(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function u(t,e,r,o){var i=t.prev,n=t,a=t.next;if(p(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,_=h<c?h<f?h:f:c<f?c:f,g=s>u?s>l?s:l:u>l?u:l,E=h>c?h>f?h:f:c>f?c:f,v=m(d,_,e,r,o),T=m(g,E,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=v&&b&&b.z<=T;){if(y.x>=d&&y.x<=g&&y.y>=_&&y.y<=E&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&p(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,b.x>=d&&b.x<=g&&b.y>=_&&b.y<=E&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&p(b.prev,b,b.next)>=0)return!1;b=b.nextZ}for(;y&&y.z>=v;){if(y.x>=d&&y.x<=g&&y.y>=_&&y.y<=E&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&p(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>=_&&b.y<=E&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&p(b.prev,b,b.next)>=0)return!1;b=b.nextZ}return!0}function l(t,e,r){var o=t;do{var i=o.prev,a=o.next.next;!E(i,a)&&v(i,o,o.next,a)&&b(i,a)&&b(a,i)&&(e.push(i.i/r|0),e.push(o.i/r|0),e.push(a.i/r|0),M(o),M(o.next),o=t=a),o=o.next}while(o!==t);return n(o)}function h(t,e,r,o,i,s){var u=t;do{for(var l=u.next.next;l!==u.prev;){if(u.i!==l.i&&g(u,l)){var h=A(u,l);return u=n(u,u.next),h=n(h,h.next),a(u,e,r,o,i,s,0),void a(h,e,r,o,i,s,0)}l=l.next}u=u.next}while(u!==t)}function c(t,e){return t.x-e.x}function f(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),b(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&d(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=A(r,t);return n(o,o.next),n(r,r.next)}function d(t,e){return p(t.prev,t,e.prev)<0&&p(e.next,t,t.next)<0}function m(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 _(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 g(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&&v(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(b(t,e)&&b(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)&&(p(t.prev,t,e.prev)||p(t,e.prev,e))||E(t,e)&&p(t.prev,t,t.next)>0&&p(e.prev,e,e.next)>0)}function p(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 v(t,e,r,o){var i=y(p(t,e,r)),n=y(p(t,e,o)),a=y(p(r,o,t)),s=y(p(r,o,e));return i!==n&&a!==s||(!(0!==i||!T(t,r,e))||(!(0!==n||!T(t,o,e))||(!(0!==a||!T(r,t,o))||!(0!==s||!T(r,e,o)))))}function T(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 y(t){return t>0?1:t<0?-1:0}function b(t,e){return p(t.prev,t,t.next)<0?p(t,e,t.next)>=0&&p(t,t.prev,e)>=0:p(t,e,t.prev)<0||p(t,t.next,e)<0}function A(t,e){var r=new w(t.i,t.x,t.y),o=new w(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 R(t,e,r,o){var i=new w(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 M(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 w(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 F(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 U(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 L(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}o.deviation=function(t,e,r,o){var i=e&&e.length,n=i?e[0]*r:t.length,a=Math.abs(F(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(F(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)},o.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 D=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 C(t,e,r){return t instanceof P?t:D(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 B(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function I(t,e){return!t||t instanceof B?t:new B(t,e)}function N(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function O(t,e){return t instanceof N?t:new N(t,e)}P.prototype={clone:function(){return new P(this.x,this.y)},add:function(t){return this.clone()._add(C(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(C(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=C(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=C(t)).x===this.x&&t.y===this.y},contains:function(t){return t=C(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)+")"}},B.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof P||"number"==typeof t[0]||"x"in t)e=r=C(t);else if(e=(t=I(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 C((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return C(this.min.x,this.max.y)},getTopRight:function(){return C(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?C(t):I(t))instanceof B?(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=I(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=I(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 I(C(e.x-o,e.y-i),C(r.x+o,r.y+i))},equals:function(t){return!!t&&(t=I(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},N.prototype={extend:function(t){var e,r,o=this._southWest,i=this._northEast;if(t instanceof X)e=t,r=t;else{if(!(t instanceof N))return t?this.extend(G(t)||O(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 X(e.lat,e.lng),this._northEast=new X(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 N(new X(e.lat-o,e.lng-i),new X(r.lat+o,r.lng+i))},getCenter:function(){return new X((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 X(this.getNorth(),this.getWest())},getSouthEast:function(){return new X(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 X||"lat"in t?G(t):O(t);var e,r,o=this._southWest,i=this._northEast;return t instanceof N?(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=O(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=O(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=O(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var z=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 B(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?U(t.lng,this.wrapLng,!0):t.lng;return new X(this.wrapLat?U(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 N(new X(n.lat-o,n.lng-i),new X(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 X(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 G(t,e,r){return t instanceof X?t:D(t)&&"object"!=typeof t[0]?3===t.length?new X(t[0],t[1],t[2]):2===t.length?new X(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new X(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new X(t,e,r)}X.prototype={equals:function(t,e){return!!t&&(t=G(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 z.distance(this,G(t))},wrap:function(){return z.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return O([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new X(this.lat,this.lng,this.alt)}};const H=256,W=.40909994067971484,j=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,i=Math.atan2(Math.sin(o)*Math.cos(W),Math.cos(o));return{dec:Math.asin(Math.sin(W)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},Y=(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 k(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,{vertices:i}=o.flatten(e.coordinates),n=new Float32Array(i.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=o(n),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 Math.max(e,o)}(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 V=(t,e)=>{const{lat:r,lng:o}=t;return new P(((t,e)=>(t+180)/360*Math.pow(2,e)*H)(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)*H)(r,e))},q=(t,e)=>{return new X((r=t.y,o=e,i=Math.PI-2*Math.PI*r/H/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/H/Math.pow(2,e)*360-180}(t.x,e));var r,o,i};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new B(new P(0,0),new P(0,0)),DEMPixelBounds:new B(new P(0,0),new P(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=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:x}=i,g=(p={getFeatures:o},r(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return k(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 B(V(r,e),V(o,e));let x=new P(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&&(x.x-=H,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,i=e.divideBy(H).floor().multiplyBy(H),n=(Math.ceil(r/H)+1)*H,a=(Math.ceil(o/H)+1)*H,s=i.add([n,a]);return new B(i,s)})({upperLeft:x,width:p,height:E}),{x:y,y:b}=T.max.subtract(T.min),A=(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)*H,yOffset:(t.y-r)*H}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:i}=t,n=e.divideBy(H).floor(),a=n.x+r/H,s=Math.min(n.y+o/H,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=i.max.x-i.min.x,M=i.max.y-i.min.y,w=Math.round(R),F=Math.round(M),U=JSON.stringify(A);if(!c&&U===$&&e<m)return J=Object.assign(Object.assign({},J),{outputWidth:w,outputHeight:F,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),J;const L=yield h.merge(A,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===L)return J=Object.assign(Object.assign({},J),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),J;const D=new B(V(new N(n.bounds).getNorthWest(),e),V(new N(n.bounds).getSouthEast(),e)),S=D.min.subtract(T.min),C=D.max.subtract(T.min),I=[S.x/y,S.y/b,C.x/y,C.y/b].map((t=>2*t-1)),O=yield g,{maxHeight:z,heightMapTex:X}=l.raster({upperLeftTile:A[0],width:y,height:b,mapZoom:e,features:O,imageData:L,gl:s,dsmSource:n,dsmCoords:I}),G=Math.max(n.maxHeight,L.maxHeight+z);$=U,J={heightMapTex:X,maxHeight:G,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:i,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:F}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:o,b:i}=t;return[r/255,o/255,i/255,e]},et=(t,e)=>{const{date:r}=e,{dec:o,Hi:i}=j(r);t.updateDate({dec:o,Hi:i})},rt=(t,e)=>r(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ot=(t,e)=>{const{color:r,opacity:o}=e,i=tt(r,o);t.updateColor({colorVec:i})},it=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:x}=o;if(0===f||0===d)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:E,y:v}=p.subtract(g),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,x))),F=w.map((t=>[(t.x-T.x)/f,(t.y-T.y)/d])).flat(),U=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/v])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),L=T.y/H/Math.pow(2,x),D=d/H/Math.pow(2,x),P=new N(q(m.getTopLeft(),x),q(m.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:i}=e,n=tt(r,o),{dec:a,Hi:s}=j(i);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:x,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 nt=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)*H)(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)*H)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new X((i=r.y,n=o,a=Math.PI-2*Math.PI*i/H/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/H/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 N(e,r)}}};const at=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 st(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 ut{constructor(t){this.gl=t,this.program=st({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width: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 x=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,x);const g=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,g,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 P(p,E),T=i/H/Math.pow(2,f),y=n/H/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 P(a[0],a[1]).subtract(v).unscaleBy(new P(T,y)).scaleBy(new P(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*e,m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),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(x),{maxHeight:d,heightMapTex:this.targetTexture}}}const lt=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===H?i:Math.floor(i/2),y:o===H?n:Math.floor(n/2),z:o===H?a:a-1}};class ht{constructor(t){this.gl=t,this.program=st({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(lt({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),at({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(lt({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),x=s.reduce(((t,e)=>Math.min(t,e.x)),1/0),g=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+H)/n,A=(_+H)/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,v-f)+(h!==H?1:0)),F=514===h?1/h:0,U=1/w,L=x%w/w,D=g%w/w,P=p%w/w,S=E%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 ct extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},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&&(et(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&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}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)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield rt(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=V(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=V(i,e),a=q(new P(n.x-r/2,n.y-o/2),e),s=q(new P(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=nt(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 P(512*n,512*a),o+1),h=q(new P(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 Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return r(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(it({kernel:this._compiledKernel,map:nt(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const 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=Y(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&&!nt(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=nt(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);return((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=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=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord: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=Y(r,.5,n);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);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=st({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"),x=i.getUniformLocation(n,"user_zoom"),g=i.getUniformLocation(n,"user_topYCoord"),p=i.getUniformLocation(n,"user_ySize"),E=i.getUniformLocation(n,"user_step"),v=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 Y=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}=Z(i+o[r]);s[2*(t*e+r)]=-n,s[2*(t*e+r)+1]=a/10}at({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(x,l),i.uniform1f(g,h),i.uniform1f(p,c),i.uniform4fv(A,f),i.uniform1f(E,M),i.uniform1f(v,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,x=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),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 g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g);const p=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,p,i.TEXTURE_2D,x,0);for(let t=0;t<o;t++){if(i.useProgram(n),h("tileloaded",t,o-1),c!==X)return i.deleteFramebuffer(g),i.deleteTexture(x),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,g);const h=i.checkFramebufferStatus(i.FRAMEBUFFER);h!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=j(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(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(i.useProgram(n),c!==X)return i.deleteTexture(x),i.uniform4fv(A,d),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.uniform1i(M,2),i.uniform1i(w,1),z(),i.uniform1i(w,0),i.uniform1i(M,0),i.deleteTexture(x),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),at({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 x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1]])).flat(),p=i.createTexture();i.activeTexture(i.TEXTURE2),at({gl:i,texture:p,imageData:new Float32Array(g),width:g.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 E=e.map((t=>{const{dec:e,Hi:r}=j(t);return[-e,r/10]})).flat(),v=i.createTexture();i.activeTexture(i.TEXTURE1),at({gl:i,texture:v,imageData:new Float32Array(E),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(v),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:_}=Y({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 x=i.createTexture();i.activeTexture(i.TEXTURE2),at({gl:i,imageData:null,width:m,height:_,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:x});const g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,x,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 E=new Uint8Array(m*_*4);return i.readPixels(0,0,m,_,i.RGBA,i.UNSIGNED_BYTE,E),i.deleteTexture(x),i.deleteFramebuffer(g),i.uniform1i(L,0),i.uniform1i(P,0),i.uniform4fv(A,d),i.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 ut(this._gl),this._tileMerger=new ht(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 ct(...t)),ct}));
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())}))}function o(t,e,r){r=r||2;var o,n,s,u,l,h,d,m=e&&e.length,x=m?e[0]*r:t.length,g=i(t,0,x,r,!0),p=[];if(!g||g.next===g.prev)return p;if(m&&(g=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(_(s));for(u.sort(c),n=0;n<u.length;n++)r=f(u[n],r);return r}(t,e,g,r)),t.length>80*r){o=s=t[0],n=u=t[1];for(var E=r;E<x;E+=r)(l=t[E])<o&&(o=l),(h=t[E+1])<n&&(n=h),l>s&&(s=l),h>u&&(u=h);d=0!==(d=Math.max(s-o,u-n))?32767/d:0}return a(g,p,r,o,n,d,0),p}function i(t,e,r,o,i){var n,a;if(i===F(t,e,r,o)>0)for(n=e;n<r;n+=o)a=R(n,t[n],t[n+1],a);else for(n=r-o;n>=e;n-=o)a=R(n,t[n],t[n+1],a);return a&&E(a,a.next)&&(M(a),a=a.next),a}function n(t,e){if(!t)return t;e||(e=t);var r,o=t;do{if(r=!1,o.steiner||!E(o,o.next)&&0!==p(o.prev,o,o.next))o=o.next;else{if(M(o),(o=e=o.prev)===o.next)break;r=!0}}while(r||o!==e);return e}function a(t,e,r,o,i,c,f){if(t){!f&&c&&function(t,e,r,o){var i=t;do{0===i.z&&(i.z=m(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,c);for(var d,_,x=t;t.prev!==t.next;)if(d=t.prev,_=t.next,c?u(t,o,i,c):s(t))e.push(d.i/r|0),e.push(t.i/r|0),e.push(_.i/r|0),M(t),t=_.next,x=_.next;else if((t=_)===x){f?1===f?a(t=l(n(t),e,r),e,r,o,i,c,2):2===f&&h(t,e,r,o,i,c):a(n(t),e,r,o,i,c,1);break}}}function s(t){var e=t.prev,r=t,o=t.next;if(p(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)&&p(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function u(t,e,r,o){var i=t.prev,n=t,a=t.next;if(p(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,_=h<c?h<f?h:f:c<f?c:f,g=s>u?s>l?s:l:u>l?u:l,E=h>c?h>f?h:f:c>f?c:f,v=m(d,_,e,r,o),T=m(g,E,e,r,o),y=t.prevZ,b=t.nextZ;y&&y.z>=v&&b&&b.z<=T;){if(y.x>=d&&y.x<=g&&y.y>=_&&y.y<=E&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&p(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,b.x>=d&&b.x<=g&&b.y>=_&&b.y<=E&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&p(b.prev,b,b.next)>=0)return!1;b=b.nextZ}for(;y&&y.z>=v;){if(y.x>=d&&y.x<=g&&y.y>=_&&y.y<=E&&y!==i&&y!==a&&x(s,h,u,c,l,f,y.x,y.y)&&p(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>=_&&b.y<=E&&b!==i&&b!==a&&x(s,h,u,c,l,f,b.x,b.y)&&p(b.prev,b,b.next)>=0)return!1;b=b.nextZ}return!0}function l(t,e,r){var o=t;do{var i=o.prev,a=o.next.next;!E(i,a)&&v(i,o,o.next,a)&&b(i,a)&&b(a,i)&&(e.push(i.i/r|0),e.push(o.i/r|0),e.push(a.i/r|0),M(o),M(o.next),o=t=a),o=o.next}while(o!==t);return n(o)}function h(t,e,r,o,i,s){var u=t;do{for(var l=u.next.next;l!==u.prev;){if(u.i!==l.i&&g(u,l)){var h=A(u,l);return u=n(u,u.next),h=n(h,h.next),a(u,e,r,o,i,s,0),void a(h,e,r,o,i,s,0)}l=l.next}u=u.next}while(u!==t)}function c(t,e){return t.x-e.x}function f(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),b(o,t)&&(u<f||u===f&&(o.x>r.x||o.x===r.x&&d(r,o)))&&(r=o,f=u)),o=o.next}while(o!==l);return r}(t,e);if(!r)return e;var o=A(r,t);return n(o,o.next),n(r,r.next)}function d(t,e){return p(t.prev,t,e.prev)<0&&p(e.next,t,t.next)<0}function m(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 _(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 g(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&&v(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(b(t,e)&&b(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)&&(p(t.prev,t,e.prev)||p(t,e.prev,e))||E(t,e)&&p(t.prev,t,t.next)>0&&p(e.prev,e,e.next)>0)}function p(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 v(t,e,r,o){var i=y(p(t,e,r)),n=y(p(t,e,o)),a=y(p(r,o,t)),s=y(p(r,o,e));return i!==n&&a!==s||(!(0!==i||!T(t,r,e))||(!(0!==n||!T(t,o,e))||(!(0!==a||!T(r,t,o))||!(0!==s||!T(r,e,o)))))}function T(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 y(t){return t>0?1:t<0?-1:0}function b(t,e){return p(t.prev,t,t.next)<0?p(t,e,t.next)>=0&&p(t,t.prev,e)>=0:p(t,e,t.prev)<0||p(t,t.next,e)<0}function A(t,e){var r=new w(t.i,t.x,t.y),o=new w(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 R(t,e,r,o){var i=new w(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 M(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 w(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 F(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 U(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 L(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}o.deviation=function(t,e,r,o){var i=e&&e.length,n=i?e[0]*r:t.length,a=Math.abs(F(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(F(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)},o.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 D=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 C(t,e,r){return t instanceof P?t:D(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 B(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function I(t,e){return!t||t instanceof B?t:new B(t,e)}function N(t,e){if(t)for(var r=e?[t,e]:t,o=0,i=r.length;o<i;o++)this.extend(r[o])}function O(t,e){return t instanceof N?t:new N(t,e)}P.prototype={clone:function(){return new P(this.x,this.y)},add:function(t){return this.clone()._add(C(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(C(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=C(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=C(t)).x===this.x&&t.y===this.y},contains:function(t){return t=C(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)+")"}},B.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof P||"number"==typeof t[0]||"x"in t)e=r=C(t);else if(e=(t=I(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 C((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return C(this.min.x,this.max.y)},getTopRight:function(){return C(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?C(t):I(t))instanceof B?(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=I(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=I(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 I(C(e.x-o,e.y-i),C(r.x+o,r.y+i))},equals:function(t){return!!t&&(t=I(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},N.prototype={extend:function(t){var e,r,o=this._southWest,i=this._northEast;if(t instanceof X)e=t,r=t;else{if(!(t instanceof N))return t?this.extend(G(t)||O(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 X(e.lat,e.lng),this._northEast=new X(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 N(new X(e.lat-o,e.lng-i),new X(r.lat+o,r.lng+i))},getCenter:function(){return new X((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 X(this.getNorth(),this.getWest())},getSouthEast:function(){return new X(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 X||"lat"in t?G(t):O(t);var e,r,o=this._southWest,i=this._northEast;return t instanceof N?(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=O(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=O(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=O(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var z=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 B(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?U(t.lng,this.wrapLng,!0):t.lng;return new X(this.wrapLat?U(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 N(new X(n.lat-o,n.lng-i),new X(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 X(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 G(t,e,r){return t instanceof X?t:D(t)&&"object"!=typeof t[0]?3===t.length?new X(t[0],t[1],t[2]):2===t.length?new X(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new X(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new X(t,e,r)}X.prototype={equals:function(t,e){return!!t&&(t=G(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 z.distance(this,G(t))},wrap:function(){return z.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return O([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new X(this.lat,this.lng,this.alt)}};const H=256,W=.40909994067971484,j=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,i=Math.atan2(Math.sin(o)*Math.cos(W),Math.cos(o));return{dec:Math.asin(Math.sin(W)*Math.sin(o)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},Y=(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 k(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,i=o.flatten(e.coordinates),n=new Float32Array(i.vertices.map(((t,e)=>{return e%2==1?(r=t,(180-180/Math.PI*Math.log(Math.tan(Math.PI/4+r*Math.PI/360)))/360):(180+t)/360;var r}))),a=o(i.vertices,i.holes,i.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 Math.max(e,o)}(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 V=(t,e)=>{const{lat:r,lng:o}=t;return new P(((t,e)=>(t+180)/360*Math.pow(2,e)*H)(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)*H)(r,e))},q=(t,e)=>{return new X((r=t.y,o=e,i=Math.PI-2*Math.PI*r/H/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/H/Math.pow(2,e)*360-180}(t.x,e));var r,o,i};let $,J={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new B(new P(0,0),new P(0,0)),DEMPixelBounds:new B(new P(0,0),new P(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const Q=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:x}=i,g=(p={getFeatures:o},r(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return k(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 B(V(r,e),V(o,e));let x=new P(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&&(x.x-=H,p+=512);const T=(t=>{const{upperLeft:e,width:r,height:o}=t,i=e.divideBy(H).floor().multiplyBy(H),n=(Math.ceil(r/H)+1)*H,a=(Math.ceil(o/H)+1)*H,s=i.add([n,a]);return new B(i,s)})({upperLeft:x,width:p,height:E}),{x:y,y:b}=T.max.subtract(T.min),A=(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)*H,yOffset:(t.y-r)*H}}))})((t=>{const{upperLeft:e,width:r,height:o,zoom:i}=t,n=e.divideBy(H).floor(),a=n.x+r/H,s=Math.min(n.y+o/H,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=i.max.x-i.min.x,M=i.max.y-i.min.y,w=Math.round(R),F=Math.round(M),U=JSON.stringify(A);if(!c&&U===$&&e<m)return J=Object.assign(Object.assign({},J),{outputWidth:w,outputHeight:F,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),J;const L=yield h.merge(A,{maxZoom:m,width:y,height:b,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:d,tileSize:_,tileLoaded:a});if(null===L)return J=Object.assign(Object.assign({},J),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),J;const D=new B(V(new N(n.bounds).getNorthWest(),e),V(new N(n.bounds).getSouthEast(),e)),S=D.min.subtract(T.min),C=D.max.subtract(T.min),I=[S.x/y,S.y/b,C.x/y,C.y/b].map((t=>2*t-1)),O=yield g,{maxHeight:z,heightMapTex:X}=l.raster({upperLeftTile:A[0],width:y,height:b,mapZoom:e,features:O,imageData:L,gl:s,dsmSource:n,dsmCoords:I}),G=Math.max(n.maxHeight,L.maxHeight+z);$=U,J={heightMapTex:X,maxHeight:G,width:y,height:b,DEMPixelBounds:T,visibleDEMPixelBounds:i,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:F}}catch(t){console.error("Could not decode height map",t)}return J})),tt=(t,e)=>{const{r:r,g:o,b:i}=t;return[r/255,o/255,i/255,e]},et=(t,e)=>{const{date:r}=e,{dec:o,Hi:i}=j(r);t.updateDate({dec:o,Hi:i})},rt=(t,e)=>r(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ot=(t,e)=>{const{color:r,opacity:o}=e,i=tt(r,o);t.updateColor({colorVec:i})},it=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:x}=o;if(0===f||0===d)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:E,y:v}=p.subtract(g),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,x))),F=w.map((t=>[(t.x-T.x)/f,(t.y-T.y)/d])).flat(),U=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/v])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),L=T.y/H/Math.pow(2,x),D=d/H/Math.pow(2,x),P=new N(q(m.getTopLeft(),x),q(m.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:o,date:i}=e,n=tt(r,o),{dec:a,Hi:s}=j(i);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:n,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:d,heightMapZoom:x,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 nt=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)*H)(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)*H)(t,o))}},unproject:(r,o)=>{return e()?t.unproject(r,o):new X((i=r.y,n=o,a=Math.PI-2*Math.PI*i/H/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/H/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 N(e,r)}}};const at=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 st(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 ut{constructor(t){this.gl=t,this.program=st({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nuniform vec2 u_centroid;\nuniform sampler2D height_map;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = a_position.x - xyz.x;\n\t\tfloat y = a_position.y - xyz.y;\n\t\tvec2 transformed = vec2(x, y);\n\t\tvec2 normalized = transformed / dimensions;\n\t\tvec2 final = normalized * 2.0 - 1.0;\n v_elevation = texture2D(height_map, vec2(u_centroid.x, u_centroid.y));\n\t\tgl_Position = vec4(final, 0, 1);\n\t}\n",fSrc:"\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n\nuniform vec4 color;\nuniform sampler2D height_map;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\nvarying vec4 v_elevation;\n\nvec4 addElevation(vec4 color, vec4 toAdd) {\n float ground = (color.b * 255.0 * 255.0 + color.a * 255.0);\n float building = (toAdd.r * 255.0 * 255.0 + toAdd.g * 255.0);\n float elevation = ground + building;\n float r = floor(elevation / 255.0) / 255.0;\n float g = floor(mod(elevation, 255.0)) / 255.0;\n return vec4(r, g, r, g);\n}\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n vec4 final = addElevation(v_elevation, color);\n\tgl_FragColor = final;\n}\n"}),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.dsmAttributeLocation=t.getAttribLocation(this.program,"dsm_position"),this.useDSMUniformLocation=t.getUniformLocation(this.program,"use_dsm"),this.xyzUniformLocation=t.getUniformLocation(this.program,"xyz"),this.dimensionsUniformLocation=t.getUniformLocation(this.program,"dimensions"),this.heightMapUniformLocation=t.getUniformLocation(this.program,"height_map"),this.centroidUniformLocation=t.getUniformLocation(this.program,"u_centroid"),this.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:o,width: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 x=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,x);const g=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,g,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 P(p,E),T=i/H/Math.pow(2,f),y=n/H/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 P(a[0],a[1]).subtract(v).unscaleBy(new P(T,y)).scaleBy(new P(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*e,m=Math.floor(f/255)/255,_=Math.floor(f%255)/255;d=Math.max(d,e),l.uniform2f(this.centroidUniformLocation,h,c),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(x),{maxHeight:d,heightMapTex:this.targetTexture}}}const lt=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===H?i:Math.floor(i/2),y:o===H?n:Math.floor(n/2),z:o===H?a:a-1}};class ht{constructor(t){this.gl=t,this.program=st({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(lt({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),at({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(lt({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),x=s.reduce(((t,e)=>Math.min(t,e.x)),1/0),g=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+H)/n,A=(_+H)/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,v-f)+(h!==H?1:0)),F=514===h?1/h:0,U=1/w,L=x%w/w,D=g%w/w,P=p%w/w,S=E%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 ct extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:o}=t;return 256*e+r+o/256-32768}},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&&(et(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&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(ot(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}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)et(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield rt(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=V(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=V(i,e),a=q(new P(n.x-r/2,n.y-o/2),e),s=q(new P(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=nt(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 P(512*n,512*a),o+1),h=q(new P(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 Q({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return r(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(it({kernel:this._compiledKernel,map:nt(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield rt(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,o){const 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=Y(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&&!nt(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=nt(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);return((t,e)=>{const r=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=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=tt({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),o=tt({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:o}))})(this._compiledKernel,Object.assign(Object.assign({},t),{startTime:r,endTime:o,texCoord: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=Y(r,.5,n);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);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=st({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"),x=i.getUniformLocation(n,"user_zoom"),g=i.getUniformLocation(n,"user_topYCoord"),p=i.getUniformLocation(n,"user_ySize"),E=i.getUniformLocation(n,"user_step"),v=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 Y=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}=Z(i+o[r]);s[2*(t*e+r)]=-n,s[2*(t*e+r)+1]=a/10}at({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(x,l),i.uniform1f(g,h),i.uniform1f(p,c),i.uniform4fv(A,f),i.uniform1f(E,M),i.uniform1f(v,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,x=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),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 g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g);const p=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,p,i.TEXTURE_2D,x,0);for(let t=0;t<o;t++){if(i.useProgram(n),h("tileloaded",t,o-1),c!==X)return i.deleteFramebuffer(g),i.deleteTexture(x),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,g);const h=i.checkFramebufferStatus(i.FRAMEBUFFER);h!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+h);const{dec:c,Hi:d}=j(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(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(i.useProgram(n),c!==X)return i.deleteTexture(x),i.uniform4fv(A,d),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.uniform1i(M,2),i.uniform1i(w,1),z(),i.uniform1i(w,0),i.uniform1i(M,0),i.deleteTexture(x),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),at({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 x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1]])).flat(),p=i.createTexture();i.activeTexture(i.TEXTURE2),at({gl:i,texture:p,imageData:new Float32Array(g),width:g.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 E=e.map((t=>{const{dec:e,Hi:r}=j(t);return[-e,r/10]})).flat(),v=i.createTexture();i.activeTexture(i.TEXTURE1),at({gl:i,texture:v,imageData:new Float32Array(E),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(v),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:_}=Y({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 x=i.createTexture();i.activeTexture(i.TEXTURE2),at({gl:i,imageData:null,width:m,height:_,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:x});const g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,x,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 E=new Uint8Array(m*_*4);return i.readPixels(0,0,m,_,i.RGBA,i.UNSIGNED_BYTE,E),i.deleteTexture(x),i.deleteFramebuffer(g),i.uniform1i(L,0),i.uniform1i(P,0),i.uniform4fv(A,d),i.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 ut(this._gl),this._tileMerger=new ht(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 ct(...t)),ct}));
@@ -0,0 +1,145 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Document</title>
8
+ <link rel="stylesheet" href="styles.css" />
9
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
10
+ integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous" />
11
+
12
+ <link rel="preconnect" href="https://fonts.googleapis.com" />
13
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
14
+ <link
15
+ href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,300;1,400;1,500;1,600;1,700;1,800&display=swap"
16
+ rel="stylesheet" />
17
+
18
+ <script src="https://unpkg.com/@jsreport/browser-client/dist/jsreport.umd.js"></script>
19
+ <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.3/dist/leaflet.css" />
20
+ <script src="https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"></script>
21
+ <script src="https://www.unpkg.com/suncalc@1.9.0/suncalc.js"></script>
22
+ <script src="https://unpkg.com/osmtogeojson/osmtogeojson.js"></script>
23
+ <!-- <script src="https://unpkg.com/leaflet-shadow-simulator@0.50.0/dist/leaflet-shadow-simulator.umd.min.js"></script> -->
24
+ <script src="../dist/leaflet-shadow-simulator.umd.min.js"></script>
25
+ </head>
26
+
27
+ <body>
28
+ <div class="px-4">
29
+ <div id="sunlight-map" style="width:100%;height:780px; margin-bottom: 20px"></div>
30
+
31
+ <script>
32
+ try {
33
+ const map = L.map("sunlight-map", {
34
+ zoomControl: false
35
+ }).setView([46.20111914696285, 6.1484776575582], 18);
36
+
37
+ L.tileLayer("https://api.mapbox.com/styles/v1/mapbox/light-v10/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoicG9wZXR5IiwiYSI6ImNpdDlydDEwYzBsMWYydXAyeXhrMHhoamIifQ.9_sT8uJ8zd_6sU0ispPK3w", {
38
+ attribution: '',
39
+ maxZoom: 18,
40
+ }).addTo(map);
41
+
42
+ let now = new Date(1633358583454);
43
+
44
+ const {
45
+ sunrise,
46
+ sunset
47
+ } = SunCalc.getTimes(now, 46.20111914696285, 6.1484776575582);
48
+
49
+ const shadeMap = new ShadeMap({
50
+ date: new Date(), // display shadows for current date
51
+ color: '#01112f', // shade color
52
+ opacity: 0.7, // opacity of shade color
53
+ apiKey: 'eyJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InRwcGlvdHJvd3NraUBzaGFkZW1hcC5hcHAiLCJjcmVhdGVkIjoxNjYyNDkzMDY2Nzk0LCJpYXQiOjE2NjI0OTMwNjZ9.ovCrLTYsdKFTF6TW3DuODxCaAtGQ3qhcmqj3DWcol5g',
54
+ terrainSource: {
55
+ tileSize: 256, // DEM tile size
56
+ _overzoom: 18,
57
+ maxZoom: 15, // Maximum zoom of DEM tile set
58
+ getSourceUrl: ({
59
+ x,
60
+ y,
61
+ z
62
+ }) => `https://s3.amazonaws.com/elevation-tiles-prod/terrarium/${z}/${x}/${y}.png`,
63
+ getElevation: ({
64
+ r,
65
+ g,
66
+ b,
67
+ a
68
+ }) => (r * 256 + g + b / 256) - 32768,
69
+ },
70
+ sunExposure: {
71
+ enabled: true,
72
+ startDate: sunrise,
73
+ endDate: sunset,
74
+ iterations: 3
75
+ },
76
+ getFeatures: async () => {
77
+ try {
78
+ if (map.getZoom() > 15) {
79
+ const bounds = map.getBounds();
80
+ const north = bounds.getNorth();
81
+ const south = bounds.getSouth();
82
+ const east = bounds.getEast();
83
+ const west = bounds.getWest();
84
+ const query = `https://overpass-api.de/api/interpreter?data=%2F*%0AThis%20has%20been%20generated%20by%20the%20overpass-turbo%20wizard.%0AThe%20original%20search%20was%3A%0A%E2%80%9Cbuilding%E2%80%9D%0A*%2F%0A%5Bout%3Ajson%5D%5Btimeout%3A25%5D%3B%0A%2F%2F%20gather%20results%0A%28%0A%20%20%2F%2F%20query%20part%20for%3A%20%E2%80%9Cbuilding%E2%80%9D%0A%20%20way%5B%22building%22%5D%28${south}%2C${west}%2C${north}%2C${east}%29%3B%0A%29%3B%0A%2F%2F%20print%20results%0Aout%20body%3B%0A%3E%3B%0Aout%20skel%20qt%3B`;
85
+ const response = await fetch(query)
86
+ const json = await response.json();
87
+ const geojson = osmtogeojson(json);
88
+ // If no building height, default to one storey of 3 meters
89
+ geojson.features.forEach(feature => {
90
+ if (!feature.properties) {
91
+ feature.properties = {};
92
+ }
93
+ if (!feature.properties.height) {
94
+ feature.properties.height = 3;
95
+ }
96
+ });
97
+ return geojson.features;
98
+ }
99
+ } catch (e) {
100
+ console.error(e);
101
+ }
102
+ return [];
103
+ },
104
+ debug: (msg) => console.log(msg),
105
+ }).addTo(map);
106
+
107
+ // shadeMap.setTerrainSource({
108
+ // getSourceUrl: (params) => {
109
+ // const { x, y, z } = params
110
+
111
+ // return `https://pub-042f8c71c32b4422a6a445f9ecaf6a55.r2.dev/${z}/${x}/${y}.webp`
112
+ // },
113
+ // getElevation: (params) => {
114
+ // const { r, g } = params
115
+
116
+ // return ((r * 256 + g) / 2 ** 16) * 4808
117
+ // },
118
+ // tileSize: 512,
119
+ // maxZoom: 17,
120
+ // _overzoom: 18,
121
+ // })
122
+
123
+
124
+ // shadeMap.setSunExposure(true, {
125
+ // startDate: sunrise,
126
+ // endDate: sunset
127
+ // });
128
+
129
+ shadeMap.once('idle', () => {
130
+ shadeMap._reset();
131
+ setTimeout(() => {
132
+ window.JSREPORT_READY_TO_START = true;
133
+ }, 4000);
134
+ });
135
+
136
+ } catch (error) {
137
+ console.error("Error name:", error.name);
138
+ console.error("Error message:", error.message);
139
+ console.error("Error stack:", error.stack);
140
+ }
141
+ </script>
142
+ </div>
143
+ </body>
144
+
145
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leaflet-shadow-simulator",
3
- "version": "0.57.0",
3
+ "version": "0.57.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",