UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 10.5 kB
import{_ as e}from"../../../../../chunks/tslib.es6.js";import{equals as t}from"../../../../../core/arrayUtils.js";import{AsyncSequence as i}from"../../../../../core/AsyncSequence.js";import{createTask as s}from"../../../../../core/asyncUtils.js";import{HandleOwner as r}from"../../../../../core/HandleOwner.js";import{makeHandle as o}from"../../../../../core/handleUtils.js";import n from"../../../../../core/Logger.js";import{isSome as a,isNone as l,unwrap as u}from"../../../../../core/maybe.js";import{throwIfAbortError as c}from"../../../../../core/promiseUtils.js";import{sync as d}from"../../../../../core/reactiveUtils.js";import{property as h}from"../../../../../core/accessorSupport/decorators/property.js";import"../../../../../core/accessorSupport/ensureType.js";import{subclass as p}from"../../../../../core/accessorSupport/decorators/subclass.js";import f from"../../../../../geometry/Extent.js";import{toExtent as y}from"../../../../../geometry/support/aaBoundingRect.js";import{unquantizeOptimizedFeatureSet as g,convertFromFeatureSet as m}from"../../../../../layers/graphics/featureConversionUtils.js";import{isHostedAgolService as _}from"../../../../../layers/support/arcgisLayerUrl.js";import{OptimizedFeatureSetParserContext as b}from"../../../../../rest/query/operations/pbfOptimizedFeatureSet.js";import{executeQueryForExtent as F,executeQueryPBF as T,executeQuery as P}from"../../../../../rest/query/operations/query.js";import I from"../../../../../rest/support/Query.js";import{StateType as v,PendingFeatureTile as O}from"./PendingFeatureTile.js";let C=class extends r{get _minimumVerticesPerFeature(){switch(this.store?.featureStore.geometryType){case"esriGeometryPoint":case"esriGeometryMultipoint":return 1;case"esriGeometryPolygon":return 4;case"esriGeometryPolyline":return 2}}set filter(e){const t=this._get("filter"),i=this._filterProperties(e);JSON.stringify(t)!==JSON.stringify(i)&&this._set("filter",i)}set customParameters(e){const t=this._get("customParameters");JSON.stringify(t)!==JSON.stringify(e)&&this._set("customParameters",e)}get _configuration(){return{filter:this.filter,customParameters:this.customParameters,tileInfo:this.tileInfo,tileSize:this.tileSize}}set tileInfo(e){const t=this._get("tileInfo");t!==e&&(a(e)&&a(t)&&JSON.stringify(e)===JSON.stringify(t)||(this._set("tileInfo",e),this.store.tileInfo=e))}set tileSize(e){this._get("tileSize")!==e&&this._set("tileSize",e)}get updating(){return this.updatingExcludingEdits||this._pendingEdits.updating}get updatingExcludingEdits(){return this.updatingHandles.updating}get hasZ(){return this.store.featureStore.hasZ}constructor(e){super(e),this.tilesOfInterest=[],this.availability=0,this._pendingTiles=new Map,this._pendingEdits=new i,this._pendingEditsAbortController=new AbortController}initialize(){this._initializeFetchExtent(),this.updatingHandles.add((()=>this._configuration),(()=>this.refresh())),this.updatingHandles.add((()=>this.tilesOfInterest),((e,i)=>{t(e,i,(({id:e},{id:t})=>e===t))||this._process()}),d)}destroy(){this._pendingTiles.forEach((e=>this._deletePendingTile(e))),this._pendingTiles.clear(),this.store.destroy(),this.tilesOfInterest.length=0,this._pendingEditsAbortController.abort(),this._pendingEditsAbortController=null}refresh(){this.store.refresh(),this._pendingTiles.forEach((e=>this._deletePendingTile(e))),this._process()}applyEdits(e){this._pendingEdits.push(e,(async e=>{if(0===e.addedFeatures.length&&0===e.updatedFeatures.length&&0===e.deletedFeatures.length)return;for(const[,i]of this._pendingTiles)i.reset();const t={...e,deletedFeatures:e.deletedFeatures.map((({objectId:e,globalId:t})=>e&&-1!==e?e:this._lookupObjectIdByGlobalId(t)))};await this.updatingHandles.addPromise(this.store.processEdits(t,((e,t)=>this._queryFeaturesById(e,t)),this._pendingEditsAbortController.signal)),this._processPendingTiles()}))}_initializeFetchExtent(){if(!this.capabilities.query.supportsExtent||!_(this.url))return;const e=s((async e=>{try{const t=await F(this.url,new I({where:"1=1",outSpatialReference:this.spatialReference,cacheHint:!!this.capabilities.query.supportsCacheHint||void 0}),{query:this._configuration.customParameters,signal:e});this.store.extent=f.fromJSON(t.data?.extent)}catch(t){c(t),n.getLogger(this.declaredClass).warn("Failed to fetch data extent",t)}}));this.updatingHandles.addPromise(e.promise.then((()=>this._process()))),this.handles.add(o((()=>e.abort())))}get debugInfo(){return{numberOfFeatures:this.store.featureStore.numFeatures,tilesOfInterest:this.tilesOfInterest,pendingTiles:Array.from(this._pendingTiles.values()).map((e=>e.debugInfo)),storedTiles:this.store.debugInfo}}_process(){this._markTilesNotAlive(),this._createPendingTiles(),this._deletePendingTiles(),this._processPendingTiles()}_markTilesNotAlive(){for(const[,e]of this._pendingTiles)e.alive=!1}_createPendingTiles(){const e=this._collectMissingTilesInfo();if(this._setAvailability(l(e)?1:e.coveredArea/e.fullArea),!l(e))for(const{data:t,resolution:i}of e.missingTiles){const e=this._pendingTiles.get(t.id);e?(e.resolution=i,e.alive=!0):this._createPendingTile(t,i)}}_collectMissingTilesInfo(){let e=null;for(let t=this.tilesOfInterest.length-1;t>=0;t--){const i=this.tilesOfInterest[t],s=this.store.process(i,((e,t)=>this._verifyTileComplexity(e,t)));l(e)?e=s:e.prepend(s)}return e}_deletePendingTiles(){for(const[,e]of this._pendingTiles)e.alive||this._deletePendingTile(e)}_processPendingTiles(){const e={fetchCount:(e,t)=>this._fetchCount(e,t),fetchFeatures:(e,t,i)=>this._fetchFeatures(e,t,i),finish:(e,t)=>this._finishPendingTile(e,t),resume:()=>this._processPendingTiles()};if(this._ensureFetchAllCounts(e))for(const[,t]of this._pendingTiles)this._verifyTileComplexity(this.store.getFeatureCount(t.data),t.resolution)&&this.updatingHandles.addPromise(t.process(e))}_verifyTileComplexity(e,t){return this._verifyVertexComplexity(e)&&this._verifyFeatureDensity(e,t)}_verifyVertexComplexity(e){return e*this._minimumVerticesPerFeature<x}_verifyFeatureDensity(e,t){if(l(this.tileInfo))return!1;const i=this.tileSize*t;return e*(j/(i*i))<w}_ensureFetchAllCounts(e){let t=!0;for(const[,i]of this._pendingTiles)i.state.type<v.FETCHED_COUNT&&this.updatingHandles.addPromise(i.process(e)),i.state.type<=v.FETCH_COUNT&&(t=!1);return t}_finishPendingTile(e,t){this.store.add(e.data,t),this._deletePendingTile(e),this._updateAvailability()}_updateAvailability(){const e=this._collectMissingTilesInfo();this._setAvailability(l(e)?1:e.coveredArea/e.fullArea)}_setAvailability(e){this._set("availability",e)}_createPendingTile(e,t){const i=new O(e,t);return this._pendingTiles.set(e.id,i),i}_deletePendingTile(e){e.reset(),this._pendingTiles.delete(e.data.id)}async _fetchCount(e,t){return this.store.fetchCount(e.data,this.url,this._createCountQuery(e),{query:this.customParameters,timeout:S,signal:t})}async _fetchFeatures(e,t,i){let s=0;const r=[];let o=0,n=t;for(;;){const a=this._createFeaturesQuery(e),l=this._setPagingParameters(a,s,n),{features:c,exceededTransferLimit:d}=await this._queryFeatures(a,i);l&&(s+=u(a.num)),o+=c.length;for(const e of c)r.push(e);if(n=t-o,!l||!d||n<=0)return r}}_filterProperties(e){return l(e)?{where:"1=1",gdbVersion:void 0,timeExtent:void 0}:{where:e.where||"1=1",timeExtent:e.timeExtent,gdbVersion:e.gdbVersion}}_lookupObjectIdByGlobalId(e){const t=this.globalIdField,i=this.objectIdField;if(l(t))throw new Error("Expected globalIdField to be defined");let s=null;if(this.store.featureStore.forEach((r=>{e===r.attributes[t]&&(s=r.objectId??r.attributes[i])})),l(s))throw new Error(`Expected to find a feature with globalId ${e}`);return s}_queryFeaturesById(e,t){const i=this._createFeaturesQuery();return i.objectIds=e,this._queryFeatures(i,t)}_queryFeatures(e,t){return this.capabilities.query.supportsFormatPBF?this._queryFeaturesPBF(e,t):this._queryFeaturesJSON(e,t)}async _queryFeaturesPBF(e,t){const{sourceSpatialReference:i}=this,{data:s}=await T(this.url,e,new b({sourceSpatialReference:i}),{query:this._configuration.customParameters,timeout:S,signal:t});return g(s)}async _queryFeaturesJSON(e,t){const{sourceSpatialReference:i}=this,{data:s}=await P(this.url,e,i,{query:this._configuration.customParameters,timeout:S,signal:t});return m(s,this.objectIdField)}_createCountQuery(e){const t=this._createBaseQuery(e);return this.capabilities.query.supportsCacheHint&&(t.cacheHint=!0),t}_createFeaturesQuery(e=null){const t=this._createBaseQuery(e);return t.outFields=this.globalIdField?[this.globalIdField,this.objectIdField]:[this.objectIdField],t.returnGeometry=!0,a(e)&&(this.capabilities.query.supportsResultType?t.resultType="tile":this.capabilities.query.supportsCacheHint&&(t.cacheHint=!0)),t}_createBaseQuery(e){const t=new I({returnZ:this.hasZ,returnM:!1,geometry:a(this.tileInfo)&&a(e)?y(e.data.extent,this.tileInfo.spatialReference):void 0}),i=this._configuration.filter;return a(i)&&(t.where=i.where,t.gdbVersion=i.gdbVersion,t.timeExtent=i.timeExtent),t.outSpatialReference=this.spatialReference,t}_setPagingParameters(e,t,i){if(!this.capabilities.query.supportsPagination)return!1;const{supportsMaxRecordCountFactor:s,supportsCacheHint:r,tileMaxRecordCount:o,maxRecordCount:n,supportsResultType:a}=this.capabilities.query,l=s?I.MAX_MAX_RECORD_COUNT_FACTOR:1,u=l*((a||r)&&o?o:n||E);return e.start=t,s?(e.maxRecordCountFactor=Math.min(l,Math.ceil(i/u)),e.num=Math.min(i,e.maxRecordCountFactor*u)):e.num=Math.min(i,u),!0}};e([h({constructOnly:!0})],C.prototype,"url",void 0),e([h({constructOnly:!0})],C.prototype,"objectIdField",void 0),e([h({constructOnly:!0})],C.prototype,"globalIdField",void 0),e([h({constructOnly:!0})],C.prototype,"capabilities",void 0),e([h({constructOnly:!0})],C.prototype,"sourceSpatialReference",void 0),e([h({constructOnly:!0})],C.prototype,"spatialReference",void 0),e([h({constructOnly:!0})],C.prototype,"store",void 0),e([h({readOnly:!0})],C.prototype,"_minimumVerticesPerFeature",null),e([h()],C.prototype,"filter",null),e([h()],C.prototype,"customParameters",null),e([h({readOnly:!0})],C.prototype,"_configuration",null),e([h()],C.prototype,"tileInfo",null),e([h()],C.prototype,"tileSize",null),e([h()],C.prototype,"tilesOfInterest",void 0),e([h({readOnly:!0})],C.prototype,"updating",null),e([h({readOnly:!0})],C.prototype,"updatingExcludingEdits",null),e([h({readOnly:!0})],C.prototype,"availability",void 0),e([h()],C.prototype,"hasZ",null),C=e([p("esri.views.interactive.snapping.featureSources.featureServiceSource.FeatureServiceTiledFetcher")],C);const E=2e3,S=6e5,x=1e6,j=25,w=1;export{C as FeatureServiceTiledFetcher};