@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 11.6 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{_ as e}from"../chunks/tslib.es6.js";import t from"../request.js";import{isSome as r}from"../core/arrayUtils.js";import i from"../core/Collection.js";import s from"../core/Error.js";import{clone as a}from"../core/lang.js";import{MultiOriginJSONMixin as o}from"../core/MultiOriginJSONSupport.js";import{getDeepValue as l}from"../core/object.js";import{throwIfAbortError as n}from"../core/promiseUtils.js";import{watch as m,on as p,sync as c}from"../core/reactiveUtils.js";import{urlToObject as u,objectToQuery as y}from"../core/urlUtils.js";import{property as d}from"../core/accessorSupport/decorators/property.js";import"../core/has.js";import"../core/Logger.js";import{reader as h}from"../core/accessorSupport/decorators/reader.js";import{subclass as f}from"../core/accessorSupport/decorators/subclass.js";import{writer as g}from"../core/accessorSupport/decorators/writer.js";import v from"../geometry/Extent.js";import w from"./Layer.js";import I from"./WebTileLayer.js";import{BlendLayer as L}from"./mixins/BlendLayer.js";import{OperationalLayer as S}from"./mixins/OperationalLayer.js";import{PortalLayer as M}from"./mixins/PortalLayer.js";import{RefreshableLayer as T}from"./mixins/RefreshableLayer.js";import{ScaleRangeLayer as j}from"./mixins/ScaleRangeLayer.js";import{createTileBitmap as P}from"./support/imageBitmapUtils.js";import b from"./support/TileInfo.js";import x from"./support/TileInfoTilemapCache.js";import{WMTSLayerInfo as E}from"./support/WMTSLayerInfo.js";import _ from"./support/WMTSSublayer.js";import{getTileUrlFromResourceUrls as C,getTileUrlTemplateFromResourceUrls as U,parseResourceInfo as R,parseText as O,validateCapabilities as A,parseCapabilities as W}from"./support/wmtsUtils.js";var F;const V={"image/png":".png","image/png8":".png","image/png24":".png","image/png32":".png","image/jpg":".jpg","image/jpeg":".jpeg","image/gif":".gif","image/bmp":".bmp","image/tiff":".tif","image/jpgpng":"","image/jpegpng":"","image/unknown":""},B=new Set(["version","service","request","layer","style","format","tilematrixset","tilematrix","tilerow","tilecol"]);let $=F=class extends(L(T(j(S(M(o(w))))))){constructor(...e){super(...e),this.activeLayer=null,this.copyright="",this.customParameters=null,this.customLayerParameters=null,this.fullExtent=null,this.operationalLayerType="WebTiledLayer",this.resourceInfo=null,this.serviceMode="RESTful",this.sublayers=null,this.type="wmts",this.version="1.0.0",this.addHandles([m((()=>this.activeLayer),((e,t)=>{t&&!this.sublayers?.includes(t)&&(t.layer=null,t.parent=null),e&&(e.layer=this,e.parent=this)}),c),p((()=>this.sublayers),"after-add",(({item:e})=>{e.layer=this,e.parent=this}),c),p((()=>this.sublayers),"after-remove",(({item:e})=>{e.layer=null,e.parent=null}),c),m((()=>this.sublayers),((e,t)=>{if(t)for(const r of t)r.layer=null,r.parent=null;if(e)for(const r of e)r.layer=this,r.parent=this}),c)])}normalizeCtorArgs(e,t){return"string"==typeof e?{url:e,...t}:e}load(e){return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["WMTS"]},e).catch(n).then((()=>this._fetchService(e))).catch((e=>{throw n(e),new s("wmtslayer:unsupported-service-data","Invalid response from the WMTS service.",{error:e})}))),Promise.resolve(this)}readActiveLayerFromService(e,t,r){this.activeLayer||(this.activeLayer=new _);let i=t.layers.find((e=>e.id===this.activeLayer.id));return i||(i=t.layers[0]),this.activeLayer.read(i,r),this.activeLayer}readActiveLayerFromItemOrWebDoc(e,t){const{templateUrl:r,wmtsInfo:i}=t,s=r?this._getLowerCasedUrlParams(r):null,a=i?.layerIdentifier;let o=null;const l=i?.tileMatrixSet;l&&(Array.isArray(l)?l.length&&(o=l[0]):o=l);const n=s?.format,m=s?.style;return new _({id:a,imageFormat:n,styleId:m,tileMatrixSetId:o})}writeActiveLayer(e,t,r,i){const s=this.activeLayer;t.templateUrl=this.getUrlTemplate(s.id,s.tileMatrixSetId,s.imageFormat,s.styleId);const a=l("tileMatrixSet.tileInfo",s);t.tileInfo=a?a.toJSON(i):null,t.wmtsInfo={...t.wmtsInfo,layerIdentifier:s.id,tileMatrixSet:s.tileMatrixSetId}}readCustomParameters(e,t){const r=t.wmtsInfo;return r?this._mergeParams(r.customParameters,r.url):null}get fullExtents(){return this.activeLayer.fullExtents}readServiceMode(e,t){return t.templateUrl.includes("?")?"KVP":"RESTful"}readSublayersFromService(e,t,r){return K(t.layers,r)}get supportedSpatialReferences(){return this.activeLayer.tileMatrixSets?.map((e=>e.tileInfo?.spatialReference)).toArray().filter(r)??[]}get tilemapCache(){const e=this.activeLayer?.tileMatrixSet?.tileInfo;return e?new x(e):void 0}get title(){return this.activeLayer?.title??"Layer"}set title(e){this._overrideIfSome("title",e)}get url(){return this._get("url")}set url(e){e&&e.endsWith("/")?this._set("url",e.slice(0,-1)):this._set("url",e)}createWebTileLayer(e){const t=this.getUrlTemplate(this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId),r=this._getTileMatrixSetById(e.tileMatrixSetId),i=r?.tileInfo,s=e.fullExtent,a=new E({layerIdentifier:e.id,tileMatrixSet:e.tileMatrixSetId,url:this.url});return this.customLayerParameters&&(a.customLayerParameters=this.customLayerParameters),this.customParameters&&(a.customParameters=this.customParameters),new I({fullExtent:s,urlTemplate:t,tileInfo:i,wmtsInfo:a})}async fetchTile(e,r,i,s={}){const{signal:a}=s,o=this.getTileUrl(e,r,i),{data:l}=await t(o,{responseType:"image",signal:a});return l}async fetchImageBitmapTile(e,r,i,s={}){const{signal:a}=s;if(this.fetchTile!==F.prototype.fetchTile){const t=await this.fetchTile(e,r,i,s);return P(t,e,r,i,a)}const o=this.getTileUrl(e,r,i),{data:l}=await t(o,{responseType:"blob",signal:a});return P(l,e,r,i,a)}findSublayerById(e){return this.sublayers?.find((t=>t.id===e))}getTileUrl(e,t,r){const i=this._getTileMatrixSetById(this.activeLayer.tileMatrixSetId),s=i?.tileInfo?.lods[e],a=s?s.levelValue||`${s.level}`:`${e}`;let o=this.resourceInfo?"":C({dimensionMap:this.dimensionMap,layerMap:this.layerMap},this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId,a,t,r);if(!o){o=this.getUrlTemplate(this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId).replaceAll(/\{level\}/gi,a).replaceAll(/\{row\}/gi,`${t}`).replaceAll(/\{col\}/gi,`${r}`)}return o=this._appendCustomLayerParameters(o),o}getUrlTemplate(e,t,r,i){if(!this.resourceInfo){const r=U({dimensionMap:this.dimensionMap,layerMap:this.layerMap},e,t,i);if(r)return r}if("KVP"===this.serviceMode)return this.url+"?SERVICE=WMTS&VERSION="+this.version+"&REQUEST=GetTile&LAYER="+e+"&STYLE="+i+"&FORMAT="+r+"&TILEMATRIXSET="+t+"&TILEMATRIX={level}&TILEROW={row}&TILECOL={col}";if("RESTful"===this.serviceMode){let s="";return V[r.toLowerCase()]&&(s=V[r.toLowerCase()]),this.url+e+"/"+i+"/"+t+"/{level}/{row}/{col}"+s}return""}async _fetchService(e){if(this.resourceInfo)return"KVP"!==this.resourceInfo.serviceMode||this.url.includes("?")||(this.url+="?"),R(this.resourceInfo),void this.read(this.resourceInfo,{origin:"service"});let t=null;try{const{data:r}=await this._getCapabilities(this.serviceMode,e);t=O(r),A(t)}catch{const r="KVP"===this.serviceMode?"RESTful":"KVP";try{const{data:i}=await this._getCapabilities(r,e);t=O(i),A(t),this.serviceMode=r}catch(o){throw new s("wmtslayer:unsupported-service-data","Services does not support RESTful or KVP service modes.",{error:o})}}const{serviceMode:r,url:i}=this,a=W(t,{serviceMode:r,url:i});this.read(a,{origin:"service"})}async _getCapabilities(e,r){const i=this._getCapabilitiesUrl(e);return await t(i,{...r,responseType:"text"})}_getTileMatrixSetById(e){const t=this.findSublayerById(this.activeLayer.id);return t?.tileMatrixSets?.find((({id:t})=>t===e))}_appendCustomParameters(e){return this._appendParameters(e,this.customParameters)}_appendCustomLayerParameters(e){return this._appendParameters(e,{...a(this.customParameters),...this.customLayerParameters})}_appendParameters(e,t){const r=u(e),i={...r.query,...t},s=y(i);return""===s?r.path:`${r.path}?${s}`}_getCapabilitiesUrl(e){this.url=u(this.url).path;let t=this.url;switch(e){case"KVP":t+=`?request=GetCapabilities&service=WMTS&version=${this.version}`;break;case"RESTful":{const e=`/${this.version}/WMTSCapabilities.xml`,r=new RegExp(e,"i");t=t.replace(r,""),t+=e;break}}return this._appendCustomParameters(t)}_getLowerCasedUrlParams(e){if(!e)return null;const t=u(e).query;if(!t)return null;const r={};return Object.keys(t).forEach((e=>{r[e.toLowerCase()]=t[e]})),r}_mergeParams(e,t){const r=this._getLowerCasedUrlParams(t);if(r){const t=Object.keys(r);t.length&&(e=e?a(e):{},t.forEach((t=>{e.hasOwnProperty(t)||B.has(t)||(e[t]=r[t])})))}return e}};function K(e,t){return e.map((e=>{const r=new _;return r.read(e,t),r}))}e([d()],$.prototype,"dimensionMap",void 0),e([d()],$.prototype,"layerMap",void 0),e([d({type:_,json:{origins:{"web-document":{write:{ignoreOrigin:!0}}}}})],$.prototype,"activeLayer",void 0),e([h("service","activeLayer",["layers"])],$.prototype,"readActiveLayerFromService",null),e([h(["web-document","portal-item"],"activeLayer",["wmtsInfo"])],$.prototype,"readActiveLayerFromItemOrWebDoc",null),e([g(["web-document","portal-item"],"activeLayer",{templateUrl:{type:String},tileInfo:{type:b},"wmtsInfo.layerIdentifier":{type:String},"wmtsInfo.tileMatrixSet":{type:String}})],$.prototype,"writeActiveLayer",null),e([d({type:String,value:"",json:{write:!0}})],$.prototype,"copyright",void 0),e([d({type:["show","hide"]})],$.prototype,"listMode",void 0),e([d({json:{read:!0,write:!0}})],$.prototype,"blendMode",void 0),e([d({json:{origins:{"web-document":{read:{source:["wmtsInfo.customParameters","wmtsInfo.url"]},write:{target:"wmtsInfo.customParameters"}},"portal-item":{read:{source:["wmtsInfo.customParameters","wmtsInfo.url"]},write:{target:"wmtsInfo.customParameters"}}}}})],$.prototype,"customParameters",void 0),e([h(["portal-item","web-document"],"customParameters")],$.prototype,"readCustomParameters",null),e([d({json:{origins:{"web-document":{read:{source:"wmtsInfo.customLayerParameters"},write:{target:"wmtsInfo.customLayerParameters"}},"portal-item":{read:{source:"wmtsInfo.customLayerParameters"},write:{target:"wmtsInfo.customLayerParameters"}}}}})],$.prototype,"customLayerParameters",void 0),e([d({type:v,json:{write:{ignoreOrigin:!0},origins:{"web-document":{read:{source:"fullExtent"}},"portal-item":{read:{source:"fullExtent"}}}}})],$.prototype,"fullExtent",void 0),e([d({readOnly:!0})],$.prototype,"fullExtents",null),e([d({type:["WebTiledLayer"]})],$.prototype,"operationalLayerType",void 0),e([d()],$.prototype,"resourceInfo",void 0),e([d()],$.prototype,"serviceMode",void 0),e([h(["portal-item","web-document"],"serviceMode",["templateUrl"])],$.prototype,"readServiceMode",null),e([d({type:i.ofType(_)})],$.prototype,"sublayers",void 0),e([h("service","sublayers",["layers"])],$.prototype,"readSublayersFromService",null),e([d({readOnly:!0})],$.prototype,"supportedSpatialReferences",null),e([d({readOnly:!0})],$.prototype,"tilemapCache",null),e([d({json:{read:{source:"title"}}})],$.prototype,"title",null),e([d({json:{read:!1},readOnly:!0,value:"wmts"})],$.prototype,"type",void 0),e([d({json:{origins:{service:{read:{source:"tileUrl"}},"web-document":{read:{source:"wmtsInfo.url"},write:{target:"wmtsInfo.url"}},"portal-item":{read:{source:"wmtsInfo.url"},write:{target:"wmtsInfo.url"}}}}})],$.prototype,"url",null),e([d()],$.prototype,"version",void 0),$=F=e([f("esri.layers.WMTSLayer")],$);const k=$;export{k as default};