diff --git a/docs/examples/mapbox_draw_plugin/app.py b/docs/examples/mapbox_draw_plugin/app.py new file mode 100644 index 00000000..440e59c3 --- /dev/null +++ b/docs/examples/mapbox_draw_plugin/app.py @@ -0,0 +1,36 @@ +# Shiny Express App + +import json + +from maplibre import Map, MapOptions, render_maplibregl +from maplibre.basemaps import Carto +from maplibre.controls import NavigationControl +from maplibre.ui import use_mapboxgl_draw + +# from shiny import reactive +from shiny.express import input, render, ui + +m = Map( + MapOptions( + style=Carto.POSITRON, + center=(-122.4, 37.74), + zoom=12, + hash=True, + pitch=40, + ) +) +m.add_control(NavigationControl()) + + +# Shiny Express +use_mapboxgl_draw() + + +@render_maplibregl +def render_map(): + return m + + +if __name__ == "__main__": + with open("docs/examples/mapbox_draw_plugin/app.html", "w") as f: + f.write(m.to_html()) diff --git a/maplibre/srcjs/index.js b/maplibre/srcjs/index.js index 0aee72fa..59545775 100644 --- a/maplibre/srcjs/index.js +++ b/maplibre/srcjs/index.js @@ -1,7 +1,7 @@ -(()=>{var I=Math.pow,w=(t,e,r)=>new Promise((n,i)=>{var s=c=>{try{o(r.next(c))}catch(u){i(u)}},a=c=>{try{o(r.throw(c))}catch(u){i(u)}},o=c=>c.done?n(c.value):Promise.resolve(c.value).then(s,a);o((r=r.apply(t,e)).next())}),E=Uint8Array,H=Uint16Array,qe=Int32Array,Ce=new E([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]),ke=new E([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]),Ge=new E([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),Me=function(t,e){for(var r=new H(31),n=0;n<31;++n)r[n]=e+=1<>1|(p&21845)<<1,P=(P&52428)>>2|(P&13107)<<2,P=(P&61680)>>4|(P&3855)<<4,oe[p]=((P&65280)>>8|(P&255)<<8)>>1;var P,p,F=function(t,e,r){for(var n=t.length,i=0,s=new H(e);i>c]=u}else for(o=new H(n),i=0;i>15-t[i]);return o},J=new E(288);for(p=0;p<144;++p)J[p]=8;var p;for(p=144;p<256;++p)J[p]=9;var p;for(p=256;p<280;++p)J[p]=7;var p;for(p=280;p<288;++p)J[p]=8;var p,Oe=new E(32);for(p=0;p<32;++p)Oe[p]=5;var p,Qe=F(J,9,1),et=F(Oe,5,1),se=function(t){for(var e=t[0],r=1;re&&(e=t[r]);return e},_=function(t,e,r){var n=e/8|0;return(t[n]|t[n+1]<<8)>>(e&7)&r},ae=function(t,e){var r=e/8|0;return(t[r]|t[r+1]<<8|t[r+2]<<16)>>(e&7)},tt=function(t){return(t+7)/8|0},rt=function(t,e,r){(e==null||e<0)&&(e=0),(r==null||r>t.length)&&(r=t.length);var n=new E(r-e);return n.set(t.subarray(e,r)),n},nt=["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"],x=function(t,e,r){var n=new Error(e||nt[t]);if(n.code=t,Error.captureStackTrace&&Error.captureStackTrace(n,x),!r)throw n;return n},ue=function(t,e,r,n){var i=t.length,s=n?n.length:0;if(!i||e.f&&!e.l)return r||new E(0);var a=!r||e.i!=2,o=e.i;r||(r=new E(i*3));var c=function(be){var De=r.length;if(be>De){var Ue=new E(Math.max(De*2,be));Ue.set(r),r=Ue}},u=e.f||0,l=e.p||0,f=e.b||0,h=e.l,v=e.d,m=e.m,g=e.n,b=i*8;do{if(!h){u=_(t,l,1);var y=_(t,l+1,3);if(l+=3,y)if(y==1)h=Qe,v=et,m=9,g=5;else if(y==2){var M=_(t,l,31)+257,$=_(t,l+10,15)+4,Y=M+_(t,l+5,31)+1;l+=14;for(var D=new E(Y),ee=new E(19),C=0;C<$;++C)ee[Ge[C]]=_(t,l+C*3,7);l+=$*3;for(var ge=se(ee),je=(1<>4;if(d<16)D[C++]=d;else{var z=0,X=0;for(d==16?(X=3+_(t,l,3),l+=2,z=D[C-1]):d==17?(X=3+_(t,l,7),l+=3):d==18&&(X=11+_(t,l,127),l+=7);X--;)D[C++]=z}}var ye=D.subarray(0,M),T=D.subarray(M);m=se(ye),g=se(T),h=F(ye,m,1),v=F(T,g,1)}else x(1);else{var d=tt(l)+4,O=t[d-4]|t[d-3]<<8,A=d+O;if(A>i){o&&x(0);break}a&&c(f+O),r.set(t.subarray(d,A),f),e.b=f+=O,e.p=l=A*8,e.f=u;continue}if(l>b){o&&x(0);break}}a&&c(f+131072);for(var We=(1<>4;if(l+=z&15,l>b){o&&x(0);break}if(z||x(2),B<256)r[f++]=B;else if(B==256){te=l,h=null;break}else{var me=B-254;if(B>264){var C=B-257,j=Ce[C];me=_(t,l,(1<>4;re||x(3),l+=re&15;var T=Xe[ne];if(ne>3){var j=ke[ne];T+=ae(t,l)&(1<b){o&&x(0);break}a&&c(f+131072);var ie=f+me;if(f>3&1)+(e>>4&1);n>0;n-=!t[r++]);return r+(e&2)},at=function(t){var e=t.length;return(t[e-4]|t[e-3]<<8|t[e-2]<<16|t[e-1]<<24)>>>0},ot=function(t,e){return((t[0]&15)!=8||t[0]>>4>7||(t[0]<<8|t[1])%31)&&x(6,"invalid zlib data"),(t[1]>>5&1)==+!e&&x(6,"invalid zlib data: "+(t[1]&32?"need":"unexpected")+" dictionary"),(t[1]>>3&4)+2};function ct(t,e){return ue(t,{i:2},e&&e.out,e&&e.dictionary)}function lt(t,e){var r=st(t);return r+8>t.length&&x(6,"invalid gzip data"),ue(t.subarray(r,-8),{i:2},e&&e.out||new E(at(t)),e&&e.dictionary)}function ut(t,e){return ue(t.subarray(ot(t,e&&e.dictionary),-4),{i:2},e&&e.out,e&&e.dictionary)}function ce(t,e){return t[0]==31&&t[1]==139&&t[2]==8?lt(t,e):(t[0]&15)!=8||t[0]>>4>7||(t[0]<<8|t[1])%31?ct(t,e):ut(t,e)}var ft=typeof TextDecoder<"u"&&new TextDecoder,ht=0;try{ft.decode(it,{stream:!0}),ht=1}catch{}var Pe=(t,e)=>t*I(2,e),N=(t,e)=>Math.floor(t/I(2,e)),Q=(t,e)=>Pe(t.getUint16(e+1,!0),8)+t.getUint8(e),Ae=(t,e)=>Pe(t.getUint32(e+2,!0),16)+t.getUint16(e,!0),dt=(t,e,r,n,i)=>{if(t!==n.getUint8(i))return t-n.getUint8(i);let s=Q(n,i+1);if(e!==s)return e-s;let a=Q(n,i+4);return r!==a?r-a:0},pt=(t,e,r,n)=>{let i=$e(t,e|128,r,n);return i?{z:e,x:r,y:n,offset:i[0],length:i[1],isDir:!0}:null},Le=(t,e,r,n)=>{let i=$e(t,e,r,n);return i?{z:e,x:r,y:n,offset:i[0],length:i[1],isDir:!1}:null},$e=(t,e,r,n)=>{let i=0,s=t.byteLength/17-1;for(;i<=s;){let a=s+i>>1,o=dt(e,r,n,t,a*17);if(o>0)i=a+1;else if(o<0)s=a-1;else return[Ae(t,a*17+7),t.getUint32(a*17+13,!0)]}return null},gt=(t,e)=>t.isDir&&!e.isDir?1:!t.isDir&&e.isDir?-1:t.z!==e.z?t.z-e.z:t.x!==e.x?t.x-e.x:t.y-e.y,ze=(t,e)=>{let r=t.getUint8(e*17);return{z:r&127,x:Q(t,e*17+1),y:Q(t,e*17+4),offset:Ae(t,e*17+7),length:t.getUint32(e*17+13,!0),isDir:r>>7===1}},xe=t=>{let e=[],r=new DataView(t);for(let n=0;n{t.sort(gt);let e=new ArrayBuffer(17*t.length),r=new Uint8Array(e);for(let n=0;n>8&255,r[n*17+3]=i.x>>16&255,r[n*17+4]=i.y&255,r[n*17+5]=i.y>>8&255,r[n*17+6]=i.y>>16&255,r[n*17+7]=i.offset&255,r[n*17+8]=N(i.offset,8)&255,r[n*17+9]=N(i.offset,16)&255,r[n*17+10]=N(i.offset,24)&255,r[n*17+11]=N(i.offset,32)&255,r[n*17+12]=N(i.offset,48)&255,r[n*17+13]=i.length&255,r[n*17+14]=i.length>>8&255,r[n*17+15]=i.length>>16&255,r[n*17+16]=i.length>>24&255}return e},yt=(t,e)=>{if(t.byteLength<17)return null;let r=t.byteLength/17,n=ze(t,r-1);if(n.isDir){let i=n.z,s=e.z-i,a=Math.trunc(e.x/(1<(e,r)=>{if(r instanceof AbortController)return t(e,r);let n=new AbortController;return t(e,n).then(i=>r(void 0,i.data,i.cacheControl||"",i.expires||""),i=>r(i)).catch(i=>r(i)),{cancel:()=>n.abort()}},Re=class{constructor(){this.tilev4=(t,e)=>w(this,null,function*(){if(t.type==="json"){let f=t.url.substr(10),h=this.tiles.get(f);h||(h=new Ee(f),this.tiles.set(f,h));let v=yield h.getHeader();return{data:{tiles:[`${t.url}/{z}/{x}/{y}`],minzoom:v.minZoom,maxzoom:v.maxZoom,bounds:[v.minLon,v.minLat,v.maxLon,v.maxLat]}}}let r=new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/),n=t.url.match(r);if(!n)throw new Error("Invalid PMTiles protocol URL");let i=n[1],s=this.tiles.get(i);s||(s=new Ee(i),this.tiles.set(i,s));let a=n[2],o=n[3],c=n[4],u=yield s.getHeader(),l=yield s?.getZxy(+a,+o,+c,e.signal);return l?{data:new Uint8Array(l.data),cacheControl:l.cacheControl,expires:l.expires}:u.tileType===1?{data:new Uint8Array}:{data:null}}),this.tile=bt(this.tilev4),this.tiles=new Map}add(t){this.tiles.set(t.source.getKey(),t)}get(t){return this.tiles.get(t)}};function R(t,e){return(e>>>0)*4294967296+(t>>>0)}function Dt(t,e){let r=e.buf,n=r[e.pos++],i=(n&112)>>4;if(n<128||(n=r[e.pos++],i|=(n&127)<<3,n<128)||(n=r[e.pos++],i|=(n&127)<<10,n<128)||(n=r[e.pos++],i|=(n&127)<<17,n<128)||(n=r[e.pos++],i|=(n&127)<<24,n<128)||(n=r[e.pos++],i|=(n&1)<<31,n<128))return R(t,i);throw new Error("Expected varint not more than 10 bytes")}function W(t){let e=t.buf,r=e[t.pos++],n=r&127;return r<128||(r=e[t.pos++],n|=(r&127)<<7,r<128)||(r=e[t.pos++],n|=(r&127)<<14,r<128)||(r=e[t.pos++],n|=(r&127)<<21,r<128)?n:(r=e[t.pos],n|=(r&15)<<28,Dt(n,t))}function Ut(t,e,r,n){if(n===0){r===1&&(e[0]=t-1-e[0],e[1]=t-1-e[1]);let i=e[0];e[0]=e[1],e[1]=i}}var Lt=[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 xt(t,e,r){if(t>26)throw Error("Tile zoom level exceeds max safe number limit (26)");if(e>I(2,t)-1||r>I(2,t)-1)throw Error("tile x/y outside zoom level bounds");let n=Lt[t],i=I(2,t),s=0,a=0,o=0,c=[e,r],u=i/2;for(;u>0;)s=(c[0]&u)>0?1:0,a=(c[1]&u)>0?1:0,o+=u*u*(3*s^a),Ut(u,c,s,a),u=u/2;return n+o}function He(t,e){return w(this,null,function*(){if(e===1||e===0)return t;if(e===2){if(typeof globalThis.DecompressionStream>"u")return ce(new Uint8Array(t));let r=new Response(t).body;if(!r)throw Error("Failed to read response stream");let n=r.pipeThrough(new globalThis.DecompressionStream("gzip"));return new Response(n).arrayBuffer()}throw Error("Compression method not supported")})}var Et=127;function Ct(t,e){let r=0,n=t.length-1;for(;r<=n;){let i=n+r>>1,s=e-t[i].tileId;if(s>0)r=i+1;else if(s<0)n=i-1;else return t[i]}return n>=0&&(t[n].runLength===0||e-t[n].tileId=300)throw Error(`Bad response code: ${c.status}`);let l=c.headers.get("Content-Length");if(c.status===200&&(!l||+l>e))throw i&&i.abort(),Error("Server returned no content-length header or content-length exceeding request. Check that your storage backend supports HTTP Byte Serving.");return{data:yield c.arrayBuffer(),etag:u||void 0,cacheControl:c.headers.get("Cache-Control")||void 0,expires:c.headers.get("Expires")||void 0}})}};function S(t,e){let r=t.getUint32(e+4,!0),n=t.getUint32(e+0,!0);return r*I(2,32)+n}function Mt(t,e){let r=new DataView(t),n=r.getUint8(7);if(n>3)throw Error(`Archive is spec version ${n} but this library supports up to spec version 3`);return{specVersion:n,rootDirectoryOffset:S(r,8),rootDirectoryLength:S(r,16),jsonMetadataOffset:S(r,24),jsonMetadataLength:S(r,32),leafDirectoryOffset:S(r,40),leafDirectoryLength:S(r,48),tileDataOffset:S(r,56),tileDataLength:S(r,64),numAddressedTiles:S(r,72),numTileEntries:S(r,80),numTileContents:S(r,88),clustered:r.getUint8(96)===1,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:e}}function Ie(t){let e={buf:new Uint8Array(t),pos:0},r=W(e),n=[],i=0;for(let s=0;s0?n[s].offset=n[s-1].offset+n[s-1].length:n[s].offset=a-1}return n}function Tt(t){let e=new DataView(t);return e.getUint16(2,!0)===2?(console.warn("PMTiles spec version 2 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade"),2):e.getUint16(2,!0)===1?(console.warn("PMTiles spec version 1 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade"),1):3}var le=class extends Error{};function _t(t,e){return w(this,null,function*(){let r=yield t.getBytes(0,16384);if(new DataView(r.data).getUint16(0,!0)!==19792)throw new Error("Wrong magic number for PMTiles archive");if(Tt(r.data)<3)return[yield Be.getHeader(t)];let i=r.data.slice(0,Et),s=Mt(i,r.etag),a=r.data.slice(s.rootDirectoryOffset,s.rootDirectoryOffset+s.rootDirectoryLength),o=`${t.getKey()}|${s.etag||""}|${s.rootDirectoryOffset}|${s.rootDirectoryLength}`,c=Ie(yield e(a,s.internalCompression));return[s,[o,c.length,c]]})}function St(t,e,r,n,i){return w(this,null,function*(){let s=yield t.getBytes(r,n,void 0,i.etag),a=yield e(s.data,i.internalCompression),o=Ie(a);if(o.length===0)throw new Error("Empty directory is invalid");return o})}var Ot=class{constructor(t=100,e=!0,r=He){this.cache=new Map,this.invalidations=new Map,this.maxCacheEntries=t,this.counter=1,this.decompress=r}getHeader(t){return w(this,null,function*(){let e=t.getKey(),r=this.cache.get(e);if(r)return r.lastUsed=this.counter++,yield r.data;let n=new Promise((i,s)=>{_t(t,this.decompress).then(a=>{a[1]&&this.cache.set(a[1][0],{lastUsed:this.counter++,data:Promise.resolve(a[1][2])}),i(a[0]),this.prune()}).catch(a=>{s(a)})});return this.cache.set(e,{lastUsed:this.counter++,data:n}),n})}getDirectory(t,e,r,n){return w(this,null,function*(){let i=`${t.getKey()}|${n.etag||""}|${e}|${r}`,s=this.cache.get(i);if(s)return s.lastUsed=this.counter++,yield s.data;let a=new Promise((o,c)=>{St(t,this.decompress,e,r,n).then(u=>{o(u),this.prune()}).catch(u=>{c(u)})});return this.cache.set(i,{lastUsed:this.counter++,data:a}),a})}getArrayBuffer(t,e,r,n){return w(this,null,function*(){let i=`${t.getKey()}|${n.etag||""}|${e}|${r}`,s=this.cache.get(i);if(s)return s.lastUsed=this.counter++,yield s.data;let a=new Promise((o,c)=>{t.getBytes(e,r,void 0,n.etag).then(u=>{o(u.data),this.cache.has(i),this.prune()}).catch(u=>{c(u)})});return this.cache.set(i,{lastUsed:this.counter++,data:a}),a})}prune(){if(this.cache.size>=this.maxCacheEntries){let t=1/0,e;this.cache.forEach((r,n)=>{r.lastUsed{this.getHeader(t).then(s=>{n(),this.invalidations.delete(e)}).catch(s=>{i(s)})});this.invalidations.set(e,r)})}},Ee=class{constructor(t,e,r){typeof t=="string"?this.source=new kt(t):this.source=t,r?this.decompress=r:this.decompress=He,e?this.cache=e:this.cache=new Ot}getHeader(){return w(this,null,function*(){return yield this.cache.getHeader(this.source)})}getZxyAttempt(t,e,r,n){return w(this,null,function*(){let i=xt(t,e,r),s=yield this.cache.getHeader(this.source);if(s.specVersion<3)return Be.getZxy(s,this.source,this.cache,t,e,r,n);if(ts.maxZoom)return;let a=s.rootDirectoryOffset,o=s.rootDirectoryLength;for(let c=0;c<=3;c++){let u=yield this.cache.getDirectory(this.source,a,o,s),l=Ct(u,i);if(l){if(l.runLength>0){let f=yield this.source.getBytes(s.tileDataOffset+l.offset,l.length,n,s.etag);return{data:yield this.decompress(f.data,s.tileCompression),cacheControl:f.cacheControl,expires:f.expires}}a=s.leafDirectoryOffset+l.offset,o=l.length}else return}throw Error("Maximum directory depth exceeded")})}getZxy(t,e,r,n){return w(this,null,function*(){try{return yield this.getZxyAttempt(t,e,r,n)}catch(i){if(i instanceof le)return this.cache.invalidate(this.source),yield this.getZxyAttempt(t,e,r,n);throw i}})}getMetadataAttempt(){return w(this,null,function*(){let t=yield this.cache.getHeader(this.source),e=yield this.source.getBytes(t.jsonMetadataOffset,t.jsonMetadataLength,void 0,t.etag),r=yield this.decompress(e.data,t.internalCompression),n=new TextDecoder("utf-8");return JSON.parse(n.decode(r))})}getMetadata(){return w(this,null,function*(){try{return yield this.getMetadataAttempt()}catch(t){if(t instanceof le)return this.cache.invalidate(this.source),yield this.getMetadataAttempt();throw t}})}};var Pt=Object.prototype.toString,K=Array.isArray||function(e){return Pt.call(e)==="[object Array]"};function he(t){return typeof t=="function"}function At(t){return K(t)?"array":typeof t}function fe(t){return t.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function Ve(t,e){return t!=null&&typeof t=="object"&&e in t}function $t(t,e){return t!=null&&typeof t!="object"&&t.hasOwnProperty&&t.hasOwnProperty(e)}var zt=RegExp.prototype.test;function Bt(t,e){return zt.call(t,e)}var Rt=/\S/;function Ht(t){return!Bt(Rt,t)}var It={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function Vt(t){return String(t).replace(/[&<>"'`=\/]/g,function(r){return It[r]})}var Kt=/\s*/,Zt=/\s+/,Ke=/\s*=/,jt=/\s*\}/,Nt=/#|\^|\/|>|\{|&|=|!/;function Wt(t,e){if(!t)return[];var r=!1,n=[],i=[],s=[],a=!1,o=!1,c="",u=0;function l(){if(a&&!o)for(;s.length;)delete i[s.pop()];else s=[];a=!1,o=!1}var f,h,v;function m(D){if(typeof D=="string"&&(D=D.split(Zt,2)),!K(D)||D.length!==2)throw new Error("Invalid tags: "+D);f=new RegExp(fe(D[0])+"\\s*"),h=new RegExp("\\s*"+fe(D[1])),v=new RegExp("\\s*"+fe("}"+D[1]))}m(e||k.tags);for(var g=new G(t),b,y,d,O,A,M;!g.eos();){if(b=g.pos,d=g.scanUntil(f),d)for(var $=0,Y=d.length;$"?A=[y,d,b,g.pos,c,u,r]:A=[y,d,b,g.pos],u++,i.push(A),y==="#"||y==="^")n.push(A);else if(y==="/"){if(M=n.pop(),!M)throw new Error('Unopened section "'+d+'" at '+b);if(M[1]!==d)throw new Error('Unclosed section "'+M[1]+'" at '+b)}else y==="name"||y==="{"||y==="&"?o=!0:y==="="&&m(d)}if(l(),M=n.pop(),M)throw new Error('Unclosed section "'+M[1]+'" at '+g.pos);return Jt(Ft(i))}function Ft(t){for(var e=[],r,n,i=0,s=t.length;i0?n[n.length-1][4]:e;break;default:r.push(i)}return e}function G(t){this.string=t,this.tail=t,this.pos=0}G.prototype.eos=function(){return this.tail===""};G.prototype.scan=function(e){var r=this.tail.match(e);if(!r||r.index!==0)return"";var n=r[0];return this.tail=this.tail.substring(n.length),this.pos+=n.length,n};G.prototype.scanUntil=function(e){var r=this.tail.search(e),n;switch(r){case-1:n=this.tail,this.tail="";break;case 0:n="";break;default:n=this.tail.substring(0,r),this.tail=this.tail.substring(r)}return this.pos+=n.length,n};function V(t,e){this.view=t,this.cache={".":this.view},this.parent=e}V.prototype.push=function(e){return new V(e,this)};V.prototype.lookup=function(e){var r=this.cache,n;if(r.hasOwnProperty(e))n=r[e];else{for(var i=this,s,a,o,c=!1;i;){if(e.indexOf(".")>0)for(s=i.view,a=e.split("."),o=0;s!=null&&o"?u=this.renderPartial(o,r,n,s):c==="&"?u=this.unescapedValue(o,r):c==="name"?u=this.escapedValue(o,r,s):c==="text"&&(u=this.rawValue(o)),u!==void 0&&(a+=u);return a};U.prototype.renderSection=function(e,r,n,i,s){var a=this,o="",c=r.lookup(e[1]);function u(h){return a.render(h,r,n,s)}if(c){if(K(c))for(var l=0,f=c.length;l0||!n)&&(s[a]=i+s[a]);return s.join(` -`)};U.prototype.renderPartial=function(e,r,n,i){if(n){var s=this.getConfigTags(i),a=he(n)?n(e[1]):n[e[1]];if(a!=null){var o=e[6],c=e[5],u=e[4],l=a;c==0&&u&&(l=this.indentPartial(a,u,o));var f=this.parse(l,s);return this.renderTokens(f,r,n,l,i)}}};U.prototype.unescapedValue=function(e,r){var n=r.lookup(e[1]);if(n!=null)return n};U.prototype.escapedValue=function(e,r,n){var i=this.getConfigEscape(n)||k.escape,s=r.lookup(e[1]);if(s!=null)return typeof s=="number"&&i===k.escape?String(s):i(s)};U.prototype.rawValue=function(e){return e[1]};U.prototype.getConfigTags=function(e){return K(e)?e:e&&typeof e=="object"?e.tags:void 0};U.prototype.getConfigEscape=function(e){if(e&&typeof e=="object"&&!K(e))return e.escape};var k={name:"mustache.js",version:"4.2.0",tags:["{{","}}"],clearCache:void 0,escape:void 0,parse:void 0,render:void 0,Scanner:void 0,Context:void 0,Writer:void 0,set templateCache(t){q.templateCache=t},get templateCache(){return q.templateCache}},q=new U;k.clearCache=function(){return q.clearCache()};k.parse=function(e,r){return q.parse(e,r)};k.render=function(e,r,n,i){if(typeof e!="string")throw new TypeError('Invalid template! Template should be a "string" but "'+At(e)+'" was given as the first argument for mustache#render(template, view, partials)');return q.render(e,r,n,i)};k.escape=Vt;k.Scanner=G;k.Context=V;k.Writer=U;var de=k;function pe(t,e,r){return r!==null?de.render(r,t.properties):e===null?Object.keys(t.properties).map(i=>`${i}: ${t.properties[i]}`).join("
"):t.properties[e]}function Ze(t,e){let r=new maplibregl.Popup({closeOnClick:!1,closeButton:!1});return t.on("mouseout",n=>r.remove()),({coordinate:n,object:i})=>{i?(r.setHTML(de.render(e,i)).setLngLat(n),r.addTo(t)):r.remove()}}var qt=new Re;maplibregl.addProtocol("pmtiles",qt.tile);function Gt(){if(typeof deck>"u")return;let t=new deck.JSONConfiguration({classes:deck});return new deck.JSONConverter({configuration:t})}var Z=class{constructor(e){this._id=e.container,this._map=new maplibregl.Map(e),this._map.on("mouseover",()=>{this._map.getCanvas().style.cursor="pointer"}),this._map.on("mouseout",()=>{this._map.getCanvas().style.cursor=""}),this._JSONConverter=Gt()}getMap(){return this._map}applyMapMethod(e,r){this._map[e](...r)}addControl(e,r,n){this._map.addControl(new maplibregl[e](r),n)}addMarker({lngLat:e,popup:r,options:n}){let i=new maplibregl.Marker(n).setLngLat(e);if(r){let s=new maplibregl.Popup(r.options).setHTML(r.text);i.setPopup(s)}i.addTo(this._map)}addLayer(e,r){this._map.addLayer(e,r),typeof Shiny<"u"&&this._map.on("click",e.id,n=>{console.log(n,n.features[0]);let i=e.id.replaceAll("-","_"),s=`${this._id}_layer_${i}`,a={props:n.features[0].properties,layer_id:e.id};console.log(s,a),Shiny.onInputChange(s,a)})}addPopup(e,r=null,n=null){let i={closeButton:!1},s=new maplibregl.Popup(i);this._map.on("click",e,a=>{let o=a.features[0],c=pe(o,r,n);s.setLngLat(a.lngLat).setHTML(c).addTo(this._map)})}addTooltip(e,r=null,n=null){let i={closeButton:!1,closeOnClick:!1},s=new maplibregl.Popup(i);this._map.on("mousemove",e,a=>{let o=a.features[0],c=pe(o,r,n);s.setLngLat(a.lngLat).setHTML(c).addTo(this._map)}),this._map.on("mouseleave",e,()=>{s.remove()})}setSourceData(e,r){this._map.getSource(e).setData(r)}addDeckOverlay(e,r=null){if(typeof this._JSONConverter>"u"){console.log("deck or JSONConverter is undefined");return}let n=this._convertDeckLayers(e,r);this._deckOverlay=new deck.MapboxOverlay({interleaved:!0,layers:n}),this._map.addControl(this._deckOverlay)}_convertDeckLayers(e,r=null){return e.map(n=>{let i=r&&typeof r=="object"?r[n.id]:r,s=Ze(this._map,i);return n.onHover=({layer:a,coordinate:o,object:c})=>{if(i&&s({coordinate:o,object:c}),typeof Shiny<"u"){let u=`${this._id}_layer_${n.id}`;Shiny.onInputChange(u,c)}},this._JSONConverter.convert(n)})}setDeckLayers(e,r=null){console.log("Updating Deck.GL layers");let n=this._convertDeckLayers(e,r);this._deckOverlay.setProps({layers:n})}render(e){e.forEach(([r,n])=>{if(["addLayer","addPopup","addTooltip","addMarker","addPopup","addControl","setSourceData","addDeckOverlay","setDeckLayers"].includes(r)){console.log("Custom method",r,n),this[r](...n);return}console.log("Map method",r),this.applyMapMethod(r,n)})}};var Yt="0.1.0";console.log("pymaplibregl",Yt);typeof Shiny>"u"&&(window.pymaplibregl=function({mapOptions:t,calls:e}){let r="pymaplibregl",n=document.getElementById(r),i=new Z(Object.assign({container:n.id},t));i.getMap().on("load",()=>{i.render(e)})});if(typeof Shiny<"u"){class t extends Shiny.OutputBinding{find(r){return r.find(".shiny-maplibregl-output")}renderValue(r,n){console.log("id:",r.id,"payload:",n);let i=window._maplibreWidget=new Z(Object.assign({container:r.id},n.mapData.mapOptions)),s=i.getMap();s.on("load",()=>{i.render(n.mapData.calls)}),s.on("click",o=>{console.log(o);let c=`${r.id}`,u={coords:o.lngLat,point:o.point};console.log(c,u),Shiny.onInputChange(c,u)});let a=`pymaplibregl-${r.id}`;console.log(a),Shiny.addCustomMessageHandler(a,({id:o,calls:c})=>{console.log(o,c),i.render(c)})}}Shiny.outputBindings.register(new t,"shiny-maplibregl-output")}})(); +(()=>{var I=Math.pow,w=(t,e,r)=>new Promise((n,i)=>{var a=c=>{try{o(r.next(c))}catch(u){i(u)}},s=c=>{try{o(r.throw(c))}catch(u){i(u)}},o=c=>c.done?n(c.value):Promise.resolve(c.value).then(a,s);o((r=r.apply(t,e)).next())}),E=Uint8Array,H=Uint16Array,qe=Int32Array,Ce=new E([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]),Me=new E([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]),Ge=new E([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),ke=function(t,e){for(var r=new H(31),n=0;n<31;++n)r[n]=e+=1<>1|(p&21845)<<1,P=(P&52428)>>2|(P&13107)<<2,P=(P&61680)>>4|(P&3855)<<4,oe[p]=((P&65280)>>8|(P&255)<<8)>>1;var P,p,W=function(t,e,r){for(var n=t.length,i=0,a=new H(e);i>c]=u}else for(o=new H(n),i=0;i>15-t[i]);return o},J=new E(288);for(p=0;p<144;++p)J[p]=8;var p;for(p=144;p<256;++p)J[p]=9;var p;for(p=256;p<280;++p)J[p]=7;var p;for(p=280;p<288;++p)J[p]=8;var p,Oe=new E(32);for(p=0;p<32;++p)Oe[p]=5;var p,Qe=W(J,9,1),et=W(Oe,5,1),ae=function(t){for(var e=t[0],r=1;re&&(e=t[r]);return e},T=function(t,e,r){var n=e/8|0;return(t[n]|t[n+1]<<8)>>(e&7)&r},se=function(t,e){var r=e/8|0;return(t[r]|t[r+1]<<8|t[r+2]<<16)>>(e&7)},tt=function(t){return(t+7)/8|0},rt=function(t,e,r){(e==null||e<0)&&(e=0),(r==null||r>t.length)&&(r=t.length);var n=new E(r-e);return n.set(t.subarray(e,r)),n},nt=["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"],x=function(t,e,r){var n=new Error(e||nt[t]);if(n.code=t,Error.captureStackTrace&&Error.captureStackTrace(n,x),!r)throw n;return n},ue=function(t,e,r,n){var i=t.length,a=n?n.length:0;if(!i||e.f&&!e.l)return r||new E(0);var s=!r||e.i!=2,o=e.i;r||(r=new E(i*3));var c=function(be){var De=r.length;if(be>De){var Ue=new E(Math.max(De*2,be));Ue.set(r),r=Ue}},u=e.f||0,l=e.p||0,f=e.b||0,h=e.l,v=e.d,m=e.m,g=e.n,b=i*8;do{if(!h){u=T(t,l,1);var y=T(t,l+1,3);if(l+=3,y)if(y==1)h=Qe,v=et,m=9,g=5;else if(y==2){var k=T(t,l,31)+257,R=T(t,l+10,15)+4,X=k+T(t,l+5,31)+1;l+=14;for(var D=new E(X),ee=new E(19),C=0;C>4;if(d<16)D[C++]=d;else{var $=0,Y=0;for(d==16?(Y=3+T(t,l,3),l+=2,$=D[C-1]):d==17?(Y=3+T(t,l,7),l+=3):d==18&&(Y=11+T(t,l,127),l+=7);Y--;)D[C++]=$}}var ye=D.subarray(0,k),_=D.subarray(k);m=ae(ye),g=ae(_),h=W(ye,m,1),v=W(_,g,1)}else x(1);else{var d=tt(l)+4,O=t[d-4]|t[d-3]<<8,A=d+O;if(A>i){o&&x(0);break}s&&c(f+O),r.set(t.subarray(d,A),f),e.b=f+=O,e.p=l=A*8,e.f=u;continue}if(l>b){o&&x(0);break}}s&&c(f+131072);for(var Fe=(1<>4;if(l+=$&15,l>b){o&&x(0);break}if($||x(2),B<256)r[f++]=B;else if(B==256){te=l,h=null;break}else{var me=B-254;if(B>264){var C=B-257,N=Ce[C];me=T(t,l,(1<>4;re||x(3),l+=re&15;var _=Ye[ne];if(ne>3){var N=Me[ne];_+=se(t,l)&(1<b){o&&x(0);break}s&&c(f+131072);var ie=f+me;if(f<_){var we=a-_,Je=Math.min(_,ie);for(we+f<0&&x(3);f>3&1)+(e>>4&1);n>0;n-=!t[r++]);return r+(e&2)},st=function(t){var e=t.length;return(t[e-4]|t[e-3]<<8|t[e-2]<<16|t[e-1]<<24)>>>0},ot=function(t,e){return((t[0]&15)!=8||t[0]>>4>7||(t[0]<<8|t[1])%31)&&x(6,"invalid zlib data"),(t[1]>>5&1)==+!e&&x(6,"invalid zlib data: "+(t[1]&32?"need":"unexpected")+" dictionary"),(t[1]>>3&4)+2};function ct(t,e){return ue(t,{i:2},e&&e.out,e&&e.dictionary)}function lt(t,e){var r=at(t);return r+8>t.length&&x(6,"invalid gzip data"),ue(t.subarray(r,-8),{i:2},e&&e.out||new E(st(t)),e&&e.dictionary)}function ut(t,e){return ue(t.subarray(ot(t,e&&e.dictionary),-4),{i:2},e&&e.out,e&&e.dictionary)}function ce(t,e){return t[0]==31&&t[1]==139&&t[2]==8?lt(t,e):(t[0]&15)!=8||t[0]>>4>7||(t[0]<<8|t[1])%31?ct(t,e):ut(t,e)}var ft=typeof TextDecoder<"u"&&new TextDecoder,ht=0;try{ft.decode(it,{stream:!0}),ht=1}catch{}var Pe=(t,e)=>t*I(2,e),j=(t,e)=>Math.floor(t/I(2,e)),Q=(t,e)=>Pe(t.getUint16(e+1,!0),8)+t.getUint8(e),Ae=(t,e)=>Pe(t.getUint32(e+2,!0),16)+t.getUint16(e,!0),dt=(t,e,r,n,i)=>{if(t!==n.getUint8(i))return t-n.getUint8(i);let a=Q(n,i+1);if(e!==a)return e-a;let s=Q(n,i+4);return r!==s?r-s:0},pt=(t,e,r,n)=>{let i=Re(t,e|128,r,n);return i?{z:e,x:r,y:n,offset:i[0],length:i[1],isDir:!0}:null},xe=(t,e,r,n)=>{let i=Re(t,e,r,n);return i?{z:e,x:r,y:n,offset:i[0],length:i[1],isDir:!1}:null},Re=(t,e,r,n)=>{let i=0,a=t.byteLength/17-1;for(;i<=a;){let s=a+i>>1,o=dt(e,r,n,t,s*17);if(o>0)i=s+1;else if(o<0)a=s-1;else return[Ae(t,s*17+7),t.getUint32(s*17+13,!0)]}return null},gt=(t,e)=>t.isDir&&!e.isDir?1:!t.isDir&&e.isDir?-1:t.z!==e.z?t.z-e.z:t.x!==e.x?t.x-e.x:t.y-e.y,$e=(t,e)=>{let r=t.getUint8(e*17);return{z:r&127,x:Q(t,e*17+1),y:Q(t,e*17+4),offset:Ae(t,e*17+7),length:t.getUint32(e*17+13,!0),isDir:r>>7===1}},Le=t=>{let e=[],r=new DataView(t);for(let n=0;n{t.sort(gt);let e=new ArrayBuffer(17*t.length),r=new Uint8Array(e);for(let n=0;n>8&255,r[n*17+3]=i.x>>16&255,r[n*17+4]=i.y&255,r[n*17+5]=i.y>>8&255,r[n*17+6]=i.y>>16&255,r[n*17+7]=i.offset&255,r[n*17+8]=j(i.offset,8)&255,r[n*17+9]=j(i.offset,16)&255,r[n*17+10]=j(i.offset,24)&255,r[n*17+11]=j(i.offset,32)&255,r[n*17+12]=j(i.offset,48)&255,r[n*17+13]=i.length&255,r[n*17+14]=i.length>>8&255,r[n*17+15]=i.length>>16&255,r[n*17+16]=i.length>>24&255}return e},yt=(t,e)=>{if(t.byteLength<17)return null;let r=t.byteLength/17,n=$e(t,r-1);if(n.isDir){let i=n.z,a=e.z-i,s=Math.trunc(e.x/(1<(e,r)=>{if(r instanceof AbortController)return t(e,r);let n=new AbortController;return t(e,n).then(i=>r(void 0,i.data,i.cacheControl||"",i.expires||""),i=>r(i)).catch(i=>r(i)),{cancel:()=>n.abort()}},ze=class{constructor(){this.tilev4=(t,e)=>w(this,null,function*(){if(t.type==="json"){let f=t.url.substr(10),h=this.tiles.get(f);h||(h=new Ee(f),this.tiles.set(f,h));let v=yield h.getHeader();return{data:{tiles:[`${t.url}/{z}/{x}/{y}`],minzoom:v.minZoom,maxzoom:v.maxZoom,bounds:[v.minLon,v.minLat,v.maxLon,v.maxLat]}}}let r=new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/),n=t.url.match(r);if(!n)throw new Error("Invalid PMTiles protocol URL");let i=n[1],a=this.tiles.get(i);a||(a=new Ee(i),this.tiles.set(i,a));let s=n[2],o=n[3],c=n[4],u=yield a.getHeader(),l=yield a?.getZxy(+s,+o,+c,e.signal);return l?{data:new Uint8Array(l.data),cacheControl:l.cacheControl,expires:l.expires}:u.tileType===1?{data:new Uint8Array}:{data:null}}),this.tile=bt(this.tilev4),this.tiles=new Map}add(t){this.tiles.set(t.source.getKey(),t)}get(t){return this.tiles.get(t)}};function z(t,e){return(e>>>0)*4294967296+(t>>>0)}function Dt(t,e){let r=e.buf,n=r[e.pos++],i=(n&112)>>4;if(n<128||(n=r[e.pos++],i|=(n&127)<<3,n<128)||(n=r[e.pos++],i|=(n&127)<<10,n<128)||(n=r[e.pos++],i|=(n&127)<<17,n<128)||(n=r[e.pos++],i|=(n&127)<<24,n<128)||(n=r[e.pos++],i|=(n&1)<<31,n<128))return z(t,i);throw new Error("Expected varint not more than 10 bytes")}function F(t){let e=t.buf,r=e[t.pos++],n=r&127;return r<128||(r=e[t.pos++],n|=(r&127)<<7,r<128)||(r=e[t.pos++],n|=(r&127)<<14,r<128)||(r=e[t.pos++],n|=(r&127)<<21,r<128)?n:(r=e[t.pos],n|=(r&15)<<28,Dt(n,t))}function Ut(t,e,r,n){if(n===0){r===1&&(e[0]=t-1-e[0],e[1]=t-1-e[1]);let i=e[0];e[0]=e[1],e[1]=i}}var xt=[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 Lt(t,e,r){if(t>26)throw Error("Tile zoom level exceeds max safe number limit (26)");if(e>I(2,t)-1||r>I(2,t)-1)throw Error("tile x/y outside zoom level bounds");let n=xt[t],i=I(2,t),a=0,s=0,o=0,c=[e,r],u=i/2;for(;u>0;)a=(c[0]&u)>0?1:0,s=(c[1]&u)>0?1:0,o+=u*u*(3*a^s),Ut(u,c,a,s),u=u/2;return n+o}function He(t,e){return w(this,null,function*(){if(e===1||e===0)return t;if(e===2){if(typeof globalThis.DecompressionStream>"u")return ce(new Uint8Array(t));let r=new Response(t).body;if(!r)throw Error("Failed to read response stream");let n=r.pipeThrough(new globalThis.DecompressionStream("gzip"));return new Response(n).arrayBuffer()}throw Error("Compression method not supported")})}var Et=127;function Ct(t,e){let r=0,n=t.length-1;for(;r<=n;){let i=n+r>>1,a=e-t[i].tileId;if(a>0)r=i+1;else if(a<0)n=i-1;else return t[i]}return n>=0&&(t[n].runLength===0||e-t[n].tileId=300)throw Error(`Bad response code: ${c.status}`);let l=c.headers.get("Content-Length");if(c.status===200&&(!l||+l>e))throw i&&i.abort(),Error("Server returned no content-length header or content-length exceeding request. Check that your storage backend supports HTTP Byte Serving.");return{data:yield c.arrayBuffer(),etag:u||void 0,cacheControl:c.headers.get("Cache-Control")||void 0,expires:c.headers.get("Expires")||void 0}})}};function S(t,e){let r=t.getUint32(e+4,!0),n=t.getUint32(e+0,!0);return r*I(2,32)+n}function kt(t,e){let r=new DataView(t),n=r.getUint8(7);if(n>3)throw Error(`Archive is spec version ${n} but this library supports up to spec version 3`);return{specVersion:n,rootDirectoryOffset:S(r,8),rootDirectoryLength:S(r,16),jsonMetadataOffset:S(r,24),jsonMetadataLength:S(r,32),leafDirectoryOffset:S(r,40),leafDirectoryLength:S(r,48),tileDataOffset:S(r,56),tileDataLength:S(r,64),numAddressedTiles:S(r,72),numTileEntries:S(r,80),numTileContents:S(r,88),clustered:r.getUint8(96)===1,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:e}}function Ie(t){let e={buf:new Uint8Array(t),pos:0},r=F(e),n=[],i=0;for(let a=0;a0?n[a].offset=n[a-1].offset+n[a-1].length:n[a].offset=s-1}return n}function _t(t){let e=new DataView(t);return e.getUint16(2,!0)===2?(console.warn("PMTiles spec version 2 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade"),2):e.getUint16(2,!0)===1?(console.warn("PMTiles spec version 1 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade"),1):3}var le=class extends Error{};function Tt(t,e){return w(this,null,function*(){let r=yield t.getBytes(0,16384);if(new DataView(r.data).getUint16(0,!0)!==19792)throw new Error("Wrong magic number for PMTiles archive");if(_t(r.data)<3)return[yield Be.getHeader(t)];let i=r.data.slice(0,Et),a=kt(i,r.etag),s=r.data.slice(a.rootDirectoryOffset,a.rootDirectoryOffset+a.rootDirectoryLength),o=`${t.getKey()}|${a.etag||""}|${a.rootDirectoryOffset}|${a.rootDirectoryLength}`,c=Ie(yield e(s,a.internalCompression));return[a,[o,c.length,c]]})}function St(t,e,r,n,i){return w(this,null,function*(){let a=yield t.getBytes(r,n,void 0,i.etag),s=yield e(a.data,i.internalCompression),o=Ie(s);if(o.length===0)throw new Error("Empty directory is invalid");return o})}var Ot=class{constructor(t=100,e=!0,r=He){this.cache=new Map,this.invalidations=new Map,this.maxCacheEntries=t,this.counter=1,this.decompress=r}getHeader(t){return w(this,null,function*(){let e=t.getKey(),r=this.cache.get(e);if(r)return r.lastUsed=this.counter++,yield r.data;let n=new Promise((i,a)=>{Tt(t,this.decompress).then(s=>{s[1]&&this.cache.set(s[1][0],{lastUsed:this.counter++,data:Promise.resolve(s[1][2])}),i(s[0]),this.prune()}).catch(s=>{a(s)})});return this.cache.set(e,{lastUsed:this.counter++,data:n}),n})}getDirectory(t,e,r,n){return w(this,null,function*(){let i=`${t.getKey()}|${n.etag||""}|${e}|${r}`,a=this.cache.get(i);if(a)return a.lastUsed=this.counter++,yield a.data;let s=new Promise((o,c)=>{St(t,this.decompress,e,r,n).then(u=>{o(u),this.prune()}).catch(u=>{c(u)})});return this.cache.set(i,{lastUsed:this.counter++,data:s}),s})}getArrayBuffer(t,e,r,n){return w(this,null,function*(){let i=`${t.getKey()}|${n.etag||""}|${e}|${r}`,a=this.cache.get(i);if(a)return a.lastUsed=this.counter++,yield a.data;let s=new Promise((o,c)=>{t.getBytes(e,r,void 0,n.etag).then(u=>{o(u.data),this.cache.has(i),this.prune()}).catch(u=>{c(u)})});return this.cache.set(i,{lastUsed:this.counter++,data:s}),s})}prune(){if(this.cache.size>=this.maxCacheEntries){let t=1/0,e;this.cache.forEach((r,n)=>{r.lastUsed{this.getHeader(t).then(a=>{n(),this.invalidations.delete(e)}).catch(a=>{i(a)})});this.invalidations.set(e,r)})}},Ee=class{constructor(t,e,r){typeof t=="string"?this.source=new Mt(t):this.source=t,r?this.decompress=r:this.decompress=He,e?this.cache=e:this.cache=new Ot}getHeader(){return w(this,null,function*(){return yield this.cache.getHeader(this.source)})}getZxyAttempt(t,e,r,n){return w(this,null,function*(){let i=Lt(t,e,r),a=yield this.cache.getHeader(this.source);if(a.specVersion<3)return Be.getZxy(a,this.source,this.cache,t,e,r,n);if(ta.maxZoom)return;let s=a.rootDirectoryOffset,o=a.rootDirectoryLength;for(let c=0;c<=3;c++){let u=yield this.cache.getDirectory(this.source,s,o,a),l=Ct(u,i);if(l){if(l.runLength>0){let f=yield this.source.getBytes(a.tileDataOffset+l.offset,l.length,n,a.etag);return{data:yield this.decompress(f.data,a.tileCompression),cacheControl:f.cacheControl,expires:f.expires}}s=a.leafDirectoryOffset+l.offset,o=l.length}else return}throw Error("Maximum directory depth exceeded")})}getZxy(t,e,r,n){return w(this,null,function*(){try{return yield this.getZxyAttempt(t,e,r,n)}catch(i){if(i instanceof le)return this.cache.invalidate(this.source),yield this.getZxyAttempt(t,e,r,n);throw i}})}getMetadataAttempt(){return w(this,null,function*(){let t=yield this.cache.getHeader(this.source),e=yield this.source.getBytes(t.jsonMetadataOffset,t.jsonMetadataLength,void 0,t.etag),r=yield this.decompress(e.data,t.internalCompression),n=new TextDecoder("utf-8");return JSON.parse(n.decode(r))})}getMetadata(){return w(this,null,function*(){try{return yield this.getMetadataAttempt()}catch(t){if(t instanceof le)return this.cache.invalidate(this.source),yield this.getMetadataAttempt();throw t}})}};var Pt=Object.prototype.toString,K=Array.isArray||function(e){return Pt.call(e)==="[object Array]"};function he(t){return typeof t=="function"}function At(t){return K(t)?"array":typeof t}function fe(t){return t.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function Ve(t,e){return t!=null&&typeof t=="object"&&e in t}function Rt(t,e){return t!=null&&typeof t!="object"&&t.hasOwnProperty&&t.hasOwnProperty(e)}var $t=RegExp.prototype.test;function Bt(t,e){return $t.call(t,e)}var zt=/\S/;function Ht(t){return!Bt(zt,t)}var It={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function Vt(t){return String(t).replace(/[&<>"'`=\/]/g,function(r){return It[r]})}var Kt=/\s*/,Zt=/\s+/,Ke=/\s*=/,Nt=/\s*\}/,jt=/#|\^|\/|>|\{|&|=|!/;function Ft(t,e){if(!t)return[];var r=!1,n=[],i=[],a=[],s=!1,o=!1,c="",u=0;function l(){if(s&&!o)for(;a.length;)delete i[a.pop()];else a=[];s=!1,o=!1}var f,h,v;function m(D){if(typeof D=="string"&&(D=D.split(Zt,2)),!K(D)||D.length!==2)throw new Error("Invalid tags: "+D);f=new RegExp(fe(D[0])+"\\s*"),h=new RegExp("\\s*"+fe(D[1])),v=new RegExp("\\s*"+fe("}"+D[1]))}m(e||M.tags);for(var g=new G(t),b,y,d,O,A,k;!g.eos();){if(b=g.pos,d=g.scanUntil(f),d)for(var R=0,X=d.length;R"?A=[y,d,b,g.pos,c,u,r]:A=[y,d,b,g.pos],u++,i.push(A),y==="#"||y==="^")n.push(A);else if(y==="/"){if(k=n.pop(),!k)throw new Error('Unopened section "'+d+'" at '+b);if(k[1]!==d)throw new Error('Unclosed section "'+k[1]+'" at '+b)}else y==="name"||y==="{"||y==="&"?o=!0:y==="="&&m(d)}if(l(),k=n.pop(),k)throw new Error('Unclosed section "'+k[1]+'" at '+g.pos);return Jt(Wt(i))}function Wt(t){for(var e=[],r,n,i=0,a=t.length;i0?n[n.length-1][4]:e;break;default:r.push(i)}return e}function G(t){this.string=t,this.tail=t,this.pos=0}G.prototype.eos=function(){return this.tail===""};G.prototype.scan=function(e){var r=this.tail.match(e);if(!r||r.index!==0)return"";var n=r[0];return this.tail=this.tail.substring(n.length),this.pos+=n.length,n};G.prototype.scanUntil=function(e){var r=this.tail.search(e),n;switch(r){case-1:n=this.tail,this.tail="";break;case 0:n="";break;default:n=this.tail.substring(0,r),this.tail=this.tail.substring(r)}return this.pos+=n.length,n};function V(t,e){this.view=t,this.cache={".":this.view},this.parent=e}V.prototype.push=function(e){return new V(e,this)};V.prototype.lookup=function(e){var r=this.cache,n;if(r.hasOwnProperty(e))n=r[e];else{for(var i=this,a,s,o,c=!1;i;){if(e.indexOf(".")>0)for(a=i.view,s=e.split("."),o=0;a!=null&&o"?u=this.renderPartial(o,r,n,a):c==="&"?u=this.unescapedValue(o,r):c==="name"?u=this.escapedValue(o,r,a):c==="text"&&(u=this.rawValue(o)),u!==void 0&&(s+=u);return s};U.prototype.renderSection=function(e,r,n,i,a){var s=this,o="",c=r.lookup(e[1]);function u(h){return s.render(h,r,n,a)}if(c){if(K(c))for(var l=0,f=c.length;l0||!n)&&(a[s]=i+a[s]);return a.join(` +`)};U.prototype.renderPartial=function(e,r,n,i){if(n){var a=this.getConfigTags(i),s=he(n)?n(e[1]):n[e[1]];if(s!=null){var o=e[6],c=e[5],u=e[4],l=s;c==0&&u&&(l=this.indentPartial(s,u,o));var f=this.parse(l,a);return this.renderTokens(f,r,n,l,i)}}};U.prototype.unescapedValue=function(e,r){var n=r.lookup(e[1]);if(n!=null)return n};U.prototype.escapedValue=function(e,r,n){var i=this.getConfigEscape(n)||M.escape,a=r.lookup(e[1]);if(a!=null)return typeof a=="number"&&i===M.escape?String(a):i(a)};U.prototype.rawValue=function(e){return e[1]};U.prototype.getConfigTags=function(e){return K(e)?e:e&&typeof e=="object"?e.tags:void 0};U.prototype.getConfigEscape=function(e){if(e&&typeof e=="object"&&!K(e))return e.escape};var M={name:"mustache.js",version:"4.2.0",tags:["{{","}}"],clearCache:void 0,escape:void 0,parse:void 0,render:void 0,Scanner:void 0,Context:void 0,Writer:void 0,set templateCache(t){q.templateCache=t},get templateCache(){return q.templateCache}},q=new U;M.clearCache=function(){return q.clearCache()};M.parse=function(e,r){return q.parse(e,r)};M.render=function(e,r,n,i){if(typeof e!="string")throw new TypeError('Invalid template! Template should be a "string" but "'+At(e)+'" was given as the first argument for mustache#render(template, view, partials)');return q.render(e,r,n,i)};M.escape=Vt;M.Scanner=G;M.Context=V;M.Writer=U;var de=M;function pe(t,e,r){return r!==null?de.render(r,t.properties):e===null?Object.keys(t.properties).map(i=>`${i}: ${t.properties[i]}`).join("
"):t.properties[e]}function Ze(t,e){let r=new maplibregl.Popup({closeOnClick:!1,closeButton:!1});return t.on("mouseout",n=>r.remove()),({coordinate:n,object:i})=>{i?(r.setHTML(de.render(e,i)).setLngLat(n),r.addTo(t)):r.remove()}}var qt=new ze;maplibregl.addProtocol("pmtiles",qt.tile);function Gt(){if(typeof deck>"u")return;let t=new deck.JSONConfiguration({classes:deck});return new deck.JSONConverter({configuration:t})}typeof MapboxDraw<"u"&&(MapboxDraw.constants.classes.CONTROL_BASE="maplibregl-ctrl",MapboxDraw.constants.classes.CONTROL_PREFIX="maplibregl-ctrl-",MapboxDraw.constants.classes.CONTROL_GROUP="maplibregl-ctrl-group");var Z=class{constructor(e){this._id=e.container,this._map=new maplibregl.Map(e),this._map.on("mouseover",()=>{this._map.getCanvas().style.cursor="pointer"}),this._map.on("mouseout",()=>{this._map.getCanvas().style.cursor=""}),this._JSONConverter=Gt(),typeof MapboxDraw<"u"&&this.addMapboxDraw()}getMap(){return this._map}applyMapMethod(e,r){this._map[e](...r)}addControl(e,r,n){this._map.addControl(new maplibregl[e](r),n)}addMarker({lngLat:e,popup:r,options:n}){let i=new maplibregl.Marker(n).setLngLat(e);if(r){let a=new maplibregl.Popup(r.options).setHTML(r.text);i.setPopup(a)}i.addTo(this._map)}addLayer(e,r){this._map.addLayer(e,r),typeof Shiny<"u"&&this._map.on("click",e.id,n=>{console.log(n,n.features[0]);let i=e.id.replaceAll("-","_"),a=`${this._id}_layer_${i}`,s={props:n.features[0].properties,layer_id:e.id};console.log(a,s),Shiny.onInputChange(a,s)})}addPopup(e,r=null,n=null){let i={closeButton:!1},a=new maplibregl.Popup(i);this._map.on("click",e,s=>{let o=s.features[0],c=pe(o,r,n);a.setLngLat(s.lngLat).setHTML(c).addTo(this._map)})}addTooltip(e,r=null,n=null){let i={closeButton:!1,closeOnClick:!1},a=new maplibregl.Popup(i);this._map.on("mousemove",e,s=>{let o=s.features[0],c=pe(o,r,n);a.setLngLat(s.lngLat).setHTML(c).addTo(this._map)}),this._map.on("mouseleave",e,()=>{a.remove()})}setSourceData(e,r){this._map.getSource(e).setData(r)}addDeckOverlay(e,r=null){if(typeof this._JSONConverter>"u"){console.log("deck or JSONConverter is undefined");return}let n=this._convertDeckLayers(e,r);this._deckOverlay=new deck.MapboxOverlay({interleaved:!0,layers:n}),this._map.addControl(this._deckOverlay)}_convertDeckLayers(e,r=null){return e.map(n=>{let i=r&&typeof r=="object"?r[n.id]:r,a=Ze(this._map,i);return n.onHover=({layer:s,coordinate:o,object:c})=>{if(i&&a({coordinate:o,object:c}),typeof Shiny<"u"){let u=`${this._id}_layer_${n.id}`;Shiny.onInputChange(u,c)}},this._JSONConverter.convert(n)})}setDeckLayers(e,r=null){console.log("Updating Deck.GL layers");let n=this._convertDeckLayers(e,r);this._deckOverlay.setProps({layers:n})}addMapboxDraw(){let e=new MapboxDraw({displayControlsDefault:!1,controls:{polygon:!0,trash:!0}});this._map.addControl(e)}render(e){e.forEach(([r,n])=>{if(["addLayer","addPopup","addTooltip","addMarker","addPopup","addControl","setSourceData","addDeckOverlay","setDeckLayers"].includes(r)){console.log("Custom method",r,n),this[r](...n);return}console.log("Map method",r),this.applyMapMethod(r,n)})}};var Xt="0.1.0";console.log("pymaplibregl",Xt);typeof Shiny>"u"&&(window.pymaplibregl=function({mapOptions:t,calls:e}){let r="pymaplibregl",n=document.getElementById(r),i=new Z(Object.assign({container:n.id},t));i.getMap().on("load",()=>{i.render(e)})});if(typeof Shiny<"u"){class t extends Shiny.OutputBinding{find(r){return r.find(".shiny-maplibregl-output")}renderValue(r,n){console.log("id:",r.id,"payload:",n);let i=window._maplibreWidget=new Z(Object.assign({container:r.id},n.mapData.mapOptions)),a=i.getMap();a.on("load",()=>{i.render(n.mapData.calls)}),a.on("click",o=>{console.log(o);let c=`${r.id}`,u={coords:o.lngLat,point:o.point};console.log(c,u),Shiny.onInputChange(c,u)});let s=`pymaplibregl-${r.id}`;console.log(s),Shiny.addCustomMessageHandler(s,({id:o,calls:c})=>{console.log(o,c),i.render(c)})}}Shiny.outputBindings.register(new t,"shiny-maplibregl-output")}})(); /*! Bundled license information: mustache/mustache.mjs: diff --git a/maplibre/ui.py b/maplibre/ui.py index db1549d5..c1e788ef 100644 --- a/maplibre/ui.py +++ b/maplibre/ui.py @@ -8,6 +8,7 @@ from ._constants import __version__, _shiny_output_class +# TODO: extract maplibregl version maplibregl_dep = HTMLDependency( "maplibregl", version="3.6.2", @@ -49,6 +50,7 @@ def output_maplibregl(id_: str, height: [int | str] = 200) -> Tag: script={"src": "dist.min.js", "type": "module"}, ) +# TODO: Remove duplicated constant DECKGL_VERSION = "9.0.16" deckgl_json_dep = HTMLDependency( @@ -61,3 +63,20 @@ def output_maplibregl(id_: str, height: [int | str] = 200) -> Tag: def use_deckgl() -> Tag: return ui.div(deckgl_dep, deckgl_json_dep) + + +MAPBOXGL_DRAW_VERSION = "1.4.3" + +mapboxgl_draw_dep = HTMLDependency( + name="mapbox-gl-draw-plugin", + version=MAPBOXGL_DRAW_VERSION, + source={ + "href": f"https://www.unpkg.com/@mapbox/mapbox-gl-draw@{MAPBOXGL_DRAW_VERSION}/dist/" + }, + script={"src": "mapbox-gl-draw.js", "type": "module"}, + stylesheet={"href": "mapbox-gl-draw.css"}, +) + + +def use_mapboxgl_draw() -> Tag: + return ui.div(mapboxgl_draw_dep) diff --git a/srcjs/pymaplibregl.js b/srcjs/pymaplibregl.js index 93d33ec3..a51f7095 100644 --- a/srcjs/pymaplibregl.js +++ b/srcjs/pymaplibregl.js @@ -17,6 +17,13 @@ function getJSONConverter() { return new deck.JSONConverter({ configuration }); } +// MapboxDraw must be imported before this one +if (typeof MapboxDraw !== "undefined") { + MapboxDraw.constants.classes.CONTROL_BASE = "maplibregl-ctrl"; + MapboxDraw.constants.classes.CONTROL_PREFIX = "maplibregl-ctrl-"; + MapboxDraw.constants.classes.CONTROL_GROUP = "maplibregl-ctrl-group"; +} + // TODO: Rename to 'MapLibreWidget' export default class PyMapLibreGL { constructor(mapOptions) { @@ -35,6 +42,9 @@ export default class PyMapLibreGL { // this._map.addControl(new maplibregl.NavigationControl()); this._JSONConverter = getJSONConverter(); + + // Just a test + if (typeof MapboxDraw !== "undefined") this.addMapboxDraw(); } getMap() { @@ -152,6 +162,17 @@ export default class PyMapLibreGL { this._deckOverlay.setProps({ layers }); } + addMapboxDraw() { + const draw = new MapboxDraw({ + displayControlsDefault: false, + controls: { + polygon: true, + trash: true, + }, + }); + this._map.addControl(draw); + } + render(calls) { calls.forEach(([name, params]) => { // Custom method