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