@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 13.5 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 t from"../../../Graphic.js";import{pickRandom as r}from"../../../core/arrayUtils.js";import i from"../../../core/Error.js";import{throwIfAborted as s}from"../../../core/promiseUtils.js";import{property as a}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import"../../../core/Logger.js";import{subclass as o}from"../../../core/accessorSupport/decorators/subclass.js";import{isHostedAgolService as n}from"../../../layers/support/arcgisLayerUrl.js";import{isTimeOnlyField as l,isNumericField as u}from"../../../layers/support/fieldUtils.js";import{generateRenderer as m}from"../../../rest/generateRenderer.js";import c from"../../../rest/support/GenerateRendererParameters.js";import p from"../../../rest/support/StatisticDefinition.js";import h from"../../../rest/support/UniqueValueDefinition.js";import{getArcadeForPredominantCategory as d,getSQLForPredominantCategoryName as y}from"../../statistics/support/predominanceUtils.js";import{mergeWhereClauses as f,getRangeExpr as F,getSQLFilterForNormalization as S}from"../../statistics/support/utils.js";import{WorkerClient as g}from"../../statistics/support/WorkerClient.js";import{isAnyDateField as w,fieldDelimiter as v}from"../utils.js";import x from"./InMemoryLayerAdapter.js";import{getQueryParamsForExpr as _,getDataRange as q,getAttributeBinsQuery as E,processQueryAttributeBinsResult as z,binParamsFromGenRend as V}from"./support/histogramUtils.js";import{ensureFeaturesJSON as L,getSummaryStatsQuery as T,getSummaryStatisticsFromFeatureSet as Q,getUVQuery as j,getUniqueValuesFromFeatureSet as b,defaultNumBins as N,updateQueryWithFeatureFilter as C,getHistogramFromFeatureSet as B,getFieldExpr as M,getDomainsForFields as R,getBins as I,getPredominantCategoriesFromUVInfos as $,getMissingFields as O}from"./support/utils.js";import{processSummaryStatisticsResult as k,createUVResult as A,getEqualIntervalBins as D,createClassBreaksDefinition as G,resolveCBResult as U}from"../../../statistics/utils.js";import{getScaleToResolutionFactor as P}from"../../../views/2d/viewpointUtils.js";const J=5,W=2e4,H=4e5;let Z=class extends x{constructor(){super(...arguments),this.adapterName="feature-layer-adapter"}_isStatsSupportedOnService(){const e=this.layer;if(!e.capabilities?.query?.supportsStatistics||"multipatch"===this.geometryType&&!n(e.url)&&e.version<10.5)throw new i(`${this.adapterName}:not-supported`,"Layer does not support statistics query");return Promise.resolve()}_fetchFeaturesFromService(e,t){return this.layer.queryFeatures(e,{signal:t}).then((e=>e.features))}_fetchFeaturesJSONFromService(e,t){return this._fetchFeaturesFromService(e,t).then(L)}_summaryStatsFromGenRend(e){const t=e.normalizationType,r=e.normalizationField;return this.classBreaks({field:e.field,numClasses:J,classificationMethod:"standard-deviation",standardDeviationInterval:.25,normalizationType:t,normalizationField:"field"===t?r:void 0,minValue:e.minValue,maxValue:e.maxValue,filter:e.filter,signal:e.signal}).then((t=>{let r,i,s;if(t.classBreakInfos?.some((e=>(e.hasAvg&&(r=e),!!r))),r){const e=r.maxValue-r.minValue;i=r.minValue+e/2,s=4*e}const a={min:t.minValue,max:t.maxValue,avg:i,stddev:s};return k(a,e.outStatisticTypes)}))}async _summaryStatsFromServiceQuery(e,t){await this._isStatsSupportedOnService(),"percent-of-total"===e.normalizationType&&(e.normalizationTotal=await this._getNormalizationTotal(e.field,e.normalizationType,e.filter));const r=w(t)||l(t),i="capabilities"in this.layer?this.layer.capabilities:null,s=T(this,e,t,i?.query?.supportsPercentileStatistics??!1),a=await this.layer.queryFeatures(s,{signal:e.signal}),o=Q(a,r);return k(o,e.outStatisticTypes)}_uvFromGenRenderer(e,t){const r=e.field??void 0,i=new h({attributeField:r}),s=new c({classificationDefinition:i});return this.generateRenderer(s,e.signal).then((e=>{const t={},i=this.getField(r);return e.uniqueValues.forEach((e=>{let r=e.value;null!=r&&""!==r&&("string"!=typeof r||""!==r.trim()&&"<null>"!==r.toLowerCase())||(r=null),null==t[r]?t[r]={count:e.count,data:u(i)&&r?Number(r):r}:t[r].count=t[r].count+e.count})),{count:t}})).then((r=>A(r,[t],e.returnAllCodedValues)))}async _uvFromServiceQuery(e,t){return this._isStatsSupportedOnService().then((()=>this.layer.queryFeatures(j(this,e),{signal:e.signal}))).then((t=>b(t,{layer:this,field:e.field,field2:e.field2,field3:e.field3,fieldDelimiter:v,view:e.view,signal:e.signal}))).then((r=>A(r,t,e.returnAllCodedValues,v)))}_getNormalizationTotal(e,t,r,i){return e&&"percent-of-total"===t?this.summaryStatistics({field:e,outStatisticTypes:{include:["sum"]},filter:r,signal:i}).then((e=>e.sum)):Promise.resolve(null)}_histogramForExpr(e){return this._getNormalizationTotal(e.field,e.normalizationType,e.filter,e.signal).then((t=>{const r=_(e,this,t);return q(r,this,e.minValue,e.maxValue).then((i=>{const s=i.min,a=i.max;if(null==s||null==a)return{bins:[],minValue:s,maxValue:a,normalizationTotal:t};const o=e.numBins||N,n=D(s,a,o),l=K(r.sqlExpression,n,null!=e.minValue&&null!=e.maxValue),u=new p({statisticType:"count",outStatisticFieldName:"countOFExpr",onStatisticField:"1"}),m=this.layer.createQuery();return m.where=f(m.where,r.sqlWhere),m.sqlFormat="standard",m.outStatistics=[u],m.groupByFieldsForStatistics=[l],m.orderByFields=[l],C(m,e.filter),this._isStatsSupportedOnService().then((()=>this.layer.queryFeatures(m,{signal:r.signal}))).then((e=>B(e,s,a,o,t)))}))}))}async _histogramFromQueryAttributeBins(e){const{field:t,normalizationType:r,filter:i,signal:s}=e,a=await this._getNormalizationTotal(t,r,i,s),o=this.layer.createQuery(),{query:n,min:l,max:u}=await E(e,this,a,o?.where);if(!n)return{bins:[],minValue:l,maxValue:u,normalizationTotal:a};const m=await this.layer.queryAttributeBins(n,{signal:s});return z(m,t?this.getField(t):null,{minValue:l,maxValue:u,normalizationTotal:a})}_classBreaksFromGenRend(e){const{field:t,normalizationType:r,normalizationField:i,normalizationTotal:s,signal:a}=e,o=S({field:t,normalizationType:r,normalizationField:i}),n=M({field:t,normalizationType:r,normalizationField:i,normalizationTotal:s,layer:this}),l=F(n,e.minValue,e.maxValue),u=G({field:t,normalizationType:r,normalizationField:i,classificationMethod:e.classificationMethod,standardDeviationInterval:e.standardDeviationInterval,breakCount:e.numClasses||J}),m=new c({classificationDefinition:u});return m.where=f(o,l),this.generateRenderer(m,a).then((t=>U(t,e.classificationMethod)))}async summaryStatistics(e){const{field:t,normalizationType:r,valueExpression:a,sqlExpression:o,view:n,features:u,useFeaturesInView:m}=e,c=t?this.getField(t):null,p=w(c)||l(c),h=a&&!(o&&this.supportsSQLExpression),d=this._hasLocalSource||u||m,y=n&&"3d"===n.type;if(d||h)return h||u||m||y?this._summaryStatsFromMemory(e,c):this._summaryStatsFromClientQuery(e,c);if(!this.supportsSQLExpression&&(p||o||"natural-log"===r||"square-root"===r))throw new i(`${this.adapterName}:not-supported`,"Layer does not support standardized SQL expression for queries");return(r&&!this.supportsSQLExpression?this._summaryStatsFromGenRend(e):this._summaryStatsFromServiceQuery(e,c)).catch((()=>(s(e.signal),this._summaryStatsFromMemory(e,c))))}async uniqueValues(e){const{valueExpression:t,sqlExpression:r,features:i,useFeaturesInView:a,signal:o}=e,n=t&&!(r&&this.supportsSQLExpression),l=this._hasLocalSource||i||a||n,u=e.view,m=u&&"3d"===u.type,c=await R(e,this);return l?n||i||a||m?this._uvFromMemory(e,c):this._uvFromClientQuery(e,c):this._uvFromServiceQuery(e,c).catch((t=>(s(o),!e.field||e.field2||e.field3||e.filter?t:this._uvFromGenRenderer(e,c[0])))).catch((()=>(s(o),m?this._uvFromMemory(e,c):this._uvFromClientQuery(e,c))))}async histogram(e){const{field:t,normalizationType:r,normalizationField:a,classificationMethod:o,view:n,filter:u,signal:m}=e,c=t?this.getField(t):null,p=w(c)||l(c),h=e.valueExpression||e.sqlExpression,d=e.valueExpression&&!(e.sqlExpression&&this.supportsSQLExpression),y=this._hasLocalSource||e.features||e.useFeaturesInView||d,f=this.supportsSQLExpression,S=!o||"equal-interval"===o,g=e.minValue,v=e.maxValue,x=null!=g&&null!=v,_=e.numBins||N;if(y)return this._histogramFromMemory(e);if(this.layer.capabilities?.operations?.supportsQueryBins&&e.useQueryAttributeBins)try{return await this._histogramFromQueryAttributeBins(e)}catch{return s(m),this._histogramFromQueryAttributeBinsFromMemory(e)}if((h||f)&&S){if(!f&&(h||"natural-log"===r||"square-root"===r))throw new i(`${this.adapterName}:not-supported`,"Layer does not support standardized SQL expression for queries");return this._histogramForExpr(e)}if(p&&S)throw new i(`${this.adapterName}:not-supported`,"Normalization and date field are not allowed when layer does not support standardized SQL expression for queries");return r||!S?V(e,this).then((s=>{if(!x)return I(this,s,t,_,n,u,m);if(g>s.max||v<s.min)throw new i(`${this.adapterName}:insufficient-data`,"Range defined by 'minValue' and 'maxValue' does not intersect available data range of the field");if(S)return I(this,{min:g,max:v,sqlExpr:s.sqlExpr,excludeZerosExpr:s.excludeZerosExpr},t,_,n,u,m);{const i={field:t,normalizationType:r,normalizationField:a,normalizationTotal:s.normTotal,layer:this},o=M(i),l=F(o,g,v);return V(e,this,l).then((e=>I(this,e,t,_,n,u,m)))}})):this._histogramForField(e)}async classBreaks(e){const t=!1!==e.analyzeData,r=this._hasLocalSource||e.features||e.useFeaturesInView||e.valueExpression||e.filter;if(t&&r)return this._classBreaksFromMemory(e);return(t?this._classBreaksFromGenRend(e):this._classBreaksFromInterpolation(e)).catch((()=>(s(e.signal),this._classBreaksFromMemory(e))))}async queryFeatureCount(e){if(this._hasLocalSource)throw new i(`${this.adapterName}:not-supported`,"Layer does not support count query");const t=this.layer,r=t.createQuery();return r.where=f(r.where,e.whereClause),C(r,e.filter),t.queryFeatureCount(r,{signal:e.signal})}async generateRenderer(e,t){const r=this.layer;if(this._hasLocalSource||r.version<10.1)throw new i(`${this.adapterName}:not-supported`,"Layer does not support generateRenderer operation (requires ArcGIS Server version 10.1+)");const s=r.createQuery();return e.where=f(e.where,s.where),m(r.parsedUrl?.path??"",{source:r.dynamicDataSource??void 0,gdbVersion:r.gdbVersion??void 0},e,{signal:t})}async predominantCategories(e){if(!this._hasLocalSource&&!this.supportsSQLExpression)throw new i(`${this.adapterName}:not-supported`,"Layer does not support advanced SQL expressions and standardized queries");const{fields:t,view:r,signal:s,filter:a}=e,o=d(t),n=y(t),l=r&&this._hasLocalSource?await this._uvFromMemory({valueExpression:o,view:r,signal:s,filter:a}):await this._uvFromServiceQuery({sqlExpression:n.expression,valueExpression:o,signal:s,filter:a});return $(l.uniqueValueInfos,t)}async getSampleFeatures(e,i){const{view:a,requiredFields:o,returnGeometry:n,filter:l,signal:u}=e,m=e.sampleSize;if(null==m||0===m)return[];const c=this.layer.createQuery(),p=1,h="json"===i;c.outSpatialReference=a?.spatialReference,c.returnGeometry=!!n,c.outFields=o,C(c,l);let d=[],y=!1;if(a)try{const s=await a.whenLayerView(this.layer);if(y=!O(this,o,s).length,y){if(m>=1&&!e.filter&&"getSampleFeatures"in s){await this._waitForLayerViewUpdate(s);const e=await s.getSampleFeatures({minFeatureCount:m,sampleSize:m});if(null!=e)return h?e:e.map((e=>t.fromJSON(e)))}if(d=await this._fetchFeaturesFromMemory(s,c,u,i),d.length>=m&&m>0)return r(d,m,p)}}catch(f){s(u)}try{if(this._hasLocalSource)return y?d:h?await this._fetchFeaturesJSONFromService(c,u):await this._fetchFeaturesFromService(c,u);const t=await this.queryFeatureCount({view:a,filter:l,signal:u}),i=this.layer.capabilities.query.maxRecordCount;let s=-1===m?t:m;if(s=i&&s>i?i:s,t<=d.length||d.length>=i)return d;if(c.maxAllowableOffset=e.resolution||(a?a.extent.width/a.width/a.scale:P(this.layer.spatialReference))*H,t<=s)return h?await this._fetchFeaturesJSONFromService(c,u):await this._fetchFeaturesFromService(c,u);if(t<=W){const e=this.layer.createQuery();C(e,l);const t=await this.layer.queryObjectIds();return c.objectIds=r(t,s,p),h?await this._fetchFeaturesJSONFromService(c,u):await this._fetchFeaturesFromService(c,u)}return this.layer.capabilities?.query?.supportsPagination&&(c.num=Math.min(s,W)),h?await this._fetchFeaturesJSONFromService(c,u):await this._fetchFeaturesFromService(c,u)}catch(f){return s(u),d}}load(e){const t=this.layer.load(e).then((async t=>{this.geometryType=t.geometryType,this.objectIdField=t.objectIdField,this.supportsSQLExpression=t.capabilities?.query?.supportsSqlExpression,this._hasLocalSource=!t.url&&!!t.source,this.hasQueryEngine=this._hasLocalSource,this.minScale=t.minScale,this.maxScale=t.maxScale,this.fullExtent=t.fullExtent,this.workerClient=g.getInstance(),await this.workerClient.open(e.signal)}));return this.addResolvingPromise(t),Promise.resolve(this)}};function K(e,t,r){const i=[],s=t.length;return t.forEach(((t,a)=>{const[o,n]=t;let l=null;l=0!==a||r?a!==s-1||r?f(`${e} >= ${o}`,`${e} ${a===s-1?" <= ":" < "} ${n}`):`${e} >= ${o}`:`${e} < ${n}`,i.push("WHEN ("+l+") THEN "+(a+1))})),["CASE",i.join(" "),"ELSE 0","END"].join(" ")}e([a({readOnly:!0})],Z.prototype,"adapterName",void 0),e([a({constructOnly:!0})],Z.prototype,"layer",void 0),Z=e([o("esri.smartMapping.support.adapters.FeatureLayerAdapter")],Z);export{Z as default};