@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 9.79 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{neverAbortedSignal as e}from"../../arcadeEnvironment.js";import t from"./cache.js";import{throwIfCancelled as a,FeatureSetError as s}from"./errorsupport.js";import{QueryIterator as r}from"./FeatureSetIterator.js";import{layerGeometryEsriConstants as i,esriFieldToJson as n,layerGeometryEsriRestConstants as l}from"./shared.js";import{max as o,min as u,sum as h,variance as d,stdev as c,mean as p,distinct as y}from"./stats.js";import{invokeRemoteGeometryOp as m}from"../../geometry/operatorsWorkerConnection.js";import{cacheAsync as f}from"../../../core/iteratorUtils.js";import _ from"../../../core/sql/WhereClause.js";import g from"../../../geometry/SpatialReference.js";import{fromJSON as b}from"../../../geometry/support/jsonUtils.js";import T from"../../../layers/support/FieldsIndex.js";function F(e,t){for(const a of e)null!=a.geometry&&(a.geometry.spatialReference??=t)}const I=Object.freeze({ordered:!0,filterApplied:!0,spatialFilterApplied:!0,features:Object.freeze({next:()=>Promise.resolve({done:!0,value:void 0}),[Symbol.asyncIterator](){return this}})});class w{constructor(){this.declaredRootClass="esri.arcade.featureset.support.FeatureSet",this._parent=null,this._maxProcessing=200,this._maxQuery=500,this._totalCount=-1,this._databaseType=7,this._databaseTypeProbed=null,this._loadPromise=null,this._allFeatures=null,this._fieldsIndex=null,this.typeIdField=null,this.types=null,this.subtypeField=null,this.subtypes=null,this.fields=null,this.geometryType="",this.objectIdField="",this.globalIdField="",this.spatialReference=null,this.hasM=!1,this.hasZ=!1}_ensureLoaded(){return this.load()}load(){return null===this._loadPromise&&(this._loadPromise=this.loadImpl()),this._loadPromise}async loadImpl(){return await(this._parent?.load()),this._initialiseFeatureSet(),this}_initialiseFeatureSet(){null!==this._parent?(this.fields=this._parent.fields.slice(),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.subtypeField=this._parent.subtypeField,this.subtypes=this._parent.subtypes):(this.fields=[],this.typeIdField="",this.subtypeField="",this.objectIdField="",this.globalIdField="",this.spatialReference=new g({wkid:4326}),this.geometryType=i.point)}getField(e,t){return e=e.toLowerCase(),(t||this.fields).find(t=>t.name.toLowerCase()===e)}getFieldsIndex(){return null===this._fieldsIndex&&(this._fieldsIndex=T.fromLayer({timeInfo:this.timeInfo,editFieldsInfo:this.editFieldsInfo,dateFieldsTimeZone:this.dateFieldsTimeZone,datesInUnknownTimezone:this.datesInUnknownTimezone,fields: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}nativeCapabilities(){return this._parent.nativeCapabilities()}get _hasCachedFeatures(){return null!=this._allFeatures}async queryAll(t){null==this._allFeatures&&(this._allFeatures=this._queryAll().then(e=>f(e)));const s=(await this._allFeatures)();return null==t||t===e?s:async function*(){for await(const e of s)a(t),yield e}()}async databaseType(){if(7===this._databaseType){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=[{dbType:2,probeSql:"(CAST( '2015-01-01' as DATETIME) = CAST( '2015-01-01' as DATETIME)) AND OBJECTID<0"},{dbType:3,probeSql:"(TO_DATE('2003-11-18','YYYY-MM-DD') = TO_DATE('2003-11-18','YYYY-MM-DD')) AND OBJECTID<0"},{dbType:1,probeSql:"(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.probeSql))return t.dbType}return 1}_cacheableFeatureSetSourceKey(){return"MUSTBESET"}async _runDatabaseProbe(e){if(null!==this._parent)return this._parent._runDatabaseProbe(e);throw new s("NotImplemented")}isTable(){return this._parent?.isTable()??!1}first(e){return this.iterator(e).next()}async isEmpty(e){return this._totalCount>=0?0===this._totalCount:null==await this.first(e)}async calculateStatistic(e,t,a,s){await this._ensureLoaded();let r=await this.queryStat({stat:e,field:t,limit:a,abortSignal:s});return!1===r.calculated&&(r=await this._manualStat(e,t,a,s)),r.result}async _manualStat(e,t,a,s){let r=null;switch(e.toLowerCase()){case"count":{if(this._totalCount>=0)return{calculated:!0,result:this._totalCount};const e=await this.queryAll(s);let t=0;for await(const a of e)t+=a.length;return this._totalCount=t,{calculated:!0,result:t}}case"distinct":return r=await y(this,t,a,s),{calculated:!0,result:r};case"avg":case"mean":return r=await p(this,t,s),{calculated:!0,result:r};case"stdev":return r=await c(this,t,s),{calculated:!0,result:r};case"variance":return r=await d(this,t,s),{calculated:!0,result:r};case"sum":return r=await h(this,t,s),{calculated:!0,result:r};case"min":return r=await u(this,t,s),{calculated:!0,result:r};case"max":return r=await o(this,t,s),{calculated:!0,result:r};default:return{calculated:!0,result:0}}}iterator(e){return new r(this,e)}async sumArea(e,t,a){let s=0;for await(const r of await this.queryAll(a))for(const a of r)null!=a.geometry&&(s+=null!=t?await m("geodeticArea",[a.geometry,e,t]):await m("area",[a.geometry,e]));return s}async sumLength(e,t,a){let s=0;for await(const r of await this.queryAll(a))for(const a of r)null!=a.geometry&&(s+=null!=t?await m("geodeticLength",[a.geometry,e,t]):await m("length",[a.geometry,e]));return s}async count(t){return await this.load(),this.calculateStatistic("count",_.create("1",{fieldsIndex:this.getFieldsIndex(),timeZone:this.dateFieldsTimeZoneDefaultUTC}),-1,t??e)}castToText(e=!1){return"object, FeatureSet"}queryAttachments(e,t,a,s,r){return this._parent.queryAttachments(e,t,a,s,r)}serviceUrl(){return this._parent.serviceUrl()}subtypeMetadata(){return this.subtypeField&&this.subtypes?{subtypeField:this.subtypeField,subtypes:this.subtypes?this.subtypes.map(e=>({name:e.name,code:e.code})):[]}: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(n(t));return{objectIdField:this.objectIdField,globalIdField:this.globalIdField,geometryType:void 0===l[this.geometryType]?"esriGeometryNull":l[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=[];for await(const a of await this.queryAll(t))for(const t of a){const a={geometry:t.geometry??null,attributes:t.attributes};if(null!==a.geometry&&a.geometry.spatialReference){const{spatialReference:e,...t}=a.geometry;a.geometry=t}e.push(a)}return JSON.stringify({...this.schema(),features:e,spatialReference:this.spatialReference.toJSON()})}return this.castToText()}getFeatureByObjectId(e,t){return this._parent.getFeatureByObjectId(e,t)}getOwningSystemUrl(){return this._parent.getOwningSystemUrl()}getIdentityUser(){return this._parent.getIdentityUser()}getRootFeatureSet(){return this._parent.getRootFeatureSet()}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 a=[];for await(const s of await this.queryAll(e))for(const e of s){const s={geometry:e.geometry?!0===t?.keepGeometryType?b(e.geometry):e.geometry:null,attributes:e.attributes};if(null!==s.geometry&&s.geometry.spatialReference&&!0!==t?.keepGeometryType){delete s.geometry.spatialReference;const{spatialReference:e,...t}=s.geometry;s.geometry=t}a.push(s)}return{...this.schema(),features:a,spatialReference:!0===t?.keepGeometryType?this.spatialReference:this.spatialReference?.toJSON()}}fieldTimeZone(e){return this.getFieldsIndex().getTimeZone(e)}get preferredTimeZone(){return this._parent?.preferredTimeZone??null}get dateFieldsTimeZone(){return this._parent?.dateFieldsTimeZone??null}get dateFieldsTimeZoneDefaultUTC(){return this.datesInUnknownTimezone?"unknown":this.dateFieldsTimeZone||"UTC"}get datesInUnknownTimezone(){return this._parent.datesInUnknownTimezone}get editFieldsInfo(){return this._parent?.editFieldsInfo??null}get timeInfo(){return this._parent?.timeInfo??null}getFeatureSetInfo(){return this._parent?this._parent.getFeatureSetInfo():Promise.resolve(null)}}class S extends w{constructor(){super(...arguments),this._parent=null}getRootFeatureSet(){return this}}export{w as FeatureSet,S as RootFeatureSet,F as assignSpatialReferenceOnFeatures,I as emptyFeaturesResult};