@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 14.5 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import t from"../../core/Error.js";import e from"../../core/Logger.js";import{assertIsSome as o}from"../../core/maybe.js";import{is as n,fromRectValues as r}from"../../geometry/support/aaBoundingBox.js";import{fromValues as s}from"../../geometry/support/aaBoundingRect.js";import{isPoint as u,isPolygon as l,isPolyline as c,isMultipoint as i}from"../../geometry/support/jsonUtils.js";import{OptimizedFeature as f}from"./OptimizedFeature.js";import a from"./OptimizedFeatureSet.js";import h from"./OptimizedGeometry.js";function d(t,e){return t?e?4:3:e?3:2}const m=()=>e.getLogger("esri.layers.graphics.featureConversionUtils"),g={esriGeometryPoint:0,esriGeometryPolyline:2,esriGeometryPolygon:3,esriGeometryMultipoint:0,esriGeometryMultiPatch:3,esriGeometryEnvelope:0},y=(t,e,o,n,r,s)=>{t[o]=r,t[o+1]=s},p=(t,e,o,n,r,s)=>{t[o]=r,t[o+1]=s,t[o+2]=e[n+2]},I=(t,e,o,n,r,s)=>{t[o]=r,t[o+1]=s,t[o+2]=e[n+3]},M=(t,e,o,n,r,s)=>{t[o]=r,t[o+1]=s,t[o+2]=e[n+2],t[o+3]=e[n+3]};function b(t,e,o,n){if(t){if(o)return e&&n?M:p;if(e&&n)return I}else if(e&&n)return p;return y}function w({scale:t,translate:e},o){return Math.round((o-e[0])/t[0])}function N({scale:t,translate:e},o){return Math.round((e[1]-o)/t[1])}function G({scale:t,translate:e},o){return Math.round((o-e[0])/t[0])}function T({scale:t,translate:e},o){return Math.round((o-e[1])/t[1])}function F(t,e){return x(t,e,0)}function P(t,e){return x(t,-e,1)}function x({scale:t,translate:e},o,n){return o*t[n]+e[n]}function j(t,e,o){return t?e?o?Y(t):v(t):o?k(t):Z(t):null}function Z(t){const e=t.coords;return{x:e[0],y:e[1]}}function E(t,e){return t.coords[0]=e.x,t.coords[1]=e.y,t}function v(t){const e=t.coords;return{x:e[0],y:e[1],z:e[2]}}function z(t,e){return t.coords[0]=e.x,t.coords[1]=e.y,t.coords[2]=e.z,t}function k(t){const e=t.coords;return{x:e[0],y:e[1],m:e[2]}}function V(t,e){return t.coords[0]=e.x,t.coords[1]=e.y,t.coords[2]=e.m,t}function Y(t){const e=t.coords;return{x:e[0],y:e[1],z:e[2],m:e[3]}}function _(t,e){return t.coords[0]=e.x,t.coords[1]=e.y,t.coords[2]=e.z,t.coords[3]=e.m,t}function L(t,e,o,n){let r=Z;o&&n?r=Y:o?r=v:n&&(r=k);for(const s of e){const{geometry:e,attributes:o}=s,n=null!=e?r(e):null;t.push({attributes:o,geometry:n})}return t}function O(t,e){return t&&e?_:t?z:e?V:E}function S(t,e,o,n,r){const s=O(o,n);for(const{geometry:u,attributes:l}of e){const e=null!=u?s(new h,u):null;t.push(new f(e,l,null,r?l[r]:void 0))}return t}function U(t,e,o=O(null!=e.z,null!=e.m)){return o(t,e)}function q(t,e,o,n){for(const{geometry:r,attributes:s}of e)t.push({attributes:s,geometry:null!=r?R(r,o,n):null});return t}function R(t,e,o){if(null==t)return null;const n=d(e,o),r=[];for(let s=0;s<t.coords.length;s+=n){const e=[];for(let o=0;o<n;o++)e.push(t.coords[s+o]);r.push(e)}return e?o?{points:r,hasZ:e,hasM:o}:{points:r,hasZ:e}:o?{points:r,hasM:o}:{points:r}}function A(t,e,o,n,r){const s=d(o,n);for(const{geometry:u,attributes:l}of e){const e=null!=u?$(new h,u,s):null;t.push(new f(e,l,null,r?l[r]:void 0))}return t}function $(t,e,o=d(e.hasZ,e.hasM)){t.lengths[0]=e.points.length;const n=t.coords;let r=0;for(const s of e.points)for(let t=0;t<o;t++)n[r++]=s[t];return t}function B(t,e,o,n){for(const{geometry:r,attributes:s}of e)t.push({attributes:s,geometry:null!=r?C(r,o,n):null});return t}function C(t,e,o){if(!t)return null;const n=d(e,o),{coords:r,lengths:s}=t,u=[];let l=0;for(const c of s){const t=[];for(let e=0;e<c;e++){const e=[];for(let t=0;t<n;t++)e.push(r[l++]);t.push(e)}u.push(t)}return e?o?{paths:u,hasZ:e,hasM:o}:{paths:u,hasZ:e}:o?{paths:u,hasM:o}:{paths:u}}function D(t,e,o,n,r){const s=d(o,n);for(const{geometry:u,attributes:l,centroid:c}of e){const e=null!=u?H(new h,u,s):null,o=null!=c?U(new h,c):null;t.push(new f(e,l,o,r?l[r]:void 0))}return t}function H(t,e,o=d(e.hasZ,e.hasM)){const{lengths:n,coords:r}=t;let s=0;for(const u of e.paths){for(const t of u)for(let e=0;e<o;e++)r[s++]=t[e];n.push(u.length)}return t}function J(t,e,o,n){for(const{geometry:r,attributes:s,centroid:u}of e){const e=null!=r?K(r,o,n):null;if(null!=u){const o=Z(u);t.push({attributes:s,centroid:o,geometry:e})}else t.push({attributes:s,geometry:e})}return t}function K(t,e,o){if(!t)return null;const n=d(e,o),{coords:r,lengths:s}=t,u=[];let l=0;for(const c of s){const t=[];for(let e=0;e<c;e++){const e=[];for(let t=0;t<n;t++)e.push(r[l++]);t.push(e)}u.push(t)}return e?o?{rings:u,hasZ:e,hasM:o}:{rings:u,hasZ:e}:o?{rings:u,hasM:o}:{rings:u}}function Q(t,e,o,n,r){for(const{geometry:s,centroid:u,attributes:l}of e){const e=null!=s?W(new h,s,o,n):null,c=r?l[r]:void 0;null!=u?t.push(new f(e,l,E(new h,u),c)):t.push(new f(e,l,null,c))}return t}function W(t,e,o=e.hasZ,n=e.hasM){return X(t,e.rings,o,n)}function X(t,e,o,n){const r=d(o,n),{lengths:s,coords:u}=t;let l=0;Pt(t);for(const c of e){for(const t of c)for(let e=0;e<r;e++)u[l++]=t[e];s.push(c.length)}return t}const tt=[],et=[];function ot(t,e,o,n,r){tt[0]=t;const[s]=nt(et,tt,e,o,n,r);return xt(tt),xt(et),s}function nt(e,o,n,r,s,u){if(xt(e),!n){for(const t of o){const o=u?t.attributes[u]:void 0;e.push(new f(null,t.attributes,null,o))}return e}switch(n){case"esriGeometryPoint":return S(e,o,r,s,u);case"esriGeometryMultipoint":return A(e,o,r,s,u);case"esriGeometryPolyline":return D(e,o,r,s,u);case"esriGeometryPolygon":case"esriGeometryMultiPatch":return Q(e,o,r,s,u);default:m().error("convertToFeatureSet:unknown-geometry",new t(`Unable to parse unknown geometry type '${n}'`)),xt(e)}return e}function rt(t,e,o,n){et[0]=t,ct(tt,et,e,o,n);const r=tt[0];return xt(tt),xt(et),r}function st(e,o,n){if(null==e)return null;const r=new h;if("hasZ"in e&&null==o&&(o=e.hasZ),"hasM"in e&&null==n&&(n=e.hasM),u(e)){return O(null!=o?o:null!=e.z,null!=n?n:null!=e.m)(r,e)}return l(e)?W(r,e,o,n):c(e)?H(r,e,d(o,n)):i(e)?$(r,e,d(o,n)):void m().error("convertFromGeometry:unknown-geometry",new t(`Unable to parse unknown geometry type '${e}'`))}function ut(e,o,n,r){const s=e&&("coords"in e?e:e.geometry);if(null==s)return null;switch(o){case"esriGeometryPoint":{let t=Z;return n&&r?t=Y:n?t=v:r&&(t=k),t(s)}case"esriGeometryMultipoint":return R(s,n,r);case"esriGeometryPolyline":return C(s,n,r);case"esriGeometryPolygon":return K(s,n,r);default:return m().error("convertToGeometry:unknown-geometry",new t(`Unable to parse unknown geometry type '${o}'`)),null}}function lt(t,e){for(const o of e)t.push({attributes:o.attributes});return t}function ct(e,o,n,r,s){if(xt(e),null==n)return lt(e,o);switch(n){case"esriGeometryPoint":return L(e,o,r,s);case"esriGeometryMultipoint":return q(e,o,r,s);case"esriGeometryPolyline":return B(e,o,r,s);case"esriGeometryPolygon":return J(e,o,r,s);default:m().error("convertToFeatureSet:unknown-geometry",new t(`Unable to parse unknown geometry type '${n}'`))}return e}function it(t){const{objectIdFieldName:e,spatialReference:o,transform:n,fields:r,hasM:s,hasZ:u,features:l,geometryType:c,exceededTransferLimit:i,uniqueIdField:f,queryGeometry:a,queryGeometryType:h}=t,d={features:ct([],l,c,u,s),fields:r,geometryType:c,objectIdFieldName:e,spatialReference:o,uniqueIdField:f,queryGeometry:ut(a,h,!1,!1)};return n&&(d.transform=n),i&&(d.exceededTransferLimit=i),s&&(d.hasM=s),u&&(d.hasZ=u),d}function ft(e,o){const n=new a,{hasM:r,hasZ:s,features:u,objectIdFieldName:l,spatialReference:c,geometryType:i,exceededTransferLimit:f,transform:h,fields:d}=e;return d&&(n.fields=d),n.geometryType=i??null,n.objectIdFieldName=l??o??null,n.spatialReference=c??null,n.objectIdFieldName?(u&&nt(n.features,u,i,s,r,n.objectIdFieldName),f&&(n.exceededTransferLimit=f),r&&(n.hasM=r),s&&(n.hasZ=s),h&&(n.transform=h),n):(m().error(new t("optimized-features:invalid-objectIdFieldName","objectIdFieldName is missing")),n)}function at(t){const{transform:e,features:o,hasM:n,hasZ:r}=t;if(!e)return t;for(const s of o)null!=s.geometry&&Mt(s.geometry,s.geometry,n,r,e),null!=s.centroid&&Mt(s.centroid,s.centroid,n,r,e);return t.transform=null,t}function ht(t,e){const{geometryType:o,features:n,hasM:r,hasZ:s}=e;if(!t)return e;for(let u=0;u<n.length;u++){const e=n[u],l=e.weakClone();l.geometry=new h,dt(l.geometry,e.geometry,r,s,o,t),e.centroid&&(l.centroid=new h,dt(l.centroid,e.centroid,r,s,"esriGeometryPoint",t)),n[u]=l}return e.transform=t,e}function dt(t,e,o,n,r,s,u=o,l=n){if(Pt(t),!e?.coords.length)return null;const c=g[r],{coords:i,lengths:f}=e,a=d(o,n),h=d(o&&u,n&&l),m=b(o,n,u,l);if(!f.length)return m(t.coords,i,0,0,w(s,i[0]),N(s,i[1])),Pt(t,a,0),t;let y,p,I,M,G=0,T=0,F=T;for(const d of f){if(d<c)continue;let e=0;T=F,I=y=w(s,i[G]),M=p=N(s,i[G+1]),m(t.coords,i,T,G,I,M),e++,G+=a,T+=h;for(let o=1;o<d;o++,G+=a)I=w(s,i[G]),M=N(s,i[G+1]),I===y&&M===p||(m(t.coords,i,T,G,I-y,M-p),T+=h,e++,y=I,p=M);e>=c&&(t.lengths.push(e),F=T)}return xt(t.coords,F),t.coords.length?t:null}function mt(t,e,o,n,r,s,u=o,l=n){if(Pt(t),!e?.coords.length)return null;const c=g[r],{coords:i,lengths:f}=e,a=d(o,n),h=d(o&&u,n&&l),m=b(o,n,u,l);if(!f.length)return m(t.coords,i,0,0,i[0],i[1]),Pt(t,a,0),t;let y=0;const p=s*s;for(const d of f){if(d<c){y+=d*a;continue}const e=t.coords.length/h,o=y,n=y+(d-1)*a;m(t.coords,i,t.coords.length,o,i[o],i[o+1]),yt(t.coords,i,a,p,m,o,n),m(t.coords,i,t.coords.length,n,i[n],i[n+1]);const r=t.coords.length/h-e;r>=c?t.lengths.push(r):xt(t.coords,e*h),y+=d*a}return t.coords.length?t:null}function gt(t,e,o,n){const r=t[e],s=t[e+1],u=t[o],l=t[o+1],c=t[n],i=t[n+1];let f=u,a=l,h=c-f,d=i-a;if(0!==h||0!==d){const t=((r-f)*h+(s-a)*d)/(h*h+d*d);t>1?(f=c,a=i):t>0&&(f+=h*t,a+=d*t)}return h=r-f,d=s-a,h*h+d*d}function yt(t,e,o,n,r,s,u){let l,c=n,i=0;for(let f=s+o;f<u;f+=o)l=gt(e,f,s,u),l>c&&(i=f,c=l);c>n&&(i-s>o&&yt(t,e,o,n,r,s,i),r(t,e,t.length,i,e[i],e[i+1]),u-i>o&&yt(t,e,o,n,r,i,u))}function pt(t,e,o,u){if(!e?.coords?.length)return null;const l=d(o,u);let c=Number.POSITIVE_INFINITY,i=Number.POSITIVE_INFINITY,f=Number.NEGATIVE_INFINITY,a=Number.NEGATIVE_INFINITY;if(e&&e.coords){const t=e.coords;for(let e=0;e<t.length;e+=l){const o=t[e],n=t[e+1];c=Math.min(c,o),f=Math.max(f,o),i=Math.min(i,n),a=Math.max(a,n)}}return n(t)?r(t,c,i,f,a):s(c,i,f,a,t),t}function It(t,e,o,n){const r=d(o,n),{lengths:s,coords:u}=e;let l=Number.POSITIVE_INFINITY,c=Number.POSITIVE_INFINITY,i=Number.NEGATIVE_INFINITY,f=Number.NEGATIVE_INFINITY,a=0;for(const h of s){let t=u[a],e=u[a+1];l=Math.min(t,l),c=Math.min(e,c),i=Math.max(t,i),f=Math.max(e,f),a+=r;for(let o=1;o<h;o++,a+=r){const o=u[a],n=u[a+1];t+=o,e+=n,o<0&&(l=Math.min(l,t)),o>0&&(i=Math.max(i,t)),n<0?c=Math.min(c,e):n>0&&(f=Math.max(f,e))}}return t[0]=l,t[1]=c,t[2]=i,t[3]=f,t}function Mt(t,e,n,r,s){const{coords:u,lengths:l}=e,c=d(n,r);if(!u.length)return t!==e&&Pt(t),t;o(s);const{originPosition:i,scale:f,translate:a}=s,h=jt;h.originPosition=i;const m=h.scale;m[0]=f[0]??1,m[1]=-(f[1]??1),m[2]=f[2]??1,m[3]=f[3]??1;const g=h.translate;if(g[0]=a[0]??0,g[1]=a[1]??0,g[2]=a[2]??0,g[3]=a[3]??0,!l.length){for(let e=0;e<c;++e)t.coords[e]=x(h,u[e],e);return t!==e&&Pt(t,c,0),t}let y=0;for(let o=0;o<l.length;o++){const e=l[o];t.lengths[o]=e;for(let o=0;o<c;++o)t.coords[y+o]=x(h,u[y+o],o);let n=t.coords[y],r=t.coords[y+1];y+=c;for(let o=1;o<e;o++,y+=c){n+=u[y]*m[0],r+=u[y+1]*m[1],t.coords[y]=n,t.coords[y+1]=r;for(let e=2;e<c;++e)t.coords[y+e]=x(h,u[y+e],e)}}return t!==e&&Pt(t,u.length,l.length),t}function bt(t,e,o,n,r,s){if(Pt(t),t.lengths.push(...e.lengths),o===r&&n===s)for(let u=0;u<e.coords.length;u++)t.coords.push(e.coords[u]);else{const u=d(o,n),l=b(o,n,r,s),c=e.coords;for(let e=0;e<c.length;e+=u)l(t.coords,c,t.coords.length,e,c[e],c[e+1])}return t}function wt(t,e,o,n,r){if(!e?.coords?.length)return null;const s=g[o],{coords:u,lengths:l}=e,c=b(n,r,n,r),i=d(n,r);let f=0,a=0,h=0,m=0;for(const d of l){a=m,c(t.coords,u,a,f,u[f],u[f+1]),f+=i;let e=u[f],o=u[f+1],n=e,r=o,l=o/e;a+=i,c(t.coords,u,a,f,n,r),f+=i;for(let s=2;s<d;s++){e=u[f],o=u[f+1];const s=o/e,h=l===s||!isFinite(l)&&!isFinite(s),d=h&&isFinite(s)?l>=0&&s>=0||l<=0&&s<=0:r>=0&&o>=0||r<=0&&o<=0;h&&d?(n+=e,r+=o):(n=e,r=o,a+=i),c(t.coords,u,a,f,n,r),f+=i,l=s}a+=i;const g=(a-m)/i;g>=s&&(t.lengths[h]=g,m=a,h++)}return t.coords.length>m&&(t.coords.length=m),t.lengths.length>h&&(t.lengths.length=h),t.coords.length&&t.lengths.length?t:null}function Nt(t,e,o,n){let r=0,s=t[n*e],u=t[n*(e+1)];for(let l=1;l<o;l++){const o=s+t[n*(e+l)],c=u+t[n*(e+l)+1],i=(o-s)*(c+u);s=o,u=c,r+=i}return.5*r}function Gt(t,e){const{coords:o,lengths:n}=t;let r=0,s=0;for(let u=0;u<n.length;u++){const t=n[u];s+=Nt(o,r,t,e),r+=t}return Math.abs(s)}function Tt(t,e,o,n){return 0===t*n-o*e&&t*o+e*n>0}function Ft(t,e,o,n,r){const s=d(n,r);if(!t.lengths.length){if(t.coords.length<2)return null;const[o,n]=t.coords,r=G(e,o),s=T(e,n);return new h([],[r,s])}const u=new h([],[0,0]),l=g[o],c="esriGeometryPolygon"===o||"esriGeometryPolyline"===o;let i=0,f=0;for(let a=0;a<t.lengths.length;a++){const o=t.lengths[a],n=f;let r=G(e,t.coords[s*i]),h=T(e,t.coords[s*i+1]);u.coords[f++]=r,u.coords[f++]=h;let d=0,m=0,g=1;for(let l=1;l<o;l++){const o=G(e,t.coords[s*(l+i)]),n=T(e,t.coords[s*(l+i)+1]);if(o!==r||n!==h){const t=o-r,e=n-h;c&&Tt(d,m,t,e)?(u.coords[f-2]+=t,u.coords[f-1]+=e,r+=t,h+=e):(u.coords[f++]=o,u.coords[f++]=n,r=o,h=n,d=t,m=e,g+=1)}}g<l?f=n:u.lengths.push(g),i+=o}return 0===u.lengths.length?null:u}function Pt(t,e=0,o=0){xt(t.lengths,o),xt(t.coords,e)}function xt(t,e=0){t.length!==e&&(t.length=e)}const jt={originPosition:"lowerLeft",scale:[1,1,1,1],translate:[0,0,0,0]};export{ot as convertFromFeature,ft as convertFromFeatureSet,nt as convertFromFeatures,st as convertFromGeometry,$ as convertFromMultipoint,A as convertFromMultipointFeatures,X as convertFromNestedArray,U as convertFromPoint,S as convertFromPointFeatures,W as convertFromPolygon,H as convertFromPolyline,D as convertFromPolylineFeatures,rt as convertToFeature,it as convertToFeatureSet,ct as convertToFeatures,ut as convertToGeometry,R as convertToMultipoint,q as convertToMultipointFeatures,j as convertToPoint,K as convertToPolygon,C as convertToPolyline,mt as generalizeOptimizedGeometry,pt as getBoundsOptimizedGeometry,Gt as getQuantizedArea,It as getQuantizedBoundsOptimizedGeometry,Nt as getSignedQuantizedRingArea,Ft as quantizeForDisplay,ht as quantizeOptimizedFeatureSet,dt as quantizeOptimizedGeometry,w as quantizeX,N as quantizeY,wt as removeCollinearVertices,bt as removeZMValues,at as unquantizeOptimizedFeatureSet,Mt as unquantizeOptimizedGeometry,x as unquantizeValue,F as unquantizeX,P as unquantizeY};