UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 11.4 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";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{MultiOriginJSONSupportMixin as o}from"../core/MultiOriginJSONSupport.js";import{throwIfAbortError as l}from"../core/promiseUtils.js";import{watch as n,sync as m,on as p}from"../core/reactiveUtils.js";import{urlToObject as c,objectToQuery as u}from"../core/urlUtils.js";import{property as y,subclass as d}from"../core/accessorSupport/decorators.js";import h from"../geometry/Extent.js";import f from"./Layer.js";import v from"./WebTileLayer.js";import{BlendLayer as g}from"./mixins/BlendLayer.js";import{OperationalLayer as w}from"./mixins/OperationalLayer.js";import{PortalLayer as I}from"./mixins/PortalLayer.js";import{RefreshableLayer as L}from"./mixins/RefreshableLayer.js";import{ScaleRangeLayer as S}from"./mixins/ScaleRangeLayer.js";import{createTileBitmap as M}from"./support/imageBitmapUtils.js";import T from"./support/TileInfo.js";import P from"./support/TileInfoTilemapCache.js";import{WMTSLayerInfo as b}from"./support/WMTSLayerInfo.js";import j from"./support/WMTSSublayer.js";import{getTileUrlFromResourceUrls as x,getTileUrlTemplateFromResourceUrls as E,parseResourceInfo as _,parseText as C,validateCapabilities as U,parseCapabilities as R}from"./support/wmtsUtils.js";import{reader as O}from"../core/accessorSupport/decorators/reader.js";import{writer as A}from"../core/accessorSupport/decorators/writer.js";var W;const F={"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":""},V=new Set(["version","service","request","layer","style","format","tilematrixset","tilematrix","tilerow","tilecol"]);let B=W=class extends(g(L(S(w(I(o(f))))))){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([n(()=>this.activeLayer,(e,t)=>{t&&!this.sublayers?.includes(t)&&(t.layer=null,t.parent=null),e&&(e.layer=this,e.parent=this)},m),p(()=>this.sublayers,"after-add",({item:e})=>{e.layer=this,e.parent=this},m),p(()=>this.sublayers,"after-remove",({item:e})=>{e.layer=null,e.parent=null},m),n(()=>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},m)])}normalizeCtorArgs(e,t){return"string"==typeof e?{url:e,...t}:e}load(e){return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["WMTS"]},e).catch(l).then(()=>this._fetchService(e)).catch(e=>{throw l(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 j);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 j({id:a,imageFormat:n,styleId:m,tileMatrixSetId:o})}writeActiveLayer(e,t,r,i){const{activeLayer:s,loaded:a}=this,{id:o,tileMatrixSet:l,tileMatrixSetId:n,imageFormat:m,styleId:p}=s;t.templateUrl=a?this.getUrlTemplate(o,n,m,p):void 0,t.tileInfo=a?l?.tileInfo?.toJSON(i)??null:void 0,t.wmtsInfo={...t.wmtsInfo,layerIdentifier:o,tileMatrixSet:n}}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 $(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 P(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 b({layerIdentifier:e.id,tileMatrixSet:e.tileMatrixSetId,url:this.url});return this.customLayerParameters&&(a.customLayerParameters=this.customLayerParameters),this.customParameters&&(a.customParameters=this.customParameters),new v({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!==W.prototype.fetchTile){const t=await this.fetchTile(e,r,i,s);return M(t,e,r,i,a)}const o=this.getTileUrl(e,r,i),{data:l}=await t(o,{responseType:"blob",signal:a});return M(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?"":x({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=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="";const a=r.toLowerCase();return F[a]&&(s=F[a]),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+="?"),_(this.resourceInfo),void this.read(this.resourceInfo,{origin:"service"});let t=null;try{const{data:r}=await this._getCapabilities(this.serviceMode,e);t=C(r),U(t)}catch{const r="KVP"===this.serviceMode?"RESTful":"KVP";try{const{data:i}=await this._getCapabilities(r,e);t=C(i),U(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=R(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=c(e),i={...r.query,...t},s=u(i);return""===s?r.path:`${r.path}?${s}`}_getCapabilitiesUrl(e){this.url=c(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=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)||V.has(t)||(e[t]=r[t])}))}return e}};function $(e,t){return e.map(e=>{const r=new j;return r.read(e,t),r})}e([y()],B.prototype,"dimensionMap",void 0),e([y()],B.prototype,"layerMap",void 0),e([y({type:j,json:{origins:{"web-document":{write:{ignoreOrigin:!0}}}}})],B.prototype,"activeLayer",void 0),e([O("service","activeLayer",["layers"])],B.prototype,"readActiveLayerFromService",null),e([O(["web-document","portal-item"],"activeLayer",["wmtsInfo"])],B.prototype,"readActiveLayerFromItemOrWebDoc",null),e([A(["web-document","portal-item"],"activeLayer",{templateUrl:{type:String},tileInfo:{type:T},"wmtsInfo.layerIdentifier":{type:String},"wmtsInfo.tileMatrixSet":{type:String}})],B.prototype,"writeActiveLayer",null),e([y({type:String,value:"",json:{write:!0}})],B.prototype,"copyright",void 0),e([y({type:["show","hide"]})],B.prototype,"listMode",void 0),e([y({json:{read:!0,write:!0}})],B.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"}}}}})],B.prototype,"customParameters",void 0),e([O(["portal-item","web-document"],"customParameters")],B.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"}}}}})],B.prototype,"customLayerParameters",void 0),e([y({type:h,json:{write:{ignoreOrigin:!0},origins:{"web-document":{read:{source:"fullExtent"}},"portal-item":{read:{source:"fullExtent"}}}}})],B.prototype,"fullExtent",void 0),e([y({readOnly:!0})],B.prototype,"fullExtents",null),e([y({type:["WebTiledLayer"]})],B.prototype,"operationalLayerType",void 0),e([y()],B.prototype,"resourceInfo",void 0),e([y()],B.prototype,"serviceMode",void 0),e([O(["portal-item","web-document"],"serviceMode",["templateUrl"])],B.prototype,"readServiceMode",null),e([y({type:i.ofType(j)})],B.prototype,"sublayers",void 0),e([O("service","sublayers",["layers"])],B.prototype,"readSublayersFromService",null),e([y({readOnly:!0})],B.prototype,"supportedSpatialReferences",null),e([y({readOnly:!0})],B.prototype,"tilemapCache",null),e([y({json:{read:{source:"title"}}})],B.prototype,"title",null),e([y({json:{read:!1},readOnly:!0,value:"wmts"})],B.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"}}}}})],B.prototype,"url",null),e([y()],B.prototype,"version",void 0),B=W=e([d("esri.layers.WMTSLayer")],B);const K=B;export{K as default};