@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 14.9 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{_ as e}from"../../../chunks/tslib.es6.js";import{pickRandom as t}from"../../../core/arrayUtils.js";import r from"../../../core/Error.js";import a from"../../../core/Logger.js";import{assertIsSome as i}from"../../../core/maybe.js";import{timeout as s,throwIfAborted as o}from"../../../core/promiseUtils.js";import{whenOnce as n}from"../../../core/reactiveUtils.js";import{property as l}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import{subclass as u}from"../../../core/accessorSupport/decorators/subclass.js";import{toQuantizationTransform as m}from"../../../geometry/support/quantizationUtils.js";import{kebabDict as p}from"../../../layers/support/fieldType.js";import{isTimeOnlyField as y}from"../../../layers/support/fieldUtils.js";import c from"../../../rest/support/QuantizationParameters.js";import{getArcadeForPredominantCategory as f}from"../../statistics/support/predominanceUtils.js";import{summaryStatistics as d,uniqueValues as h,histogram as w,classBreaks as g,heatmapStatistics as F}from"../../statistics/support/statsWorker.js";import{mergeWhereClauses as S}from"../../statistics/support/utils.js";import{WorkerClient as v}from"../../statistics/support/WorkerClient.js";import{getFieldsList as q,isAnyDateField as V,fieldDelimiter as x}from"../utils.js";import _ from"./LayerAdapter.js";import{getHistogramAttributeDefinition as z,getAttributeBinsQuery as T,processQueryAttributeBinsResult as N}from"./support/histogramUtils.js";import{ensureFeaturesJSON as M,getSummaryStatsQuery as L,getSummaryStatisticsFromFeatureSet as C,getViewInfoParams as E,getMissingFields as j,updateQueryWithFeatureFilter as b,getUVQuery as k,getUniqueValuesFromFeatureSet as I,getBins as O,getDomainsForFields as Q,getPredominantCategoriesFromUVInfos as J}from"./support/utils.js";import{processSummaryStatisticsResult as B,createUVResult as P,resolveCBResult as U}from"../../../statistics/utils.js";import{loadArcade as A}from"../../../support/loadArcade.js";const $=5;let G=null,R=class extends _{constructor(){super(...arguments),this._hasLocalSource=!1,this.adapterName="in-memory-layer-adapter"}destroy(){this.workerClient?.destroy()}async _waitForLayerViewUpdate(e){if(!e)throw new r(`${this.adapterName}:insufficient-data`,"layerView is required to fetch the features");const t=new AbortController,i=n((()=>!e.updating),t.signal);await s(i,5e3,t).catch((e=>{throw a.getLogger(this).warn("LayerView is taking too long to update. Aborting fetch from layerView."),e}))}async _fetchFeatureSetFromMemory(e,t,a){const i=this.layer;if(this._hasLocalSource&&"queryFeatures"in i)return i.queryFeatures(e);if(!t)throw new r(`${this.adapterName}:insufficient-data`,"view is required to fetch the features from layerView");const s=await t.whenLayerView(this.layer);return await this._waitForLayerViewUpdate(s),s.queryFeatures(e,{signal:a})}async _fetchFeaturesFromMemory(e,t,a,i){const s=this.layer,o="json"===i;if(this._hasLocalSource&&"queryFeatures"in s){const e=await s.queryFeatures(t);return o?M(e.features):e.features}if(await this._waitForLayerViewUpdate(e),o&&"queryFeaturesJSON"in e&&e.queryFeaturesJSON){const{features:r}=await e.queryFeaturesJSON(t,{signal:a});return r}if(!("queryFeatures"in e))throw new r(`${this.adapterName}:not-supported`,"'queryFeatures' is not supported on the layerView");const n=await e.queryFeatures(t,{signal:a});return o?M(n.features):n.features}_fetchFeaturesForStats(e,t){return q({field:e.field,field2:e.field2,field3:e.field3,normalizationField:e.normalizationField,valueExpression:e.valueExpression}).then((r=>this.getSampleFeatures({sampleSize:-1,view:e.view,returnGeometry:e.returnGeometry,filter:e.filter,requiredFields:r,signal:e.signal},t)))}async _summaryStatsFromClientQuery(e,t){const{view:r,signal:a}=e,i=L(this,e,t),s=await this._fetchFeatureSetFromMemory(i,r,a),o=C(s,V(t)||y(t));return B(o,e.outStatisticTypes)}async _getNormalizationTotalFromMemory(e,t,a){const{featuresJSON:i,graphics:s,layerView:o,query:n}=t,l={include:["sum"]},u=(!i&&!s&&o&&"querySummaryStatistics"in o?await o.querySummaryStatistics(n,{field:e},{signal:a}):i?await this.workerClient.summaryStatistics({field:e,outStatisticTypes:l},i):await d({attribute:{field:e,outStatisticTypes:l},features:s??[]})).sum;if(null==u)throw new r(`${this.adapterName}:invalid`,"invalid normalizationTotal");return u}async _summaryStatsFromMemory(e,t){const{view:r,field:a,valueExpression:i,normalizationType:s,signal:o}=e,n={field:a,valueExpression:i,normalizationType:s,normalizationField:e.normalizationField,normalizationTotal:e.normalizationTotal,minValue:e.minValue,maxValue:e.maxValue,outStatisticTypes:e.outStatisticTypes},{featuresJSON:l,graphics:u,layerView:m,query:y,fieldInfos:c}=await this._processStatsFromMemoryParams({...e,layerViewFunc:"querySummaryStatistics"});return i&&r&&(l||u)&&(n.fieldType=t?.type?p.toJSON(t.type):null,n.viewInfoParams=E(r),n.timeZone=r.timeZone,n.fieldInfos=c),"percent-of-total"===s&&null==e.normalizationTotal&&(n.normalizationTotal=await this._getNormalizationTotalFromMemory(a,{featuresJSON:l,graphics:u,layerView:m,query:y},o)),!l&&!u&&m&&"querySummaryStatistics"in m?m.querySummaryStatistics(y,n,{signal:o}):l?this.workerClient.summaryStatistics(n,l):d({attribute:n,features:u})}async _getFilteredFeatures(e,t){let r=e;const a=t?.geometry;if("intersects"===t?.spatialRelationship&&a){const t=await import("../../../geometry/operators/intersectionOperator.js");r=e.filter((({geometry:e})=>!(!e||!t.execute(e,a))))}return e.length&&"declaredClass"in e[0]&&"esri.Graphic"===e[0].declaredClass?{graphics:r}:{featuresJSON:r}}async _processStatsFromMemoryParams(e){const{features:t,filter:r}=e;if(t?.length)return this._getFilteredFeatures(t,r);const{view:a,field:i,field2:s,field3:o,normalizationField:n,valueExpression:l,layerViewFunc:u,signal:m}=e;let p=e.returnGeometry;if(null==p&&l){if(!G){const{arcadeUtils:e}=await A();G=e}const e=G.hasGeometryOperations(l);e&&await G.enableGeometryOperations(),p=e}let y=null,c=null,f=null,d=null,h=null;if(a)try{const e="subtype-sublayer"===this.layer.type?this.layer.parent:this.layer;y=await a.whenLayerView(e),c=null!=u&&u in y&&"function"==typeof y[u]}catch{c=!1}if(c)try{await this._waitForLayerViewUpdate(y);const e=await q({field:i,field2:s,field3:o,normalizationField:n,valueExpression:l});j(this,e,y).length?c=!1:(f=this.layer.createQuery(),f.outFields=e,f.returnGeometry=!1,b(f,r)),y.suspended&&(c=!1)}catch{c=!1}return c||(d=await this._fetchFeaturesForStats({field:i,field2:s,field3:o,valueExpression:l,normalizationField:n,returnGeometry:p,filter:r,view:a,signal:m},"json"),h=(await q({valueExpression:l})).map((e=>this.getField(e)?.toJSON())).filter(Boolean)),{layerView:y,query:f,featuresJSON:d,fieldInfos:h}}async _uvFromClientQuery(e,t){const{view:r,signal:a}=e,i=k(this,e),s=await this._fetchFeatureSetFromMemory(i,r,a),o=await I(s,{layer:this,field:e.field,field2:e.field2,field3:e.field3,fieldDelimiter:x,view:e.view,signal:e.signal});return P(o,t,e.returnAllCodedValues,x)}async _uvFromMemory(e,t){const{view:r,field:a,valueExpression:i,returnAllCodedValues:s,signal:o}=e,{featuresJSON:n,graphics:l,layerView:u,query:m,fieldInfos:p}=await this._processStatsFromMemoryParams({...e,layerViewFunc:"queryUniqueValues"}),y={field:a,field2:e.field2,field3:e.field3,fieldDelimiter:x,valueExpression:i,domains:t,returnAllCodedValues:s};return i&&r&&(n||l)&&(y.viewInfoParams=E(r),y.timeZone=r.timeZone,y.fieldInfos=p),!n&&!l&&u&&"queryUniqueValues"in u?u.queryUniqueValues(m,y,{signal:o}):n?this.workerClient.uniqueValues(y,n):h({attribute:y,features:l})}_histogramForField(e){let t=null;return t=null!=e.minValue&&null!=e.maxValue?Promise.resolve({min:e.minValue,max:e.maxValue}):this.summaryStatistics({...e,outStatisticTypes:{include:["min","max","count"]}}).then((e=>{if(!e.count)throw new r(`${this.adapterName}:insufficient-data`,"Either the layer has no features or none of the features have data for the field");return{min:e.min,max:e.max}})),t.then((t=>O(this,{min:t.min,max:t.max},e.field,e.numBins??void 0,e.view,e.filter,e.signal)))}async _histogramFromQueryAttributeBinsFromMemory(e){const{field:t,normalizationType:r,signal:a}=e,i=await this._processStatsFromMemoryParams({...e,layerViewFunc:"queryAttributeBins"}),{featuresJSON:s,graphics:o,layerView:n,query:l}=i,u="percent-of-total"===r?e.normalizationTotal??await this._getNormalizationTotalFromMemory(t,i,a):void 0;if(!n||!("queryAttributeBins"in n)||s||o){const t=z(e,i,u);return s?this.workerClient.histogram(t,s):w({attribute:t,features:o})}const{query:m,min:p,max:y}=await T(e,this,u,l?.where);if(!m)return{bins:[],minValue:p,maxValue:y,normalizationTotal:u};const c=await n.queryAttributeBins(m,{signal:a});return N(c,t?this.getField(t):null,{minValue:p,maxValue:y,normalizationTotal:u})}async _histogramFromMemory(e){const{field:t,signal:r}=e,a=await this._processStatsFromMemoryParams({...e,layerViewFunc:"queryHistogram"}),{featuresJSON:i,graphics:s,layerView:o,query:n}=a,l="percent-of-total"===e.normalizationType?e.normalizationTotal??await this._getNormalizationTotalFromMemory(t,a,r):void 0,u=z(e,a,l);return!i&&!s&&o&&"queryHistogram"in o?o.queryHistogram(n,u,{signal:r}):i?this.workerClient.histogram(u,i):w({attribute:u,features:s})}_classBreaksFromInterpolation(e){const{minValue:t,maxValue:r}=e,a=e.numClasses||$,i=[],s=(r-t)/a;for(let l=0;l<a;l++){const e=t+l*s;i.push({minValue:e,maxValue:e+s})}i[a-1].maxValue=r;const o={classBreaks:i,normalizationTotal:e.normalizationTotal},n=U(o,e.classificationMethod);return Promise.resolve(n)}async _classBreaksFromMemory(e){const{view:t,field:r,valueExpression:a,signal:i}=e,{featuresJSON:s,graphics:o,layerView:n,query:l,fieldInfos:u}=await this._processStatsFromMemoryParams({...e,layerViewFunc:"queryClassBreaks"}),m={field:r,valueExpression:a,normalizationType:e.normalizationType,normalizationField:e.normalizationField,normalizationTotal:e.normalizationTotal,minValue:e.minValue,maxValue:e.maxValue,standardDeviationInterval:e.standardDeviationInterval,classificationMethod:e.classificationMethod,numClasses:e.numClasses};return a&&t&&(s||o)&&(m.viewInfoParams=E(t),m.timeZone=t.timeZone,m.fieldInfos=u),"percent-of-total"===e.normalizationType&&null==e.normalizationTotal&&(m.normalizationTotal=await this._getNormalizationTotalFromMemory(r,{featuresJSON:s,graphics:o,layerView:n,query:l},i)),!s&&!o&&n&&"queryClassBreaks"in n?n.queryClassBreaks(l,m,{signal:i}):s?this.workerClient.classBreaks(m,s):g({attribute:m,features:o})}async _heatmapStatsFromMemory(e){const{view:t,field:r,radius:a,signal:s}=e;i(t,"InMemoryLayerAdapter: must have a view");try{const r=await t.whenLayerView(this.layer);if("queryHeatmapStatistics"in r&&!r.updating&&!e.filter)return await r.queryHeatmapStatistics({field:e.field,radius:e.radius})}catch{o(s)}const{featuresJSON:n,graphics:l}=await this._processStatsFromMemoryParams({...e,returnGeometry:!0}),u=new c({extent:t.extent,tolerance:"2d"===t.type?t.state.resolution:t.pixelSizeAt?.(t.center)}),p={field:r,radius:a,transform:m(u),spatialReference:t.spatialReference?.toJSON(),size:t.size};return n?.length||l?.length?n?this.workerClient.heatmapStatistics(p,n):F({attribute:p,features:l}):{min:null,max:null}}getField(e=""){return this.layer.getField(e)}getFieldUsageInfo(e){return this.getField(e)?{supportsLabelingInfo:!0,supportsRenderer:!0,supportsPopupTemplate:!0,supportsLayerQuery:!0,supportsStatistics:!0}:null}getFieldDomain(e,t){return this.layer.getFieldDomain(e,t)}createQuery(){return this.layer.createQuery()}async summaryStatistics(e){const{field:t,valueExpression:a,sqlExpression:i,features:s,view:o}=e,n=t?this.getField(t):null,l=!!a,u="3d"===o?.type;if(!a&&i&&!this.supportsSQLExpression)throw new r(`${this.adapterName}:not-supported`,"Layer does not support standardized SQL expression for queries");return l||s||u?this._summaryStatsFromMemory(e,n):this._summaryStatsFromClientQuery(e,n)}async uniqueValues(e){const{valueExpression:t,sqlExpression:a,features:i,view:s}=e,o=await Q(e,this),n=!!t,l="3d"===s?.type;if(!t&&a&&!this.supportsSQLExpression)throw new r(`${this.adapterName}:not-supported`,"Layer does not support standardized SQL expression for queries");return n||i||l?this._uvFromMemory(e,o):this._uvFromClientQuery(e,o)}async histogram(e){const{features:t,valueExpression:a,normalizationType:i,sqlExpression:s}=e,o=t||!!a;if(!a&&s&&!this.supportsSQLExpression)throw new r(`${this.adapterName}:not-supported`,"Layer does not support standardized SQL expression for queries");return o||i?this._histogramFromMemory(e):this._histogramForField(e)}async classBreaks(e){return!1!==e.analyzeData?this._classBreaksFromMemory(e):this._classBreaksFromInterpolation(e)}async queryFeatureCount(e){const{whereClause:t,view:a,signal:i}=e;if(!a)throw new r(`${this.adapterName}:insufficient-data`,"view is required to fetch the features from layerView");const s=this.layer.createQuery();s.where=S(s.where,t);const o=await a.whenLayerView(this.layer);return await n((()=>!o.updating),i),o.queryFeatureCount(s,{signal:i})}async generateRenderer(e,t){throw new r(`${this.adapterName}:not-supported`,"Layer does not support generateRenderer operation")}async predominantCategories(e){const{fields:t,view:r,signal:a,filter:i}=e,s=f(t),o=await this._uvFromMemory({valueExpression:s,view:r,signal:a,filter:i});return J(o.uniqueValueInfos,t)}async heatmapStatistics(e){return this._heatmapStatsFromMemory(e)}async getSampleFeatures(e,a){const{view:i,sampleSize:s,requiredFields:o,returnGeometry:n,filter:l,signal:u}=e,m=this.layer.createQuery(),p=1;if(m.outSpatialReference=i?.spatialReference,m.returnGeometry=!!n,m.outFields=o,b(m,l),!i)throw new r(`${this.adapterName}:not-supported`,"view is required to get sample features for Layer");const y=await i.whenLayerView(this.layer);if(j(this,o,y).length)throw new r(`${this.adapterName}:not-supported`,"Required fields need to be passed in the outFields for Layer");const c=await this._fetchFeaturesFromMemory(y,m,u,a),f=null!=s&&s>0&&s<=c.length?s:c.length;return t(c,f,p)}load(e){const t=this.layer.load(e).then((async t=>{this.geometryType=t.geometryType,this.objectIdField=t.objectIdField,this.supportsSQLExpression="stream"===t.type||!!t.capabilities?.query?.supportsSqlExpression,this.minScale=t.minScale,this.maxScale=t.maxScale,this.fullExtent="fullExtent"in t?t.fullExtent:t.parent?.fullExtent,this._hasLocalSource=!1,this.hasQueryEngine=!0,this.workerClient=v.getInstance(),await this.workerClient.open(e.signal)}));return this.addResolvingPromise(t),Promise.resolve(this)}};e([l({readOnly:!0})],R.prototype,"adapterName",void 0),e([l({constructOnly:!0})],R.prototype,"layer",void 0),R=e([u("esri.smartMapping.support.adapters.InMemoryLayerAdapter")],R);export{R as default};