UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 8.08 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import{_ as e}from"../../../chunks/tslib.es6.js";import{forEach as t,result as r}from"../../../core/asyncUtils.js";import{makeHandle as i}from"../../../core/handleUtils.js";import s from"../../../core/Logger.js";import{abortMaybe as a}from"../../../core/maybe.js";import{debounce as o,isAbortError as n,onAbort as l,isAborted as h,createAbortError as m,throwIfAborted as d}from"../../../core/promiseUtils.js";import{whenOnce as c}from"../../../core/reactiveUtils.js";import{property as g}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import"../../../core/RandomLCG.js";import{subclass as u}from"../../../core/accessorSupport/decorators/subclass.js";import p from"../../../geometry/Extent.js";import{equals as f,create as y,width as w,height as _,copy as x,intersection as v}from"../../../geometry/support/aaBoundingRect.js";import{DrapeSourceType as R}from"./interfaces.js";import{LayerView3D as b}from"./LayerView3D.js";import{computeImageExportSize as E,createGeometryForExtent as j,createOuterImageGeometry as S}from"./support/overlayImageUtils.js";import{toViewIfLocal as I}from"./support/projectExtentUtils.js";import{debugFlags as A}from"../support/debugFlags.js";import{OverlayIndex as D}from"../terrain/interfaces.js";import{DirtyOperation as M}from"../webgl-engine/lib/ModelDirtyTypes.js";import{RenderGeometry as L}from"../webgl-engine/lib/RenderGeometry.js";import{Texture as C}from"../webgl-engine/lib/Texture.js";import{UpdatePolicy as G}from"../webgl-engine/lib/UpdatePolicy.js";import{ImageMaterial as P}from"../webgl-engine/materials/ImageMaterial.js";import T from"../../layers/LayerView.js";import O from"../../layers/RefreshableLayerView.js";import{isInEffectiveScaleRange as U}from"../../support/layerViewUtils.js";import{TextureWrapMode as H}from"../../webgl/enums.js";let V=class extends(O(b(T))){constructor(){super(...arguments),this.drapeSourceType=R.RasterImage,this.updatePolicy=G.SYNC,this.fullExtentInLocalViewSpatialReference=null,this.maximumDataResolution=null,this._images=new Array,this._extents=new Array,this._overlays=new Array,this.updateWhenStationary=!0,this._drapeSourceRenderer=null,this.refreshDebounced=o((async e=>{this.destroyed||await this._doRefresh(e).catch((e=>{n(e)||s.getLogger(this).error(e)}))}),2e3)}get visibleAtCurrentScale(){const e=this.layer,t="effectiveScaleRange"in e?e.effectiveScaleRange:null;return U(t,this.view.scale)}initialize(){this._drapeSourceRenderer=this.view.basemapTerrain.overlayManager.registerGeometryDrapeSource(this),this.addHandles(i((()=>this.view.basemapTerrain.overlayManager.unregisterDrapeSource(this)))),this.addResolvingPromise(I(this).then((e=>this._set("fullExtentInLocalViewSpatialReference",e)))),this._updatingHandles.add((()=>this.suspended),(()=>this._suspendedChangeHandler()))}destroy(){this.clear()}setDrapingExtent(e,t){this._spatialReference=t,e.forEach(((e,t)=>{this._overlays[t]=e,this._updateImageExtent(e,t)}))}_updateImageExtent(e,t){const r=this._clippedExtent(e.extent,z);if(null==r)return;const i=E(e.extent,r,e.resolution);let a=e.pixelRatio*this.view.state.pixelRatio;const{layer:o}=this;if("imageMaxWidth"in o&&null!=o.imageMaxWidth||"imageMaxHeight"in o&&null!=o.imageMaxHeight){const e=o.imageMaxWidth,t=o.imageMaxHeight;if(i.width>e){const t=e/i.width;i.height=Math.floor(i.height*t),i.width=e,a*=t}if(i.height>t){const e=t/i.height;i.width=Math.floor(i.width*e),i.height=t,a*=e}}const l=this._extents[t];l&&f(l.extent,r)&&this._imageSizeEquals(r,l.imageSize,i)||(this._extents[t]={extent:y(r),imageSize:i,pixelRatio:a},this.suspended||this._fetch(t).catch((e=>{n(e)||s.getLogger(this).error(e)})))}clear(){for(let e=0;e<this._images.length;e++)this._clearImage(e)}async doRefresh(){return this._doRefresh()}async _doRefresh(e){if(this.suspended)return;const t=[];for(let r=0;r<this._extents.length;r++)this._extents[r]&&t.push(this._fetch(r,e));await Promise.allSettled(t)}async processResult(e,t,r){(t instanceof HTMLImageElement||t instanceof HTMLCanvasElement)&&(e.image=t)}findExtentInfoAt(e){for(const t of this._extents){const r=t.extent;if(new p(r[0],r[1],r[2],r[3],this._spatialReference).contains(e))return t}return null}getFetchOptions(){}async redraw(e,r){await t(this._images,(async(t,i)=>{t&&(await e(t,r),await this._createStageObjects(i,t.image,r))}))}_imageSizeEquals(e,t,r){if(!this.maximumDataResolution)return!1;const i=w(e)/this.maximumDataResolution.x,s=_(e)/this.maximumDataResolution.y,a=i/t.width,o=s/t.height,n=i/r.width,l=s/r.height,h=Math.abs(a-n),m=Math.abs(o-l),d=A.TESTS_DISABLE_OPTIMIZATIONS?0:1.5;return h<=d&&m<=d}async _fetch(e,t){if(this.suspended)return;const r=this._extents[e],i=r.extent;this._images[e]||(this._images[e]={texture:null,material:null,renderGeometry:null,loadingPromise:null,loadingAbortController:null,image:null,pixelData:null,renderExtent:y(i)});const o=this._images[e];o.loadingAbortController=a(o.loadingAbortController);const c=new p(i[0],i[1],i[2],i[3],this._spatialReference);if(0===c.width||0===c.height)return void this._clearImage(e);const g=new AbortController;o.loadingAbortController=g,l(t,(()=>g.abort()));const u=g.signal,f=this._waitFetchReady(u).then((async()=>{const t={requestAsImageElement:!0,pixelRatio:this._overlays[e].pixelRatio,...this.getFetchOptions(),signal:u},{height:i,width:s}=r.imageSize;return this.layer.fetchImage(c,s,i,t)})).then((e=>{if(h(u))throw s.getLogger(this).warnOnce("A call to fetchImage resolved even though the request was aborted. fetchImage should not resolve if options.signal.aborted is true."),m();return this.processResult(o,e)})).then((()=>{x(o.renderExtent,i)}));o.loadingPromise=f,await this._updatingHandles.addPromise(f.then((async()=>{d(u),await this._createStageObjects(e,o.image,u)})).catch((e=>{throw e&&!n(e)&&s.getLogger(this).error(e),e})).finally((()=>{f===o.loadingPromise&&(o.loadingPromise=null,o.loadingAbortController=null)})))}_clearImage(e){const t=this._images[e];if(t){null!=t.renderGeometry&&(this._drapeSourceRenderer.removeGeometries([t.renderGeometry],M.UPDATE),t.renderGeometry=null);const e=this.view._stage,r=t.texture;r?.unload(),e.remove(r),t.texture=null,e.remove(t.material),t.material=null,t.loadingAbortController=a(t.loadingAbortController),t.loadingPromise=null,t.image=null,t.pixelData=null}}async _createStageObjects(e,t,i){const s=this.view._stage,a=this._images[e],o=()=>{a.texture?.unload(),s.remove(a.texture),a.texture=null,a.renderGeometry&&(this._drapeSourceRenderer.removeGeometries([a.renderGeometry],M.UPDATE),a.renderGeometry=null)};if(t){const n=new C(t,{width:t.width,height:t.height,preMultiplyAlpha:!0,wrap:{s:H.CLAMP_TO_EDGE,t:H.CLAMP_TO_EDGE}});let l;if(await r(this._images[e===D.INNER?D.OUTER:D.INNER].loadingPromise),d(i),o(),await s.schedule((()=>n.load(s.renderView.renderingContext)),i),s.add(n),a.texture=n,null==a.material?(a.material=new P({textureId:n.id}),s.add(a.material)):a.material.setParameters({textureId:n.id}),e===D.INNER)l=j(a.material,a.renderExtent);else{const e=this._images[0].renderExtent;if(!e)return void o();l=S(a.material,e,a.renderExtent)}a.renderGeometry=new L(l),a.renderGeometry.localOrigin=this._overlays[e].renderLocalOrigin,this._drapeSourceRenderer.addGeometries([a.renderGeometry],M.UPDATE)}else o(),s.remove(a.material),a.material=null}_clippedExtent(e,t){if("local"!==this.view.viewingMode)return x(t,e);const r=this.view.basemapTerrain;return r.ready?v(e,r.extent,t):x(t,e)}_suspendedChangeHandler(){this.suspended?this.clear():this.refreshDebounced()}async _waitFetchReady(e){await c((()=>this.view.stationary),e),d(e)}};e([g()],V.prototype,"layer",void 0),e([g()],V.prototype,"suspended",void 0),e([g({readOnly:!0})],V.prototype,"fullExtentInLocalViewSpatialReference",void 0),e([g({readOnly:!0})],V.prototype,"visibleAtCurrentScale",null),e([g()],V.prototype,"updating",void 0),V=e([u("esri.views.3d.layers.DynamicLayerView3D")],V);const N=V,z=y();export{N as default};