UNPKG

maplibre-gl

Version:

BSD licensed community fork of mapbox-gl, a WebGL interactive maps library

6 lines 19 kB
/** * MapLibre GL JS * @license 3-Clause BSD. Full text of license: https://github.com/maplibre/maplibre-gl-js/blob/v6.9.0/LICENSE.txt */ import{$n as e,An as t,Cn as n,D as r,Dt as i,En as a,Et as o,F as s,Fn as c,G as l,H as u,N as d,Nn as f,On as p,Pr as m,Ri as h,Rr as g,Sn as _,Vt as v,_ as y,bt as b,c as x,d as S,dt as C,g as w,gr as T,hn as E,i as D,l as O,ln as k,lr as A,mr as j,o as M,r as N,rn as P,sr as F,t as I,tn as L,tr as R,un as z,z as B}from"./maplibre-gl-shared.mjs";function V(e){let t=typeof e;if(t===`number`||t===`boolean`||t===`string`||e==null)return JSON.stringify(e);if(Array.isArray(e)){let t=`[`;for(let n of e)t+=`${V(n)},`;return`${t}]`}let n=Object.keys(e).sort(),r=`{`;for(let t=0;t<n.length;t++)r+=`${JSON.stringify(n[t])}:${V(e[n[t]])},`;return`${r}}`}function H(e){let t=``;for(let n of E)t+=`/${V(e[n])}`;return t}function U(e,t){let n={};for(let r=0;r<e.length;r++){let i=t&&t[e[r].id]||H(e[r]);t&&(t[e[r].id]=i);let a=n[i];a||=n[i]=[],a.push(e[r])}let r=[];for(let e in n)r.push(n[e]);return r}var W=class{constructor(e,t){this.keyCache={},e&&this.replace(e,t)}replace(e,t){this._layerConfigs={},this._layers={},this.update(e,[],t)}update(e,t,n){for(let t of e){this._layerConfigs[t.id]=t;let e=this._layers[t.id]=y(t,n);e._featureFilter=k(e.filter,`layers[${t.id}].filter`,n),this.keyCache[t.id]&&delete this.keyCache[t.id]}for(let e of t)delete this.keyCache[e],delete this._layerConfigs[e],delete this._layers[e];this.familiesBySource={};let r=U(Object.values(this._layerConfigs),this.keyCache);for(let e of r){let t=e.map(e=>this._layers[e.id]),n=t[0];if(n.isHidden())continue;let r=n.source||``,i=this.familiesBySource[r];i||=this.familiesBySource[r]={};let a=n.sourceLayer||`_geojsonTileLayer`,o=i[a];o||=i[a]=[],o.push(t)}}},G=class{constructor(e){let t={},n=[];for(let r in e){let i=e[r],a=t[r]={};for(let e in i){let t=i[e];if(!t||t.bitmap.width===0||t.bitmap.height===0)continue;let r={x:0,y:0,w:t.bitmap.width+2,h:t.bitmap.height+2};n.push(r),a[e]={rect:r,metrics:t.metrics}}}let{w:r,h:i}=s(n),a=new o({width:r||1,height:i||1});for(let n in e){let r=e[n];for(let e in r){let i=r[e];if(!i||i.bitmap.width===0||i.bitmap.height===0)continue;let s=t[n][e].rect;o.copy(i.bitmap,a,{x:0,y:0},{x:s.x+1,y:s.y+1},i.bitmap)}}this.image=a,this.positions=t}};P(`GlyphAtlas`,G);var K=class{constructor(e){this.tileID=new S(e.tileID.overscaledZ,e.tileID.wrap,e.tileID.canonical.z,e.tileID.canonical.x,e.tileID.canonical.y),this.uid=e.uid,this.zoom=e.zoom,this.pixelRatio=e.pixelRatio,this.tileSize=e.tileSize,this.source=e.source,this.overscaling=this.tileID.overscaleFactor(),this.showCollisionBoxes=e.showCollisionBoxes,this.collectResourceTiming=!!e.collectResourceTiming,this.returnDependencies=!!e.returnDependencies,this.promoteId=e.promoteId,this.inFlightDependencies=[]}async parse(e,t,n,r,i){this.data=e,this.collisionBoxArray=new v;let a=new M(Object.keys(e.layers).sort()),o=new N(this.tileID,this.promoteId);o.bucketLayerIDs=[];let s={},c={featureIndex:o,iconDependencies:{},patternDependencies:{},glyphDependencies:{},dashDependencies:{},availableImages:n,subdivisionGranularity:i},l=t.familiesBySource[this.source];for(let t in l){let r=e.layers[t];if(!r)continue;r.version===1&&m(`Vector tile source "${this.source}" layer "${t}" does not use vector tile spec v2 and therefore may have some rendering errors.`);let i=a.encode(t),u=[];for(let e=0;e<r.length;e++){let n=r.feature(e),a=o.getId(n,t);u.push({feature:n,id:a,index:e,sourceLayerIndex:i})}for(let e of l[t]){let t=e[0];t.source!==this.source&&m(`layer.source = ${t.source} does not equal this.source = ${this.source}`),!t.isHidden(this.zoom,!0)&&(q(e,this.zoom,n),(s[t.id]=t.createBucket({index:o.bucketLayerIDs.length,layers:e,zoom:this.zoom,pixelRatio:this.pixelRatio,overscaling:this.overscaling,collisionBoxArray:this.collisionBoxArray,sourceLayerIndex:i,sourceID:this.source})).populate(u,c,this.tileID.canonical),o.bucketLayerIDs.push(e.map(e=>e.id)))}}let u=T(c.glyphDependencies,e=>Object.keys(e));for(let e of this.inFlightDependencies)e?.abort();this.inFlightDependencies=[];let f=Promise.resolve({});if(Object.keys(u).length){let e=new AbortController;this.inFlightDependencies.push(e),f=r.sendAsync({type:`GG`,data:{stacks:u,source:this.source,tileID:this.tileID,type:`glyphs`}},e)}let p=Object.keys(c.iconDependencies),h=Promise.resolve({});if(p.length){let e=new AbortController;this.inFlightDependencies.push(e),h=r.sendAsync({type:`GI`,data:{icons:p,source:this.source,tileID:this.tileID,type:`icons`}},e)}let g=Object.keys(c.patternDependencies),_=Promise.resolve({});if(g.length){let e=new AbortController;this.inFlightDependencies.push(e),_=r.sendAsync({type:`GI`,data:{icons:g,source:this.source,tileID:this.tileID,type:`patterns`}},e)}let y=c.dashDependencies,b=Promise.resolve({});if(Object.keys(y).length){let e=new AbortController;this.inFlightDependencies.push(e),b=r.sendAsync({type:`GDA`,data:{dashes:y}},e)}let[x,S,C,w]=await Promise.all([f,h,_,b]),E=new G(x),D=new d(S,C);for(let e in s){let t=s[e];t.hasDependencies&&(q(t.layers,this.zoom,n),t.addFeatures({options:c,canonical:this.tileID.canonical,glyphMap:x,glyphPositions:E.positions,iconMap:S,iconPositions:D.iconPositions,patternMap:C,patternPositions:D.patternPositions,dashPositions:w,showCollisionBoxes:this.showCollisionBoxes}))}return{buckets:Object.values(s).filter(e=>!e.isEmpty()),featureIndex:o,collisionBoxArray:this.collisionBoxArray,glyphAtlasImage:E.image,imageAtlas:D,dashPositions:w,glyphMap:this.returnDependencies?x:null,iconMap:this.returnDependencies?S:null,glyphPositions:this.returnDependencies?E.positions:null}}};function q(e,t,n){let r=new L(t);for(let t of e)t.recalculate(r,n)}var J=class{constructor(){this.loading={},this.loaded={},this.parsing={}}startLoading(e,t){this.loading[e]=t}finishLoading(e){delete this.loading[e]}abort(e){let t=this.loading[e];t?.abort&&(t.abort.abort(),delete this.loading[e])}getParsing(e){return this.parsing[e]}setParsing(e,t){this.parsing[e]=t}removeParsing(e){delete this.parsing[e]}markLoaded(e,t){this.loaded[e]=t}getLoaded(e){let t=this.loaded[e];if(t)return t}removeLoaded(e){delete this.loaded[e]}clearLoaded(){this.loaded={}}},Y=class{constructor(e){this.start=`${e}#start`,this.end=`${e}#end`,this.measure=e,performance.mark(this.start)}finish(){performance.mark(this.end);let e=performance.getEntriesByName(this.measure);return e.length===0&&(performance.measure(this.measure,this.start,this.end),e=performance.getEntriesByName(this.measure),performance.clearMarks(this.start),performance.clearMarks(this.end),performance.clearMeasures(this.measure)),e}},X=class{constructor(e,t,n,r,i){this.type=e,this.properties=n||{},this.extent=i,this.pointsArray=t,this.id=r}loadGeometry(){return this.pointsArray.map(e=>e.map(e=>new h(e.x,e.y)))}},ee=class{constructor(e,t,n){this.version=2,this._myFeatures=e,this.name=t,this.length=e.length,this.extent=n}feature(e){return this._myFeatures[e]}},te=class{constructor(){this.layers={}}addLayer(e){this.layers[e.name]=e}};function ne(e,t,n){let{extent:i}=e,a=2**(n.z-t.z),o=(n.x-t.x*a)*i,s=(n.y-t.y*a)*i,c=[];for(let t=0;t<e.length;t++){let n=e.feature(t),l=n.loadGeometry();for(let e of l)for(let t of e)t.x=t.x*a-o,t.y=t.y*a-s;l=r(l,n.type,-128,-128,i+128,i+128),l.length!==0&&c.push(new X(n.type,l,n.properties,n.id,i))}return new ee(c,e.name,i)}var re=class{constructor(e,t,n){this.actor=e,this.layerIndex=t,this.availableImages=n,this.tileState=new J,this.overzoomedTileResultCache=new I(1e3)}loadVectorTile(t,n){try{return{vectorTile:t.encoding===`mlt`?new D(n):new C(new B(n)),rawData:n}}catch(r){let i=new Uint8Array(n),a=i[0]===31&&i[1]===139,o=`Unable to parse the tile at ${t.request.url}, `;throw o+=a?`please make sure the data is not gzipped and that you have configured the relevant header in the server`:`got error: ${e(r).message}`,Error(o)}}async loadTile(e){let{uid:t,overzoomParameters:n}=e;n&&(e.request=n.overzoomRequest);let r=this._startRequestTiming(e),i=new K(e);this.tileState.startLoading(t,i);let a=new AbortController;i.abort=a;try{let o=await _(e.request,a);if(e.etag&&e.etag===o.etag)return this.tileState.finishLoading(t),this._getEtagUnmodifiedResult(o,r);let s=this.loadVectorTile(e,o.data);if(this.tileState.finishLoading(t),!s)return null;let{vectorTile:c,rawData:l}=s;n&&({vectorTile:c,rawData:l}=this._getOverzoomTile(e,c));let u=this._getExpiryData(o),d=this._finishRequestTiming(r);i.vectorTile=c,i.etag=o.etag,this.tileState.markLoaded(t,i);let f={rawData:l,cacheControl:u,resourceTiming:d};return this.tileState.setParsing(t,f),await this._parseWorkerTile(i,e)}catch(e){throw this.tileState.finishLoading(t),this.tileState.markLoaded(t,i),e}}_getEtagUnmodifiedResult(e,t){let n=this._getExpiryData(e),r=this._finishRequestTiming(t);return R({etagUnmodified:!0},n,r)}async _parseWorkerTile(e,t){let n=this.tileState.getParsing(e.uid),r=await e.parse(e.vectorTile,this.layerIndex,this.availableImages,this.actor,t.subdivisionGranularity);if(n){let{rawData:i,cacheControl:a,resourceTiming:o}=n,s=t.overzoomParameters?`mvt`:t.encoding;r=R({rawTileData:i.slice(0),encoding:s},r,a,o),this.tileState.removeParsing(e.uid)}else e.etag&&(r=R(r,{etag:e.etag}));return r}_getExpiryData({expires:e,cacheControl:t,etag:n}){let r={};return e&&(r.expires=e),t&&(r.cacheControl=t),n&&(r.etag=n),r}_startRequestTiming(e){if(e.request?.collectResourceTiming)return new Y(e.request.url)}_finishRequestTiming(e){let t=e?.finish();return t?{resourceTiming:JSON.parse(JSON.stringify(t))}:{}}_getOverzoomTile(e,t){let{tileID:n,source:r,overzoomParameters:i}=e,{maxZoomTileID:a}=i,o=`${a.key}_${n.key}_${e.request?.url}`,s=this.overzoomedTileResultCache.get(o);if(s)return s;let c=new te,l=this.layerIndex.familiesBySource[r];for(let e in l){let r=t.layers[e];if(!r)continue;let i=ne(r,a,n.canonical);i.length>0&&c.addLayer(i)}let u={vectorTile:c,rawData:O(c).buffer};return this.overzoomedTileResultCache.set(o,u),u}async reloadTile(e){let t=e.uid,n=this.tileState.getLoaded(t);if(!n)throw Error(`Should not be trying to reload a tile that was never loaded or has been removed`);if(n.vectorTile)return n.showCollisionBoxes=e.showCollisionBoxes,await this._parseWorkerTile(n,e)}async abortTile(e){this.tileState.abort(e.uid)}async removeTile(e){this.tileState.removeLoaded(e.uid)}},ie=class{constructor(){this.loaded={}}async loadTile(e){let{uid:t,encoding:n,rawImageData:r,redFactor:a,greenFactor:o,blueFactor:s,baseShift:c}=e,l=r.width+4,u=r.height+4,d=A(r)?new i({width:l,height:u},await F(r,-2,-2,l,u)):r,f=new b(t,d,n,a,o,s,c);return this.loaded||={},this.loaded[t]=f,f}removeTile(e){let t=this.loaded,n=e.uid;t?.[n]&&delete t[n]}},ae=class{constructor(e,t,n,r=oe){this.actor=e,this.layerIndex=t,this.availableImages=n,this.tileState=new J,this._createGeoJSONIndex=r}loadVectorTile(e){if(!this._geoJSONIndex)throw Error(`Unable to parse the data into a cluster or geojson`);let{z:t,x:n,y:r}=e.tileID.canonical,i=this._geoJSONIndex.getTile(t,n,r);if(!i)return null;let a=new x(i.features,{version:2,extent:g});return{vectorTile:a,rawData:O(a,c).buffer}}async loadTile(e){let{uid:t}=e,n=new K(e);n.abort=new AbortController;try{let r=this.loadVectorTile(e);if(!r)return null;let{vectorTile:i,rawData:a}=r;n.vectorTile=i,this.tileState.markLoaded(t,n);let o={rawData:a};return this.tileState.setParsing(t,o),await this._parseWorkerTile(n,e)}catch(e){throw this.tileState.markLoaded(t,n),e}}async _parseWorkerTile(e,t){let n=this.tileState.getParsing(e.uid),r=await e.parse(e.vectorTile,this.layerIndex,this.availableImages,this.actor,t.subdivisionGranularity);if(n){let{rawData:t}=n;r=R({rawTileData:t.slice(0),encoding:`mvt`},r),this.tileState.removeParsing(e.uid)}return r}async abortTile(e){this.tileState.abort(e.uid)}async removeTile(e){this.tileState.removeLoaded(e.uid)}async loadData(e){this._pendingRequest?.abort();let t=this._startRequestTiming(e);this._pendingRequest=new AbortController;try{await this.loadAndProcessGeoJSON(e,this._pendingRequest),delete this._pendingRequest,this.tileState.clearLoaded();let n={};return e.request&&(n.data=e.data),this._finishRequestTiming(t,e,n),n}catch(e){if(delete this._pendingRequest,!f(e))throw e;return{abandoned:!0}}}_startRequestTiming(e){if(e.request?.collectResourceTiming)return new Y(e.request.url)}_finishRequestTiming(e,t,n){let r=e?.finish();r&&(n.resourceTiming={[t.source]:JSON.parse(JSON.stringify(r))})}async reloadTile(e){let t=e.uid,n=this.tileState.getLoaded(t);if(!n)return await this.loadTile(e);if(n.vectorTile)return n.showCollisionBoxes=e.showCollisionBoxes,await this._parseWorkerTile(n,e)}async loadAndProcessGeoJSON(e,t){if(e.request&&(e.data=(await n(e.request,t)).data),e.data){e.data=this._filterGeoJSON(e.data,e.filter,e.source),this._geoJSONIndex=this._createGeoJSONIndex(e.data,e);return}if(e.dataDiff){this._geoJSONIndex??=this._createGeoJSONIndex({type:`FeatureCollection`,features:[]},e),this._geoJSONIndex.updateData(e.dataDiff,this._getFilterPredicate(e.filter,e.source));return}if(e.updateCluster&&this._geoJSONIndex.updateClusterOptions(e.geojsonVtOptions.cluster,Z(e)),this._geoJSONIndex==null)throw Error(`Input data given to '${e.source}' is not a valid GeoJSON object.`)}_filterGeoJSON(e,t,n){if(e.type!==`FeatureCollection`)return e;let r=this._getFilterPredicate(t,n);return r?{type:`FeatureCollection`,features:e.features.filter(e=>r(e))}:e}_getFilterPredicate(e,t){if(typeof e!=`boolean`&&!e?.length)return;let n=z(e,`sources.${t}.filter`,{type:`boolean`,"property-type":`data-driven`,overridable:!1,transition:!1});if(n.result===`error`)throw Error(n.value.map(e=>`${e.key}: ${e.message}`).join(`, `));return e=>n.value.evaluate({zoom:0},e)}async removeSource(e){this._pendingRequest?.abort()}getClusterExpansionZoom(e){return this._geoJSONIndex.getClusterExpansionZoom(e.clusterId)}getClusterChildren(e){return this._geoJSONIndex.getClusterChildren(e.clusterId)}getClusterLeaves(e){return this._geoJSONIndex.getClusterLeaves(e.clusterId,e.limit,e.offset)}};function oe(e,t){let n=R(t.geojsonVtOptions||{},{updateable:!0,clusterOptions:Z(t)});return new l(e,n)}function Z({geojsonVtOptions:e,clusterProperties:t,source:n}){if(!t||!e.clusterOptions)return e.clusterOptions;let r={},i={},a={accumulated:null,zoom:0},o={properties:null},s=Object.keys(t);for(let e of s){let[a,o]=t[e],s=z(o,`sources.${n}.clusterProperties.${e}[1]`),c=z(typeof a==`string`?[a,[`accumulated`],[`get`,e]]:a,`sources.${n}.clusterProperties.${e}[0]`);r[e]=s.value,i[e]=c.value}return e.clusterOptions.map=e=>{o.properties=e;let t={};for(let e of s)t[e]=r[e].evaluate(a,o);return t},e.clusterOptions.reduce=(e,t)=>{o.properties=t;for(let t of s)a.accumulated=e[t],e[t]=i[t].evaluate(a,o)},e.clusterOptions}async function Q(e){if(e.endsWith(`.mjs`)){await import(e);return}let t=await fetch(e,{credentials:`same-origin`});if(!t.ok)throw Error(`Failed to load ${e}: ${t.status}`);let n=await t.text();if(/^[ \t]*(import|export)\s/m.test(n)){let e=URL.createObjectURL(new Blob([n],{type:`text/javascript`}));try{await import(e)}finally{URL.revokeObjectURL(e)}return}globalThis.eval(n)}var $=class{constructor(e){this.self=e,this.actor=new w(e),this.layerIndexes={},this.availableImages={},this.workerSources={},this.demWorkerSources={},this.externalWorkerSourceTypes={},this.globalStates=new Map,this.self.registerWorkerSource=(e,t)=>{if(this.externalWorkerSourceTypes[e])throw Error(`Worker source with name "${e}" already registered.`);this.externalWorkerSourceTypes[e]=t},this.self.addProtocol=p,this.self.removeProtocol=t,this.self.registerRTLTextPlugin=e=>{u.setMethods(e)},this.self.makeRequest=a,this.actor.registerMessageHandler(`LDT`,(e,t)=>this._getDEMWorkerSource(e,t.source).loadTile(t)),this.actor.registerMessageHandler(`RDT`,async(e,t)=>{this._getDEMWorkerSource(e,t.source).removeTile(t)}),this.actor.registerMessageHandler(`GCEZ`,async(e,t)=>this._getWorkerSource(e,t.type,t.source).getClusterExpansionZoom(t)),this.actor.registerMessageHandler(`GCC`,async(e,t)=>this._getWorkerSource(e,t.type,t.source).getClusterChildren(t)),this.actor.registerMessageHandler(`GCL`,async(e,t)=>this._getWorkerSource(e,t.type,t.source).getClusterLeaves(t)),this.actor.registerMessageHandler(`LD`,(e,t)=>this._getWorkerSource(e,t.type,t.source).loadData(t)),this.actor.registerMessageHandler(`LT`,(e,t)=>this._getWorkerSource(e,t.type,t.source).loadTile(t)),this.actor.registerMessageHandler(`RT`,(e,t)=>this._getWorkerSource(e,t.type,t.source).reloadTile(t)),this.actor.registerMessageHandler(`AT`,(e,t)=>this._getWorkerSource(e,t.type,t.source).abortTile(t)),this.actor.registerMessageHandler(`RMT`,(e,t)=>this._getWorkerSource(e,t.type,t.source).removeTile(t)),this.actor.registerMessageHandler(`RS`,async(e,t)=>{if(!this.workerSources[e]?.[t.type]?.[t.source])return;let n=this.workerSources[e][t.type][t.source];delete this.workerSources[e][t.type][t.source],n.removeSource!==void 0&&n.removeSource(t)}),this.actor.registerMessageHandler(`RM`,async e=>{delete this.layerIndexes[e],delete this.availableImages[e],delete this.workerSources[e],delete this.demWorkerSources[e],this.globalStates.delete(e)}),this.actor.registerMessageHandler(`SR`,async(e,t)=>{this.referrer=t}),this.actor.registerMessageHandler(`SRPS`,(e,t)=>this._syncRTLPluginState(e,t)),this.actor.registerMessageHandler(`IS`,async(e,t)=>{await Q(t)}),this.actor.registerMessageHandler(`SI`,(e,t)=>this._setImages(e,t)),this.actor.registerMessageHandler(`UL`,async(e,t)=>{this._getLayerIndex(e).update(t.layers,t.removedIds,this._getGlobalState(e))}),this.actor.registerMessageHandler(`UGS`,async(e,t)=>{let n=this._getGlobalState(e);for(let e in t)n[e]=t[e]}),this.actor.registerMessageHandler(`SL`,async(e,t)=>{this._getLayerIndex(e).replace(t,this._getGlobalState(e))})}_getGlobalState(e){let t=this.globalStates.get(e);return t||(t={},this.globalStates.set(e,t)),t}async _setImages(e,t){this.availableImages[e]=t;for(let n in this.workerSources[e]){let r=this.workerSources[e][n];for(let e in r)r[e].availableImages=t}}async _syncRTLPluginState(e,t){return await u.syncState(t,Q)}_getAvailableImages(e){let t=this.availableImages[e];return t||=[],t}_getLayerIndex(e){let t=this.layerIndexes[e];return t||=this.layerIndexes[e]=new W,t}_getWorkerSource(e,t,n){if(this.workerSources[e]||={},this.workerSources[e][t]||={},!this.workerSources[e][t][n]){let r={sendAsync:(t,n)=>(t.targetMapId=e,this.actor.sendAsync(t,n))};switch(t){case`vector`:this.workerSources[e][t][n]=new re(r,this._getLayerIndex(e),this._getAvailableImages(e));break;case`geojson`:this.workerSources[e][t][n]=new ae(r,this._getLayerIndex(e),this._getAvailableImages(e));break;default:this.workerSources[e][t][n]=new this.externalWorkerSourceTypes[t](r,this._getLayerIndex(e),this._getAvailableImages(e))}}return this.workerSources[e][t][n]}_getDEMWorkerSource(e,t){return this.demWorkerSources[e]||={},this.demWorkerSources[e][t]||=new ie,this.demWorkerSources[e][t]}};j(self)&&(self.worker=new $(self));export{$ as default}; //# sourceMappingURL=maplibre-gl-worker.mjs.map