@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 15.3 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__addDisposableResource as t,__disposeResources as e}from"tslib";import{baseTypedSubArrayMemory as r}from"../core/memoryEstimations.js";import s from"./SpatialReference.js";import{empty as n,expandWithExtent as i}from"./support/aaBoundingBox.js";import{empty as a,expandExtentInPlace as o}from"./support/aaBoundingRect.js";import{isPoint as u,isMultipoint as h,isPolyline as l,isGeometryWithM as c,isGeometryWithZ as g}from"./support/jsonTypeUtils.js";import{equals as f}from"./support/spatialReferenceUtils.js";import{interpolateCircle as m}from"./support/curves/circleUtils.js";import{deriveCircleFromCircularArc as p}from"./support/curves/circularArcUtils.js";import{curveExtent as x}from"./support/curves/curveExtent.js";import{getEndpoint as v,isCurvedGeometry as d,isEllipticArc7 as y,isEllipticArc4 as C,isCircularArc as M,isBezierCurve as E}from"./support/curves/curveUtils.js";import{deriveCircleFromEllipticArc4 as O}from"./support/curves/ellipticArc4Utils.js";import{deriveEllipse as S,interpolateEllipse as F}from"./support/curves/ellipticArc7Utils.js";import P from"../layers/graphics/OptimizedGeometry.js";class _{constructor({type:t,spatialReference:e,vertexCount:r,vertexXY:s,vertexZ:n,vertexM:i,partCount:a,partOffsets:o,partFlags:u,segmentFlags:h,segmentIndices:l,segmentParams:c,segmentCountArc:g,segmentCountBezier:f}){this.segmentCountArc=0,this.segmentCountBezier=0,this.type=t,this.spatialReference=e,this.vertexCount=r,this.vertexXY=s,this.vertexZ=n,this.vertexM=i,this.partCount=a,this.partOffsets=o,this.partFlags=u,this.segmentFlags=h,this.segmentIndices=l,this.segmentParams=c,this.segmentCountArc=g,this.segmentCountBezier=f}get segmentParamCount(){return 10*this.segmentCountArc+4*this.segmentCountBezier}get segmentCountLine(){const{segmentCount:t,segmentCountArc:e,segmentCountBezier:r}=this;return t&&t-e-r}get segmentCount(){return"polyline"===this.type?this.vertexCount-this.partCount:"polygon"===this.type?this.vertexCount:0}get hasZ(){return null!=this.vertexZ}get hasM(){return null!=this.vertexM}hasCurves(){return null!=this.segmentFlags}isMultipart(){return"polyline"===this.type||"polygon"===this.type}get usedMemory(){function t(t){return t?r+t.byteLength:0}return t(this.vertexXY)+t(this.vertexZ)+t(this.vertexM)+t(this.partOffsets)+t(this.partFlags)+t(this.segmentFlags)+t(this.segmentIndices)+t(this.segmentParams)}clone(){const{type:t,vertexCount:e,partCount:r,hasZ:s,hasM:n,segmentCountArc:i,segmentCountBezier:a}=this,o=U(t,e,r,s,n,this.hasCurves(),i,a);o.spatialReference=this.spatialReference;const{vertexXY:u,vertexZ:h,vertexM:l}=this;if(o.vertexXY.set(u.subarray(0,2*e),0),o.vertexZ?.set(h.subarray(0,e),0),o.vertexM?.set(l.subarray(0,e),0),this.isMultipart()){const{partOffsets:t,partFlags:e}=this;o.partFlags.set(e.subarray(0,r+1)),o.partOffsets.set(t.subarray(0,r+1))}if(this.hasCurves()){const{segmentFlags:t,segmentIndices:r,segmentParams:s,segmentParamCount:n}=this;o.segmentFlags.set(t.subarray(0,e),0),o.segmentIndices.set(r.subarray(0,e),0),o.segmentParams.set(s.subarray(0,n),0)}return o}equals(t){return this.equalsWithinTolerance(t,0)}equalsWithinTolerance(t,e){if(null==t)return!1;if(this===t)return!0;if(!f(this.spatialReference,t.spatialReference))return!1;if(this.vertexCount!==t.vertexCount||this.partCount!==t.partCount)return!1;if(this.hasZ!==t.hasZ||this.hasM!==t.hasM)return!1;if(this.hasCurves()!==t.hasCurves()||this.segmentCountArc!==t.segmentCountArc||this.segmentCountBezier!==t.segmentCountBezier)return!1;const r=0===e?(t,e)=>t!==e||isNaN(t)!==isNaN(e):(t,r)=>Math.abs(t-r)>e||isNaN(t)!==isNaN(r);for(let s=0;s<this.vertexCount;++s)if(r(this.vertexXY[2*s],t.vertexXY[2*s])||r(this.vertexXY[2*s+1],t.vertexXY[2*s+1]))return!1;if(this.hasZ)for(let s=0;s<this.vertexCount;++s)if(r(this.vertexZ[s],t.vertexZ[s]))return!1;if(this.hasM)for(let s=0;s<this.vertexCount;++s)if(r(this.vertexM[s],t.vertexM[s]))return!1;for(let s=0;s<this.partCount;++s)if(this.partFlags[s]!==t.partFlags[s]||this.partOffsets[s]!==t.partOffsets[s])return!1;if(this.hasCurves()){for(let e=0;e<this.vertexCount;++e)if(this.segmentFlags[e]!==t.segmentFlags[e]||this.segmentIndices[e]!==t.segmentIndices[e])return!1;for(let e=0;e<this.segmentParamCount;++e)if(r(this.segmentParams[e],t.segmentParams[e]))return!1}return!0}get parts(){return this.isMultipart()?new Z(this):[][Symbol.iterator]()}}class Z{constructor(t){this.geometry=t,this.value=new A(t)}get done(){return this.value.index>=this.geometry.partCount}next(){return++this.value.index,this}[Symbol.iterator](){return this}}class A{constructor(t){this.geometry=t,this.index=-1,this._readVertex=j(t),this._segments=new R(this)}get segments(){return this._segments.reset(),this._segments}get startVertex(){return this._readVertex(this.geometry.partOffsets[this.index])}get vertexCount(){const t=this.geometry.partOffsets;return t[this.index+1]-t[this.index]}}class R{constructor(t){this._part=t,this._partStart=0,this._partEnd=0,this._isPolygon="polygon"===t.geometry.type,this.value=new V(t.geometry)}get done(){return this.value.indexOfStartVertex>this._partEnd}next(){++this.value.indexOfStartVertex;const t=this.value.indexOfStartVertex>=this._partEnd;return this.value.indexOfEndVertex=t&&this._isPolygon?this._partStart:this.value.indexOfStartVertex+1,this}[Symbol.iterator](){return this}reset(){const{index:t}=this._part,e=this._part.geometry.partOffsets;this._partStart=e[t],this._partEnd=e[t+1]-(this._isPolygon?1:2),this.value.indexOfStartVertex=this._partStart-1}}class V{constructor(t){this.geometry=t,this.indexOfStartVertex=-1,this.indexOfEndVertex=-1,this._readVertex=j(t)}get xStart(){return this.geometry.vertexXY[2*this.indexOfStartVertex]}get yStart(){return this.geometry.vertexXY[2*this.indexOfStartVertex+1]}get zStart(){return this.geometry.vertexZ[this.indexOfStartVertex]}get mStart(){return this.geometry.vertexM[this.indexOfStartVertex]}get xEnd(){return this.geometry.vertexXY[2*this.indexOfEndVertex]}get yEnd(){return this.geometry.vertexXY[2*this.indexOfEndVertex+1]}get zEnd(){return this.geometry.vertexZ[this.indexOfEndVertex]}get mEnd(){return this.geometry.vertexM[this.indexOfEndVertex]}get start(){return this._readVertex(this.indexOfStartVertex)}get end(){return this._readVertex(this.indexOfEndVertex)}get curve(){const t=this.geometry.segmentFlags?.[this.indexOfStartVertex]??1;if(1===t)return this.end;const e=this.geometry.segmentParams;let r=this.geometry.segmentIndices[this.indexOfStartVertex];if(2===t){const t=e[r++],s=e[r++],n=e[r++],i=e[r++];return{b:[this.end,[t,s],[n,i]]}}const s=e[r++],n=e[r++],i=e[r++],a=e[r++],o=e[r++],u=e[r++];r++,r++;const h=e[r++],l=e[r++];if(!Boolean(4&s)&&1===i)return{c:[this.end,[h,l]]};const c=2&s?0:1,g=1&s?0:1;return{a:[this.end,[o,u],c,g,a,n,i]}}}function Y(r){let s;const{vertexCount:n,vertexXY:i,vertexZ:o,vertexM:u}=r,h=z(r);if(r.hasCurves()){const n={stack:[],error:void 0,hasError:!1};try{t(n,new k(r,["vertexZ","vertexM"]),!1);const e=a();for(const t of r.parts){let r=t.startVertex;for(const{curve:s}of t.segments)x(e,r,s),r=v(s)}const[i,o,u,l]=e;s={xmin:i,ymin:o,xmax:u,ymax:l,spatialReference:h}}catch(l){n.error=l,n.hasError=!0}finally{e(n)}}else{let t,e,r,a;t=e=1/0,r=a=-1/0;for(let s=0;s<n;++s){const n=i[2*s],o=i[2*s+1];t=Math.min(t,n),r=Math.max(r,n),e=Math.min(e,o),a=Math.max(a,o)}s={xmin:t,ymin:e,xmax:r,ymax:a,spatialReference:h}}if(o){let t=1/0,e=-1/0;for(const r of o)t=Math.min(t,r),e=Math.max(e,r);s.zmin=t,s.zmax=e}if(u){let t=1/0,e=-1/0;for(const r of u)t=Math.min(t,r),e=Math.max(e,r);s.mmin=t,s.mmax=e}return s}function b(r,s){const n={stack:[],error:void 0,hasError:!1};try{s??=a();t(n,new k(r,["vertexZ","vertexM","spatialReference"]),!1);return o(s,Y(r)),s}catch(i){n.error=i,n.hasError=!0}finally{e(n)}}function w(r,s){const a={stack:[],error:void 0,hasError:!1};try{s??=n();t(a,new k(r,["vertexM","spatialReference"]),!1);return i(s,Y(r)),s}catch(o){a.error=o,a.hasError=!0}finally{e(a)}}function I(t){let e,r,s;if(u(t)){s="point";const{x:n,y:i,z:a,m:o}=t,u=[n,i];null!=a&&u.push(a),null!=o&&u.push(o),r=[[u]],e=1}else h(t)?(s="multipoint",r=[t.points],e=t.points.length):l(t)?(s="polyline",r=t.curvePaths??t.paths,e=r.reduce((t,e)=>t+e.length,0)):(s="polygon",r=t.curveRings??t.rings,e=r.reduce((t,e)=>(0===e.length||(t+=e.length,W(e[0],v(e.at(-1)))&&--t),t),0));const n=U(s,e,s.includes("point")?0:r.length,g(t),c(t),d(t)),{hasZ:i,hasM:a,vertexXY:o,vertexZ:f,vertexM:m}=n;function p(t){o[2*O]=t[0],o[2*O+1]=t[1],i&&(f[O]=t[2]),a&&(m[O]=t[x]),++O}n.spatialReference=t.spatialReference??void 0;const x=n.hasZ?3:2;let O=0;if(!n.isMultipart()){if(!i&&!a){for(const[t,e]of r[0])o[2*O]=t,o[2*O+1]=e,++O;return n}for(const t of r[0])p(t);return n}const S="polygon"===s,{partOffsets:F}=n;let P=0;if(!n.hasCurves()){for(const t of r){if(F[P++]=O,0===t.length)continue;let e=t.length;if(S&&W(t[0],t.at(-1))&&--e,i||a)for(let r=0;r<e;++r)p(t[r]);else for(let r=0;r<e;++r){const[e,s]=t[r];o[2*O]=e,o[2*O+1]=s,++O}}return F[P]=O,n}function _(t){const e=Z+t;if(e<=(n.segmentParams?.length??0))return;let r=e*K;r=20*Math.ceil(r/20);const s=n.segmentParams;n.segmentParams=new Float64Array(r),null!=s&&n.segmentParams.set(s)}let Z=0;for(const u of r){if(n.partOffsets[P++]=O,0===u.length)continue;const t=u[0];let e=t;for(let r=1;r<u.length;++r){const t=u[r];p(e);const s=O-1;y(t)?(_(10),++n.segmentCountArc,Z=D(e,t,n,s,Z),e=t.a[0]):C(t)?(_(10),++n.segmentCountArc,Z=q(e,t,n,s,Z),e=t.a[0]):M(t)?(_(10),++n.segmentCountArc,Z=G(e,t,n,s,Z),e=t.c[0]):E(t)?(_(4),++n.segmentCountBezier,Z=H(e,t,n,s,Z),e=t.b[0]):e=t}S&&W(t,e)||p(e)}return n.partOffsets[P]=O,n}function X(t){const e=j(t),{vertexCount:r,partCount:s,hasZ:n,hasM:i}=t,a=z(t);if("point"===t.type){const[e,r]=t.vertexXY,s=t.vertexZ?.[0],n=t.vertexM?.[0];return{x:e,y:r,z:s,m:n,spatialReference:a}}let o=0;if("multipoint"===t.type){const t=[];for(;o<r;)t.push(e(o++));return{points:t,spatialReference:a,hasZ:n,hasM:i}}const u="polygon"===t.type;if(!t.hasCurves()){const r=new Array;for(let n=0;n<s;++n){const s=t.partOffsets[n+1],i=new Array;for(;o<s;)i.push(e(o++));u&&i.length>0&&i.push(i[0]),r.push(i)}return u?{rings:r,hasZ:n,hasM:i,spatialReference:a}:{paths:r,hasZ:n,hasM:i,spatialReference:a}}const h=new Array;for(const{segments:l,startVertex:c}of t.parts){const t=[c];for(const{curve:e}of l)t.push(e);h.push(t)}return u?{rings:[],curveRings:h,hasZ:n,hasM:i,spatialReference:a}:{paths:[],curvePaths:h,hasZ:n,hasM:i,spatialReference:a}}function B(t,{lengths:e,coords:r,hasZ:s,hasM:n},i){const a=t.includes("point")?0:e.length,o="polygon"===t,u="point"===t?1:e.reduce((t,e)=>t+e,0)-(o?a:0);s??=!1,n??=!1;const h=U(t,u,a,s,n,!1,0,0);if(h.spatialReference=i??void 0,!h.isMultipart()){for(let t=0,e=0;t<u;++t)h.vertexXY[2*t]=r[e++],h.vertexXY[2*t+1]=r[e++],s&&(h.vertexZ[t]=r[e++]),n&&(h.vertexM[t]=r[e++]);return h}const l=2+Number(s)+Number(n);let c=0,g=0;h.partOffsets[0]=0;for(const f of e){const t=h.partOffsets[g],e=t+f-Number(o);h.partOffsets[++g]=e;for(let i=t;i<e;++i)h.vertexXY[2*i]=r[c++],h.vertexXY[2*i+1]=r[c++],s&&(h.vertexZ[i]=r[c++]),n&&(h.vertexM[i]=r[c++]);o&&(c+=l)}return h}function L({type:t,vertexCount:e,vertexXY:r,vertexZ:s,vertexM:n,partCount:i,partOffsets:a,hasZ:o,hasM:u}){const h=new P([],[],o,u);i=Math.max(i,1);const l="polygon"===t;let c=0;for(let g=0;g<i;++g){const t=a?.[g+1]??e,i=h.coords.length;for(let e=c;e<t;++e)h.coords.push(r[2*e],r[2*e+1]),o&&h.coords.push(s[e]),u&&h.coords.push(n[e]);if(l){const t=c;h.coords.push(r[2*t],r[2*t+1]),o&&h.coords.push(s[t]),u&&h.coords.push(n[t])}h.lengths.push((h.coords.length-i)/h.stride),c=t}return"point"===t&&(h.lengths.length=0),h}function N(t,e){return"getSpatialReference"in t&&(e=T(t.getSpatialReference()),t=t.getGeometry()),new _({...t.toFlatGeometry(),spatialReference:e})}function T(t){if(null==t)return;const e=t.getText()||void 0,r=t.isCustomWkid()?0:t.getOldID();if(r<=0)return{wkt:e};let s=t.getLatestID();(s<=0||s===r)&&(s=void 0);const n=t.getVCS();if(null==n)return{wkid:r,latestWkid:s,wkt:e};const i=n.isCustomWkid()?0:n.getOldID();if(i<=0)return{wkt:e};let a=t.getLatestVerticalID();return(a<=0||a===i)&&(a=void 0),{wkid:r,wkt:e,latestWkid:s,vcsWkid:i,latestVcsWkid:a}}class k{constructor(t,e){this.target=t,this.keys=e,this.oldValues=e.map(e=>t[e]);for(const r of e)t[r]=void 0}[Symbol.dispose](){this.keys.map((t,e)=>{this.target[t]=this.oldValues[e]})}}function z({spatialReference:t}){return t instanceof s?t.toJSON():t}function j({vertexXY:t,vertexZ:e,vertexM:r}){return e?r?s=>[t[2*s],t[2*s+1],e[s],r[s]]:r=>[t[2*r],t[2*r+1],e[r]]:r?e=>[t[2*e],t[2*e+1],r[e]]:e=>[t[2*e],t[2*e+1]]}function W(t,e){return t.every((t,r)=>t===e[r])}function U(t,e,r,s,n,i,a,o){a??=0,o??=0;const u=new _({type:t,vertexCount:e,vertexXY:null,vertexZ:void 0,vertexM:void 0,partCount:r,partOffsets:void 0,partFlags:void 0,segmentFlags:void 0,segmentIndices:void 0,segmentParams:void 0,segmentCountArc:a,segmentCountBezier:o});let h=2*Float64Array.BYTES_PER_ELEMENT*e;s&&(h+=Float64Array.BYTES_PER_ELEMENT*e),n&&(h+=Float64Array.BYTES_PER_ELEMENT*e),u.isMultipart()&&(h+=(r+1)*Int32Array.BYTES_PER_ELEMENT,h+=(r+1)*Int8Array.BYTES_PER_ELEMENT),i&&(h+=e*Int8Array.BYTES_PER_ELEMENT,h+=e*Int32Array.BYTES_PER_ELEMENT,h+=u.segmentParamCount*Float64Array.BYTES_PER_ELEMENT);const l=new ArrayBuffer(h);let c=0;return u.vertexXY=new Float64Array(l,c,2*e),c+=u.vertexXY.byteLength,s&&(u.vertexZ=new Float64Array(l,c,e),c+=u.vertexZ.byteLength),n&&(u.vertexM=new Float64Array(l,c,e),c+=u.vertexM.byteLength),i&&(u.segmentParams=new Float64Array(l,c,u.segmentParamCount),c+=u.segmentParams.byteLength,u.segmentIndices=new Int32Array(l,c,e),u.segmentIndices.fill(Q),c+=u.segmentIndices.byteLength),u.isMultipart()&&(u.partOffsets=new Int32Array(l,c,r+1),c+=u.partOffsets.byteLength,u.partFlags=new Int8Array(l,c,r+1),c+=u.partFlags.byteLength,"polygon"===u.type&&u.partFlags.subarray(0,r).fill(1)),i&&(u.segmentFlags=new Int8Array(l,c,e),u.segmentFlags.fill(1),c+=u.segmentFlags.byteLength),u}function D(t,e,{segmentFlags:r,segmentIndices:s,segmentParams:n},i,a){r[i]=4,s[i]=a;const[o,[u,h],l,c,g,f,m]=e.a;let p=4;c||(p|=1),l||(p|=2);const x=S(t,e),[v,d]=F(x,.5);return n[a++]=p,n[a++]=f,n[a++]=m,n[a++]=g,n[a++]=u,n[a++]=h,n[a++]=x.u2-x.u1,n[a++]=x.u1,n[a++]=v,n[a++]=d,a}function q(t,e,{segmentFlags:r,segmentIndices:s,segmentParams:n},i,a){r[i]=4,s[i]=a;const o=O(t,e);return J(o,4,...m(o,.5),n,a)}function G(t,e,{segmentFlags:r,segmentIndices:s,segmentParams:n},i,a){r[i]=4,s[i]=a;return J(p(t,e),0,...e.c[1],n,a)}function J(t,e,r,s,n,i){const{cx:a,cy:o,thetaStart:u,thetaEnd:h,radius:l}=t;u>h||(e|=1);return Math.abs(h-u)<=Math.PI||(e|=2),n[i++]=e,n[i++]=l,n[i++]=1,n[i++]=0,n[i++]=a,n[i++]=o,n[i++]=h-u,n[i++]=u,n[i++]=r,n[i++]=s,i}function H(t,{b:[e,[r,s],[n,i]]},{segmentFlags:a,segmentIndices:o,segmentParams:u},h,l){return a[h]=2,o[h]=l,u[l++]=r,u[l++]=s,u[l++]=n,u[l++]=i,l}const K=1.61803,Q=-1;export{_ as FlatGeometry,U as allocateFlatGeometry,X as convertFlatToGeometryJSON,L as convertFlatToOptimizedGeometry,I as convertGeometryToFlat,N as convertGeometryxToFlat,B as convertOptimizedGeometryToFlat,w as getBoundingBox,b as getBoundingRect,Y as getExtentJSON,H as writeBezierCurveSegment,G as writeCircularArcSegment,q as writeEllipticArc4Segment,D as writeEllipticArc7Segment};