@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 7.24 kB
JavaScript
import e from"../../../../../core/Error.js";import t from"../../../../../core/Logger.js";import s from"../../../../../core/LRUCache.js";import{isSome as r}from"../../../../../core/maybe.js";import{createRendererExpression as i}from"../../../../../support/arcadeOnDemand.js";import a from"../../../arcade/callExpressionWithFeature.js";import{expandSymbol as n}from"../../../layers/support/cimSymbolUtils.js";const l=t.getLogger("esri/views/2d/engine/webgl/util/Matcher");async function o(e,t,s,r){switch(e.type){case"simple":case"heatmap":return u.fromBasicRenderer(e,t,s,r);case"map":return d.fromUVRenderer(e,t,s,r);case"interval":return m.fromCBRenderer(e,t,s,r);case"dictionary":return y.fromDictionaryRenderer(e,t,s,r);case"pie-chart":return c.fromPieChartRenderer(e,t,s,r);case"subtype":return c.fromSubtypes(e,t,s,r)}}class u{constructor(){this.type="feature",this._defaultResult=null}static async fromBasicRenderer(e,t,s,r){const i=new u;if(e.symbol){const a=await n(e.symbol,s,r),l=t.createTemplateGroup(a,null);i.setDefault(l)}return i}static async fromPieChartRenderer(e,t,s,r){const i=new u;if(e.markerSymbol){const a=await n(e.markerSymbol,s,r);let l;e.fillSymbol&&(l=await n(e.fillSymbol,s,r));const o=t.createTemplateGroup(a,l);i.setDefault(o)}return i}size(){return 1}getDefault(){return this._defaultResult}setDefault(e){this._defaultResult=e}match(e,t,s,r,i){return this.getDefault()}async analyze(e,t,s,r,i,a){return null}}class c extends u{constructor(e,t){super(),this._subMatchers=e,this._subtypeField=t}static async fromSubtypes(e,t,s,r){const i=new Map,a=[];for(const n in e.renderers){const l=parseInt(n,10),u=o(e.renderers[n],t,s,r).then((e=>i.set(l,e)));a.push(u)}return await Promise.all(a),new c(i,e.subtypeField)}match(e,t,s,r,i){const a=t.readAttribute(this._subtypeField),n=this._subMatchers.get(a);return n?n.match(e,t,s,r,i):null}}class m extends u{constructor(e,t,s,r){super(),this.type="interval",this._intervals=[],this._isMaxInclusive=t,this._fieldIndex=r,this._field=e,this._normalizationInfo=s}static async fromCBRenderer(e,t,s,r){const{isMaxInclusive:i,normalizationField:a,normalizationTotal:l,normalizationType:o}=e,u=e.field,c=new m(u,i,{normalizationField:a,normalizationTotal:l,normalizationType:o},e.fieldIndex),d=await n(e.backgroundFillSymbol,s,r);await Promise.all(e.intervals.map((async e=>{const i=await n(e.symbol,s,r),a=await t.createTemplateGroup(i,d),l={min:e.min,max:e.max};c.add(l,a)})));const h=await n(e.defaultSymbol,s,r);if(h){const e=await t.createTemplateGroup(h,d);c.setDefault(e)}return c}add(e,t){this._intervals.push({interval:e,result:t}),this._intervals.sort(((e,t)=>e.interval.min-t.interval.min))}size(){return super.size()+this._intervals.length}match(e,t,s,r,i){if(null==this._fieldIndex&&!this._field)return this.getDefault();const a=null!=this._fieldIndex?t.getComputedNumericAtIndex(this._fieldIndex):this._getValueFromField(t);if(null==a||isNaN(a)||a===1/0||a===-1/0)return this.getDefault();for(let n=0;n<this._intervals.length;n++){const{interval:e,result:t}=this._intervals[n],s=a>=e.min,r=this._isMaxInclusive?a<=e.max:a<e.max;if(s&&r)return t}return this.getDefault()}_needsNormalization(){const e=this._normalizationInfo;return e&&(e.normalizationField||e.normalizationTotal||e.normalizationType)}_getValueFromField(e){const t=e.readAttribute(this._field);if(!this._needsNormalization()||null==t)return t;const{normalizationField:s,normalizationTotal:r,normalizationType:i}=this._normalizationInfo,a=e.readAttribute(s)??1;if(i)switch(i){case"esriNormalizeByField":return a?t/a:void 0;case"esriNormalizeByLog":return Math.log(t)*Math.LOG10E;case"esriNormalizeByPercentOfTotal":return t/r*100;default:return void l.error(`Found unknown normalization type: ${i}`)}else l.error("Normalization is required, but no type was set!")}}class d extends u{constructor(e,t,s){super(),this.type="map",this._nullResult=null,this._resultsMap=new Map,this._fields=[],this._fieldsIndex=s,this._fields=e,this._seperator=t||""}static async fromUVRenderer(e,t,s,r){const i=e.fieldDelimiter,a=[e.field];e.field2&&a.push(e.field2),e.field3&&a.push(e.field3);const l=await n(e.backgroundFillSymbol,s,r),o=new d(a,i,e.fieldIndex);await Promise.all(e.map.map((async(e,i)=>{const a=await n(e.symbol,s,r),u=i+1,c=await t.createTemplateGroup(a,l,u);"<Null>"===e.value?o.setNullResult(c):o.add(e.value,c)})));const u=await n(e.defaultSymbol,s,r);if(u){const e=Number.MAX_SAFE_INTEGER,s=await t.createTemplateGroup(u,l,e);o.setDefault(s)}return o}setNullResult(e){this._nullResult=e}add(e,t){this._resultsMap.set(e.toString(),t)}size(){return super.size()+this._resultsMap.size}match(e,t,s,r,i){if(null==this._fieldsIndex&&!this._fields)return this.getDefault();const a=null!=this._fieldsIndex?t.getComputedStringAtIndex(this._fieldsIndex):this._getValueFromFields(t);if(null!==this._nullResult&&(null==a||""===a||"<Null>"===a))return this._nullResult;if(null==a)return this.getDefault();const n=a.toString();return this._resultsMap.has(n)?this._resultsMap.get(n):this.getDefault()}_getValueFromFields(e){const t=[];for(const s of this._fields){const r=e.readAttribute(s);null==r||""===r?t.push("<Null>"):t.push(r)}return t.join(this._seperator)}}async function h(e,t){const s=e||1;if("number"==typeof s)return(e,t,r)=>s;const r=await i(s,t.spatialReference,t.fields);return(e,s,i)=>a(r,e,{$view:i},t.geometryType,s)||1}let f;async function p(){return f||(f=import("../../../layers/features/createSymbolSchema.js")),f}class y extends u{constructor(e,t,r,i,a,n){super(),this.type="dictionary",this._groupIdCache=new s(100),this._loader=e,this._fieldMap=e.fieldMap,this._symbolFields=e.getSymbolFields(),this._templates=t,this._info=r,this._scaleFn=i,this._schemaUtilsModule=a,this._symbolOptions=n}static async fromDictionaryRenderer(e,t,s,r){const[{DictionaryLoader:i},a]=await Promise.all([import("../../../../../renderers/support/DictionaryLoader.js"),p()]),n=new i(e.url,e.config,e.fieldMap);await n.fetchResources({spatialReference:s.spatialReference,fields:s.fields});const l=await h(e.scaleExpression,s);return new y(n,t,s,l,a,e.symbolOptions)}async _analyzeFeature(t,s,r,i,a){const o=t.readLegacyFeature(),u=this._scaleFn(o,r,i),c=this._attributeHash(o)+"-"+u,m=this._groupIdCache.get(c);if(m)return m;const d={...i,spatialReference:this._info.spatialReference,abortOptions:a,fields:this._info.fields},h=await this._loader.getSymbolAsync(o,d),f=this._schemaUtilsModule.createSymbolSchema(h,this._symbolOptions),p=n(f,this._info,s,a).then((t=>{if("expanded-cim"!==t?.type)return l.error(new e("mapview-bad-type",`Found unexpected type ${t?.type} in dictionary response`)),null;t.hash+="-"+u;for(const e of t.layers)e.scaleFactor=u,e.templateHash+="-"+u;return this._templates.createTemplateGroup(t,null)}));return this._groupIdCache.put(c,p,1),p}async analyze(e,t,s,i,a,n){const l=t.getCursor(),o=[];for(;l.next();)o.push(this._analyzeFeature(l,s,i,a,n));return Promise.all(o).then((e=>e.filter(r)))}match(e,t,s,r,i){return null}_attributeHash(e){let t="";for(const s of this._symbolFields){const r=this._fieldMap?.[s];r&&(t+=e.attributes[r]+"-")}return t}}export{y as DictionaryMatcher,u as FeatureMatcher,m as IntervalMatcher,d as MapMatcher,c as SubtypeMatcher,o as createMatcher};