UNPKG

weatherlayers-gl

Version:

WeatherLayers GL - Weather Visualization Layers for deck.gl

16 lines (14 loc) 7.8 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 */ "use strict";function t(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(a){if("default"!==a){var n=Object.getOwnPropertyDescriptor(t,a);Object.defineProperty(e,a,n.get?n:{enumerable:!0,get:function(){return t[a]}})}})),e.default=t,Object.freeze(e)}var e=t(require("geotiff"));function a(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 n(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 i=new Map;async function r(t){let a;try{a=await e.fromUrl(t,{allowFullFile:!0,blockSize:10485760})}catch(e){throw new Error(`Image ${t} can't be decoded.`,{cause:e})}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=i)=>{if(!1===a)return t(e);const n=a.get(e);if(n)return n;const r=t(e);return a.set(e,r),r.then((t=>{a.set(e,t)})),r}}const o=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 r=i.getImageData(0,0,n.width,n.height),{data:s,width:o,height:c}=r;return{data:s,width:o,height:c}}(t);if(t.includes(".tif"))return r(t);throw new Error("Unsupported data format")})),c=s((async t=>(await fetch(t)).json()));function l(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 h,f,u,d,m,w,g,p,E,y,C,T,A,I,b,v,R,L,O;!function(t){t.METRIC="METRIC",t.IMPERIAL="IMPERIAL",t.NAUTICAL="NAUTICAL"}(h||(h={})),function(t){t.LICENSOR="licensor",t.PRODUCER="producer",t.PROCESSOR="processor",t.HOST="host"}(f||(f={})),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"}(u||(u={})),function(t){t.DATA="data",t.OVERVIEW="overview",t.THUMBNAIL="thumbnail",t.PALETTE="palette"}(d||(d={}));const S=h.METRIC;function P(t,e){const a=t.providers.find((t=>t.roles.includes(f.PRODUCER))),n=t.providers.find((t=>t.roles.includes(f.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===S))??a[0]}w=new WeakMap,g=new WeakMap,p=new WeakMap,E=new WeakMap,m=new WeakSet,y=function(t,e={}){const n=e.accessToken??a(this,w,"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){a(this,p,"f").set(t.id,t)},T=function(t,e){a(this,E,"f").has(t)||a(this,E,"f").set(t,new Map),a(this,E,"f").get(t).set(e.properties.datetime,e)},A=async function(t={}){const e=t.url??a(this,w,"f").url??"https://catalog.weatherlayers.com",n=a(this,m,"m",y).call(this,`${e}/catalog`,t);return await c(n,a(this,g,"f"))},I=async function(t={}){const e=(await a(this,m,"m",A).call(this,t)).links.find((t=>t.rel===u.DATA));if(!e)throw new Error("STAC Catalog data link not found");const n=a(this,m,"m",y).call(this,e.href,t),i=(await c(n,a(this,g,"f"))).collections;for(const t of i)a(this,m,"m",C).call(this,t);return i},b=async function(t,e={}){await a(this,m,"m",I).call(this,e);let n=a(this,p,"f").get(t);if(!n)throw new Error(`STAC Collection ${t} not found`);return a(this,m,"m",C).call(this,n),n},v=async function(t,e={}){const n=await a(this,m,"m",b).call(this,t,e),i=Object.values(n.assets).find((t=>t.roles.includes(d.PALETTE)&&"application/json"===t.type));if(!i)throw new Error(`STAC Collection ${t} palette asset not found`);const r=a(this,m,"m",y).call(this,i.href,a(this,w,"f"));return await c(r,a(this,g,"f"))},R=async function(t,e,n={}){const i=(await a(this,m,"m",A).call(this,n)).links.find((t=>t.rel===u.SEARCH));if(!i)throw new Error("STAC Catalog search link not found");const r=new URL(i.href);r.searchParams.set("collections",t),r.searchParams.set("datetime",function(t){const[e,a]=t;return`${e??".."}/${a??".."}`}(e));const s=a(this,m,"m",y).call(this,r.toString(),n),o=(await c(s,a(this,g,"f"))).features;for(const e of o)a(this,m,"m",T).call(this,t,e);return o},L=async function(t,e,n={}){let i=a(this,E,"f").get(t)?.get(e);if(!i){i=(await a(this,m,"m",R).call(this,t,[e,e],n))[0]}if(!i)throw new Error(`STAC Item ${t}/${e} not found`);return i},O=async function(t,e,n={}){const i=n.dataFormat??a(this,w,"f").dataFormat??"byte.png",r=(await a(this,m,"m",L).call(this,t,e)).assets[`data.${i}`];if(!r)throw new Error(`STAC Item ${t}/${e} data asset not found`);const s=a(this,m,"m",y).call(this,r.href,a(this,w,"f"));return o(s,a(this,g,"f"))},exports.Client=class{constructor(t){m.add(this),w.set(this,void 0),g.set(this,new Map),p.set(this,new Map),E.set(this,new Map),n(this,w,t,"f")}getConfig(){return{...a(this,w,"f")}}setConfig(t){n(this,w,t,"f")}updateConfig(t){this.setConfig({...a(this,w,"f"),...t})}async loadCatalog(t={}){return(await a(this,m,"m",I).call(this,t)).map((t=>t.id))}async loadDataset(t,e={}){const n=await a(this,m,"m",b).call(this,t,e),i=e.unitSystem??a(this,w,"f").unitSystem??S,r=e.attributionLinkClass??a(this,w,"f").attributionLinkClass??"";return{title:n.title,unitFormat:$(n,i),attribution:P(n,r),datetimeRange:n.extent.temporal.interval[0],datetimes:n.links.filter((t=>t.rel===u.ITEM)).map((t=>t.datetime)).filter((t=>!!t)),palette:await a(this,m,"m",v).call(this,t)}}async loadDatasetSlice(t,e,n={}){return{datetimes:(await a(this,m,"m",R).call(this,t,e,n)).map((t=>t.properties.datetime))}}async loadDatasetData(t,e,n={}){const i=n.datetimeInterpolate??a(this,w,"f").datetimeInterpolate??!1,r=await a(this,m,"m",b).call(this,t,n),s=Array.from(a(this,E,"f").get(t)?.values()??[]).map((t=>t.properties.datetime)).sort(),o=function(t,e){return[...t].reverse().find((t=>t<=e))}(s,e),c=function(t,e){return t.find((t=>t>=e))}(s,e);let h,f;i&&o&&c&&o!==c?(h=o,f=c):(h=e,f=null);const[u,d]=await Promise.all([a(this,m,"m",O).call(this,t,h,n),i&&f?a(this,m,"m",O).call(this,t,f,n):null]);return{image:u,image2:d,imageWeight:i&&f?l(h,f,e):0,imageType:r["weatherLayers:imageType"],imageUnscale:u.data instanceof Uint8Array||u.data instanceof Uint8ClampedArray?r["weatherLayers:imageUnscale"]:null,bounds:r.extent.spatial.bbox[0]}}}; //# sourceMappingURL=weatherlayers-client.min.cjs.map