leaflet-shadow-simulator 0.37.1 → 0.38.0
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.
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright Ted Piotrowski 2023
|
|
3
|
+
* Package: leaflet-shadow-simulator
|
|
4
|
+
* Version: 0.38.0
|
|
5
|
+
* For licensing visit: https://shademap.app/about/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import{ImageOverlay as t,DomUtil as e,Util as r}from"leaflet";class n extends t{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const t="CANVAS"===this._url.tagName,n=this._image=t?this._url:e.create("canvas");return e.addClass(n,"leaflet-image-layer"),this._zoomAnimated&&e.addClass(n,"leaflet-zoom-animated"),this.options.className&&e.addClass(n,this.options.className),n.onselectstart=r.falseFn,n.onmousemove=r.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function i(t,e,r,n){return new(r||(r=Promise))((function(i,o){function s(t){try{u(n.next(t))}catch(t){o(t)}}function a(t){try{u(n.throw(t))}catch(t){o(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(s,a)}u((n=n.apply(t,e||[])).next())}))}function o(t,e,r){var n=e[1],i=e[0],o=n-i;return t===n&&r?t:((t-i)%o+o)%o+i}function s(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var a=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function u(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var h=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function l(t,e,r){return t instanceof u?t:a(t)?new u(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new u(t.x,t.y):new u(t,e,r)}function c(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function f(t,e){return!t||t instanceof c?t:new c(t,e)}function d(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function m(t,e){return t instanceof d?t:new d(t,e)}u.prototype={clone:function(){return new u(this.x,this.y)},add:function(t){return this.clone()._add(l(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(l(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 u(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new u(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=h(this.x),this.y=h(this.y),this},distanceTo:function(t){var e=(t=l(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=l(t)).x===this.x&&t.y===this.y},contains:function(t){return t=l(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+s(this.x)+", "+s(this.y)+")"}},c.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof u||"number"==typeof t[0]||"x"in t)e=r=l(t);else if(e=(t=f(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 l((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return l(this.min.x,this.max.y)},getTopRight:function(){return l(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 u?l(t):f(t))instanceof c?(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=f(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>=e.x&&n.x<=r.x,s=i.y>=e.y&&n.y<=r.y;return o&&s},overlaps:function(t){t=f(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>e.x&&n.x<r.x,s=i.y>e.y&&n.y<r.y;return o&&s},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,n=Math.abs(e.x-r.x)*t,i=Math.abs(e.y-r.y)*t;return f(l(e.x-n,e.y-i),l(r.x+n,r.y+i))},equals:function(t){return!!t&&(t=f(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},d.prototype={extend:function(t){var e,r,n=this._southWest,i=this._northEast;if(t instanceof x)e=t,r=t;else{if(!(t instanceof d))return t?this.extend(g(t)||m(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return n||i?(n.lat=Math.min(e.lat,n.lat),n.lng=Math.min(e.lng,n.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,n=Math.abs(e.lat-r.lat)*t,i=Math.abs(e.lng-r.lng)*t;return new d(new x(e.lat-n,e.lng-i),new x(r.lat+n,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):m(t);var e,r,n=this._southWest,i=this._northEast;return t instanceof d?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=n.lat&&r.lat<=i.lat&&e.lng>=n.lng&&r.lng<=i.lng},intersects:function(t){t=m(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>=e.lat&&n.lat<=r.lat,s=i.lng>=e.lng&&n.lng<=r.lng;return o&&s},overlaps:function(t){t=m(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>e.lat&&n.lat<r.lat,s=i.lng>e.lng&&n.lng<r.lng;return o&&s},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=m(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var _=function(t){var e,r,n,i;for(r=1,n=arguments.length;r<n;r++)for(e in i=arguments[r])t[e]=i[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),n=this.scale(e);return this.transformation._transform(r,n)},pointToLatLng:function(t,e){var r=this.scale(e),n=this.transformation.untransform(t,r);return this.projection.unproject(n)},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 c(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?o(t.lng,this.wrapLng,!0):t.lng;return new x(this.wrapLat?o(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),n=e.lat-r.lat,i=e.lng-r.lng;if(0===n&&0===i)return t;var o=t.getSouthWest(),s=t.getNorthEast();return new d(new x(o.lat-n,o.lng-i),new x(s.lat-n,s.lng-i))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,n=t.lat*r,i=e.lat*r,o=Math.sin((e.lat-t.lat)*r/2),s=Math.sin((e.lng-t.lng)*r/2),a=o*o+Math.cos(n)*Math.cos(i)*s*s,u=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));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:a(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("+s(this.lat,t)+", "+s(this.lng,t)+")"},distanceTo:function(t){return _.distance(this,g(t))},wrap:function(){return _.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return m([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new x(this.lat,this.lng,this.alt)}};const p=(t,e)=>{const{lat:r,lng:n}=t;return new u(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(n,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},E=(t,e)=>{return new x((r=t.y,n=e,i=Math.PI-2*Math.PI*r/256/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,n,i},T=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,n=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(n)*Math.cos(.40909994067971484),Math.cos(n));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(n)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},v=(t,e,r)=>{const n=1/e,i=Math.min(t[0]*n,255),o=Math.min(t[1]*n,255),s=Math.min(t[2]*n,255);let a=0;return i+o+s!==0&&(a=i>0?i/255*.5+.5:s>0?.5*(1-s/255):.5),a*r},y=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),n=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-n.getTime()};function R(t){const{gl:e,vSrc:r,fSrc:n}=t,i=e.createShader(e.VERTEX_SHADER);e.shaderSource(i,r),e.compileShader(i);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,n),e.compileShader(o);const s=e.createProgram();return e.attachShader(s,i),e.attachShader(s,o),e.linkProgram(s),s}class A{constructor(t){this.gl=t,this.program=R({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nfloat PI = 3.141592653589793;\nvarying vec2 vCoord;\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\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 + 180.0) / 360.0 * pow(2.0, xyz.z)) * 256.0 - xyz.x;\n\t\tfloat y = (1.0 - (log(a_position.y) / PI)) / 2.0 * pow(2.0, xyz.z) * 256.0 - 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\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;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n\t \tgl_FragColor = vec4(textureColor.b, textureColor.a, 0, 1.0);\n\t\treturn;\n\t} \n\tgl_FragColor = color;\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.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:n,width:i,height:o,imageData:s,dsmCoords:a,dsmSource:h,gl:l}=t,{x:c,y:f,z:d}=r;let m=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1);const _=l.createTexture();l.bindTexture(l.TEXTURE_2D,_),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,i,o,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,s);const x=i,g=o;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,x,g,0,l.RGBA,l.UNSIGNED_BYTE,null),n>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,x,g);const E=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,E);const T=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,T,l.TEXTURE_2D,this.targetTexture,0),l.bindBuffer(l.ARRAY_BUFFER,this.positionBuffer),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,this.indexBuffer),l.clearColor(0,0,0,0),l.clear(l.COLOR_BUFFER_BIT);const v=256*c,y=256*f,R=new u(v,y);l.uniform3f(this.xyzUniformLocation,v,y,d),l.uniform2f(this.dimensionsUniformLocation,i,o),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const A=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,A,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.drawArrays(l.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:n,startPos:a,highlight:u}=t,h=p({lng:a[0],lat:a[1]},d).subtract(R).floor();if(h.x<0||h.y<0||h.x>i||h.y>o)return;const c=256*s[h.y*i*2+2*h.x]+s[h.y*i*2+2*h.x+1]+5*e,f=Math.floor(c/256)/256,_=Math.floor(c%256)/256;m=Math.max(m,c/5),l.uniform4f(this.colorUniformLocation,f,_,u?1:0,1),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,n,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,n.length,n.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),l.deleteTexture(_),0!==h.data.length){l.activeTexture(l.TEXTURE1);const t=l.createTexture();l.bindTexture(l.TEXTURE_2D,t),l.pixelStorei(l.UNPACK_ALIGNMENT,2),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST),l.texImage2D(l.TEXTURE_2D,0,l.LUMINANCE_ALPHA,h.width,h.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,h.data);const e=[a[0],a[3],a[2],a[3],a[0],a[1],a[2],a[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.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.deleteFramebuffer(E),m}}function w(t,e,r){r=r||2;var n,i,o,s,a,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=M(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(l&&(f=function(t,e,r,n){var i,o,s,a=[];for(i=0,o=e.length;i<o;i++)(s=M(t,e[i]*n,i<o-1?e[i+1]*n:t.length,n,!1))===s.next&&(s.steiner=!0),a.push(N(s));for(a.sort(C),i=0;i<a.length;i++)r=I(a[i],r);return r}(t,e,f,r)),t.length>80*r){n=o=t[0],i=s=t[1];for(var m=r;m<c;m+=r)(a=t[m])<n&&(n=a),(u=t[m+1])<i&&(i=u),a>o&&(o=a),u>s&&(s=u);h=0!==(h=Math.max(o-n,s-i))?32767/h:0}return U(f,d,r,n,i,h,0),d}function M(t,e,r,n,i){var o,s;if(i===V(t,e,r,n)>0)for(o=e;o<r;o+=n)s=K(o,t[o],t[o+1],s);else for(o=r-n;o>=e;o-=n)s=K(o,t[o],t[o+1],s);return s&&H(s,s.next)&&(q(s),s=s.next),s}function b(t,e){if(!t)return t;e||(e=t);var r,n=t;do{if(r=!1,n.steiner||!H(n,n.next)&&0!==z(n.prev,n,n.next))n=n.next;else{if(q(n),(n=e=n.prev)===n.next)break;r=!0}}while(r||n!==e);return e}function U(t,e,r,n,i,o,s){if(t){!s&&o&&function(t,e,r,n){var i=t;do{0===i.z&&(i.z=B(i.x,i.y,e,r,n)),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,n,i,o,s,a,u,h=1;do{for(r=t,t=null,o=null,s=0;r;){for(s++,n=r,a=0,e=0;e<h&&(a++,n=n.nextZ);e++);for(u=h;a>0||u>0&&n;)0!==a&&(0===u||!n||r.z<=n.z)?(i=r,r=r.nextZ,a--):(i=n,n=n.nextZ,u--),o?o.nextZ=i:t=i,i.prevZ=o,o=i;r=n}o.nextZ=null,h*=2}while(s>1)}(i)}(t,n,i,o);for(var a,u,h=t;t.prev!==t.next;)if(a=t.prev,u=t.next,o?L(t,n,i,o):D(t))e.push(a.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),q(t),t=u.next,h=u.next;else if((t=u)===h){s?1===s?U(t=P(b(t),e,r),e,r,n,i,o,2):2===s&&S(t,e,r,n,i,o):U(b(t),e,r,n,i,o,1);break}}}function D(t){var e=t.prev,r=t,n=t.next;if(z(e,r,n)>=0)return!1;for(var i=e.x,o=r.x,s=n.x,a=e.y,u=r.y,h=n.y,l=i<o?i<s?i:s:o<s?o:s,c=a<u?a<h?a:h:u<h?u:h,f=i>o?i>s?i:s:o>s?o:s,d=a>u?a>h?a:h:u>h?u:h,m=n.next;m!==e;){if(m.x>=l&&m.x<=f&&m.y>=c&&m.y<=d&&X(i,a,o,u,s,h,m.x,m.y)&&z(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function L(t,e,r,n){var i=t.prev,o=t,s=t.next;if(z(i,o,s)>=0)return!1;for(var a=i.x,u=o.x,h=s.x,l=i.y,c=o.y,f=s.y,d=a<u?a<h?a:h:u<h?u:h,m=l<c?l<f?l:f:c<f?c:f,_=a>u?a>h?a:h:u>h?u:h,x=l>c?l>f?l:f:c>f?c:f,g=B(d,m,e,r,n),p=B(_,x,e,r,n),E=t.prevZ,T=t.nextZ;E&&E.z>=g&&T&&T.z<=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=x&&E!==i&&E!==s&&X(a,l,u,c,h,f,E.x,E.y)&&z(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,T.x>=d&&T.x<=_&&T.y>=m&&T.y<=x&&T!==i&&T!==s&&X(a,l,u,c,h,f,T.x,T.y)&&z(T.prev,T,T.next)>=0)return!1;T=T.nextZ}for(;E&&E.z>=g;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=x&&E!==i&&E!==s&&X(a,l,u,c,h,f,E.x,E.y)&&z(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;T&&T.z<=p;){if(T.x>=d&&T.x<=_&&T.y>=m&&T.y<=x&&T!==i&&T!==s&&X(a,l,u,c,h,f,T.x,T.y)&&z(T.prev,T,T.next)>=0)return!1;T=T.nextZ}return!0}function P(t,e,r){var n=t;do{var i=n.prev,o=n.next.next;!H(i,o)&&G(i,n,n.next,o)&&Z(i,o)&&Z(o,i)&&(e.push(i.i/r|0),e.push(n.i/r|0),e.push(o.i/r|0),q(n),q(n.next),n=t=o),n=n.next}while(n!==t);return b(n)}function S(t,e,r,n,i,o){var s=t;do{for(var a=s.next.next;a!==s.prev;){if(s.i!==a.i&&O(s,a)){var u=Y(s,a);return s=b(s,s.next),u=b(u,u.next),U(s,e,r,n,i,o,0),void U(u,e,r,n,i,o,0)}a=a.next}s=s.next}while(s!==t)}function C(t,e){return t.x-e.x}function I(t,e){var r=function(t,e){var r,n=e,i=t.x,o=t.y,s=-1/0;do{if(o<=n.y&&o>=n.next.y&&n.next.y!==n.y){var a=n.x+(o-n.y)*(n.next.x-n.x)/(n.next.y-n.y);if(a<=i&&a>s&&(s=a,r=n.x<n.next.x?n:n.next,a===i))return r}n=n.next}while(n!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;n=r;do{i>=n.x&&n.x>=l&&i!==n.x&&X(o<c?i:s,o,l,c,o<c?s:i,o,n.x,n.y)&&(u=Math.abs(o-n.y)/(i-n.x),Z(n,t)&&(u<f||u===f&&(n.x>r.x||n.x===r.x&&F(r,n)))&&(r=n,f=u)),n=n.next}while(n!==h);return r}(t,e);if(!r)return e;var n=Y(r,t);return b(n,n.next),b(r,r.next)}function F(t,e){return z(t.prev,t,e.prev)<0&&z(e.next,t,t.next)<0}function B(t,e,r,n,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-n)*i|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function N(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,n,i,o,s,a){return(i-s)*(e-a)>=(t-s)*(o-a)&&(t-s)*(n-a)>=(r-s)*(e-a)&&(r-s)*(o-a)>=(i-s)*(n-a)}function O(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&&G(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(Z(t,e)&&Z(e,t)&&function(t,e){var r=t,n=!1,i=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&i<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(n=!n),r=r.next}while(r!==t);return n}(t,e)&&(z(t.prev,t,e.prev)||z(t,e.prev,e))||H(t,e)&&z(t.prev,t,t.next)>0&&z(e.prev,e,e.next)>0)}function z(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function H(t,e){return t.x===e.x&&t.y===e.y}function G(t,e,r,n){var i=j(z(t,e,r)),o=j(z(t,e,n)),s=j(z(r,n,t)),a=j(z(r,n,e));return i!==o&&s!==a||(!(0!==i||!W(t,r,e))||(!(0!==o||!W(t,n,e))||(!(0!==s||!W(r,t,n))||!(0!==a||!W(r,e,n)))))}function W(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 j(t){return t>0?1:t<0?-1:0}function Z(t,e){return z(t.prev,t,t.next)<0?z(t,e,t.next)>=0&&z(t,t.prev,e)>=0:z(t,e,t.prev)<0||z(t,t.next,e)<0}function Y(t,e){var r=new k(t.i,t.x,t.y),n=new k(e.i,e.x,e.y),i=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=i,i.prev=r,n.next=r,r.prev=n,o.next=n,n.prev=o,n}function K(t,e,r,n){var i=new k(t,e,r);return n?(i.next=n.next,i.prev=n,n.next.prev=i,n.next=i):(i.prev=i,i.next=i),i}function q(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 k(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 V(t,e,r,n){for(var i=0,o=e,s=r-n;o<r;o+=n)i+=(t[s]-t[o])*(t[o+1]+t[s+1]),s=o;return i}function $(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:n,type:i}=e;if("MultiPolygon"===r.type)for(let n=0;n<r.coordinates.length;n++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[n]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:n}=w.flatten(e.coordinates),i=new Float32Array(n.map(((t,e)=>{if(e%2==1){const e=t*Math.PI/180;return Math.tan(e)+1/Math.cos(e)}return t}))),o=w(n),s=o.length>256?new Uint16Array(o):new Uint8Array(o),a=function(t){const{height:e=0,levels:r=0,render_height:n=0}=t;if(r)return 3.04*r;return Math.max(e,n)}(r),u=r.highlight||!1;return{aPosition:i,cuts:s,buildingHeight:a,startPos:[n[0],n[1]],highlight:u}}))}w.deviation=function(t,e,r,n){var i=e&&e.length,o=i?e[0]*r:t.length,s=Math.abs(V(t,0,o,r));if(i)for(var a=0,u=e.length;a<u;a++){var h=e[a]*r,l=a<u-1?e[a+1]*r:t.length;s-=Math.abs(V(t,h,l,r))}var c=0;for(a=0;a<n.length;a+=3){var f=n[a]*r,d=n[a+1]*r,m=n[a+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===s&&0===c?0:Math.abs((c-s)/s)},w.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},n=0,i=0;i<t.length;i++){for(var o=0;o<t[i].length;o++)for(var s=0;s<e;s++)r.vertices.push(t[i][o][s]);i>0&&(n+=t[i-1].length,r.holes.push(n))}return r};const J=t=>new Promise(((e,r)=>{const n=new Int32Array(t.buffer).reduce(((t,e)=>Math.max(t,(t=>{const e=(256*(255&t)+(t>>8&255))/5,r=(256*(t>>16&255)+(t>>24&255))/5;return Math.max(e,r)})(e))),0);e({maxHeight:Math.min(8848,n)})}));let Q,tt={width:0,height:0,imageData:new Uint8ClampedArray(0),visibleDEMPixelBounds:new c(new u(0,0),new u(0,0)),DEMPixelBounds:new c(new u(0,0),new u(0,0)),maxHeight:8848,maxBuildingHeight:0,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const et=t=>i(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:n,dsmSource:o,tileLoaded:s,gl:a,bounds:h,buildingRasterizer:l,tileMerger:f,forceUpdate:m=!1}=t,{getSourceUrl:_,getElevation:x,maxZoom:g,tileSize:E,_overzoom:T}=n,v=(y={getFeatures:r},i(void 0,void 0,void 0,(function*(){const{getFeatures:t}=y;try{return $(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var y;try{const t=h,r=t.getNorthWest(),n=t.getSouthEast(),i=new c(p(r,e),p(n,e));let T=new u(i.min.x,i.min.y),y=i.max.x-i.min.x;const R=i.max.y-i.min.y,A=i.max.subtract(i.min);A.y>A.x&&(T.x-=256,y+=512);const w=(t=>{const{upperLeft:e,width:r,height:n}=t,i=e.divideBy(256).floor().multiplyBy(256),o=256*(Math.ceil(r/256)+1),s=256*(Math.ceil(n/256)+1),a=i.add([o,s]);return new c(i,a)})({upperLeft:T,width:y,height:R}),{x:M,y:b}=w.max.subtract(w.min),U=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.x<t.x?e:t)).x,r=t.reduce(((t,e)=>e.y<t.y?e:t)).y;return t.map((t=>{const n=Math.pow(2,t.z);return{x:(t.x%n+n)%n,y:(t.y%n+n)%n,z:t.z,xOffset:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:n,zoom:i}=t,o=e.divideBy(256).floor(),s=o.x+r/256,a=Math.min(o.y+n/256,Math.pow(2,i)-1),u=[];for(var h=o.x;h<s;h++)for(var l=o.y;l<a;l++)u.push({x:h,y:l,z:i});return u})({upperLeft:w.min,width:M,height:b,zoom:e})),D=i.max.x-i.min.x,L=i.max.y-i.min.y,P=D,S=L,C=JSON.stringify(U);if(!m&&C===Q&&e<g)return tt=Object.assign(Object.assign({},tt),{outputWidth:P,outputHeight:S,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),tt;const I=yield f.merge(U,{maxZoom:g,width:M,height:b,crossOrigin:"Anonymous",getSourceUrl:_,getElevation:x,tileSize:E,tileLoaded:s});if(null===I)return tt=Object.assign(Object.assign({},tt),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),tt;const F=new c(p(new d(o.bounds).getNorthWest(),e),p(new d(o.bounds).getSouthEast(),e)),B=F.min.subtract(w.min),N=F.max.subtract(w.min),X=[B.x/M,B.y/b,N.x/M,N.y/b].map((t=>2*t-1));let O=0;const z=yield v;O=l.raster({upperLeftTile:U[0],width:M,height:b,mapZoom:e,features:z,imageData:I,gl:a,dsmSource:o,dsmCoords:X}),O=Math.max(o.maxHeight,O);Q=C,tt={maxHeight:8848,maxBuildingHeight:O,width:M,height:b,imageData:I,DEMPixelBounds:w,visibleDEMPixelBounds:i,raster:U,demZoom:e,dirty:!0,outputWidth:P,outputHeight:S}}catch(t){console.error("Could not decode height map",t)}return tt})),rt=t=>{const{tile:e,maxZoom:r,tileSize:n}=t;let{x:i,y:o,z:s}=e;if(s>r){const t=Math.pow(2,e.z-r);i=Math.floor(i/t),o=Math.floor(o/t),s=r}return{x:256===n?i:Math.floor(i/2),y:256===n?o:Math.floor(o/2),z:256===n?s:s-1}};class nt{constructor(){this.canvas=document.createElement("canvas"),this.canvas.width=256,this.canvas.height=256,this.ctx=this.canvas.getContext("2d",{antialias:!1,alpha:!1,willReadFrequently:!0}),null!==this.ctx&&(this.ctx.imageSmoothingEnabled=!1),this.inProgress=[],this.finished=0}merge(t,e){return i(this,void 0,void 0,(function*(){const{width:r,height:n,crossOrigin:o,getSourceUrl:s,getElevation:a,tileSize:u,tileLoaded:h,maxZoom:l}=e;if(this.ctx){const e=new Uint8ClampedArray(2*r*n);this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const c=new Set;t.forEach((t=>{c.add(s(rt({tile:t,maxZoom:l,tileSize:u})))}));const f=Array.from(c).map((n=>i(this,void 0,void 0,(function*(){return new Promise(((i,h)=>{const c=new Image;this.inProgress.push(c),c.onload=()=>{t.filter((t=>n===s(rt({tile:t,maxZoom:l,tileSize:u})))).forEach((t=>{let n=0,i=0;const o=514===u?1:0;let s=256,h=256;if(256!==u&&(n=t.x%2*256,i=t.y%2*256,s=256,h=256),t.z>l){const e=Math.pow(2,t.z-l);256!==u&&(n=Math.floor(t.x/e)%2*256,i=Math.floor(t.y/e)%2*256),n+=t.x%e/e*256,i+=t.y%e/e*256,s=256/e,h=256/e}this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.drawImage(c,o+n,o+i,s,h,0,0,s,h);const f=this.ctx.getImageData(0,0,s,h).data,d=new Uint8ClampedArray(131072),m=new Array(s*h),_=256/s;for(let t=0;t<f.length;t+=4){const e=5*(a({r:f[t],g:f[t+1],b:f[t+2],a:f[t+3]})||0);m[t/4]=e}for(let t=0;t<d.length;t+=2){const e=m[Math.floor(t/2/256/_)*s+Math.floor(t/2%256/_)],r=Math.floor(e/256),n=Math.floor(e%256);d[t]=r,d[t+1]=n}for(let n=0;n<256;n++)e.set(d.slice(256*n*2,256*(n+1)*2),2*(t.yOffset*r+n*r+t.xOffset))})),i(null)},c.onerror=t=>{if(c.src!==c.originalSource)return h("new tiles requested");i(null)},c.crossOrigin=o||null,c.src=n,c.originalSource=c.src})).then((()=>{this.finished++,h(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(f)}catch(t){return console.log(`${f.length} requests aborted`,t),null}return this.inProgress=[],e}throw new Error("Could not get canvas context for merging tile images")}))}}const it=(t,e)=>{const{r:r,g:n,b:i}=t;return[r/255,n/255,i/255,e]},ot=(t,e)=>{const{date:r}=e,{dec:n,Hi:i}=T(r);t.updateDate({dec:n,Hi:i})},st=(t,e)=>i(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),at=(t,e)=>{const{color:r,opacity:n}=e,i=it(r,n);t.updateColor({colorVec:i})};new d([0,0],[0,0]);const ut=t=>{const{kernel:e,map:r,heightMap:n,now:i,color:o,opacity:s}=t;try{const{outputHeight:t,outputWidth:a,maxHeight:u,maxBuildingHeight:h,width:l,height:c,DEMPixelBounds:f,imageData:m,visibleDEMPixelBounds:_,demZoom:x}=n;if(0===l||0===c)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:v,y:y}=p.subtract(g),R=f.min,A=r.getPixelDimensions(),w=r.screenUnproject([0,0]),M=r.screenUnproject([A.width,0]),b=r.screenUnproject([A.width,A.height]),U=r.screenUnproject([0,A.height]),D=[U,b,w,M].map((t=>r.project(t,x))),L=D.map((t=>[(t.x-R.x)/l,(t.y-R.y)/c])).flat(),P=D.map((t=>[(t.x-g.x)/v,(t.y-g.y)/y])).map((t=>[2*t[0]-1,-1*(2*t[1]-1)])).flat(),S=R.y/256/Math.pow(2,x),C=c/256/Math.pow(2,x),I=new d(E(f.getTopLeft(),x),E(f.getBottomRight(),x)),F=I.getWest(),B=Math.abs(I.getWest()-I.getEast());((t,e)=>{const{color:r,opacity:n,date:i,imageData:o,maxBuildingHeight:s}=e,a=it(r,n),{dec:u,Hi:h}=T(i);t.updateLocation(Object.assign({dec:u,Hi:h,colorVec:a,step:1},e)),J(o).then((({maxHeight:e})=>{t.updateMaxHeight({maxHeight:Math.max(e,s)})}))})(e,{imageData:m,maxHeight:u,maxBuildingHeight:h,width:l,height:c,heightMapZoom:x,cornerTextureCoords:L,cornerClipCoords:P,topYCoord:S,ySize:C,west:F,dLng:B,date:i,color:o,opacity:s,outputHeight:t,outputWidth:a})}catch(t){console.error("EXCEPTION",t)}};const ht=t=>{const e=()=>!t.getPitch;return{project:(r,n)=>{if(e())return t.project(r,n);{const{lat:t,lng:e}=r;return new u(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,n),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,n))}},unproject:(r,n)=>{return e()?t.unproject(r,n):new x((i=r.y,o=n,s=Math.PI-2*Math.PI*i/256/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(s)-Math.exp(-s)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,n));var i,o,s},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 d(e,r)}}};class lt 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:n}=t;return 256*e+r+n/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>i(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>i(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)}))));const r={preserveDrawingBuffer:!0,antialias:!1};this._canvas=document.createElement("canvas"),this._gl=this._canvas.getContext("webgl",r)||this._canvas.getContext("experimental-webgl",r),this._compiledKernel=function(t){const{context:e}=t,r=e,n=R({gl:r,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 getElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*256.0*256.0+result.g*256.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 sampler2D user_sunExposureTexture;uniform bool user_renderToSunExposureTexture;uniform bool user_outputSunExposure;uniform vec4 user_exposureDelta;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_z=getElevationFromSampler2D(user_a,user_x,user_y);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.001;float user_lit=1.0;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=getElevationFromSampler2D(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));if(result.z==1.0){shade_color=vec4(.92941,.4375,.078125,.85);}iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){color(u_sunColor);}else{color(shade_color);}if(user_renderToSunExposureTexture){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));if((user_lit==1.0)){color(aggregateColor.r,0.0,0.0,1.0);}else{color(aggregateColor.r+user_exposureDelta.r,0.0,0.0,1.0);}if(user_outputSunExposure){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;}"});r.useProgram(n);const o=r.createBuffer(),s=r.getAttribLocation(n,"a_pos"),a=r.createBuffer(),u=r.getAttribLocation(n,"a_tex_pos");r.enable(r.BLEND),r.blendFunc(r.SRC_ALPHA,r.ONE_MINUS_SRC_ALPHA);const h=r.getUniformLocation(n,"user_a");r.uniform1i(h,0);const l=r.getUniformLocation(n,"user_width"),c=r.getUniformLocation(n,"user_height"),f=r.getUniformLocation(n,"user_maxHeight"),d=r.getUniformLocation(n,"user_zoom"),m=r.getUniformLocation(n,"user_topYCoord"),_=r.getUniformLocation(n,"user_ySize"),x=r.getUniformLocation(n,"user_step"),g=r.getUniformLocation(n,"user_west"),p=r.getUniformLocation(n,"user_dLng"),E=r.getUniformLocation(n,"user_dec"),v=r.getUniformLocation(n,"user_Hi"),y=r.getUniformLocation(n,"user_color"),A=r.getUniformLocation(n,"user_xStart"),w=r.getUniformLocation(n,"user_yStart"),M=r.getUniformLocation(n,"user_xEnd"),b=r.getUniformLocation(n,"user_yEnd"),U=r.getUniformLocation(n,"user_sunExposureTexture");r.uniform1i(U,2);const D=r.getUniformLocation(n,"user_renderToSunExposureTexture"),L=r.getUniformLocation(n,"user_outputSunExposure"),P=r.getUniformLocation(n,"user_exposureDelta"),S=r.getUniformLocation(n,"u_outputShadeProfile"),C=r.getUniformLocation(n,"u_gpxTexture"),I=r.getUniformLocation(n,"u_decHiTexture"),F=r.getUniformLocation(n,"u_sunColor"),B=r.getUniformLocation(n,"u_outputLocationShadeProfile"),N=r.getUniformLocation(n,"u_shadeProfileLocation"),X=()=>{r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4)};let O=0;return{updateLocation:function(t){const{width:e,height:i,maxHeight:h,heightMapZoom:T,topYCoord:R,ySize:A,colorVec:w,step:M,west:b,dLng:U,dec:D,Hi:L,cornerClipCoords:P,cornerTextureCoords:S,outputWidth:C,outputHeight:I}=t;r.useProgram(n),r.canvas.width=C,r.canvas.height=I,r.viewport(0,0,C,I),r.bindFramebuffer(r.FRAMEBUFFER,null),r.bindBuffer(r.ARRAY_BUFFER,o),r.bufferData(r.ARRAY_BUFFER,new Float32Array(P),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.bindBuffer(r.ARRAY_BUFFER,a),r.bufferData(r.ARRAY_BUFFER,new Float32Array(S),r.STATIC_DRAW),r.enableVertexAttribArray(u),r.vertexAttribPointer(u,2,r.FLOAT,!1,0,0),r.uniform1f(l,e),r.uniform1f(c,i),r.uniform1f(f,h),r.uniform1f(d,T),r.uniform1f(m,R),r.uniform1f(_,A),r.uniform4fv(y,w),r.uniform1f(x,M),r.uniform1f(g,b),r.uniform1f(p,U),r.uniform1f(E,D),r.uniform1f(v,L),window.requestAnimationFrame(X)},updateViewport:function(t){const{xStart:e,yStart:n,xEnd:i,yEnd:o}=t;r.uniform1f(A,e),r.uniform1f(w,n),r.uniform1f(M,i),r.uniform1f(b,o),window.requestAnimationFrame(X)},updateDate:t=>{const{dec:e,Hi:n}=t;r.uniform1f(E,e),r.uniform1f(v,n),window.requestAnimationFrame(X)},updateDateRange:t=>i(this,void 0,void 0,(function*(){const{startDate:e,endDate:n,iterations:i,emit:o}=t,s=O=Date.now();r.uniform4fv(P,new Float32Array([1/i,0,0,0]));const a=Math.floor((n.getTime()-e.getTime())/i),u=r.canvas.width,h=r.canvas.height,l=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,l),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,u,h,0,r.RGBA,r.UNSIGNED_BYTE,null),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.LINEAR);const c=r.createTexture();r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,c),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,u,h,0,r.RGBA,r.UNSIGNED_BYTE,null),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.LINEAR),r.uniform1i(D,1);const f=r.createFramebuffer();r.bindFramebuffer(r.FRAMEBUFFER,f);const d=r.COLOR_ATTACHMENT0;r.viewport(0,0,u,h);for(let t=0;t<i;t++){if(o("tileloaded",t,i-1),s!==O)return!0;yield new Promise(((n,i)=>{window.requestAnimationFrame((()=>{t%2==0?(r.uniform1i(U,2),r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,l),r.framebufferTexture2D(r.FRAMEBUFFER,d,r.TEXTURE_2D,l,0)):(r.uniform1i(U,1),r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,c),r.framebufferTexture2D(r.FRAMEBUFFER,d,r.TEXTURE_2D,c,0));const{dec:i,Hi:o}=T(new Date(e.getTime()+a*t));r.uniform1f(E,i),r.uniform1f(v,o),r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4),n()}))}))}return yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{s!==O&&t(!0),r.bindFramebuffer(r.FRAMEBUFFER,null),r.uniform1i(U,i%2==0?2:1),r.uniform1i(L,1),X(),r.uniform1i(D,0),r.uniform1i(L,0),r.deleteFramebuffer(f),r.deleteTexture(l),r.deleteTexture(c),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;r.uniform4fv(y,e),window.requestAnimationFrame(X)},updateMaxHeight:t=>{const{maxHeight:e}=t;r.uniform1f(f,e)},generateShadeProfile:t=>{if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoords:i,shadeColor:a,sunColor:u}=t,h=i.length,l=e.length,c=r.getParameter(r.VIEWPORT),f=r.getUniform(n,y);r.uniform4fv(y,a),r.uniform4fv(F,u),r.uniform1i(S,1);const d=r.createTexture();r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,d);const m=i.map((t=>[t[0],t[1],0,0])).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,m.length/4,1,0,r.RGBA,r.FLOAT,new Float32Array(m)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(C,2);const _=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,_);const x=e.map((t=>{const{dec:e,Hi:r}=T(t);return[-e,r/10,0,0]})).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,1,e.length,0,r.RGBA,r.FLOAT,new Float32Array(x)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(I,1),r.viewport(0,0,h,l),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,o),r.bufferData(r.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4);const g=new Uint8Array(h*l*4);return r.readPixels(0,0,h,l,r.RGBA,r.UNSIGNED_BYTE,g),r.deleteTexture(_),r.deleteTexture(d),r.uniform1i(I,0),r.uniform1i(C,0),r.uniform1i(S,0),r.viewport(c[0],c[1],c[2],c[3]),r.uniform4fv(y,f),r.uniform4fv(F,[0,0,0,0]),g},generateLocationShadeProfile:t=>{if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoord:i,shadeColor:a,sunColor:u}=t,h=e[0].length,l=e.length,c=r.getParameter(r.VIEWPORT),f=r.getUniform(n,y);r.uniform4fv(y,a),r.uniform4fv(F,u),r.uniform1i(B,1),r.uniform2fv(N,[i[0],i[1]]);const d=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,d);const m=e.flat().map((t=>{const{dec:e,Hi:r}=T(t);return[-e,r/10,0,0]})).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,h,l,0,r.RGBA,r.FLOAT,new Float32Array(m)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(I,1),r.viewport(0,0,h,l),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,o),r.bufferData(r.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4);const _=new Uint8Array(h*l*4);return r.readPixels(0,0,h,l,r.RGBA,r.UNSIGNED_BYTE,_),r.deleteTexture(d),r.uniform1i(I,0),r.uniform1i(B,0),r.viewport(c[0],c[1],c[2],c[3]),r.uniform4fv(y,f),r.uniform4fv(F,[0,0,0,0]),_}}}({context:this._gl}),this._color=this._parseColor(this.options.color),this._buildingRasterizer=new A(this._gl),this._tileMerger=new nt,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&&(ot(this._compiledKernel,{date:this.options.date}),this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=y(this.options.date)-y(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(at(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setOpacity(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(at(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return i(this,void 0,void 0,(function*(){if(this._map&&this._compiledKernel){if(!1===t)this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),ot(this._compiledKernel,{date:this.options.date});else{const{startDate:r,endDate:n,iterations:i=32}=e;if(this.options.sunExposure={enabled:t,startDate:r,endDate:n,iterations:i},!(r instanceof Date&&n instanceof Date))throw new Error("Start date or end date or both are missing");if(n.getTime()<r.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield st(this._compiledKernel,{startDate:r,endDate:n,iterations:i,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:n,height:i}=this._heightMap,o=e.min;return t.map((t=>{const e=p(t,r);return[(e.x-o.x)/n,(e.y-o.y)/i]}))}return[]}_getBounds(t,e){const{width:r,height:n}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(n))return t.getBounds();const i=t.getCenter(),o=p(i,e),s=E(new u(o.x-r/2,o.y-n/2),e),a=E(new u(o.x+r/2,o.y+n/2),e);return t.createBounds({nw:s,se:a})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return i(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=ht(this._map),e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}const r=yield et({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return i(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(ut({kernel:this._compiledKernel,map:ht(this._map),heightMap:t,color:this._color,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield st(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.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}toGeoTiff(){const t=(t,e,r,n)=>{const i=new Uint8Array(r*n*4);this._gl.readPixels(t,e,r,n,this._gl.RGBA,this._gl.UNSIGNED_BYTE,i);const o=r*n*1,s=1*r,a=(n-1)*s,u=new Uint8Array(o),h=new Uint8Array(o);let l=0;const{startDate:c,endDate:f}=this.options.sunExposure;this.options.sunExposure.enabled&&c&&f&&(l=f.getTime()-c.getTime());for(let t=0;t<i.length;t+=4){let e;if(this.options.sunExposure.enabled){const r=i.subarray(t,t+3),n=v(r,.5,l)/1e3/60,o=Math.min(Math.floor(n/6),255);e=new Uint8Array([o])}else{e=i[t]+i[t+1]+i[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}u.set(e,t/4*1)}for(let t=0;t<o;t+=s)h.set(u.subarray(t,t+s),a-t);return h};if(this._map){const e=this._canvas.width,r=this._canvas.height,n=t(0,0,e,r),i=ht(this._map),{lat:o,lng:s}=i.getBounds().getNorthWest(),a=i.getBounds().getSouthEast(),u=[0,0,0,s,o,0],h=[(a.lng-s)/e,(o-a.lat)/r,0];return this.options.sunExposure.enabled,{data:n,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:h,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height;this._canvas.width=t.locations.length,this._canvas.height=t.dates.length;const n=this._lngLatToTextureCoords(t.locations),i=((t,e)=>{const r=it({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),n=it({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:n}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:n}));return this._canvas.width=e,this._canvas.height=r,i}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height,n=y(t.startDate)-y(t.startDate,t.tzId),i=t.startDate.getTime()-n,o=(t.endDate.getTime()-n-i)/86400/1e3,s=1440;this._canvas.width=o,this._canvas.height=s;const a=[],u=828e5,h=y(new Date(i),t.tzId);for(let e=0;e<o;e++){const r=y(new Date(i+86400*e*1e3+u),t.tzId)-h;a[e]=r}const l=[];for(let t=0;t<s;t++){const e=[];for(let r=0;r<o;r++){const n=i+86400*r*1e3+60*t*1e3;e.push(new Date(n+a[r]))}l.push(e)}const c=this._lngLatToTextureCoords([t.location])[0],f=((t,e)=>{const r=it({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),n=it({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:n}))})(this._compiledKernel,Object.assign(Object.assign({},t),{dates:l,texCoord:c}));return this._canvas.width=e,this._canvas.height=r,f.toArray=function(){const e=new Array;for(let r=0;r<o;r++){const n=[];for(let e=s-1;e>=0;e--){const i=e*o*4+4*r,s=this.slice(i,i+4);n.push(s.join("")===t.sunColor.join("")?1:0)}e.push(n)}return e},{data:f,width:o,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:n,endDate:i}=this.options.sunExposure,o=i.getTime()-n.getTime(),s=v(r,.5,o);return Math.abs(s/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){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)}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 n(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._reset(),this}_repositionCanvas(t){this._canvasOverlay&&this._canvasOverlay.setBounds(t),this.emit("idle")}}void 0!==window.L&&(window.L.shadeMap=(...t)=>new lt(...t));export{lt as default};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Copyright Ted Piotrowski 2023
|
|
3
3
|
* Package: leaflet-shadow-simulator
|
|
4
|
-
* Version: 0.
|
|
4
|
+
* Version: 0.38.0
|
|
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";var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(t,r)};function r(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var n=function(){return(n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var i in e=arguments[r])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function i(t,e,r,n){return new(r||(r=Promise))((function(i,o){function a(t){try{u(n.next(t))}catch(t){o(t)}}function s(t){try{u(n.throw(t))}catch(t){o(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((n=n.apply(t,e||[])).next())}))}function o(t,e){var r,n,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,n=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=e.call(t,a)}catch(t){o=[6,t],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function a(t,e,r){if(r||2===arguments.length)for(var n,i=0,o=e.length;i<o;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}var s=function(e){function n(t,r,n){void 0===n&&(n={});var i=e.call(this,"",r,n)||this;return i._url=t,i}return r(n,e),n.prototype._initImage=function(){var 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},n.prototype.setBounds=function(t){return this._bounds=t,this._map&&this._reset(),this},n}(t.ImageOverlay);function u(t,e,r){var n=e[1],i=e[0],o=n-i;return t===n&&r?t:((t-i)%o+o)%o+i}function h(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var l=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 f=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function d(t,e,r){return t instanceof c?t:l(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 m(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function _(t,e){return!t||t instanceof m?t:new m(t,e)}function p(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function x(t,e){return t instanceof p?t:new p(t,e)}c.prototype={clone:function(){return new c(this.x,this.y)},add:function(t){return this.clone()._add(d(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(d(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=f(this.x),this.y=f(this.y),this},distanceTo:function(t){var e=(t=d(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=d(t)).x===this.x&&t.y===this.y},contains:function(t){return t=d(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+h(this.x)+", "+h(this.y)+")"}},m.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof c||"number"==typeof t[0]||"x"in t)e=r=d(t);else if(e=(t=_(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 d((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return d(this.min.x,this.max.y)},getTopRight:function(){return d(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?d(t):_(t))instanceof m?(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=_(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>=e.x&&n.x<=r.x,a=i.y>=e.y&&n.y<=r.y;return o&&a},overlaps:function(t){t=_(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>e.x&&n.x<r.x,a=i.y>e.y&&n.y<r.y;return o&&a},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,n=Math.abs(e.x-r.x)*t,i=Math.abs(e.y-r.y)*t;return _(d(e.x-n,e.y-i),d(r.x+n,r.y+i))},equals:function(t){return!!t&&(t=_(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},p.prototype={extend:function(t){var e,r,n=this._southWest,i=this._northEast;if(t instanceof v)e=t,r=t;else{if(!(t instanceof p))return t?this.extend(E(t)||x(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return n||i?(n.lat=Math.min(e.lat,n.lat),n.lng=Math.min(e.lng,n.lng),i.lat=Math.max(r.lat,i.lat),i.lng=Math.max(r.lng,i.lng)):(this._southWest=new v(e.lat,e.lng),this._northEast=new v(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,n=Math.abs(e.lat-r.lat)*t,i=Math.abs(e.lng-r.lng)*t;return new p(new v(e.lat-n,e.lng-i),new v(r.lat+n,r.lng+i))},getCenter:function(){return new v((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 v(this.getNorth(),this.getWest())},getSouthEast:function(){return new v(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 v||"lat"in t?E(t):x(t);var e,r,n=this._southWest,i=this._northEast;return t instanceof p?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=n.lat&&r.lat<=i.lat&&e.lng>=n.lng&&r.lng<=i.lng},intersects:function(t){t=x(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>=e.lat&&n.lat<=r.lat,a=i.lng>=e.lng&&n.lng<=r.lng;return o&&a},overlaps:function(t){t=x(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>e.lat&&n.lat<r.lat,a=i.lng>e.lng&&n.lng<r.lng;return o&&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,n,i;for(r=1,n=arguments.length;r<n;r++)for(e in i=arguments[r])t[e]=i[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),n=this.scale(e);return this.transformation._transform(r,n)},pointToLatLng:function(t,e){var r=this.scale(e),n=this.transformation.untransform(t,r);return this.projection.unproject(n)},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 m(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 v(this.wrapLat?u(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),n=e.lat-r.lat,i=e.lng-r.lng;if(0===n&&0===i)return t;var o=t.getSouthWest(),a=t.getNorthEast();return new p(new v(o.lat-n,o.lng-i),new v(a.lat-n,a.lng-i))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,n=t.lat*r,i=e.lat*r,o=Math.sin((e.lat-t.lat)*r/2),a=Math.sin((e.lng-t.lng)*r/2),s=o*o+Math.cos(n)*Math.cos(i)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function v(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 E(t,e,r){return t instanceof v?t:l(t)&&"object"!=typeof t[0]?3===t.length?new v(t[0],t[1],t[2]):2===t.length?new v(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new v(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new v(t,e,r)}v.prototype={equals:function(t,e){return!!t&&(t=E(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("+h(this.lat,t)+", "+h(this.lng,t)+")"},distanceTo:function(t){return g.distance(this,E(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 v(this.lat,this.lng,this.alt)}};var y=8848,T=256;var R=function(t,e){var r=t.lat;return new c(function(t,e){return(t+180)/360*Math.pow(2,e)*T}(t.lng,e),function(t,e){return(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*T}(r,e))},w=function(t,e){return new v((r=t.y,n=e,i=Math.PI-2*Math.PI*r/T/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/T/Math.pow(2,e)*360-180}(t.x,e));var r,n,i},A=.40909994067971484,b=function(t){var e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,n=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(n)*Math.cos(A),Math.cos(n));return{dec:Math.asin(Math.sin(A)*Math.sin(n)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},M=function(t,e,r){var n=1/e,i=Math.min(t[0]*n,255),o=Math.min(t[1]*n,255),a=Math.min(t[2]*n,255),s=0;return i+o+a!==0&&(s=i>0?i/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},U=function(t,e){void 0===t&&(t=new Date);var r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),n=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-n.getTime()};function D(t){var e=t.gl,r=t.vSrc,n=t.fSrc,i=e.createShader(e.VERTEX_SHADER);e.shaderSource(i,r),e.compileShader(i);var o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,n),e.compileShader(o);var a=e.createProgram();return e.attachShader(a,i),e.attachShader(a,o),e.linkProgram(a),a}function L(t){var e=this,r=t.context,n=D({gl:r,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 getElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*256.0*256.0+result.g*256.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 sampler2D user_sunExposureTexture;uniform bool user_renderToSunExposureTexture;uniform bool user_outputSunExposure;uniform vec4 user_exposureDelta;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_z=getElevationFromSampler2D(user_a,user_x,user_y);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.001;float user_lit=1.0;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=getElevationFromSampler2D(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));if(result.z==1.0){shade_color=vec4(.92941,.4375,.078125,.85);}iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){color(u_sunColor);}else{color(shade_color);}if(user_renderToSunExposureTexture){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));if((user_lit==1.0)){color(aggregateColor.r,0.0,0.0,1.0);}else{color(aggregateColor.r+user_exposureDelta.r,0.0,0.0,1.0);}if(user_outputSunExposure){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;}"});r.useProgram(n);var a=r.createBuffer(),s=r.getAttribLocation(n,"a_pos"),u=r.createBuffer(),h=r.getAttribLocation(n,"a_tex_pos");r.enable(r.BLEND),r.blendFunc(r.SRC_ALPHA,r.ONE_MINUS_SRC_ALPHA);var l=r.getUniformLocation(n,"user_a");r.uniform1i(l,0);var c=r.getUniformLocation(n,"user_width"),f=r.getUniformLocation(n,"user_height"),d=r.getUniformLocation(n,"user_maxHeight"),m=r.getUniformLocation(n,"user_zoom"),_=r.getUniformLocation(n,"user_topYCoord"),p=r.getUniformLocation(n,"user_ySize"),x=r.getUniformLocation(n,"user_step"),g=r.getUniformLocation(n,"user_west"),v=r.getUniformLocation(n,"user_dLng"),E=r.getUniformLocation(n,"user_dec"),y=r.getUniformLocation(n,"user_Hi"),T=r.getUniformLocation(n,"user_color"),R=r.getUniformLocation(n,"user_xStart"),w=r.getUniformLocation(n,"user_yStart"),A=r.getUniformLocation(n,"user_xEnd"),M=r.getUniformLocation(n,"user_yEnd"),U=r.getUniformLocation(n,"user_sunExposureTexture");r.uniform1i(U,2);var L=r.getUniformLocation(n,"user_renderToSunExposureTexture"),P=r.getUniformLocation(n,"user_outputSunExposure"),S=r.getUniformLocation(n,"user_exposureDelta"),C=r.getUniformLocation(n,"u_outputShadeProfile"),I=r.getUniformLocation(n,"u_gpxTexture"),F=r.getUniformLocation(n,"u_decHiTexture"),B=r.getUniformLocation(n,"u_sunColor"),N=r.getUniformLocation(n,"u_outputLocationShadeProfile"),X=r.getUniformLocation(n,"u_shadeProfileLocation"),O=function(){r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4)},z=0;return{updateLocation:function(t){var e=t.width,i=t.height,o=t.maxHeight,l=t.heightMapZoom,R=t.topYCoord,w=t.ySize,A=t.colorVec,b=t.step,M=t.west,U=t.dLng,D=t.dec,L=t.Hi,P=t.cornerClipCoords,S=t.cornerTextureCoords,C=t.outputWidth,I=t.outputHeight;r.useProgram(n),r.canvas.width=C,r.canvas.height=I,r.viewport(0,0,C,I),r.bindFramebuffer(r.FRAMEBUFFER,null),r.bindBuffer(r.ARRAY_BUFFER,a),r.bufferData(r.ARRAY_BUFFER,new Float32Array(P),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.bindBuffer(r.ARRAY_BUFFER,u),r.bufferData(r.ARRAY_BUFFER,new Float32Array(S),r.STATIC_DRAW),r.enableVertexAttribArray(h),r.vertexAttribPointer(h,2,r.FLOAT,!1,0,0),r.uniform1f(c,e),r.uniform1f(f,i),r.uniform1f(d,o),r.uniform1f(m,l),r.uniform1f(_,R),r.uniform1f(p,w),r.uniform4fv(T,A),r.uniform1f(x,b),r.uniform1f(g,M),r.uniform1f(v,U),r.uniform1f(E,D),r.uniform1f(y,L),window.requestAnimationFrame(O)},updateViewport:function(t){var e=t.xStart,n=t.yStart,i=t.xEnd,o=t.yEnd;r.uniform1f(R,e),r.uniform1f(w,n),r.uniform1f(A,i),r.uniform1f(M,o),window.requestAnimationFrame(O)},updateDate:function(t){var e=t.dec,n=t.Hi;r.uniform1f(E,e),r.uniform1f(y,n),window.requestAnimationFrame(O)},updateDateRange:function(t){return i(e,void 0,void 0,(function(){var e,n,i,a,s,u,h,l,c,f,d,m,_,p,x;return o(this,(function(g){switch(g.label){case 0:e=t.startDate,n=t.endDate,i=t.iterations,a=t.emit,s=z=Date.now(),r.uniform4fv(S,new Float32Array([1/i,0,0,0])),u=Math.floor((n.getTime()-e.getTime())/i),h=r.canvas.width,l=r.canvas.height,c=r.createTexture(),r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,c),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,h,l,0,r.RGBA,r.UNSIGNED_BYTE,null),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.LINEAR),f=r.createTexture(),r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,f),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,h,l,0,r.RGBA,r.UNSIGNED_BYTE,null),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.LINEAR),r.uniform1i(L,1),d=r.createFramebuffer(),r.bindFramebuffer(r.FRAMEBUFFER,d),m=r.COLOR_ATTACHMENT0,r.viewport(0,0,h,l),_=function(t){return o(this,(function(n){switch(n.label){case 0:return a("tileloaded",t,i-1),s!==z?[2,{value:!0}]:[4,new Promise((function(n,i){window.requestAnimationFrame((function(){t%2==0?(r.uniform1i(U,2),r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,c),r.framebufferTexture2D(r.FRAMEBUFFER,m,r.TEXTURE_2D,c,0)):(r.uniform1i(U,1),r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,f),r.framebufferTexture2D(r.FRAMEBUFFER,m,r.TEXTURE_2D,f,0));var i=b(new Date(e.getTime()+u*t)),o=i.dec,a=i.Hi;r.uniform1f(E,o),r.uniform1f(y,a),r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4),n()}))}))];case 1:return n.sent(),[2]}}))},p=0,g.label=1;case 1:return p<i?[5,_(p)]:[3,4];case 2:if("object"==typeof(x=g.sent()))return[2,x.value];g.label=3;case 3:return p++,[3,1];case 4:return[4,new Promise((function(t,e){window.requestAnimationFrame((function(){s!==z&&t(!0),r.bindFramebuffer(r.FRAMEBUFFER,null),r.uniform1i(U,i%2==0?2:1),r.uniform1i(P,1),O(),r.uniform1i(L,0),r.uniform1i(P,0),r.deleteFramebuffer(d),r.deleteTexture(c),r.deleteTexture(f),t(!1)}))}))];case 5:return[2,g.sent()]}}))}))},updateColor:function(t){var e=t.colorVec;r.uniform4fv(T,e),window.requestAnimationFrame(O)},updateMaxHeight:function(t){var e=t.maxHeight;r.uniform1f(d,e)},generateShadeProfile:function(t){if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");var e=t.dates,i=t.texCoords,o=t.shadeColor,u=t.sunColor,h=i.length,l=e.length,c=r.getParameter(r.VIEWPORT),f=r.getUniform(n,T);r.uniform4fv(T,o),r.uniform4fv(B,u),r.uniform1i(C,1);var d=r.createTexture();r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,d);var m=i.map((function(t){return[t[0],t[1],0,0]})).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,m.length/4,1,0,r.RGBA,r.FLOAT,new Float32Array(m)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(I,2);var _=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,_);var p=e.map((function(t){var e=b(t);return[-e.dec,e.Hi/10,0,0]})).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,1,e.length,0,r.RGBA,r.FLOAT,new Float32Array(p)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(F,1),r.viewport(0,0,h,l),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,a),r.bufferData(r.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4);var x=new Uint8Array(h*l*4);return r.readPixels(0,0,h,l,r.RGBA,r.UNSIGNED_BYTE,x),r.deleteTexture(_),r.deleteTexture(d),r.uniform1i(F,0),r.uniform1i(I,0),r.uniform1i(C,0),r.viewport(c[0],c[1],c[2],c[3]),r.uniform4fv(T,f),r.uniform4fv(B,[0,0,0,0]),x},generateLocationShadeProfile:function(t){if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");var e=t.dates,i=t.texCoord,o=t.shadeColor,u=t.sunColor,h=e[0].length,l=e.length,c=r.getParameter(r.VIEWPORT),f=r.getUniform(n,T);r.uniform4fv(T,o),r.uniform4fv(B,u),r.uniform1i(N,1),r.uniform2fv(X,[i[0],i[1]]);var d=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,d);var m=e.flat().map((function(t){var e=b(t);return[-e.dec,e.Hi/10,0,0]})).flat();r.texImage2D(r.TEXTURE_2D,0,r.RGBA,h,l,0,r.RGBA,r.FLOAT,new Float32Array(m)),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.NEAREST),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MAG_FILTER,r.NEAREST),r.uniform1i(F,1),r.viewport(0,0,h,l),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,a),r.bufferData(r.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),r.STATIC_DRAW),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4);var _=new Uint8Array(h*l*4);return r.readPixels(0,0,h,l,r.RGBA,r.UNSIGNED_BYTE,_),r.deleteTexture(d),r.uniform1i(F,0),r.uniform1i(N,0),r.viewport(c[0],c[1],c[2],c[3]),r.uniform4fv(T,f),r.uniform4fv(B,[0,0,0,0]),_}}}var P=function(){function t(t){this.gl=t,this.program=D({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nfloat PI = 3.141592653589793;\nvarying vec2 vCoord;\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\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 + 180.0) / 360.0 * pow(2.0, xyz.z)) * 256.0 - xyz.x;\n\t\tfloat y = (1.0 - (log(a_position.y) / PI)) / 2.0 * pow(2.0, xyz.z) * 256.0 - 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\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;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n\t \tgl_FragColor = vec4(textureColor.b, textureColor.a, 0, 1.0);\n\t\treturn;\n\t} \n\tgl_FragColor = color;\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.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}return t.prototype.raster=function(t){var e=this,r=t.features,n=t.upperLeftTile,i=t.mapZoom,o=t.width,a=t.height,s=t.imageData,u=t.dsmCoords,h=t.dsmSource,l=t.gl,f=n.x,d=n.y,m=n.z,_=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1);var p=l.createTexture();l.bindTexture(l.TEXTURE_2D,p),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,o,a,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,s);var x=o,g=a;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,x,g,0,l.RGBA,l.UNSIGNED_BYTE,null),i>15&&r.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,x,g);var v=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,v);var E=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,E,l.TEXTURE_2D,this.targetTexture,0),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);var y=f*T,w=d*T,A=new c(y,w);l.uniform3f(this.xyzUniformLocation,y,w,m),l.uniform2f(this.dimensionsUniformLocation,o,a),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);var 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),r.forEach((function(t){var r=t.buildingHeight,n=t.aPosition,i=t.cuts,u=t.startPos,h=t.highlight,c=R({lng:u[0],lat:u[1]},m).subtract(A).floor();if(!(c.x<0||c.y<0||c.x>o||c.y>a)){var f=256*s[c.y*o*2+2*c.x]+s[c.y*o*2+2*c.x+1]+5*r,d=Math.floor(f/256)/256,p=Math.floor(f%256)/256;_=Math.max(_,f/5),l.uniform4f(e.colorUniformLocation,d,p,h?1:0,1),l.bufferData(l.ARRAY_BUFFER,n,l.DYNAMIC_DRAW),l.vertexAttribPointer(e.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ARRAY_BUFFER,n,l.DYNAMIC_DRAW),l.vertexAttribPointer(e.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ELEMENT_ARRAY_BUFFER,i,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,i.length,i.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)}})),l.deleteTexture(p),0!==h.data.length){l.activeTexture(l.TEXTURE1);var M=l.createTexture();l.bindTexture(l.TEXTURE_2D,M),l.pixelStorei(l.UNPACK_ALIGNMENT,2),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST),l.texImage2D(l.TEXTURE_2D,0,l.LUMINANCE_ALPHA,h.width,h.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,h.data);var U=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],D=new Float32Array(U);l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,D,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.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(M)}return l.deleteFramebuffer(v),_},t}(),S=function(){function t(){this.events={}}return t.prototype.on=function(t,e){var r=this;return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),function(){return r.removeListener(t,e)}},t.prototype.removeListener=function(t,e){if("object"==typeof this.events[t]){var r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}},t.prototype.removeAllListeners=function(){var t=this;Object.keys(this.events).forEach((function(e){return t.events[e].splice(0,t.events[e].length)}))},t.prototype.emit=function(t){for(var e=this,r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];"object"==typeof this.events[t]&&a([],this.events[t]).forEach((function(t){return t.apply(e,r)}))},t.prototype.once=function(t,e){var r=this,n=this.on(t,(function(){for(var t=[],i=0;i<arguments.length;i++)t[i]=arguments[i];n(),e.apply(r,t)}));return n},t}();function C(t,e,r){r=r||2;var n,i,o,a,s,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=I(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(l&&(f=function(t,e,r,n){var i,o,a,s=[];for(i=0,o=e.length;i<o;i++)(a=I(t,e[i]*n,i<o-1?e[i+1]*n:t.length,n,!1))===a.next&&(a.steiner=!0),s.push(j(a));for(s.sort(H),i=0;i<s.length;i++)r=G(s[i],r);return r}(t,e,f,r)),t.length>80*r){n=o=t[0],i=a=t[1];for(var m=r;m<c;m+=r)(s=t[m])<n&&(n=s),(u=t[m+1])<i&&(i=u),s>o&&(o=s),u>a&&(a=u);h=0!==(h=Math.max(o-n,a-i))?32767/h:0}return B(f,d,r,n,i,h,0),d}function I(t,e,r,n,i){var o,a;if(i===it(t,e,r,n)>0)for(o=e;o<r;o+=n)a=et(o,t[o],t[o+1],a);else for(o=r-n;o>=e;o-=n)a=et(o,t[o],t[o+1],a);return a&&q(a,a.next)&&(rt(a),a=a.next),a}function F(t,e){if(!t)return t;e||(e=t);var r,n=t;do{if(r=!1,n.steiner||!q(n,n.next)&&0!==k(n.prev,n,n.next))n=n.next;else{if(rt(n),(n=e=n.prev)===n.next)break;r=!0}}while(r||n!==e);return e}function B(t,e,r,n,i,o,a){if(t){!a&&o&&function(t,e,r,n){var i=t;do{0===i.z&&(i.z=Z(i.x,i.y,e,r,n)),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,n,i,o,a,s,u,h=1;do{for(r=t,t=null,o=null,a=0;r;){for(a++,n=r,s=0,e=0;e<h&&(s++,n=n.nextZ);e++);for(u=h;s>0||u>0&&n;)0!==s&&(0===u||!n||r.z<=n.z)?(i=r,r=r.nextZ,s--):(i=n,n=n.nextZ,u--),o?o.nextZ=i:t=i,i.prevZ=o,o=i;r=n}o.nextZ=null,h*=2}while(a>1)}(i)}(t,n,i,o);for(var s,u,h=t;t.prev!==t.next;)if(s=t.prev,u=t.next,o?X(t,n,i,o):N(t))e.push(s.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),rt(t),t=u.next,h=u.next;else if((t=u)===h){a?1===a?B(t=O(F(t),e,r),e,r,n,i,o,2):2===a&&z(t,e,r,n,i,o):B(F(t),e,r,n,i,o,1);break}}}function N(t){var e=t.prev,r=t,n=t.next;if(k(e,r,n)>=0)return!1;for(var i=e.x,o=r.x,a=n.x,s=e.y,u=r.y,h=n.y,l=i<o?i<a?i:a:o<a?o:a,c=s<u?s<h?s:h:u<h?u:h,f=i>o?i>a?i:a:o>a?o:a,d=s>u?s>h?s:h:u>h?u:h,m=n.next;m!==e;){if(m.x>=l&&m.x<=f&&m.y>=c&&m.y<=d&&Y(i,s,o,u,a,h,m.x,m.y)&&k(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function X(t,e,r,n){var i=t.prev,o=t,a=t.next;if(k(i,o,a)>=0)return!1;for(var s=i.x,u=o.x,h=a.x,l=i.y,c=o.y,f=a.y,d=s<u?s<h?s:h:u<h?u:h,m=l<c?l<f?l:f:c<f?c:f,_=s>u?s>h?s:h:u>h?u:h,p=l>c?l>f?l:f:c>f?c:f,x=Z(d,m,e,r,n),g=Z(_,p,e,r,n),v=t.prevZ,E=t.nextZ;v&&v.z>=x&&E&&E.z<=g;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=p&&v!==i&&v!==a&&Y(s,l,u,c,h,f,v.x,v.y)&&k(v.prev,v,v.next)>=0)return!1;if(v=v.prevZ,E.x>=d&&E.x<=_&&E.y>=m&&E.y<=p&&E!==i&&E!==a&&Y(s,l,u,c,h,f,E.x,E.y)&&k(E.prev,E,E.next)>=0)return!1;E=E.nextZ}for(;v&&v.z>=x;){if(v.x>=d&&v.x<=_&&v.y>=m&&v.y<=p&&v!==i&&v!==a&&Y(s,l,u,c,h,f,v.x,v.y)&&k(v.prev,v,v.next)>=0)return!1;v=v.prevZ}for(;E&&E.z<=g;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=p&&E!==i&&E!==a&&Y(s,l,u,c,h,f,E.x,E.y)&&k(E.prev,E,E.next)>=0)return!1;E=E.nextZ}return!0}function O(t,e,r){var n=t;do{var i=n.prev,o=n.next.next;!q(i,o)&&V(i,n,n.next,o)&&Q(i,o)&&Q(o,i)&&(e.push(i.i/r|0),e.push(n.i/r|0),e.push(o.i/r|0),rt(n),rt(n.next),n=t=o),n=n.next}while(n!==t);return F(n)}function z(t,e,r,n,i,o){var a=t;do{for(var s=a.next.next;s!==a.prev;){if(a.i!==s.i&&K(a,s)){var u=tt(a,s);return a=F(a,a.next),u=F(u,u.next),B(a,e,r,n,i,o,0),void B(u,e,r,n,i,o,0)}s=s.next}a=a.next}while(a!==t)}function H(t,e){return t.x-e.x}function G(t,e){var r=function(t,e){var r,n=e,i=t.x,o=t.y,a=-1/0;do{if(o<=n.y&&o>=n.next.y&&n.next.y!==n.y){var s=n.x+(o-n.y)*(n.next.x-n.x)/(n.next.y-n.y);if(s<=i&&s>a&&(a=s,r=n.x<n.next.x?n:n.next,s===i))return r}n=n.next}while(n!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;n=r;do{i>=n.x&&n.x>=l&&i!==n.x&&Y(o<c?i:a,o,l,c,o<c?a:i,o,n.x,n.y)&&(u=Math.abs(o-n.y)/(i-n.x),Q(n,t)&&(u<f||u===f&&(n.x>r.x||n.x===r.x&&W(r,n)))&&(r=n,f=u)),n=n.next}while(n!==h);return r}(t,e);if(!r)return e;var n=tt(r,t);return F(n,n.next),F(r,r.next)}function W(t,e){return k(t.prev,t,e.prev)<0&&k(e.next,t,t.next)<0}function Z(t,e,r,n,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-n)*i|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function j(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 Y(t,e,r,n,i,o,a,s){return(i-a)*(e-s)>=(t-a)*(o-s)&&(t-a)*(n-s)>=(r-a)*(e-s)&&(r-a)*(o-s)>=(i-a)*(n-s)}function K(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)&&(Q(t,e)&&Q(e,t)&&function(t,e){var r=t,n=!1,i=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&i<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(n=!n),r=r.next}while(r!==t);return n}(t,e)&&(k(t.prev,t,e.prev)||k(t,e.prev,e))||q(t,e)&&k(t.prev,t,t.next)>0&&k(e.prev,e,e.next)>0)}function k(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function q(t,e){return t.x===e.x&&t.y===e.y}function V(t,e,r,n){var i=$(k(t,e,r)),o=$(k(t,e,n)),a=$(k(r,n,t)),s=$(k(r,n,e));return i!==o&&a!==s||(!(0!==i||!J(t,r,e))||(!(0!==o||!J(t,n,e))||(!(0!==a||!J(r,t,n))||!(0!==s||!J(r,e,n)))))}function J(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 $(t){return t>0?1:t<0?-1:0}function Q(t,e){return k(t.prev,t,t.next)<0?k(t,e,t.next)>=0&&k(t,t.prev,e)>=0:k(t,e,t.prev)<0||k(t,t.next,e)<0}function tt(t,e){var r=new nt(t.i,t.x,t.y),n=new nt(e.i,e.x,e.y),i=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=i,i.prev=r,n.next=r,r.prev=n,o.next=n,n.prev=o,n}function et(t,e,r,n){var i=new nt(t,e,r);return n?(i.next=n.next,i.prev=n,n.next.prev=i,n.next=i):(i.prev=i,i.next=i),i}function rt(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 nt(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 it(t,e,r,n){for(var i=0,o=e,a=r-n;o<r;o+=n)i+=(t[a]-t[o])*(t[o+1]+t[a+1]),a=o;return i}function ot(t){return t.filter((function(t){return"MultiPolygon"===t.geometry.type})).forEach((function(e){var r=e.geometry;if(e.properties,e.type,"MultiPolygon"===r.type)for(var i=0;i<r.coordinates.length;i++)t.push(n(n({},e),{geometry:n(n({},r),{type:"Polygon",coordinates:r.coordinates[i]})}))})),t.filter((function(t){return"Polygon"===t.geometry.type})).map((function(t){var e=t.geometry,r=t.properties,n=C.flatten(e.coordinates).vertices,i=new Float32Array(n.map((function(t,e){if(e%2==1){var r=t*Math.PI/180;return Math.tan(r)+1/Math.cos(r)}return t}))),o=C(n),a=o.length>256?new Uint16Array(o):new Uint8Array(o),s=function(t){var e=t,r=e.height,n=void 0===r?0:r,i=e.levels,o=void 0===i?0:i,a=e.render_height,s=void 0===a?0:a;if(o)return 3.04*o;return Math.max(n,s)}(r),u=r.highlight||!1;return{aPosition:i,cuts:a,buildingHeight:s,startPos:[n[0],n[1]],highlight:u}}))}C.deviation=function(t,e,r,n){var i=e&&e.length,o=i?e[0]*r:t.length,a=Math.abs(it(t,0,o,r));if(i)for(var s=0,u=e.length;s<u;s++){var h=e[s]*r,l=s<u-1?e[s+1]*r:t.length;a-=Math.abs(it(t,h,l,r))}var c=0;for(s=0;s<n.length;s+=3){var f=n[s]*r,d=n[s+1]*r,m=n[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)},C.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},n=0,i=0;i<t.length;i++){for(var o=0;o<t[i].length;o++)for(var a=0;a<e;a++)r.vertices.push(t[i][o][a]);i>0&&(n+=t[i-1].length,r.holes.push(n))}return r};var at,st=function(t){return new Promise((function(e,r){var n=new Int32Array(t.buffer).reduce((function(t,e){return Math.max(t,(n=(256*(255&(r=e))+(r>>8&255))/5,i=(256*(r>>16&255)+(r>>24&255))/5,Math.max(n,i)));var r,n,i}),0);e({maxHeight:Math.min(y,n)})}))},ut={width:0,height:0,imageData:new Uint8ClampedArray(0),visibleDEMPixelBounds:new m(new c(0,0),new c(0,0)),DEMPixelBounds:new m(new c(0,0),new c(0,0)),maxHeight:y,maxBuildingHeight:0,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0},ht=function(t){return i(void 0,void 0,void 0,(function(){var e,r,a,s,u,h,l,f,d,_,x,g,v,E,w,A,b,M,U,D,L,P,S,C,I,F,B,N,X,O,z,H,G,W,Z,j,Y,K,k,q,V,J,$;return o(this,(function(Q){switch(Q.label){case 0:e=t.demZoom,r=t.getFeatures,a=t.terrainSource,s=t.dsmSource,u=t.tileLoaded,h=t.gl,l=t.bounds,f=t.buildingRasterizer,d=t.tileMerger,_=t.forceUpdate,x=void 0!==_&&_,g=a.getSourceUrl,v=a.getElevation,E=a.maxZoom,w=a.tileSize,tt={getFeatures:r},A=i(void 0,void 0,void 0,(function(){var t,e,r;return o(this,(function(n){switch(n.label){case 0:t=tt.getFeatures,n.label=1;case 1:return n.trys.push([1,3,,4]),e=ot,[4,t()];case 2:return[2,e.apply(void 0,[n.sent()])];case 3:return r=n.sent(),console.log("Error merging buildings",r),[3,4];case 4:return[2,[]]}}))})),Q.label=1;case 1:return Q.trys.push([1,4,,5]),M=(b=l).getNorthWest(),U=b.getSouthEast(),D=new m(R(M,e),R(U,e)),L=new c(D.min.x,D.min.y),P=D.max.x-D.min.x,S=D.max.y-D.min.y,(C=D.max.subtract(D.min)).y>C.x&&(L.x-=T,P+=512),I=function(t){var e=t.upperLeft,r=t.width,n=t.height,i=e.divideBy(T).floor().multiplyBy(T),o=(Math.ceil(r/T)+1)*T,a=(Math.ceil(n/T)+1)*T,s=i.add([o,a]);return new m(i,s)}({upperLeft:L,width:P,height:S}),F=I.max.subtract(I.min),B=F.x,N=F.y,X=function(t){for(var e=t.upperLeft,r=t.width,n=t.height,i=t.zoom,o=e.divideBy(T).floor(),a=o.x+r/T,s=Math.min(o.y+n/T,Math.pow(2,i)-1),u=[],h=o.x;h<a;h++)for(var l=o.y;l<s;l++)u.push({x:h,y:l,z:i});return u}({upperLeft:I.min,width:B,height:N,zoom:e}),O=function(t){t.sort((function(t,e){return t.y!==e.y?t.y-e.y:t.x-e.x}));var e=t.reduce((function(t,e){return e.x<t.x?e:t})).x,r=t.reduce((function(t,e){return e.y<t.y?e:t})).y;return t.map((function(t){var n=Math.pow(2,t.z);return{x:(t.x%n+n)%n,y:(t.y%n+n)%n,z:t.z,xOffset:(t.x-e)*T,yOffset:(t.y-r)*T}}))}(X),z=D.max.x-D.min.x,H=D.max.y-D.min.y,G=z,W=H,Z=JSON.stringify(O),!x&&Z===at&&e<E?[2,ut=n(n({},ut),{outputWidth:G,outputHeight:W,visibleDEMPixelBounds:D,demZoom:e,dirty:!0})]:[4,d.merge(O,{maxZoom:E,width:B,height:N,crossOrigin:"Anonymous",getSourceUrl:g,getElevation:v,tileSize:w,tileLoaded:u})];case 2:return null===(j=Q.sent())?[2,ut=n(n({},ut),{visibleDEMPixelBounds:D,demZoom:e,dirty:!1})]:(Y=new m(R(new p(s.bounds).getNorthWest(),e),R(new p(s.bounds).getSouthEast(),e)),K=Y.min.subtract(I.min),k=Y.max.subtract(I.min),q=[K.x/B,K.y/N,k.x/B,k.y/N].map((function(t){return 2*t-1})),V=0,[4,A]);case 3:return J=Q.sent(),V=f.raster({upperLeftTile:O[0],width:B,height:N,mapZoom:e,features:J,imageData:j,gl:h,dsmSource:s,dsmCoords:q}),V=Math.max(s.maxHeight,V),y,at=Z,ut={maxHeight:8848,maxBuildingHeight:V,width:B,height:N,imageData:j,DEMPixelBounds:I,visibleDEMPixelBounds:D,raster:O,demZoom:e,dirty:!0,outputWidth:G,outputHeight:W},[3,5];case 4:return $=Q.sent(),console.error("Could not decode height map",$),[3,5];case 5:return[2,ut]}var tt}))}))},lt=function(t){var e=t.tile,r=t.maxZoom,n=t.tileSize,i=e.x,o=e.y,a=e.z;if(a>r){var s=Math.pow(2,e.z-r);i=Math.floor(i/s),o=Math.floor(o/s),a=r}return{x:n===T?i:Math.floor(i/2),y:n===T?o:Math.floor(o/2),z:n===T?a:a-1}},ct=function(){function t(){this.canvas=document.createElement("canvas"),this.canvas.width=T,this.canvas.height=T,this.ctx=this.canvas.getContext("2d",{antialias:!1,alpha:!1,willReadFrequently:!0}),null!==this.ctx&&(this.ctx.imageSmoothingEnabled=!1),this.inProgress=[],this.finished=0}return t.prototype.merge=function(t,e){return i(this,void 0,void 0,(function(){var r,n,a,s,u,h,l,c,f,d,m,_,p=this;return o(this,(function(x){switch(x.label){case 0:if(r=e.width,n=e.height,a=e.crossOrigin,s=e.getSourceUrl,u=e.getElevation,h=e.tileSize,l=e.tileLoaded,c=e.maxZoom,!this.ctx)return[3,5];f=new Uint8ClampedArray(2*r*n),this.inProgress.forEach((function(t){return t.src=""})),this.inProgress=[],this.finished=0,d=new Set,t.forEach((function(t){d.add(s(lt({tile:t,maxZoom:c,tileSize:h})))})),m=Array.from(d).map((function(e){return i(p,void 0,void 0,(function(){var n=this;return o(this,(function(i){return[2,new Promise((function(i,o){var l=new Image;n.inProgress.push(l),l.onload=function(){t.filter((function(t){return e===s(lt({tile:t,maxZoom:c,tileSize:h}))})).forEach((function(t){var e=0,i=0,o=514===h?1:0,a=T,s=T;if(h!==T&&(e=t.x%2*T,i=t.y%2*T,a=T,s=T),t.z>c){var d=Math.pow(2,t.z-c);h!==T&&(e=Math.floor(t.x/d)%2*T,i=Math.floor(t.y/d)%2*T),e+=T*(t.x%d/d),i+=T*(t.y%d/d),a=T/d,s=T/d}n.ctx.clearRect(0,0,n.canvas.width,n.canvas.height),n.ctx.drawImage(l,o+e,o+i,a,s,0,0,a,s);for(var m=n.ctx.getImageData(0,0,a,s).data,_=new Uint8ClampedArray(131072),p=new Array(a*s),x=T/a,g=0;g<m.length;g+=4){var v=5*((E=u({r:m[g],g:m[g+1],b:m[g+2],a:m[g+3]}))||0);p[g/4]=v}for(g=0;g<_.length;g+=2){var E=p[Math.floor(g/2/T/x)*a+Math.floor(g/2%T/x)],y=Math.floor(E/256),R=Math.floor(E%256);_[g]=y,_[g+1]=R}for(g=0;g<T;g++)f.set(_.slice(g*T*2,(g+1)*T*2),2*(t.yOffset*r+g*r+t.xOffset))})),i(null)},l.onerror=function(t){if(l.src!==l.originalSource)return o("new tiles requested");i(null)},l.crossOrigin=a||null,l.src=e,l.originalSource=l.src})).then((function(){n.finished++,l(n.finished,n.inProgress.length)}))]}))}))})),x.label=1;case 1:return x.trys.push([1,3,,4]),[4,Promise.all(m)];case 2:return x.sent(),[3,4];case 3:return _=x.sent(),console.log(m.length+" requests aborted",_),[2,null];case 4:return this.inProgress=[],[2,f];case 5:throw new Error("Could not get canvas context for merging tile images")}}))}))},t}(),ft=function(t,e){return[t.r/255,t.g/255,t.b/255,e]},dt=function(t,e){var r=e.date,n=b(r),i=n.dec,o=n.Hi;t.updateDate({dec:i,Hi:o})},mt=function(t,e){return i(void 0,void 0,void 0,(function(){return o(this,(function(r){switch(r.label){case 0:return[4,t.updateDateRange(n({},e))];case 1:return[2,r.sent()]}}))}))},_t=function(t,e){var r=e.color,n=e.opacity,i=ft(r,n);t.updateColor({colorVec:i})};new p([0,0],[0,0]);var pt=function(t){var e=t.kernel,r=t.map,i=t.heightMap,o=t.now,a=t.color,s=t.opacity;try{var u=i.outputHeight,h=i.outputWidth,l=i.maxHeight,c=i.maxBuildingHeight,f=i.width,d=i.height,m=i.DEMPixelBounds,_=i.imageData,x=i.visibleDEMPixelBounds,g=i.demZoom;if(0===f||0===d)return;var v=x.min,E=x.max;if(!v||!E)return;var y=E.subtract(v),R=y.x,A=y.y,M=m.min,U=r.getPixelDimensions(),D=r.screenUnproject([0,0]),L=r.screenUnproject([U.width,0]),P=r.screenUnproject([U.width,U.height]),S=[r.screenUnproject([0,U.height]),P,D,L].map((function(t){return r.project(t,g)})),C=S.map((function(t){return[(t.x-M.x)/f,(t.y-M.y)/d]})).flat(),I=S.map((function(t){return[(t.x-v.x)/R,(t.y-v.y)/A]})).map((function(t){return[2*t[0]-1,-1*(2*t[1]-1)]})).flat(),F=M.y/T/Math.pow(2,g),B=d/T/Math.pow(2,g),N=new p(w(m.getTopLeft(),g),w(m.getBottomRight(),g)),X=N.getWest(),O=Math.abs(N.getWest()-N.getEast());!function(t,e){var r=e.color,i=e.opacity,o=e.date,a=e.imageData,s=e.maxBuildingHeight,u=ft(r,i),h=b(o),l=h.dec,c=h.Hi;t.updateLocation(n({dec:l,Hi:c,colorVec:u,step:1},e)),st(a).then((function(e){var r=e.maxHeight;t.updateMaxHeight({maxHeight:Math.max(r,s)})}))}(e,{imageData:_,maxHeight:l,maxBuildingHeight:c,width:f,height:d,heightMapZoom:g,cornerTextureCoords:C,cornerClipCoords:I,topYCoord:F,ySize:B,west:X,dLng:O,date:o,color:a,opacity:s,outputHeight:u,outputWidth:h})}catch(t){console.error("EXCEPTION",t)}};var xt=function(t){var e=function(){return!t.getPitch};return{project:function(r,n){if(e())return t.project(r,n);var i=r.lat;return new c(function(t,e){return(t+180)/360*Math.pow(2,e)*T}(r.lng,n),function(t,e){return(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*T}(i,n))},unproject:function(r,n){return e()?t.unproject(r,n):new v((i=r.y,o=n,a=Math.PI-2*Math.PI*i/T/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/T/Math.pow(2,e)*360-180}(r.x,n));var i,o,a},screenUnproject:function(r){return e()?t.containerPointToLatLng(r):t.unproject(r)},getZoom:function(){return e()?t.getZoom():t.getZoom()+1},getCenter:function(){return t.getCenter()},getBounds:function(){return t.getBounds()},eachLayer:function(r){e()&&t.eachLayer(r)},getBearing:function(){return e()?0:t.getBearing()},getPitch:function(){return e()?0:t.getPitch()},rawMap:function(){return t},isLeaflet:e,getPixelDimensions:function(){var e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:function(t){var e=t.nw,r=t.se;return{getNorthWest:function(){return new v(e.lat,e.lng)},getNorthEast:function(){return new v(e.lat,r.lng)},getSouthWest:function(){return new v(r.lat,e.lng)},getSouthEast:function(){return new v(r.lat,r.lng)}}}}},gt=function(t){function e(e){return t.call(this,e)||this}return r(e,t),e.prototype.addTo=function(t){return this.onAdd(t)},e.prototype.onAdd=function(t){if(this._map=t,!this._canvasOverlay&&this._canvas){var e=this._map.getBounds();this._canvasOverlay=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return new(s.bind.apply(s,a([void 0],t)))}(this._canvas,e),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._reset(),this},e.prototype._repositionCanvas=function(t){this._canvasOverlay&&this._canvasOverlay.setBounds(t),this.emit("idle")},e}(function(t){function e(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];var n=t.call(this)||this;n.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:function(t){return t.x,t.y,t.z,"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png"},getElevation:function(t){return 256*t.r+t.g+t.b/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},getFeatures:function(){return Promise.resolve([])},getSize:function(){return{width:Number.NaN,height:Number.NaN}},debug:function(t){}};var a=e[0];if(n.options=Object.assign(n.options,a),!n.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:n.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((function(t){return i(n,void 0,void 0,(function(){var e;return o(this,(function(r){switch(r.label){case 0:return 200===t.status?[3,2]:(e=Error.bind,[4,t.text()]);case 1:throw new(e.apply(Error,[void 0,r.sent()]));case 2:return[2]}}))}))})).catch((function(t){return i(n,void 0,void 0,(function(){return o(this,(function(e){throw new Error("Could not load ShadeMap API. Key: "+this.options.apiKey+" Error: "+t)}))}))}));var s={preserveDrawingBuffer:!0,antialias:!1};return n._canvas=document.createElement("canvas"),n._gl=n._canvas.getContext("webgl",s)||n._canvas.getContext("experimental-webgl",s),n._compiledKernel=L({context:n._gl}),n._color=n._parseColor(n.options.color),n._buildingRasterizer=new P(n._gl),n._tileMerger=new ct,n._reset=n._reset.bind(n),n._draw=n._draw.bind(n),n}return r(e,t),e.prototype.onRemove=function(){return this._map&&this._map.off("moveend",this._reset),this},e.prototype.setDate=function(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(dt(this._compiledKernel,{date:this.options.date}),this._flush())):this.emit("idle"),this},e.prototype._setDateForTimezone=function(t,e){var r=0;e&&(r=U(this.options.date)-U(this.options.date,e)),this.setDate(new Date(t.getTime()-r))},e.prototype.setColor=function(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(_t(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this},e.prototype.setOpacity=function(t){return this.options.opacity!==t&&(this.options.opacity=t,this._compiledKernel&&(_t(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this},e.prototype.setTerrainSource=function(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this},e.prototype.setDSMSource=function(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this},e.prototype.setSunExposure=function(t,e){return void 0===t&&(t=!1),i(this,void 0,void 0,(function(){var r,i,a,s,u;return o(this,(function(o){switch(o.label){case 0:return this._map&&this._compiledKernel?!1!==t?[3,1]:(this.options.sunExposure=n(n({},this.options.sunExposure),{enabled:t}),dt(this._compiledKernel,{date:this.options.date}),[3,3]):[3,4];case 1:if(i=(r=e).startDate,a=r.endDate,s=r.iterations,u=void 0===s?32:s,this.options.sunExposure={enabled:t,startDate:i,endDate:a,iterations:u},!(i instanceof Date&&a instanceof Date))throw new Error("Start date or end date or both are missing");if(a.getTime()<i.getTime())throw new Error("End date must come after the start date to calculate sun exposure");return[4,mt(this._compiledKernel,{startDate:i,endDate:a,iterations:u,emit:this.emit.bind(this)})];case 2:if(!0===o.sent())return[2,this];o.label=3;case 3:this._flush(),o.label=4;case 4:return[2,this]}}))}))},e.prototype._lngLatToTextureCoords=function(t){if(this._heightMap){var e=this._heightMap,r=e.DEMPixelBounds,n=e.demZoom,i=e.width,o=e.height,a=r.min;return t.map((function(t){var e=R(t,n);return[(e.x-a.x)/i,(e.y-a.y)/o]}))}return[]},e.prototype._getBounds=function(t,e){var r=this.options.getSize(),n=r.width,i=r.height;if(Number.isNaN(n)||Number.isNaN(i))return t.getBounds();var o=t.getCenter(),a=R(o,e),s=w(new c(a.x-n/2,a.y-i/2),e),u=w(new c(a.x+n/2,a.y+i/2),e);return t.createBounds({nw:s,se:u})},e.prototype._getDEMZoom=function(t){var e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)},e.prototype._reset=function(){return i(this,void 0,void 0,(function(){var t,e,r,n=this;return o(this,(function(i){switch(i.label){case 0:if(this.options.debug("_reset()"),!this._map)return[3,3];t=xt(this._map),e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),[2,this]}return[4,ht({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:function(t,e){return n.emit("tileloaded",t,e)},forceUpdate:!this._heightMap})];case 1:return r=i.sent(),this._heightMap=r,r.dirty?[4,this._draw(r)]:[3,3];case 2:i.sent(),i.label=3;case 3:return[2,this]}}))}))},e.prototype._draw=function(t){return i(this,void 0,void 0,(function(){var e,r,n,i;return o(this,(function(o){switch(o.label){case 0:return this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map?(pt({kernel:this._compiledKernel,map:xt(this._map),heightMap:t,color:this._color,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom}),this.options.sunExposure.enabled?(e=this.options.sunExposure,r=e.startDate,n=e.endDate,i=e.iterations,[4,mt(this._compiledKernel,{startDate:r,endDate:n,iterations:i,emit:this.emit.bind(this)})]):[3,2]):[3,3];case 1:if(!0===o.sent())return[2,this];o.label=2;case 2:this._repositionCanvas(this._bounds),o.label=3;case 3:return[2,this]}}))}))},e.prototype.readPixel=function(t,e){var r=new Uint8Array(4);return this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r},e.prototype.toGeoTiff=function(){var t=this;if(this._map){var e=this._canvas.width,r=this._canvas.height,n=function(e,r,n,i){var o=new Uint8Array(n*i*4);t._gl.readPixels(e,r,n,i,t._gl.RGBA,t._gl.UNSIGNED_BYTE,o);var a=n*i*1,s=1*n,u=(i-1)*s,h=new Uint8Array(a),l=new Uint8Array(a),c=0,f=t.options.sunExposure,d=f.startDate,m=f.endDate;t.options.sunExposure.enabled&&d&&m&&(c=m.getTime()-d.getTime());for(var _=0;_<o.length;_+=4){var p=void 0;if(t.options.sunExposure.enabled){var x=o.subarray(_,_+3),g=M(x,.5,c)/1e3/60,v=Math.min(Math.floor(g/6),255);p=new Uint8Array([v])}else p=o[_]+o[_+1]+o[_+2]===0?new Uint8Array([255]):new Uint8Array([0]);h.set(p,_/4*1)}for(_=0;_<a;_+=s)l.set(h.subarray(_,_+s),u-_);return l}(0,0,e,r),i=xt(this._map),o=i.getBounds().getNorthWest(),a=o.lat,s=o.lng,u=i.getBounds().getSouthEast(),h=[0,0,0,s,a,0],l=[(u.lng-s)/e,(a-u.lat)/r,0];return this.options.sunExposure.enabled,{data:n,metadata:{width:e,height:r,ModelTiepoint:h,ModelPixelScale:l,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null},e.prototype._generateShadeProfile=function(t){if(this._compiledKernel){var e=this._canvas.width,r=this._canvas.height;this._canvas.width=t.locations.length,this._canvas.height=t.dates.length;var i=this._lngLatToTextureCoords(t.locations),o=function(t,e){var r=ft({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=ft({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(n(n({},e),{sunColor:r,shadeColor:i}))}(this._compiledKernel,n(n({},t),{texCoords:i}));return this._canvas.width=e,this._canvas.height=r,o}return new Uint8Array},e.prototype._generateLocationShadeProfile=function(t){if(this._compiledKernel){var e=this._canvas.width,r=this._canvas.height,i=U(t.startDate)-U(t.startDate,t.tzId),o=t.startDate.getTime()-i,a=(t.endDate.getTime()-i-o)/86400/1e3,s=1440;this._canvas.width=a,this._canvas.height=s;for(var u=[],h=U(new Date(o),t.tzId),l=0;l<a;l++){var c=o+86400*l*1e3+828e5,f=U(new Date(c),t.tzId)-h;u[l]=f}for(var d=[],m=0;m<s;m++){var _=[];for(l=0;l<a;l++){c=o+86400*l*1e3+60*m*1e3;_.push(new Date(c+u[l]))}d.push(_)}var p=this._lngLatToTextureCoords([t.location])[0],x=function(t,e){var r=ft({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=ft({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(n(n({},e),{sunColor:r,shadeColor:i}))}(this._compiledKernel,n(n({},t),{dates:d,texCoord:p}));return this._canvas.width=e,this._canvas.height=r,x.toArray=function(){for(var e=new Array,r=0;r<a;r++){for(var n=[],i=1439;i>=0;i--){var o=i*a*4+4*r,s=this.slice(o,o+4);n.push(s.join("")===t.sunColor.join("")?1:0)}e.push(n)}return e},{data:x,width:a,height:s}}return{data:new Uint8Array,width:0,height:0}},e.prototype.getHoursOfSun=function(t,e){if(this.options.sunExposure.enabled){var r=this.readPixel(t,e),n=this.options.sunExposure,i=n.startDate,o=n.endDate.getTime()-i.getTime(),a=M(r,.5,o);return Math.abs(a/1e3/3600)}return 0},e.prototype._repositionCanvas=function(t){},e.prototype._flush=function(){},e.prototype.flushSync=function(){this._gl.finish()},e.prototype._parseColor=function(t){t=t.replace("#","");var 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},e}(S));return void 0!==window.L&&(window.L.shadeMap=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return new(gt.bind.apply(gt,a([void 0],t)))}),gt}));
|
|
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,n){return new(r||(r=Promise))((function(i,o){function s(t){try{u(n.next(t))}catch(t){o(t)}}function a(t){try{u(n.throw(t))}catch(t){o(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(s,a)}u((n=n.apply(t,e||[])).next())}))}function n(t,e,r){var n=e[1],i=e[0],o=n-i;return t===n&&r?t:((t-i)%o+o)%o+i}function i(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var o=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function s(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var a=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function u(t,e,r){return t instanceof s?t:o(t)?new s(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new s(t.x,t.y):new s(t,e,r)}function h(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function l(t,e){return!t||t instanceof h?t:new h(t,e)}function c(t,e){if(t)for(var r=e?[t,e]:t,n=0,i=r.length;n<i;n++)this.extend(r[n])}function f(t,e){return t instanceof c?t:new c(t,e)}s.prototype={clone:function(){return new s(this.x,this.y)},add:function(t){return this.clone()._add(u(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(u(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new s(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new s(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=a(this.x),this.y=a(this.y),this},distanceTo:function(t){var e=(t=u(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=u(t)).x===this.x&&t.y===this.y},contains:function(t){return t=u(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+i(this.x)+", "+i(this.y)+")"}},h.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof s||"number"==typeof t[0]||"x"in t)e=r=u(t);else if(e=(t=l(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 u((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return u(this.min.x,this.max.y)},getTopRight:function(){return u(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof s?u(t):l(t))instanceof h?(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=l(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>=e.x&&n.x<=r.x,s=i.y>=e.y&&n.y<=r.y;return o&&s},overlaps:function(t){t=l(t);var e=this.min,r=this.max,n=t.min,i=t.max,o=i.x>e.x&&n.x<r.x,s=i.y>e.y&&n.y<r.y;return o&&s},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,n=Math.abs(e.x-r.x)*t,i=Math.abs(e.y-r.y)*t;return l(u(e.x-n,e.y-i),u(r.x+n,r.y+i))},equals:function(t){return!!t&&(t=l(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},c.prototype={extend:function(t){var e,r,n=this._southWest,i=this._northEast;if(t instanceof m)e=t,r=t;else{if(!(t instanceof c))return t?this.extend(_(t)||f(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return n||i?(n.lat=Math.min(e.lat,n.lat),n.lng=Math.min(e.lng,n.lng),i.lat=Math.max(r.lat,i.lat),i.lng=Math.max(r.lng,i.lng)):(this._southWest=new m(e.lat,e.lng),this._northEast=new m(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,n=Math.abs(e.lat-r.lat)*t,i=Math.abs(e.lng-r.lng)*t;return new c(new m(e.lat-n,e.lng-i),new m(r.lat+n,r.lng+i))},getCenter:function(){return new m((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 m(this.getNorth(),this.getWest())},getSouthEast:function(){return new m(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 m||"lat"in t?_(t):f(t);var e,r,n=this._southWest,i=this._northEast;return t instanceof c?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=n.lat&&r.lat<=i.lat&&e.lng>=n.lng&&r.lng<=i.lng},intersects:function(t){t=f(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>=e.lat&&n.lat<=r.lat,s=i.lng>=e.lng&&n.lng<=r.lng;return o&&s},overlaps:function(t){t=f(t);var e=this._southWest,r=this._northEast,n=t.getSouthWest(),i=t.getNorthEast(),o=i.lat>e.lat&&n.lat<r.lat,s=i.lng>e.lng&&n.lng<r.lng;return o&&s},toBBoxString:function(){return[this.getWest(),this.getSouth(),this.getEast(),this.getNorth()].join(",")},equals:function(t,e){return!!t&&(t=f(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var d=function(t){var e,r,n,i;for(r=1,n=arguments.length;r<n;r++)for(e in i=arguments[r])t[e]=i[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),n=this.scale(e);return this.transformation._transform(r,n)},pointToLatLng:function(t,e){var r=this.scale(e),n=this.transformation.untransform(t,r);return this.projection.unproject(n)},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 h(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?n(t.lng,this.wrapLng,!0):t.lng;return new m(this.wrapLat?n(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),n=e.lat-r.lat,i=e.lng-r.lng;if(0===n&&0===i)return t;var o=t.getSouthWest(),s=t.getNorthEast();return new c(new m(o.lat-n,o.lng-i),new m(s.lat-n,s.lng-i))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,n=t.lat*r,i=e.lat*r,o=Math.sin((e.lat-t.lat)*r/2),s=Math.sin((e.lng-t.lng)*r/2),a=o*o+Math.cos(n)*Math.cos(i)*s*s,u=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return this.R*u}});function m(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 _(t,e,r){return t instanceof m?t:o(t)&&"object"!=typeof t[0]?3===t.length?new m(t[0],t[1],t[2]):2===t.length?new m(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new m(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new m(t,e,r)}m.prototype={equals:function(t,e){return!!t&&(t=_(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("+i(this.lat,t)+", "+i(this.lng,t)+")"},distanceTo:function(t){return d.distance(this,_(t))},wrap:function(){return d.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return f([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new m(this.lat,this.lng,this.alt)}};const x=8848,g=256;const p=(t,e)=>{const{lat:r,lng:n}=t;return new s(((t,e)=>(t+180)/360*Math.pow(2,e)*g)(n,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*g)(r,e))},E=(t,e)=>{return new m((r=t.y,n=e,i=Math.PI-2*Math.PI*r/g/Math.pow(2,n),180/Math.PI*Math.atan(.5*(Math.exp(i)-Math.exp(-i)))),function(t,e){return t/g/Math.pow(2,e)*360-180}(t.x,e));var r,n,i},T=.40909994067971484,v=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,n=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(n)*Math.cos(T),Math.cos(n));return{dec:Math.asin(Math.sin(T)*Math.sin(n)),Hi:(4.889714432387314+6.3003876824396166*e-i)%(2*Math.PI)+2*Math.PI}},y=(t,e,r)=>{const n=1/e,i=Math.min(t[0]*n,255),o=Math.min(t[1]*n,255),s=Math.min(t[2]*n,255);let a=0;return i+o+s!==0&&(a=i>0?i/255*.5+.5:s>0?.5*(1-s/255):.5),a*r},R=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),n=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-n.getTime()};function A(t){const{gl:e,vSrc:r,fSrc:n}=t,i=e.createShader(e.VERTEX_SHADER);e.shaderSource(i,r),e.compileShader(i);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,n),e.compileShader(o);const s=e.createProgram();return e.attachShader(s,i),e.attachShader(s,o),e.linkProgram(s),s}class w{constructor(t){this.gl=t,this.program=A({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nfloat PI = 3.141592653589793;\nvarying vec2 vCoord;\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\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 + 180.0) / 360.0 * pow(2.0, xyz.z)) * 256.0 - xyz.x;\n\t\tfloat y = (1.0 - (log(a_position.y) / PI)) / 2.0 * pow(2.0, xyz.z) * 256.0 - 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\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;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n\t \tgl_FragColor = vec4(textureColor.b, textureColor.a, 0, 1.0);\n\t\treturn;\n\t} \n\tgl_FragColor = color;\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.colorUniformLocation=t.getUniformLocation(this.program,"color"),this.positionBuffer=t.createBuffer(),this.dsmBuffer=t.createBuffer(),this.indexBuffer=t.createBuffer(),this.targetTexture=t.createTexture()}raster(t){const{features:e,upperLeftTile:r,mapZoom:n,width:i,height:o,imageData:a,dsmCoords:u,dsmSource:h,gl:l}=t,{x:c,y:f,z:d}=r;let m=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1);const _=l.createTexture();l.bindTexture(l.TEXTURE_2D,_),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,i,o,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,a);const x=i,E=o;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,x,E,0,l.RGBA,l.UNSIGNED_BYTE,null),n>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,x,E);const T=l.createFramebuffer();l.bindFramebuffer(l.FRAMEBUFFER,T);const v=l.COLOR_ATTACHMENT0;l.framebufferTexture2D(l.FRAMEBUFFER,v,l.TEXTURE_2D,this.targetTexture,0),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 y=c*g,R=f*g,A=new s(y,R);l.uniform3f(this.xyzUniformLocation,y,R,d),l.uniform2f(this.dimensionsUniformLocation,i,o),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const w=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,w,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:n,startPos:s,highlight:u}=t,h=p({lng:s[0],lat:s[1]},d).subtract(A).floor();if(h.x<0||h.y<0||h.x>i||h.y>o)return;const c=256*a[h.y*i*2+2*h.x]+a[h.y*i*2+2*h.x+1]+5*e,f=Math.floor(c/256)/256,_=Math.floor(c%256)/256;m=Math.max(m,c/5),l.uniform4f(this.colorUniformLocation,f,_,u?1:0,1),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,n,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,n.length,n.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),l.deleteTexture(_),0!==h.data.length){l.activeTexture(l.TEXTURE1);const t=l.createTexture();l.bindTexture(l.TEXTURE_2D,t),l.pixelStorei(l.UNPACK_ALIGNMENT,2),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST),l.texImage2D(l.TEXTURE_2D,0,l.LUMINANCE_ALPHA,h.width,h.height,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,h.data);const e=[u[0],u[3],u[2],u[3],u[0],u[1],u[2],u[1]],r=new Float32Array(e);l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,r,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.enableVertexAttribArray(this.dsmAttributeLocation),l.bindBuffer(l.ARRAY_BUFFER,this.dsmBuffer),l.bufferData(l.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),l.STATIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.uniform1i(this.useDSMUniformLocation,1),l.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.deleteFramebuffer(T),m}}function M(t,e,r){r=r||2;var n,i,o,s,a,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=b(t,0,c,r,!0),d=[];if(!f||f.next===f.prev)return d;if(l&&(f=function(t,e,r,n){var i,o,s,a=[];for(i=0,o=e.length;i<o;i++)(s=b(t,e[i]*n,i<o-1?e[i+1]*n:t.length,n,!1))===s.next&&(s.steiner=!0),a.push(X(s));for(a.sort(I),i=0;i<a.length;i++)r=F(a[i],r);return r}(t,e,f,r)),t.length>80*r){n=o=t[0],i=s=t[1];for(var m=r;m<c;m+=r)(a=t[m])<n&&(n=a),(u=t[m+1])<i&&(i=u),a>o&&(o=a),u>s&&(s=u);h=0!==(h=Math.max(o-n,s-i))?32767/h:0}return D(f,d,r,n,i,h,0),d}function b(t,e,r,n,i){var o,s;if(i===$(t,e,r,n)>0)for(o=e;o<r;o+=n)s=q(o,t[o],t[o+1],s);else for(o=r-n;o>=e;o-=n)s=q(o,t[o],t[o+1],s);return s&&G(s,s.next)&&(k(s),s=s.next),s}function U(t,e){if(!t)return t;e||(e=t);var r,n=t;do{if(r=!1,n.steiner||!G(n,n.next)&&0!==H(n.prev,n,n.next))n=n.next;else{if(k(n),(n=e=n.prev)===n.next)break;r=!0}}while(r||n!==e);return e}function D(t,e,r,n,i,o,s){if(t){!s&&o&&function(t,e,r,n){var i=t;do{0===i.z&&(i.z=N(i.x,i.y,e,r,n)),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,n,i,o,s,a,u,h=1;do{for(r=t,t=null,o=null,s=0;r;){for(s++,n=r,a=0,e=0;e<h&&(a++,n=n.nextZ);e++);for(u=h;a>0||u>0&&n;)0!==a&&(0===u||!n||r.z<=n.z)?(i=r,r=r.nextZ,a--):(i=n,n=n.nextZ,u--),o?o.nextZ=i:t=i,i.prevZ=o,o=i;r=n}o.nextZ=null,h*=2}while(s>1)}(i)}(t,n,i,o);for(var a,u,h=t;t.prev!==t.next;)if(a=t.prev,u=t.next,o?P(t,n,i,o):L(t))e.push(a.i/r|0),e.push(t.i/r|0),e.push(u.i/r|0),k(t),t=u.next,h=u.next;else if((t=u)===h){s?1===s?D(t=S(U(t),e,r),e,r,n,i,o,2):2===s&&C(t,e,r,n,i,o):D(U(t),e,r,n,i,o,1);break}}}function L(t){var e=t.prev,r=t,n=t.next;if(H(e,r,n)>=0)return!1;for(var i=e.x,o=r.x,s=n.x,a=e.y,u=r.y,h=n.y,l=i<o?i<s?i:s:o<s?o:s,c=a<u?a<h?a:h:u<h?u:h,f=i>o?i>s?i:s:o>s?o:s,d=a>u?a>h?a:h:u>h?u:h,m=n.next;m!==e;){if(m.x>=l&&m.x<=f&&m.y>=c&&m.y<=d&&O(i,a,o,u,s,h,m.x,m.y)&&H(m.prev,m,m.next)>=0)return!1;m=m.next}return!0}function P(t,e,r,n){var i=t.prev,o=t,s=t.next;if(H(i,o,s)>=0)return!1;for(var a=i.x,u=o.x,h=s.x,l=i.y,c=o.y,f=s.y,d=a<u?a<h?a:h:u<h?u:h,m=l<c?l<f?l:f:c<f?c:f,_=a>u?a>h?a:h:u>h?u:h,x=l>c?l>f?l:f:c>f?c:f,g=N(d,m,e,r,n),p=N(_,x,e,r,n),E=t.prevZ,T=t.nextZ;E&&E.z>=g&&T&&T.z<=p;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=x&&E!==i&&E!==s&&O(a,l,u,c,h,f,E.x,E.y)&&H(E.prev,E,E.next)>=0)return!1;if(E=E.prevZ,T.x>=d&&T.x<=_&&T.y>=m&&T.y<=x&&T!==i&&T!==s&&O(a,l,u,c,h,f,T.x,T.y)&&H(T.prev,T,T.next)>=0)return!1;T=T.nextZ}for(;E&&E.z>=g;){if(E.x>=d&&E.x<=_&&E.y>=m&&E.y<=x&&E!==i&&E!==s&&O(a,l,u,c,h,f,E.x,E.y)&&H(E.prev,E,E.next)>=0)return!1;E=E.prevZ}for(;T&&T.z<=p;){if(T.x>=d&&T.x<=_&&T.y>=m&&T.y<=x&&T!==i&&T!==s&&O(a,l,u,c,h,f,T.x,T.y)&&H(T.prev,T,T.next)>=0)return!1;T=T.nextZ}return!0}function S(t,e,r){var n=t;do{var i=n.prev,o=n.next.next;!G(i,o)&&W(i,n,n.next,o)&&Y(i,o)&&Y(o,i)&&(e.push(i.i/r|0),e.push(n.i/r|0),e.push(o.i/r|0),k(n),k(n.next),n=t=o),n=n.next}while(n!==t);return U(n)}function C(t,e,r,n,i,o){var s=t;do{for(var a=s.next.next;a!==s.prev;){if(s.i!==a.i&&z(s,a)){var u=K(s,a);return s=U(s,s.next),u=U(u,u.next),D(s,e,r,n,i,o,0),void D(u,e,r,n,i,o,0)}a=a.next}s=s.next}while(s!==t)}function I(t,e){return t.x-e.x}function F(t,e){var r=function(t,e){var r,n=e,i=t.x,o=t.y,s=-1/0;do{if(o<=n.y&&o>=n.next.y&&n.next.y!==n.y){var a=n.x+(o-n.y)*(n.next.x-n.x)/(n.next.y-n.y);if(a<=i&&a>s&&(s=a,r=n.x<n.next.x?n:n.next,a===i))return r}n=n.next}while(n!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;n=r;do{i>=n.x&&n.x>=l&&i!==n.x&&O(o<c?i:s,o,l,c,o<c?s:i,o,n.x,n.y)&&(u=Math.abs(o-n.y)/(i-n.x),Y(n,t)&&(u<f||u===f&&(n.x>r.x||n.x===r.x&&B(r,n)))&&(r=n,f=u)),n=n.next}while(n!==h);return r}(t,e);if(!r)return e;var n=K(r,t);return U(n,n.next),U(r,r.next)}function B(t,e){return H(t.prev,t,e.prev)<0&&H(e.next,t,t.next)<0}function N(t,e,r,n,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-n)*i|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function X(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 O(t,e,r,n,i,o,s,a){return(i-s)*(e-a)>=(t-s)*(o-a)&&(t-s)*(n-a)>=(r-s)*(e-a)&&(r-s)*(o-a)>=(i-s)*(n-a)}function z(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&&W(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(Y(t,e)&&Y(e,t)&&function(t,e){var r=t,n=!1,i=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&i<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(n=!n),r=r.next}while(r!==t);return n}(t,e)&&(H(t.prev,t,e.prev)||H(t,e.prev,e))||G(t,e)&&H(t.prev,t,t.next)>0&&H(e.prev,e,e.next)>0)}function H(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function G(t,e){return t.x===e.x&&t.y===e.y}function W(t,e,r,n){var i=Z(H(t,e,r)),o=Z(H(t,e,n)),s=Z(H(r,n,t)),a=Z(H(r,n,e));return i!==o&&s!==a||(!(0!==i||!j(t,r,e))||(!(0!==o||!j(t,n,e))||(!(0!==s||!j(r,t,n))||!(0!==a||!j(r,e,n)))))}function j(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function Z(t){return t>0?1:t<0?-1:0}function Y(t,e){return H(t.prev,t,t.next)<0?H(t,e,t.next)>=0&&H(t,t.prev,e)>=0:H(t,e,t.prev)<0||H(t,t.next,e)<0}function K(t,e){var r=new V(t.i,t.x,t.y),n=new V(e.i,e.x,e.y),i=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=i,i.prev=r,n.next=r,r.prev=n,o.next=n,n.prev=o,n}function q(t,e,r,n){var i=new V(t,e,r);return n?(i.next=n.next,i.prev=n,n.next.prev=i,n.next=i):(i.prev=i,i.next=i),i}function k(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 V(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 $(t,e,r,n){for(var i=0,o=e,s=r-n;o<r;o+=n)i+=(t[s]-t[o])*(t[o+1]+t[s+1]),s=o;return i}function J(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:n,type:i}=e;if("MultiPolygon"===r.type)for(let n=0;n<r.coordinates.length;n++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[n]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:n}=M.flatten(e.coordinates),i=new Float32Array(n.map(((t,e)=>{if(e%2==1){const e=t*Math.PI/180;return Math.tan(e)+1/Math.cos(e)}return t}))),o=M(n),s=o.length>256?new Uint16Array(o):new Uint8Array(o),a=function(t){const{height:e=0,levels:r=0,render_height:n=0}=t;if(r)return 3.04*r;return Math.max(e,n)}(r),u=r.highlight||!1;return{aPosition:i,cuts:s,buildingHeight:a,startPos:[n[0],n[1]],highlight:u}}))}M.deviation=function(t,e,r,n){var i=e&&e.length,o=i?e[0]*r:t.length,s=Math.abs($(t,0,o,r));if(i)for(var a=0,u=e.length;a<u;a++){var h=e[a]*r,l=a<u-1?e[a+1]*r:t.length;s-=Math.abs($(t,h,l,r))}var c=0;for(a=0;a<n.length;a+=3){var f=n[a]*r,d=n[a+1]*r,m=n[a+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===s&&0===c?0:Math.abs((c-s)/s)},M.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},n=0,i=0;i<t.length;i++){for(var o=0;o<t[i].length;o++)for(var s=0;s<e;s++)r.vertices.push(t[i][o][s]);i>0&&(n+=t[i-1].length,r.holes.push(n))}return r};const Q=t=>new Promise(((e,r)=>{const n=new Int32Array(t.buffer).reduce(((t,e)=>Math.max(t,(t=>{const e=(256*(255&t)+(t>>8&255))/5,r=(256*(t>>16&255)+(t>>24&255))/5;return Math.max(e,r)})(e))),0);e({maxHeight:Math.min(x,n)})}));let tt,et={width:0,height:0,imageData:new Uint8ClampedArray(0),visibleDEMPixelBounds:new h(new s(0,0),new s(0,0)),DEMPixelBounds:new h(new s(0,0),new s(0,0)),maxHeight:x,maxBuildingHeight:0,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const rt=t=>r(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:n,terrainSource:i,dsmSource:o,tileLoaded:a,gl:u,bounds:l,buildingRasterizer:f,tileMerger:d,forceUpdate:m=!1}=t,{getSourceUrl:_,getElevation:E,maxZoom:T,tileSize:v,_overzoom:y}=i,R=(A={getFeatures:n},r(void 0,void 0,void 0,(function*(){const{getFeatures:t}=A;try{return J(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var A;try{const t=l,r=t.getNorthWest(),n=t.getSouthEast(),i=new h(p(r,e),p(n,e));let y=new s(i.min.x,i.min.y),A=i.max.x-i.min.x;const w=i.max.y-i.min.y,M=i.max.subtract(i.min);M.y>M.x&&(y.x-=g,A+=512);const b=(t=>{const{upperLeft:e,width:r,height:n}=t,i=e.divideBy(g).floor().multiplyBy(g),o=(Math.ceil(r/g)+1)*g,s=(Math.ceil(n/g)+1)*g,a=i.add([o,s]);return new h(i,a)})({upperLeft:y,width:A,height:w}),{x:U,y:D}=b.max.subtract(b.min),L=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.x<t.x?e:t)).x,r=t.reduce(((t,e)=>e.y<t.y?e:t)).y;return t.map((t=>{const n=Math.pow(2,t.z);return{x:(t.x%n+n)%n,y:(t.y%n+n)%n,z:t.z,xOffset:(t.x-e)*g,yOffset:(t.y-r)*g}}))})((t=>{const{upperLeft:e,width:r,height:n,zoom:i}=t,o=e.divideBy(g).floor(),s=o.x+r/g,a=Math.min(o.y+n/g,Math.pow(2,i)-1),u=[];for(var h=o.x;h<s;h++)for(var l=o.y;l<a;l++)u.push({x:h,y:l,z:i});return u})({upperLeft:b.min,width:U,height:D,zoom:e})),P=i.max.x-i.min.x,S=i.max.y-i.min.y,C=P,I=S,F=JSON.stringify(L);if(!m&&F===tt&&e<T)return et=Object.assign(Object.assign({},et),{outputWidth:C,outputHeight:I,visibleDEMPixelBounds:i,demZoom:e,dirty:!0}),et;const B=yield d.merge(L,{maxZoom:T,width:U,height:D,crossOrigin:"Anonymous",getSourceUrl:_,getElevation:E,tileSize:v,tileLoaded:a});if(null===B)return et=Object.assign(Object.assign({},et),{visibleDEMPixelBounds:i,demZoom:e,dirty:!1}),et;const N=new h(p(new c(o.bounds).getNorthWest(),e),p(new c(o.bounds).getSouthEast(),e)),X=N.min.subtract(b.min),O=N.max.subtract(b.min),z=[X.x/U,X.y/D,O.x/U,O.y/D].map((t=>2*t-1));let H=0;const G=yield R;H=f.raster({upperLeftTile:L[0],width:U,height:D,mapZoom:e,features:G,imageData:B,gl:u,dsmSource:o,dsmCoords:z}),H=Math.max(o.maxHeight,H);tt=F,et={maxHeight:x,maxBuildingHeight:H,width:U,height:D,imageData:B,DEMPixelBounds:b,visibleDEMPixelBounds:i,raster:L,demZoom:e,dirty:!0,outputWidth:C,outputHeight:I}}catch(t){console.error("Could not decode height map",t)}return et})),nt=t=>{const{tile:e,maxZoom:r,tileSize:n}=t;let{x:i,y:o,z:s}=e;if(s>r){const t=Math.pow(2,e.z-r);i=Math.floor(i/t),o=Math.floor(o/t),s=r}return{x:n===g?i:Math.floor(i/2),y:n===g?o:Math.floor(o/2),z:n===g?s:s-1}};class it{constructor(){this.canvas=document.createElement("canvas"),this.canvas.width=g,this.canvas.height=g,this.ctx=this.canvas.getContext("2d",{antialias:!1,alpha:!1,willReadFrequently:!0}),null!==this.ctx&&(this.ctx.imageSmoothingEnabled=!1),this.inProgress=[],this.finished=0}merge(t,e){return r(this,void 0,void 0,(function*(){const{width:n,height:i,crossOrigin:o,getSourceUrl:s,getElevation:a,tileSize:u,tileLoaded:h,maxZoom:l}=e;if(this.ctx){const e=new Uint8ClampedArray(2*n*i);this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const c=new Set;t.forEach((t=>{c.add(s(nt({tile:t,maxZoom:l,tileSize:u})))}));const f=Array.from(c).map((i=>r(this,void 0,void 0,(function*(){return new Promise(((r,h)=>{const c=new Image;this.inProgress.push(c),c.onload=()=>{t.filter((t=>i===s(nt({tile:t,maxZoom:l,tileSize:u})))).forEach((t=>{let r=0,i=0;const o=514===u?1:0;let s=g,h=g;if(u!==g&&(r=t.x%2*g,i=t.y%2*g,s=g,h=g),t.z>l){const e=Math.pow(2,t.z-l);u!==g&&(r=Math.floor(t.x/e)%2*g,i=Math.floor(t.y/e)%2*g),r+=g*(t.x%e/e),i+=g*(t.y%e/e),s=g/e,h=g/e}this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this.ctx.drawImage(c,o+r,o+i,s,h,0,0,s,h);const f=this.ctx.getImageData(0,0,s,h).data,d=new Uint8ClampedArray(131072),m=new Array(s*h),_=g/s;for(let t=0;t<f.length;t+=4){const e=5*(a({r:f[t],g:f[t+1],b:f[t+2],a:f[t+3]})||0);m[t/4]=e}for(let t=0;t<d.length;t+=2){const e=m[Math.floor(t/2/g/_)*s+Math.floor(t/2%g/_)],r=Math.floor(e/256),n=Math.floor(e%256);d[t]=r,d[t+1]=n}for(let r=0;r<g;r++)e.set(d.slice(r*g*2,(r+1)*g*2),2*(t.yOffset*n+r*n+t.xOffset))})),r(null)},c.onerror=t=>{if(c.src!==c.originalSource)return h("new tiles requested");r(null)},c.crossOrigin=o||null,c.src=i,c.originalSource=c.src})).then((()=>{this.finished++,h(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(f)}catch(t){return console.log(`${f.length} requests aborted`,t),null}return this.inProgress=[],e}throw new Error("Could not get canvas context for merging tile images")}))}}const ot=(t,e)=>{const{r:r,g:n,b:i}=t;return[r/255,n/255,i/255,e]},st=(t,e)=>{const{date:r}=e,{dec:n,Hi:i}=v(r);t.updateDate({dec:n,Hi:i})},at=(t,e)=>r(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),ut=(t,e)=>{const{color:r,opacity:n}=e,i=ot(r,n);t.updateColor({colorVec:i})};new c([0,0],[0,0]);const ht=t=>{const{kernel:e,map:r,heightMap:n,now:i,color:o,opacity:s}=t;try{const{outputHeight:t,outputWidth:a,maxHeight:u,maxBuildingHeight:h,width:l,height:f,DEMPixelBounds:d,imageData:m,visibleDEMPixelBounds:_,demZoom:x}=n;if(0===l||0===f)return;const{min:p,max:T}=_;if(!p||!T)return;const{x:y,y:R}=T.subtract(p),A=d.min,w=r.getPixelDimensions(),M=r.screenUnproject([0,0]),b=r.screenUnproject([w.width,0]),U=r.screenUnproject([w.width,w.height]),D=r.screenUnproject([0,w.height]),L=[D,U,M,b].map((t=>r.project(t,x))),P=L.map((t=>[(t.x-A.x)/l,(t.y-A.y)/f])).flat(),S=L.map((t=>[(t.x-p.x)/y,(t.y-p.y)/R])).map((t=>[2*t[0]-1,-1*(2*t[1]-1)])).flat(),C=A.y/g/Math.pow(2,x),I=f/g/Math.pow(2,x),F=new c(E(d.getTopLeft(),x),E(d.getBottomRight(),x)),B=F.getWest(),N=Math.abs(F.getWest()-F.getEast());((t,e)=>{const{color:r,opacity:n,date:i,imageData:o,maxBuildingHeight:s}=e,a=ot(r,n),{dec:u,Hi:h}=v(i);t.updateLocation(Object.assign({dec:u,Hi:h,colorVec:a,step:1},e)),Q(o).then((({maxHeight:e})=>{t.updateMaxHeight({maxHeight:Math.max(e,s)})}))})(e,{imageData:m,maxHeight:u,maxBuildingHeight:h,width:l,height:f,heightMapZoom:x,cornerTextureCoords:P,cornerClipCoords:S,topYCoord:C,ySize:I,west:B,dLng:N,date:i,color:o,opacity:s,outputHeight:t,outputWidth:a})}catch(t){console.error("EXCEPTION",t)}};const lt=t=>{const e=()=>!t.getPitch;return{project:(r,n)=>{if(e())return t.project(r,n);{const{lat:t,lng:e}=r;return new s(((t,e)=>(t+180)/360*Math.pow(2,e)*g)(e,n),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*g)(t,n))}},unproject:(r,n)=>{return e()?t.unproject(r,n):new m((i=r.y,o=n,s=Math.PI-2*Math.PI*i/g/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(s)-Math.exp(-s)))),function(t,e){return t/g/Math.pow(2,e)*360-180}(r.x,n));var i,o,s},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 c(e,r)}}};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:n}=t;return 256*e+r+n/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},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}`)}))));const n={preserveDrawingBuffer:!0,antialias:!1};this._canvas=document.createElement("canvas"),this._gl=this._canvas.getContext("webgl",n)||this._canvas.getContext("experimental-webgl",n),this._compiledKernel=function(t){const{context:e}=t,n=e,i=A({gl:n,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*256.0*256.0+result.g*256.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 sampler2D user_sunExposureTexture;uniform bool user_renderToSunExposureTexture;uniform bool user_outputSunExposure;uniform vec4 user_exposureDelta;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_z=getElevationFromSampler2D(user_a,user_x,user_y);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.001;float user_lit=1.0;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=getElevationFromSampler2D(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));if(result.z==1.0){shade_color=vec4(.92941,.4375,.078125,.85);}iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){color(u_sunColor);}else{color(shade_color);}if(user_renderToSunExposureTexture){vec4 aggregateColor=texture2D(user_sunExposureTexture,vec2(vTexCoordFull.x,vTexCoordFull.y));if((user_lit==1.0)){color(aggregateColor.r,0.0,0.0,1.0);}else{color(aggregateColor.r+user_exposureDelta.r,0.0,0.0,1.0);}if(user_outputSunExposure){float timeInSun=1.0-aggregateColor.r;float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,0.5);}}}void main(void){kernel();gl_FragColor=actualColor;}"});n.useProgram(i);const o=n.createBuffer(),s=n.getAttribLocation(i,"a_pos"),a=n.createBuffer(),u=n.getAttribLocation(i,"a_tex_pos");n.enable(n.BLEND),n.blendFunc(n.SRC_ALPHA,n.ONE_MINUS_SRC_ALPHA);const h=n.getUniformLocation(i,"user_a");n.uniform1i(h,0);const l=n.getUniformLocation(i,"user_width"),c=n.getUniformLocation(i,"user_height"),f=n.getUniformLocation(i,"user_maxHeight"),d=n.getUniformLocation(i,"user_zoom"),m=n.getUniformLocation(i,"user_topYCoord"),_=n.getUniformLocation(i,"user_ySize"),x=n.getUniformLocation(i,"user_step"),g=n.getUniformLocation(i,"user_west"),p=n.getUniformLocation(i,"user_dLng"),E=n.getUniformLocation(i,"user_dec"),T=n.getUniformLocation(i,"user_Hi"),y=n.getUniformLocation(i,"user_color"),R=n.getUniformLocation(i,"user_xStart"),w=n.getUniformLocation(i,"user_yStart"),M=n.getUniformLocation(i,"user_xEnd"),b=n.getUniformLocation(i,"user_yEnd"),U=n.getUniformLocation(i,"user_sunExposureTexture");n.uniform1i(U,2);const D=n.getUniformLocation(i,"user_renderToSunExposureTexture"),L=n.getUniformLocation(i,"user_outputSunExposure"),P=n.getUniformLocation(i,"user_exposureDelta"),S=n.getUniformLocation(i,"u_outputShadeProfile"),C=n.getUniformLocation(i,"u_gpxTexture"),I=n.getUniformLocation(i,"u_decHiTexture"),F=n.getUniformLocation(i,"u_sunColor"),B=n.getUniformLocation(i,"u_outputLocationShadeProfile"),N=n.getUniformLocation(i,"u_shadeProfileLocation"),X=()=>{n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4)};let O=0;return{updateLocation:function(t){const{width:e,height:r,maxHeight:h,heightMapZoom:v,topYCoord:R,ySize:A,colorVec:w,step:M,west:b,dLng:U,dec:D,Hi:L,cornerClipCoords:P,cornerTextureCoords:S,outputWidth:C,outputHeight:I}=t;n.useProgram(i),n.canvas.width=C,n.canvas.height=I,n.viewport(0,0,C,I),n.bindFramebuffer(n.FRAMEBUFFER,null),n.bindBuffer(n.ARRAY_BUFFER,o),n.bufferData(n.ARRAY_BUFFER,new Float32Array(P),n.STATIC_DRAW),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,a),n.bufferData(n.ARRAY_BUFFER,new Float32Array(S),n.STATIC_DRAW),n.enableVertexAttribArray(u),n.vertexAttribPointer(u,2,n.FLOAT,!1,0,0),n.uniform1f(l,e),n.uniform1f(c,r),n.uniform1f(f,h),n.uniform1f(d,v),n.uniform1f(m,R),n.uniform1f(_,A),n.uniform4fv(y,w),n.uniform1f(x,M),n.uniform1f(g,b),n.uniform1f(p,U),n.uniform1f(E,D),n.uniform1f(T,L),window.requestAnimationFrame(X)},updateViewport:function(t){const{xStart:e,yStart:r,xEnd:i,yEnd:o}=t;n.uniform1f(R,e),n.uniform1f(w,r),n.uniform1f(M,i),n.uniform1f(b,o),window.requestAnimationFrame(X)},updateDate:t=>{const{dec:e,Hi:r}=t;n.uniform1f(E,e),n.uniform1f(T,r),window.requestAnimationFrame(X)},updateDateRange:t=>r(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:i,emit:o}=t,s=O=Date.now();n.uniform4fv(P,new Float32Array([1/i,0,0,0]));const a=Math.floor((r.getTime()-e.getTime())/i),u=n.canvas.width,h=n.canvas.height,l=n.createTexture();n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,l),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,u,h,0,n.RGBA,n.UNSIGNED_BYTE,null),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.LINEAR),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.LINEAR);const c=n.createTexture();n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,c),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,u,h,0,n.RGBA,n.UNSIGNED_BYTE,null),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.LINEAR),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.LINEAR),n.uniform1i(D,1);const f=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,f);const d=n.COLOR_ATTACHMENT0;n.viewport(0,0,u,h);for(let t=0;t<i;t++){if(o("tileloaded",t,i-1),s!==O)return!0;yield new Promise(((r,i)=>{window.requestAnimationFrame((()=>{t%2==0?(n.uniform1i(U,2),n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,l),n.framebufferTexture2D(n.FRAMEBUFFER,d,n.TEXTURE_2D,l,0)):(n.uniform1i(U,1),n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,c),n.framebufferTexture2D(n.FRAMEBUFFER,d,n.TEXTURE_2D,c,0));const{dec:i,Hi:o}=v(new Date(e.getTime()+a*t));n.uniform1f(E,i),n.uniform1f(T,o),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4),r()}))}))}return yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{s!==O&&t(!0),n.bindFramebuffer(n.FRAMEBUFFER,null),n.uniform1i(U,i%2==0?2:1),n.uniform1i(L,1),X(),n.uniform1i(D,0),n.uniform1i(L,0),n.deleteFramebuffer(f),n.deleteTexture(l),n.deleteTexture(c),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;n.uniform4fv(y,e),window.requestAnimationFrame(X)},updateMaxHeight:t=>{const{maxHeight:e}=t;n.uniform1f(f,e)},generateShadeProfile:t=>{if(null===n.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoords:r,shadeColor:a,sunColor:u}=t,h=r.length,l=e.length,c=n.getParameter(n.VIEWPORT),f=n.getUniform(i,y);n.uniform4fv(y,a),n.uniform4fv(F,u),n.uniform1i(S,1);const d=n.createTexture();n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,d);const m=r.map((t=>[t[0],t[1],0,0])).flat();n.texImage2D(n.TEXTURE_2D,0,n.RGBA,m.length/4,1,0,n.RGBA,n.FLOAT,new Float32Array(m)),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.uniform1i(C,2);const _=n.createTexture();n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,_);const x=e.map((t=>{const{dec:e,Hi:r}=v(t);return[-e,r/10,0,0]})).flat();n.texImage2D(n.TEXTURE_2D,0,n.RGBA,1,e.length,0,n.RGBA,n.FLOAT,new Float32Array(x)),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.uniform1i(I,1),n.viewport(0,0,h,l),n.clear(n.COLOR_BUFFER_BIT),n.bindBuffer(n.ARRAY_BUFFER,o),n.bufferData(n.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),n.STATIC_DRAW),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.drawArrays(n.TRIANGLE_STRIP,0,4);const g=new Uint8Array(h*l*4);return n.readPixels(0,0,h,l,n.RGBA,n.UNSIGNED_BYTE,g),n.deleteTexture(_),n.deleteTexture(d),n.uniform1i(I,0),n.uniform1i(C,0),n.uniform1i(S,0),n.viewport(c[0],c[1],c[2],c[3]),n.uniform4fv(y,f),n.uniform4fv(F,[0,0,0,0]),g},generateLocationShadeProfile:t=>{if(null===n.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoord:r,shadeColor:a,sunColor:u}=t,h=e[0].length,l=e.length,c=n.getParameter(n.VIEWPORT),f=n.getUniform(i,y);n.uniform4fv(y,a),n.uniform4fv(F,u),n.uniform1i(B,1),n.uniform2fv(N,[r[0],r[1]]);const d=n.createTexture();n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,d);const m=e.flat().map((t=>{const{dec:e,Hi:r}=v(t);return[-e,r/10,0,0]})).flat();n.texImage2D(n.TEXTURE_2D,0,n.RGBA,h,l,0,n.RGBA,n.FLOAT,new Float32Array(m)),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.uniform1i(I,1),n.viewport(0,0,h,l),n.clear(n.COLOR_BUFFER_BIT),n.bindBuffer(n.ARRAY_BUFFER,o),n.bufferData(n.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),n.STATIC_DRAW),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.drawArrays(n.TRIANGLE_STRIP,0,4);const _=new Uint8Array(h*l*4);return n.readPixels(0,0,h,l,n.RGBA,n.UNSIGNED_BYTE,_),n.deleteTexture(d),n.uniform1i(I,0),n.uniform1i(B,0),n.viewport(c[0],c[1],c[2],c[3]),n.uniform4fv(y,f),n.uniform4fv(F,[0,0,0,0]),_}}}({context:this._gl}),this._color=this._parseColor(this.options.color),this._buildingRasterizer=new w(this._gl),this._tileMerger=new it,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&&(st(this._compiledKernel,{date:this.options.date}),this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=R(this.options.date)-R(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&&(ut(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&&(ut(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),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._map&&this._compiledKernel){if(!1===t)this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),st(this._compiledKernel,{date:this.options.date});else{const{startDate:r,endDate:n,iterations:i=32}=e;if(this.options.sunExposure={enabled:t,startDate:r,endDate:n,iterations:i},!(r instanceof Date&&n instanceof Date))throw new Error("Start date or end date or both are missing");if(n.getTime()<r.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield at(this._compiledKernel,{startDate:r,endDate:n,iterations:i,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:n,height:i}=this._heightMap,o=e.min;return t.map((t=>{const e=p(t,r);return[(e.x-o.x)/n,(e.y-o.y)/i]}))}return[]}_getBounds(t,e){const{width:r,height:n}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(n))return t.getBounds();const i=t.getCenter(),o=p(i,e),a=E(new s(o.x-r/2,o.y-n/2),e),u=E(new s(o.x+r/2,o.y+n/2),e);return t.createBounds({nw:a,se:u})}_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=lt(this._map),e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}const r=yield rt({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return r(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(ht({kernel:this._compiledKernel,map:lt(this._map),heightMap:t,color:this._color,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield at(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.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}toGeoTiff(){const t=(t,e,r,n)=>{const i=new Uint8Array(r*n*4);this._gl.readPixels(t,e,r,n,this._gl.RGBA,this._gl.UNSIGNED_BYTE,i);const o=r*n*1,s=1*r,a=(n-1)*s,u=new Uint8Array(o),h=new Uint8Array(o);let l=0;const{startDate:c,endDate:f}=this.options.sunExposure;this.options.sunExposure.enabled&&c&&f&&(l=f.getTime()-c.getTime());for(let t=0;t<i.length;t+=4){let e;if(this.options.sunExposure.enabled){const r=i.subarray(t,t+3),n=y(r,.5,l)/1e3/60,o=Math.min(Math.floor(n/6),255);e=new Uint8Array([o])}else{e=i[t]+i[t+1]+i[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}u.set(e,t/4*1)}for(let t=0;t<o;t+=s)h.set(u.subarray(t,t+s),a-t);return h};if(this._map){const e=this._canvas.width,r=this._canvas.height,n=t(0,0,e,r),i=lt(this._map),{lat:o,lng:s}=i.getBounds().getNorthWest(),a=i.getBounds().getSouthEast(),u=[0,0,0,s,o,0],h=[(a.lng-s)/e,(o-a.lat)/r,0];return this.options.sunExposure.enabled,{data:n,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:h,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height;this._canvas.width=t.locations.length,this._canvas.height=t.dates.length;const n=this._lngLatToTextureCoords(t.locations),i=((t,e)=>{const r=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),n=ot({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:n}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:n}));return this._canvas.width=e,this._canvas.height=r,i}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height,n=R(t.startDate)-R(t.startDate,t.tzId),i=t.startDate.getTime()-n,o=(t.endDate.getTime()-n-i)/86400/1e3,s=1440;this._canvas.width=o,this._canvas.height=s;const a=[],u=828e5,h=R(new Date(i),t.tzId);for(let e=0;e<o;e++){const r=R(new Date(i+86400*e*1e3+u),t.tzId)-h;a[e]=r}const l=[];for(let t=0;t<s;t++){const e=[];for(let r=0;r<o;r++){const n=i+86400*r*1e3+60*t*1e3;e.push(new Date(n+a[r]))}l.push(e)}const c=this._lngLatToTextureCoords([t.location])[0],f=((t,e)=>{const r=ot({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),n=ot({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:n}))})(this._compiledKernel,Object.assign(Object.assign({},t),{dates:l,texCoord:c}));return this._canvas.width=e,this._canvas.height=r,f.toArray=function(){const e=new Array;for(let r=0;r<o;r++){const n=[];for(let e=s-1;e>=0;e--){const i=e*o*4+4*r,s=this.slice(i,i+4);n.push(s.join("")===t.sunColor.join("")?1:0)}e.push(n)}return e},{data:f,width:o,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:n,endDate:i}=this.options.sunExposure,o=i.getTime()-n.getTime(),s=y(r,.5,o);return Math.abs(s/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){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)}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._reset(),this}_repositionCanvas(t){this._canvasOverlay&&this._canvasOverlay.setBounds(t),this.emit("idle")}}return void 0!==window.L&&(window.L.shadeMap=(...t)=>new ct(...t)),ct}));
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leaflet-shadow-simulator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.0",
|
|
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
|
+
"browser": "dist/leaflet-shadow-simulator.umd.min.js",
|
|
7
|
+
"module": "dist/leaflet-shadow-simulator.esm.js",
|
|
6
8
|
"types": "dist/leaflet-shadow-simulator.d.ts",
|
|
7
9
|
"scripts": {
|
|
8
10
|
"test": "echo \"Error: no test specified\" && exit 1"
|