@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 8.1 kB
JavaScript
import{_ as e}from"../../../../../chunks/tslib.es6.js";import t from"../../../../../core/Accessor.js";import{ByteSizeUnit as s,estimateAttributesObjectSize as i,estimateFixedArraySize as r}from"../../../../../core/byteSizeEstimations.js";import{isNone as o,isSome as n}from"../../../../../core/maybe.js";import{property as l}from"../../../../../core/accessorSupport/decorators/property.js";import"../../../../../core/accessorSupport/ensureType.js";import"../../../../../core/arrayUtils.js";import{subclass as a}from"../../../../../core/accessorSupport/decorators/subclass.js";import{create as c,intersects as h,fromExtent as u,area as d}from"../../../../../geometry/support/aaBoundingRect.js";import{getBoundsOptimizedGeometry as f}from"../../../../../layers/graphics/featureConversionUtils.js";import{BoundsStore as _}from"../../../../../layers/graphics/data/BoundsStore.js";import p from"../../../../../layers/support/TileInfo.js";import{TileKey as m}from"../../../../../layers/support/TileKey.js";import{executeQueryForCount as g}from"../../../../../rest/query/operations/query.js";import{FeatureServiceTileCache as y}from"./FeatureServiceTileCache.js";let v=class extends t{constructor(e){super(e),this.tileInfo=null,this.extent=null,this.maximumByteSize=10*s.MEGABYTES,this._tileBounds=new _,this._tiles=new y,this._refCounts=new Map,this._tileFeatureCounts=new Map,this._tmpBoundingRect=c()}add(e,t){const s=[];for(const i of t)this._referenceFeature(i.objectId)===x.ADDED&&s.push(i);this._addTileStorage(e,new Set(t.map((e=>e.objectId))),C(t)),this.featureStore.addMany(s),this._tiles.applyByteSizeLimit(this.maximumByteSize,(e=>this._removeTileStorage(e)))}destroy(){this.clear(),this._tileFeatureCounts.clear()}clear(){this.featureStore.clear(),this._tileBounds.clear(),this._tiles.clear(),this._refCounts.clear()}refresh(){this.clear(),this._tileFeatureCounts.clear()}processEdits(e,t,s){return this._processEditsDelete(e.deletedFeatures.concat(e.updatedFeatures)),this._processEditsRefetch(e.addedFeatures.concat(e.updatedFeatures),t,s)}_addTileStorage(e,t,s){const i=e.id;this._tiles.set(i,new T(e,t,s)),this._tileBounds.set(i,e.extent),this._tileFeatureCounts.set(i,t.size)}_remove({id:e}){const t=this._tiles.get(e);t&&this._removeTileStorage(t)}_removeTileStorage(e){const t=[];for(const i of e.objectIds)this._unreferenceFeature(i)===x.REMOVED&&t.push(i);this.featureStore.removeManyById(t);const s=e.data.id;this._tiles.delete(s),this._tileBounds.delete(s)}_processEditsDelete(e){this.featureStore.removeManyById(e);for(const[,t]of this._tiles){for(const s of e)t.objectIds.delete(s);this._tileFeatureCounts.set(t.data.id,t.objectIds.size)}for(const t of e)this._refCounts.delete(t)}async _processEditsRefetch(e,t,s){const i=(await t(e,s)).features,{hasZ:r,hasM:n}=this.featureStore;for(const l of i){const e=f(this._tmpBoundingRect,l.geometry,r,n);o(e)||this._tileBounds.forEachInBounds(e,(e=>{const t=this._tiles.get(e);this.featureStore.add(l);const s=l.objectId;t.objectIds.has(s)||(t.objectIds.add(s),this._referenceFeature(s),this._tileFeatureCounts.set(t.data.id,t.objectIds.size))}))}}process(e,t=(()=>!0)){if(o(this.tileInfo)||!e.extent||n(this.extent)&&!h(u(this.extent,this._tmpBoundingRect),e.extent))return new I(e);if(this._tiles.has(e.id))return new I(e);const s=this._createTileTree(e,this.tileInfo);return this._simplify(s,t,null,0,1),this._collectMissingTiles(e,s,this.tileInfo)}get debugInfo(){return Array.from(this._tiles.values()).map((({data:e})=>({data:e,featureCount:this._tileFeatureCounts.get(e.id)||0})))}getFeatureCount(e){return this._tileFeatureCounts.get(e.id)??0}async fetchCount(e,t,s,i){const r=this._tileFeatureCounts.get(e.id);if(null!=r)return r;const o=await g(t,s,i);return this._tileFeatureCounts.set(e.id,o.data.count),o.data.count}_createTileTree(e,t){const s=new F(e.level,e.row,e.col);return t.updateTileInfo(s,p.ExtrapolateOptions.POWER_OF_TWO),this._tileBounds.forEachInBounds(e.extent,(i=>{const r=this._tiles.get(i)?.data;r&&this._tilesAreRelated(e,r)&&this._populateChildren(s,r,t,this._tileFeatureCounts.get(r.id)||0)})),s}_tilesAreRelated(e,t){if(!e||!t)return!1;if(e.level===t.level)return e.row===t.row&&e.col===t.col;const s=e.level<t.level,i=s?e:t,r=s?t:e,o=1<<r.level-i.level;return Math.floor(r.row/o)===i.row&&Math.floor(r.col/o)===i.col}_populateChildren(e,t,s,i){const r=t.level-e.level-1;if(r<0)return void(e.isLeaf=!0);const o=t.row>>r,l=t.col>>r,a=e.row<<1,c=l-(e.col<<1)+(o-a<<1),h=e.children[c];if(n(h))this._populateChildren(h,t,s,i);else{const r=new F(e.level+1,o,l);s.updateTileInfo(r,p.ExtrapolateOptions.POWER_OF_TWO),e.children[c]=r,this._populateChildren(r,t,s,i)}}_simplify(e,t,s,i,r){const o=r*r;if(e.isLeaf)return t(this.getFeatureCount(e),r)?0:(this._remove(e),n(s)&&(s.children[i]=null),o);const l=r/2,a=l*l;let c=0;for(let h=0;h<e.children.length;h++){const s=e.children[h];c+=n(s)?this._simplify(s,t,e,h,l):a}return 0===c?this._mergeChildren(e):1-c/o<w&&(this._purge(e),n(s)&&(s.children[i]=null),c=o),c}_mergeChildren(e){const t=new Set;let s=0;this._forEachLeaf(e,(e=>{const i=this._tiles.get(e.id);if(i){s+=i.byteSize;for(const e of i.objectIds)t.has(e)||(t.add(e),this._referenceFeature(e));this._remove(e)}})),this._addTileStorage(e,t,s),e.isLeaf=!0,e.children[0]=e.children[1]=e.children[2]=e.children[3]=null,this._tileFeatureCounts.set(e.id,t.size)}_forEachLeaf(e,t){for(const s of e.children)o(s)||(s.isLeaf?t(s):this._forEachLeaf(s,t))}_purge(e){if(!o(e))if(e.isLeaf)this._remove(e);else for(let t=0;t<e.children.length;t++){const s=e.children[t];this._purge(s),e.children[t]=null}}_collectMissingTiles(e,t,s){const i=new j(s,e,this.extent);return this._collectMissingTilesRecurse(t,i,1),i.info}_collectMissingTilesRecurse(e,t,s){if(e.isLeaf)return;if(!e.hasChildren)return void t.addMissing(e.level,e.row,e.col,s);const i=s/2;for(let r=0;r<e.children.length;r++){const s=e.children[r];o(s)?t.addMissing(e.level+1,(e.row<<1)+((2&r)>>1),(e.col<<1)+(1&r),i):this._collectMissingTilesRecurse(s,t,i)}}_referenceFeature(e){const t=(this._refCounts.get(e)||0)+1;return this._refCounts.set(e,t),1===t?x.ADDED:x.UNCHANGED}_unreferenceFeature(e){const t=(this._refCounts.get(e)||0)-1;return 0===t?(this._refCounts.delete(e),x.REMOVED):(t>0&&this._refCounts.set(e,t),x.UNCHANGED)}get test(){return{tiles:Array.from(this._tiles.values()).map((e=>`${e.data.id}:[${Array.from(e.objectIds)}]`)),featureReferences:Array.from(this._refCounts.keys()).map((e=>`${e}:${this._refCounts.get(e)}`))}}};function C(e){return e.reduce(((e,t)=>e+E(t)),0)}function E(e){return 32+S(e.geometry)+i(e.attributes)}function S(e){if(o(e))return 0;const t=r(e.lengths,4);return 32+r(e.coords,8)+t}e([l({constructOnly:!0})],v.prototype,"featureStore",void 0),e([l()],v.prototype,"tileInfo",void 0),e([l()],v.prototype,"extent",void 0),e([l()],v.prototype,"maximumByteSize",void 0),v=e([a("esri.views.interactive.snapping.featureSources.featureServiceSource.FeatureServiceTileStore")],v);class T{constructor(e,t,s){this.data=e,this.objectIds=t,this.byteSize=s}}class F{constructor(e,t,s){this.level=e,this.row=t,this.col=s,this.isLeaf=!1,this.extent=null,this.children=[null,null,null,null]}get hasChildren(){return!this.isLeaf&&(n(this.children[0])||n(this.children[1])||n(this.children[2])||n(this.children[3]))}}class I{constructor(e,t=[]){this.missingTiles=t,this.fullArea=0,this.coveredArea=0,this.fullArea=d(e.extent),this.coveredArea=this.fullArea}prepend(e){this.missingTiles=e.missingTiles.concat(this.missingTiles),this.coveredArea+=e.coveredArea,this.fullArea+=e.fullArea}}class j{constructor(e,t,s){this._tileInfo=e,this._extent=null,this.info=new I(t),n(s)&&(this._extent=u(s))}addMissing(e,t,s,i){const r=new m(null,e,t,s);this._tileInfo.updateTileInfo(r,p.ExtrapolateOptions.POWER_OF_TWO),o(r.extent)||n(this._extent)&&!h(this._extent,r.extent)||(this.info.missingTiles.push({data:r,resolution:i}),this.info.coveredArea-=d(r.extent))}}const w=.18751;var x;!function(e){e[e.ADDED=0]="ADDED",e[e.REMOVED=1]="REMOVED",e[e.UNCHANGED=2]="UNCHANGED"}(x||(x={}));export{v as FeatureServiceTileStore,I as ProcessResult};