featureConversionUtils.js 14 KB

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