UNPKG

weatherlayers-gl

Version:

WeatherLayers GL - Weather Visualization Layers for deck.gl

16 lines (14 loc) 7.53 kB
/*! * Copyright (c) 2021-2023 WeatherLayers.com * * WeatherLayers Cloud Client 2023.8.1 * * A valid access token is required to use the library. Contact support@weatherlayers.com for details. * * Homepage - https://weatherlayers.com/ * Demo - https://demo.weatherlayers.com/ * Docs - https://docs.weatherlayers.com/ * WeatherLayers Cloud Terms of Use - https://weatherlayers.com/terms-of-use.html */ import*as t from"geotiff";function e(t,e,a,n){if("a"===a&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?n:"a"===a?n.call(t):n?n.value:e.get(t)}function a(t,e,a,n,i){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?i.call(t,a):i?i.value=a:e.set(t,a),a}const n=new Map;async function i(e){let a;try{a=await t.fromUrl(e,{allowFullFile:!0,blockSize:10485760})}catch(t){throw new Error(`Image ${e} can't be decoded.`,{cause:t})}const n=await a.getImage(0),i=function(t,e){if(null==e)return t;const a=t.slice(0);for(let t=0;t<a.length;t++)Math.abs(a[t]-e)<2*Number.EPSILON&&(a[t]=NaN);return a}(await n.readRasters({interleave:!0}),n.getGDALNoData());return{data:i,width:n.getWidth(),height:n.getHeight()}}function s(t){return(e,a=n)=>{if(!1===a)return t(e);const i=a.get(e);if(i)return i;const s=t(e);return a.set(e,s),s.then((t=>{a.set(e,t)})),s}}const r=s((t=>{if(t.includes(".png"))return async function(t){const e=await(await fetch(t)).blob(),a=new Image;a.src=URL.createObjectURL(e);try{await a.decode()}catch(e){throw new Error(`Image ${t} can't be decoded.`,{cause:e})}const n=document.createElement("canvas");n.width=a.width,n.height=a.height;const i=n.getContext("2d");i.drawImage(a,0,0);const s=i.getImageData(0,0,n.width,n.height),{data:r,width:o,height:c}=s;return{data:r,width:o,height:c}}(t);if(t.includes(".tif"))return i(t);throw new Error("Unsupported data format")})),o=s((async t=>(await fetch(t)).json()));function c(t,e,a){if(!e){if(t===a)return 0;throw new Error("Invalid state")}if(a<=t)return 0;if(a>=e)return 1;{const n=new Date(t),i=new Date(e);return(new Date(a).getTime()-n.getTime())/(i.getTime()-n.getTime())}}var l,h,f,u,d,m,w,g,p,E,C,y,T,A,I,R,L,S,v;!function(t){t.METRIC="METRIC",t.IMPERIAL="IMPERIAL",t.NAUTICAL="NAUTICAL"}(l||(l={})),function(t){t.LICENSOR="licensor",t.PRODUCER="producer",t.PROCESSOR="processor",t.HOST="host"}(h||(h={})),function(t){t.LICENSE="license",t.SELF="self",t.ROOT="root",t.PARENT="parent",t.CHILD="child",t.ITEM="item",t.COLLECTION="collection",t.CONFORMANCE="conformance",t.DATA="data",t.SEARCH="search"}(f||(f={})),function(t){t.DATA="data",t.OVERVIEW="overview",t.THUMBNAIL="thumbnail",t.PALETTE="palette"}(u||(u={}));const b=l.METRIC;function P(t,e){const a=t.providers.find((t=>t.roles.includes(h.PRODUCER))),n=t.providers.find((t=>t.roles.includes(h.PROCESSOR)));return[...a?[`<a href="${a.url}"${e?` class="${e}"`:""}>${a.name}</a>`]:[],...n?[`<a href="${n.url}"${e?` class="${e}"`:""}>${n.name}</a>`]:[]].join(" via ")}function $(t,e){const a=t["weatherLayers:units"];return a.find((t=>t.system===e))??a.find((t=>t.system===b))??a[0]}class M{constructor(t){d.add(this),m.set(this,void 0),w.set(this,new Map),g.set(this,new Map),p.set(this,new Map),a(this,m,t,"f")}getConfig(){return{...e(this,m,"f")}}setConfig(t){a(this,m,t,"f")}updateConfig(t){this.setConfig({...e(this,m,"f"),...t})}async loadCatalog(t={}){return(await e(this,d,"m",A).call(this,t)).map((t=>t.id))}async loadDataset(t,a={}){const n=await e(this,d,"m",I).call(this,t,a),i=a.unitSystem??e(this,m,"f").unitSystem??b,s=a.attributionLinkClass??e(this,m,"f").attributionLinkClass??"";return{title:n.title,unitFormat:$(n,i),attribution:P(n,s),datetimeRange:n.extent.temporal.interval[0],datetimes:n.links.filter((t=>t.rel===f.ITEM)).map((t=>t.datetime)).filter((t=>!!t)),palette:await e(this,d,"m",R).call(this,t)}}async loadDatasetSlice(t,a,n={}){return{datetimes:(await e(this,d,"m",L).call(this,t,a,n)).map((t=>t.properties.datetime))}}async loadDatasetData(t,a,n={}){const i=n.datetimeInterpolate??e(this,m,"f").datetimeInterpolate??!1,s=await e(this,d,"m",I).call(this,t,n),r=Array.from(e(this,p,"f").get(t)?.values()??[]).map((t=>t.properties.datetime)).sort(),o=function(t,e){return[...t].reverse().find((t=>t<=e))}(r,a),l=function(t,e){return t.find((t=>t>=e))}(r,a);let h,f;i&&o&&l&&o!==l?(h=o,f=l):(h=a,f=null);const[u,w]=await Promise.all([e(this,d,"m",v).call(this,t,h,n),i&&f?e(this,d,"m",v).call(this,t,f,n):null]);return{image:u,image2:w,imageWeight:i&&f?c(h,f,a):0,imageType:s["weatherLayers:imageType"],imageUnscale:u.data instanceof Uint8Array||u.data instanceof Uint8ClampedArray?s["weatherLayers:imageUnscale"]:null,bounds:s.extent.spatial.bbox[0]}}}m=new WeakMap,w=new WeakMap,g=new WeakMap,p=new WeakMap,d=new WeakSet,E=function(t,a={}){const n=a.accessToken??e(this,m,"f").accessToken??null,i=new URL(t);return i.searchParams.has("access_token")||null==n||i.searchParams.set("access_token",n),i.searchParams.has("version")||i.searchParams.set("version","2023.8.1"),i.toString()},C=function(t){e(this,g,"f").set(t.id,t)},y=function(t,a){e(this,p,"f").has(t)||e(this,p,"f").set(t,new Map),e(this,p,"f").get(t).set(a.properties.datetime,a)},T=async function(t={}){const a=t.url??e(this,m,"f").url??"https://catalog.weatherlayers.com",n=e(this,d,"m",E).call(this,`${a}/catalog`,t);return await o(n,e(this,w,"f"))},A=async function(t={}){const a=(await e(this,d,"m",T).call(this,t)).links.find((t=>t.rel===f.DATA));if(!a)throw new Error("STAC Catalog data link not found");const n=e(this,d,"m",E).call(this,a.href,t),i=(await o(n,e(this,w,"f"))).collections;for(const t of i)e(this,d,"m",C).call(this,t);return i},I=async function(t,a={}){await e(this,d,"m",A).call(this,a);let n=e(this,g,"f").get(t);if(!n)throw new Error(`STAC Collection ${t} not found`);return e(this,d,"m",C).call(this,n),n},R=async function(t,a={}){const n=await e(this,d,"m",I).call(this,t,a),i=Object.values(n.assets).find((t=>t.roles.includes(u.PALETTE)&&"application/json"===t.type));if(!i)throw new Error(`STAC Collection ${t} palette asset not found`);const s=e(this,d,"m",E).call(this,i.href,e(this,m,"f"));return await o(s,e(this,w,"f"))},L=async function(t,a,n={}){const i=(await e(this,d,"m",T).call(this,n)).links.find((t=>t.rel===f.SEARCH));if(!i)throw new Error("STAC Catalog search link not found");const s=new URL(i.href);s.searchParams.set("collections",t),s.searchParams.set("datetime",function(t){const[e,a]=t;return`${e??".."}/${a??".."}`}(a));const r=e(this,d,"m",E).call(this,s.toString(),n),c=(await o(r,e(this,w,"f"))).features;for(const a of c)e(this,d,"m",y).call(this,t,a);return c},S=async function(t,a,n={}){let i=e(this,p,"f").get(t)?.get(a);if(!i){i=(await e(this,d,"m",L).call(this,t,[a,a],n))[0]}if(!i)throw new Error(`STAC Item ${t}/${a} not found`);return i},v=async function(t,a,n={}){const i=n.dataFormat??e(this,m,"f").dataFormat??"byte.png",s=(await e(this,d,"m",S).call(this,t,a)).assets[`data.${i}`];if(!s)throw new Error(`STAC Item ${t}/${a} data asset not found`);const o=e(this,d,"m",E).call(this,s.href,e(this,m,"f"));return r(o,e(this,w,"f"))};export{M as Client}; //# sourceMappingURL=weatherlayers-client.min.js.map