@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 8.44 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 e from"./Graphic.js";import{ArcadeDate as t}from"./arcade/ArcadeDate.js";import r from"./arcade/ImmutableArray.js";import"./core/has.js";import{deprecated as a}from"./core/deprecate.js";import n from"./core/Error.js";import{clone as o}from"./core/lang.js";import c from"./core/Logger.js";import{DateOnly as s}from"./core/sql/DateOnly.js";import{TimeOnly as i}from"./core/sql/TimeOnly.js";import p from"./layers/FeatureLayer.js";import u from"./portal/Portal.js";import l from"./rest/support/FeatureSet.js";import{loadArcade as m}from"./support/loadArcade.js";let y=null;function f(e,t,a,n={}){const o=t.elementType,c="value",s="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 r(e.map((e=>{const t={};return g(t,s,{[c]:e},a,n),t[c]})))}function d(e,t,r={}){const a=e instanceof l?new p({source:e.features,geometryType:e.geometryType,fields:e.fields,spatialReference:e.spatialReference}):e;return t.constructFeatureSet(a,r.spatialReference,null,!0,r.lruCache,r.interceptor)}function b(e,t,r={}){const{spatialReference:a,interceptor:n,lruCache:o}=r;return"string"==typeof e?t.createFeatureSetCollectionFromService(e,a,o,n):t.createFeatureSetCollectionFromMap(e,a,o,n)}function v(e,t,r,a={}){const n=Object.create(null);return g(n,t.properties,e,r,a),new y.Dictionary(n)}function g(e,r,a,n,o={}){const c={};for(const t of Object.keys(a))c[t.toLowerCase()]=a[t];for(const p of r){const r=p.name.toLowerCase();if(o.variablesPreProcessed)e[r]=c[r];else switch(p.type){case"array":{const t=c[r];e[r]=null==t?null:f(t,p,n,o);break}case"feature":{const t=c[r];e[r]=null==t?null:y.Feature.createFromGraphic(t,o?.timeZone);break}case"featureSet":{const t=c[r];e[r]=null==t?null:n?d(t,n,o):null;break}case"featureSetCollection":{const t=c[r];e[r]=null==t?null:n?b(t,n,o):null;break}case"dictionary":{const t=c[r];e[r]=null==t?null:v(t,p,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 s?t:s.fromReader(t):null;break}case"time":{const t=c[r];e[r]=t?t instanceof i?t:i.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 y.Voxel(t,o?.timeZone);break}}}}function w(e,t){for(const r of e)t.push(r),"dictionary"===r.type&&w(r.properties,t);return t}function S(e,t,r,a,n){const{spatialReference:o,interceptor:c,lruCache:s,console:i,abortSignal:p,timeZone:l,services:m={portal:u.getDefault()}}=r,y={vars:{},spatialReference:o,interceptor:c,timeZone:l,lrucache:s,useAsync:n,services:m,console:i,abortSignal:p};return t?(g(y.vars,e.variables,t,a,r),y):y}function h(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]=h(e.field(t),r);return n}case"array":return("toArray"in t?t.toArray():t).map((e=>h(e,r)))}return t}const $={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"}]}]}]},x={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"}]}]}]},C={variables:[{name:"$voxel",type:"voxel"}]},j=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",$],["popup-element",$],["popup-feature-reduction",x],["popup-element-feature-reduction",x],["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",C],["popup-element-voxel",C]]);function A(e){"feature-reduction-popup"===e?(a(c.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&&(a(c.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=j.get(e);if(!t){const t=Array.from(j.keys()).map((e=>`'${e}'`)).join(", ");throw new n("createArcadeProfile:invalid-profile-name",`Invalid profile name '${e}'. You must specify one of the following values: ${t}`)}return o(t)}async function k(e,t,r={}){y||(y=await m());const{arcade:a,arcadeUtils:o,batchExec:{createBatchExecutor:c},aiServices:{BatchTranslationServiceFactory:s,PortalTranslationService:i}}=y,{loadScriptDependencies:p,referencesMember:l,scriptIsAsync:f}=a,d=w(t.variables,[]),b=d.filter((e=>"featureSet"===e.type||"featureSetCollection"===e.type)).map((e=>e.name.toLowerCase())),v=a.parseScript(e,b);if(!v)throw new n("arcade:invalid-script","Unable to create SyntaxTree");const g=o.extractFieldNames(v),$=a.scriptTouchesGeometry(v),x=d.map((e=>e.name.toLowerCase())).filter((e=>l(v,e))),C=f(v,b);await p(v,C,b);const j={vars:{},useAsync:C};for(const n of x)j.vars[n]="any";const{lruCache:A,services:k}=r,T=a.compileScript(v,j),F=a.featureSetUtils(),I=(e,r={})=>{if(C)throw new n("arcade:invalid-execution-mode","Cannot execute the script in synchronous mode");const a={...k,...r.services},c={lruCache:A,...r,services:a},s=T(S(t,e,c,F,C));return r.rawOutput?s:h(s,o)},O=async(e,r={})=>{const a={...k,...r.services},n={lruCache:A,...r,services:a},c=await T(S(t,e,n,F,C));return r.rawOutput?c:h(c,o)},D=async(e,r,a={})=>{const n=[],p=c(e,(async(e,c)=>{const s=r(e);try{const e={...l,translation:m.create(c)},r={lruCache:A,...a,services:e},i=await T(S(t,s,r,F,C)),p=a.rawOutput?i:h(i,o);n.push({id:c.id,result:{status:"fulfilled",value:p}})}catch(i){throw n.push({id:c.id,result:{status:"rejected",reason:i}}),i}}),a.maxConcurrency??64,a.abortSignal),l={...k,...a.services},m=new s(p,l.translation??new i(l.portal??u.getDefault(),a.console));return await p.run(),n.sort((({id:e},{id:t})=>e-t)).map((({result:e})=>e))};return{execute:I,executeAsync:O,executeAsyncBatch:D,isAsync:C,variablesUsed:x,fieldsUsed:g,geometryUsed:$,syntaxTree:v}}export{k as createArcadeExecutor,A as createArcadeProfile};