@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 4.37 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import e from"../../request.js";import{clone as t}from"../../core/lang.js";import{throwIfAborted as r}from"../../core/promiseUtils.js";import{normalize as o,isProtocolRelative as s,isAbsolute as l,join as n,removeTrailingSlash as i,removeFile as u,getAppBaseUrl as c,addQueryParameters as a}from"../../core/urlUtils.js";import{wgs84 as f,webMercator as p}from"../../geometry/support/spatialReferenceUtils.js";import{geographicToWebMercator as y}from"../../geometry/support/webMercatorUtils.js";import m from"../../views/2d/engine/vectorTiles/style/VectorTileSource.js";async function x(e,t){const r={source:null,sourceBase:null,sourceUrl:null,validatedSource:null,style:null,styleBase:null,styleUrl:null,sourceNameToSource:{},primarySourceName:"",spriteFormat:"png"},[o,s]="string"==typeof e?[e,null]:[null,e.jsonUrl];await S(r,"esri",e,s,t);return{layerDefinition:r.validatedSource,url:o,serviceUrl:r.sourceUrl,style:r.style,styleUrl:r.styleUrl,spriteUrl:r.style.sprite&&d(r.styleBase,r.style.sprite),spriteFormat:r.spriteFormat,glyphsUrl:r.style.glyphs&&d(r.styleBase,r.style.glyphs),sourceNameToSource:r.sourceNameToSource,primarySourceName:r.primarySourceName}}function d(...e){let t;for(const r of e)if(null!=r)if(s(r)){if(t){const e=t.split("://")[0];t=e+":"+r.trim()}}else t=l(r)?r:n(t,r);return t?i(t):void 0}async function S(t,s,l,n,i){let u,c,a;if(r(i),"string"==typeof l){const t=o(l);a=await e(t,{...i,responseType:"json",query:{f:"json",...i?.query}}),a.ssl&&(u&&(u=u.replace(/^http:/i,"https:")),c&&(c=c.replace(/^http:/i,"https:"))),u=t,c=t}else null!=l&&(a={data:l},u=l.jsonUrl||null,c=n);const f=a?.data;if(w(f))return t.styleUrl=u||null,j(t,f,c,i);if(U(f))return t.sourceUrl?g(t,f,c,!1,s,i):(t.sourceUrl=u||null,g(t,f,c,!0,s,i));throw new Error("You must specify the URL or the JSON for a service or for a style.")}function h(e){return"object"==typeof e&&!!e&&"tilejson"in e&&null!=e.tilejson}function w(e){return!!e&&"sources"in e&&!!e.sources}function U(e){return!w(e)}async function j(e,t,r,o){const s=r?u(r):c();e.styleBase=s,e.style=t,t["sprite-format"]&&"webp"===t["sprite-format"].toLowerCase()&&(e.spriteFormat="webp");const l=[];if(t.sources&&t.sources.esri){const r=t.sources.esri;r.url?await S(e,"esri",d(s,r.url),void 0,o):l.push(S(e,"esri",r,s,o))}for(const n of Object.keys(t.sources))"esri"!==n&&"vector"===t.sources[n].type&&(t.sources[n].url?l.push(S(e,n,d(s,t.sources[n].url),void 0,o)):t.sources[n].tiles&&l.push(S(e,n,t.sources[n],s,o)));await Promise.all(l)}async function g(e,t,r,o,s,l){const n=r?i(r)+"/":c(),u=v(t),f=new m(s,a(n,l?.query??{}),u);if(!o&&e.primarySourceName in e.sourceNameToSource){const t=e.sourceNameToSource[e.primarySourceName];if(!t.isCompatibleWith(f))return;null!=f.fullExtent&&(null!=t.fullExtent?t.fullExtent.union(f.fullExtent):t.fullExtent=f.fullExtent.clone()),t.tileInfo&&f.tileInfo&&t.tileInfo.lods.length<f.tileInfo.lods.length&&(t.tileInfo=f.tileInfo)}if(o&&(e.sourceBase=n,e.source=t,e.validatedSource=u,e.primarySourceName=s),e.sourceNameToSource[s]=f,!h(e)&&"defaultStyles"in t&&!e.style){if(null==t.defaultStyles)throw new Error;return"string"==typeof t.defaultStyles?S(e,"",d(n,t.defaultStyles,"root.json"),void 0,l):S(e,"",t.defaultStyles,d(n,"root.json"),l)}}function v(e){const r=512;if(N(e)){const t=e?.tileInfo;return null!=t&&(null==t.rows&&(t.rows=r),null==t.cols&&(t.cols=r)),e}const o={xmin:-20037507.067161843,ymin:-20037507.067161843,xmax:20037507.067161843,ymax:20037507.067161843,spatialReference:{wkid:102100,latestWkid:3857}};let s=null;if(h(e)){const{bounds:r}=e;if(r){const e=y({x:r[0],y:r[1],spatialReference:t(f)}),o=y({x:r[2],y:r[3],spatialReference:t(f)});s={xmin:e.x,ymin:e.y,xmax:o.x,ymax:o.y,spatialReference:t(p)}}}null===s&&(s=o);let l=78271.51696400007,n=295828763.7957775;const i=[],u=e.hasOwnProperty("maxzoom")&&null!=e.maxzoom?+e.maxzoom:22,c=0,a=0;for(let t=0;t<=u;t++)i.push({level:t,scale:n,resolution:l}),l/=2,n/=2;return{capabilities:"TilesOnly",initialExtent:s,fullExtent:o,minScale:c,maxScale:a,tiles:e.tiles,tileInfo:{rows:r,cols:r,dpi:96,format:"pbf",origin:{x:-20037508.342787,y:20037508.342787},lods:i,spatialReference:t(p)}}}function N(e){return e.hasOwnProperty("tileInfo")}export{x as loadMetadata};