leaflet-shadow-simulator 0.49.0 → 0.54.2
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.
- package/dist/leaflet-shadow-simulator.esm.js +2 -2
- package/dist/leaflet-shadow-simulator.umd.min.js +2 -2
- package/dist/shademap/leaflet-shademap/dist/ShadeMap.d.ts +4 -3
- package/dist/shademap/leaflet-shademap/dist/ShadeMapLeaflet.d.ts +3 -0
- package/dist/shademap/leaflet-shademap/dist/ShadeMapMapbox.d.ts +3 -0
- package/dist/shademap/leaflet-shademap/dist/buildings/BuildingRasterizer.d.ts +4 -1
- package/dist/shademap/leaflet-shademap/dist/lib/TileMerger.d.ts +3 -1
- package/dist/shademap/leaflet-shademap/dist/lib/heightMap.d.ts +3 -2
- package/dist/shademap/leaflet-shademap/dist/lib/shadowMap.d.ts +1 -0
- package/dist/shademap/leaflet-shademap/dist/lib/webgl.d.ts +13 -0
- package/dist/shademap/leaflet-shademap/dist/shader/kernel.d.ts +5 -10
- package/dist/shademap/leaflet-shademap/dist/types/shadow3DData.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Copyright Ted Piotrowski 2024
|
|
3
3
|
* Package: leaflet-shadow-simulator
|
|
4
|
-
* Version: 0.
|
|
4
|
+
* Version: 0.54.2
|
|
5
5
|
* For licensing visit: https://shademap.app/about/
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import{ImageOverlay as t,DomUtil as e,Util as r}from"leaflet";class i extends t{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const t="CANVAS"===this._url.tagName,i=this._image=t?this._url:e.create("canvas");return e.addClass(i,"leaflet-image-layer"),this._zoomAnimated&&e.addClass(i,"leaflet-zoom-animated"),this.options.className&&e.addClass(i,this.options.className),i.onselectstart=r.falseFn,i.onmousemove=r.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function n(t,e,r,i){return new(r||(r=Promise))((function(n,o){function s(t){try{u(i.next(t))}catch(t){o(t)}}function a(t){try{u(i.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,a)}u((i=i.apply(t,e||[])).next())}))}function o(t,e,r){var i=e[1],n=e[0],o=i-n;return t===i&&r?t:((t-n)%o+o)%o+n}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,i=0,n=r.length;i<n;i++)this.extend(r[i])}function f(t,e){return!t||t instanceof c?t:new c(t,e)}function _(t,e){if(t)for(var r=e?[t,e]:t,i=0,n=r.length;i<n;i++)this.extend(r[i])}function d(t,e){return t instanceof _?t:new _(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,i=t.min,n=t.max,o=n.x>=e.x&&i.x<=r.x,s=n.y>=e.y&&i.y<=r.y;return o&&s},overlaps:function(t){t=f(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>e.x&&i.x<r.x,s=n.y>e.y&&i.y<r.y;return o&&s},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,i=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return f(l(e.x-i,e.y-n),l(r.x+i,r.y+n))},equals:function(t){return!!t&&(t=f(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},_.prototype={extend:function(t){var e,r,i=this._southWest,n=this._northEast;if(t instanceof g)e=t,r=t;else{if(!(t instanceof _))return t?this.extend(x(t)||d(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return i||n?(i.lat=Math.min(e.lat,i.lat),i.lng=Math.min(e.lng,i.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new g(e.lat,e.lng),this._northEast=new g(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,i=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new _(new g(e.lat-i,e.lng-n),new g(r.lat+i,r.lng+n))},getCenter:function(){return new g((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new g(this.getNorth(),this.getWest())},getSouthEast:function(){return new g(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof g||"lat"in t?x(t):d(t);var e,r,i=this._southWest,n=this._northEast;return t instanceof _?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=i.lat&&r.lat<=n.lat&&e.lng>=i.lng&&r.lng<=n.lng},intersects:function(t){t=d(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>=e.lat&&i.lat<=r.lat,s=n.lng>=e.lng&&i.lng<=r.lng;return o&&s},overlaps:function(t){t=d(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>e.lat&&i.lat<r.lat,s=n.lng>e.lng&&i.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=d(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var m=function(t){var e,r,i,n;for(r=1,i=arguments.length;r<i;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),i=this.scale(e);return this.transformation._transform(r,i)},pointToLatLng:function(t,e){var r=this.scale(e),i=this.transformation.untransform(t,r);return this.projection.unproject(i)},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 g(this.wrapLat?o(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),i=e.lat-r.lat,n=e.lng-r.lng;if(0===i&&0===n)return t;var o=t.getSouthWest(),s=t.getNorthEast();return new _(new g(o.lat-i,o.lng-n),new g(s.lat-i,s.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,i=t.lat*r,n=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(i)*Math.cos(n)*s*s,u=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return this.R*u}});function g(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function x(t,e,r){return t instanceof g?t:a(t)&&"object"!=typeof t[0]?3===t.length?new g(t[0],t[1],t[2]):2===t.length?new g(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new g(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new g(t,e,r)}g.prototype={equals:function(t,e){return!!t&&(t=x(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 m.distance(this,x(t))},wrap:function(){return m.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return d([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new g(this.lat,this.lng,this.alt)}};const p=(t,e)=>{const{lat:r,lng:i}=t;return new u(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(i,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},E=(t,e)=>{return new g((r=t.y,i=e,n=Math.PI-2*Math.PI*r/256/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,i,n},T=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,n=Math.atan2(Math.sin(i)*Math.cos(.40909994067971484),Math.cos(i));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},v=(t,e,r)=>{const i=1/e,n=Math.min(t[0]*i,255),o=Math.min(t[1]*i,255),s=Math.min(t[2]*i,255);let a=0;return n+o+s!==0&&(a=n>0?n/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"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};function R(t){const{gl:e,vSrc:r,fSrc:i}=t,n=e.createShader(e.VERTEX_SHADER);e.shaderSource(n,r),e.compileShader(n);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,i),e.compileShader(o);const s=e.createProgram();return e.attachShader(s,n),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;\nvarying float v_use_dsm;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = ((a_position.x + 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;\nvarying float v_use_dsm;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n\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:i,width:n,height:o,imageData:s,dsmCoords:a,dsmSource:h,gl:l}=t,{x:c,y:f,z:_}=r;let d=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1);const 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,n,o,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,s);const g=n,x=o;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,g,x,0,l.RGBA,l.UNSIGNED_BYTE,null),i>15&&e.length>5?(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.NEAREST),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.NEAREST)):(l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MIN_FILTER,l.LINEAR),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_MAG_FILTER,l.LINEAR)),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_S,l.CLAMP_TO_EDGE),l.texParameteri(l.TEXTURE_2D,l.TEXTURE_WRAP_T,l.CLAMP_TO_EDGE),l.viewport(0,0,g,x);const 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.disable(l.BLEND),l.bindBuffer(l.ARRAY_BUFFER,this.positionBuffer),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,this.indexBuffer),l.clearColor(0,0,0,0),l.clear(l.COLOR_BUFFER_BIT);const v=256*c,y=256*f,R=new u(v,y);l.uniform3f(this.xyzUniformLocation,v,y,_),l.uniform2f(this.dimensionsUniformLocation,n,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:i,centroid:a,highlight:u}=t,h=p({lng:a[0],lat:a[1]},_).subtract(R).floor();if(h.x<0||h.y<0||h.x>n||h.y>o)return;const c=256*s[h.y*n*2+2*h.x]+s[h.y*n*2+2*h.x+1]+5*e,f=Math.floor(c/256)/255,m=Math.floor(c%256)/255;d=Math.max(d,c/5),l.uniform4f(this.colorUniformLocation,f,m,f,m),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ELEMENT_ARRAY_BUFFER,i,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,i.length,i.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),l.deleteTexture(m),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.colorMask(!1,!1,!0,!0),l.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.colorMask(!0,!0,!0,!0),l.enable(l.BLEND),l.blendFunc(l.SRC_ALPHA,l.ONE_MINUS_SRC_ALPHA),l.deleteFramebuffer(E),d}}function b(t,e,r){r=r||2;var i,n,o,s,a,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=w(t,0,c,r,!0),_=[];if(!f||f.next===f.prev)return _;if(l&&(f=function(t,e,r,i){var n,o,s,a=[];for(n=0,o=e.length;n<o;n++)(s=w(t,e[n]*i,n<o-1?e[n+1]*i:t.length,i,!1))===s.next&&(s.steiner=!0),a.push(N(s));for(a.sort(C),n=0;n<a.length;n++)r=F(a[n],r);return r}(t,e,f,r)),t.length>80*r){i=o=t[0],n=s=t[1];for(var d=r;d<c;d+=r)(a=t[d])<i&&(i=a),(u=t[d+1])<n&&(n=u),a>o&&(o=a),u>s&&(s=u);h=0!==(h=Math.max(o-i,s-n))?32767/h:0}return U(f,_,r,i,n,h,0),_}function w(t,e,r,i,n){var o,s;if(n===V(t,e,r,i)>0)for(o=e;o<r;o+=i)s=K(o,t[o],t[o+1],s);else for(o=r-i;o>=e;o-=i)s=K(o,t[o],t[o+1],s);return s&&G(s,s.next)&&(q(s),s=s.next),s}function M(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!G(i,i.next)&&0!==z(i.prev,i,i.next))i=i.next;else{if(q(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function U(t,e,r,i,n,o,s){if(t){!s&&o&&function(t,e,r,i){var n=t;do{0===n.z&&(n.z=B(n.x,n.y,e,r,i)),n.prevZ=n.prev,n.nextZ=n.next,n=n.next}while(n!==t);n.prevZ.nextZ=null,n.prevZ=null,function(t){var e,r,i,n,o,s,a,u,h=1;do{for(r=t,t=null,o=null,s=0;r;){for(s++,i=r,a=0,e=0;e<h&&(a++,i=i.nextZ);e++);for(u=h;a>0||u>0&&i;)0!==a&&(0===u||!i||r.z<=i.z)?(n=r,r=r.nextZ,a--):(n=i,i=i.nextZ,u--),o?o.nextZ=n:t=n,n.prevZ=o,o=n;r=i}o.nextZ=null,h*=2}while(s>1)}(n)}(t,i,n,o);for(var a,u,h=t;t.prev!==t.next;)if(a=t.prev,u=t.next,o?L(t,i,n,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(M(t),e,r),e,r,i,n,o,2):2===s&&S(t,e,r,i,n,o):U(M(t),e,r,i,n,o,1);break}}}function D(t){var e=t.prev,r=t,i=t.next;if(z(e,r,i)>=0)return!1;for(var n=e.x,o=r.x,s=i.x,a=e.y,u=r.y,h=i.y,l=n<o?n<s?n:s:o<s?o:s,c=a<u?a<h?a:h:u<h?u:h,f=n>o?n>s?n:s:o>s?o:s,_=a>u?a>h?a:h:u>h?u:h,d=i.next;d!==e;){if(d.x>=l&&d.x<=f&&d.y>=c&&d.y<=_&&X(n,a,o,u,s,h,d.x,d.y)&&z(d.prev,d,d.next)>=0)return!1;d=d.next}return!0}function L(t,e,r,i){var n=t.prev,o=t,s=t.next;if(z(n,o,s)>=0)return!1;for(var a=n.x,u=o.x,h=s.x,l=n.y,c=o.y,f=s.y,_=a<u?a<h?a:h:u<h?u:h,d=l<c?l<f?l:f:c<f?c:f,m=a>u?a>h?a:h:u>h?u:h,g=l>c?l>f?l:f:c>f?c:f,x=B(_,d,e,r,i),p=B(m,g,e,r,i),E=t.prevZ,T=t.nextZ;E&&E.z>=x&&T&&T.z<=p;){if(E.x>=_&&E.x<=m&&E.y>=d&&E.y<=g&&E!==n&&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>=_&&T.x<=m&&T.y>=d&&T.y<=g&&T!==n&&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>=x;){if(E.x>=_&&E.x<=m&&E.y>=d&&E.y<=g&&E!==n&&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>=_&&T.x<=m&&T.y>=d&&T.y<=g&&T!==n&&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 i=t;do{var n=i.prev,o=i.next.next;!G(n,o)&&H(n,i,i.next,o)&&Z(n,o)&&Z(o,n)&&(e.push(n.i/r|0),e.push(i.i/r|0),e.push(o.i/r|0),q(i),q(i.next),i=t=o),i=i.next}while(i!==t);return M(i)}function S(t,e,r,i,n,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=M(s,s.next),u=M(u,u.next),U(s,e,r,i,n,o,0),void U(u,e,r,i,n,o,0)}a=a.next}s=s.next}while(s!==t)}function C(t,e){return t.x-e.x}function F(t,e){var r=function(t,e){var r,i=e,n=t.x,o=t.y,s=-1/0;do{if(o<=i.y&&o>=i.next.y&&i.next.y!==i.y){var a=i.x+(o-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(a<=n&&a>s&&(s=a,r=i.x<i.next.x?i:i.next,a===n))return r}i=i.next}while(i!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;i=r;do{n>=i.x&&i.x>=l&&n!==i.x&&X(o<c?n:s,o,l,c,o<c?s:n,o,i.x,i.y)&&(u=Math.abs(o-i.y)/(n-i.x),Z(i,t)&&(u<f||u===f&&(i.x>r.x||i.x===r.x&&I(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=Y(r,t);return M(i,i.next),M(r,r.next)}function I(t,e){return z(t.prev,t,e.prev)<0&&z(e.next,t,t.next)<0}function B(t,e,r,i,n){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*n|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*n|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,i,n,o,s,a){return(n-s)*(e-a)>=(t-s)*(o-a)&&(t-s)*(i-a)>=(r-s)*(e-a)&&(r-s)*(o-a)>=(n-s)*(i-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&&H(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,i=!1,n=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&n<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(z(t.prev,t,e.prev)||z(t,e.prev,e))||G(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 G(t,e){return t.x===e.x&&t.y===e.y}function H(t,e,r,i){var n=j(z(t,e,r)),o=j(z(t,e,i)),s=j(z(r,i,t)),a=j(z(r,i,e));return n!==o&&s!==a||(!(0!==n||!W(t,r,e))||(!(0!==o||!W(t,i,e))||(!(0!==s||!W(r,t,i))||!(0!==a||!W(r,e,i)))))}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),i=new k(e.i,e.x,e.y),n=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=n,n.prev=r,i.next=r,r.prev=i,o.next=i,i.prev=o,i}function K(t,e,r,i){var n=new k(t,e,r);return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}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,i){for(var n=0,o=e,s=r-i;o<r;o+=i)n+=(t[s]-t[o])*(t[o+1]+t[s+1]),s=o;return n}function $(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:n}=e;if("MultiPolygon"===r.type)for(let i=0;i<r.coordinates.length;i++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[i]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:i}=b.flatten(e.coordinates),n=new Float32Array(i.map(((t,e)=>{if(e%2==1){const e=t*Math.PI/180;return Math.tan(e)+1/Math.cos(e)}return t}))),o=b(i),s=o.length>256?new Uint16Array(o):new Uint8Array(o),a=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),u=r.highlight||!1;let h=0,l=0,c=0;for(let t=0;t<i.length;t+=2)h+=i[t],l+=i[t+1],c++;return{aPosition:n,cuts:s,buildingHeight:a,centroid:[h/c,l/c],highlight:u}}))}b.deviation=function(t,e,r,i){var n=e&&e.length,o=n?e[0]*r:t.length,s=Math.abs(V(t,0,o,r));if(n)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<i.length;a+=3){var f=i[a]*r,_=i[a+1]*r,d=i[a+2]*r;c+=Math.abs((t[f]-t[d])*(t[_+1]-t[f+1])-(t[f]-t[_])*(t[d+1]-t[f+1]))}return 0===s&&0===c?0:Math.abs((c-s)/s)},b.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},i=0,n=0;n<t.length;n++){for(var o=0;o<t[n].length;o++)for(var s=0;s<e;s++)r.vertices.push(t[n][o][s]);n>0&&(i+=t[n-1].length,r.holes.push(i))}return r};let J,Q={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,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const tt=t=>n(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:i,dsmSource:o,tileLoaded:s,gl:a,bounds:h,buildingRasterizer:l,tileMerger:f,forceUpdate:d=!1}=t,{getSourceUrl:m,getElevation:g,maxZoom:x,tileSize:E,_overzoom:T}=i,v=(y={getFeatures:r},n(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(),i=t.getSouthEast(),n=new c(p(r,e),p(i,e));let T=new u(n.min.x,n.min.y),y=n.max.x-n.min.x;const R=n.max.y-n.min.y,A=n.max.subtract(n.min);A.y>A.x&&(T.x-=256,y+=512);const b=(t=>{const{upperLeft:e,width:r,height:i}=t,n=e.divideBy(256).floor().multiplyBy(256),o=256*(Math.ceil(r/256)+1),s=256*(Math.ceil(i/256)+1),a=n.add([o,s]);return new c(n,a)})({upperLeft:T,width:y,height:R}),{x:w,y:M}=b.max.subtract(b.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 i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:n}=t,o=e.divideBy(256).floor(),s=o.x+r/256,a=Math.min(o.y+i/256,Math.pow(2,n)-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:n});return u})({upperLeft:b.min,width:w,height:M,zoom:e})),D=n.max.x-n.min.x,L=n.max.y-n.min.y,P=D,S=L,C=JSON.stringify(U);if(!d&&C===J&&e<x)return Q=Object.assign(Object.assign({},Q),{outputWidth:P,outputHeight:S,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),Q;const F=yield f.merge(U,{maxZoom:x,width:w,height:M,crossOrigin:"Anonymous",getSourceUrl:m,getElevation:g,tileSize:E,tileLoaded:s});if(null===F)return Q=Object.assign(Object.assign({},Q),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),Q;const I=new c(p(new _(o.bounds).getNorthWest(),e),p(new _(o.bounds).getSouthEast(),e)),B=I.min.subtract(b.min),N=I.max.subtract(b.min),X=[B.x/w,B.y/M,N.x/w,N.y/M].map((t=>2*t-1)),O=yield v,z=l.raster({upperLeftTile:U[0],width:w,height:M,mapZoom:e,features:O,imageData:F,gl:a,dsmSource:o,dsmCoords:X}),G=Math.max(Math.max(o.maxHeight,z),F.maxHeight);J=C,Q={maxHeight:G,width:w,height:M,imageData:F,DEMPixelBounds:b,visibleDEMPixelBounds:n,raster:U,demZoom:e,dirty:!0,outputWidth:P,outputHeight:S}}catch(t){console.error("Could not decode height map",t)}return Q})),et=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:n,y:o,z:s}=e;if(s>r){const t=Math.pow(2,e.z-r);n=Math.floor(n/t),o=Math.floor(o/t),s=r}return{x:256===i?n:Math.floor(n/2),y:256===i?o:Math.floor(o/2),z:256===i?s:s-1}};class rt{constructor(t){this.gl=t,this.program=R({vSrc:"\n attribute vec4 a_position;\n varying vec2 v_pos;\n void main() {\n v_pos = ((a_position + 1.0) * 0.5).xy;\n gl_Position = a_position;\n }\n",fSrc:"\n precision mediump float;\n varying vec2 v_pos;\n uniform sampler2D u_texture;\n void main() {\n gl_FragColor = texture2D(u_texture, v_pos);\n }\n",gl:t}),this.positionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.positionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),t.enableVertexAttribArray(this.positionAttributeLocation),t.vertexAttribPointer(this.positionAttributeLocation,2,t.FLOAT,!1,0,0),this.texture=t.createTexture(),this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.NEAREST),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.NEAREST),this.inProgress=[],this.finished=0}merge(t,e){return n(this,void 0,void 0,(function*(){const{width:r,height:i,crossOrigin:o,getSourceUrl:s,getElevation:a,tileSize:u,tileLoaded:h,maxZoom:l}=e;if(this.gl){const e=new Uint8ClampedArray(2*r*i);e.maxHeight=0,this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const c=new Set;t.forEach((t=>{c.add(s(et({tile:t,maxZoom:l,tileSize:u})))}));const f=Array.from(c).map((i=>n(this,void 0,void 0,(function*(){return new Promise(((n,h)=>{const c=new Image;this.inProgress.push(c),c.onload=()=>{t.filter((t=>i===s(et({tile:t,maxZoom:l,tileSize:u})))).forEach((t=>{let i=0,n=0;const o=514===u?1:0;let s=0,h=256,f=256;if(256!==u&&(i=t.x%2*256,n=t.y%2*256,h=256,f=256),t.z>l){const e=Math.pow(2,t.z-l);256!==u&&(i=Math.floor(t.x/e)%2*256,n=Math.floor(t.y/e)%2*256),i+=t.x%e/e*256,n+=t.y%e/e*256,h=256/e,f=256/e,s=514===u?1:0}this.gl.useProgram(this.program),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,c);const _=this.gl.getParameter(this.gl.VIEWPORT),d=this.gl.canvas.width,m=this.gl.canvas.height;this.gl.canvas.width=u,this.gl.canvas.height=u,this.gl.viewport(0,0,u,u),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.positionBuffer),this.gl.enableVertexAttribArray(this.positionAttributeLocation),this.gl.vertexAttribPointer(this.positionAttributeLocation,2,this.gl.FLOAT,!1,0,0),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);const g=new Uint8Array(4*(h+s)*(f+s));this.gl.readPixels(o+i,o+n,h+s,f+s,this.gl.RGBA,this.gl.UNSIGNED_BYTE,g),this.gl.canvas.width=d,this.gl.canvas.height=m,this.gl.viewport(_[0],_[1],_[2],_[3]);const x=new Uint8ClampedArray(131072);let p=new Array((h+s)*(f+s)),E=256/h;for(let t=0;t<g.length;t+=4){const r=a({r:g[t],g:g[t+1],b:g[t+2],a:g[t+3]});e.maxHeight=Math.max(r,e.maxHeight);const i=5*(r||0);p[t/4]=i}if(E>1&&514===u)for(p=function(t,e,r,i){const n=Math.floor(e*i),o=Math.floor(r*i),s=[];for(let a=0;a<o;a++)for(let o=0;o<n;o++){const n=o/i,u=a/i,h=Math.floor(n),l=Math.floor(u),c=Math.min(h+1,e-1),f=Math.min(l+1,r-1),_=n-h,d=u-l,m=l*e+c,g=f*e+h,x=f*e+c,p=t[l*e+h]*(1-_)*(1-d)+t[m]*_*(1-d)+t[g]*(1-_)*d+t[x]*_*d;s.push(p)}return function(t,e,r,i,n){if(i>=e||n>=r)return[];const o=e-i,s=r-n,a=[];for(let r=0;r<s;r++)for(let i=0;i<o;i++){const n=r*e+i;a.push(t[n])}return a}(s,Math.sqrt(s.length),Math.sqrt(s.length),i,i)}(p,h+1,f+1,E);E>1;)E/=2,f*=2,h*=2;for(let t=0;t<x.length;t+=2){const e=p[Math.floor(t/2/256/E)*h+Math.floor(t/2%256/E)],r=Math.floor(e/256),i=Math.floor(e%256);x[t]=r,x[t+1]=i}for(let i=0;i<256;i++)e.set(x.slice(256*i*2,256*(i+1)*2),2*(t.yOffset*r+i*r+t.xOffset))})),n(null)},c.onerror=t=>{if(c.src!==c.originalSource)return h("new tiles requested");n(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 it=(t,e)=>{const{r:r,g:i,b:n}=t;return[r/255,i/255,n/255,e]},nt=(t,e)=>{const{date:r}=e,{dec:i,Hi:n}=T(r);t.updateDate({dec:i,Hi:n})},ot=(t,e)=>n(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),st=(t,e)=>{const{color:r,opacity:i}=e,n=it(r,i);t.updateColor({colorVec:n})};new _([0,0],[0,0]);const at=t=>{const{kernel:e,map:r,heightMap:i,now:n,color:o,opacity:s,belowCanopy:a}=t;try{const{outputHeight:t,outputWidth:u,maxHeight:h,width:l,height:c,DEMPixelBounds:f,imageData:d,visibleDEMPixelBounds:m,demZoom:g}=i;if(0===l||0===c)return;const{min:x,max:p}=m;if(!x||!p)return;const{x:v,y:y}=p.subtract(x),R=f.min,A=r.getPixelDimensions(),b=r.screenUnproject([0,0]),w=r.screenUnproject([A.width,0]),M=r.screenUnproject([A.width,A.height]),U=r.screenUnproject([0,A.height]),D=[U,M,b,w].map((t=>r.project(t,g))),L=D.map((t=>[(t.x-R.x)/l,(t.y-R.y)/c])).flat(),P=D.map((t=>[(t.x-x.x)/v,(t.y-x.y)/y])).map((t=>[2*t[0]-1,-1*(2*t[1]-1)])).flat(),S=R.y/256/Math.pow(2,g),C=c/256/Math.pow(2,g),F=new _(E(f.getTopLeft(),g),E(f.getBottomRight(),g)),I=F.getWest(),B=Math.abs(F.getWest()-F.getEast());((t,e)=>{const{color:r,opacity:i,date:n}=e,o=it(r,i),{dec:s,Hi:a}=T(n);t.updateLocation(Object.assign({dec:s,Hi:a,colorVec:o,step:1},e))})(e,{imageData:d,maxHeight:h,width:l,height:c,heightMapZoom:g,cornerTextureCoords:L,cornerClipCoords:P,topYCoord:S,ySize:C,west:I,dLng:B,date:n,color:o,opacity:s,outputHeight:t,outputWidth:u,belowCanopy:a})}catch(t){console.error("EXCEPTION",t)}};const ut=t=>{const e=()=>!t.getPitch;return{project:(r,i)=>{if(e())return t.project(r,i);{const{lat:t,lng:e}=r;return new u(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,i),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,i))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new g((n=r.y,o=i,s=Math.PI-2*Math.PI*n/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,i));var n,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 _(e,r)}}};class ht extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>n(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>n(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)}))));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,i=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 getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_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_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_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(i);const o=r.createBuffer(),s=r.getAttribLocation(i,"a_pos"),a=r.createBuffer(),u=r.getAttribLocation(i,"a_tex_pos"),h=r.createBuffer();r.bindBuffer(r.ARRAY_BUFFER,h),r.bufferData(r.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),r.STATIC_DRAW),r.enable(r.BLEND),r.blendFunc(r.SRC_ALPHA,r.ONE_MINUS_SRC_ALPHA);const l=r.getUniformLocation(i,"user_a");r.uniform1i(l,0);const c=r.getUniformLocation(i,"user_width"),f=r.getUniformLocation(i,"user_height"),_=r.getUniformLocation(i,"user_maxHeight"),d=r.getUniformLocation(i,"user_zoom"),m=r.getUniformLocation(i,"user_topYCoord"),g=r.getUniformLocation(i,"user_ySize"),x=r.getUniformLocation(i,"user_step"),p=r.getUniformLocation(i,"user_west"),E=r.getUniformLocation(i,"user_dLng"),v=r.getUniformLocation(i,"user_dec"),y=r.getUniformLocation(i,"user_Hi"),A=r.getUniformLocation(i,"user_color"),b=r.getUniformLocation(i,"user_xStart"),w=r.getUniformLocation(i,"user_yStart"),M=r.getUniformLocation(i,"user_xEnd"),U=r.getUniformLocation(i,"user_yEnd"),D=r.getUniformLocation(i,"u_below_canopy"),L=r.getUniformLocation(i,"user_sunExposureTexture");r.uniform1i(L,2);const P=r.getUniformLocation(i,"user_renderToSunExposureTexture"),S=r.getUniformLocation(i,"user_outputSunExposure"),C=r.getUniformLocation(i,"user_exposureDelta"),F=r.getUniformLocation(i,"u_outputShadeProfile"),I=r.getUniformLocation(i,"u_gpxTexture"),B=r.getUniformLocation(i,"u_decHiTexture"),N=r.getUniformLocation(i,"u_sunColor"),X=r.getUniformLocation(i,"u_outputLocationShadeProfile"),O=r.getUniformLocation(i,"u_shadeProfileLocation"),z=()=>{r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4)};let G=0;return{updateLocation:function(t){const{width:e,height:n,maxHeight:h,heightMapZoom:l,topYCoord:T,ySize:R,colorVec:b,step:w,west:M,dLng:U,dec:L,Hi:P,cornerClipCoords:S,cornerTextureCoords:C,outputWidth:F,outputHeight:I,belowCanopy:B}=t;r.useProgram(i),r.canvas.width=F,r.canvas.height=I,r.viewport(0,0,F,I),r.bindFramebuffer(r.FRAMEBUFFER,null),r.bindBuffer(r.ARRAY_BUFFER,o),r.bufferData(r.ARRAY_BUFFER,new Float32Array(S),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(C),r.STATIC_DRAW),r.enableVertexAttribArray(u),r.vertexAttribPointer(u,2,r.FLOAT,!1,0,0),r.uniform1f(c,e),r.uniform1f(f,n),r.uniform1f(_,h),r.uniform1f(d,l),r.uniform1f(m,T),r.uniform1f(g,R),r.uniform4fv(A,b),r.uniform1f(x,w),r.uniform1f(p,M),r.uniform1f(E,U),r.uniform1f(v,L),r.uniform1f(y,P),r.uniform1f(D,B?1:0),window.requestAnimationFrame(z)},updateViewport:function(t){const{xStart:e,yStart:i,xEnd:n,yEnd:o}=t;r.uniform1f(b,e),r.uniform1f(w,i),r.uniform1f(M,n),r.uniform1f(U,o),window.requestAnimationFrame(z)},updateDate:t=>{const{dec:e,Hi:i}=t;r.uniform1f(v,e),r.uniform1f(y,i),window.requestAnimationFrame(z)},updateDateRange:t=>n(this,void 0,void 0,(function*(){const{startDate:e,endDate:i,iterations:n,emit:o}=t,s=G=Date.now();r.uniform4fv(C,new Float32Array([1/n,0,0,0]));const a=Math.floor((i.getTime()-e.getTime())/n),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(P,1),r.viewport(0,0,u,h);const f=r.createFramebuffer();r.bindFramebuffer(r.FRAMEBUFFER,f);const _=r.COLOR_ATTACHMENT0;r.framebufferTexture2D(r.FRAMEBUFFER,_,r.TEXTURE_2D,l,0);for(let t=0;t<n;t++){if(o("tileloaded",t,n-1),s!==G)return r.deleteFramebuffer(f),r.deleteTexture(l),r.deleteTexture(c),!0;yield new Promise(((i,n)=>{window.requestAnimationFrame((()=>{t%2==0?(r.uniform1i(L,2),r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,l),r.framebufferTexture2D(r.FRAMEBUFFER,_,r.TEXTURE_2D,l,0)):(r.uniform1i(L,1),r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,c),r.framebufferTexture2D(r.FRAMEBUFFER,_,r.TEXTURE_2D,c,0));const{dec:n,Hi:o}=T(new Date(e.getTime()+a*t));r.uniform1f(v,n),r.uniform1f(y,o),r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4),i()}))}))}return yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{s!==G&&(r.deleteFramebuffer(f),r.deleteTexture(l),r.deleteTexture(c),t(!0)),r.bindFramebuffer(r.FRAMEBUFFER,null),r.uniform1i(L,n%2==0?2:1),r.uniform1i(S,1),z(),r.uniform1i(P,0),r.uniform1i(S,0),r.deleteFramebuffer(f),r.deleteTexture(l),r.deleteTexture(c),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;r.uniform4fv(A,e),window.requestAnimationFrame(z)},generateShadeProfile:t=>{if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoords:n,shadeColor:a,sunColor:u}=t,l=n.length,c=e.length,f=r.getParameter(r.VIEWPORT),_=r.getUniform(i,A);r.uniform4fv(A,a),r.uniform4fv(N,u),r.uniform1i(F,1);const d=r.createTexture();r.activeTexture(r.TEXTURE2),r.bindTexture(r.TEXTURE_2D,d);const m=n.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(I,2);const g=r.createTexture();r.activeTexture(r.TEXTURE1),r.bindTexture(r.TEXTURE_2D,g);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(B,1),r.viewport(0,0,l,c),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,h),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4),r.bindBuffer(r.ARRAY_BUFFER,o),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0);const p=new Uint8Array(l*c*4);return r.readPixels(0,0,l,c,r.RGBA,r.UNSIGNED_BYTE,p),r.deleteTexture(g),r.deleteTexture(d),r.uniform1i(B,0),r.uniform1i(I,0),r.uniform1i(F,0),r.viewport(f[0],f[1],f[2],f[3]),r.uniform4fv(A,_),r.uniform4fv(N,[0,0,0,0]),p},generateLocationShadeProfile:t=>{if(null===r.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoord:n,shadeColor:a,sunColor:u}=t,l=e[0].length,c=e.length,f=r.getParameter(r.VIEWPORT),_=r.getUniform(i,A);r.uniform4fv(A,a),r.uniform4fv(N,u),r.uniform1i(X,1),r.uniform2fv(O,[n[0],n[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,l,c,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(B,1),r.viewport(0,0,l,c),r.clear(r.COLOR_BUFFER_BIT),r.bindBuffer(r.ARRAY_BUFFER,h),r.enableVertexAttribArray(s),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0),r.drawArrays(r.TRIANGLE_STRIP,0,4),r.bindBuffer(r.ARRAY_BUFFER,o),r.vertexAttribPointer(s,2,r.FLOAT,!1,0,0);const g=new Uint8Array(l*c*4);return r.readPixels(0,0,l,c,r.RGBA,r.UNSIGNED_BYTE,g),r.deleteTexture(d),r.uniform1i(B,0),r.uniform1i(X,0),r.viewport(f[0],f[1],f[2],f[3]),r.uniform4fv(A,_),r.uniform4fv(N,[0,0,0,0]),g}}}({context:this._gl}),this._color=this._parseColor(this.options.color),this._buildingRasterizer=new A(this._gl),this._tileMerger=new rt(this._gl),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(nt(this._compiledKernel,{date:this.options.date}),this._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&&(st(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&&(st(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return n(this,void 0,void 0,(function*(){if(this._map&&this._compiledKernel){if(!1===t)this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),nt(this._compiledKernel,{date:this.options.date});else{const{startDate:r,endDate:i,iterations:n=32}=e;if(this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:n},!(r instanceof Date&&i instanceof Date))throw new Error("Start date or end date or both are missing");if(i.getTime()<r.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield ot(this._compiledKernel,{startDate:r,endDate:i,iterations:n,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:i,height:n}=this._heightMap,o=e.min;return t.map((t=>{const e=p(t,r);return[(e.x-o.x)/i,(e.y-o.y)/n]}))}return[]}_getBounds(t,e){const{width:r,height:i}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(i))return t.getBounds();const n=t.getCenter(),o=p(n,e),s=E(new u(o.x-r/2,o.y-i/2),e),a=E(new u(o.x+r/2,o.y+i/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 n(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=ut(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),i=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===i)).map((t=>t.canonical)),[o,s,a,h]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),l=E(new u(512*o,512*s),i+1),c=E(new u(512*(a+1),512*(h+1)),i+1);e=i+1,this._bounds=t.createBounds({nw:l,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield tt({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return n(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(at({kernel:this._compiledKernel,map:ut(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield ot(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}toGeoTiff(){const t=(t,e,r,i)=>{const n=new Uint8Array(r*i*4);this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n);const o=r*i*1,s=1*r,a=(i-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<n.length;t+=4){let e;if(this.options.sunExposure.enabled){const r=n.subarray(t,t+3),i=v(r,.5,l)/1e3/60,o=Math.min(Math.floor(i/6),255);e=new Uint8Array([o])}else{e=n[t]+n[t+1]+n[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,i=t(0,0,e,r),n=ut(this._map),{lat:o,lng:s}=n.getBounds().getNorthWest(),a=n.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:i,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 i=this._lngLatToTextureCoords(t.locations),n=((t,e)=>{const r=it({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=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:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:i}));return this._canvas.width=e,this._canvas.height=r,n}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height,i=y(t.startDate)-y(t.startDate,t.tzId),n=t.startDate.getTime()-i,o=(t.endDate.getTime()-i-n)/86400/1e3,s=1440;this._canvas.width=o,this._canvas.height=s;const a=[],u=828e5,h=y(new Date(n),t.tzId);for(let e=0;e<o;e++){const r=y(new Date(n+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 i=n+86400*r*1e3+60*t*1e3;e.push(new Date(i+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),i=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:i}))})(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 i=[];for(let e=s-1;e>=0;e--){const n=e*o*4+4*r,s=this.slice(n,n+4);i.push(s.join("")===t.sunColor.join("")?1:0)}e.push(i)}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:i,endDate:n}=this.options.sunExposure,o=n.getTime()-i.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 i(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">© ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._flush()}_flush(){this.emit("idle")}}void 0!==window.L&&(window.L.shadeMap=(...t)=>new ht(...t));export{ht as default};
|
|
8
|
+
import{ImageOverlay as t,DomUtil as e,Util as r}from"leaflet";class i extends t{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const t="CANVAS"===this._url.tagName,i=this._image=t?this._url:e.create("canvas");return e.addClass(i,"leaflet-image-layer"),this._zoomAnimated&&e.addClass(i,"leaflet-zoom-animated"),this.options.className&&e.addClass(i,this.options.className),i.onselectstart=r.falseFn,i.onmousemove=r.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function n(t,e,r,i){return new(r||(r=Promise))((function(n,o){function a(t){try{u(i.next(t))}catch(t){o(t)}}function s(t){try{u(i.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((i=i.apply(t,e||[])).next())}))}function o(t,e,r){r=r||2;var i,n,o,s,h,l,c,f=e&&e.length,d=f?e[0]*r:t.length,x=a(t,0,d,r,!0),p=[];if(!x||x.next===x.prev)return p;if(f&&(x=function(t,e,r,i){var n,o,s,u=[];for(n=0,o=e.length;n<o;n++)(s=a(t,e[n]*i,n<o-1?e[n+1]*i:t.length,i,!1))===s.next&&(s.steiner=!0),u.push(g(s));for(u.sort(m),n=0;n<u.length;n++)r=_(u[n],r);return r}(t,e,x,r)),t.length>80*r){i=o=t[0],n=s=t[1];for(var E=r;E<d;E+=r)(h=t[E])<i&&(i=h),(l=t[E+1])<n&&(n=l),h>o&&(o=h),l>s&&(s=l);c=0!==(c=Math.max(o-i,s-n))?32767/c:0}return u(x,p,r,i,n,c,0),p}function a(t,e,r,i,n){var o,a;if(n===D(t,e,r,i)>0)for(o=e;o<r;o+=i)a=w(o,t[o],t[o+1],a);else for(o=r-i;o>=e;o-=i)a=w(o,t[o],t[o+1],a);return a&&v(a,a.next)&&(U(a),a=a.next),a}function s(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!v(i,i.next)&&0!==T(i.prev,i,i.next))i=i.next;else{if(U(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function u(t,e,r,i,n,o,a){if(t){!a&&o&&function(t,e,r,i){var n=t;do{0===n.z&&(n.z=x(n.x,n.y,e,r,i)),n.prevZ=n.prev,n.nextZ=n.next,n=n.next}while(n!==t);n.prevZ.nextZ=null,n.prevZ=null,function(t){var e,r,i,n,o,a,s,u,h=1;do{for(r=t,t=null,o=null,a=0;r;){for(a++,i=r,s=0,e=0;e<h&&(s++,i=i.nextZ);e++);for(u=h;s>0||u>0&&i;)0!==s&&(0===u||!i||r.z<=i.z)?(n=r,r=r.nextZ,s--):(n=i,i=i.nextZ,u--),o?o.nextZ=n:t=n,n.prevZ=o,o=n;r=i}o.nextZ=null,h*=2}while(a>1)}(n)}(t,i,n,o);for(var m,_,d=t;t.prev!==t.next;)if(m=t.prev,_=t.next,o?l(t,i,n,o):h(t))e.push(m.i/r|0),e.push(t.i/r|0),e.push(_.i/r|0),U(t),t=_.next,d=_.next;else if((t=_)===d){a?1===a?u(t=c(s(t),e,r),e,r,i,n,o,2):2===a&&f(t,e,r,i,n,o):u(s(t),e,r,i,n,o,1);break}}}function h(t){var e=t.prev,r=t,i=t.next;if(T(e,r,i)>=0)return!1;for(var n=e.x,o=r.x,a=i.x,s=e.y,u=r.y,h=i.y,l=n<o?n<a?n:a:o<a?o:a,c=s<u?s<h?s:h:u<h?u:h,f=n>o?n>a?n:a:o>a?o:a,m=s>u?s>h?s:h:u>h?u:h,_=i.next;_!==e;){if(_.x>=l&&_.x<=f&&_.y>=c&&_.y<=m&&p(n,s,o,u,a,h,_.x,_.y)&&T(_.prev,_,_.next)>=0)return!1;_=_.next}return!0}function l(t,e,r,i){var n=t.prev,o=t,a=t.next;if(T(n,o,a)>=0)return!1;for(var s=n.x,u=o.x,h=a.x,l=n.y,c=o.y,f=a.y,m=s<u?s<h?s:h:u<h?u:h,_=l<c?l<f?l:f:c<f?c:f,d=s>u?s>h?s:h:u>h?u:h,g=l>c?l>f?l:f:c>f?c:f,E=x(m,_,e,r,i),v=x(d,g,e,r,i),y=t.prevZ,R=t.nextZ;y&&y.z>=E&&R&&R.z<=v;){if(y.x>=m&&y.x<=d&&y.y>=_&&y.y<=g&&y!==n&&y!==a&&p(s,l,u,c,h,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,R.x>=m&&R.x<=d&&R.y>=_&&R.y<=g&&R!==n&&R!==a&&p(s,l,u,c,h,f,R.x,R.y)&&T(R.prev,R,R.next)>=0)return!1;R=R.nextZ}for(;y&&y.z>=E;){if(y.x>=m&&y.x<=d&&y.y>=_&&y.y<=g&&y!==n&&y!==a&&p(s,l,u,c,h,f,y.x,y.y)&&T(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;R&&R.z<=v;){if(R.x>=m&&R.x<=d&&R.y>=_&&R.y<=g&&R!==n&&R!==a&&p(s,l,u,c,h,f,R.x,R.y)&&T(R.prev,R,R.next)>=0)return!1;R=R.nextZ}return!0}function c(t,e,r){var i=t;do{var n=i.prev,o=i.next.next;!v(n,o)&&y(n,i,i.next,o)&&b(n,o)&&b(o,n)&&(e.push(n.i/r|0),e.push(i.i/r|0),e.push(o.i/r|0),U(i),U(i.next),i=t=o),i=i.next}while(i!==t);return s(i)}function f(t,e,r,i,n,o){var a=t;do{for(var h=a.next.next;h!==a.prev;){if(a.i!==h.i&&E(a,h)){var l=M(a,h);return a=s(a,a.next),l=s(l,l.next),u(a,e,r,i,n,o,0),void u(l,e,r,i,n,o,0)}h=h.next}a=a.next}while(a!==t)}function m(t,e){return t.x-e.x}function _(t,e){var r=function(t,e){var r,i=e,n=t.x,o=t.y,a=-1/0;do{if(o<=i.y&&o>=i.next.y&&i.next.y!==i.y){var s=i.x+(o-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(s<=n&&s>a&&(a=s,r=i.x<i.next.x?i:i.next,s===n))return r}i=i.next}while(i!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;i=r;do{n>=i.x&&i.x>=l&&n!==i.x&&p(o<c?n:a,o,l,c,o<c?a:n,o,i.x,i.y)&&(u=Math.abs(o-i.y)/(n-i.x),b(i,t)&&(u<f||u===f&&(i.x>r.x||i.x===r.x&&d(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=M(r,t);return s(i,i.next),s(r,r.next)}function d(t,e){return T(t.prev,t,e.prev)<0&&T(e.next,t,t.next)<0}function x(t,e,r,i,n){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*n|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*n|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function g(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function p(t,e,r,i,n,o,a,s){return(n-a)*(e-s)>=(t-a)*(o-s)&&(t-a)*(i-s)>=(r-a)*(e-s)&&(r-a)*(o-s)>=(n-a)*(i-s)}function E(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&y(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(b(t,e)&&b(e,t)&&function(t,e){var r=t,i=!1,n=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&n<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(T(t.prev,t,e.prev)||T(t,e.prev,e))||v(t,e)&&T(t.prev,t,t.next)>0&&T(e.prev,e,e.next)>0)}function T(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function v(t,e){return t.x===e.x&&t.y===e.y}function y(t,e,r,i){var n=A(T(t,e,r)),o=A(T(t,e,i)),a=A(T(r,i,t)),s=A(T(r,i,e));return n!==o&&a!==s||(!(0!==n||!R(t,r,e))||(!(0!==o||!R(t,i,e))||(!(0!==a||!R(r,t,i))||!(0!==s||!R(r,e,i)))))}function R(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function A(t){return t>0?1:t<0?-1:0}function b(t,e){return T(t.prev,t,t.next)<0?T(t,e,t.next)>=0&&T(t,t.prev,e)>=0:T(t,e,t.prev)<0||T(t,t.next,e)<0}function M(t,e){var r=new F(t.i,t.x,t.y),i=new F(e.i,e.x,e.y),n=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=n,n.prev=r,i.next=r,r.prev=i,o.next=i,i.prev=o,i}function w(t,e,r,i){var n=new F(t,e,r);return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}function U(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function F(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function D(t,e,r,i){for(var n=0,o=e,a=r-i;o<r;o+=i)n+=(t[a]-t[o])*(t[o+1]+t[a+1]),a=o;return n}function L(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:n}=e;if("MultiPolygon"===r.type)for(let i=0;i<r.coordinates.length;i++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[i]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:i}=o.flatten(e.coordinates),n=new Float32Array(i.map(((t,e)=>{if(e%2==1){const e=t*Math.PI/180;return Math.tan(e)+1/Math.cos(e)}return t}))),a=o(i),s=a.length>256?new Uint16Array(a):new Uint8Array(a),u=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),h=r.highlight||!1;let l=0,c=0,f=0;for(let t=0;t<i.length;t+=2)l+=i[t],c+=i[t+1],f++;return{aPosition:n,cuts:s,buildingHeight:u,centroid:[l/f,c/f],highlight:h}}))}o.deviation=function(t,e,r,i){var n=e&&e.length,o=n?e[0]*r:t.length,a=Math.abs(D(t,0,o,r));if(n)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(D(t,h,l,r))}var c=0;for(s=0;s<i.length;s+=3){var f=i[s]*r,m=i[s+1]*r,_=i[s+2]*r;c+=Math.abs((t[f]-t[_])*(t[m+1]-t[f+1])-(t[f]-t[m])*(t[_+1]-t[f+1]))}return 0===a&&0===c?0:Math.abs((c-a)/a)},o.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},i=0,n=0;n<t.length;n++){for(var o=0;o<t[n].length;o++)for(var a=0;a<e;a++)r.vertices.push(t[n][o][a]);n>0&&(i+=t[n-1].length,r.holes.push(i))}return r};function P(t,e,r){var i=e[1],n=e[0],o=i-n;return t===i&&r?t:((t-n)%o+o)%o+n}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 C=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function B(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var I=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function N(t,e,r){return t instanceof B?t:C(t)?new B(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new B(t.x,t.y):new B(t,e,r)}function O(t,e){if(t)for(var r=e?[t,e]:t,i=0,n=r.length;i<n;i++)this.extend(r[i])}function X(t,e){return!t||t instanceof O?t:new O(t,e)}function G(t,e){if(t)for(var r=e?[t,e]:t,i=0,n=r.length;i<n;i++)this.extend(r[i])}function z(t,e){return t instanceof G?t:new G(t,e)}B.prototype={clone:function(){return new B(this.x,this.y)},add:function(t){return this.clone()._add(N(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(N(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new B(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new B(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=I(this.x),this.y=I(this.y),this},distanceTo:function(t){var e=(t=N(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=N(t)).x===this.x&&t.y===this.y},contains:function(t){return t=N(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+S(this.x)+", "+S(this.y)+")"}},O.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof B||"number"==typeof t[0]||"x"in t)e=r=N(t);else if(e=(t=X(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return N((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return N(this.min.x,this.max.y)},getTopRight:function(){return N(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof B?N(t):X(t))instanceof O?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=X(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>=e.x&&i.x<=r.x,a=n.y>=e.y&&i.y<=r.y;return o&&a},overlaps:function(t){t=X(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>e.x&&i.x<r.x,a=n.y>e.y&&i.y<r.y;return o&&a},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,i=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return X(N(e.x-i,e.y-n),N(r.x+i,r.y+n))},equals:function(t){return!!t&&(t=X(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},G.prototype={extend:function(t){var e,r,i=this._southWest,n=this._northEast;if(t instanceof W)e=t,r=t;else{if(!(t instanceof G))return t?this.extend(j(t)||z(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return i||n?(i.lat=Math.min(e.lat,i.lat),i.lng=Math.min(e.lng,i.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new W(e.lat,e.lng),this._northEast=new W(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,i=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new G(new W(e.lat-i,e.lng-n),new W(r.lat+i,r.lng+n))},getCenter:function(){return new W((this._southWest.lat+this._northEast.lat)/2,(this._southWest.lng+this._northEast.lng)/2)},getSouthWest:function(){return this._southWest},getNorthEast:function(){return this._northEast},getNorthWest:function(){return new W(this.getNorth(),this.getWest())},getSouthEast:function(){return new W(this.getSouth(),this.getEast())},getWest:function(){return this._southWest.lng},getSouth:function(){return this._southWest.lat},getEast:function(){return this._northEast.lng},getNorth:function(){return this._northEast.lat},contains:function(t){t="number"==typeof t[0]||t instanceof W||"lat"in t?j(t):z(t);var e,r,i=this._southWest,n=this._northEast;return t instanceof G?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=i.lat&&r.lat<=n.lat&&e.lng>=i.lng&&r.lng<=n.lng},intersects:function(t){t=z(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>=e.lat&&i.lat<=r.lat,a=n.lng>=e.lng&&i.lng<=r.lng;return o&&a},overlaps:function(t){t=z(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>e.lat&&i.lat<r.lat,a=n.lng>e.lng&&i.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=z(t),this._southWest.equals(t.getSouthWest(),e)&&this._northEast.equals(t.getNorthEast(),e))},isValid:function(){return!(!this._southWest||!this._northEast)}};var H=function(t){var e,r,i,n;for(r=1,i=arguments.length;r<i;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),i=this.scale(e);return this.transformation._transform(r,i)},pointToLatLng:function(t,e){var r=this.scale(e),i=this.transformation.untransform(t,r);return this.projection.unproject(i)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new O(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?P(t.lng,this.wrapLng,!0):t.lng;return new W(this.wrapLat?P(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),i=e.lat-r.lat,n=e.lng-r.lng;if(0===i&&0===n)return t;var o=t.getSouthWest(),a=t.getNorthEast();return new G(new W(o.lat-i,o.lng-n),new W(a.lat-i,a.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,i=t.lat*r,n=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(i)*Math.cos(n)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function W(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function j(t,e,r){return t instanceof W?t:C(t)&&"object"!=typeof t[0]?3===t.length?new W(t[0],t[1],t[2]):2===t.length?new W(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new W(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new W(t,e,r)}W.prototype={equals:function(t,e){return!!t&&(t=j(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+S(this.lat,t)+", "+S(this.lng,t)+")"},distanceTo:function(t){return H.distance(this,j(t))},wrap:function(){return H.wrapLatLng(this)},toBounds:function(t){var e=180*t/40075017,r=e/Math.cos(Math.PI/180*this.lat);return z([this.lat-e,this.lng-r],[this.lat+e,this.lng+r])},clone:function(){return new W(this.lat,this.lng,this.alt)}};const Z=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,n=Math.atan2(Math.sin(i)*Math.cos(.40909994067971484),Math.cos(i));return{dec:Math.asin(Math.sin(.40909994067971484)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},Y=(t,e,r)=>{const i=1/e,n=Math.min(t[0]*i,255),o=Math.min(t[1]*i,255),a=Math.min(t[2]*i,255);let s=0;return n+o+a!==0&&(s=n>0?n/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},K=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};const k=(t,e)=>{const{lat:r,lng:i}=t;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(i,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(r,e))},V=(t,e)=>{return new W((r=t.y,i=e,n=Math.PI-2*Math.PI*r/256/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(t.x,e));var r,i,n};let q,$={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new O(new B(0,0),new B(0,0)),DEMPixelBounds:new O(new B(0,0),new B(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const J=t=>n(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:r,terrainSource:i,dsmSource:o,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:h,tileMerger:l,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:m,maxZoom:_,tileSize:d,_overzoom:x}=i,g=(p={getFeatures:r},n(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return L(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var p;try{const t=u,r=t.getNorthWest(),i=t.getSouthEast(),n=new O(k(r,e),k(i,e));let x=new B(n.min.x,n.min.y),p=n.max.x-n.min.x;const E=n.max.y-n.min.y,T=n.max.subtract(n.min);T.y>T.x&&(x.x-=256,p+=512);const v=(t=>{const{upperLeft:e,width:r,height:i}=t,n=e.divideBy(256).floor().multiplyBy(256),o=256*(Math.ceil(r/256)+1),a=256*(Math.ceil(i/256)+1),s=n.add([o,a]);return new O(n,s)})({upperLeft:x,width:p,height:E}),{x:y,y:R}=v.max.subtract(v.min),A=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.x<t.x?e:t)).x,r=t.reduce(((t,e)=>e.y<t.y?e:t)).y;return t.map((t=>{const i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:256*(t.x-e),yOffset:256*(t.y-r)}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:n}=t,o=e.divideBy(256).floor(),a=o.x+r/256,s=Math.min(o.y+i/256,Math.pow(2,n)-1),u=[];for(var h=o.x;h<a;h++)for(var l=o.y;l<s;l++)u.push({x:h,y:l,z:n});return u})({upperLeft:v.min,width:y,height:R,zoom:e})),b=n.max.x-n.min.x,M=n.max.y-n.min.y,w=Math.round(b),U=Math.round(M),F=JSON.stringify(A);if(!c&&F===q&&e<_)return $=Object.assign(Object.assign({},$),{outputWidth:w,outputHeight:U,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),$;const D=yield l.merge(A,{maxZoom:_,width:y,height:R,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:m,tileSize:d,tileLoaded:a});if(null===D)return $=Object.assign(Object.assign({},$),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),$;const L=new O(k(new G(o.bounds).getNorthWest(),e),k(new G(o.bounds).getSouthEast(),e)),P=L.min.subtract(v.min),S=L.max.subtract(v.min),C=[P.x/y,P.y/R,S.x/y,S.y/R].map((t=>2*t-1)),I=yield g,{maxHeight:N,heightMapTex:X}=h.raster({upperLeftTile:A[0],width:y,height:R,mapZoom:e,features:I,imageData:D,gl:s,dsmSource:o,dsmCoords:C}),z=Math.max(Math.max(o.maxHeight,N),D.maxHeight);q=F,$={heightMapTex:X,maxHeight:z,width:y,height:R,DEMPixelBounds:v,visibleDEMPixelBounds:n,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:U}}catch(t){console.error("Could not decode height map",t)}return $})),Q=(t,e)=>{const{r:r,g:i,b:n}=t;return[r/255,i/255,n/255,e]},tt=(t,e)=>{const{date:r}=e,{dec:i,Hi:n}=Z(r);t.updateDate({dec:i,Hi:n})},et=(t,e)=>n(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),rt=(t,e)=>{const{color:r,opacity:i}=e,n=Q(r,i);t.updateColor({colorVec:n})},it=t=>{const{kernel:e,map:r,heightMap:i,now:n,color:o,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:h,outputWidth:l,maxHeight:c,width:f,height:m,DEMPixelBounds:_,visibleDEMPixelBounds:d,demZoom:x}=i;if(0===f||0===m)return;const{min:g,max:p}=d;if(!g||!p)return;const{x:E,y:T}=p.subtract(g),v=_.min,y=r.getPixelDimensions(),R=r.screenUnproject([0,0]),A=r.screenUnproject([y.width,0]),b=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,b,R,A].map((t=>r.project(t,x))),U=w.map((t=>[(t.x-v.x)/f,(t.y-v.y)/m])).flat(),F=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/T])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),D=v.y/256/Math.pow(2,x),L=m/256/Math.pow(2,x),P=new G(V(_.getTopLeft(),x),V(_.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:i,date:n}=e,o=Q(r,i),{dec:a,Hi:s}=Z(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:o,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:m,heightMapZoom:x,cornerTextureCoords:U,cornerClipCoords:F,topYCoord:D,ySize:L,west:S,dLng:C,date:n,color:o,opacity:a,outputHeight:h,outputWidth:l,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const nt=t=>{const e=()=>!t.getPitch;return{project:(r,i)=>{if(e())return t.project(r,i);{const{lat:t,lng:e}=r;return new B(((t,e)=>(t+180)/360*Math.pow(2,e)*256)(e,i),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*256)(t,i))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new W((n=r.y,o=i,a=Math.PI-2*Math.PI*n/256/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/256/Math.pow(2,e)*360-180}(r.x,i));var n,o,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new G(e,r)}}};const ot=t=>{const{gl:e}=t,{texture:r,imageData:i=null,format:n=e.RGBA,width:o,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:h=e.UNSIGNED_BYTE,internalFormat:l=n}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,l,o,a,0,n,h,i)};function at(t){const{gl:e,vSrc:r,fSrc:i}=t,n=e.createShader(e.VERTEX_SHADER);e.shaderSource(n,r),e.compileShader(n);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,i),e.compileShader(o);const a=e.createProgram();return e.attachShader(a,n),e.attachShader(a,o),e.linkProgram(a),a}class st{constructor(t){this.gl=t,this.program=at({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nfloat PI = 3.141592653589793;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = ((a_position.x + 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;\nvarying float v_use_dsm;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n\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:i,width:n,height:o,imageData:a,dsmCoords:s,dsmSource:u,gl:h}=t,{x:l,y:c,z:f}=r;let m=0;h.useProgram(this.program),h.activeTexture(h.TEXTURE1);const _=h.createTexture();h.bindTexture(h.TEXTURE_2D,_),h.pixelStorei(h.UNPACK_ALIGNMENT,2),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST),h.texImage2D(h.TEXTURE_2D,0,h.LUMINANCE_ALPHA,n,o,0,h.LUMINANCE_ALPHA,h.UNSIGNED_BYTE,a),h.pixelStorei(h.UNPACK_ALIGNMENT,4);const d=n,x=o;h.activeTexture(h.TEXTURE0),h.bindTexture(h.TEXTURE_2D,this.targetTexture),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,d,x,0,h.RGBA,h.UNSIGNED_BYTE,null),i>15&&e.length>5?(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST)):(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.LINEAR),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.LINEAR)),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.viewport(0,0,d,x);const g=h.createFramebuffer();h.bindFramebuffer(h.FRAMEBUFFER,g);const p=h.COLOR_ATTACHMENT0;h.framebufferTexture2D(h.FRAMEBUFFER,p,h.TEXTURE_2D,this.targetTexture,0),h.disable(h.BLEND),h.bindBuffer(h.ARRAY_BUFFER,this.positionBuffer),h.bindBuffer(h.ELEMENT_ARRAY_BUFFER,this.indexBuffer),h.clearColor(0,0,0,0),h.clear(h.COLOR_BUFFER_BIT);const E=256*l,T=256*c,v=new B(E,T);h.uniform3f(this.xyzUniformLocation,E,T,f),h.uniform2f(this.dimensionsUniformLocation,n,o),h.uniform1i(this.heightMapUniformLocation,1),h.uniform1i(this.useDSMUniformLocation,0);const y=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,y,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,0,0,0,0),h.drawArrays(h.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:i,centroid:s,highlight:u}=t,l=k({lng:s[0],lat:s[1]},f).subtract(v).floor();if(l.x<0||l.y<0||l.x>n||l.y>o)return;const c=256*a[l.y*n*2+2*l.x]+a[l.y*n*2+2*l.x+1]+5*e,_=Math.floor(c/256)/255,d=Math.floor(c%256)/255;m=Math.max(m,c/5),h.uniform4f(this.colorUniformLocation,_,d,_,d),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ELEMENT_ARRAY_BUFFER,i,h.DYNAMIC_DRAW),h.drawElements(h.TRIANGLES,i.length,i.length>256?h.UNSIGNED_SHORT:h.UNSIGNED_BYTE,0)})),h.deleteTexture(_),0!==u.data.length){h.activeTexture(h.TEXTURE1);const t=h.createTexture();h.bindTexture(h.TEXTURE_2D,t),h.pixelStorei(h.UNPACK_ALIGNMENT,2),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST),h.texImage2D(h.TEXTURE_2D,0,h.LUMINANCE_ALPHA,u.width,u.height,0,h.LUMINANCE_ALPHA,h.UNSIGNED_BYTE,u.data),h.pixelStorei(h.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[1]],r=new Float32Array(e);h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,r,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.enableVertexAttribArray(this.dsmAttributeLocation),h.bindBuffer(h.ARRAY_BUFFER,this.dsmBuffer),h.bufferData(h.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),h.STATIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,0,0,0,0),h.uniform1i(this.useDSMUniformLocation,1),h.colorMask(!1,!1,!0,!0),h.drawArrays(h.TRIANGLE_STRIP,0,4),h.deleteTexture(t)}return h.colorMask(!0,!0,!0,!0),h.deleteFramebuffer(g),{maxHeight:m,heightMapTex:this.targetTexture}}}const ut=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:n,y:o,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);n=Math.floor(n/t),o=Math.floor(o/t),a=r}return{x:256===i?n:Math.floor(n/2),y:256===i?o:Math.floor(o/2),z:256===i?a:a-1}};class ht{constructor(t){this.gl=t,this.program=at({vSrc:"\n attribute vec4 a_position;\n varying vec2 v_pos;\n void main() {\n v_pos = ((a_position + 1.0) * 0.5).xy;\n gl_Position = a_position;\n }\n",fSrc:"\n precision mediump float;\n varying vec2 v_pos;\n uniform sampler2D u_texture;\n void main() {\n gl_FragColor = texture2D(u_texture, v_pos);\n }\n",gl:t}),t.useProgram(this.program),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.positionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.positionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.positionAttributeLocation),t.vertexAttribPointer(this.positionAttributeLocation,2,t.FLOAT,!1,0,0),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return n(this,void 0,void 0,(function*(){const r=this.gl,{width:i,height:o,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:h,tileLoaded:l,maxZoom:c}=e;if(r){const e=new Uint8ClampedArray(2*i*o);e.maxHeight=0,this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const f=new Set;t.forEach((t=>{f.add(s(ut({tile:t,maxZoom:c,tileSize:h})))}));const m=Array.from(f).map((o=>n(this,void 0,void 0,(function*(){return new Promise(((n,l)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{t.filter((t=>o===s(ut({tile:t,maxZoom:c,tileSize:h})))).forEach((t=>{let n=0,o=0;const a=514===h?1:0;let s=0,l=256,m=256;if(256!==h&&(n=t.x%2*256,o=t.y%2*256,l=256,m=256),t.z>c){const e=Math.pow(2,t.z-c);256!==h&&(n=Math.floor(t.x/e)%2*256,o=Math.floor(t.y/e)%2*256),n+=t.x%e/e*256,o+=t.y%e/e*256,l=256/e,m=256/e,s=514===h?1:0}r.useProgram(this.program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this.tileTexture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,f),r.activeTexture(r.TEXTURE1),ot({gl:r,texture:this.outputTexture,imageData:null,format:r.RGBA,width:h,height:h,wrap:r.CLAMP_TO_EDGE,filter:r.NEAREST}),r.bindFramebuffer(r.FRAMEBUFFER,this.framebuffer),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,this.outputTexture,0);const _=r.checkFramebufferStatus(r.FRAMEBUFFER);_!==r.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+_),r.bindBuffer(r.ARRAY_BUFFER,this.positionBuffer),r.enableVertexAttribArray(this.positionAttributeLocation),r.vertexAttribPointer(this.positionAttributeLocation,2,r.FLOAT,!1,0,0),r.viewport(0,0,h,h),r.clear(r.COLOR_BUFFER_BIT),r.drawArrays(r.TRIANGLE_STRIP,0,4);const d=new Uint8Array(4*(l+s)*(m+s));r.readPixels(a+n,a+o,l+s,m+s,r.RGBA,r.UNSIGNED_BYTE,d),r.bindFramebuffer(r.FRAMEBUFFER,null);const x=new Uint8ClampedArray(131072);let g=new Array((l+s)*(m+s)),p=256/l;for(let t=0;t<d.length;t+=4){const r=u({r:d[t],g:d[t+1],b:d[t+2],a:d[t+3]});e.maxHeight=Math.max(r,e.maxHeight);const i=5*(r||0);g[t/4]=i}if(p>1&&514===h)for(g=function(t,e,r,i){const n=Math.floor(e*i),o=Math.floor(r*i),a=[];for(let s=0;s<o;s++)for(let o=0;o<n;o++){const n=o/i,u=s/i,h=Math.floor(n),l=Math.floor(u),c=Math.min(h+1,e-1),f=Math.min(l+1,r-1),m=n-h,_=u-l,d=l*e+c,x=f*e+h,g=f*e+c,p=t[l*e+h]*(1-m)*(1-_)+t[d]*m*(1-_)+t[x]*(1-m)*_+t[g]*m*_;a.push(p)}return function(t,e,r,i,n){if(i>=e||n>=r)return[];const o=e-i,a=r-n,s=[];for(let r=0;r<a;r++)for(let i=0;i<o;i++){const n=r*e+i;s.push(t[n])}return s}(a,Math.sqrt(a.length),Math.sqrt(a.length),i,i)}(g,l+1,m+1,p);p>1;)p/=2,m*=2,l*=2;for(let t=0;t<x.length;t+=2){const e=g[Math.floor(t/2/256/p)*l+Math.floor(t/2%256/p)],r=Math.floor(e/256),i=Math.floor(e%256);x[t]=r,x[t+1]=i}for(let r=0;r<256;r++)e.set(x.slice(256*r*2,256*(r+1)*2),2*(t.yOffset*i+r*i+t.xOffset))})),n(null)},f.onerror=t=>{if(f.src!==f.originalSource)return l("new tiles requested");n(null)},f.crossOrigin=a||null,f.src=o,f.originalSource=f.src})).then((()=>{this.finished++,l(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(m)}catch(t){return console.log(`${m.length} requests aborted`,t),null}return this.inProgress=[],e}throw new Error("Could not get canvas context for merging tile images")}))}}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:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>n(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>n(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(tt(this._compiledKernel,{date:this.options.date}),this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=K(this.options.date)-K(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(rt(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&&(rt(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return n(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:i=new Date,iterations:n=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:n}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)tt(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield et(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:i,height:n}=this._heightMap,o=e.min;return t.map((t=>{const e=k(t,r);return[(e.x-o.x)/i,(e.y-o.y)/n]}))}return[]}_getBounds(t,e){const{width:r,height:i}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(i))return t.getBounds();const n=t.getCenter(),o=k(n,e),a=V(new B(o.x-r/2,o.y-i/2),e),s=V(new B(o.x+r/2,o.y+i/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return n(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=nt(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),i=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===i)).map((t=>t.canonical)),[o,a,s,u]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),h=V(new B(512*o,512*a),i+1),l=V(new B(512*(s+1),512*(u+1)),i+1);e=i+1,this._bounds=t.createBounds({nw:h,se:l})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield J({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return n(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(it({kernel:this._compiledKernel,map:nt(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield et(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,i){const n=new Uint8Array(r*i*4);return this._gl&&this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n),n}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),i=t*e*1,n=1*t,o=(e-1)*n,a=new Uint8Array(i),s=new Uint8Array(i);let u=0;const{startDate:h,endDate:l}=this.options.sunExposure;this.options.sunExposure.enabled&&h&&l&&(u=l.getTime()-h.getTime());for(let t=0;t<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const i=r.subarray(t,t+3),n=Y(i,.5,u)/1e3/60,o=Math.min(Math.floor(n/6),255);e=new Uint8Array([o])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!nt(this._map).isLeaflet())return a;for(let t=0;t<i;t+=n)s.set(a.subarray(t,t+n),o-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,i=t(e,r),n=nt(this._map),{lat:o,lng:a}=n.getBounds().getNorthWest(),s=n.getBounds().getSouthEast(),u=[0,0,0,a,o,0],h=[(s.lng-a)/e,(o-s.lat)/r,0];return this.options.sunExposure.enabled,{data:i,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:h,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations);return((t,e)=>{const r=Q({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=Q({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=K(t.startDate)-K(t.startDate,t.tzId),r=t.startDate.getTime()-e,i=(t.endDate.getTime()-e-r)/86400/1e3,n=1440,o=[],a=828e5,s=K(new Date(r),t.tzId);for(let e=0;e<i;e++){const i=K(new Date(r+86400*e*1e3+a),t.tzId)-s;o[e]=i}const u=[];for(let t=0;t<n;t++){const e=[];for(let n=0;n<i;n++){const i=r+86400*n*1e3+60*t*1e3;e.push(new Date(i+o[n]))}u.push(e)}const h=this._lngLatToTextureCoords([t.location])[0],l=((t,e)=>{const r=Q({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=Q({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{dates:u,texCoord:h}));return l.toArray=function(){const e=new Array;for(let r=0;r<i;r++){const o=[];for(let e=n-1;e>=0;e--){const n=e*i*4+4*r,a=this.slice(n,n+4);o.push(a.join("")===t.sunColor.join("")?1:0)}e.push(o)}return e},{data:l,width:i,height:n}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:i,endDate:n}=this.options.sunExposure,o=n.getTime()-i.getTime(),a=Y(r,.5,o);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:r}=t,i=e,o=at({gl:i,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_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_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_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;}"});i.useProgram(o);const a=i.createBuffer(),s=i.getAttribLocation(o,"a_pos"),u=i.createBuffer(),h=i.getAttribLocation(o,"a_tex_pos"),l=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,l),i.bufferData(i.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),i.STATIC_DRAW);const c=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,c),i.bufferData(i.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),i.STATIC_DRAW);const f=i.getUniformLocation(o,"user_a");i.uniform1i(f,0);const m=i.getUniformLocation(o,"user_width"),_=i.getUniformLocation(o,"user_height"),d=i.getUniformLocation(o,"user_maxHeight"),x=i.getUniformLocation(o,"user_zoom"),g=i.getUniformLocation(o,"user_topYCoord"),p=i.getUniformLocation(o,"user_ySize"),E=i.getUniformLocation(o,"user_step"),T=i.getUniformLocation(o,"user_west"),v=i.getUniformLocation(o,"user_dLng"),y=i.getUniformLocation(o,"user_dec"),R=i.getUniformLocation(o,"user_Hi"),A=i.getUniformLocation(o,"user_color");i.getUniformLocation(o,"user_xStart"),i.getUniformLocation(o,"user_yStart"),i.getUniformLocation(o,"user_xEnd"),i.getUniformLocation(o,"user_yEnd");const b=i.getUniformLocation(o,"u_below_canopy"),M=i.getUniformLocation(o,"user_sunExposureTexture"),w=i.getUniformLocation(o,"user_renderToSunExposureTexture"),U=i.getUniformLocation(o,"user_outputSunExposure"),F=i.getUniformLocation(o,"user_exposureDelta"),D=i.getUniformLocation(o,"u_outputShadeProfile"),L=i.getUniformLocation(o,"u_gpxTexture"),P=i.getUniformLocation(o,"u_decHiTexture"),S=i.getUniformLocation(o,"u_sunColor"),C=i.getUniformLocation(o,"u_outputLocationShadeProfile"),B=i.getUniformLocation(o,"u_shadeProfileLocation");let I=0,N=0,O=0,X=null;const G=()=>{N&&O&&(i.useProgram(o),i.bindBuffer(i.ARRAY_BUFFER,a),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,u),i.enableVertexAttribArray(h),i.vertexAttribPointer(h,2,i.FLOAT,!1,0,0),null!==X&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,X)),r(i,N,O),i.viewport(0,0,N,O),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4))};let z=0;return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:n,maxHeight:s,heightMapZoom:h,topYCoord:l,ySize:c,colorVec:f,step:M,west:w,dLng:U,dec:F,Hi:D,cornerClipCoords:L,cornerTextureCoords:P,outputWidth:S,outputHeight:C,belowCanopy:B,skipRender:z}=t;i.useProgram(o),N=S,O=C,X=e,i.bindBuffer(i.ARRAY_BUFFER,a),i.bufferData(i.ARRAY_BUFFER,new Float32Array(L),i.STATIC_DRAW),i.bindBuffer(i.ARRAY_BUFFER,u),i.bufferData(i.ARRAY_BUFFER,new Float32Array(P),i.STATIC_DRAW),i.uniform1f(m,r),i.uniform1f(_,n),i.uniform1f(d,s),i.uniform1f(x,h),i.uniform1f(g,l),i.uniform1f(p,c),i.uniform4fv(A,f),i.uniform1f(E,M),i.uniform1f(T,w),i.uniform1f(v,U),i.uniform1f(y,F),i.uniform1f(R,D),i.uniform1f(b,B?1:0),z||(window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G))},updateDate:t=>{const{dec:e,Hi:r}=t;i.useProgram(o),i.uniform1f(y,e),i.uniform1f(R,r),window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G)},updateDateRange:t=>n(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:n,emit:l}=t;i.useProgram(o);const c=z=Date.now();i.uniform4fv(F,new Float32Array([1/n,0,0,0]));const f=Math.floor((r.getTime()-e.getTime())/n),m=N,_=O,d=i.createTexture();i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,d),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,m,_,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST);const x=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,x),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,m,_,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST);const g=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,g);const p=i.COLOR_ATTACHMENT0;for(let t=0;t<n;t++){if(i.useProgram(o),l("tileloaded",t,n-1),c!==z)return i.deleteFramebuffer(g),i.deleteTexture(d),i.deleteTexture(x),!0;yield new Promise(((r,n)=>{window.requestAnimationFrame((()=>{i.useProgram(o),i.uniform1i(M,1),i.uniform1i(w,1),null!==X&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,X));const n=t%2==0?x:d,l=t%2==0?d:x;i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,n),i.bindFramebuffer(i.FRAMEBUFFER,g),i.framebufferTexture2D(i.FRAMEBUFFER,p,i.TEXTURE_2D,l,0);const c=i.checkFramebufferStatus(i.FRAMEBUFFER);c!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+c);const{dec:E,Hi:T}=Z(new Date(e.getTime()+f*t));i.uniform1f(y,E),i.uniform1f(R,T),i.bindBuffer(i.ARRAY_BUFFER,a),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,u),i.enableVertexAttribArray(h),i.vertexAttribPointer(h,2,i.FLOAT,!1,0,0),i.viewport(0,0,m,_),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4),r()}))}))}return i.deleteFramebuffer(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(i.useProgram(o),c!==z)return i.deleteTexture(d),i.deleteTexture(x),void t(!0);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,(n-1)%2==0?d:x),i.uniform1i(M,2),i.uniform1i(U,1),i.uniform1i(w,1),G(),i.uniform1i(w,0),i.uniform1i(U,0),i.uniform1i(M,0),i.deleteTexture(d),i.deleteTexture(x),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;i.useProgram(o),i.uniform4fv(A,e),window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G)},generateShadeProfile:t=>{i.useProgram(o);const{dates:e,texCoords:r,shadeColor:n,sunColor:a}=t,u=r.length,f=e.length,m=i.getUniform(o,A);i.uniform4fv(A,n),i.uniform4fv(S,a),i.uniform1i(D,1);const _=i.createTexture();i.activeTexture(i.TEXTURE3),ot({gl:i,imageData:null,width:u,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:_});const d=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,d),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,_,0);const x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1],0,0])).flat(),p=i.createTexture();i.activeTexture(i.TEXTURE2),ot({gl:i,texture:p,imageData:new Float32Array(g),width:g.length/4,height:1,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,internalFormat:i.RGBA32F,type:i.FLOAT}),i.uniform1i(L,2);const E=e.map((t=>{const{dec:e,Hi:r}=Z(t);return[-e,r/10,0,0]})).flat(),T=i.createTexture();i.activeTexture(i.TEXTURE1),ot({gl:i,texture:T,imageData:new Float32Array(E),width:1,height:e.length,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,internalFormat:i.RGBA32F,type:i.FLOAT}),i.uniform1i(P,1),null!==X&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,X)),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,c),i.enableVertexAttribArray(h),i.vertexAttribPointer(h,2,i.FLOAT,!1,0,0),i.viewport(0,0,u,f),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const v=new Uint8Array(u*f*4);return i.readPixels(0,0,u,f,i.RGBA,i.UNSIGNED_BYTE,v),i.deleteTexture(_),i.deleteTexture(T),i.deleteTexture(p),i.deleteFramebuffer(d),i.uniform1i(P,0),i.uniform1i(L,0),i.uniform1i(D,0),i.uniform4fv(A,m),i.uniform4fv(S,[0,0,0,0]),v},generateLocationShadeProfile:t=>{i.useProgram(o);const{dates:e,texCoord:r,shadeColor:n,sunColor:a}=t,u=e[0].length,f=e.length,m=i.getUniform(o,A);i.uniform4fv(A,n),i.uniform4fv(S,a),i.uniform1i(C,1),i.uniform2fv(B,[r[0],r[1]]);const _=i.createTexture();i.activeTexture(i.TEXTURE2),ot({gl:i,imageData:null,width:u,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,texture:_});const d=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,d),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,_,0);const x=i.checkFramebufferStatus(i.FRAMEBUFFER);x!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=e.flat().map((t=>{const{dec:e,Hi:r}=Z(t);return[-e,r/10,0,0]})).flat(),p=i.createTexture();i.activeTexture(i.TEXTURE1),ot({gl:i,texture:p,imageData:new Float32Array(g),width:u,height:f,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST,format:i.RGBA,internalFormat:i.RGBA32F,type:i.FLOAT}),i.uniform1i(P,1),null!==X&&(i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,X)),i.uniform1i(w,0),i.bindBuffer(i.ARRAY_BUFFER,l),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,c),i.enableVertexAttribArray(h),i.vertexAttribPointer(h,2,i.FLOAT,!1,0,0),i.viewport(0,0,u,f),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const E=new Uint8Array(u*f*4);return i.readPixels(0,0,u,f,i.RGBA,i.UNSIGNED_BYTE,E),i.deleteTexture(_),i.deleteTexture(p),i.deleteFramebuffer(d),i.uniform1i(P,0),i.uniform1i(C,0),i.uniform4fv(A,m),i.uniform4fv(S,[0,0,0,0]),E}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new st(this._gl),this._tileMerger=new ht(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new i(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">© ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}void 0!==window.L&&(window.L.shadeMap=(...t)=>new lt(...t));export{lt as default};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Copyright Ted Piotrowski 2024
|
|
3
3
|
* Package: leaflet-shadow-simulator
|
|
4
|
-
* Version: 0.
|
|
4
|
+
* Version: 0.54.2
|
|
5
5
|
* For licensing visit: https://shademap.app/about/
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("leaflet")):"function"==typeof define&&define.amd?define(["leaflet"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).ShadeMap=e(t.L)}(this,(function(t){"use strict";class e extends t.ImageOverlay{constructor(t,e,r={}){super("",e,r),this._url=t}_initImage(){const e="CANVAS"===this._url.tagName,r=this._image=e?this._url:t.DomUtil.create("canvas");return t.DomUtil.addClass(r,"leaflet-image-layer"),this._zoomAnimated&&t.DomUtil.addClass(r,"leaflet-zoom-animated"),this.options.className&&t.DomUtil.addClass(r,this.options.className),r.onselectstart=t.Util.falseFn,r.onmousemove=t.Util.falseFn,this}setBounds(t){return this._bounds=t,this._map&&this._reset(),this}}function r(t,e,r,i){return new(r||(r=Promise))((function(n,o){function s(t){try{u(i.next(t))}catch(t){o(t)}}function a(t){try{u(i.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,a)}u((i=i.apply(t,e||[])).next())}))}function i(t,e,r){var i=e[1],n=e[0],o=i-n;return t===i&&r?t:((t-n)%o+o)%o+n}function n(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,i=0,n=r.length;i<n;i++)this.extend(r[i])}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,i=0,n=r.length;i<n;i++)this.extend(r[i])}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("+n(this.x)+", "+n(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,i=t.min,n=t.max,o=n.x>=e.x&&i.x<=r.x,s=n.y>=e.y&&i.y<=r.y;return o&&s},overlaps:function(t){t=l(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>e.x&&i.x<r.x,s=n.y>e.y&&i.y<r.y;return o&&s},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,i=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return l(u(e.x-i,e.y-n),u(r.x+i,r.y+n))},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,i=this._southWest,n=this._northEast;if(t instanceof d)e=t,r=t;else{if(!(t instanceof c))return t?this.extend(m(t)||f(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return i||n?(i.lat=Math.min(e.lat,i.lat),i.lng=Math.min(e.lng,i.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new d(e.lat,e.lng),this._northEast=new d(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,i=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new c(new d(e.lat-i,e.lng-n),new d(r.lat+i,r.lng+n))},getCenter:function(){return new d((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 d(this.getNorth(),this.getWest())},getSouthEast:function(){return new d(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 d||"lat"in t?m(t):f(t);var e,r,i=this._southWest,n=this._northEast;return t instanceof c?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=i.lat&&r.lat<=n.lat&&e.lng>=i.lng&&r.lng<=n.lng},intersects:function(t){t=f(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>=e.lat&&i.lat<=r.lat,s=n.lng>=e.lng&&i.lng<=r.lng;return o&&s},overlaps:function(t){t=f(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>e.lat&&i.lat<r.lat,s=n.lng>e.lng&&i.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 _=function(t){var e,r,i,n;for(r=1,i=arguments.length;r<i;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),i=this.scale(e);return this.transformation._transform(r,i)},pointToLatLng:function(t,e){var r=this.scale(e),i=this.transformation.untransform(t,r);return this.projection.unproject(i)},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?i(t.lng,this.wrapLng,!0):t.lng;return new d(this.wrapLat?i(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),i=e.lat-r.lat,n=e.lng-r.lng;if(0===i&&0===n)return t;var o=t.getSouthWest(),s=t.getNorthEast();return new c(new d(o.lat-i,o.lng-n),new d(s.lat-i,s.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,i=t.lat*r,n=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(i)*Math.cos(n)*s*s,u=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));return this.R*u}});function d(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 m(t,e,r){return t instanceof d?t:o(t)&&"object"!=typeof t[0]?3===t.length?new d(t[0],t[1],t[2]):2===t.length?new d(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new d(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new d(t,e,r)}d.prototype={equals:function(t,e){return!!t&&(t=m(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("+n(this.lat,t)+", "+n(this.lng,t)+")"},distanceTo:function(t){return _.distance(this,m(t))},wrap:function(){return _.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 d(this.lat,this.lng,this.alt)}};const g=256;const x=(t,e)=>{const{lat:r,lng:i}=t;return new s(((t,e)=>(t+180)/360*Math.pow(2,e)*g)(i,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*g)(r,e))},p=(t,e)=>{return new d((r=t.y,i=e,n=Math.PI-2*Math.PI*r/g/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/g/Math.pow(2,e)*360-180}(t.x,e));var r,i,n},E=.40909994067971484,T=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,n=Math.atan2(Math.sin(i)*Math.cos(E),Math.cos(i));return{dec:Math.asin(Math.sin(E)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},v=(t,e,r)=>{const i=1/e,n=Math.min(t[0]*i,255),o=Math.min(t[1]*i,255),s=Math.min(t[2]*i,255);let a=0;return n+o+s!==0&&(a=n>0?n/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"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};function R(t){const{gl:e,vSrc:r,fSrc:i}=t,n=e.createShader(e.VERTEX_SHADER);e.shaderSource(n,r),e.compileShader(n);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,i),e.compileShader(o);const s=e.createProgram();return e.attachShader(s,n),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;\nvarying float v_use_dsm;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = ((a_position.x + 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;\nvarying float v_use_dsm;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n\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:i,width:n,height:o,imageData:a,dsmCoords:u,dsmSource:h,gl:l}=t,{x:c,y:f,z:_}=r;let d=0;l.useProgram(this.program),l.activeTexture(l.TEXTURE1);const 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,n,o,0,l.LUMINANCE_ALPHA,l.UNSIGNED_BYTE,a);const p=n,E=o;l.activeTexture(l.TEXTURE0),l.bindTexture(l.TEXTURE_2D,this.targetTexture),l.texImage2D(l.TEXTURE_2D,0,l.RGBA,p,E,0,l.RGBA,l.UNSIGNED_BYTE,null),i>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,p,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.disable(l.BLEND),l.bindBuffer(l.ARRAY_BUFFER,this.positionBuffer),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,this.indexBuffer),l.clearColor(0,0,0,0),l.clear(l.COLOR_BUFFER_BIT);const y=c*g,R=f*g,A=new s(y,R);l.uniform3f(this.xyzUniformLocation,y,R,_),l.uniform2f(this.dimensionsUniformLocation,n,o),l.uniform1i(this.heightMapUniformLocation,1),l.uniform1i(this.useDSMUniformLocation,0);const b=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(l.enableVertexAttribArray(this.positionAttributeLocation),l.bufferData(l.ARRAY_BUFFER,b,l.STATIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.uniform4f(this.colorUniformLocation,0,0,0,0),l.drawArrays(l.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:i,centroid:s,highlight:u}=t,h=x({lng:s[0],lat:s[1]},_).subtract(A).floor();if(h.x<0||h.y<0||h.x>n||h.y>o)return;const c=256*a[h.y*n*2+2*h.x]+a[h.y*n*2+2*h.x+1]+5*e,f=Math.floor(c/256)/255,m=Math.floor(c%256)/255;d=Math.max(d,c/5),l.uniform4f(this.colorUniformLocation,f,m,f,m),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.positionAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ARRAY_BUFFER,r,l.DYNAMIC_DRAW),l.vertexAttribPointer(this.dsmAttributeLocation,2,l.FLOAT,!1,0,0),l.bufferData(l.ELEMENT_ARRAY_BUFFER,i,l.DYNAMIC_DRAW),l.drawElements(l.TRIANGLES,i.length,i.length>256?l.UNSIGNED_SHORT:l.UNSIGNED_BYTE,0)})),l.deleteTexture(m),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.colorMask(!1,!1,!0,!0),l.drawArrays(l.TRIANGLE_STRIP,0,4),l.deleteTexture(t)}return l.colorMask(!0,!0,!0,!0),l.enable(l.BLEND),l.blendFunc(l.SRC_ALPHA,l.ONE_MINUS_SRC_ALPHA),l.deleteFramebuffer(T),d}}function b(t,e,r){r=r||2;var i,n,o,s,a,u,h,l=e&&e.length,c=l?e[0]*r:t.length,f=w(t,0,c,r,!0),_=[];if(!f||f.next===f.prev)return _;if(l&&(f=function(t,e,r,i){var n,o,s,a=[];for(n=0,o=e.length;n<o;n++)(s=w(t,e[n]*i,n<o-1?e[n+1]*i:t.length,i,!1))===s.next&&(s.steiner=!0),a.push(N(s));for(a.sort(C),n=0;n<a.length;n++)r=F(a[n],r);return r}(t,e,f,r)),t.length>80*r){i=o=t[0],n=s=t[1];for(var d=r;d<c;d+=r)(a=t[d])<i&&(i=a),(u=t[d+1])<n&&(n=u),a>o&&(o=a),u>s&&(s=u);h=0!==(h=Math.max(o-i,s-n))?32767/h:0}return U(f,_,r,i,n,h,0),_}function w(t,e,r,i,n){var o,s;if(n===V(t,e,r,i)>0)for(o=e;o<r;o+=i)s=K(o,t[o],t[o+1],s);else for(o=r-i;o>=e;o-=i)s=K(o,t[o],t[o+1],s);return s&&G(s,s.next)&&(q(s),s=s.next),s}function M(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!G(i,i.next)&&0!==z(i.prev,i,i.next))i=i.next;else{if(q(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function U(t,e,r,i,n,o,s){if(t){!s&&o&&function(t,e,r,i){var n=t;do{0===n.z&&(n.z=B(n.x,n.y,e,r,i)),n.prevZ=n.prev,n.nextZ=n.next,n=n.next}while(n!==t);n.prevZ.nextZ=null,n.prevZ=null,function(t){var e,r,i,n,o,s,a,u,h=1;do{for(r=t,t=null,o=null,s=0;r;){for(s++,i=r,a=0,e=0;e<h&&(a++,i=i.nextZ);e++);for(u=h;a>0||u>0&&i;)0!==a&&(0===u||!i||r.z<=i.z)?(n=r,r=r.nextZ,a--):(n=i,i=i.nextZ,u--),o?o.nextZ=n:t=n,n.prevZ=o,o=n;r=i}o.nextZ=null,h*=2}while(s>1)}(n)}(t,i,n,o);for(var a,u,h=t;t.prev!==t.next;)if(a=t.prev,u=t.next,o?L(t,i,n,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(M(t),e,r),e,r,i,n,o,2):2===s&&S(t,e,r,i,n,o):U(M(t),e,r,i,n,o,1);break}}}function D(t){var e=t.prev,r=t,i=t.next;if(z(e,r,i)>=0)return!1;for(var n=e.x,o=r.x,s=i.x,a=e.y,u=r.y,h=i.y,l=n<o?n<s?n:s:o<s?o:s,c=a<u?a<h?a:h:u<h?u:h,f=n>o?n>s?n:s:o>s?o:s,_=a>u?a>h?a:h:u>h?u:h,d=i.next;d!==e;){if(d.x>=l&&d.x<=f&&d.y>=c&&d.y<=_&&X(n,a,o,u,s,h,d.x,d.y)&&z(d.prev,d,d.next)>=0)return!1;d=d.next}return!0}function L(t,e,r,i){var n=t.prev,o=t,s=t.next;if(z(n,o,s)>=0)return!1;for(var a=n.x,u=o.x,h=s.x,l=n.y,c=o.y,f=s.y,_=a<u?a<h?a:h:u<h?u:h,d=l<c?l<f?l:f:c<f?c:f,m=a>u?a>h?a:h:u>h?u:h,g=l>c?l>f?l:f:c>f?c:f,x=B(_,d,e,r,i),p=B(m,g,e,r,i),E=t.prevZ,T=t.nextZ;E&&E.z>=x&&T&&T.z<=p;){if(E.x>=_&&E.x<=m&&E.y>=d&&E.y<=g&&E!==n&&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>=_&&T.x<=m&&T.y>=d&&T.y<=g&&T!==n&&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>=x;){if(E.x>=_&&E.x<=m&&E.y>=d&&E.y<=g&&E!==n&&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>=_&&T.x<=m&&T.y>=d&&T.y<=g&&T!==n&&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 i=t;do{var n=i.prev,o=i.next.next;!G(n,o)&&H(n,i,i.next,o)&&Z(n,o)&&Z(o,n)&&(e.push(n.i/r|0),e.push(i.i/r|0),e.push(o.i/r|0),q(i),q(i.next),i=t=o),i=i.next}while(i!==t);return M(i)}function S(t,e,r,i,n,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=M(s,s.next),u=M(u,u.next),U(s,e,r,i,n,o,0),void U(u,e,r,i,n,o,0)}a=a.next}s=s.next}while(s!==t)}function C(t,e){return t.x-e.x}function F(t,e){var r=function(t,e){var r,i=e,n=t.x,o=t.y,s=-1/0;do{if(o<=i.y&&o>=i.next.y&&i.next.y!==i.y){var a=i.x+(o-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(a<=n&&a>s&&(s=a,r=i.x<i.next.x?i:i.next,a===n))return r}i=i.next}while(i!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;i=r;do{n>=i.x&&i.x>=l&&n!==i.x&&X(o<c?n:s,o,l,c,o<c?s:n,o,i.x,i.y)&&(u=Math.abs(o-i.y)/(n-i.x),Z(i,t)&&(u<f||u===f&&(i.x>r.x||i.x===r.x&&I(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=Y(r,t);return M(i,i.next),M(r,r.next)}function I(t,e){return z(t.prev,t,e.prev)<0&&z(e.next,t,t.next)<0}function B(t,e,r,i,n){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*n|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*n|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,i,n,o,s,a){return(n-s)*(e-a)>=(t-s)*(o-a)&&(t-s)*(i-a)>=(r-s)*(e-a)&&(r-s)*(o-a)>=(n-s)*(i-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&&H(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,i=!1,n=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&n<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(z(t.prev,t,e.prev)||z(t,e.prev,e))||G(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 G(t,e){return t.x===e.x&&t.y===e.y}function H(t,e,r,i){var n=j(z(t,e,r)),o=j(z(t,e,i)),s=j(z(r,i,t)),a=j(z(r,i,e));return n!==o&&s!==a||(!(0!==n||!W(t,r,e))||(!(0!==o||!W(t,i,e))||(!(0!==s||!W(r,t,i))||!(0!==a||!W(r,e,i)))))}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),i=new k(e.i,e.x,e.y),n=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=n,n.prev=r,i.next=r,r.prev=i,o.next=i,i.prev=o,i}function K(t,e,r,i){var n=new k(t,e,r);return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}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,i){for(var n=0,o=e,s=r-i;o<r;o+=i)n+=(t[s]-t[o])*(t[o+1]+t[s+1]),s=o;return n}function $(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:n}=e;if("MultiPolygon"===r.type)for(let i=0;i<r.coordinates.length;i++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[i]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:i}=b.flatten(e.coordinates),n=new Float32Array(i.map(((t,e)=>{if(e%2==1){const e=t*Math.PI/180;return Math.tan(e)+1/Math.cos(e)}return t}))),o=b(i),s=o.length>256?new Uint16Array(o):new Uint8Array(o),a=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),u=r.highlight||!1;let h=0,l=0,c=0;for(let t=0;t<i.length;t+=2)h+=i[t],l+=i[t+1],c++;return{aPosition:n,cuts:s,buildingHeight:a,centroid:[h/c,l/c],highlight:u}}))}b.deviation=function(t,e,r,i){var n=e&&e.length,o=n?e[0]*r:t.length,s=Math.abs(V(t,0,o,r));if(n)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<i.length;a+=3){var f=i[a]*r,_=i[a+1]*r,d=i[a+2]*r;c+=Math.abs((t[f]-t[d])*(t[_+1]-t[f+1])-(t[f]-t[_])*(t[d+1]-t[f+1]))}return 0===s&&0===c?0:Math.abs((c-s)/s)},b.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},i=0,n=0;n<t.length;n++){for(var o=0;o<t[n].length;o++)for(var s=0;s<e;s++)r.vertices.push(t[n][o][s]);n>0&&(i+=t[n-1].length,r.holes.push(i))}return r};let J,Q={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:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const tt=t=>r(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:i,terrainSource:n,dsmSource:o,tileLoaded:a,gl:u,bounds:l,buildingRasterizer:f,tileMerger:_,forceUpdate:d=!1}=t,{getSourceUrl:m,getElevation:p,maxZoom:E,tileSize:T,_overzoom:v}=n,y=(R={getFeatures:i},r(void 0,void 0,void 0,(function*(){const{getFeatures:t}=R;try{return $(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var R;try{const t=l,r=t.getNorthWest(),i=t.getSouthEast(),n=new h(x(r,e),x(i,e));let v=new s(n.min.x,n.min.y),R=n.max.x-n.min.x;const A=n.max.y-n.min.y,b=n.max.subtract(n.min);b.y>b.x&&(v.x-=g,R+=512);const w=(t=>{const{upperLeft:e,width:r,height:i}=t,n=e.divideBy(g).floor().multiplyBy(g),o=(Math.ceil(r/g)+1)*g,s=(Math.ceil(i/g)+1)*g,a=n.add([o,s]);return new h(n,a)})({upperLeft:v,width:R,height:A}),{x:M,y:U}=w.max.subtract(w.min),D=(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 i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:(t.x-e)*g,yOffset:(t.y-r)*g}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:n}=t,o=e.divideBy(g).floor(),s=o.x+r/g,a=Math.min(o.y+i/g,Math.pow(2,n)-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:n});return u})({upperLeft:w.min,width:M,height:U,zoom:e})),L=n.max.x-n.min.x,P=n.max.y-n.min.y,S=L,C=P,F=JSON.stringify(D);if(!d&&F===J&&e<E)return Q=Object.assign(Object.assign({},Q),{outputWidth:S,outputHeight:C,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),Q;const I=yield _.merge(D,{maxZoom:E,width:M,height:U,crossOrigin:"Anonymous",getSourceUrl:m,getElevation:p,tileSize:T,tileLoaded:a});if(null===I)return Q=Object.assign(Object.assign({},Q),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),Q;const B=new h(x(new c(o.bounds).getNorthWest(),e),x(new c(o.bounds).getSouthEast(),e)),N=B.min.subtract(w.min),X=B.max.subtract(w.min),O=[N.x/M,N.y/U,X.x/M,X.y/U].map((t=>2*t-1)),z=yield y,G=f.raster({upperLeftTile:D[0],width:M,height:U,mapZoom:e,features:z,imageData:I,gl:u,dsmSource:o,dsmCoords:O}),H=Math.max(Math.max(o.maxHeight,G),I.maxHeight);J=F,Q={maxHeight:H,width:M,height:U,imageData:I,DEMPixelBounds:w,visibleDEMPixelBounds:n,raster:D,demZoom:e,dirty:!0,outputWidth:S,outputHeight:C}}catch(t){console.error("Could not decode height map",t)}return Q})),et=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:n,y:o,z:s}=e;if(s>r){const t=Math.pow(2,e.z-r);n=Math.floor(n/t),o=Math.floor(o/t),s=r}return{x:i===g?n:Math.floor(n/2),y:i===g?o:Math.floor(o/2),z:i===g?s:s-1}};class rt{constructor(t){this.gl=t,this.program=R({vSrc:"\n attribute vec4 a_position;\n varying vec2 v_pos;\n void main() {\n v_pos = ((a_position + 1.0) * 0.5).xy;\n gl_Position = a_position;\n }\n",fSrc:"\n precision mediump float;\n varying vec2 v_pos;\n uniform sampler2D u_texture;\n void main() {\n gl_FragColor = texture2D(u_texture, v_pos);\n }\n",gl:t}),this.positionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.positionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),t.enableVertexAttribArray(this.positionAttributeLocation),t.vertexAttribPointer(this.positionAttributeLocation,2,t.FLOAT,!1,0,0),this.texture=t.createTexture(),this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.NEAREST),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.NEAREST),this.inProgress=[],this.finished=0}merge(t,e){return r(this,void 0,void 0,(function*(){const{width:i,height:n,crossOrigin:o,getSourceUrl:s,getElevation:a,tileSize:u,tileLoaded:h,maxZoom:l}=e;if(this.gl){const e=new Uint8ClampedArray(2*i*n);e.maxHeight=0,this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const c=new Set;t.forEach((t=>{c.add(s(et({tile:t,maxZoom:l,tileSize:u})))}));const f=Array.from(c).map((n=>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=>n===s(et({tile:t,maxZoom:l,tileSize:u})))).forEach((t=>{let r=0,n=0;const o=514===u?1:0;let s=0,h=g,f=g;if(u!==g&&(r=t.x%2*g,n=t.y%2*g,h=g,f=g),t.z>l){const e=Math.pow(2,t.z-l);u!==g&&(r=Math.floor(t.x/e)%2*g,n=Math.floor(t.y/e)%2*g),r+=g*(t.x%e/e),n+=g*(t.y%e/e),h=g/e,f=g/e,s=514===u?1:0}this.gl.useProgram(this.program),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,c);const _=this.gl.getParameter(this.gl.VIEWPORT),d=this.gl.canvas.width,m=this.gl.canvas.height;this.gl.canvas.width=u,this.gl.canvas.height=u,this.gl.viewport(0,0,u,u),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.positionBuffer),this.gl.enableVertexAttribArray(this.positionAttributeLocation),this.gl.vertexAttribPointer(this.positionAttributeLocation,2,this.gl.FLOAT,!1,0,0),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);const x=new Uint8Array(4*(h+s)*(f+s));this.gl.readPixels(o+r,o+n,h+s,f+s,this.gl.RGBA,this.gl.UNSIGNED_BYTE,x),this.gl.canvas.width=d,this.gl.canvas.height=m,this.gl.viewport(_[0],_[1],_[2],_[3]);const p=new Uint8ClampedArray(131072);let E=new Array((h+s)*(f+s)),T=g/h;for(let t=0;t<x.length;t+=4){const r=a({r:x[t],g:x[t+1],b:x[t+2],a:x[t+3]});e.maxHeight=Math.max(r,e.maxHeight);const i=5*(r||0);E[t/4]=i}if(T>1&&514===u)for(E=function(t,e,r,i){const n=Math.floor(e*i),o=Math.floor(r*i),s=[];for(let a=0;a<o;a++)for(let o=0;o<n;o++){const n=o/i,u=a/i,h=Math.floor(n),l=Math.floor(u),c=Math.min(h+1,e-1),f=Math.min(l+1,r-1),_=n-h,d=u-l,m=l*e+c,g=f*e+h,x=f*e+c,p=t[l*e+h]*(1-_)*(1-d)+t[m]*_*(1-d)+t[g]*(1-_)*d+t[x]*_*d;s.push(p)}return function(t,e,r,i,n){if(i>=e||n>=r)return[];const o=e-i,s=r-n,a=[];for(let r=0;r<s;r++)for(let i=0;i<o;i++){const n=r*e+i;a.push(t[n])}return a}(s,Math.sqrt(s.length),Math.sqrt(s.length),i,i)}(E,h+1,f+1,T);T>1;)T/=2,f*=2,h*=2;for(let t=0;t<p.length;t+=2){const e=E[Math.floor(t/2/g/T)*h+Math.floor(t/2%g/T)],r=Math.floor(e/256),i=Math.floor(e%256);p[t]=r,p[t+1]=i}for(let r=0;r<g;r++)e.set(p.slice(r*g*2,(r+1)*g*2),2*(t.yOffset*i+r*i+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=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:i,b:n}=t;return[r/255,i/255,n/255,e]},nt=(t,e)=>{const{date:r}=e,{dec:i,Hi:n}=T(r);t.updateDate({dec:i,Hi:n})},ot=(t,e)=>r(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),st=(t,e)=>{const{color:r,opacity:i}=e,n=it(r,i);t.updateColor({colorVec:n})};new c([0,0],[0,0]);const at=t=>{const{kernel:e,map:r,heightMap:i,now:n,color:o,opacity:s,belowCanopy:a}=t;try{const{outputHeight:t,outputWidth:u,maxHeight:h,width:l,height:f,DEMPixelBounds:_,imageData:d,visibleDEMPixelBounds:m,demZoom:x}=i;if(0===l||0===f)return;const{min:E,max:v}=m;if(!E||!v)return;const{x:y,y:R}=v.subtract(E),A=_.min,b=r.getPixelDimensions(),w=r.screenUnproject([0,0]),M=r.screenUnproject([b.width,0]),U=r.screenUnproject([b.width,b.height]),D=r.screenUnproject([0,b.height]),L=[D,U,w,M].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-E.x)/y,(t.y-E.y)/R])).map((t=>[2*t[0]-1,-1*(2*t[1]-1)])).flat(),C=A.y/g/Math.pow(2,x),F=f/g/Math.pow(2,x),I=new c(p(_.getTopLeft(),x),p(_.getBottomRight(),x)),B=I.getWest(),N=Math.abs(I.getWest()-I.getEast());((t,e)=>{const{color:r,opacity:i,date:n}=e,o=it(r,i),{dec:s,Hi:a}=T(n);t.updateLocation(Object.assign({dec:s,Hi:a,colorVec:o,step:1},e))})(e,{imageData:d,maxHeight:h,width:l,height:f,heightMapZoom:x,cornerTextureCoords:P,cornerClipCoords:S,topYCoord:C,ySize:F,west:B,dLng:N,date:n,color:o,opacity:s,outputHeight:t,outputWidth:u,belowCanopy:a})}catch(t){console.error("EXCEPTION",t)}};const ut=t=>{const e=()=>!t.getPitch;return{project:(r,i)=>{if(e())return t.project(r,i);{const{lat:t,lng:e}=r;return new s(((t,e)=>(t+180)/360*Math.pow(2,e)*g)(e,i),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*g)(t,i))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new d((n=r.y,o=i,s=Math.PI-2*Math.PI*n/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,i));var n,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 ht extends class extends class{constructor(){this.events={}}on(t,e){return"object"!=typeof this.events[t]&&(this.events[t]=[]),this.events[t].push(e),()=>this.removeListener(t,e)}removeListener(t,e){if("object"!=typeof this.events[t])return;const r=this.events[t].indexOf(e);r>-1&&this.events[t].splice(r,1)}removeAllListeners(){Object.keys(this.events).forEach((t=>this.events[t].splice(0,this.events[t].length)))}emit(t,...e){"object"==typeof this.events[t]&&[...this.events[t]].forEach((t=>t.apply(this,e)))}once(t,e){const r=this.on(t,((...t)=>{r(),e.apply(this,t)}));return r}}{constructor(...t){super(),this.options={date:new Date,color:"000",opacity:.3,sunExposure:{enabled:!1,startDate:new Date,endDate:new Date,iterations:32},apiKey:"",terrainSource:{maxZoom:15,tileSize:256,_overzoom:15,getSourceUrl:t=>"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/7/17/45.png",getElevation:t=>{const{r:e,g:r,b:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>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 i={preserveDrawingBuffer:!0,antialias:!1};this._canvas=document.createElement("canvas"),this._gl=this._canvas.getContext("webgl",i)||this._canvas.getContext("experimental-webgl",i),this._compiledKernel=function(t){const{context:e}=t,i=e,n=R({gl:i,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_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_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_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;}"});i.useProgram(n);const o=i.createBuffer(),s=i.getAttribLocation(n,"a_pos"),a=i.createBuffer(),u=i.getAttribLocation(n,"a_tex_pos"),h=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,h),i.bufferData(i.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),i.STATIC_DRAW),i.enable(i.BLEND),i.blendFunc(i.SRC_ALPHA,i.ONE_MINUS_SRC_ALPHA);const l=i.getUniformLocation(n,"user_a");i.uniform1i(l,0);const c=i.getUniformLocation(n,"user_width"),f=i.getUniformLocation(n,"user_height"),_=i.getUniformLocation(n,"user_maxHeight"),d=i.getUniformLocation(n,"user_zoom"),m=i.getUniformLocation(n,"user_topYCoord"),g=i.getUniformLocation(n,"user_ySize"),x=i.getUniformLocation(n,"user_step"),p=i.getUniformLocation(n,"user_west"),E=i.getUniformLocation(n,"user_dLng"),v=i.getUniformLocation(n,"user_dec"),y=i.getUniformLocation(n,"user_Hi"),A=i.getUniformLocation(n,"user_color"),b=i.getUniformLocation(n,"user_xStart"),w=i.getUniformLocation(n,"user_yStart"),M=i.getUniformLocation(n,"user_xEnd"),U=i.getUniformLocation(n,"user_yEnd"),D=i.getUniformLocation(n,"u_below_canopy"),L=i.getUniformLocation(n,"user_sunExposureTexture");i.uniform1i(L,2);const P=i.getUniformLocation(n,"user_renderToSunExposureTexture"),S=i.getUniformLocation(n,"user_outputSunExposure"),C=i.getUniformLocation(n,"user_exposureDelta"),F=i.getUniformLocation(n,"u_outputShadeProfile"),I=i.getUniformLocation(n,"u_gpxTexture"),B=i.getUniformLocation(n,"u_decHiTexture"),N=i.getUniformLocation(n,"u_sunColor"),X=i.getUniformLocation(n,"u_outputLocationShadeProfile"),O=i.getUniformLocation(n,"u_shadeProfileLocation"),z=()=>{i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4)};let G=0;return{updateLocation:function(t){const{width:e,height:r,maxHeight:h,heightMapZoom:l,topYCoord:T,ySize:R,colorVec:b,step:w,west:M,dLng:U,dec:L,Hi:P,cornerClipCoords:S,cornerTextureCoords:C,outputWidth:F,outputHeight:I,belowCanopy:B}=t;i.useProgram(n),i.canvas.width=F,i.canvas.height=I,i.viewport(0,0,F,I),i.bindFramebuffer(i.FRAMEBUFFER,null),i.bindBuffer(i.ARRAY_BUFFER,o),i.bufferData(i.ARRAY_BUFFER,new Float32Array(S),i.STATIC_DRAW),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.bindBuffer(i.ARRAY_BUFFER,a),i.bufferData(i.ARRAY_BUFFER,new Float32Array(C),i.STATIC_DRAW),i.enableVertexAttribArray(u),i.vertexAttribPointer(u,2,i.FLOAT,!1,0,0),i.uniform1f(c,e),i.uniform1f(f,r),i.uniform1f(_,h),i.uniform1f(d,l),i.uniform1f(m,T),i.uniform1f(g,R),i.uniform4fv(A,b),i.uniform1f(x,w),i.uniform1f(p,M),i.uniform1f(E,U),i.uniform1f(v,L),i.uniform1f(y,P),i.uniform1f(D,B?1:0),window.requestAnimationFrame(z)},updateViewport:function(t){const{xStart:e,yStart:r,xEnd:n,yEnd:o}=t;i.uniform1f(b,e),i.uniform1f(w,r),i.uniform1f(M,n),i.uniform1f(U,o),window.requestAnimationFrame(z)},updateDate:t=>{const{dec:e,Hi:r}=t;i.uniform1f(v,e),i.uniform1f(y,r),window.requestAnimationFrame(z)},updateDateRange:t=>r(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:n,emit:o}=t,s=G=Date.now();i.uniform4fv(C,new Float32Array([1/n,0,0,0]));const a=Math.floor((r.getTime()-e.getTime())/n),u=i.canvas.width,h=i.canvas.height,l=i.createTexture();i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,l),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,u,h,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.LINEAR);const c=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,c),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,u,h,0,i.RGBA,i.UNSIGNED_BYTE,null),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.LINEAR),i.uniform1i(P,1),i.viewport(0,0,u,h);const f=i.createFramebuffer();i.bindFramebuffer(i.FRAMEBUFFER,f);const _=i.COLOR_ATTACHMENT0;i.framebufferTexture2D(i.FRAMEBUFFER,_,i.TEXTURE_2D,l,0);for(let t=0;t<n;t++){if(o("tileloaded",t,n-1),s!==G)return i.deleteFramebuffer(f),i.deleteTexture(l),i.deleteTexture(c),!0;yield new Promise(((r,n)=>{window.requestAnimationFrame((()=>{t%2==0?(i.uniform1i(L,2),i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,l),i.framebufferTexture2D(i.FRAMEBUFFER,_,i.TEXTURE_2D,l,0)):(i.uniform1i(L,1),i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,c),i.framebufferTexture2D(i.FRAMEBUFFER,_,i.TEXTURE_2D,c,0));const{dec:n,Hi:o}=T(new Date(e.getTime()+a*t));i.uniform1f(v,n),i.uniform1f(y,o),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4),r()}))}))}return yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{s!==G&&(i.deleteFramebuffer(f),i.deleteTexture(l),i.deleteTexture(c),t(!0)),i.bindFramebuffer(i.FRAMEBUFFER,null),i.uniform1i(L,n%2==0?2:1),i.uniform1i(S,1),z(),i.uniform1i(P,0),i.uniform1i(S,0),i.deleteFramebuffer(f),i.deleteTexture(l),i.deleteTexture(c),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;i.uniform4fv(A,e),window.requestAnimationFrame(z)},generateShadeProfile:t=>{if(null===i.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoords:r,shadeColor:a,sunColor:u}=t,l=r.length,c=e.length,f=i.getParameter(i.VIEWPORT),_=i.getUniform(n,A);i.uniform4fv(A,a),i.uniform4fv(N,u),i.uniform1i(F,1);const d=i.createTexture();i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,d);const m=r.map((t=>[t[0],t[1],0,0])).flat();i.texImage2D(i.TEXTURE_2D,0,i.RGBA,m.length/4,1,0,i.RGBA,i.FLOAT,new Float32Array(m)),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST),i.uniform1i(I,2);const g=i.createTexture();i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,g);const x=e.map((t=>{const{dec:e,Hi:r}=T(t);return[-e,r/10,0,0]})).flat();i.texImage2D(i.TEXTURE_2D,0,i.RGBA,1,e.length,0,i.RGBA,i.FLOAT,new Float32Array(x)),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST),i.uniform1i(B,1),i.viewport(0,0,l,c),i.clear(i.COLOR_BUFFER_BIT),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.drawArrays(i.TRIANGLE_STRIP,0,4),i.bindBuffer(i.ARRAY_BUFFER,o),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0);const p=new Uint8Array(l*c*4);return i.readPixels(0,0,l,c,i.RGBA,i.UNSIGNED_BYTE,p),i.deleteTexture(g),i.deleteTexture(d),i.uniform1i(B,0),i.uniform1i(I,0),i.uniform1i(F,0),i.viewport(f[0],f[1],f[2],f[3]),i.uniform4fv(A,_),i.uniform4fv(N,[0,0,0,0]),p},generateLocationShadeProfile:t=>{if(null===i.getExtension("OES_texture_float"))throw new Error("Float texture support required");const{dates:e,texCoord:r,shadeColor:a,sunColor:u}=t,l=e[0].length,c=e.length,f=i.getParameter(i.VIEWPORT),_=i.getUniform(n,A);i.uniform4fv(A,a),i.uniform4fv(N,u),i.uniform1i(X,1),i.uniform2fv(O,[r[0],r[1]]);const d=i.createTexture();i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,d);const m=e.flat().map((t=>{const{dec:e,Hi:r}=T(t);return[-e,r/10,0,0]})).flat();i.texImage2D(i.TEXTURE_2D,0,i.RGBA,l,c,0,i.RGBA,i.FLOAT,new Float32Array(m)),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.NEAREST),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.NEAREST),i.uniform1i(B,1),i.viewport(0,0,l,c),i.clear(i.COLOR_BUFFER_BIT),i.bindBuffer(i.ARRAY_BUFFER,h),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.drawArrays(i.TRIANGLE_STRIP,0,4),i.bindBuffer(i.ARRAY_BUFFER,o),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0);const g=new Uint8Array(l*c*4);return i.readPixels(0,0,l,c,i.RGBA,i.UNSIGNED_BYTE,g),i.deleteTexture(d),i.uniform1i(B,0),i.uniform1i(X,0),i.viewport(f[0],f[1],f[2],f[3]),i.uniform4fv(A,_),i.uniform4fv(N,[0,0,0,0]),g}}}({context:this._gl}),this._color=this._parseColor(this.options.color),this._buildingRasterizer=new A(this._gl),this._tileMerger=new rt(this._gl),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(nt(this._compiledKernel,{date:this.options.date}),this._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&&(st(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&&(st(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return r(this,void 0,void 0,(function*(){if(this._map&&this._compiledKernel){if(!1===t)this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),nt(this._compiledKernel,{date:this.options.date});else{const{startDate:r,endDate:i,iterations:n=32}=e;if(this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:n},!(r instanceof Date&&i instanceof Date))throw new Error("Start date or end date or both are missing");if(i.getTime()<r.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield ot(this._compiledKernel,{startDate:r,endDate:i,iterations:n,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:i,height:n}=this._heightMap,o=e.min;return t.map((t=>{const e=x(t,r);return[(e.x-o.x)/i,(e.y-o.y)/n]}))}return[]}_getBounds(t,e){const{width:r,height:i}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(i))return t.getBounds();const n=t.getCenter(),o=x(n,e),a=p(new s(o.x-r/2,o.y-i/2),e),u=p(new s(o.x+r/2,o.y+i/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=ut(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),i=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===i)).map((t=>t.canonical)),[o,a,u,h]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),l=p(new s(512*o,512*a),i+1),c=p(new s(512*(u+1),512*(h+1)),i+1);e=i+1,this._bounds=t.createBounds({nw:l,se:c})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield tt({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,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(at({kernel:this._compiledKernel,map:ut(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield ot(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}toGeoTiff(){const t=(t,e,r,i)=>{const n=new Uint8Array(r*i*4);this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n);const o=r*i*1,s=1*r,a=(i-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<n.length;t+=4){let e;if(this.options.sunExposure.enabled){const r=n.subarray(t,t+3),i=v(r,.5,l)/1e3/60,o=Math.min(Math.floor(i/6),255);e=new Uint8Array([o])}else{e=n[t]+n[t+1]+n[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,i=t(0,0,e,r),n=ut(this._map),{lat:o,lng:s}=n.getBounds().getNorthWest(),a=n.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:i,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 i=this._lngLatToTextureCoords(t.locations),n=((t,e)=>{const r=it({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=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:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:i}));return this._canvas.width=e,this._canvas.height=r,n}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=this._canvas.width,r=this._canvas.height,i=y(t.startDate)-y(t.startDate,t.tzId),n=t.startDate.getTime()-i,o=(t.endDate.getTime()-i-n)/86400/1e3,s=1440;this._canvas.width=o,this._canvas.height=s;const a=[],u=828e5,h=y(new Date(n),t.tzId);for(let e=0;e<o;e++){const r=y(new Date(n+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 i=n+86400*r*1e3+60*t*1e3;e.push(new Date(i+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),i=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:i}))})(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 i=[];for(let e=s-1;e>=0;e--){const n=e*o*4+4*r,s=this.slice(n,n+4);i.push(s.join("")===t.sunColor.join("")?1:0)}e.push(i)}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:i,endDate:n}=this.options.sunExposure,o=n.getTime()-i.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 e(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">© ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._flush()}_flush(){this.emit("idle")}}return void 0!==window.L&&(window.L.shadeMap=(...t)=>new ht(...t)),ht}));
|
|
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,i){return new(r||(r=Promise))((function(n,o){function a(t){try{u(i.next(t))}catch(t){o(t)}}function s(t){try{u(i.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,s)}u((i=i.apply(t,e||[])).next())}))}function i(t,e,r){r=r||2;var i,o,s,u,h,l,m,d=e&&e.length,x=d?e[0]*r:t.length,g=n(t,0,x,r,!0),p=[];if(!g||g.next===g.prev)return p;if(d&&(g=function(t,e,r,i){var o,a,s,u=[];for(o=0,a=e.length;o<a;o++)(s=n(t,e[o]*i,o<a-1?e[o+1]*i:t.length,i,!1))===s.next&&(s.steiner=!0),u.push(_(s));for(u.sort(c),o=0;o<u.length;o++)r=f(u[o],r);return r}(t,e,g,r)),t.length>80*r){i=s=t[0],o=u=t[1];for(var E=r;E<x;E+=r)(h=t[E])<i&&(i=h),(l=t[E+1])<o&&(o=l),h>s&&(s=h),l>u&&(u=l);m=0!==(m=Math.max(s-i,u-o))?32767/m:0}return a(g,p,r,i,o,m,0),p}function n(t,e,r,i,n){var o,a;if(n===U(t,e,r,i)>0)for(o=e;o<r;o+=i)a=b(o,t[o],t[o+1],a);else for(o=r-i;o>=e;o-=i)a=b(o,t[o],t[o+1],a);return a&&E(a,a.next)&&(M(a),a=a.next),a}function o(t,e){if(!t)return t;e||(e=t);var r,i=t;do{if(r=!1,i.steiner||!E(i,i.next)&&0!==p(i.prev,i,i.next))i=i.next;else{if(M(i),(i=e=i.prev)===i.next)break;r=!0}}while(r||i!==e);return e}function a(t,e,r,i,n,c,f){if(t){!f&&c&&function(t,e,r,i){var n=t;do{0===n.z&&(n.z=d(n.x,n.y,e,r,i)),n.prevZ=n.prev,n.nextZ=n.next,n=n.next}while(n!==t);n.prevZ.nextZ=null,n.prevZ=null,function(t){var e,r,i,n,o,a,s,u,h=1;do{for(r=t,t=null,o=null,a=0;r;){for(a++,i=r,s=0,e=0;e<h&&(s++,i=i.nextZ);e++);for(u=h;s>0||u>0&&i;)0!==s&&(0===u||!i||r.z<=i.z)?(n=r,r=r.nextZ,s--):(n=i,i=i.nextZ,u--),o?o.nextZ=n:t=n,n.prevZ=o,o=n;r=i}o.nextZ=null,h*=2}while(a>1)}(n)}(t,i,n,c);for(var m,_,x=t;t.prev!==t.next;)if(m=t.prev,_=t.next,c?u(t,i,n,c):s(t))e.push(m.i/r|0),e.push(t.i/r|0),e.push(_.i/r|0),M(t),t=_.next,x=_.next;else if((t=_)===x){f?1===f?a(t=h(o(t),e,r),e,r,i,n,c,2):2===f&&l(t,e,r,i,n,c):a(o(t),e,r,i,n,c,1);break}}}function s(t){var e=t.prev,r=t,i=t.next;if(p(e,r,i)>=0)return!1;for(var n=e.x,o=r.x,a=i.x,s=e.y,u=r.y,h=i.y,l=n<o?n<a?n:a:o<a?o:a,c=s<u?s<h?s:h:u<h?u:h,f=n>o?n>a?n:a:o>a?o:a,m=s>u?s>h?s:h:u>h?u:h,d=i.next;d!==e;){if(d.x>=l&&d.x<=f&&d.y>=c&&d.y<=m&&x(n,s,o,u,a,h,d.x,d.y)&&p(d.prev,d,d.next)>=0)return!1;d=d.next}return!0}function u(t,e,r,i){var n=t.prev,o=t,a=t.next;if(p(n,o,a)>=0)return!1;for(var s=n.x,u=o.x,h=a.x,l=n.y,c=o.y,f=a.y,m=s<u?s<h?s:h:u<h?u:h,_=l<c?l<f?l:f:c<f?c:f,g=s>u?s>h?s:h:u>h?u:h,E=l>c?l>f?l:f:c>f?c:f,T=d(m,_,e,r,i),v=d(g,E,e,r,i),y=t.prevZ,R=t.nextZ;y&&y.z>=T&&R&&R.z<=v;){if(y.x>=m&&y.x<=g&&y.y>=_&&y.y<=E&&y!==n&&y!==a&&x(s,l,u,c,h,f,y.x,y.y)&&p(y.prev,y,y.next)>=0)return!1;if(y=y.prevZ,R.x>=m&&R.x<=g&&R.y>=_&&R.y<=E&&R!==n&&R!==a&&x(s,l,u,c,h,f,R.x,R.y)&&p(R.prev,R,R.next)>=0)return!1;R=R.nextZ}for(;y&&y.z>=T;){if(y.x>=m&&y.x<=g&&y.y>=_&&y.y<=E&&y!==n&&y!==a&&x(s,l,u,c,h,f,y.x,y.y)&&p(y.prev,y,y.next)>=0)return!1;y=y.prevZ}for(;R&&R.z<=v;){if(R.x>=m&&R.x<=g&&R.y>=_&&R.y<=E&&R!==n&&R!==a&&x(s,l,u,c,h,f,R.x,R.y)&&p(R.prev,R,R.next)>=0)return!1;R=R.nextZ}return!0}function h(t,e,r){var i=t;do{var n=i.prev,a=i.next.next;!E(n,a)&&T(n,i,i.next,a)&&R(n,a)&&R(a,n)&&(e.push(n.i/r|0),e.push(i.i/r|0),e.push(a.i/r|0),M(i),M(i.next),i=t=a),i=i.next}while(i!==t);return o(i)}function l(t,e,r,i,n,s){var u=t;do{for(var h=u.next.next;h!==u.prev;){if(u.i!==h.i&&g(u,h)){var l=A(u,h);return u=o(u,u.next),l=o(l,l.next),a(u,e,r,i,n,s,0),void a(l,e,r,i,n,s,0)}h=h.next}u=u.next}while(u!==t)}function c(t,e){return t.x-e.x}function f(t,e){var r=function(t,e){var r,i=e,n=t.x,o=t.y,a=-1/0;do{if(o<=i.y&&o>=i.next.y&&i.next.y!==i.y){var s=i.x+(o-i.y)*(i.next.x-i.x)/(i.next.y-i.y);if(s<=n&&s>a&&(a=s,r=i.x<i.next.x?i:i.next,s===n))return r}i=i.next}while(i!==e);if(!r)return null;var u,h=r,l=r.x,c=r.y,f=1/0;i=r;do{n>=i.x&&i.x>=l&&n!==i.x&&x(o<c?n:a,o,l,c,o<c?a:n,o,i.x,i.y)&&(u=Math.abs(o-i.y)/(n-i.x),R(i,t)&&(u<f||u===f&&(i.x>r.x||i.x===r.x&&m(r,i)))&&(r=i,f=u)),i=i.next}while(i!==h);return r}(t,e);if(!r)return e;var i=A(r,t);return o(i,i.next),o(r,r.next)}function m(t,e){return p(t.prev,t,e.prev)<0&&p(e.next,t,t.next)<0}function d(t,e,r,i,n){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&((t=(t-r)*n|0)|t<<8))|t<<4))|t<<2))|t<<1))|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=(e-i)*n|0)|e<<8))|e<<4))|e<<2))|e<<1))<<1}function _(t){var e=t,r=t;do{(e.x<r.x||e.x===r.x&&e.y<r.y)&&(r=e),e=e.next}while(e!==t);return r}function x(t,e,r,i,n,o,a,s){return(n-a)*(e-s)>=(t-a)*(o-s)&&(t-a)*(i-s)>=(r-a)*(e-s)&&(r-a)*(o-s)>=(n-a)*(i-s)}function g(t,e){return t.next.i!==e.i&&t.prev.i!==e.i&&!function(t,e){var r=t;do{if(r.i!==t.i&&r.next.i!==t.i&&r.i!==e.i&&r.next.i!==e.i&&T(r,r.next,t,e))return!0;r=r.next}while(r!==t);return!1}(t,e)&&(R(t,e)&&R(e,t)&&function(t,e){var r=t,i=!1,n=(t.x+e.x)/2,o=(t.y+e.y)/2;do{r.y>o!=r.next.y>o&&r.next.y!==r.y&&n<(r.next.x-r.x)*(o-r.y)/(r.next.y-r.y)+r.x&&(i=!i),r=r.next}while(r!==t);return i}(t,e)&&(p(t.prev,t,e.prev)||p(t,e.prev,e))||E(t,e)&&p(t.prev,t,t.next)>0&&p(e.prev,e,e.next)>0)}function p(t,e,r){return(e.y-t.y)*(r.x-e.x)-(e.x-t.x)*(r.y-e.y)}function E(t,e){return t.x===e.x&&t.y===e.y}function T(t,e,r,i){var n=y(p(t,e,r)),o=y(p(t,e,i)),a=y(p(r,i,t)),s=y(p(r,i,e));return n!==o&&a!==s||(!(0!==n||!v(t,r,e))||(!(0!==o||!v(t,i,e))||(!(0!==a||!v(r,t,i))||!(0!==s||!v(r,e,i)))))}function v(t,e,r){return e.x<=Math.max(t.x,r.x)&&e.x>=Math.min(t.x,r.x)&&e.y<=Math.max(t.y,r.y)&&e.y>=Math.min(t.y,r.y)}function y(t){return t>0?1:t<0?-1:0}function R(t,e){return p(t.prev,t,t.next)<0?p(t,e,t.next)>=0&&p(t,t.prev,e)>=0:p(t,e,t.prev)<0||p(t,t.next,e)<0}function A(t,e){var r=new w(t.i,t.x,t.y),i=new w(e.i,e.x,e.y),n=t.next,o=e.prev;return t.next=e,e.prev=t,r.next=n,n.prev=r,i.next=r,r.prev=i,o.next=i,i.prev=o,i}function b(t,e,r,i){var n=new w(t,e,r);return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}function M(t){t.next.prev=t.prev,t.prev.next=t.next,t.prevZ&&(t.prevZ.nextZ=t.nextZ),t.nextZ&&(t.nextZ.prevZ=t.prevZ)}function w(t,e,r){this.i=t,this.x=e,this.y=r,this.prev=null,this.next=null,this.z=0,this.prevZ=null,this.nextZ=null,this.steiner=!1}function U(t,e,r,i){for(var n=0,o=e,a=r-i;o<r;o+=i)n+=(t[a]-t[o])*(t[o+1]+t[a+1]),a=o;return n}function F(t){t.filter((t=>"MultiPolygon"===t.geometry.type)).forEach((e=>{const{geometry:r,properties:i,type:n}=e;if("MultiPolygon"===r.type)for(let i=0;i<r.coordinates.length;i++)t.push(Object.assign(Object.assign({},e),{geometry:Object.assign(Object.assign({},r),{type:"Polygon",coordinates:r.coordinates[i]})}))}));return t.filter((t=>"Polygon"===t.geometry.type)).map((t=>{const{geometry:e,properties:r}=t,{vertices:n}=i.flatten(e.coordinates),o=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}))),a=i(n),s=a.length>256?new Uint16Array(a):new Uint8Array(a),u=function(t){const{height:e=0,levels:r=0,render_height:i=0}=t;if(r)return 3.04*r;return Math.max(e,i)}(r),h=r.highlight||!1;let l=0,c=0,f=0;for(let t=0;t<n.length;t+=2)l+=n[t],c+=n[t+1],f++;return{aPosition:o,cuts:s,buildingHeight:u,centroid:[l/f,c/f],highlight:h}}))}i.deviation=function(t,e,r,i){var n=e&&e.length,o=n?e[0]*r:t.length,a=Math.abs(U(t,0,o,r));if(n)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(U(t,h,l,r))}var c=0;for(s=0;s<i.length;s+=3){var f=i[s]*r,m=i[s+1]*r,d=i[s+2]*r;c+=Math.abs((t[f]-t[d])*(t[m+1]-t[f+1])-(t[f]-t[m])*(t[d+1]-t[f+1]))}return 0===a&&0===c?0:Math.abs((c-a)/a)},i.flatten=function(t){for(var e=t[0][0].length,r={vertices:[],holes:[],dimensions:e},i=0,n=0;n<t.length;n++){for(var o=0;o<t[n].length;o++)for(var a=0;a<e;a++)r.vertices.push(t[n][o][a]);n>0&&(i+=t[n-1].length,r.holes.push(i))}return r};function D(t,e,r){var i=e[1],n=e[0],o=i-n;return t===i&&r?t:((t-n)%o+o)%o+n}function L(t,e){if(!1===e)return t;var r=Math.pow(10,void 0===e?6:e);return Math.round(t*r)/r}var P=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)};function S(t,e,r){this.x=r?Math.round(t):t,this.y=r?Math.round(e):e}var C=Math.trunc||function(t){return t>0?Math.floor(t):Math.ceil(t)};function B(t,e,r){return t instanceof S?t:P(t)?new S(t[0],t[1]):null==t?t:"object"==typeof t&&"x"in t&&"y"in t?new S(t.x,t.y):new S(t,e,r)}function I(t,e){if(t)for(var r=e?[t,e]:t,i=0,n=r.length;i<n;i++)this.extend(r[i])}function N(t,e){return!t||t instanceof I?t:new I(t,e)}function O(t,e){if(t)for(var r=e?[t,e]:t,i=0,n=r.length;i<n;i++)this.extend(r[i])}function X(t,e){return t instanceof O?t:new O(t,e)}S.prototype={clone:function(){return new S(this.x,this.y)},add:function(t){return this.clone()._add(B(t))},_add:function(t){return this.x+=t.x,this.y+=t.y,this},subtract:function(t){return this.clone()._subtract(B(t))},_subtract:function(t){return this.x-=t.x,this.y-=t.y,this},divideBy:function(t){return this.clone()._divideBy(t)},_divideBy:function(t){return this.x/=t,this.y/=t,this},multiplyBy:function(t){return this.clone()._multiplyBy(t)},_multiplyBy:function(t){return this.x*=t,this.y*=t,this},scaleBy:function(t){return new S(this.x*t.x,this.y*t.y)},unscaleBy:function(t){return new S(this.x/t.x,this.y/t.y)},round:function(){return this.clone()._round()},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this},floor:function(){return this.clone()._floor()},_floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this},ceil:function(){return this.clone()._ceil()},_ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this},trunc:function(){return this.clone()._trunc()},_trunc:function(){return this.x=C(this.x),this.y=C(this.y),this},distanceTo:function(t){var e=(t=B(t)).x-this.x,r=t.y-this.y;return Math.sqrt(e*e+r*r)},equals:function(t){return(t=B(t)).x===this.x&&t.y===this.y},contains:function(t){return t=B(t),Math.abs(t.x)<=Math.abs(this.x)&&Math.abs(t.y)<=Math.abs(this.y)},toString:function(){return"Point("+L(this.x)+", "+L(this.y)+")"}},I.prototype={extend:function(t){var e,r;if(!t)return this;if(t instanceof S||"number"==typeof t[0]||"x"in t)e=r=B(t);else if(e=(t=N(t)).min,r=t.max,!e||!r)return this;return this.min||this.max?(this.min.x=Math.min(e.x,this.min.x),this.max.x=Math.max(r.x,this.max.x),this.min.y=Math.min(e.y,this.min.y),this.max.y=Math.max(r.y,this.max.y)):(this.min=e.clone(),this.max=r.clone()),this},getCenter:function(t){return B((this.min.x+this.max.x)/2,(this.min.y+this.max.y)/2,t)},getBottomLeft:function(){return B(this.min.x,this.max.y)},getTopRight:function(){return B(this.max.x,this.min.y)},getTopLeft:function(){return this.min},getBottomRight:function(){return this.max},getSize:function(){return this.max.subtract(this.min)},contains:function(t){var e,r;return(t="number"==typeof t[0]||t instanceof S?B(t):N(t))instanceof I?(e=t.min,r=t.max):e=r=t,e.x>=this.min.x&&r.x<=this.max.x&&e.y>=this.min.y&&r.y<=this.max.y},intersects:function(t){t=N(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>=e.x&&i.x<=r.x,a=n.y>=e.y&&i.y<=r.y;return o&&a},overlaps:function(t){t=N(t);var e=this.min,r=this.max,i=t.min,n=t.max,o=n.x>e.x&&i.x<r.x,a=n.y>e.y&&i.y<r.y;return o&&a},isValid:function(){return!(!this.min||!this.max)},pad:function(t){var e=this.min,r=this.max,i=Math.abs(e.x-r.x)*t,n=Math.abs(e.y-r.y)*t;return N(B(e.x-i,e.y-n),B(r.x+i,r.y+n))},equals:function(t){return!!t&&(t=N(t),this.min.equals(t.getTopLeft())&&this.max.equals(t.getBottomRight()))}},O.prototype={extend:function(t){var e,r,i=this._southWest,n=this._northEast;if(t instanceof z)e=t,r=t;else{if(!(t instanceof O))return t?this.extend(H(t)||X(t)):this;if(e=t._southWest,r=t._northEast,!e||!r)return this}return i||n?(i.lat=Math.min(e.lat,i.lat),i.lng=Math.min(e.lng,i.lng),n.lat=Math.max(r.lat,n.lat),n.lng=Math.max(r.lng,n.lng)):(this._southWest=new z(e.lat,e.lng),this._northEast=new z(r.lat,r.lng)),this},pad:function(t){var e=this._southWest,r=this._northEast,i=Math.abs(e.lat-r.lat)*t,n=Math.abs(e.lng-r.lng)*t;return new O(new z(e.lat-i,e.lng-n),new z(r.lat+i,r.lng+n))},getCenter:function(){return new z((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 z(this.getNorth(),this.getWest())},getSouthEast:function(){return new z(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 z||"lat"in t?H(t):X(t);var e,r,i=this._southWest,n=this._northEast;return t instanceof O?(e=t.getSouthWest(),r=t.getNorthEast()):e=r=t,e.lat>=i.lat&&r.lat<=n.lat&&e.lng>=i.lng&&r.lng<=n.lng},intersects:function(t){t=X(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>=e.lat&&i.lat<=r.lat,a=n.lng>=e.lng&&i.lng<=r.lng;return o&&a},overlaps:function(t){t=X(t);var e=this._southWest,r=this._northEast,i=t.getSouthWest(),n=t.getNorthEast(),o=n.lat>e.lat&&i.lat<r.lat,a=n.lng>e.lng&&i.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,i,n;for(r=1,i=arguments.length;r<i;r++)for(e in n=arguments[r])t[e]=n[e];return t}({},{latLngToPoint:function(t,e){var r=this.projection.project(t),i=this.scale(e);return this.transformation._transform(r,i)},pointToLatLng:function(t,e){var r=this.scale(e),i=this.transformation.untransform(t,r);return this.projection.unproject(i)},project:function(t){return this.projection.project(t)},unproject:function(t){return this.projection.unproject(t)},scale:function(t){return 256*Math.pow(2,t)},zoom:function(t){return Math.log(t/256)/Math.LN2},getProjectedBounds:function(t){if(this.infinite)return null;var e=this.projection.bounds,r=this.scale(t);return new I(this.transformation.transform(e.min,r),this.transformation.transform(e.max,r))},infinite:!1,wrapLatLng:function(t){var e=this.wrapLng?D(t.lng,this.wrapLng,!0):t.lng;return new z(this.wrapLat?D(t.lat,this.wrapLat,!0):t.lat,e,t.alt)},wrapLatLngBounds:function(t){var e=t.getCenter(),r=this.wrapLatLng(e),i=e.lat-r.lat,n=e.lng-r.lng;if(0===i&&0===n)return t;var o=t.getSouthWest(),a=t.getNorthEast();return new O(new z(o.lat-i,o.lng-n),new z(a.lat-i,a.lng-n))}},{wrapLng:[-180,180],R:6371e3,distance:function(t,e){var r=Math.PI/180,i=t.lat*r,n=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(i)*Math.cos(n)*a*a,u=2*Math.atan2(Math.sqrt(s),Math.sqrt(1-s));return this.R*u}});function z(t,e,r){if(isNaN(t)||isNaN(e))throw new Error("Invalid LatLng object: ("+t+", "+e+")");this.lat=+t,this.lng=+e,void 0!==r&&(this.alt=+r)}function H(t,e,r){return t instanceof z?t:P(t)&&"object"!=typeof t[0]?3===t.length?new z(t[0],t[1],t[2]):2===t.length?new z(t[0],t[1]):null:null==t?t:"object"==typeof t&&"lat"in t?new z(t.lat,"lng"in t?t.lng:t.lon,t.alt):void 0===e?null:new z(t,e,r)}z.prototype={equals:function(t,e){return!!t&&(t=H(t),Math.max(Math.abs(this.lat-t.lat),Math.abs(this.lng-t.lng))<=(void 0===e?1e-9:e))},toString:function(t){return"LatLng("+L(this.lat,t)+", "+L(this.lng,t)+")"},distanceTo:function(t){return G.distance(this,H(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 z(this.lat,this.lng,this.alt)}};const W=256,j=.40909994067971484,Z=t=>{const e=t.valueOf()/864e5-10957.5,r=6.240059966692059+.017201969994578018*e,i=r+.017453292519943295*(1.9148*Math.sin(r)+.02*Math.sin(2*r)+3e-4*Math.sin(3*r))+1.796593062783907+Math.PI,n=Math.atan2(Math.sin(i)*Math.cos(j),Math.cos(i));return{dec:Math.asin(Math.sin(j)*Math.sin(i)),Hi:(4.889714432387314+6.3003876824396166*e-n)%(2*Math.PI)+2*Math.PI}},Y=(t,e,r)=>{const i=1/e,n=Math.min(t[0]*i,255),o=Math.min(t[1]*i,255),a=Math.min(t[2]*i,255);let s=0;return n+o+a!==0&&(s=n>0?n/255*.5+.5:a>0?.5*(1-a/255):.5),s*r},K=(t=new Date,e)=>{const r=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),i=new Date(t.toLocaleString("en-US",e?{timeZone:e}:{}));return r.getTime()-i.getTime()};const k=(t,e)=>{const{lat:r,lng:i}=t;return new S(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(i,e),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*W)(r,e))},q=(t,e)=>{return new z((r=t.y,i=e,n=Math.PI-2*Math.PI*r/W/Math.pow(2,i),180/Math.PI*Math.atan(.5*(Math.exp(n)-Math.exp(-n)))),function(t,e){return t/W/Math.pow(2,e)*360-180}(t.x,e));var r,i,n};let V,$={heightMapTex:null,width:0,height:0,visibleDEMPixelBounds:new I(new S(0,0),new S(0,0)),DEMPixelBounds:new I(new S(0,0),new S(0,0)),maxHeight:8848,raster:[],demZoom:0,dirty:!1,outputWidth:0,outputHeight:0};const J=t=>r(void 0,void 0,void 0,(function*(){const{demZoom:e,getFeatures:i,terrainSource:n,dsmSource:o,tileLoaded:a,gl:s,bounds:u,buildingRasterizer:h,tileMerger:l,forceUpdate:c=!1}=t,{getSourceUrl:f,getElevation:m,maxZoom:d,tileSize:_,_overzoom:x}=n,g=(p={getFeatures:i},r(void 0,void 0,void 0,(function*(){const{getFeatures:t}=p;try{return F(yield t())}catch(t){console.log("Error merging buildings",t)}return[]})));var p;try{const t=u,r=t.getNorthWest(),i=t.getSouthEast(),n=new I(k(r,e),k(i,e));let x=new S(n.min.x,n.min.y),p=n.max.x-n.min.x;const E=n.max.y-n.min.y,T=n.max.subtract(n.min);T.y>T.x&&(x.x-=W,p+=512);const v=(t=>{const{upperLeft:e,width:r,height:i}=t,n=e.divideBy(W).floor().multiplyBy(W),o=(Math.ceil(r/W)+1)*W,a=(Math.ceil(i/W)+1)*W,s=n.add([o,a]);return new I(n,s)})({upperLeft:x,width:p,height:E}),{x:y,y:R}=v.max.subtract(v.min),A=(t=>{t.sort(((t,e)=>t.y!==e.y?t.y-e.y:t.x-e.x));const e=t.reduce(((t,e)=>e.x<t.x?e:t)).x,r=t.reduce(((t,e)=>e.y<t.y?e:t)).y;return t.map((t=>{const i=Math.pow(2,t.z);return{x:(t.x%i+i)%i,y:(t.y%i+i)%i,z:t.z,xOffset:(t.x-e)*W,yOffset:(t.y-r)*W}}))})((t=>{const{upperLeft:e,width:r,height:i,zoom:n}=t,o=e.divideBy(W).floor(),a=o.x+r/W,s=Math.min(o.y+i/W,Math.pow(2,n)-1),u=[];for(var h=o.x;h<a;h++)for(var l=o.y;l<s;l++)u.push({x:h,y:l,z:n});return u})({upperLeft:v.min,width:y,height:R,zoom:e})),b=n.max.x-n.min.x,M=n.max.y-n.min.y,w=Math.round(b),U=Math.round(M),F=JSON.stringify(A);if(!c&&F===V&&e<d)return $=Object.assign(Object.assign({},$),{outputWidth:w,outputHeight:U,visibleDEMPixelBounds:n,demZoom:e,dirty:!0}),$;const D=yield l.merge(A,{maxZoom:d,width:y,height:R,crossOrigin:"Anonymous",getSourceUrl:f,getElevation:m,tileSize:_,tileLoaded:a});if(null===D)return $=Object.assign(Object.assign({},$),{visibleDEMPixelBounds:n,demZoom:e,dirty:!1}),$;const L=new I(k(new O(o.bounds).getNorthWest(),e),k(new O(o.bounds).getSouthEast(),e)),P=L.min.subtract(v.min),C=L.max.subtract(v.min),B=[P.x/y,P.y/R,C.x/y,C.y/R].map((t=>2*t-1)),N=yield g,{maxHeight:X,heightMapTex:G}=h.raster({upperLeftTile:A[0],width:y,height:R,mapZoom:e,features:N,imageData:D,gl:s,dsmSource:o,dsmCoords:B}),z=Math.max(Math.max(o.maxHeight,X),D.maxHeight);V=F,$={heightMapTex:G,maxHeight:z,width:y,height:R,DEMPixelBounds:v,visibleDEMPixelBounds:n,raster:A,demZoom:e,dirty:!0,outputWidth:w,outputHeight:U}}catch(t){console.error("Could not decode height map",t)}return $})),Q=(t,e)=>{const{r:r,g:i,b:n}=t;return[r/255,i/255,n/255,e]},tt=(t,e)=>{const{date:r}=e,{dec:i,Hi:n}=Z(r);t.updateDate({dec:i,Hi:n})},et=(t,e)=>r(void 0,void 0,void 0,(function*(){return yield t.updateDateRange(Object.assign({},e))})),rt=(t,e)=>{const{color:r,opacity:i}=e,n=Q(r,i);t.updateColor({colorVec:n})},it=t=>{const{kernel:e,map:r,heightMap:i,now:n,color:o,opacity:a,belowCanopy:s,skipRender:u}=t;try{const{heightMapTex:t,outputHeight:h,outputWidth:l,maxHeight:c,width:f,height:m,DEMPixelBounds:d,visibleDEMPixelBounds:_,demZoom:x}=i;if(0===f||0===m)return;const{min:g,max:p}=_;if(!g||!p)return;const{x:E,y:T}=p.subtract(g),v=d.min,y=r.getPixelDimensions(),R=r.screenUnproject([0,0]),A=r.screenUnproject([y.width,0]),b=r.screenUnproject([y.width,y.height]),M=r.screenUnproject([0,y.height]),w=[M,b,R,A].map((t=>r.project(t,x))),U=w.map((t=>[(t.x-v.x)/f,(t.y-v.y)/m])).flat(),F=w.map((t=>[(t.x-g.x)/E,(t.y-g.y)/T])).map((t=>[2*t[0]-1,(2*t[1]-1)*(r.isLeaflet()?-1:1)])).flat(),D=v.y/W/Math.pow(2,x),L=m/W/Math.pow(2,x),P=new O(q(d.getTopLeft(),x),q(d.getBottomRight(),x)),S=P.getWest(),C=Math.abs(P.getWest()-P.getEast());((t,e)=>{const{color:r,opacity:i,date:n}=e,o=Q(r,i),{dec:a,Hi:s}=Z(n);t.updateHeightMap(Object.assign({dec:a,Hi:s,colorVec:o,step:1},e))})(e,{heightMapTex:t,maxHeight:c,width:f,height:m,heightMapZoom:x,cornerTextureCoords:U,cornerClipCoords:F,topYCoord:D,ySize:L,west:S,dLng:C,date:n,color:o,opacity:a,outputHeight:h,outputWidth:l,belowCanopy:s,skipRender:u})}catch(t){console.error("EXCEPTION",t)}};const nt=t=>{const e=()=>!t.getPitch;return{project:(r,i)=>{if(e())return t.project(r,i);{const{lat:t,lng:e}=r;return new S(((t,e)=>(t+180)/360*Math.pow(2,e)*W)(e,i),((t,e)=>(1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e)*W)(t,i))}},unproject:(r,i)=>{return e()?t.unproject(r,i):new z((n=r.y,o=i,a=Math.PI-2*Math.PI*n/W/Math.pow(2,o),180/Math.PI*Math.atan(.5*(Math.exp(a)-Math.exp(-a)))),function(t,e){return t/W/Math.pow(2,e)*360-180}(r.x,i));var n,o,a},screenUnproject:r=>e()?t.containerPointToLatLng(r):t.unproject(r),getZoom:()=>e()?t.getZoom():t.getZoom()+1,getCenter:()=>t.getCenter(),getBounds:()=>t.getBounds(),eachLayer:r=>{e()&&t.eachLayer(r)},getBearing:()=>e()?0:t.getBearing(),getPitch:()=>e()?0:t.getPitch(),rawMap:()=>t,isLeaflet:e,getPixelDimensions:()=>{const e=t.getContainer();return{width:e.clientWidth,height:e.clientHeight}},createBounds:t=>{const{nw:e,se:r}=t;return new O(e,r)}}};const ot=t=>{const{gl:e}=t,{texture:r,imageData:i=null,format:n=e.RGBA,width:o,height:a,filter:s=e.NEAREST,wrap:u=e.CLAMP_TO_EDGE,type:h=e.UNSIGNED_BYTE,internalFormat:l=n}=t;e.bindTexture(e.TEXTURE_2D,r),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,s),e.texImage2D(e.TEXTURE_2D,0,l,o,a,0,n,h,i)};function at(t){const{gl:e,vSrc:r,fSrc:i}=t,n=e.createShader(e.VERTEX_SHADER);e.shaderSource(n,r),e.compileShader(n);const o=e.createShader(e.FRAGMENT_SHADER);e.shaderSource(o,i),e.compileShader(o);const a=e.createProgram();return e.attachShader(a,n),e.attachShader(a,o),e.linkProgram(a),a}class st{constructor(t){this.gl=t,this.program=at({gl:t,vSrc:"\nuniform vec3 xyz;\nuniform vec2 dimensions;\nuniform bool use_dsm;\nattribute vec2 dsm_position;\nattribute vec2 a_position;\nfloat PI = 3.141592653589793;\nvarying vec2 vCoord;\nvarying float v_use_dsm;\n\n\tvoid main() {\n\t\tif (use_dsm == true) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = dsm_position;\n v_use_dsm = 1.0;\n\t\t\treturn;\n\t\t}\n\t\tif (abs(a_position) == vec2(1,1)) {\n\t\t\tgl_Position = vec4(a_position, 0, 1);\n\t\t\tvCoord = a_position * 0.5 + 0.5;\n\t\t\treturn;\n\t\t}\n\t\tfloat x = ((a_position.x + 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;\nvarying float v_use_dsm;\n\nvoid main() {\n\tif (color == vec4(0,0,0,0)) {\n\t \tvec4 textureColor = texture2D(height_map, vec2(vCoord.x, vCoord.y));\n if (v_use_dsm == 1.0) {\n\t \t gl_FragColor = vec4(0.0, 0.0, textureColor.b, textureColor.a);\n if (gl_FragColor.zw == vec2(0.0)) {\n discard;\n }\n } else {\n gl_FragColor = vec4(textureColor.b, textureColor.a, textureColor.b, textureColor.a);\n }\n // DSM noData values should not be included in heightMap because they produce ugly borders\n\t\treturn;\n\t} \n\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:i,width:n,height:o,imageData:a,dsmCoords:s,dsmSource:u,gl:h}=t,{x:l,y:c,z:f}=r;let m=0;h.useProgram(this.program),h.activeTexture(h.TEXTURE1);const d=h.createTexture();h.bindTexture(h.TEXTURE_2D,d),h.pixelStorei(h.UNPACK_ALIGNMENT,2),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST),h.texImage2D(h.TEXTURE_2D,0,h.LUMINANCE_ALPHA,n,o,0,h.LUMINANCE_ALPHA,h.UNSIGNED_BYTE,a),h.pixelStorei(h.UNPACK_ALIGNMENT,4);const _=n,x=o;h.activeTexture(h.TEXTURE0),h.bindTexture(h.TEXTURE_2D,this.targetTexture),h.texImage2D(h.TEXTURE_2D,0,h.RGBA,_,x,0,h.RGBA,h.UNSIGNED_BYTE,null),i>15&&e.length>5?(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST)):(h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.LINEAR),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.LINEAR)),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.viewport(0,0,_,x);const g=h.createFramebuffer();h.bindFramebuffer(h.FRAMEBUFFER,g);const p=h.COLOR_ATTACHMENT0;h.framebufferTexture2D(h.FRAMEBUFFER,p,h.TEXTURE_2D,this.targetTexture,0),h.disable(h.BLEND),h.bindBuffer(h.ARRAY_BUFFER,this.positionBuffer),h.bindBuffer(h.ELEMENT_ARRAY_BUFFER,this.indexBuffer),h.clearColor(0,0,0,0),h.clear(h.COLOR_BUFFER_BIT);const E=l*W,T=c*W,v=new S(E,T);h.uniform3f(this.xyzUniformLocation,E,T,f),h.uniform2f(this.dimensionsUniformLocation,n,o),h.uniform1i(this.heightMapUniformLocation,1),h.uniform1i(this.useDSMUniformLocation,0);const y=new Float32Array([-1,-1,1,-1,-1,1,1,1]);if(h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,y,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,0,0,0,0),h.drawArrays(h.TRIANGLE_STRIP,0,4),e.forEach((t=>{const{buildingHeight:e,aPosition:r,cuts:i,centroid:s,highlight:u}=t,l=k({lng:s[0],lat:s[1]},f).subtract(v).floor();if(l.x<0||l.y<0||l.x>n||l.y>o)return;const c=256*a[l.y*n*2+2*l.x]+a[l.y*n*2+2*l.x+1]+5*e,d=Math.floor(c/256)/255,_=Math.floor(c%256)/255;m=Math.max(m,c/5),h.uniform4f(this.colorUniformLocation,d,_,d,_),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ARRAY_BUFFER,r,h.DYNAMIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.bufferData(h.ELEMENT_ARRAY_BUFFER,i,h.DYNAMIC_DRAW),h.drawElements(h.TRIANGLES,i.length,i.length>256?h.UNSIGNED_SHORT:h.UNSIGNED_BYTE,0)})),h.deleteTexture(d),0!==u.data.length){h.activeTexture(h.TEXTURE1);const t=h.createTexture();h.bindTexture(h.TEXTURE_2D,t),h.pixelStorei(h.UNPACK_ALIGNMENT,2),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MIN_FILTER,h.NEAREST),h.texParameteri(h.TEXTURE_2D,h.TEXTURE_MAG_FILTER,h.NEAREST),h.texImage2D(h.TEXTURE_2D,0,h.LUMINANCE_ALPHA,u.width,u.height,0,h.LUMINANCE_ALPHA,h.UNSIGNED_BYTE,u.data),h.pixelStorei(h.UNPACK_ALIGNMENT,4);const e=[s[0],s[3],s[2],s[3],s[0],s[1],s[2],s[1]],r=new Float32Array(e);h.enableVertexAttribArray(this.positionAttributeLocation),h.bufferData(h.ARRAY_BUFFER,r,h.STATIC_DRAW),h.vertexAttribPointer(this.positionAttributeLocation,2,h.FLOAT,!1,0,0),h.enableVertexAttribArray(this.dsmAttributeLocation),h.bindBuffer(h.ARRAY_BUFFER,this.dsmBuffer),h.bufferData(h.ARRAY_BUFFER,new Float32Array([0,1,1,1,0,0,1,0]),h.STATIC_DRAW),h.vertexAttribPointer(this.dsmAttributeLocation,2,h.FLOAT,!1,0,0),h.uniform4f(this.colorUniformLocation,0,0,0,0),h.uniform1i(this.useDSMUniformLocation,1),h.colorMask(!1,!1,!0,!0),h.drawArrays(h.TRIANGLE_STRIP,0,4),h.deleteTexture(t)}return h.colorMask(!0,!0,!0,!0),h.deleteFramebuffer(g),{maxHeight:m,heightMapTex:this.targetTexture}}}const ut=t=>{const{tile:e,maxZoom:r,tileSize:i}=t;let{x:n,y:o,z:a}=e;if(a>r){const t=Math.pow(2,e.z-r);n=Math.floor(n/t),o=Math.floor(o/t),a=r}return{x:i===W?n:Math.floor(n/2),y:i===W?o:Math.floor(o/2),z:i===W?a:a-1}};class ht{constructor(t){this.gl=t,this.program=at({vSrc:"\n attribute vec4 a_position;\n varying vec2 v_pos;\n void main() {\n v_pos = ((a_position + 1.0) * 0.5).xy;\n gl_Position = a_position;\n }\n",fSrc:"\n precision mediump float;\n varying vec2 v_pos;\n uniform sampler2D u_texture;\n void main() {\n gl_FragColor = texture2D(u_texture, v_pos);\n }\n",gl:t}),t.useProgram(this.program),this.positionAttributeLocation=t.getAttribLocation(this.program,"a_position"),this.positionBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.positionBuffer),t.bufferData(t.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),t.STATIC_DRAW),t.enableVertexAttribArray(this.positionAttributeLocation),t.vertexAttribPointer(this.positionAttributeLocation,2,t.FLOAT,!1,0,0),this.tileTexture=t.createTexture(),t.bindTexture(t.TEXTURE_2D,this.tileTexture),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.NEAREST),this.outputTexture=t.createTexture(),this.framebuffer=t.createFramebuffer(),this.inProgress=[],this.finished=0}merge(t,e){return r(this,void 0,void 0,(function*(){const i=this.gl,{width:n,height:o,crossOrigin:a,getSourceUrl:s,getElevation:u,tileSize:h,tileLoaded:l,maxZoom:c}=e;if(i){const e=new Uint8ClampedArray(2*n*o);e.maxHeight=0,this.inProgress.forEach((t=>t.src="")),this.inProgress=[],this.finished=0;const f=new Set;t.forEach((t=>{f.add(s(ut({tile:t,maxZoom:c,tileSize:h})))}));const m=Array.from(f).map((o=>r(this,void 0,void 0,(function*(){return new Promise(((r,l)=>{const f=new Image;this.inProgress.push(f),f.onload=()=>{t.filter((t=>o===s(ut({tile:t,maxZoom:c,tileSize:h})))).forEach((t=>{let r=0,o=0;const a=514===h?1:0;let s=0,l=W,m=W;if(h!==W&&(r=t.x%2*W,o=t.y%2*W,l=W,m=W),t.z>c){const e=Math.pow(2,t.z-c);h!==W&&(r=Math.floor(t.x/e)%2*W,o=Math.floor(t.y/e)%2*W),r+=W*(t.x%e/e),o+=W*(t.y%e/e),l=W/e,m=W/e,s=514===h?1:0}i.useProgram(this.program),i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,this.tileTexture),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,i.RGBA,i.UNSIGNED_BYTE,f),i.activeTexture(i.TEXTURE1),ot({gl:i,texture:this.outputTexture,imageData:null,format:i.RGBA,width:h,height:h,wrap:i.CLAMP_TO_EDGE,filter:i.NEAREST}),i.bindFramebuffer(i.FRAMEBUFFER,this.framebuffer),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,this.outputTexture,0);const d=i.checkFramebufferStatus(i.FRAMEBUFFER);d!==i.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+d),i.bindBuffer(i.ARRAY_BUFFER,this.positionBuffer),i.enableVertexAttribArray(this.positionAttributeLocation),i.vertexAttribPointer(this.positionAttributeLocation,2,i.FLOAT,!1,0,0),i.viewport(0,0,h,h),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLE_STRIP,0,4);const _=new Uint8Array(4*(l+s)*(m+s));i.readPixels(a+r,a+o,l+s,m+s,i.RGBA,i.UNSIGNED_BYTE,_),i.bindFramebuffer(i.FRAMEBUFFER,null);const x=new Uint8ClampedArray(131072);let g=new Array((l+s)*(m+s)),p=W/l;for(let t=0;t<_.length;t+=4){const r=u({r:_[t],g:_[t+1],b:_[t+2],a:_[t+3]});e.maxHeight=Math.max(r,e.maxHeight);const i=5*(r||0);g[t/4]=i}if(p>1&&514===h)for(g=function(t,e,r,i){const n=Math.floor(e*i),o=Math.floor(r*i),a=[];for(let s=0;s<o;s++)for(let o=0;o<n;o++){const n=o/i,u=s/i,h=Math.floor(n),l=Math.floor(u),c=Math.min(h+1,e-1),f=Math.min(l+1,r-1),m=n-h,d=u-l,_=l*e+c,x=f*e+h,g=f*e+c,p=t[l*e+h]*(1-m)*(1-d)+t[_]*m*(1-d)+t[x]*(1-m)*d+t[g]*m*d;a.push(p)}return function(t,e,r,i,n){if(i>=e||n>=r)return[];const o=e-i,a=r-n,s=[];for(let r=0;r<a;r++)for(let i=0;i<o;i++){const n=r*e+i;s.push(t[n])}return s}(a,Math.sqrt(a.length),Math.sqrt(a.length),i,i)}(g,l+1,m+1,p);p>1;)p/=2,m*=2,l*=2;for(let t=0;t<x.length;t+=2){const e=g[Math.floor(t/2/W/p)*l+Math.floor(t/2%W/p)],r=Math.floor(e/256),i=Math.floor(e%256);x[t]=r,x[t+1]=i}for(let r=0;r<W;r++)e.set(x.slice(r*W*2,(r+1)*W*2),2*(t.yOffset*n+r*n+t.xOffset))})),r(null)},f.onerror=t=>{if(f.src!==f.originalSource)return l("new tiles requested");r(null)},f.crossOrigin=a||null,f.src=o,f.originalSource=f.src})).then((()=>{this.finished++,l(this.finished,this.inProgress.length)}))}))));try{yield Promise.all(m)}catch(t){return console.log(`${m.length} requests aborted`,t),null}return this.inProgress=[],e}throw new Error("Could not get canvas context for merging tile images")}))}}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:i}=t;return 256*e+r+i/256-32768}},dsmSource:{bounds:[{lat:0,lng:0},{lat:0,lng:0}],data:new Uint8ClampedArray,width:0,height:0,maxHeight:0},belowCanopy:!1,getFeatures:()=>Promise.resolve([]),getSize:()=>({width:Number.NaN,height:Number.NaN}),debug:t=>{}};const e=t[0];if(this.options=Object.assign(this.options,e),!this.options.apiKey)throw new Error("Could not load ShadeMap: apiKey missing");fetch("https://shademap.app/sdk/load",{method:"POST",body:JSON.stringify({api_key:this.options.apiKey}),headers:{"Content-Type":"application/json"}}).then((t=>r(this,void 0,void 0,(function*(){if(200!==t.status)throw new Error(yield t.text())})))).catch((t=>r(this,void 0,void 0,(function*(){throw new Error(`Could not load ShadeMap API. Key: ${this.options.apiKey} Error: ${t}`)})))),this._canvas=document.createElement("canvas"),this._color=this._parseColor(this.options.color),this._reset=this._reset.bind(this),this._draw=this._draw.bind(this)}onRemove(){return this._map&&this._map.off("moveend",this._reset),this}setDate(t){return this.options.date.getTime()!==t.getTime()?(this.options.date=t,this._compiledKernel&&(tt(this._compiledKernel,{date:this.options.date}),this._flush())):this.emit("idle"),this}_setDateForTimezone(t,e){let r=0;e&&(r=K(this.options.date)-K(this.options.date,e)),this.setDate(new Date(t.getTime()-r))}setColor(t){return this.options.color!==t&&(this.options.color=t,this._color=this._parseColor(this.options.color),this._compiledKernel&&(rt(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&&(rt(this._compiledKernel,{color:this._color,opacity:this.options.opacity}),this._flush())),this}setBelowCanopy(t){return this.options.belowCanopy=t,this._heightMap?this._draw(this._heightMap):this._reset(),this}setTerrainSource(t){return this.options.terrainSource=t,delete this._heightMap,this._reset(),this}setDSMSource(t){return this.options.dsmSource=t,delete this._heightMap,this._reset(),this}setSunExposure(t=!1,e){return r(this,void 0,void 0,(function*(){if(this.options.sunExposure=Object.assign(Object.assign({},this.options.sunExposure),{enabled:t}),e){const{startDate:r=new Date,endDate:i=new Date,iterations:n=32}=e;this.options.sunExposure={enabled:t,startDate:r,endDate:i,iterations:n}}if(this._map&&this._compiledKernel&&this._heightMap){if(!1===t)tt(this._compiledKernel,{date:this.options.date});else{const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!(t instanceof Date&&e instanceof Date))throw new Error("Start date or end date or both are missing");if(e.getTime()<t.getTime())throw new Error("End date must come after the start date to calculate sun exposure");if(!0===(yield et(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._flush()}return this}))}_lngLatToTextureCoords(t){if(this._heightMap){const{DEMPixelBounds:e,demZoom:r,width:i,height:n}=this._heightMap,o=e.min;return t.map((t=>{const e=k(t,r);return[(e.x-o.x)/i,(e.y-o.y)/n]}))}return[]}_getBounds(t,e){const{width:r,height:i}=this.options.getSize();if(Number.isNaN(r)||Number.isNaN(i))return t.getBounds();const n=t.getCenter(),o=k(n,e),a=q(new S(o.x-r/2,o.y-i/2),e),s=q(new S(o.x+r/2,o.y+i/2),e);return t.createBounds({nw:a,se:s})}_getDEMZoom(t){const e=Math.min(this.options.terrainSource._overzoom||this.options.terrainSource.maxZoom,t.getZoom());return Math.round(e)}_reset(){return r(this,void 0,void 0,(function*(){if(this.options.debug("_reset()"),this._map){const t=nt(this._map);let e=this._getDEMZoom(t);try{this._bounds=this._getBounds(t,e)}catch(t){return console.error("Invalid bounds returned: ",t),this}try{if(!t.isLeaflet()&&this._map.getPitch()>45){yield new Promise((t=>{this._map.loaded()?t(!0):this._map.once("idle",(function(){t(!0)}))}));const r=this._map.style._sourceCaches["other:composite"].getVisibleCoordinates().reverse(),i=r.reduce(((t,e)=>Math.max(t,e.canonical.z)),Number.MIN_SAFE_INTEGER),n=r.filter((t=>t.canonical.z===i)).map((t=>t.canonical)),[o,a,s,u]=n.reduce(((t,e)=>[Math.min(t[0],e.x),Math.min(t[1],e.y),Math.max(t[2],e.x),Math.max(t[3],e.y)]),[Number.MAX_SAFE_INTEGER,Number.MAX_SAFE_INTEGER,Number.MIN_SAFE_INTEGER,Number.MIN_SAFE_INTEGER]),h=q(new S(512*o,512*a),i+1),l=q(new S(512*(s+1),512*(u+1)),i+1);e=i+1,this._bounds=t.createBounds({nw:h,se:l})}}catch(t){console.log("Mapbox tile optimization failed",t)}const r=yield J({gl:this._gl,demZoom:e,bounds:this._bounds,terrainSource:this.options.terrainSource,dsmSource:this.options.dsmSource,getFeatures:this.options.getFeatures,buildingRasterizer:this._buildingRasterizer,tileMerger:this._tileMerger,tileLoaded:(t,e)=>this.emit("tileloaded",t,e),forceUpdate:!this._heightMap});this._heightMap=r,r.dirty&&(yield this._draw(r))}return this}))}_draw(t){return r(this,void 0,void 0,(function*(){if(this.options.debug("_draw()"),this._canvas&&this._compiledKernel&&this._map){if(it({kernel:this._compiledKernel,map:nt(this._map),heightMap:t,color:this._color,belowCanopy:this.options.belowCanopy,opacity:this.options.opacity,now:this.options.date,maxZoom:this.options.terrainSource.maxZoom,skipRender:this.options.sunExposure.enabled}),this.options.sunExposure.enabled){const{startDate:t,endDate:e,iterations:r}=this.options.sunExposure;if(!0===(yield et(this._compiledKernel,{startDate:t,endDate:e,iterations:r,emit:this.emit.bind(this)})))return this}this._bounds&&this._repositionCanvas(this._bounds)}return this}))}readPixel(t,e){const r=new Uint8Array(4);return this._gl&&this._gl.readPixels(t,e,1,1,this._gl.RGBA,this._gl.UNSIGNED_BYTE,r),r}readPixels(t,e,r,i){const n=new Uint8Array(r*i*4);return this._gl&&this._gl.readPixels(t,e,r,i,this._gl.RGBA,this._gl.UNSIGNED_BYTE,n),n}toGeoTiff(){const t=(t,e)=>{const r=this.readPixels(0,0,t,e),i=t*e*1,n=1*t,o=(e-1)*n,a=new Uint8Array(i),s=new Uint8Array(i);let u=0;const{startDate:h,endDate:l}=this.options.sunExposure;this.options.sunExposure.enabled&&h&&l&&(u=l.getTime()-h.getTime());for(let t=0;t<r.length;t+=4){let e;if(this.options.sunExposure.enabled){const i=r.subarray(t,t+3),n=Y(i,.5,u)/1e3/60,o=Math.min(Math.floor(n/6),255);e=new Uint8Array([o])}else{e=r[t]+r[t+1]+r[t+2]===0?new Uint8Array([255]):new Uint8Array([0])}a.set(e,t/4*1)}if(this._map&&!nt(this._map).isLeaflet())return a;for(let t=0;t<i;t+=n)s.set(a.subarray(t,t+n),o-t);return s};if(this._map&&this._heightMap){const e=this._heightMap.outputWidth,r=this._heightMap.outputHeight,i=t(e,r),n=nt(this._map),{lat:o,lng:a}=n.getBounds().getNorthWest(),s=n.getBounds().getSouthEast(),u=[0,0,0,a,o,0],h=[(s.lng-a)/e,(o-s.lat)/r,0];return this.options.sunExposure.enabled,{data:i,metadata:{width:e,height:r,ModelTiepoint:u,ModelPixelScale:h,GeographicTypeGeoKey:4326,GeogCitationGeoKey:"WGS 84"}}}return null}_generateShadeProfile(t){if(this._compiledKernel){const e=this._lngLatToTextureCoords(t.locations);return((t,e)=>{const r=Q({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=Q({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{texCoords:e}))}return new Uint8Array}_generateLocationShadeProfile(t){if(this._compiledKernel){const e=K(t.startDate)-K(t.startDate,t.tzId),r=t.startDate.getTime()-e,i=(t.endDate.getTime()-e-r)/86400/1e3,n=1440,o=[],a=828e5,s=K(new Date(r),t.tzId);for(let e=0;e<i;e++){const i=K(new Date(r+86400*e*1e3+a),t.tzId)-s;o[e]=i}const u=[];for(let t=0;t<n;t++){const e=[];for(let n=0;n<i;n++){const i=r+86400*n*1e3+60*t*1e3;e.push(new Date(i+o[n]))}u.push(e)}const h=this._lngLatToTextureCoords([t.location])[0],l=((t,e)=>{const r=Q({r:e.sunColor[0],g:e.sunColor[1],b:e.sunColor[2]},1),i=Q({r:e.shadeColor[0],g:e.shadeColor[1],b:e.shadeColor[2]},1);return t.generateLocationShadeProfile(Object.assign(Object.assign({},e),{sunColor:r,shadeColor:i}))})(this._compiledKernel,Object.assign(Object.assign({},t),{dates:u,texCoord:h}));return l.toArray=function(){const e=new Array;for(let r=0;r<i;r++){const o=[];for(let e=n-1;e>=0;e--){const n=e*i*4+4*r,a=this.slice(n,n+4);o.push(a.join("")===t.sunColor.join("")?1:0)}e.push(o)}return e},{data:l,width:i,height:n}}return{data:new Uint8Array,width:0,height:0}}getHoursOfSun(t,e){if(this.options.sunExposure.enabled){const r=this.readPixel(t,e),{startDate:i,endDate:n}=this.options.sunExposure,o=n.getTime()-i.getTime(),a=Y(r,.5,o);return Math.abs(a/1e3/3600)}return 0}_repositionCanvas(t){}_flush(){}flushSync(){this._gl&&this._gl.finish()}_parseColor(t){t=t.replace("#","");const e={r:0,g:0,b:0};return/^([0-9A-F]{3}){1,2}$/i.test(t)&&(3===t.length?(e.r=parseInt(t[0]+t[0],16),e.g=parseInt(t[1]+t[1],16),e.b=parseInt(t[2]+t[2],16)):6===t.length&&(e.r=parseInt(t[0]+t[1],16),e.g=parseInt(t[2]+t[3],16),e.b=parseInt(t[4]+t[5],16))),e}}{constructor(t){super(t);const e={preserveDrawingBuffer:!0,antialias:!1};this._gl=this._canvas.getContext("webgl",e)||this._canvas.getContext("experimental-webgl",e),this._canvas.width=window.innerWidth,this._canvas.height=window.innerHeight,this._compiledKernel=function(t){const{context:e,setRenderBuffer:i}=t,n=e,o=at({gl:n,vSrc:"precision lowp float;precision lowp int;precision lowp sampler2D;attribute vec2 a_pos;attribute vec2 a_tex_pos;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;void main(void){gl_Position=vec4(a_pos,0,1);vTexCoordFull=(gl_Position*0.5+0.5).xy;vTexCoordCropped=a_tex_pos;}",fSrc:"#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nprecision lowp int;precision lowp sampler2D;float atan2(float v1,float v2){if(v1==0.0||v2==0.0)return 0.0;return atan(v1,v2);}float _pow(float v1,float v2){if(v2==0.0)return 1.0;return pow(v1,v2);}float integerMod(float x,float y){float res=floor(mod(x,y));return res*(res>floor(y)-1.0 ? 0.0 : 1.0);}float divWithIntCheck(float x,float y){if(floor(x)==x&&floor(y)==y&&integerMod(x,y)==0.0){return float(int(x)/int(y));}return x/y;}float integerCorrectionModulo(float number,float divisor){if(number<0.0){number=abs(number);if(divisor<0.0){divisor=abs(divisor);}return-(number-(divisor*floor(divWithIntCheck(number,divisor))));}if(divisor<0.0){divisor=abs(divisor);}return number-(divisor*floor(divWithIntCheck(number,divisor)));}float getDEMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.r*255.0*256.0+result.g*255.0)/5000.0;}float getDSMElevationFromSampler2D(sampler2D tex,float x,float y){vec4 result=texture2D(tex,vec2(x,y));return(result.b*255.0*256.0+result.a*255.0)/5000.0;}vec4 actualColor;void color(float r,float g,float b,float a){actualColor=vec4(r,g,b,a);}void color(float r,float g,float b){color(r,g,b,1.0);}void color(float r){color(r,r,r,1.0);}void color(vec4 color){actualColor=color;}const int LOOP_MAX=1000;varying vec2 vTexCoordCropped;varying vec2 vTexCoordFull;uniform sampler2D user_a;uniform float user_width;uniform float user_height;uniform float user_maxHeight;uniform float user_zoom;uniform float user_topYCoord;uniform float user_ySize;uniform vec4 user_color;uniform vec4 u_sunColor;uniform float user_step;uniform float user_west;uniform float user_dLng;uniform float user_dec;uniform float user_Hi;uniform bool u_below_canopy;uniform sampler2D user_sunExposureTexture;uniform bool user_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_lit=1.0;float dsm_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(u_below_canopy==true){float dem_height=getDEMElevationFromSampler2D(user_a,user_x,user_y);if(dsm_height-dem_height>.002){user_lit=0.0;}}float user_z=dsm_height;float user_PI=3.141592653589793;float user_2PI=6.283185307179586;float earthRadiusInKm=6378.137;float user_deg=57.29577951308232;float user_y_coord=user_topYCoord+(user_y*user_ySize);float user_lat_coord=(user_y_coord-0.5)/-0.15915494309189532;float user_lat=(2.0*atan(exp(user_lat_coord)))-(user_PI/2.0);float user_rad=0.017453292519943295;float user_lng=user_west+(user_dLng*user_x);float user_H=integerCorrectionModulo((sunHi-(user_rad*-user_lng)),user_2PI);float sun_azimuth=atan2(sin(user_H),((cos(user_H)*sin(user_lat))-(tan(sunDec)*cos(user_lat))));float sun_altitude=asin(((sin(user_lat)*sin(sunDec))+((cos(user_lat)*cos(sunDec))*cos(user_H))));float user_zoom_factor=_pow(2.0,user_zoom);float user_kmPerPixel=divWithIntCheck(156.5430339296875,user_zoom_factor)*abs(cos(user_lat));float user_dx=((-sin(sun_azimuth)*cos(sun_altitude))*user_step)/user_width;float user_dy=((cos(sun_azimuth)*cos(sun_altitude))*user_step)/user_height;float user_dz=((sin(sun_altitude)*user_kmPerPixel)*user_step);float shadow_bias=0.0005;float user_curvature=0.0;float cur_height=0.0;float user_distance=0.0;float user_startX=user_x;float user_startY=user_y;user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;if((abs(1.0)<0.0)){kernelResult=0.0;return;}float minAngle=asin((earthRadiusInKm/(earthRadiusInKm+user_z)))-(user_PI/2.0);if(user_z<0.0||sun_altitude<minAngle){user_lit=0.0;}else{float xIter=ceil(user_dx<0.0 ? abs(user_x/user_dx):(1.0-user_x)/user_dx);float yIter=ceil(user_dy<0.0 ? abs(user_y/user_dy):(1.0-user_y)/user_dy);float zIter=ceil(user_dz<0.0 ? float(LOOP_MAX):(maxHeight-user_z)/user_dz);int iter=int(min(xIter,min(yIter,zIter)));float user_distance=(sqrt(pow(user_dx*user_width,2.0)+pow(user_dy*user_height,2.0))*user_kmPerPixel)/earthRadiusInKm;for(int safeI=0;safeI<LOOP_MAX;safeI++){if(safeI>iter){break;}cur_height=getDSMElevationFromSampler2D(user_a,user_x,user_y);if(cur_height-user_z>shadow_bias){user_curvature=earthRadiusInKm*(1.0-cos(user_distance*float(safeI+1)));if(user_z<(cur_height-user_curvature)){user_lit=0.0;vec4 result=texture2D(user_a,vec2(user_x,user_y));iter=0;break;}}user_x+=user_dx;user_y+=user_dy;user_z+=user_dz;}}if((user_lit==1.0)){if(u_outputLocationShadeProfile==true&&u_sunColor==shade_color){float timeInSun=pow(.7,pow(sin(sun_altitude),.678))*sin(sun_altitude)*(1.0/0.7);float h=0.5;vec4 blue=vec4(0.0,0.0,1.0,1.0);vec4 green=vec4(0.0,1.0,0.0,1.0);vec4 red=vec4(1.0,0.0,0.0,1.0);vec4 col=mix(mix(blue,green,timeInSun/h),mix(green,red,(timeInSun-h)/(1.0-h)),step(h,timeInSun));color(col.r,col.g,col.b,1.0);}else{color(u_sunColor);}}else{color(shade_color);}if(user_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(o);const a=n.createBuffer(),s=n.getAttribLocation(o,"a_pos"),u=n.createBuffer(),h=n.getAttribLocation(o,"a_tex_pos"),l=n.createBuffer();n.bindBuffer(n.ARRAY_BUFFER,l),n.bufferData(n.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),n.STATIC_DRAW);const c=n.createBuffer();n.bindBuffer(n.ARRAY_BUFFER,c),n.bufferData(n.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),n.STATIC_DRAW);const f=n.getUniformLocation(o,"user_a");n.uniform1i(f,0);const m=n.getUniformLocation(o,"user_width"),d=n.getUniformLocation(o,"user_height"),_=n.getUniformLocation(o,"user_maxHeight"),x=n.getUniformLocation(o,"user_zoom"),g=n.getUniformLocation(o,"user_topYCoord"),p=n.getUniformLocation(o,"user_ySize"),E=n.getUniformLocation(o,"user_step"),T=n.getUniformLocation(o,"user_west"),v=n.getUniformLocation(o,"user_dLng"),y=n.getUniformLocation(o,"user_dec"),R=n.getUniformLocation(o,"user_Hi"),A=n.getUniformLocation(o,"user_color");n.getUniformLocation(o,"user_xStart"),n.getUniformLocation(o,"user_yStart"),n.getUniformLocation(o,"user_xEnd"),n.getUniformLocation(o,"user_yEnd");const b=n.getUniformLocation(o,"u_below_canopy"),M=n.getUniformLocation(o,"user_sunExposureTexture"),w=n.getUniformLocation(o,"user_renderToSunExposureTexture"),U=n.getUniformLocation(o,"user_outputSunExposure"),F=n.getUniformLocation(o,"user_exposureDelta"),D=n.getUniformLocation(o,"u_outputShadeProfile"),L=n.getUniformLocation(o,"u_gpxTexture"),P=n.getUniformLocation(o,"u_decHiTexture"),S=n.getUniformLocation(o,"u_sunColor"),C=n.getUniformLocation(o,"u_outputLocationShadeProfile"),B=n.getUniformLocation(o,"u_shadeProfileLocation");let I=0,N=0,O=0,X=null;const G=()=>{N&&O&&(n.useProgram(o),n.bindBuffer(n.ARRAY_BUFFER,a),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,u),n.enableVertexAttribArray(h),n.vertexAttribPointer(h,2,n.FLOAT,!1,0,0),null!==X&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,X)),i(n,N,O),n.viewport(0,0,N,O),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4))};let z=0;return{updateHeightMap:function(t){const{heightMapTex:e,width:r,height:i,maxHeight:s,heightMapZoom:h,topYCoord:l,ySize:c,colorVec:f,step:M,west:w,dLng:U,dec:F,Hi:D,cornerClipCoords:L,cornerTextureCoords:P,outputWidth:S,outputHeight:C,belowCanopy:B,skipRender:z}=t;n.useProgram(o),N=S,O=C,X=e,n.bindBuffer(n.ARRAY_BUFFER,a),n.bufferData(n.ARRAY_BUFFER,new Float32Array(L),n.STATIC_DRAW),n.bindBuffer(n.ARRAY_BUFFER,u),n.bufferData(n.ARRAY_BUFFER,new Float32Array(P),n.STATIC_DRAW),n.uniform1f(m,r),n.uniform1f(d,i),n.uniform1f(_,s),n.uniform1f(x,h),n.uniform1f(g,l),n.uniform1f(p,c),n.uniform4fv(A,f),n.uniform1f(E,M),n.uniform1f(T,w),n.uniform1f(v,U),n.uniform1f(y,F),n.uniform1f(R,D),n.uniform1f(b,B?1:0),z||(window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G))},updateDate:t=>{const{dec:e,Hi:r}=t;n.useProgram(o),n.uniform1f(y,e),n.uniform1f(R,r),window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G)},updateDateRange:t=>r(this,void 0,void 0,(function*(){const{startDate:e,endDate:r,iterations:i,emit:l}=t;n.useProgram(o);const c=z=Date.now();n.uniform4fv(F,new Float32Array([1/i,0,0,0]));const f=Math.floor((r.getTime()-e.getTime())/i),m=N,d=O,_=n.createTexture();n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,_),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,m,d,0,n.RGBA,n.UNSIGNED_BYTE,null),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST);const x=n.createTexture();n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,x),n.texImage2D(n.TEXTURE_2D,0,n.RGBA,m,d,0,n.RGBA,n.UNSIGNED_BYTE,null),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST);const g=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,g);const p=n.COLOR_ATTACHMENT0;for(let t=0;t<i;t++){if(n.useProgram(o),l("tileloaded",t,i-1),c!==z)return n.deleteFramebuffer(g),n.deleteTexture(_),n.deleteTexture(x),!0;yield new Promise(((r,i)=>{window.requestAnimationFrame((()=>{n.useProgram(o),n.uniform1i(M,1),n.uniform1i(w,1),null!==X&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,X));const i=t%2==0?x:_,l=t%2==0?_:x;n.activeTexture(n.TEXTURE1),n.bindTexture(n.TEXTURE_2D,i),n.bindFramebuffer(n.FRAMEBUFFER,g),n.framebufferTexture2D(n.FRAMEBUFFER,p,n.TEXTURE_2D,l,0);const c=n.checkFramebufferStatus(n.FRAMEBUFFER);c!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+c);const{dec:E,Hi:T}=Z(new Date(e.getTime()+f*t));n.uniform1f(y,E),n.uniform1f(R,T),n.bindBuffer(n.ARRAY_BUFFER,a),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,u),n.enableVertexAttribArray(h),n.vertexAttribPointer(h,2,n.FLOAT,!1,0,0),n.viewport(0,0,m,d),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4),r()}))}))}return n.deleteFramebuffer(g),yield new Promise(((t,e)=>{window.requestAnimationFrame((()=>{if(n.useProgram(o),c!==z)return n.deleteTexture(_),n.deleteTexture(x),void t(!0);n.activeTexture(n.TEXTURE2),n.bindTexture(n.TEXTURE_2D,(i-1)%2==0?_:x),n.uniform1i(M,2),n.uniform1i(U,1),n.uniform1i(w,1),G(),n.uniform1i(w,0),n.uniform1i(U,0),n.uniform1i(M,0),n.deleteTexture(_),n.deleteTexture(x),t(!1)}))}))})),updateColor:t=>{const{colorVec:e}=t;n.useProgram(o),n.uniform4fv(A,e),window.cancelAnimationFrame(I),I=window.requestAnimationFrame(G)},generateShadeProfile:t=>{n.useProgram(o);const{dates:e,texCoords:r,shadeColor:i,sunColor:a}=t,u=r.length,f=e.length,m=n.getUniform(o,A);n.uniform4fv(A,i),n.uniform4fv(S,a),n.uniform1i(D,1);const d=n.createTexture();n.activeTexture(n.TEXTURE3),ot({gl:n,imageData:null,width:u,height:f,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,texture:d});const _=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,_),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,d,0);const x=n.checkFramebufferStatus(n.FRAMEBUFFER);x!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=r.map((t=>[t[0],t[1],0,0])).flat(),p=n.createTexture();n.activeTexture(n.TEXTURE2),ot({gl:n,texture:p,imageData:new Float32Array(g),width:g.length/4,height:1,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,internalFormat:n.RGBA32F,type:n.FLOAT}),n.uniform1i(L,2);const E=e.map((t=>{const{dec:e,Hi:r}=Z(t);return[-e,r/10,0,0]})).flat(),T=n.createTexture();n.activeTexture(n.TEXTURE1),ot({gl:n,texture:T,imageData:new Float32Array(E),width:1,height:e.length,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,internalFormat:n.RGBA32F,type:n.FLOAT}),n.uniform1i(P,1),null!==X&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,X)),n.bindBuffer(n.ARRAY_BUFFER,l),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,c),n.enableVertexAttribArray(h),n.vertexAttribPointer(h,2,n.FLOAT,!1,0,0),n.viewport(0,0,u,f),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4);const v=new Uint8Array(u*f*4);return n.readPixels(0,0,u,f,n.RGBA,n.UNSIGNED_BYTE,v),n.deleteTexture(d),n.deleteTexture(T),n.deleteTexture(p),n.deleteFramebuffer(_),n.uniform1i(P,0),n.uniform1i(L,0),n.uniform1i(D,0),n.uniform4fv(A,m),n.uniform4fv(S,[0,0,0,0]),v},generateLocationShadeProfile:t=>{n.useProgram(o);const{dates:e,texCoord:r,shadeColor:i,sunColor:a}=t,u=e[0].length,f=e.length,m=n.getUniform(o,A);n.uniform4fv(A,i),n.uniform4fv(S,a),n.uniform1i(C,1),n.uniform2fv(B,[r[0],r[1]]);const d=n.createTexture();n.activeTexture(n.TEXTURE2),ot({gl:n,imageData:null,width:u,height:f,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,texture:d});const _=n.createFramebuffer();n.bindFramebuffer(n.FRAMEBUFFER,_),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,d,0);const x=n.checkFramebufferStatus(n.FRAMEBUFFER);x!==n.FRAMEBUFFER_COMPLETE&&console.error("Framebuffer is incomplete: "+x);const g=e.flat().map((t=>{const{dec:e,Hi:r}=Z(t);return[-e,r/10,0,0]})).flat(),p=n.createTexture();n.activeTexture(n.TEXTURE1),ot({gl:n,texture:p,imageData:new Float32Array(g),width:u,height:f,wrap:n.CLAMP_TO_EDGE,filter:n.NEAREST,format:n.RGBA,internalFormat:n.RGBA32F,type:n.FLOAT}),n.uniform1i(P,1),null!==X&&(n.activeTexture(n.TEXTURE0),n.bindTexture(n.TEXTURE_2D,X)),n.uniform1i(w,0),n.bindBuffer(n.ARRAY_BUFFER,l),n.enableVertexAttribArray(s),n.vertexAttribPointer(s,2,n.FLOAT,!1,0,0),n.bindBuffer(n.ARRAY_BUFFER,c),n.enableVertexAttribArray(h),n.vertexAttribPointer(h,2,n.FLOAT,!1,0,0),n.viewport(0,0,u,f),n.clear(n.COLOR_BUFFER_BIT),n.drawArrays(n.TRIANGLE_STRIP,0,4);const E=new Uint8Array(u*f*4);return n.readPixels(0,0,u,f,n.RGBA,n.UNSIGNED_BYTE,E),n.deleteTexture(d),n.deleteTexture(p),n.deleteFramebuffer(_),n.uniform1i(P,0),n.uniform1i(C,0),n.uniform4fv(A,m),n.uniform4fv(S,[0,0,0,0]),E}}}({context:this._gl,setRenderBuffer:t=>{t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.bindFramebuffer(t.FRAMEBUFFER,null)}}),this._buildingRasterizer=new st(this._gl),this._tileMerger=new ht(this._gl)}addTo(t){return this.onAdd(t)}onAdd(t){if(this._map=t,!this._canvasOverlay&&this._canvas){const t=this._map.getBounds();this._canvasOverlay=function(...t){return new e(...t)}(this._canvas,t),this._canvasOverlay.addTo(this._map)}return this._map.on("moveend",this._reset),this._map.attributionControl.addAttribution('<a href="https://shademap.app/about">© ShadeMap</a>'),this._reset(),this}_repositionCanvas(t){if(this._canvasOverlay&&this._canvasOverlay.setBounds(t),this._map){const{x:t,y:e}=this._map.getSize();this._canvas.width=t,this._canvas.height=e}this._flush()}_flush(){this.emit("idle")}}return void 0!==window.L&&(window.L.shadeMap=(...t)=>new lt(...t)),lt}));
|
|
@@ -11,13 +11,13 @@ import { Color } from "./types/color";
|
|
|
11
11
|
import { DSMSource, SunExposureOptions, TerrainSource } from "./types/shadeMapOptions";
|
|
12
12
|
export declare class ShadeMap extends EventEmitter {
|
|
13
13
|
_canvas: HTMLCanvasElement;
|
|
14
|
-
_gl
|
|
14
|
+
_gl?: WebGLRenderingContext;
|
|
15
15
|
_map?: Map | LeafletMap;
|
|
16
16
|
_compiledKernel?: CompiledKernel;
|
|
17
17
|
_color: Color;
|
|
18
18
|
_bounds?: LatLngBounds;
|
|
19
|
-
_buildingRasterizer
|
|
20
|
-
_tileMerger
|
|
19
|
+
_buildingRasterizer?: BuildingRasterizer;
|
|
20
|
+
_tileMerger?: TileMerger;
|
|
21
21
|
_heightMap?: HeightMap;
|
|
22
22
|
options: {
|
|
23
23
|
date: Date;
|
|
@@ -80,6 +80,7 @@ export declare class ShadeMap extends EventEmitter {
|
|
|
80
80
|
_reset(): Promise<this>;
|
|
81
81
|
_draw(heightMap: HeightMap): Promise<this>;
|
|
82
82
|
readPixel(x: number, y: number): Uint8Array;
|
|
83
|
+
readPixels(x: number, y: number, width: number, height: number): Uint8Array;
|
|
83
84
|
toGeoTiff(): {
|
|
84
85
|
data: Uint8Array;
|
|
85
86
|
metadata: {
|
|
@@ -13,6 +13,7 @@ export default class extends ShadeMap {
|
|
|
13
13
|
_refreshing: number;
|
|
14
14
|
_raf: number;
|
|
15
15
|
_moveEndHandler: () => void;
|
|
16
|
+
_framebuffer?: WebGLFramebuffer;
|
|
16
17
|
constructor(options: ShadeMapOptions);
|
|
17
18
|
render(gl: WebGLRenderingContext, matrix: number[]): void;
|
|
18
19
|
addTo(map: Map): this;
|
|
@@ -21,6 +22,8 @@ export default class extends ShadeMap {
|
|
|
21
22
|
_getHeightMapCoords(x: number, y: number): any;
|
|
22
23
|
getHoursOfSun(x: number, y: number): number;
|
|
23
24
|
remove(): void;
|
|
25
|
+
readPixel(x: number, y: number): Uint8Array;
|
|
26
|
+
readPixels(x: number, y: number, width: number, height: number): Uint8Array;
|
|
24
27
|
_flush(): void;
|
|
25
28
|
_repositionCanvas(bounds: LatLngBounds): this;
|
|
26
29
|
}
|
|
@@ -32,6 +32,9 @@ export declare class BuildingRasterizer {
|
|
|
32
32
|
indexBuffer: WebGLBuffer;
|
|
33
33
|
targetTexture: WebGLTexture;
|
|
34
34
|
constructor(gl: WebGLRenderingContext);
|
|
35
|
-
raster(args: BuildingRasterGLArgs):
|
|
35
|
+
raster(args: BuildingRasterGLArgs): {
|
|
36
|
+
maxHeight: number;
|
|
37
|
+
heightMapTex: WebGLTexture;
|
|
38
|
+
};
|
|
36
39
|
}
|
|
37
40
|
export {};
|
|
@@ -31,10 +31,12 @@ interface MergeImageOptions {
|
|
|
31
31
|
}
|
|
32
32
|
export declare class TileMerger {
|
|
33
33
|
gl: WebGLRenderingContext;
|
|
34
|
-
|
|
34
|
+
tileTexture: WebGLTexture;
|
|
35
|
+
outputTexture: WebGLTexture;
|
|
35
36
|
program: WebGLProgram;
|
|
36
37
|
positionAttributeLocation: number;
|
|
37
38
|
positionBuffer: WebGLBuffer;
|
|
39
|
+
framebuffer: WebGLFramebuffer;
|
|
38
40
|
inProgress: HTMLImageElement[];
|
|
39
41
|
finished: number;
|
|
40
42
|
constructor(gl: WebGLRenderingContext);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MapboxGeoJSONFeature } from "mapbox-gl";
|
|
2
2
|
import { BuildingRasterizer } from "../buildings/BuildingRasterizer";
|
|
3
|
+
import { XYZRaster } from "../lib/image";
|
|
3
4
|
import { TileMerger } from "./TileMerger";
|
|
4
5
|
import { LatLngBounds, ValidBounds } from "../map/geometryLeaflet";
|
|
5
6
|
import { DSMSource, TerrainSource } from "../types/shadeMapOptions";
|
|
@@ -16,17 +17,17 @@ interface HeightMapParams {
|
|
|
16
17
|
forceUpdate: boolean;
|
|
17
18
|
}
|
|
18
19
|
export interface HeightMap {
|
|
20
|
+
heightMapTex: WebGLTexture | null;
|
|
19
21
|
width: number;
|
|
20
22
|
height: number;
|
|
21
|
-
imageData: Uint8ClampedArray;
|
|
22
23
|
maxHeight: number;
|
|
23
24
|
visibleDEMPixelBounds: ValidBounds;
|
|
24
25
|
DEMPixelBounds: ValidBounds;
|
|
26
|
+
raster: XYZRaster[];
|
|
25
27
|
demZoom: number;
|
|
26
28
|
dirty: boolean;
|
|
27
29
|
outputWidth: number;
|
|
28
30
|
outputHeight: number;
|
|
29
|
-
kmPerPixel: number;
|
|
30
31
|
}
|
|
31
32
|
export declare const getHeightMap: (params: HeightMapParams) => Promise<HeightMap>;
|
|
32
33
|
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface BindTextureOptions {
|
|
2
|
+
gl: WebGLRenderingContext;
|
|
3
|
+
texture: WebGLTexture;
|
|
4
|
+
imageData?: Uint8ClampedArray | Float32Array | null;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
filter?: number;
|
|
8
|
+
wrap?: number;
|
|
9
|
+
format?: number;
|
|
10
|
+
type?: number;
|
|
11
|
+
internalFormat?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const bindTexture: (options: BindTextureOptions) => void;
|
|
@@ -3,7 +3,8 @@ interface CompileProgramArgs {
|
|
|
3
3
|
vSrc: string;
|
|
4
4
|
fSrc: string;
|
|
5
5
|
}
|
|
6
|
-
interface
|
|
6
|
+
interface UpdateHeightMapArgs {
|
|
7
|
+
heightMapTex: WebGLTexture | null;
|
|
7
8
|
width: number;
|
|
8
9
|
height: number;
|
|
9
10
|
maxHeight: number;
|
|
@@ -21,13 +22,7 @@ interface UpdateLocationArgs {
|
|
|
21
22
|
outputWidth: number;
|
|
22
23
|
outputHeight: number;
|
|
23
24
|
belowCanopy: boolean;
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
interface UpdateViewportArgs {
|
|
27
|
-
xStart: number;
|
|
28
|
-
yStart: number;
|
|
29
|
-
xEnd: number;
|
|
30
|
-
yEnd: number;
|
|
25
|
+
skipRender: boolean;
|
|
31
26
|
}
|
|
32
27
|
interface UpdateDateArgs {
|
|
33
28
|
dec: number;
|
|
@@ -43,8 +38,7 @@ interface UpdateColorArgs {
|
|
|
43
38
|
colorVec: number[];
|
|
44
39
|
}
|
|
45
40
|
export interface CompiledKernel {
|
|
46
|
-
|
|
47
|
-
updateViewport: (args: UpdateViewportArgs) => void;
|
|
41
|
+
updateHeightMap: (args: UpdateHeightMapArgs) => void;
|
|
48
42
|
updateDate: (args: UpdateDateArgs) => void;
|
|
49
43
|
updateDateRange: (args: UpdateDateRangeArgs) => Promise<boolean>;
|
|
50
44
|
updateColor: (args: UpdateColorArgs) => void;
|
|
@@ -64,6 +58,7 @@ export interface CompiledKernel {
|
|
|
64
58
|
export declare function compileProgram(args: CompileProgramArgs): WebGLProgram;
|
|
65
59
|
interface Settings {
|
|
66
60
|
context: WebGLRenderingContext;
|
|
61
|
+
setRenderBuffer: (gl: WebGLRenderingContext, viewportWidth: number, viewportHeight: number) => void;
|
|
67
62
|
}
|
|
68
63
|
declare function kernel(settings: Settings): CompiledKernel;
|
|
69
64
|
export default kernel;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Color } from "./color";
|
|
2
2
|
export interface Shadow3DData {
|
|
3
|
-
|
|
3
|
+
heightMapTex: WebGLTexture | null;
|
|
4
4
|
maxHeight: number;
|
|
5
5
|
width: number;
|
|
6
6
|
height: number;
|
|
@@ -17,5 +17,5 @@ export interface Shadow3DData {
|
|
|
17
17
|
outputWidth: number;
|
|
18
18
|
outputHeight: number;
|
|
19
19
|
belowCanopy: boolean;
|
|
20
|
-
|
|
20
|
+
skipRender: boolean;
|
|
21
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leaflet-shadow-simulator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.2",
|
|
4
4
|
"description": "Display terrain and structure shadows cast by the sun in a custom Leaflet layer",
|
|
5
5
|
"main": "dist/leaflet-shadow-simulator.umd.min.js",
|
|
6
6
|
"browser": "dist/leaflet-shadow-simulator.umd.min.js",
|