UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 19.1 kB
import{DateTimeReferenceFieldIndex as e}from"../../arcadeTimeUtils.js";import t from"./cache.js";import{FeatureSetError as s,FeatureSetErrorCodes as r}from"./errorsupport.js";import n from"./FeatureSetIterator.js";import i from"./IdSet.js";import{FeatureServiceDatabaseType as a,layerGeometryEsriConstants as l,IdState as u,convertSquareUnitsToCode as h,convertLinearUnitsToCode as c,esriFieldToJson as o,layerGeometryEsriRestConstants as d}from"./shared.js";import{max as _,min as p,sum as f,variance as g,stdev as m,mean as y,distinct as F,count as b}from"./stats.js";import{isPromiseLike as I}from"../../../core/promiseUtils.js";import{WhereClause as w}from"../../../core/sql/WhereClause.js";import{union as S,geodesicArea as T,planarArea as P,geodesicLength as x,planarLength as k}from"../../../geometry/geometryEngineAsync.js";import D from"../../../geometry/SpatialReference.js";import C from"../../../layers/support/FieldsIndex.js";class v{constructor(e){this.recentlyUsedQueries=null,this.featureSetQueryInterceptor=null,this._idstates=[],this._parent=null,this._wset=null,this._mainSetInUse=null,this._maxProcessing=200,this._maxQuery=500,this._totalCount=-1,this._databaseType=a.NotEvaluated,this._databaseTypeProbed=null,this.declaredRootClass="esri.arcade.featureset.support.FeatureSet",this._featureCache=[],this.typeIdField=null,this.types=null,this.fields=null,this.geometryType="",this.objectIdField="",this.globalIdField="",this.spatialReference=null,this.hasM=!1,this.hasZ=!1,this._transparent=!1,this.loaded=!1,this._loadPromise=null,this._fieldsIndex=null,this._dateFieldIndex=null,e&&e.lrucache&&(this.recentlyUsedQueries=e.lrucache),e&&e.interceptor&&(this.featureSetQueryInterceptor=e.interceptor)}optimisePagingFeatureQueries(e){this._parent&&this._parent.optimisePagingFeatureQueries(e)}_hasMemorySource(){return!0}prop(e,t){return void 0===t?this[e]:(void 0!==this[e]&&(this[e]=t),this)}end(){return null!==this._parent&&!0===this._parent._transparent?this._parent.end():this._parent}_ensureLoaded(){return this.load()}load(){return null===this._loadPromise&&(this._loadPromise=this.loadImpl()),this._loadPromise}async loadImpl(){return!0===this._parent?.loaded?(this._initialiseFeatureSet(),this):(await(this._parent?.load()),this._initialiseFeatureSet(),this)}_initialiseFeatureSet(){null!==this._parent?(this.fields=this._parent.fields.slice(0),this.geometryType=this._parent.geometryType,this.objectIdField=this._parent.objectIdField,this.globalIdField=this._parent.globalIdField,this.spatialReference=this._parent.spatialReference,this.hasM=this._parent.hasM,this.hasZ=this._parent.hasZ,this.typeIdField=this._parent.typeIdField,this.types=this._parent.types):(this.fields=[],this.typeIdField="",this.objectIdField="",this.globalIdField="",this.spatialReference=new D({wkid:4326}),this.geometryType=l.point)}getField(e,t){let s;return(t=t||this.fields)&&(e=e.toLowerCase(),t.some((t=>(t&&t.name.toLowerCase()===e&&(s=t),!!s)))),s}getFieldsIndex(){return null===this._fieldsIndex&&(this._fieldsIndex=new C(this.fields)),this._fieldsIndex}_maxProcessingRate(){return null!==this._parent?Math.min(this._maxProcessing,this._parent._maxProcessingRate()):Math.min(this._maxProcessing,this._maxQueryRate())}_maxQueryRate(){return null!==this._parent?Math.max(this._maxQuery,this._parent._maxQueryRate()):this._maxQuery}_checkCancelled(e){if(null!=e&&e.aborted)throw new s(r.Cancelled)}nativeCapabilities(){return this._parent.nativeCapabilities()}async _canDoAggregates(e,t,s,r,n){return null!==this._parent&&this._parent._canDoAggregates(e,t,s,r,n)}async _getAggregatePagesDataSourceDefinition(e,t,n,i,a,l,u){if(null===this._parent)throw new s(r.NeverReach);return this._parent._getAggregatePagesDataSourceDefinition(e,t,n,i,a,l,u)}async _getAgregagtePhysicalPage(e,t,n){if(null===this._parent)throw new s(r.NeverReach);return this._parent._getAgregagtePhysicalPage(e,t,n)}async databaseType(){if(this._databaseType===a.NotEvaluated){if(null!==t.applicationCache){const e=t.applicationCache.getDatabaseType(this._cacheableFeatureSetSourceKey());if(null!==e)return e}if(null!==this._databaseTypeProbed)return this._databaseTypeProbed;try{this._databaseTypeProbed=this._getDatabaseTypeImpl(),null!==t.applicationCache&&t.applicationCache.setDatabaseType(this._cacheableFeatureSetSourceKey(),this._databaseTypeProbed)}catch(e){throw null!==t.applicationCache&&t.applicationCache.clearDatabaseType(this._cacheableFeatureSetSourceKey()),e}return this._databaseTypeProbed}return this._databaseType}async _getDatabaseTypeImpl(){const e=[{thetype:a.SqlServer,testwhere:"(CAST( '2015-01-01' as DATETIME) = CAST( '2015-01-01' as DATETIME)) AND OBJECTID<0"},{thetype:a.Oracle,testwhere:"(TO_DATE('2003-11-18','YYYY-MM-DD') = TO_DATE('2003-11-18','YYYY-MM-DD')) AND OBJECTID<0"},{thetype:a.StandardisedNoInterval,testwhere:"(date '2015-01-01 10:10:10' = date '2015-01-01 10:10:10') AND OBJECTID<0"}];for(const t of e){if(!0===await this._runDatabaseProbe(t.testwhere))return t.thetype}return a.StandardisedNoInterval}_cacheableFeatureSetSourceKey(){return"MUSTBESET"}async _runDatabaseProbe(e){if(null!==this._parent)return this._parent._runDatabaseProbe(e);throw new s(r.NotImplemented)}isTable(){return this._parent?.isTable()??!1}_featureFromCache(e){if(void 0!==this._featureCache[e])return this._featureCache[e]}_isInFeatureSet(e){return u.Unknown}_getSet(e){throw new s(r.NotImplemented)}async _getFeature(e,t,n){if(this._checkCancelled(n),void 0!==this._featureFromCache(t))return this._featureFromCache(t);if(await this._getFeatures(e,t,this._maxProcessingRate(),n),this._checkCancelled(n),void 0!==this._featureFromCache(t))return this._featureFromCache(t);throw new s(r.MissingFeatures)}async _getFeatureBatch(e,t){this._checkCancelled(t);const s=new i([],e,!1,null),r=[];await this._getFeatures(s,-1,e.length,t),this._checkCancelled(t);for(const n of e)void 0!==this._featureFromCache(n)&&r.push(this._featureFromCache(n));return r}async _getFeatures(e,t,s,r){return"success"}_getFilteredSet(e,t,n,i,a){throw new s(r.NotImplemented)}async _refineSetBlock(e,t,s){if(!0===this._checkIfNeedToExpandCandidatePage(e,this._maxQueryRate()))return await this._expandPagedSet(e,this._maxQueryRate(),0,0,s),this._refineSetBlock(e,t,s);this._checkCancelled(s);const r=e._candidates.length;this._refineKnowns(e,t);let n=r-e._candidates.length;if(0===e._candidates.length)return e;if(n>=t)return e;if(await this._refineIfParentKnown(e,t-n,s),this._checkCancelled(s),this._refineKnowns(e,t-n),n=r-e._candidates.length,n<t&&e._candidates.length>0){const r=t-n,i=this._prepareFetchAndRefineSet(e._candidates);return await this._fetchAndRefineFeatures(i,i.length>r?r:e._candidates.length,s),this._checkCancelled(s),this._refineKnowns(e,t-n),e}return e}_fetchAndRefineFeatures(e,t,s){return null}_prepareFetchAndRefineSet(e){const t=[];for(let s=0;s<e.length;s++)this._isPhysicalFeature(e[s])&&t.push(e[s]);return t}_isPhysicalFeature(e){return null===this._parent||this._parent._isPhysicalFeature(e)}_refineKnowns(e,t){let s=0,r=null;const n=[];t=this._maxQueryRate();for(let i=0;i<e._candidates.length&&"GETPAGES"!==e._candidates[i];i++){let a=!1;const l=this._candidateIdTransform(e._candidates[i]);l!==e._candidates[i]&&(a=!0);const h=this._isInFeatureSet(l);if(h===u.InFeatureSet)!0===a?e._known.includes(l)||(e._known.push(l),s+=1):(e._known.push(e._candidates[i]),s+=1),null===r?r={start:i,end:i}:r.end===i-1?r.end=i:(n.push(r),r={start:i,end:i});else if(h===u.NotInFeatureSet)null===r?r={start:i,end:i}:r.end===i-1?r.end=i:(n.push(r),r={start:i,end:i}),s+=1;else if(h===u.Unknown&&(s+=1,!0===e._ordered))break;if(s>=t)break}null!==r&&n.push(r);for(let i=n.length-1;i>=0;i--)e._candidates.splice(n[i].start,n[i].end-n[i].start+1)}_refineIfParentKnown(e,t,s){const r=new i([],[],e._ordered,null);return r._candidates=e._candidates.slice(0),this._parent._refineSetBlock(r,t,s)}_candidateIdTransform(e){return this._parent._candidateIdTransform(e)}_checkIfNeedToExpandKnownPage(e,t){if(null===e.pagesDefinition)return!1;let s=0;for(let r=e._lastFetchedIndex;r<e._known.length;r++){if("GETPAGES"===e._known[r])return!0;if(void 0===this._featureCache[e._known[r]]&&(s+=1,s>=t))break}return!1}_checkIfNeedToExpandCandidatePage(e,t){if(null===e.pagesDefinition)return!1;let s=0;for(let r=0;r<e._candidates.length;r++){if("GETPAGES"===e._candidates[r])return!0;if(s+=1,s>=t)break}return!1}async _expandPagedSet(e,t,n,i,a){if(null===this._parent)throw new s(r.NotImplemented);return this._parent._expandPagedSet(e,t,n,i,a)}async _expandPagedSetFeatureSet(e,t,s,r,n){if(e._known.length>0&&"GETPAGES"===e._known[e._known.length-1]&&(r=1),0===r&&e._candidates.length>0&&"GETPAGES"===e._candidates[e._candidates.length-1]&&(r=2),0===r)return"finished";const i=await this._getPage(e,r,n);return s+i<t?this._expandPagedSet(e,t,s+i,0,n):"success"}async _getPage(e,t,s){const r=1===t?e._known:e._candidates;if(e.pagesDefinition.internal.set.length>e.pagesDefinition.resultOffset||!0===e.pagesDefinition.internal.fullyResolved){r.length=r.length-1;let t=0;for(let n=0;n<e.pagesDefinition.resultRecordCount&&!(e.pagesDefinition.resultOffset+n>=e.pagesDefinition.internal.set.length);n++)r[r.length]=e.pagesDefinition.internal.set[e.pagesDefinition.resultOffset+n],t++;e.pagesDefinition.resultOffset+=t;let s=!1;return!0===e.pagesDefinition.internal.fullyResolved&&e.pagesDefinition.internal.set.length<=e.pagesDefinition.resultOffset&&(s=!0),!1===s&&r.push("GETPAGES"),t}return await this._getPhysicalPage(e,t,s),this._getPage(e,t,s)}_getPhysicalPage(e,t,s){return null}_clonePageDefinition(e){return null===this._parent?null:this._parent._clonePageDefinition(e)}_first(e){return this.iterator(e).next()}first(e){return this._first(e)}async calculateStatistic(e,t,s,r){await this._ensureLoaded();let n=await this._stat(e,t,"",null,null,s,r);return!1===n.calculated&&(n=await this._manualStat(e,t,s,r)),n.result}async _manualStat(e,t,s,r){let n=null;switch(e.toLowerCase()){case"count":return n=await b(this,r),{calculated:!0,result:n};case"distinct":return n=await F(this,t,s,r),{calculated:!0,result:n};case"avg":case"mean":return n=await y(this,t,r),{calculated:!0,result:n};case"stdev":return n=await m(this,t,r),{calculated:!0,result:n};case"variance":return n=await g(this,t,r),{calculated:!0,result:n};case"sum":return n=await f(this,t,r),{calculated:!0,result:n};case"min":return n=await p(this,t,r),{calculated:!0,result:n};case"max":return n=await _(this,t,r),{calculated:!0,result:n};default:return{calculated:!0,result:0}}}async _stat(e,t,s,r,n,i,a){const l=await this._parent._stat(e,t,s,r,n,i,a);return!1===l.calculated?null===n&&""===s&&null===r?this._manualStat(e,t,i,a):{calculated:!1}:l}_unionAllGeomSelf(e){const t=this.iterator(this._defaultTracker(e)),s=[];return new Promise(((e,r)=>{this._unionShapeInBatches(s,t,e,r)}))}_unionAllGeom(e){return new Promise(((t,s)=>{const r=this.iterator(this._defaultTracker(e)),n=[];this._unionShapeInBatches(n,r,t,s)}))}_unionShapeInBatches(e,t,s,r){t.next().then((n=>{try{null!==n&&null!==n.geometry&&e.push(n.geometry),e.length>30||null===n&&e.length>1?S(e).then((i=>{try{null===n?s(i):(e=[i],this._unionShapeInBatches(e,t,s,r))}catch(a){r(a)}}),r):null===n?1===e.length?s(e[0]):s(null):this._unionShapeInBatches(e,t,s,r)}catch(i){r(i)}}),r)}iterator(e){return new n(this,e)}intersection(e,t=!1){return v._featuresetFunctions.intersection.bind(this)(e,t)}difference(e,t=!1,s=!0){return v._featuresetFunctions.difference.bind(this)(e,t,s)}symmetricDifference(e,t=!1,s=!0){return v._featuresetFunctions.symmetricDifference.bind(this)(e,t,s)}morphShape(e,t,s="unknown",r=null){return v._featuresetFunctions.morphShape.bind(this)(e,t,s,r)}morphShapeAndAttributes(e,t,s="unknown"){return v._featuresetFunctions.morphShapeAndAttributes.bind(this)(e,t,s)}union(e,t=!1){return v._featuresetFunctions.union.bind(this)(e,t)}intersects(e){return v._featuresetFunctions.intersects.bind(this)(e)}envelopeIntersects(e){return v._featuresetFunctions.envelopeIntersects.bind(this)(e)}contains(e){return v._featuresetFunctions.contains.bind(this)(e)}overlaps(e){return v._featuresetFunctions.overlaps.bind(this)(e)}relate(e,t){return v._featuresetFunctions.relate.bind(this)(e,t)}within(e){return v._featuresetFunctions.within.bind(this)(e)}touches(e){return v._featuresetFunctions.touches.bind(this)(e)}top(e){return v._featuresetFunctions.top.bind(this)(e)}crosses(e){return v._featuresetFunctions.crosses.bind(this)(e)}buffer(e,t,s,r=!0){return v._featuresetFunctions.buffer.bind(this)(e,t,s,r)}filter(e,t=null){return v._featuresetFunctions.filter.bind(this)(e,t)}orderBy(e){return v._featuresetFunctions.orderBy.bind(this)(e)}dissolve(e,t){return v._featuresetFunctions.dissolve.bind(this)(e,t)}groupby(e,t){return v._featuresetFunctions.groupby.bind(this)(e,t)}reduce(e,t=null,s){return new Promise(((r,n)=>{this._reduceImpl(this.iterator(this._defaultTracker(s)),e,t,0,r,n,0)}))}_reduceImpl(e,t,s,r,n,i,a){try{if(++a>1e3)return void setTimeout((()=>{a=0,this._reduceImpl(e,t,s,r,n,i,a)}));e.next().then((l=>{try{if(null===l)n(s);else{const u=t(s,l,r,this);I(u)?u.then((s=>{this._reduceImpl(e,t,s,r+1,n,i,a)}),i):this._reduceImpl(e,t,u,r+1,n,i,a)}}catch(u){i(u)}}),i)}catch(l){i(l)}}removeField(e){return v._featuresetFunctions.removeField.bind(this)(e)}addField(e,t,s=null){return v._featuresetFunctions.addField.bind(this)(e,t,s)}sumArea(e,t=!1,s){const r=h(e);return this.reduce(((e,s)=>null===s.geometry?0:t?T(s.geometry,r).then((t=>e+t)):P(s.geometry,r).then((t=>e+t))),0,s)}sumLength(e,t=!1,s){const r=c(e);return this.reduce(((e,s)=>null===s.geometry?0:t?x(s.geometry,r).then((t=>e+t)):k(s.geometry,r).then((t=>e+t))),0,s)}_substituteVars(e,t){if(null!==t){const s={};for(const e in t)s[e.toLowerCase()]=t[e];e.parameters=s}}async distinct(e,t=1e3,s=null,r){await this.load();const n=w.create(e,this.getFieldsIndex());return this._substituteVars(n,s),this.calculateStatistic("distinct",n,t,this._defaultTracker(r))}async min(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("min",r,-1,this._defaultTracker(s))}async max(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("max",r,-1,this._defaultTracker(s))}async avg(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("avg",r,-1,this._defaultTracker(s))}async sum(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("sum",r,-1,this._defaultTracker(s))}async stdev(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("stdev",r,-1,this._defaultTracker(s))}async variance(e,t=null,s){await this.load();const r=w.create(e,this.getFieldsIndex());return this._substituteVars(r,t),this.calculateStatistic("variance",r,-1,this._defaultTracker(s))}async count(e){return await this.load(),this.calculateStatistic("count",w.create("1",this.getFieldsIndex()),-1,this._defaultTracker(e))}_defaultTracker(e){return e||{aborted:!1}}forEach(e,t){return new Promise(((s,r)=>{this._forEachImpl(this.iterator(this._defaultTracker(t)),e,this,s,r,0)}))}_forEachImpl(e,t,s,r,n,i){try{if(++i>1e3)return void setTimeout((()=>{i=0,this._forEachImpl(e,t,s,r,n,i)}),0);e.next().then((a=>{try{if(null===a)r(s);else{const l=t(a);null==l?this._forEachImpl(e,t,s,r,n,i):I(l)?l.then((()=>{try{this._forEachImpl(e,t,s,r,n,i)}catch(a){n(a)}}),n):this._forEachImpl(e,t,s,r,n,i)}}catch(l){n(l)}}),n)}catch(a){n(a)}}convertToJSON(e){const t={layerDefinition:{geometryType:this.geometryType,fields:[]},featureSet:{features:[],geometryType:this.geometryType}};for(let s=0;s<this.fields.length;s++)t.layerDefinition.fields.push(o(this.fields[s]));return this.reduce(((e,s)=>{const r={geometry:s.geometry&&s.geometry.toJSON(),attributes:{}};for(const t in s.attributes)r.attributes[t]=s.attributes[t];return t.featureSet.features.push(r),1}),0,e).then((()=>t))}castToText(e=!1){return"object, FeatureSet"}queryAttachments(e,t,s,r,n){return this._parent.queryAttachments(e,t,s,r,n)}serviceUrl(){return this._parent.serviceUrl()}subtypes(){return this.typeIdField?{subtypeField:this.typeIdField,subtypes:this.types?this.types.map((e=>({name:e.name,code:e.id}))):[]}:null}relationshipMetaData(){return this._parent.relationshipMetaData()}get gdbVersion(){return this._parent?this._parent.gdbVersion:""}schema(){const e=[];for(const t of this.fields)e.push(o(t));return{objectIdField:this.objectIdField,globalIdField:this.globalIdField,geometryType:void 0===d[this.geometryType]?"esriGeometryNull":d[this.geometryType],fields:e}}async convertToText(e,t){if("schema"===e)return await this._ensureLoaded(),JSON.stringify(this.schema());if("featureset"===e){await this._ensureLoaded();const e=[];await this.reduce(((t,s)=>{const r={geometry:s.geometry?s.geometry.toJSON():null,attributes:s.attributes};return null!==r.geometry&&r.geometry.spatialReference&&delete r.geometry.spatialReference,e.push(r),1}),0,t);const s=this.schema();return s.features=e,s.spatialReference=this.spatialReference.toJSON(),JSON.stringify(s)}return this.castToText()}getFeatureByObjectId(e,t){return this._parent.getFeatureByObjectId(e,t)}getOwningSystemUrl(){return this._parent.getOwningSystemUrl()}getIdentityUser(){return this._parent.getIdentityUser()}getRootFeatureSet(){return null!==this._parent?this._parent.getRootFeatureSet():this}getDataSourceFeatureSet(){return null!==this._parent?this._parent.getDataSourceFeatureSet():this}castAsJson(e=null){return"keeptype"===e?.featureset?this:"none"===e?.featureset?null:{type:"FeatureSet"}}async castAsJsonAsync(e=null,t=null){if("keeptype"===t?.featureset)return this;if("schema"===t?.featureset)return await this._ensureLoaded(),JSON.parse(JSON.stringify(this.schema()));if("none"===t?.featureset)return null;await this._ensureLoaded();const s=[];await this.reduce(((e,r)=>{const n={geometry:r.geometry?!0===t?.keepGeometryType?r.geometry:r.geometry.toJSON():null,attributes:r.attributes};return null!==n.geometry&&n.geometry.spatialReference&&!0!==t?.keepGeometryType&&delete n.geometry.spatialReference,s.push(n),1}),0,e);const r=this.schema();return r.features=s,r.spatialReference=!0===t?.keepGeometryType?this.spatialReference:this.spatialReference?.toJSON(),r}get dateTimeReferenceFieldIndex(){return null===this._dateFieldIndex&&(this._dateFieldIndex=e.create(this.getFieldsIndex(),this)),this._dateFieldIndex}fieldTimeZone(e){return this.dateTimeReferenceFieldIndex.fieldTimeZone(e)}get preferredTimeReference(){return this._parent?.preferredTimeReference??null}get dateFieldsTimeReference(){return this._parent?.dateFieldsTimeReference??null}get datesInUnknownTimezone(){return this._parent.datesInUnknownTimezone}get editFieldsInfo(){return this._parent?.editFieldsInfo??null}get timeInfo(){return this._parent?.timeInfo??null}}v._featuresetFunctions={};export{v as default};