WMTSLayer.js 11 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{_ as e}from"../chunks/tslib.es6.js";import t from"../request.js";import r from"../core/Collection.js";import i from"../core/Error.js";import s from"../core/Handles.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,sync as p}from"../core/reactiveUtils.js";import{urlToObject as c,objectToQuery as u}from"../core/urlUtils.js";import{property as y}from"../core/accessorSupport/decorators/property.js";import"../core/accessorSupport/ensureType.js";import{reader as d}from"../core/accessorSupport/decorators/reader.js";import{subclass as h}from"../core/accessorSupport/decorators/subclass.js";import{writer as f}from"../core/accessorSupport/decorators/writer.js";import v from"../geometry/Extent.js";import g from"./Layer.js";import w from"./WebTileLayer.js";import{BlendLayer as L}from"./mixins/BlendLayer.js";import{OperationalLayer as I}from"./mixins/OperationalLayer.js";import{PortalLayer as S}from"./mixins/PortalLayer.js";import{RefreshableLayer as M}from"./mixins/RefreshableLayer.js";import{ScaleRangeLayer as T}from"./mixins/ScaleRangeLayer.js";import P from"./support/TileInfo.js";import b from"./support/TileInfoTilemapCache.js";import{WMTSLayerInfo as j}from"./support/WMTSLayerInfo.js";import x from"./support/WMTSSublayer.js";import{getTileUrlFromResourceUrls as _,getTileUrlTemplateFromResourceUrls as E,validateCapabilities as C,parseCapabilities as U,parseResourceInfo as R}from"./support/wmtsUtils.js";const O={"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":""},W=new Set(["version","service","request","layer","style","format","tilematrixset","tilematrix","tilerow","tilecol"]);let A=class extends(L(M(T(I(S(o(g))))))){constructor(...e){super(...e),this._sublayersHandles=new s,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.own([m((()=>this.activeLayer),((e,t)=>{t&&(t.layer=null),e&&(e.layer=this)}),p),m((()=>this.sublayers),((e,t)=>{t&&(t.forEach((e=>{e.layer=null})),this._sublayersHandles.removeAll(),this._sublayersHandles=null),e&&(e.forEach((e=>{e.layer=this})),this._sublayersHandles||(this._sublayersHandles=new s),this._sublayersHandles.add([e.on("after-add",(({item:e})=>{e.layer=this})),e.on("after-remove",(({item:e})=>{e.layer=null}))]))}),p)])}normalizeCtorArgs(e,t){return"string"==typeof e?{url:e,...t}:e}load(e){if("KVP"===this.serviceMode||"RESTful"===this.serviceMode)return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["WMTS"]},e).catch(n).then((()=>this._fetchService(e))).catch((e=>{throw n(e),new i("wmtslayer:unsupported-service-data","Invalid response from the WMTS service.",{error:e})}))),Promise.resolve(this);console.error("WMTS mode could only be 'KVP' or 'RESTful'")}get activeLayer(){return this._get("activeLayer")}set activeLayer(e){this._set("activeLayer",e)}readActiveLayerFromService(e,t,r){this.activeLayer||(this.activeLayer=new x);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 x({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 F(t.layers,r)}get supportedSpatialReferences(){return this.activeLayer.tileMatrixSets.map((e=>e.tileInfo.spatialReference)).toArray()}get tilemapCache(){const e=this.activeLayer?.tileMatrixSet?.tileInfo;return new b(e)}get title(){return this.activeLayer?.title??"Layer"}set title(e){e?this._override("title",e):this._clearOverride("title")}get url(){return this._get("url")}set url(e){e&&"/"===e.substr(-1)?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).tileInfo,i=e.fullExtent,s=new j({layerIdentifier:e.id,tileMatrixSet:e.tileMatrixSetId,url:this.url});return this.customLayerParameters&&(s.customLayerParameters=this.customLayerParameters),this.customParameters&&(s.customParameters=this.customParameters),new w({fullExtent:i,urlTemplate:t,tileInfo:r,wmtsInfo:s})}fetchTile(e,r,i){const s=this.getTileUrl(e,r,i);return t(s,{responseType:"image"}).then((e=>e.data))}findSublayerById(e){return this.sublayers.find((t=>t.id===e))}getTileUrl(e,t,r){const i=this._getTileMatrixSetById(this.activeLayer.tileMatrixSetId).tileInfo.lods[e],s=i?i.levelValue?i.levelValue:`${i.level}`:`${e}`;let a=this.resourceInfo?"":_({dimensionMap:this.dimensionMap,layerMap:this.layerMap},this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId,s,t,r);if(!a){a=this.getUrlTemplate(this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId).replace(/\{level\}/gi,s).replace(/\{row\}/gi,`${t}`).replace(/\{col\}/gi,`${r}`)}return a=this._appendCustomLayerParameters(a),a}getUrlTemplate(e,t,r,i){if(!this.resourceInfo){const r=E({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 O[r.toLowerCase()]&&(s=O[r.toLowerCase()]),this.url+e+"/"+i+"/"+t+"/{level}/{row}/{col}"+s}return""}async _fetchService(e){let t;if(this.resourceInfo)"KVP"===this.resourceInfo.serviceMode&&(this.url+=this.url.includes("?")?"":"?"),t={ssl:!1,data:this.resourceInfo};else try{t=await this._getCapabilities(this.serviceMode,e),C(t.data)}catch{const s="KVP"===this.serviceMode?"RESTful":"KVP";try{t=await this._getCapabilities(s,e),C(t.data),this.serviceMode=s}catch(r){throw new i("wmtslayer:unsupported-service-data","Services does not support RESTful or KVP service modes.",{error:r})}}this.resourceInfo?t.data=R(t.data):t.data=U(t.data,{serviceMode:this.serviceMode,url:this.url}),t.data&&this.read(t.data,{origin:"service"})}async _getCapabilities(e,r){const i=this._getCapabilitiesUrl(e);return await t(i,{...r,responseType:"text"})}_getTileMatrixSetById(e){return this.findSublayerById(this.activeLayer.id).tileMatrixSets.find((t=>t.id===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=c(e),i={...r.query,...t},s=u(i);return""===s?r.path:`${r.path}?${s}`}_getCapabilitiesUrl(e){this.url=this.url.split("?")[0];const t="KVP"===e?`${this.url}?request=GetCapabilities&service=WMTS&version=${this.version}`:`${this.url}/${this.version}/WMTSCapabilities.xml`;return this._appendCustomParameters(t)}_getLowerCasedUrlParams(e){if(!e)return null;const t=c(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)||W.has(t)||(e[t]=r[t])})))}return e}};function F(e,t){return e.map((e=>{const r=new x;return r.read(e,t),r}))}e([y()],A.prototype,"dimensionMap",void 0),e([y()],A.prototype,"layerMap",void 0),e([y({type:x,json:{origins:{"web-document":{write:{ignoreOrigin:!0}}}}})],A.prototype,"activeLayer",null),e([d("service","activeLayer",["layers"])],A.prototype,"readActiveLayerFromService",null),e([d(["web-document","portal-item"],"activeLayer",["wmtsInfo"])],A.prototype,"readActiveLayerFromItemOrWebDoc",null),e([f(["web-document","portal-item"],"activeLayer",{templateUrl:{type:String},tileInfo:{type:P},"wmtsInfo.layerIdentifier":{type:String},"wmtsInfo.tileMatrixSet":{type:String}})],A.prototype,"writeActiveLayer",null),e([y({type:String,value:"",json:{write:!0}})],A.prototype,"copyright",void 0),e([y({type:["show","hide"]})],A.prototype,"listMode",void 0),e([y({json:{read:!0,write:!0}})],A.prototype,"blendMode",void 0),e([y({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"}}}}})],A.prototype,"customParameters",void 0),e([d(["portal-item","web-document"],"customParameters")],A.prototype,"readCustomParameters",null),e([y({json:{origins:{"web-document":{read:{source:"wmtsInfo.customLayerParameters"},write:{target:"wmtsInfo.customLayerParameters"}},"portal-item":{read:{source:"wmtsInfo.customLayerParameters"},write:{target:"wmtsInfo.customLayerParameters"}}}}})],A.prototype,"customLayerParameters",void 0),e([y({type:v,json:{write:{ignoreOrigin:!0},origins:{"web-document":{read:{source:"fullExtent"}},"portal-item":{read:{source:"fullExtent"}}}}})],A.prototype,"fullExtent",void 0),e([y({readOnly:!0})],A.prototype,"fullExtents",null),e([y({type:["WebTiledLayer"]})],A.prototype,"operationalLayerType",void 0),e([y()],A.prototype,"resourceInfo",void 0),e([y()],A.prototype,"serviceMode",void 0),e([d(["portal-item","web-document"],"serviceMode",["templateUrl"])],A.prototype,"readServiceMode",null),e([y({type:r.ofType(x)})],A.prototype,"sublayers",void 0),e([d("service","sublayers",["layers"])],A.prototype,"readSublayersFromService",null),e([y({readOnly:!0})],A.prototype,"supportedSpatialReferences",null),e([y({readOnly:!0})],A.prototype,"tilemapCache",null),e([y({json:{read:{source:"title"}}})],A.prototype,"title",null),e([y({json:{read:!1},readOnly:!0,value:"wmts"})],A.prototype,"type",void 0),e([y({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"}}}}})],A.prototype,"url",null),e([y()],A.prototype,"version",void 0),A=e([h("esri.layers.WMTSLayer")],A);const V=A;export{V as default};