UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 9.87 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import e from"./Graphic.js";import{ArcadeDate as t}from"./arcade/ArcadeDate.js";import{toSymbolId as r}from"./arcade/arcadeEnvironment.js";import a from"./arcade/ImmutableArray.js";import n from"./arcade/featureset/support/RecentlyUsedCache.js";import{deprecatedProperty as o,deprecated as c}from"./core/deprecate.js";import i from"./core/Error.js";import{clone as s}from"./core/lang.js";import p from"./core/Logger.js";import{DateOnly as u}from"./core/sql/DateOnly.js";import{TimeOnly as l}from"./core/sql/TimeOnly.js";import m from"./layers/FeatureLayer.js";import y from"./portal/Portal.js";import f from"./rest/support/FeatureSet.js";import{loadArcade as d}from"./support/loadArcade.js";let b=null;function v(e,t,r,n={}){const o=t.elementType,c="value",i="array"===o.type?[{name:c,type:o.type,elementType:o.elementType}]:"dictionary"===o.type?[{name:c,type:o.type,properties:o.properties}]:[{name:c,type:o.type}];return new a(e.map(e=>{const t={};return $(t,i,{[c]:e},r,n),t[c]}))}function g(e,t,r={}){const a=e instanceof f?new m({source:e.features,geometryType:e.geometryType,fields:e.fields,spatialReference:e.spatialReference}):e;return t.constructFeatureSet(a,r.spatialReference,null,!0,D(r.cache),r.interceptor)}function h(e,t,r={}){const{spatialReference:a,interceptor:n,cache:o}=r;return"string"==typeof e?t.createFeatureSetCollectionFromService(e,a,D(o),n):t.createFeatureSetCollectionFromMap(e,a,D(o),n)}function w(e,t,r,a={}){const n=Object.create(null);return $(n,t.properties,e,r,a),new b.Dictionary(n)}function S(e){if(null==e)return null;if("string"==typeof e||"number"==typeof e||"boolean"==typeof e)return e;if(Array.isArray(e))return new a(e.map(e=>S(e)));if("object"==typeof e){const t=Object.create(null);for(const r of Object.entries(e))t[r[0]]=S(r[1]);return new b.Dictionary(t)}throw new Error(`Unsupported json value: ${e}`)}function $(e,r,a,n,o={}){const c={};for(const t of Object.keys(a))c[t.toLowerCase()]=a[t];for(const i of r){const r=i.name.toLowerCase();if(o.variablesPreProcessed)e[r]=c[r];else switch(i.type){case"array":{const t=c[r];e[r]=null==t?null:v(t,i,n,o);break}case"feature":{const t=c[r];e[r]=null==t?null:b.Feature.createFromGraphic(t,o?.timeZone);break}case"featureSet":{const t=c[r];e[r]=null==t?null:n?g(t,n,o):null;break}case"featureSetCollection":{const t=c[r];e[r]=null==t?null:n?h(t,n,o):null;break}case"dictionary":{const t=c[r];e[r]=null==t?null:w(t,i,n,o);break}case"date":{const a=c[r];e[r]=a?a instanceof t?a:o?.timeZone?t.dateJSAndZoneToArcadeDate(a,o?.timeZone):t.dateJSToArcadeDate(a):null;break}case"dateOnly":{const t=c[r];e[r]=t?t instanceof u?t:u.fromReader(t):null;break}case"time":{const t=c[r];e[r]=t?t instanceof l?t:l.fromReader(t):null;break}case"knowledgeGraph":case"geometry":case"boolean":case"text":case"number":e[r]=c[r];break;case"voxel":{const t=c[r];e[r]=null==t?null:new b.Voxel(t,o?.timeZone);break}case"pixel":{const t=c[r];e[r]=null==t?null:b.Pixel.fromImageryGraphic(t,o?.timeZone);break}case"json":e[r]=S(c[r])}}}function x(e,t){for(const r of e)t.push(r),"dictionary"===r.type&&x(r.properties,t);return t}function C(e,t,r,a,n){null!=r.lruCache&&o(p.getLogger("esri.arcade"),"ExecuteContext#lruCache",{replacement:"ExecuteContext#cache",warnOnce:!0,version:"5.0"});const c={vars:{},spatialReference:r.spatialReference,interceptor:r.interceptor,timeZone:r.timeZone,lrucache:D(r.cache),useAsync:n,services:r.services,console:r.console,abortSignal:r.abortSignal,track:r.track};return t?($(c.vars,e.variables,t,a,r),c):c}function j(t,r){switch(r.getArcadeType(t)){case"number":case"text":case"boolean":case"point":case"polygon":case"polyline":case"multipoint":case"extent":return t;case"date":return t.toJSDate();case"time":case"dateOnly":return t.toString();case"feature":{const r=(t.type,t),a="geometry"in r?r.geometry():null,n="readAttributes"in r?r.readAttributes():r.attributes;return new e({geometry:a,attributes:n})}case"dictionary":{const e=t,a=e.attributes,n={};for(const t of Object.keys(a))n[t]=j(e.field(t),r);return n}case"array":return("toArray"in t?t.toArray():t).map(e=>j(e,r))}return t}const A={variables:[{name:"$feature",type:"feature"},{name:"$layer",type:"featureSet"},{name:"$datastore",type:"featureSetCollection"},{name:"$map",type:"featureSetCollection"},{name:"$userInput",type:"geometry"},{name:"$graph",type:"knowledgeGraph"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"},{name:"timeProperties",type:"dictionary",properties:[{name:"currentStart",type:"date"},{name:"currentEnd",type:"date"},{name:"startIncluded",type:"boolean"},{name:"endIncluded",type:"boolean"}]}]}]},O={variables:[{name:"$feature",type:"feature"},{name:"$aggregatedFeatures",type:"featureSet"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"},{name:"timeProperties",type:"dictionary",properties:[{name:"currentStart",type:"date"},{name:"currentEnd",type:"date"},{name:"startIncluded",type:"boolean"},{name:"endIncluded",type:"boolean"}]}]}]},k={variables:[{name:"$voxel",type:"voxel"}]},I={variables:[{name:"$pixel",type:"pixel"},{name:"$imageCollectionItem",type:"feature"},{name:"$userInput",type:"geometry"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"},{name:"timeProperties",type:"dictionary",properties:[{name:"currentStart",type:"date"},{name:"currentEnd",type:"date"},{name:"startIncluded",type:"boolean"},{name:"endIncluded",type:"boolean"}]}]}]},E=new Map([["aggregate-field",{variables:[{name:"$feature",type:"feature"}]}],["form-constraint",{variables:[{name:"$feature",type:"feature"}]}],["feature-z",{variables:[{name:"$feature",type:"feature"}]}],["field-calculation",{variables:[{name:"$feature",type:"feature"},{name:"$datastore",type:"featureSetCollection"}]}],["form-calculation",{variables:[{name:"$feature",type:"feature"},{name:"$originalFeature",type:"feature"},{name:"$layer",type:"featureSet"},{name:"$featureSet",type:"featureSet"},{name:"$datastore",type:"featureSetCollection"},{name:"$map",type:"featureSetCollection"},{name:"$editContext",type:"dictionary",properties:[{name:"editType",type:"text"}]}]}],["labeling",{variables:[{name:"$feature",type:"feature"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"},{name:"timeProperties",type:"dictionary",properties:[{name:"currentStart",type:"date"},{name:"currentEnd",type:"date"},{name:"startIncluded",type:"boolean"},{name:"endIncluded",type:"boolean"}]}]}]}],["popup",A],["popup-element",A],["popup-feature-reduction",O],["popup-element-feature-reduction",O],["visualization",{variables:[{name:"$feature",type:"feature"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"},{name:"timeProperties",type:"dictionary",properties:[{name:"currentStart",type:"date"},{name:"currentEnd",type:"date"},{name:"startIncluded",type:"boolean"},{name:"endIncluded",type:"boolean"}]}]}]}],["popup-voxel",k],["popup-element-voxel",k],["popup-imagery",I],["popup-element-imagery",I]]);function T(e){"feature-reduction-popup"===e?(c(p.getLogger("esri.arcade"),'profile name: "feature-reduction-popup"',{replacement:'"popup-feature-reduction"',version:"4.32",warnOnce:!0}),e="popup-feature-reduction"):"feature-reduction-popup-element"===e&&(c(p.getLogger("esri.arcade"),'profile name: "feature-reduction-popup-element"',{replacement:'"popup-element-feature-reduction"',version:"4.32",warnOnce:!0}),e="popup-element-feature-reduction");const t=E.get(e);if(!t){const t=Array.from(E.keys()).map(e=>`'${e}'`).join(", ");throw new i("createArcadeProfile:invalid-profile-name",`Invalid profile name '${e}'. You must specify one of the following values: ${t}`)}return s(t)}async function F(e,t,a={}){b||(b=await d());const{arcade:n,arcadeUtils:c,batchExec:{createBatchExecutor:s},aiServices:{BatchTranslationServiceFactory:u,PortalTranslationService:l}}=b,{loadScriptDependencies:m,referencesMember:f,scriptIsAsync:v}=n,g=x(t.variables,[]).filter(e=>"featureSet"===e.type||"featureSetCollection"===e.type).map(e=>r(e.name)),h=n.parseScript(e,g);if(!h)throw new i("arcade:invalid-script","Unable to create SyntaxTree");const w=c.extractFieldNames(h),S=n.scriptTouchesGeometry(h),$=t.variables.map(e=>r(e.name)).filter(e=>f(h,e)),A=v(h,g);await m(h,A,g);const O=n.compileScript(h,{vars:Object.fromEntries($.map(e=>[e,!0])),useAsync:A}),k=a.services,I=a.cache??a.lruCache;null!=a.lruCache&&o(p.getLogger("esri.arcade"),"ExecutorOptions#lruCache",{replacement:"ExecutorOptions#cache",warnOnce:!0,version:"5.0"});const E=n.featureSetUtils(),T=(e,r={})=>{if(A)throw new i("arcade:invalid-execution-mode","Cannot execute the script in synchronous mode");const a={...k,...r.services},n=r.cache??r.lruCache??I,o={...r,cache:n,services:a},s=O(C(t,e,o,E,A));return r.rawOutput?s:j(s,c)},F=async(e,r={})=>{const a={...k,...r.services},n=r.cache??r.lruCache??I,o={...r,cache:n,services:a},i=await O(C(t,e,o,E,A));return r.rawOutput?i:j(i,c)},P=async(e,r,a={})=>{const n=[],o=a.cache??a.lruCache??I,i=s(e,async(e,i)=>{const s=r(e);try{const e={...p,translation:m.create(i)},r={...a,cache:o,services:e},u=await O(C(t,s,r,E,A)),l=a.rawOutput?u:j(u,c);n.push({id:i.id,result:{status:"fulfilled",value:l}})}catch(u){throw n.push({id:i.id,result:{status:"rejected",reason:u}}),u}},a.maxConcurrency??64,a.abortSignal),p={...k,...a.services},m=new u(i,p.translation??new l(p.portal??y.getDefault(),a.console));return await i.run(),n.sort(({id:e},{id:t})=>e-t).map(({result:e})=>e)};return{execute:T,executeAsync:F,executeAsyncBatch:P,isAsync:A,variablesUsed:$,fieldsUsed:w,geometryUsed:S,syntaxTree:h}}function P(){return new n}function D(e){if(null!=e&&!(e instanceof n))throw new Error("Use createArcadeCache() to construct a cache instance or provide null/undefined.");return e}export{P as createArcadeCache,F as createArcadeExecutor,T as createArcadeProfile};