wrolpi/modules/map/static/libs/maplibre-contour-pmtiles.js
Roland b8a532c8d9
Key changes:
- Add MapLibre GL JS viewer with light/dark themes, terrain layer,
  and embedded static map page for coordinate sharing
- Add map pin support with persistent map_pins.yaml config
- Add map region subscriptions and downloader with progress tracking,
  sourcing pre-extracted PMTiles from a cold CDN
- Add map export to PNG and GPS coordinate copy via context menu
- Add MapViewer React component alongside reworked Map page
- Add map catalog and region-building script (build_map_regions.py)
- Remove OSM import/render scripts, leaflet files, Apache configs,
  renderd service, and osm-map-dumper Docker container
- Update Caddyfile to serve PMTiles and map static assets
- Update build/install/repair scripts for Protomaps dependencies
- Drop map_file table, add/drop map_subscription table (alembic)
- Adding button to skip primary drive mounting during onboarding.
2026-04-04 13:48:19 -06:00

1 line
49 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).mlcontour=t()}(this,(function(){"use strict";var e,t,r;function i(i,n){if(e)if(t){var s="var sharedChunk = {}; ("+e+")(sharedChunk); ("+t+")(sharedChunk);",o={};e(o),r=n(o),"undefined"!=typeof window&&(r.workerUrl=window.URL.createObjectURL(new Blob([s],{type:"text/javascript"})))}else t=n;else e=n}return i(0,(function(e){class t{constructor(e,t){this.start=e,this.end=t,this.points=[],this.append=this.append.bind(this),this.prepend=this.prepend.bind(this)}append(e,t){this.points.push(Math.round(e),Math.round(t))}prepend(e,t){this.points.splice(0,0,Math.round(e),Math.round(t))}lineString(){return this.toArray()}isEmpty(){return this.points.length<2}appendFragment(e){this.points.push(...e.points),this.end=e.end}toArray(){return this.points}}const r=[[],[[[1,2],[0,1]]],[[[2,1],[1,2]]],[[[2,1],[0,1]]],[[[1,0],[2,1]]],[[[1,2],[0,1]],[[1,0],[2,1]]],[[[1,0],[1,2]]],[[[1,0],[0,1]]],[[[0,1],[1,0]]],[[[1,2],[1,0]]],[[[0,1],[1,0]],[[2,1],[1,2]]],[[[2,1],[1,0]]],[[[0,1],[2,1]]],[[[1,2],[2,1]]],[[[0,1],[1,2]]],[]];function i(e,t,r,i){return(t=2*t+i[0])+(r=2*r+i[1])*(e+1)*2}function n(e,t,r){return(t-e)/(r-e)}function s(e,s,o=4096,a=1){if(!e)return{};const l=o/(s.width-1);let h,c,d,u,f,p;const g={},m=new Map,w=new Map;function y(e,t,r){0===e[0]?r(l*(p-1),l*(f-n(d,t,h))):2===e[0]?r(l*p,l*(f-n(u,t,c))):0===e[1]?r(l*(p-n(c,t,h)),l*(f-1)):r(l*(p-n(u,t,d)),l*f)}for(f=1-a;f<s.height+a;f++){c=s.get(0,f-1),u=s.get(0,f);let n=Math.min(c,u),o=Math.max(c,u);for(p=1-a;p<s.width+a;p++){h=c,d=u,c=s.get(p,f-1),u=s.get(p,f);const a=n,l=o;if(n=Math.min(c,u),o=Math.max(c,u),isNaN(h)||isNaN(c)||isNaN(u)||isNaN(d))continue;const v=Math.min(a,n),b=Math.max(l,o),x=Math.ceil(v/e)*e,T=Math.floor(b/e)*e;for(let n=x;n<=T;n+=e){const e=h>n,o=c>n,a=d>n,l=u>n;for(const h of r[(e?8:0)|(o?4:0)|(l?2:0)|(a?1:0)]){let e=m.get(n);e||m.set(n,e=new Map);let r=w.get(n);r||w.set(n,r=new Map);const o=h[0],a=h[1],l=i(s.width,p,f,o),c=i(s.width,p,f,a);let d,u;if(d=r.get(l))if(r.delete(l),u=e.get(c))if(e.delete(c),d===u){if(y(a,n,d.append),!d.isEmpty()){let e=g[n];e||(g[n]=e=[]),e.push(d.lineString())}}else d.appendFragment(u),r.set(d.end=u.end,d);else y(a,n,d.append),r.set(d.end=c,d);else if(d=e.get(c))e.delete(c),y(o,n,d.prepend),e.set(d.start=l,d);else{const i=new t(l,c);y(o,n,i.append),y(a,n,i.append),e.set(l,i),r.set(c,i)}}}}}for(const[e,t]of m.entries()){let r=null;for(const i of t.values())i.isEmpty()||(null==r&&(r=g[e]||(g[e]=[])),r.push(i.lineString()))}return g}function o(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(i=Object.getOwnPropertySymbols(e);n<i.length;n++)t.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(e,i[n])&&(r[i[n]]=e[i[n]])}return r}function a(e,t,r,i){return new(r||(r=Promise))((function(n,s){function o(e){try{l(i.next(e))}catch(e){s(e)}}function a(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))}function l(e){const t=Object.entries(e);return t.sort((([e],[t])=>e<t?-1:e>t?1:0)),t}function h(e){return l(e).map((([e,t])=>[e,..."number"==typeof t?[t]:t].join("*"))).join("~")}function c(e){return l(e).map((([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`)).join(",")}"function"==typeof SuppressedError&&SuppressedError;let d=null;function u(){return null==d&&(d="undefined"!=typeof OffscreenCanvas&&new OffscreenCanvas(1,1).getContext("2d")&&"function"==typeof createImageBitmap),d||!1}let f=null;function p(e,t,r){let i=()=>{};const n=setTimeout((()=>{i(new Error("timed out")),null==r||r.abort()}),e);g(r,(()=>{i(new Error("aborted")),clearTimeout(n)}));const s=new Promise(((e,t)=>{i=t}));return Promise.race([s,t.finally((()=>clearTimeout(n)))])}function g(e,t){t&&(null==e||e.signal.addEventListener("abort",t))}function m(e){var t;return Boolean(null===(t=null==e?void 0:e.signal)||void 0===t?void 0:t.aborted)}let w=0;class y{constructor(e=100){this.size=()=>this.items.size,this.get=(e,t,r)=>{let i=this.items.get(e);if(i)i.lastUsed=++w,i.waiting++;else{const r=new AbortController,n=t(e,r);i={abortController:r,item:n,lastUsed:++w,waiting:1},this.items.set(e,i),this.prune()}const n=this.items,s=i.item.then((e=>e),(t=>(n.delete(e),Promise.reject(t))));let o=!1;return g(r,(()=>{var t;i&&i.abortController&&!o&&(o=!0,--i.waiting<=0&&(null===(t=i.abortController)||void 0===t||t.abort(),n.delete(e)))})),s},this.clear=()=>this.items.clear(),this.maxSize=e,this.items=new Map}prune(){if(this.items.size>this.maxSize){let e,t=1/0;this.items.forEach(((r,i)=>{r.lastUsed<t&&(t=r.lastUsed,e=i)})),void 0!==e&&this.items.delete(e)}}}var v=Uint8Array,b=Uint16Array,x=Int32Array,T=new v([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),P=new v([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),k=new v([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),M=function(e,t){for(var r=new b(31),i=0;i<31;++i)r[i]=t+=1<<e[i-1];var n=new x(r[30]);for(i=1;i<30;++i)for(var s=r[i];s<r[i+1];++s)n[s]=s-r[i]<<5|i;return{b:r,r:n}},E=M(T,2),C=E.b,U=E.r;C[28]=258,U[258]=28;for(var F=M(P,0).b,V=new b(32768),S=0;S<32768;++S){var O=(43690&S)>>1|(21845&S)<<1;O=(61680&(O=(52428&O)>>2|(13107&O)<<2))>>4|(3855&O)<<4,V[S]=((65280&O)>>8|(255&O)<<8)>>1}var N=function(e,t,r){for(var i=e.length,n=0,s=new b(t);n<i;++n)e[n]&&++s[e[n]-1];var o,a=new b(t);for(n=1;n<t;++n)a[n]=a[n-1]+s[n-1]<<1;if(r){o=new b(1<<t);var l=15-t;for(n=0;n<i;++n)if(e[n])for(var h=n<<4|e[n],c=t-e[n],d=a[e[n]-1]++<<c,u=d|(1<<c)-1;d<=u;++d)o[V[d]>>l]=h}else for(o=new b(i),n=0;n<i;++n)e[n]&&(o[n]=V[a[e[n]-1]++]>>15-e[n]);return o},I=new v(288);for(S=0;S<144;++S)I[S]=8;for(S=144;S<256;++S)I[S]=9;for(S=256;S<280;++S)I[S]=7;for(S=280;S<288;++S)I[S]=8;var D=new v(32);for(S=0;S<32;++S)D[S]=5;var $=N(I,9,1),A=N(D,5,1),B=function(e){for(var t=e[0],r=1;r<e.length;++r)e[r]>t&&(t=e[r]);return t},z=function(e,t,r){var i=t/8|0;return(e[i]|e[i+1]<<8)>>(7&t)&r},R=function(e,t){var r=t/8|0;return(e[r]|e[r+1]<<8|e[r+2]<<16)>>(7&t)},j=["unexpected EOF","invalid block type","invalid length/literal","invalid distance","stream finished","no stream handler",,"no callback","invalid UTF-8 data","extra field too long","date not in range 1980-2099","filename too long","stream finishing","invalid zip data"],H=function(e,t,r){var i=new Error(t||j[e]);if(i.code=e,Error.captureStackTrace&&Error.captureStackTrace(i,H),!r)throw i;return i},W=function(e,t,r,i){var n=e.length;if(!n||t.f&&!t.l)return r||new v(0);var s=!r,o=s||2!=t.i,a=t.i;s&&(r=new v(3*n));var l=function(e){var t=r.length;if(e>t){var i=new v(Math.max(2*t,e));i.set(r),r=i}},h=t.f||0,c=t.p||0,d=t.b||0,u=t.l,f=t.d,p=t.m,g=t.n,m=8*n;do{if(!u){h=z(e,c,1);var w=z(e,c+1,3);if(c+=3,!w){var y=e[(D=4+((c+7)/8|0))-4]|e[D-3]<<8,b=D+y;if(b>n){a&&H(0);break}o&&l(d+y),r.set(e.subarray(D,b),d),t.b=d+=y,t.p=c=8*b,t.f=h;continue}if(1==w)u=$,f=A,p=9,g=5;else if(2==w){var x=z(e,c,31)+257,M=z(e,c+10,15)+4,E=x+z(e,c+5,31)+1;c+=14;for(var U=new v(E),V=new v(19),S=0;S<M;++S)V[k[S]]=z(e,c+3*S,7);c+=3*M;var O=B(V),L=(1<<O)-1,I=N(V,O,1);for(S=0;S<E;){var D,j=I[z(e,c,L)];if(c+=15&j,(D=j>>4)<16)U[S++]=D;else{var W=0,Z=0;for(16==D?(Z=3+z(e,c,3),c+=2,W=U[S-1]):17==D?(Z=3+z(e,c,7),c+=3):18==D&&(Z=11+z(e,c,127),c+=7);Z--;)U[S++]=W}}var G=U.subarray(0,x),K=U.subarray(x);p=B(G),g=B(K),u=N(G,p,1),f=N(K,g,1)}else H(1);if(c>m){a&&H(0);break}}o&&l(d+131072);for(var _=(1<<p)-1,J=(1<<g)-1,q=c;;q=c){var Y=(W=u[R(e,c)&_])>>4;if((c+=15&W)>m){a&&H(0);break}if(W||H(2),Y<256)r[d++]=Y;else{if(256==Y){q=c,u=null;break}var Q=Y-254;if(Y>264){var X=T[S=Y-257];Q=z(e,c,(1<<X)-1)+C[S],c+=X}var ee=f[R(e,c)&J],te=ee>>4;ee||H(3),c+=15&ee;K=F[te];if(te>3){X=P[te];K+=R(e,c)&(1<<X)-1,c+=X}if(c>m){a&&H(0);break}o&&l(d+131072);var re=d+Q;if(d<K){var ie=0-K,ne=Math.min(K,re);for(ie+d<0&&H(3);d<ne;++d)r[d]=i[ie+d]}for(;d<re;++d)r[d]=r[d-K]}}t.l=u,t.p=q,t.b=d,t.f=h,u&&(h=1,t.m=p,t.d=f,t.n=g)}while(!h);return d!=r.length&&s?function(e,t,r){return(null==r||r>e.length)&&(r=e.length),new v(e.subarray(t,r))}(r,0,d):r.subarray(0,d)},Z=new v(0),G=function(e){31==e[0]&&139==e[1]&&8==e[2]||H(6,"invalid gzip data");var t=e[3],r=10;4&t&&(r+=2+(e[10]|e[11]<<8));for(var i=(t>>3&1)+(t>>4&1);i>0;i-=!e[r++]);return r+(2&t)},K=function(e){var t=e.length;return(e[t-4]|e[t-3]<<8|e[t-2]<<16|e[t-1]<<24)>>>0},_=function(e,t){return(8!=(15&e[0])||e[0]>>4>7||(e[0]<<8|e[1])%31)&&H(6,"invalid zlib data"),1==(e[1]>>5&1)&&H(6,"invalid zlib data: "+(32&e[1]?"need":"unexpected")+" dictionary"),2+(e[1]>>3&4)};function J(e,t){return 31==e[0]&&139==e[1]&&8==e[2]?function(e,t){var r=G(e);return r+8>e.length&&H(6,"invalid gzip data"),W(e.subarray(r,-8),{i:2},new v(K(e)),t)}(e,t):8!=(15&e[0])||e[0]>>4>7||(e[0]<<8|e[1])%31?function(e,t){return W(e,{i:2},t,t)}(e,t):function(e,t){return W(e.subarray(_(e),-4),{i:2},t,t)}(e,t)}var q="undefined"!=typeof TextDecoder&&new TextDecoder;try{q.decode(Z,{stream:!0}),1}catch(e){}var Y=Object.defineProperty,Q=Math.pow,X=(e,t)=>Y(e,"name",{value:t,configurable:!0}),ee=(e,t,r)=>new Promise(((i,n)=>{var s=e=>{try{a(r.next(e))}catch(e){n(e)}},o=e=>{try{a(r.throw(e))}catch(e){n(e)}},a=e=>e.done?i(e.value):Promise.resolve(e.value).then(s,o);a((r=r.apply(e,t)).next())}));X(((e,t)=>{let r=!1,i="";return new(L.GridLayer.extend({createTile:X(((t,n)=>{let s=document.createElement("img"),o=new AbortController,a=o.signal;return s.cancel=()=>{o.abort()},r||(e.getHeader().then((e=>{1===e.tileType?console.error("Error: archive contains MVT vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details."):2===e.tileType?i="image/png":3===e.tileType?i="image/jpeg":4===e.tileType?i="image/webp":5===e.tileType&&(i="image/avif")})),r=!0),e.getZxy(t.z,t.x,t.y,a).then((e=>{if(e){let t=new Blob([e.data],{type:i}),r=window.URL.createObjectURL(t);s.src=r,s.cancel=void 0,n(void 0,s)}})).catch((e=>{if("AbortError"!==e.name)throw e})),s}),"createTile"),_removeTile:X((function(e){let t=this._tiles[e];t&&(t.el.cancel&&t.el.cancel(),t.el.width=0,t.el.height=0,t.el.deleted=!0,L.DomUtil.remove(t.el),delete this._tiles[e],this.fire("tileunload",{tile:t.el,coords:this._keyToTileCoords(e)}))}),"_removeTile")}))(t)}),"leafletRasterLayer");var te=X((e=>(t,r)=>{if(r instanceof AbortController)return e(t,r);let i=new AbortController;return e(t,i).then((e=>r(void 0,e.data,e.cacheControl||"",e.expires||"")),(e=>r(e))).catch((e=>r(e))),{cancel:X((()=>i.abort()),"cancel")}}),"v3compat");function re(e,t){return 4294967296*(t>>>0)+(e>>>0)}function ie(e,t){let r=t.buf,i=r[t.pos++],n=(112&i)>>4;if(i<128||(i=r[t.pos++],n|=(127&i)<<3,i<128)||(i=r[t.pos++],n|=(127&i)<<10,i<128)||(i=r[t.pos++],n|=(127&i)<<17,i<128)||(i=r[t.pos++],n|=(127&i)<<24,i<128)||(i=r[t.pos++],n|=(1&i)<<31,i<128))return re(e,n);throw new Error("Expected varint not more than 10 bytes")}function ne(e){let t=e.buf,r=t[e.pos++],i=127&r;return r<128||(r=t[e.pos++],i|=(127&r)<<7,r<128)||(r=t[e.pos++],i|=(127&r)<<14,r<128)||(r=t[e.pos++],i|=(127&r)<<21,r<128)?i:(r=t[e.pos],i|=(15&r)<<28,ie(i,e))}function se(e,t,r,i){if(0===i){1===r&&(t[0]=e-1-t[0],t[1]=e-1-t[1]);let i=t[0];t[0]=t[1],t[1]=i}}function oe(e,t){let r=Q(2,e),i=t,n=t,s=t,o=[0,0],a=1;for(;a<r;)i=1&s/2,n=1&(s^i),se(a,o,i,n),o[0]+=a*i,o[1]+=a*n,s/=4,a*=2;return[e,o[0],o[1]]}X(class{constructor(e){this.tilev4=X(((e,t)=>ee(this,null,(function*(){if("json"===e.type){let t=e.url.substr(10),r=this.tiles.get(t);if(r||(r=new Ue(t),this.tiles.set(t,r)),this.metadata)return{data:yield r.getTileJson(e.url)};let i=yield r.getHeader();return(i.minLon>=i.maxLon||i.minLat>=i.maxLat)&&console.error(`Bounds of PMTiles archive ${i.minLon},${i.minLat},${i.maxLon},${i.maxLat} are not valid.`),{data:{tiles:[`${e.url}/{z}/{x}/{y}`],minzoom:i.minZoom,maxzoom:i.maxZoom,bounds:[i.minLon,i.minLat,i.maxLon,i.maxLat]}}}let r=new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/),i=e.url.match(r);if(!i)throw new Error("Invalid PMTiles protocol URL");let n=i[1],s=this.tiles.get(n);s||(s=new Ue(n),this.tiles.set(n,s));let o=i[2],a=i[3],l=i[4],h=yield s.getHeader(),c=yield null==s?void 0:s.getZxy(+o,+a,+l,t.signal);if(c)return{data:new Uint8Array(c.data),cacheControl:c.cacheControl,expires:c.expires};if(1===h.tileType){if(this.errorOnMissingTile)throw new Error("Tile not found.");return{data:new Uint8Array}}return{data:null}}))),"tilev4"),this.tile=te(this.tilev4),this.tiles=new Map,this.metadata=(null==e?void 0:e.metadata)||!1,this.errorOnMissingTile=(null==e?void 0:e.errorOnMissingTile)||!1}add(e){this.tiles.set(e.source.getKey(),e)}get(e){return this.tiles.get(e)}},"Protocol"),X(re,"toNum"),X(ie,"readVarintRemainder"),X(ne,"readVarint"),X(se,"rotate"),X(oe,"idOnLevel");var ae=[0,1,5,21,85,341,1365,5461,21845,87381,349525,1398101,5592405,22369621,89478485,357913941,1431655765,5726623061,22906492245,91625968981,366503875925,1466015503701,5864062014805,23456248059221,93824992236885,375299968947541,0x5555555555555];function le(e,t,r){if(e>26)throw new Error("Tile zoom level exceeds max safe number limit (26)");if(t>Q(2,e)-1||r>Q(2,e)-1)throw new Error("tile x/y outside zoom level bounds");let i=ae[e],n=0,s=0,o=0,a=[t,r],l=Q(2,e)/2;for(;l>0;)n=(a[0]&l)>0?1:0,s=(a[1]&l)>0?1:0,o+=l*l*(3*n^s),se(l,a,n,s),l/=2;return i+o}X(le,"zxyToTileId"),X((function(e){let t=0;for(let r=0;r<27;r++){let i=(1<<r)*(1<<r);if(t+i>e)return oe(r,e-t);t+=i}throw new Error("Tile zoom level exceeds max safe number limit (26)")}),"tileIdToZxy");var he,ce=((he=ce||{})[he.Unknown=0]="Unknown",he[he.None=1]="None",he[he.Gzip=2]="Gzip",he[he.Brotli=3]="Brotli",he[he.Zstd=4]="Zstd",he);function de(e,t){return ee(this,null,(function*(){if(1===t||0===t)return e;if(2===t){if(void 0===globalThis.DecompressionStream)return J(new Uint8Array(e));let t=new Response(e).body;if(!t)throw new Error("Failed to read response stream");let r=t.pipeThrough(new globalThis.DecompressionStream("gzip"));return new Response(r).arrayBuffer()}throw new Error("Compression method not supported")}))}X(de,"defaultDecompress");var ue,fe=((ue=fe||{})[ue.Unknown=0]="Unknown",ue[ue.Mvt=1]="Mvt",ue[ue.Png=2]="Png",ue[ue.Jpeg=3]="Jpeg",ue[ue.Webp=4]="Webp",ue[ue.Avif=5]="Avif",ue);function pe(e){return 1===e?".mvt":2===e?".png":3===e?".jpg":4===e?".webp":5===e?".avif":""}X(pe,"tileTypeExt");function ge(e,t){let r=0,i=e.length-1;for(;r<=i;){let n=i+r>>1,s=t-e[n].tileId;if(s>0)r=n+1;else{if(!(s<0))return e[n];i=n-1}}return i>=0&&(0===e[i].runLength||t-e[i].tileId<e[i].runLength)?e[i]:null}X(ge,"findTile");X(class{constructor(e){this.file=e}getKey(){return this.file.name}getBytes(e,t){return ee(this,null,(function*(){return{data:yield this.file.slice(e,e+t).arrayBuffer()}}))}},"FileSource");var me=class{constructor(e,t=new Headers){this.url=e,this.customHeaders=t,this.mustReload=!1;let r="";"navigator"in globalThis&&(r=globalThis.navigator.userAgent||"");let i=r.indexOf("Windows")>-1,n=/Chrome|Chromium|Edg|OPR|Brave/.test(r);this.chromeWindowsNoCache=!1,i&&n&&(this.chromeWindowsNoCache=!0)}getKey(){return this.url}setHeaders(e){this.customHeaders=e}getBytes(e,t,r,i){return ee(this,null,(function*(){let n,s;r?s=r:(n=new AbortController,s=n.signal);let o,a=new Headers(this.customHeaders);a.set("range",`bytes=${e}-${e+t-1}`),this.mustReload?o="reload":this.chromeWindowsNoCache&&(o="no-store");let l=yield fetch(this.url,{signal:s,cache:o,headers:a});if(0===e&&416===l.status){let e=l.headers.get("Content-Range");if(!e||!e.startsWith("bytes */"))throw new Error("Missing content-length on 416 response");let t=+e.substr(8);l=yield fetch(this.url,{signal:s,cache:"reload",headers:{range:"bytes=0-"+(t-1)}})}let h=l.headers.get("Etag");if(null!=h&&h.startsWith("W/")&&(h=null),416===l.status||i&&h&&h!==i)throw this.mustReload=!0,new Te(`Server returned non-matching ETag ${i} after one retry. Check browser extensions and servers for issues that may affect correct ETag headers.`);if(l.status>=300)throw new Error(`Bad response code: ${l.status}`);let c=l.headers.get("Content-Length");if(200===l.status&&(!c||+c>t))throw n&&n.abort(),new Error("Server returned no content-length header or content-length exceeding request. Check that your storage backend supports HTTP Byte Serving.");return{data:yield l.arrayBuffer(),etag:h||void 0,cacheControl:l.headers.get("Cache-Control")||void 0,expires:l.headers.get("Expires")||void 0}}))}};X(me,"FetchSource");var we=me;function ye(e,t){let r=e.getUint32(t+4,!0),i=e.getUint32(t+0,!0);return r*Q(2,32)+i}function ve(e,t){let r=new DataView(e),i=r.getUint8(7);if(i>3)throw new Error(`Archive is spec version ${i} but this library supports up to spec version 3`);return{specVersion:i,rootDirectoryOffset:ye(r,8),rootDirectoryLength:ye(r,16),jsonMetadataOffset:ye(r,24),jsonMetadataLength:ye(r,32),leafDirectoryOffset:ye(r,40),leafDirectoryLength:ye(r,48),tileDataOffset:ye(r,56),tileDataLength:ye(r,64),numAddressedTiles:ye(r,72),numTileEntries:ye(r,80),numTileContents:ye(r,88),clustered:1===r.getUint8(96),internalCompression:r.getUint8(97),tileCompression:r.getUint8(98),tileType:r.getUint8(99),minZoom:r.getUint8(100),maxZoom:r.getUint8(101),minLon:r.getInt32(102,!0)/1e7,minLat:r.getInt32(106,!0)/1e7,maxLon:r.getInt32(110,!0)/1e7,maxLat:r.getInt32(114,!0)/1e7,centerZoom:r.getUint8(118),centerLon:r.getInt32(119,!0)/1e7,centerLat:r.getInt32(123,!0)/1e7,etag:t}}function be(e){let t={buf:new Uint8Array(e),pos:0},r=ne(t),i=[],n=0;for(let e=0;e<r;e++){let e=ne(t);i.push({tileId:n+e,offset:0,length:0,runLength:1}),n+=e}for(let e=0;e<r;e++)i[e].runLength=ne(t);for(let e=0;e<r;e++)i[e].length=ne(t);for(let e=0;e<r;e++){let r=ne(t);i[e].offset=0===r&&e>0?i[e-1].offset+i[e-1].length:r-1}return i}X(ye,"getUint64"),X(ve,"bytesToHeader"),X(be,"deserializeIndex");var xe=class extends Error{};X(xe,"EtagMismatch");var Te=xe;function Pe(e,t){return ee(this,null,(function*(){let r=yield e.getBytes(0,16384);if(19792!==new DataView(r.data).getUint16(0,!0))throw new Error("Wrong magic number for PMTiles archive");let i=ve(r.data.slice(0,127),r.etag),n=r.data.slice(i.rootDirectoryOffset,i.rootDirectoryOffset+i.rootDirectoryLength),s=`${e.getKey()}|${i.etag||""}|${i.rootDirectoryOffset}|${i.rootDirectoryLength}`,o=be(yield t(n,i.internalCompression));return[i,[s,o.length,o]]}))}function ke(e,t,r,i,n){return ee(this,null,(function*(){let s=yield e.getBytes(r,i,void 0,n.etag),o=be(yield t(s.data,n.internalCompression));if(0===o.length)throw new Error("Empty directory is invalid");return o}))}X(Pe,"getHeaderAndRoot"),X(ke,"getDirectory");X(class{constructor(e=100,t=!0,r=de){this.cache=new Map,this.maxCacheEntries=e,this.counter=1,this.decompress=r}getHeader(e){return ee(this,null,(function*(){let t=e.getKey(),r=this.cache.get(t);if(r)return r.lastUsed=this.counter++,r.data;let i=yield Pe(e,this.decompress);return i[1]&&this.cache.set(i[1][0],{lastUsed:this.counter++,data:i[1][2]}),this.cache.set(t,{lastUsed:this.counter++,data:i[0]}),this.prune(),i[0]}))}getDirectory(e,t,r,i){return ee(this,null,(function*(){let n=`${e.getKey()}|${i.etag||""}|${t}|${r}`,s=this.cache.get(n);if(s)return s.lastUsed=this.counter++,s.data;let o=yield ke(e,this.decompress,t,r,i);return this.cache.set(n,{lastUsed:this.counter++,data:o}),this.prune(),o}))}prune(){if(this.cache.size>this.maxCacheEntries){let e,t=1/0;this.cache.forEach(((r,i)=>{r.lastUsed<t&&(t=r.lastUsed,e=i)})),e&&this.cache.delete(e)}}invalidate(e){return ee(this,null,(function*(){this.cache.delete(e.getKey())}))}},"ResolvedValueCache");var Me=class{constructor(e=100,t=!0,r=de){this.cache=new Map,this.invalidations=new Map,this.maxCacheEntries=e,this.counter=1,this.decompress=r}getHeader(e){return ee(this,null,(function*(){let t=e.getKey(),r=this.cache.get(t);if(r)return r.lastUsed=this.counter++,yield r.data;let i=new Promise(((t,r)=>{Pe(e,this.decompress).then((e=>{e[1]&&this.cache.set(e[1][0],{lastUsed:this.counter++,data:Promise.resolve(e[1][2])}),t(e[0]),this.prune()})).catch((e=>{r(e)}))}));return this.cache.set(t,{lastUsed:this.counter++,data:i}),i}))}getDirectory(e,t,r,i){return ee(this,null,(function*(){let n=`${e.getKey()}|${i.etag||""}|${t}|${r}`,s=this.cache.get(n);if(s)return s.lastUsed=this.counter++,yield s.data;let o=new Promise(((n,s)=>{ke(e,this.decompress,t,r,i).then((e=>{n(e),this.prune()})).catch((e=>{s(e)}))}));return this.cache.set(n,{lastUsed:this.counter++,data:o}),o}))}prune(){if(this.cache.size>=this.maxCacheEntries){let e,t=1/0;this.cache.forEach(((r,i)=>{r.lastUsed<t&&(t=r.lastUsed,e=i)})),e&&this.cache.delete(e)}}invalidate(e){return ee(this,null,(function*(){let t=e.getKey();if(this.invalidations.get(t))return yield this.invalidations.get(t);this.cache.delete(e.getKey());let r=new Promise(((r,i)=>{this.getHeader(e).then((e=>{r(),this.invalidations.delete(t)})).catch((e=>{i(e)}))}));this.invalidations.set(t,r)}))}};X(Me,"SharedPromiseCache");var Ee=Me,Ce=class{constructor(e,t,r){this.source="string"==typeof e?new we(e):e,this.decompress=r||de,this.cache=t||new Ee}getHeader(){return ee(this,null,(function*(){return yield this.cache.getHeader(this.source)}))}getZxyAttempt(e,t,r,i){return ee(this,null,(function*(){let n=le(e,t,r),s=yield this.cache.getHeader(this.source);if(e<s.minZoom||e>s.maxZoom)return;let o=s.rootDirectoryOffset,a=s.rootDirectoryLength;for(let e=0;e<=3;e++){let e=ge(yield this.cache.getDirectory(this.source,o,a,s),n);if(!e)return;if(e.runLength>0){let t=yield this.source.getBytes(s.tileDataOffset+e.offset,e.length,i,s.etag);return{data:yield this.decompress(t.data,s.tileCompression),cacheControl:t.cacheControl,expires:t.expires}}o=s.leafDirectoryOffset+e.offset,a=e.length}throw new Error("Maximum directory depth exceeded")}))}getZxy(e,t,r,i){return ee(this,null,(function*(){try{return yield this.getZxyAttempt(e,t,r,i)}catch(n){if(n instanceof Te)return this.cache.invalidate(this.source),yield this.getZxyAttempt(e,t,r,i);throw n}}))}getMetadataAttempt(){return ee(this,null,(function*(){let e=yield this.cache.getHeader(this.source),t=yield this.source.getBytes(e.jsonMetadataOffset,e.jsonMetadataLength,void 0,e.etag),r=yield this.decompress(t.data,e.internalCompression),i=new TextDecoder("utf-8");return JSON.parse(i.decode(r))}))}getMetadata(){return ee(this,null,(function*(){try{return yield this.getMetadataAttempt()}catch(e){if(e instanceof Te)return this.cache.invalidate(this.source),yield this.getMetadataAttempt();throw e}}))}getTileJson(e){return ee(this,null,(function*(){let t=yield this.getHeader(),r=yield this.getMetadata(),i=pe(t.tileType);return{tilejson:"3.0.0",scheme:"xyz",tiles:[`${e}/{z}/{x}/{y}${i}`],vector_layers:r.vector_layers,attribution:r.attribution,description:r.description,name:r.name,version:r.version,bounds:[t.minLon,t.minLat,t.maxLon,t.maxLat],center:[t.centerLon,t.centerLat,t.centerZoom],minzoom:t.minZoom,maxzoom:t.maxZoom}}))}};X(Ce,"PMTiles");var Ue=Ce;let Fe,Ve,Se,Oe;function Le(e,t){return Fe||(Fe=new OffscreenCanvas(e.width,e.height),Ve=Fe.getContext("2d",{willReadFrequently:!0})),Ie(e,t,Fe,Ve)}const Ne=function(){if(null==f&&(f=!1,u()&&"undefined"!=typeof VideoFrame)){const e=5,t=new OffscreenCanvas(5,5).getContext("2d",{willReadFrequently:!0});if(t){for(let r=0;r<e*e;r++){const i=4*r;t.fillStyle=`rgb(${i},${i+1},${i+2})`,t.fillRect(r%e,Math.floor(r/e),1,1)}const r=t.getImageData(0,0,e,e).data;for(let t=0;t<e*e*4;t++)if(t%4!=3&&r[t]!==t){f=!0;break}}}return f||!1}()?function(e,t,r){return a(this,void 0,void 0,(function*(){var i,n,s;const o=yield createImageBitmap(e);if(m(r))return null;const a=new VideoFrame(o,{timestamp:0});try{if(!((null===(i=null==a?void 0:a.format)||void 0===i?void 0:i.startsWith("BGR"))||(null===(n=null==a?void 0:a.format)||void 0===n?void 0:n.startsWith("RGB"))))throw new Error(`Unrecognized format: ${null==a?void 0:a.format}`);const e=null===(s=null==a?void 0:a.format)||void 0===s?void 0:s.startsWith("BGR"),r=a.allocationSize(),l=new Uint8ClampedArray(r);if(yield a.copyTo(l),e)for(let e=0;e<l.length;e+=4){const t=l[e];l[e]=l[e+2],l[e+2]=t}return De(o.width,o.height,t,l)}catch(e){return m(r)?null:Le(o,t)}finally{a.close()}}))}:u()?function(e,t,r){return a(this,void 0,void 0,(function*(){const i=yield createImageBitmap(e);return m(r)?null:Le(i,t)}))}:"undefined"!=typeof WorkerGlobalScope&&"undefined"!=typeof self&&self instanceof WorkerGlobalScope?function(e,t,r){return self.actor.send("decodeImage",[],r,void 0,e,t)}:"undefined"!=typeof document?function(e,t,r){return a(this,void 0,void 0,(function*(){Se||(Se=document.createElement("canvas"),Oe=Se.getContext("2d",{willReadFrequently:!0}));const i=new Image;g(r,(()=>i.src=""));return Ie(yield new Promise(((t,n)=>{i.onload=()=>{m(r)||t(i),URL.revokeObjectURL(i.src),i.onload=null},i.onerror=()=>n(new Error("Could not load image.")),i.src=e.size?URL.createObjectURL(e):""})),t,Se,Oe)}))}:function(e,t,r){return a(this,void 0,void 0,(function*(){const e=yield function(e,t,r){return a(this,void 0,void 0,(function*(){}))}();return m(r)?null:e}))};function Ie(e,t,r,i){if(r.width=e.width,r.height=e.height,!i)throw new Error("failed to get context");i.drawImage(e,0,0,e.width,e.height);const n=i.getImageData(0,0,e.width,e.height).data;return De(e.width,e.height,t,n)}function De(e,t,r,i){const n="mapbox"===r?(e,t,r)=>.1*(256*e*256+256*t+r)-1e4:(e,t,r)=>256*e+t+r/256-32768,s=new Float32Array(e*t);for(let e=0;e<i.length;e+=4)s[e/4]=n(i[e],i[e+1],i[e+2]);return{width:e,height:t,data:s}}class $e{constructor(e,t,r){this.split=(e,t,r)=>{if(0===e)return this;const i=1<<e,n=t*this.width/i,s=r*this.height/i;return new $e(this.width/i,this.height/i,((e,t)=>this.get(e+n,t+s)))},this.subsamplePixelCenters=e=>{const t=(e,t,r)=>isNaN(e)?t:isNaN(t)?e:e+(t-e)*r;if(e<=1)return this;const r=.5-1/(2*e);return new $e(this.width*e,this.height*e,((i,n)=>{const s=i/e-r,o=n/e-r,a=Math.floor(s),l=Math.floor(o),h=this.get(a,l),c=this.get(a+1,l),d=this.get(a,l+1),u=this.get(a+1,l+1),f=s-a,p=o-l,g=t(h,c,f),m=t(d,u,f);return t(g,m,p)}))},this.averagePixelCentersToGrid=(e=1)=>new $e(this.width+1,this.height+1,((t,r)=>{let i=0,n=0,s=0;for(let o=t-e;o<t+e;o++)for(let t=r-e;t<r+e;t++)isNaN(s=this.get(o,t))||(n++,i+=s);return 0===n?NaN:i/n})),this.scaleElevation=e=>1===e?this:new $e(this.width,this.height,((t,r)=>this.get(t,r)*e)),this.materialize=(e=2)=>{const t=this.width+2*e,r=new Float32Array(t*(this.height+2*e));let i=0;for(let t=-e;t<this.height+e;t++)for(let n=-e;n<this.width+e;n++)r[i++]=this.get(n,t);return new $e(this.width,this.height,((i,n)=>r[(n+e)*t+i+e]))},this.get=r,this.width=e,this.height=t}static fromRawDem(e){return new $e(e.width,e.height,((t,r)=>{const i=e.data[r*e.width+t];return n=i,!isNaN(n)&&n>=-12e3&&n<=9e3?i:NaN;var n}))}static combineNeighbors(e){if(9!==e.length)throw new Error("Must include a tile plus 8 neighbors");const t=e[4];if(!t)return;const r=t.width,i=t.height;return new $e(r,i,((t,n)=>{let s=0;n<0?n+=i:n<i?s+=3:(n-=i,s+=6),t<0?t+=r:t<r?s+=1:(t-=r,s+=2);const o=e[s];return o?o.get(t,n):NaN}))}}const Ae=4294967296,Be=1/Ae,ze="undefined"==typeof TextDecoder?null:new TextDecoder("utf-8");class Re{constructor(e=new Uint8Array(16)){this.buf=ArrayBuffer.isView(e)?e:new Uint8Array(e),this.dataView=new DataView(this.buf.buffer),this.pos=0,this.type=0,this.length=this.buf.length}readFields(e,t,r=this.length){for(;this.pos<r;){const r=this.readVarint(),i=r>>3,n=this.pos;this.type=7&r,e(i,t,this),this.pos===n&&this.skip(r)}return t}readMessage(e,t){return this.readFields(e,t,this.readVarint()+this.pos)}readFixed32(){const e=this.dataView.getUint32(this.pos,!0);return this.pos+=4,e}readSFixed32(){const e=this.dataView.getInt32(this.pos,!0);return this.pos+=4,e}readFixed64(){const e=this.dataView.getUint32(this.pos,!0)+this.dataView.getUint32(this.pos+4,!0)*Ae;return this.pos+=8,e}readSFixed64(){const e=this.dataView.getUint32(this.pos,!0)+this.dataView.getInt32(this.pos+4,!0)*Ae;return this.pos+=8,e}readFloat(){const e=this.dataView.getFloat32(this.pos,!0);return this.pos+=4,e}readDouble(){const e=this.dataView.getFloat64(this.pos,!0);return this.pos+=8,e}readVarint(e){const t=this.buf;let r,i;return i=t[this.pos++],r=127&i,i<128?r:(i=t[this.pos++],r|=(127&i)<<7,i<128?r:(i=t[this.pos++],r|=(127&i)<<14,i<128?r:(i=t[this.pos++],r|=(127&i)<<21,i<128?r:(i=t[this.pos],r|=(15&i)<<28,function(e,t,r){const i=r.buf;let n,s;if(s=i[r.pos++],n=(112&s)>>4,s<128)return je(e,n,t);if(s=i[r.pos++],n|=(127&s)<<3,s<128)return je(e,n,t);if(s=i[r.pos++],n|=(127&s)<<10,s<128)return je(e,n,t);if(s=i[r.pos++],n|=(127&s)<<17,s<128)return je(e,n,t);if(s=i[r.pos++],n|=(127&s)<<24,s<128)return je(e,n,t);if(s=i[r.pos++],n|=(1&s)<<31,s<128)return je(e,n,t);throw new Error("Expected varint not more than 10 bytes")}(r,e,this)))))}readVarint64(){return this.readVarint(!0)}readSVarint(){const e=this.readVarint();return e%2==1?(e+1)/-2:e/2}readBoolean(){return Boolean(this.readVarint())}readString(){const e=this.readVarint()+this.pos,t=this.pos;return this.pos=e,e-t>=12&&ze?ze.decode(this.buf.subarray(t,e)):function(e,t,r){let i="",n=t;for(;n<r;){const t=e[n];let s,o,a,l=null,h=t>239?4:t>223?3:t>191?2:1;if(n+h>r)break;1===h?t<128&&(l=t):2===h?(s=e[n+1],128==(192&s)&&(l=(31&t)<<6|63&s,l<=127&&(l=null))):3===h?(s=e[n+1],o=e[n+2],128==(192&s)&&128==(192&o)&&(l=(15&t)<<12|(63&s)<<6|63&o,(l<=2047||l>=55296&&l<=57343)&&(l=null))):4===h&&(s=e[n+1],o=e[n+2],a=e[n+3],128==(192&s)&&128==(192&o)&&128==(192&a)&&(l=(15&t)<<18|(63&s)<<12|(63&o)<<6|63&a,(l<=65535||l>=1114112)&&(l=null))),null===l?(l=65533,h=1):l>65535&&(l-=65536,i+=String.fromCharCode(l>>>10&1023|55296),l=56320|1023&l),i+=String.fromCharCode(l),n+=h}return i}(this.buf,t,e)}readBytes(){const e=this.readVarint()+this.pos,t=this.buf.subarray(this.pos,e);return this.pos=e,t}readPackedVarint(e=[],t){const r=this.readPackedEnd();for(;this.pos<r;)e.push(this.readVarint(t));return e}readPackedSVarint(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readSVarint());return e}readPackedBoolean(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readBoolean());return e}readPackedFloat(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readFloat());return e}readPackedDouble(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readDouble());return e}readPackedFixed32(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readFixed32());return e}readPackedSFixed32(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readSFixed32());return e}readPackedFixed64(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readFixed64());return e}readPackedSFixed64(e=[]){const t=this.readPackedEnd();for(;this.pos<t;)e.push(this.readSFixed64());return e}readPackedEnd(){return 2===this.type?this.readVarint()+this.pos:this.pos+1}skip(e){const t=7&e;if(0===t)for(;this.buf[this.pos++]>127;);else if(2===t)this.pos=this.readVarint()+this.pos;else if(5===t)this.pos+=4;else{if(1!==t)throw new Error(`Unimplemented type: ${t}`);this.pos+=8}}writeTag(e,t){this.writeVarint(e<<3|t)}realloc(e){let t=this.length||16;for(;t<this.pos+e;)t*=2;if(t!==this.length){const e=new Uint8Array(t);e.set(this.buf),this.buf=e,this.dataView=new DataView(e.buffer),this.length=t}}finish(){return this.length=this.pos,this.pos=0,this.buf.subarray(0,this.length)}writeFixed32(e){this.realloc(4),this.dataView.setInt32(this.pos,e,!0),this.pos+=4}writeSFixed32(e){this.realloc(4),this.dataView.setInt32(this.pos,e,!0),this.pos+=4}writeFixed64(e){this.realloc(8),this.dataView.setInt32(this.pos,-1&e,!0),this.dataView.setInt32(this.pos+4,Math.floor(e*Be),!0),this.pos+=8}writeSFixed64(e){this.realloc(8),this.dataView.setInt32(this.pos,-1&e,!0),this.dataView.setInt32(this.pos+4,Math.floor(e*Be),!0),this.pos+=8}writeVarint(e){(e=+e||0)>268435455||e<0?function(e,t){let r,i;e>=0?(r=e%4294967296|0,i=e/4294967296|0):(r=~(-e%4294967296),i=~(-e/4294967296),4294967295^r?r=r+1|0:(r=0,i=i+1|0));if(e>=0x10000000000000000||e<-0x10000000000000000)throw new Error("Given varint doesn't fit into 10 bytes");t.realloc(10),function(e,t,r){r.buf[r.pos++]=127&e|128,e>>>=7,r.buf[r.pos++]=127&e|128,e>>>=7,r.buf[r.pos++]=127&e|128,e>>>=7,r.buf[r.pos++]=127&e|128,e>>>=7,r.buf[r.pos]=127&e}(r,0,t),function(e,t){const r=(7&e)<<4;if(t.buf[t.pos++]|=r|((e>>>=3)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;t.buf[t.pos++]=127&e}(i,t)}(e,this):(this.realloc(4),this.buf[this.pos++]=127&e|(e>127?128:0),e<=127||(this.buf[this.pos++]=127&(e>>>=7)|(e>127?128:0),e<=127||(this.buf[this.pos++]=127&(e>>>=7)|(e>127?128:0),e<=127||(this.buf[this.pos++]=e>>>7&127))))}writeSVarint(e){this.writeVarint(e<0?2*-e-1:2*e)}writeBoolean(e){this.writeVarint(+e)}writeString(e){e=String(e),this.realloc(4*e.length),this.pos++;const t=this.pos;this.pos=function(e,t,r){for(let i,n,s=0;s<t.length;s++){if(i=t.charCodeAt(s),i>55295&&i<57344){if(!n){i>56319||s+1===t.length?(e[r++]=239,e[r++]=191,e[r++]=189):n=i;continue}if(i<56320){e[r++]=239,e[r++]=191,e[r++]=189,n=i;continue}i=n-55296<<10|i-56320|65536,n=null}else n&&(e[r++]=239,e[r++]=191,e[r++]=189,n=null);i<128?e[r++]=i:(i<2048?e[r++]=i>>6|192:(i<65536?e[r++]=i>>12|224:(e[r++]=i>>18|240,e[r++]=i>>12&63|128),e[r++]=i>>6&63|128),e[r++]=63&i|128)}return r}(this.buf,e,this.pos);const r=this.pos-t;r>=128&&He(t,r,this),this.pos=t-1,this.writeVarint(r),this.pos+=r}writeFloat(e){this.realloc(4),this.dataView.setFloat32(this.pos,e,!0),this.pos+=4}writeDouble(e){this.realloc(8),this.dataView.setFloat64(this.pos,e,!0),this.pos+=8}writeBytes(e){const t=e.length;this.writeVarint(t),this.realloc(t);for(let r=0;r<t;r++)this.buf[this.pos++]=e[r]}writeRawMessage(e,t){this.pos++;const r=this.pos;e(t,this);const i=this.pos-r;i>=128&&He(r,i,this),this.pos=r-1,this.writeVarint(i),this.pos+=i}writeMessage(e,t,r){this.writeTag(e,2),this.writeRawMessage(t,r)}writePackedVarint(e,t){t.length&&this.writeMessage(e,We,t)}writePackedSVarint(e,t){t.length&&this.writeMessage(e,Ze,t)}writePackedBoolean(e,t){t.length&&this.writeMessage(e,_e,t)}writePackedFloat(e,t){t.length&&this.writeMessage(e,Ge,t)}writePackedDouble(e,t){t.length&&this.writeMessage(e,Ke,t)}writePackedFixed32(e,t){t.length&&this.writeMessage(e,Je,t)}writePackedSFixed32(e,t){t.length&&this.writeMessage(e,qe,t)}writePackedFixed64(e,t){t.length&&this.writeMessage(e,Ye,t)}writePackedSFixed64(e,t){t.length&&this.writeMessage(e,Qe,t)}writeBytesField(e,t){this.writeTag(e,2),this.writeBytes(t)}writeFixed32Field(e,t){this.writeTag(e,5),this.writeFixed32(t)}writeSFixed32Field(e,t){this.writeTag(e,5),this.writeSFixed32(t)}writeFixed64Field(e,t){this.writeTag(e,1),this.writeFixed64(t)}writeSFixed64Field(e,t){this.writeTag(e,1),this.writeSFixed64(t)}writeVarintField(e,t){this.writeTag(e,0),this.writeVarint(t)}writeSVarintField(e,t){this.writeTag(e,0),this.writeSVarint(t)}writeStringField(e,t){this.writeTag(e,2),this.writeString(t)}writeFloatField(e,t){this.writeTag(e,5),this.writeFloat(t)}writeDoubleField(e,t){this.writeTag(e,1),this.writeDouble(t)}writeBooleanField(e,t){this.writeVarintField(e,+t)}}function je(e,t,r){return r?4294967296*t+(e>>>0):4294967296*(t>>>0)+(e>>>0)}function He(e,t,r){const i=t<=16383?1:t<=2097151?2:t<=268435455?3:Math.floor(Math.log(t)/(7*Math.LN2));r.realloc(i);for(let t=r.pos-1;t>=e;t--)r.buf[t+i]=r.buf[t]}function We(e,t){for(let r=0;r<e.length;r++)t.writeVarint(e[r])}function Ze(e,t){for(let r=0;r<e.length;r++)t.writeSVarint(e[r])}function Ge(e,t){for(let r=0;r<e.length;r++)t.writeFloat(e[r])}function Ke(e,t){for(let r=0;r<e.length;r++)t.writeDouble(e[r])}function _e(e,t){for(let r=0;r<e.length;r++)t.writeBoolean(e[r])}function Je(e,t){for(let r=0;r<e.length;r++)t.writeFixed32(e[r])}function qe(e,t){for(let r=0;r<e.length;r++)t.writeSFixed32(e[r])}function Ye(e,t){for(let r=0;r<e.length;r++)t.writeFixed64(e[r])}function Qe(e,t){for(let r=0;r<e.length;r++)t.writeSFixed64(e[r])}var Xe;function et(e,t){if(!t)throw new Error("pbf undefined");t.writeVarintField(15,2),t.writeStringField(1,e.id||""),t.writeVarintField(5,e.extent||4096);const r={keys:[],values:[],keycache:{},valuecache:{}};for(const i of e.features)r.feature=i,t.writeMessage(2,tt,r);for(const e of r.keys)t.writeStringField(3,e);for(const e of r.values)t.writeMessage(4,ot,e)}function tt(e,t){const r=e.feature;if(!r||!t)throw new Error;t.writeMessage(2,rt,e),t.writeVarintField(3,r.type),t.writeMessage(4,st,r)}function rt(e,t){const r=e.feature;if(!r||!t)throw new Error;const i=e.keys,n=e.values,s=e.keycache,o=e.valuecache;for(const e in r.properties){let a=r.properties[e],l=s[e];if(null===a)continue;void 0===l&&(i.push(e),l=i.length-1,s[e]=l),t.writeVarint(l);const h=typeof a;"string"!==h&&"boolean"!==h&&"number"!==h&&(a=JSON.stringify(a));const c=`${h}:${a}`;let d=o[c];void 0===d&&(n.push(a),d=n.length-1,o[c]=d),t.writeVarint(d)}}function it(e,t){return(t<<3)+(7&e)}function nt(e){return e<<1^e>>31}function st(e,t){if(!t)throw new Error;const r=e.geometry,i=e.type;let n=0,s=0;for(const e of r){let r=1;i===Xe.POINT&&(r=e.length/2),t.writeVarint(it(1,r));const o=e.length/2,a=i===Xe.POLYGON?o-1:o;for(let r=0;r<a;r++){1===r&&1!==i&&t.writeVarint(it(2,a-1));const o=e[2*r]-n,l=e[2*r+1]-s;t.writeVarint(nt(o)),t.writeVarint(nt(l)),n+=o,s+=l}i===Xe.POLYGON&&t.writeVarint(it(7,1))}}function ot(e,t){if(!t)throw new Error;"string"==typeof e?t.writeStringField(1,e):"boolean"==typeof e?t.writeBooleanField(7,e):"number"==typeof e&&(e%1!=0?t.writeDoubleField(3,e):e<0?t.writeSVarintField(6,e):t.writeVarintField(5,e))}!function(e){e[e.UNKNOWN=0]="UNKNOWN",e[e.POINT=1]="POINT",e[e.LINESTRING=2]="LINESTRING",e[e.POLYGON=3]="POLYGON"}(Xe||(Xe={}));const at="undefined"!=typeof performance?performance:void 0,lt=at?at.timeOrigin||(new Date).getTime()-at.now():(new Date).getTime();function ht(e){var t;return JSON.parse(JSON.stringify((null===(t=null==at?void 0:at.getEntriesByName)||void 0===t?void 0:t.call(at,e))||[]))}function ct(){return at?at.now():(new Date).getTime()}function dt(e){const t=[];for(const r of e)t.push(...r);return t}class ut{constructor(e){this.marks={},this.urls=[],this.fetched=[],this.resources=[],this.tilesFetched=0,this.timeOrigin=lt,this.finish=e=>{this.markFinish();const t=e=>{const t=this.marks[e]||[],r=Math.max(...t.map((e=>Math.max(...e)))),i=Math.min(...t.map((e=>Math.min(...e))));return Number.isFinite(r)?r-i:void 0},r=t("main")||0,i=t("fetch"),n=t("decode"),s=t("isoline");return{url:e,tilesUsed:this.tilesFetched,origin:this.timeOrigin,marks:this.marks,resources:[...this.resources,...dt(this.fetched.map(ht))],duration:r,fetch:i,decode:n,process:s,wait:r-(i||0)-(n||0)-(s||0)}},this.error=e=>Object.assign(Object.assign({},this.finish(e)),{error:!0}),this.marker=e=>{var t;this.marks[e]||(this.marks[e]=[]);const r=[ct()];return null===(t=this.marks[e])||void 0===t||t.push(r),()=>r.push(ct())},this.useTile=e=>{this.urls.indexOf(e)<0&&(this.urls.push(e),this.tilesFetched++)},this.fetchTile=e=>{this.fetched.indexOf(e)<0&&this.fetched.push(e)},this.addAll=e=>{var t;this.tilesFetched+=e.tilesUsed;const r=e.origin-this.timeOrigin;for(const i in e.marks){const n=i;(this.marks[n]||(this.marks[n]=[])).push(...(null===(t=e.marks[n])||void 0===t?void 0:t.map((e=>e.map((e=>e+r)))))||[])}this.resources.push(...e.resources.map((e=>function(e,t){const r={};for(const i in e)0!==e[i]&&ft.test(i)?r[i]=Number(e[i])+t:r[i]=e[i];return r}(e,r))))},this.markFinish=this.marker(e)}}const ft=/(Start$|End$|^start|^end)/;const pt=(e,t,r,i,n)=>a(void 0,void 0,void 0,(function*(){const s=i.replace("{z}",e.toString()).replace("{x}",t.toString()).replace("{y}",r.toString()),o={signal:n.signal},a=yield fetch(s,o);if(!a.ok)throw new Error(`Bad response: ${a.status} for ${s}`);return{data:yield a.blob(),expires:a.headers.get("expires")||void 0,cacheControl:a.headers.get("cache-control")||void 0}})),gt=(e,t,r,i,n,s)=>a(void 0,void 0,void 0,(function*(){if(!s)throw new Error("pmtiles is not initialized.");if(n.signal.aborted)throw new Error("Request aborted by parent.");try{const i=yield function(e,t,r,i){return a(this,void 0,void 0,(function*(){try{const n=yield e.getZxy(t,r,i);return n&&n.data?{data:n.data}:{data:void 0}}catch(e){return console.error("Error fetching tile:",e),{data:void 0}}}))}(s,e,t,r);if(i&&i.data){return{data:new Blob([i.data]),expires:void 0,cacheControl:void 0}}throw new Error(`Tile data not found for z:${e} x:${t} y:${r}`)}catch(i){throw new Error(`Failed to fetch DEM tile for z:${e} x:${t} y:${r} from PMTiles: ${i}`)}}));let mt=0;e.A=class{constructor(e,t,r=2e4){this.callbacks={},this.cancels={},this.dest=e,this.timeoutMs=r,this.dest.onmessage=e=>a(this,[e],void 0,(function*({data:e}){const r=e;if("cancel"===r.type){const e=this.cancels[r.id];delete this.cancels[r.id],null==e||e.abort()}else if("response"===r.type){const e=this.callbacks[r.id];delete this.callbacks[r.id],e&&e(r.error?new Error(r.error):void 0,r.response,r.timings)}else if("request"===r.type){const e=new ut("worker"),i=t[r.name],n=new AbortController,s=i.apply(i,[...r.args,n,e]),o=`${r.name}_${r.id}`;if(r.id&&s){this.cancels[r.id]=n;try{const t=yield s,i=null==t?void 0:t.transferrables;this.postMessage({id:r.id,type:"response",response:t,timings:e.finish(o)},i)}catch(t){this.postMessage({id:r.id,type:"response",error:(null==t?void 0:t.toString())||"error",timings:e.finish(o)})}delete this.cancels[r.id]}}}))}postMessage(e,t){this.dest.postMessage(e,t||[])}send(e,t,r,i,...n){const s=++mt,o=new Promise(((r,o)=>{this.postMessage({id:s,type:"request",name:e,args:n},t),this.callbacks[s]=(e,t,n)=>{null==i||i.addAll(n),e?o(e):r(t)}}));return g(r,(()=>{delete this.callbacks[s],this.postMessage({id:s,type:"cancel"})})),p(this.timeoutMs,o,r)}},e.H=$e,e.L=class{constructor(e){if(this.pmtiles=null,this.loaded=Promise.resolve(),this.fetchAndParseTile=(e,t,r,i,n)=>{const s=this,o=`${e}/${t}/${r}`;return null==n||n.useTile(o),this.parsedCache.get(o,((i,o)=>a(this,void 0,void 0,(function*(){const i=yield s.fetchTile(e,t,r,o,n);if(m(o))throw new Error("canceled");const a=s.decodeImage(i.data,s.encoding,o),l=null==n?void 0:n.marker("decode"),h=yield a;return null==l||l(),h}))),i)},this.tileCache=new y(e.cacheSize),this.parsedCache=new y(e.cacheSize),this.contourCache=new y(e.cacheSize),this.timeoutMs=e.timeoutMs,this.demUrlPattern=e.demUrlPattern,this.encoding=e.encoding,this.maxzoom=e.maxzoom,this.decodeImage=e.decodeImage||Ne,this.demUrlPattern.startsWith("pmtiles://"))try{this.pmtiles=function(e){const t=new we(e);return new Ue(t)}(this.demUrlPattern.replace("pmtiles://",""))}catch(e){console.warn("Could not open pmtiles",e)}this.getTile=e.getTile||(this.demUrlPattern.startsWith("pmtiles://")?gt:pt)}fetchTile(e,t,r,i,n){const s=`${e}/${t}/${r}`;return null==n||n.useTile(s),this.tileCache.get(s,((i,o)=>{null==n||n.fetchTile(s);const a=null==n?void 0:n.marker("fetch");return p(this.timeoutMs,this.getTile(e,t,r,this.demUrlPattern,o,this.pmtiles).finally((()=>null==a?void 0:a())),o)}),i)}fetchDem(e,t,r,i,n,s){return a(this,void 0,void 0,(function*(){const o=Math.min(e-(i.overzoom||0),this.maxzoom),a=e-o,l=1<<a,h=Math.floor(t/l),c=Math.floor(r/l),d=yield this.fetchAndParseTile(o,h,c,n,s);return $e.fromRawDem(d).split(a,t%l,r%l)}))}fetchContourTile(e,t,r,i,n,o){const{levels:l,multiplier:h=1,buffer:d=1,extent:u=4096,contourLayer:f="contours",elevationKey:p="ele",levelKey:g="level",subsampleBelow:w=100}=i;if(!l||0===l.length)return Promise.resolve({arrayBuffer:new ArrayBuffer(0)});const y=[e,t,r,c(i)].join("/");return this.contourCache.get(y,((n,c)=>a(this,void 0,void 0,(function*(){const n=1<<e,a=[];for(let s=r-1;s<=r+1;s++)for(let r=t-1;r<=t+1;r++)a.push(s<0||s>=n?void 0:this.fetchDem(e,(r+n)%n,s,i,c,o));const y=yield Promise.all(a);let v=$e.combineNeighbors(y);if(!v||m(c))return{arrayBuffer:(new Uint8Array).buffer};const b=null==o?void 0:o.marker("isoline");if(v.width>=w)v=v.materialize(2);else for(;v.width<w;)v=v.subsamplePixelCenters(2).materialize(2);v=v.averagePixelCentersToGrid().scaleElevation(h).materialize(1);const x=s(l[0],v,u,d);null==b||b();const T=function(e){const t=new Re;for(const r in e.layers){const i=e.layers[r];i.extent||(i.extent=e.extent),t.writeMessage(3,et,Object.assign(Object.assign({},i),{id:r}))}return t.finish()}({extent:u,layers:{[f]:{features:Object.entries(x).map((([e,t])=>{const r=Number(e);return{type:Xe.LINESTRING,geometry:t,properties:{[p]:r,[g]:Math.max(...l.map(((e,t)=>r%e==0?t:0)))}}}))}}});return null==b||b(),{arrayBuffer:T.buffer}}))),n)}},e.T=ut,e._=a,e.a=function(e){return Object.fromEntries(e.replace(/^.*\?/,"").split("&").map((e=>{const t=e.split("=").map(decodeURIComponent),r=t[0];let i=t[1];switch(r){case"thresholds":n=i,i=Object.fromEntries(n.split("~").map((e=>e.split("*").map(Number))).map((([e,...t])=>[e,t])));break;case"extent":case"multiplier":case"overzoom":case"buffer":i=Number(i)}var n;return[r,i]})))},e.b=De,e.c=s,e.d=Ne,e.e=function(e){var{thresholds:t}=e,r=o(e,["thresholds"]);return l(Object.assign({thresholds:h(t)},r)).map((([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`)).join("&")},e.f=function(e){return e.then((({arrayBuffer:e})=>{const t=function(e){const t=new ArrayBuffer(e.byteLength);return new Uint8Array(t).set(new Uint8Array(e)),t}(e);return{arrayBuffer:t,transferrables:[t]}}))},e.g=function(e,t){const{thresholds:r}=e,i=o(e,["thresholds"]);let n=[];if(r){let e=-1/0;Object.entries(r).forEach((([r,i])=>{const s=Number(r);s<=t&&s>e&&(e=s,n="number"==typeof i?[i]:i)}))}return Object.assign({levels:n},i)},e.p=function(e,t){return e.then((e=>{var{data:r}=e,i=o(e,["data"]);let n=r;return t&&(n=new Float32Array(r.length),n.set(r)),Object.assign(Object.assign({},i),{data:n,transferrables:[n.buffer]})}))}})),i(0,(function(e){const t=e=>Promise.reject(new Error(`No manager registered for ${e}`));const r="undefined"!=typeof self?self:"undefined"!=typeof window?window:global;r.actor=new e.A(r,new class{constructor(){this.managers={},this.init=(t,r)=>(this.managers[t.managerId]=new e.L(t),Promise.resolve()),this.fetchTile=(e,r,i,n,s,o)=>{var a;return(null===(a=this.managers[e])||void 0===a?void 0:a.fetchTile(r,i,n,s,o))||t(e)},this.fetchAndParseTile=(r,i,n,s,o,a)=>{var l;return e.p((null===(l=this.managers[r])||void 0===l?void 0:l.fetchAndParseTile(i,n,s,o,a))||t(r),!0)},this.fetchContourTile=(r,i,n,s,o,a,l)=>{var h;return e.f((null===(h=this.managers[r])||void 0===h?void 0:h.fetchContourTile(i,n,s,o,a,l))||t(r))}}})})),i(0,(function(e){const t={workerUrl:""};let r,i=0;class n{constructor(){this.decodeImage=(t,r,i)=>e.p(e.d(t,r,i),!1)}}class s{constructor(s){this.fetchTile=(e,t,r,i,n)=>this.actor.send("fetchTile",[],i,n,this.managerId,e,t,r),this.fetchAndParseTile=(e,t,r,i,n)=>this.actor.send("fetchAndParseTile",[],i,n,this.managerId,e,t,r),this.fetchContourTile=(e,t,r,i,n,s)=>this.actor.send("fetchContourTile",[],n,s,this.managerId,e,t,r,i);const o=this.managerId=++i;this.actor=s.actor||function(){if(!r){const i=new Worker(t.workerUrl),s=new n;r=new e.A(i,s)}return r}(),this.loaded=this.actor.send("init",[],new AbortController,void 0,Object.assign(Object.assign({},s),{managerId:o}))}}Blob.prototype.arrayBuffer||(Blob.prototype.arrayBuffer=function(){return new Promise(((e,t)=>{const r=new FileReader;r.onload=t=>{var r;return e(null===(r=t.target)||void 0===r?void 0:r.result)},r.onerror=t,r.readAsArrayBuffer(this)}))});const o=e=>(t,r)=>{if(r instanceof AbortController)return e(t,r);{const i=new AbortController;return e(t,i).then((e=>r(void 0,e.data,e.cacheControl,e.expires)),(e=>r(e))).catch((e=>r(e))),{cancel:()=>i.abort()}}},a=new Set;const l={generateIsolines:e.c,DemSource:class{constructor({url:t,cacheSize:r=100,id:i="dem",encoding:n="terrarium",maxzoom:l=12,worker:h=!0,timeoutMs:c=1e4,actor:d}){this.timingCallbacks=[],this.onTiming=e=>{this.timingCallbacks.push(e)},this.setupMaplibre=e=>{e.addProtocol(this.sharedDemProtocolId,this.sharedDemProtocol),e.addProtocol(this.contourProtocolId,this.contourProtocol)},this.sharedDemProtocolV4=(t,r)=>e._(this,void 0,void 0,(function*(){const[i,n,s]=this.parseUrl(t.url),o=new e.T("main");let a;try{const e=yield this.manager.fetchTile(i,n,s,r,o);a=o.finish(t.url);return{data:yield e.data.arrayBuffer(),cacheControl:e.cacheControl,expires:e.expires}}catch(e){throw a=o.error(t.url),e}finally{this.timingCallbacks.forEach((e=>e(a)))}})),this.contourProtocolV4=(t,r)=>e._(this,void 0,void 0,(function*(){const i=new e.T("main");let n;try{const[s,o,a]=this.parseUrl(t.url),l=e.a(t.url),h=yield this.manager.fetchContourTile(s,o,a,e.g(l,s),r,i);return n=i.finish(t.url),{data:h.arrayBuffer}}catch(e){throw n=i.error(t.url),e}finally{this.timingCallbacks.forEach((e=>e(n)))}})),this.contourProtocol=o(this.contourProtocolV4),this.sharedDemProtocol=o(this.sharedDemProtocolV4),this.contourProtocolUrl=t=>`${this.contourProtocolUrlBase}?${e.e(t)}`;let u=i,f=1;for(;a.has(u);)u=i+f++;a.add(u),this.sharedDemProtocolId=`${u}-shared`,this.contourProtocolId=`${u}-contour`,this.sharedDemProtocolUrl=`${this.sharedDemProtocolId}://{z}/{x}/{y}`,this.contourProtocolUrlBase=`${this.contourProtocolId}://{z}/{x}/{y}`;const p=h?s:e.L;this.manager=new p({demUrlPattern:t,cacheSize:r,encoding:n,maxzoom:l,timeoutMs:c,actor:d})}getDemTile(e,t,r,i){return this.manager.fetchAndParseTile(e,t,r,i||new AbortController)}parseUrl(e){const[,t,r,i]=/\/\/(\d+)\/(\d+)\/(\d+)/.exec(e)||[];return[Number(t),Number(r),Number(i)]}},HeightTile:e.H,LocalDemManager:e.L,decodeParsedImage:e.b,set workerUrl(e){t.workerUrl=e},get workerUrl(){return t.workerUrl}};return l})),r}));