UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 9.36 kB
import e from"./Dictionary.js";import t from"./ImmutableArray.js";import{a as i,b as r,c as s,t as n,k as a,f as o,d as l}from"../chunks/languageUtils.js";import u from"../geometry/Geometry.js";import d from"../geometry/Point.js";import{fromJSON as f}from"../geometry/support/jsonUtils.js";import{convertToGeometry as c}from"../layers/graphics/featureConversionUtils.js";import{isSome as h,unwrap as m}from"../core/maybe.js";import{ArcadeDate as y}from"./ArcadeDate.js";import{ArcadeExecutionError as _,ExecutionErrorCodes as b}from"./executionError.js";import{DateTimeReferenceFieldIndex as p}from"./arcadeTimeUtils.js";class g{constructor(){this.arcadeDeclaredClass="esri.arcade.Feature",this._optimizedGeomDefinition=null,this._geometry=null,this.attributes=null,this._layer=null,this._datesfixed=!0,this.dateTimeReferenceFieldIndex=null,this.contextTimeReference=null,this.immutable=!0,this._datefields=null,this.immutable=!0}static createFromGraphic(e,t){const i=new g;return i.contextTimeReference=t??null,i._geometry=h(e.geometry)?e.geometry:null,void 0===e.attributes||null===e.attributes?i.attributes={}:i.attributes=e.attributes,e._sourceLayer?(i._layer=e._sourceLayer,i._datesfixed=!1):e._layer?(i._layer=e._layer,i._datesfixed=!1):e.layer&&"fields"in e.layer?(i._layer=e.layer,i._datesfixed=!1):e.sourceLayer&&"fields"in e.sourceLayer&&(i._layer=e.sourceLayer,i._datesfixed=!1),i._layer&&!1===i._datesfixed&&(void 0!==i._layer.dateTimeReferenceFieldIndex?i.dateTimeReferenceFieldIndex=i._layer.dateTimeReferenceFieldIndex:i.dateTimeReferenceFieldIndex=p.createFromLayer(i._layer)),i}static createFromArcadeFeature(e){const t=new g;return t._datesfixed=e._datesfixed,t.attributes=e.attributes,t._geometry=e._geometry,t._optimizedGeomDefinition=e._optimizedGeomDefinition,e._layer&&(t._layer=e._layer),t.dateTimeReferenceFieldIndex=e.dateTimeReferenceFieldIndex,t.contextTimeReference=e.contextTimeReference,t}static createFromOptimisedFeature(e,t,i){const r=new g;return r._geometry=e.geometry?{geometry:e.geometry}:null,r._optimizedGeomDefinition=i,r.attributes=e.attributes||{},r._layer=t,r._datesfixed=!1,r}static createFromArcadeDictionary(t){const i=new g;return i.attributes=t.field("attributes"),null!==i.attributes&&i.attributes instanceof e?(i.attributes=i.attributes.attributes,null===i.attributes&&(i.attributes={})):i.attributes={},i._geometry=t.field("geometry"),null!==i._geometry&&(i._geometry instanceof e?i._geometry=g.parseGeometryFromDictionary(i._geometry):i._geometry instanceof u||(i._geometry=null)),i}static createFromGraphicLikeObject(e,t,i=null,r){const s=new g;return s.contextTimeReference=r??null,null===t&&(t={}),s.attributes=t,s._geometry=h(e)?e:null,s._layer=i,s._layer&&(s._datesfixed=!1,void 0!==s._layer.dateTimeReferenceFieldIndex?s.dateTimeReferenceFieldIndex=s._layer.dateTimeReferenceFieldIndex:s.dateTimeReferenceFieldIndex=p.createFromLayer(s._layer)),s}repurposeFromGraphicLikeObject(e,t,i=null){null===t&&(t={}),this.attributes=t,this._geometry=e||null,this._layer=i,this._layer?this._datesfixed=!1:this._datesfixed=!0}get layerPreferredTimeZone(){return this.dateTimeReferenceFieldIndex?.layerPreferredTimeZone??""}fieldSourceTimeZone(e){return this.dateTimeReferenceFieldIndex?.fieldTimeZone(e)??""}castToText(e=!1){let a="";!1===this._datesfixed&&this._fixDates();for(const o in this.attributes){""!==a&&(a+=",");const l=this.attributes[o];null==l?a+=JSON.stringify(o)+":null":i(l)||r(l)||s(l)?a+=JSON.stringify(o)+":"+JSON.stringify(l):l instanceof u?a+=JSON.stringify(o)+":"+n(l):l instanceof t||l instanceof Array?a+=JSON.stringify(o)+":"+n(l,null,e):l instanceof y?a+=e?JSON.stringify(o)+":"+JSON.stringify(l.getTime()):JSON.stringify(o)+":"+l.stringify():null!==l&&"object"==typeof l&&void 0!==l.castToText&&(a+=JSON.stringify(o)+":"+l.castToText(e))}return'{"geometry":'+(null===this.geometry()?"null":n(this.geometry()))+',"attributes":{'+a+"}}"}_fixDates(){if(null!==this._datefields)return this._datefields.length>0&&this._fixDateFields(this._datefields),void(this._datesfixed=!0);const e=[],t=this._layer.fields;for(let i=0;i<t.length;i++){const r=t[i],s=r.type;"date"!==s&&"esriFieldTypeDate"!==s||e.push(r.name)}this._datefields=e,e.length>0&&this._fixDateFields(e),this._datesfixed=!0}isUnknownDateTimeField(e){return"unknown"===this.dateTimeReferenceFieldIndex?.fieldTimeZone(e)}_fixDateFields(e){this.attributes={...this.attributes};const t=this.contextTimeReference?.timeZone??"system";for(let i=0;i<e.length;i++){let r=this.attributes[e[i]];if(null===r);else if(void 0===r){for(const s in this.attributes)if(s.toLowerCase()===e[i].toLowerCase()){if(r=this.attributes[s],null!==r){const e=this.isUnknownDateTimeField(s);a(r)?this.attributes[s]=r:r instanceof Date?this.attributes[s]=e?y.unknownDateJSToArcadeDate(r):y.dateJSAndZoneToArcadeDate(r,t):this.attributes[s]=e?y.unknownEpochToArcadeDate(r):y.epochToArcadeDate(r,t)}break}}else{const s=this.isUnknownDateTimeField(e[i]);a(r)?this.attributes[e[i]]=r:r instanceof Date?this.attributes[e[i]]=s?y.unknownDateJSToArcadeDate(r):y.dateJSAndZoneToArcadeDate(r,t):this.attributes[e[i]]=s?y.unknownEpochToArcadeDate(r):y.epochToArcadeDate(r,t)}}}geometry(){return null===this._geometry||this._geometry instanceof u||(this._optimizedGeomDefinition?(this._geometry=m(f(c(this._geometry,this._optimizedGeomDefinition.geometryType,this._optimizedGeomDefinition.hasZ,this._optimizedGeomDefinition.hasM))),this._geometry.spatialReference=this._optimizedGeomDefinition.spatialReference):this._geometry=m(f(this._geometry))),this._geometry}field(e){!1===this._datesfixed&&this._fixDates();const t=this.attributes[e];if(void 0!==t)return t;const i=e.toLowerCase();for(const r in this.attributes)if(r.toLowerCase()===i)return this.attributes[r];if(this._hasFieldDefinition(i))return null;throw new _(null,b.FieldNotFound,null,{key:e})}_hasFieldDefinition(e){if(null===this._layer)return!1;for(let t=0;t<this._layer.fields.length;t++){if(this._layer.fields[t].name.toLowerCase()===e)return!0}return!1}setField(e,t){if(this.immutable)throw new _(null,b.Immutable,null);if(t instanceof Date&&(t=this.isUnknownDateTimeField(e)?y.unknownDateJSToArcadeDate(t):y.dateJSToArcadeDate(t)),!1===o(t))throw new _(null,b.TypeNotAllowedInFeature,null);const i=e.toLowerCase();if(void 0===this.attributes[e]){for(const e in this.attributes)if(e.toLowerCase()===i)return void(this.attributes[e]=t);this.attributes[e]=t}else this.attributes[e]=t}hasField(e){const t=e.toLowerCase();if(void 0!==this.attributes[e])return!0;for(const i in this.attributes)if(i.toLowerCase()===t)return!0;return!!this._hasFieldDefinition(t)}keys(){let e=[];const t={};for(const i in this.attributes)e.push(i),t[i.toLowerCase()]=1;if(null!==this._layer)for(let i=0;i<this._layer.fields.length;i++){const r=this._layer.fields[i];1!==t[r.name.toLowerCase()]&&e.push(r.name)}return e=e.sort(),e}static parseGeometryFromDictionary(e){const t=g._convertDictionaryToJson(e,!0);return void 0!==t.hasm&&(t.hasM=t.hasm,delete t.hasm),void 0!==t.hasz&&(t.hasZ=t.hasz,delete t.hasz),void 0!==t.spatialreference&&(t.spatialReference=t.spatialreference,delete t.spatialreference),void 0!==t.rings&&(t.rings=this._fixPathArrays(t.rings,!0===t.hasZ,!0===t.hasZ)),void 0!==t.paths&&(t.paths=this._fixPathArrays(t.paths,!0===t.hasZ,!0===t.hasM)),void 0!==t.points&&(t.points=this._fixPointArrays(t.points,!0===t.hasZ,!0===t.hasM)),f(t)}static _fixPathArrays(e,i,r){const s=[];if(e instanceof Array)for(let t=0;t<e.length;t++)s.push(this._fixPointArrays(e[t],i,r));else if(e instanceof t)for(let t=0;t<e.length();t++)s.push(this._fixPointArrays(e.get(t),i,r));return s}static _fixPointArrays(e,i,r){const s=[];if(e instanceof Array)for(let n=0;n<e.length;n++){const a=e[n];a instanceof d?i&&r?s.push([a.x,a.y,a.z,a.m]):i?s.push([a.x,a.y,a.z]):r?s.push([a.x,a.y,a.m]):s.push([a.x,a.y]):a instanceof t?s.push(a.toArray()):s.push(a)}else if(e instanceof t)for(let n=0;n<e.length();n++){const a=e.get(n);a instanceof d?i&&r?s.push([a.x,a.y,a.z,a.m]):i?s.push([a.x,a.y,a.z]):r?s.push([a.x,a.y,a.m]):s.push([a.x,a.y]):a instanceof t?s.push(a.toArray()):s.push(a)}return s}static _convertDictionaryToJson(t,i=!1){const r={};for(const s in t.attributes){let n=t.attributes[s];n instanceof e&&(n=g._convertDictionaryToJson(n)),i?r[s.toLowerCase()]=n:r[s]=n}return r}static parseAttributesFromDictionary(e){const t={};for(const i in e.attributes){const r=e.attributes[i];if(!o(r))throw new _(null,b.InvalidParameter,null);t[i]=r}return t}static fromJson(e,t){let n=null;null!==e.geometry&&void 0!==e.geometry&&(n=f(e.geometry));const o={};if(null!==e.attributes&&void 0!==e.attributes)for(const l in e.attributes){const t=e.attributes[l];if(null===t)o[l]=t;else{if(!(s(t)||r(t)||i(t)||a(t)))throw new _(null,b.InvalidParameter,null);o[l]=t}}return g.createFromGraphicLikeObject(n,o,null,t??null)}fullSchema(){return this._layer}gdbVersion(){if(null===this._layer)return"";const e=this._layer.gdbVersion;return void 0===e?"":""===e&&this._layer.capabilities&&this._layer.capabilities.isVersioned?"SDE.DEFAULT":e}castAsJson(e){const t={attributes:{},geometry:!0===e?.keepGeometryType?this.geometry():this.geometry()?.toJSON()??null};for(const i in this.attributes){const r=this.attributes[i];void 0!==r&&(t.attributes[i]=l(r,e))}return t}async castAsJsonAsync(e=null,t){return this.castAsJson(t)}}export{g as default};