TileLayer.js 7.3 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{_ as e}from"../chunks/tslib.es6.js";import r from"../request.js";import t from"../core/Error.js";import{HandleOwnerMixin as s}from"../core/HandleOwner.js";import{isSome as i}from"../core/maybe.js";import{MultiOriginJSONMixin as o}from"../core/MultiOriginJSONSupport.js";import{throwIfAbortError as a}from"../core/promiseUtils.js";import{urlToObject as l,objectToQuery as n,makeAbsolute as p}from"../core/urlUtils.js";import{property as c}from"../core/accessorSupport/decorators/property.js";import"../core/arrayUtils.js";import{cast as u}from"../core/accessorSupport/decorators/cast.js";import{reader as m}from"../core/accessorSupport/decorators/reader.js";import{subclass as h}from"../core/accessorSupport/decorators/subclass.js";import{writer as d}from"../core/accessorSupport/decorators/writer.js";import y from"../geometry/SpatialReference.js";import f from"./Layer.js";import{APIKeyMixin as v}from"./mixins/APIKeyMixin.js";import{ArcGISCachedService as g}from"./mixins/ArcGISCachedService.js";import{ArcGISMapService as S}from"./mixins/ArcGISMapService.js";import{ArcGISService as _}from"./mixins/ArcGISService.js";import{BlendLayer as j}from"./mixins/BlendLayer.js";import{CustomParametersMixin as b}from"./mixins/CustomParametersMixin.js";import{OperationalLayer as T}from"./mixins/OperationalLayer.js";import{PortalLayer as w}from"./mixins/PortalLayer.js";import{RefreshableLayer as O}from"./mixins/RefreshableLayer.js";import{ScaleRangeLayer as L}from"./mixins/ScaleRangeLayer.js";import{SublayersOwner as R}from"./mixins/SublayersOwner.js";import{parse as U,isHostedAgolService as P,isArcGISUrl as x}from"./support/arcgisLayerUrl.js";import{url as I}from"./support/commonProperties.js";import A from"./support/Sublayer.js";const M=["Canvas/World_Dark_Gray_Base","Canvas/World_Dark_Gray_Reference","Canvas/World_Light_Gray_Base","Canvas/World_Light_Gray_Reference","Elevation/World_Hillshade","Elevation/World_Hillshade_Dark","Ocean/World_Ocean_Base","Ocean/World_Ocean_Reference","Ocean_Basemap","Reference/World_Boundaries_and_Places","Reference/World_Boundaries_and_Places_Alternate","Reference/World_Transportation","World_Imagery","World_Street_Map","World_Topo_Map"];let W=class extends(j(R(L(T(w(g(S(_(o(s(O(v(b(f)))))))))))))){constructor(...e){super(...e),this.listMode="show",this.isReference=null,this.operationalLayerType="ArcGISTiledMapServiceLayer",this.resampling=!0,this.sourceJSON=null,this.spatialReference=null,this.path=null,this.sublayers=null,this.type="tile",this.url=null}normalizeCtorArgs(e,r){return"string"==typeof e?{url:e,...r}:e}load(e){const r=i(e)?e.signal:null;return this.addResolvingPromise(this.loadFromPortal({supportedTypes:["Map Service"]},e).catch(a).then((()=>this._fetchService(r)))),Promise.resolve(this)}get attributionDataUrl(){const e=this.parsedUrl?.path.toLowerCase();return e?this._getDefaultAttribution(this._getMapName(e)):null}readSpatialReference(e,r){return(e=e||r.tileInfo&&r.tileInfo.spatialReference)&&y.fromJSON(e)}writeSublayers(e,r,t,s){if(!this.loaded||!e)return;const i=e.slice().reverse().flatten((({sublayers:e})=>e&&e.toArray().reverse())).toArray(),o=[],a={writeSublayerStructure:!1,...s};i.forEach((e=>{const r=e.write({},a);o.push(r)}));o.some((e=>Object.keys(e).length>1))&&(r.layers=o)}get tileServers(){return this._getDefaultTileServers(this.parsedUrl.path)}castTileServers(e){return Array.isArray(e)?e.map((e=>l(e).path)):null}fetchTile(e,t,s,i={}){const{signal:o}=i,a=this.getTileUrl(e,t,s),l={responseType:"image",signal:o,query:{...this.refreshParameters}};return r(a,l).then((e=>e.data))}async fetchImageBitmapTile(e,t,s,i={}){const{signal:o}=i,a=this.getTileUrl(e,t,s),l={responseType:"blob",signal:o,query:{...this.refreshParameters}},n=await r(a,l);return createImageBitmap(n.data)}getTileUrl(e,r,t){const s=!this.tilemapCache&&this.supportsBlankTile,i=n({...this.parsedUrl.query,blankTile:!s&&null,...this.customParameters,token:this.apiKey}),o=this.tileServers;return`${o&&o.length?o[r%o.length]:this.parsedUrl.path}/tile/${e}/${r}/${t}${i?"?"+i:""}`}_fetchService(e){return new Promise(((s,o)=>{if(this.sourceJSON){if(null!=this.sourceJSON.bandCount&&null!=this.sourceJSON.pixelSizeX)throw new t("tile-layer:unsupported-url","use ImageryTileLayer to open a tiled image service");return void s({data:this.sourceJSON})}if(!this.parsedUrl)throw new t("tile-layer:undefined-url","layer's url is not defined");const a=U(this.parsedUrl.path);if(i(a)&&"ImageServer"===a.serverType)throw new t("tile-layer:unsupported-url","use ImageryTileLayer to open a tiled image service");r(this.parsedUrl.path,{query:{f:"json",...this.parsedUrl.query,...this.customParameters,token:this.apiKey},responseType:"json",signal:e}).then(s,o)})).then((r=>{if(r.ssl&&(this.url=this.url.replace(/^http:/i,"https:")),this.sourceJSON=r.data,this.read(r.data,{origin:"service",url:this.parsedUrl}),10.1===this.version&&!P(this.url))return this._fetchServerVersion(this.url,e).then((e=>{this.read({currentVersion:e})})).catch((()=>{}))}))}_fetchServerVersion(e,s){if(!x(e))return Promise.reject();const i=e.replace(/(.*\/rest)\/.*/i,"$1")+"/info";return r(i,{query:{f:"json",...this.customParameters,token:this.apiKey},responseType:"json",signal:s}).then((e=>{if(e.data&&e.data.currentVersion)return e.data.currentVersion;throw new t("tile-layer:version-not-available")}))}_getMapName(e){const r=e.match(/^(?:https?:)?\/\/(server\.arcgisonline\.com|services\.arcgisonline\.com|ibasemaps-api\.arcgis\.com)\/arcgis\/rest\/services\/([^\/]+(\/[^\/]+)*)\/mapserver/i);return r&&r[2]}_getDefaultAttribution(e){if(!e)return;let r;e=e.toLowerCase();for(let t=0,s=M.length;t<s;t++)if(r=M[t],r.toLowerCase().includes(e))return p("//static.arcgis.com/attribution/"+r)}_getDefaultTileServers(e){const r=-1!==e.search(/^(?:https?:)?\/\/server\.arcgisonline\.com/i),t=-1!==e.search(/^(?:https?:)?\/\/services\.arcgisonline\.com/i);return r||t?[e,e.replace(r?/server\.arcgisonline/i:/services\.arcgisonline/i,r?"services.arcgisonline":"server.arcgisonline")]:[]}get hasOverriddenFetchTile(){return!this.fetchTile.__isDefault__}};e([c({readOnly:!0})],W.prototype,"attributionDataUrl",null),e([c({type:["show","hide","hide-children"]})],W.prototype,"listMode",void 0),e([c({json:{read:!0,write:!0}})],W.prototype,"blendMode",void 0),e([c({type:Boolean,json:{read:!1,write:{enabled:!0,overridePolicy:()=>({enabled:!1})}}})],W.prototype,"isReference",void 0),e([c({readOnly:!0,type:["ArcGISTiledMapServiceLayer"]})],W.prototype,"operationalLayerType",void 0),e([c({type:Boolean})],W.prototype,"resampling",void 0),e([c()],W.prototype,"sourceJSON",void 0),e([c({type:y})],W.prototype,"spatialReference",void 0),e([m("spatialReference",["spatialReference","tileInfo"])],W.prototype,"readSpatialReference",null),e([c({type:String,json:{origins:{"web-scene":{read:!0,write:!0}},read:!1}})],W.prototype,"path",void 0),e([c({readOnly:!0})],W.prototype,"sublayers",void 0),e([d("sublayers",{layers:{type:[A]}})],W.prototype,"writeSublayers",null),e([c({json:{read:!1,write:!1}})],W.prototype,"popupEnabled",void 0),e([c()],W.prototype,"tileServers",null),e([u("tileServers")],W.prototype,"castTileServers",null),e([c({readOnly:!0,json:{read:!1}})],W.prototype,"type",void 0),e([c(I)],W.prototype,"url",void 0),W=e([h("esri.layers.TileLayer")],W),W.prototype.fetchTile.__isDefault__=!0;const B=W;export{B as default};