UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 9.42 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{equals as t,pushIfSome as e}from"../../../core/arrayUtils.js";import{Evented as r}from"../../../core/Evented.js";import i from"../../../geometry/Multipoint.js";import s from"../../../geometry/Polygon.js";import n from"../../../geometry/Polyline.js";import{getEndpoint as o,isCoordinate as h,isBezierCurve as a,isCircularArc as c,isEllipticArc4 as l}from"../../../geometry/support/curves/curveUtils.js";import{createCoordinateHelper as p}from"../coordinateHelper.js";import{unnormalize as g,getUnnormalizationInfo as u}from"./unnormalizationHelper.js";class f{constructor(t){this.part=t,this.leftSegment=null,this.rightSegment=null,this.type="vertex",this.index=null}get pos(){return this._pos}set pos(t){this._pos=t,this.part.unnormalizeVertexPositions()}}class m{constructor(t,e,r){this.part=t,this.leftVertex=e,this.rightVertex=r,this.type="line",e.rightSegment=this,r.leftSegment=this}toCurveOrCoordinate(){return[...this.rightVertex.pos]}}class d{constructor(t,e,r,i){this.part=t,this.leftVertex=e,this.rightVertex=r,this.curveDefinition=i,this.type="bezier",e.rightSegment=this,r.leftSegment=this}toCurveOrCoordinate(){return{b:[[...this.rightVertex.pos],[...this.curveDefinition.controlPoint1],[...this.curveDefinition.controlPoint2]]}}}class v{constructor(t,e,r,i){this.part=t,this.leftVertex=e,this.rightVertex=r,this.curveDefinition=i,this.type="circular-arc",e.rightSegment=this,r.leftSegment=this}toCurveOrCoordinate(){const t=this.rightVertex.pos,{interiorPoint:e}=this.curveDefinition;return{c:[[...t],[...e]]}}}class y{constructor(t,e,r,i){this.part=t,this.leftVertex=e,this.rightVertex=r,this.curveDefinition=i,this.type="elliptic-arc-4",e.rightSegment=this,r.leftSegment=this}toCurveOrCoordinate(){const t=this.rightVertex.pos,{centerPoint:e,sweep:r,orientation:i}=this.curveDefinition;return{a:[[...t],[...e],r,i]}}}class x{constructor(t,e,r,i){this.part=t,this.leftVertex=e,this.rightVertex=r,this.curveDefinition=i,this.type="elliptic-arc-7",e.rightSegment=this,r.leftSegment=this}toCurveOrCoordinate(){const t=this.rightVertex.pos,{centerPoint:e,sweep:r,orientation:i,rotation:s,semiMajorAxisLength:n,minorMajorAxisRatio:o}=this.curveDefinition;return{a:[[...t],e,r,i,s,n,o]}}}class V{constructor(t,e,r=!1){this._spatialReference=t,this._viewingMode=e,this.autoClose=r,this.vertices=[],this.segments=[],this.index=null}unnormalizeVertexPositions(){this.vertices.length<=1||g(this.vertices,u(this._spatialReference,this._viewingMode))}updateVertexIndex(t,e){const{vertices:r}=this;if(0===r.length)return;const i=r[0];let s=null,n=t,o=e;do{s=n,s.index=o++,n=s.rightSegment?s.rightSegment.rightVertex:null}while(null!=n&&n!==i);s.leftSegment&&s!==r[r.length-1]&&this.swapVertices(r.indexOf(s),r.length-1)}getFirstVertex(){return this.vertices.at(0)}getLastVertex(){return this.vertices.at(-1)}isClosed(){return null!=this.getFirstVertex()?.leftSegment}swapVertices(t,e){const{vertices:r}=this,i=r[t];r[t]=r[e],r[e]=i}*iterateVertices(){const t=this.getFirstVertex();let e=t;if(e)do{yield e,e=e.rightSegment?.rightVertex}while(e!==t&&null!=e)}}class w extends r{constructor(t,e){super(),this.type=t,this.coordinateHelper=e,this._geometry=null,this._dirty=!0,this.parts=[]}get geometry(){if(this._dirty){switch(this.type){case"point":this._geometry=this._toPoint();break;case"multipoint":this._geometry=this._toMultipoint();break;case"polyline":this._geometry=this._toPolyline();break;case"polygon":this._geometry=this._toPolygon();break;case"mesh":break;default:this.type}this._dirty=!1}return this._geometry}get spatialReference(){return this.coordinateHelper.spatialReference}get allVerticesUnordered(){return Array.from(this.iterateVerticesUnordered())}*iterateVerticesUnordered(){for(const t of this.parts)for(const e of t.vertices)yield e}get allVertices(){return Array.from(this.iterateVertices())}*iterateVertices(){for(const t of this.parts)yield*t.iterateVertices()}notifyChanges(t){this._dirty=!0,this.emit("change",t)}getPartialGeometry(t,e,r){const{parts:i,hasCurves:s}=S(this,{startingVertex:t,endingVertexInclusive:e,geometryType:r});return C(i,s,r,this.coordinateHelper)}_toPoint(){const{parts:t,coordinateHelper:e}=this;return t.at(0)?.vertices.length?e.vectorToPoint(t[0].vertices[0].pos):null}_toMultipoint(){const t=[],{coordinateHelper:e,parts:r,spatialReference:s}=this;for(const i of r){const r=i.getFirstVertex();r&&t.push(e.arrayToVector(r.pos))}return new i({hasM:e.hasM(),hasZ:e.hasZ(),spatialReference:s,points:t})}_toPolyline(){const t=[];let e=!1;for(const r of this.parts){if(r.vertices.length<1)continue;const i=r.vertices[0],s=[];s.push(i.pos);let n=i.rightSegment;for(;n;)e||="line"!==n.type,s.push(n.toCurveOrCoordinate()),n=n.rightVertex.rightSegment;t.push(s)}return new n({paths:e?void 0:t,curvePaths:e?t:void 0,spatialReference:this.spatialReference,hasZ:this.coordinateHelper.hasZ(),hasM:this.coordinateHelper.hasM()})}_toPolygon(){const e=[];let r=!1;for(const i of this.parts){if(i.vertices.length<1)continue;const s=i.vertices[0],n=[];n.push(s.pos);let a=s.rightSegment;for(;a&&(r||="line"!==a.type,n.push(a.toCurveOrCoordinate()),a=a.rightVertex.rightSegment,a?.leftVertex!==s););if(i.autoClose&&n.length>1){const e=n.at(-1),r=o(n[0]),i=o(e);2===n.length&&h(e)||t(r,i)||n.push(r)}e.push(n)}return new s({rings:r?void 0:e,curveRings:r?e:void 0,spatialReference:this.spatialReference,hasZ:this.coordinateHelper.hasZ(),hasM:this.coordinateHelper.hasM()})}static fromGeometry(t,e,r){const i=t.spatialReference,s=p(t.hasZ,t.hasM,i),n=new w(t.type,s);switch(t.type){case"polygon":R(n,t,e,r?.allowCurves??!1);break;case"polyline":M(n,t,e,r?.allowCurves??!1);break;case"point":b(n,t,e);break;case"multipoint":H(n,t,e);break;case"mesh":b(n,t.origin,e),n._geometry=t,n._dirty=!1}return n}}function P(t,e,r,i){const s=new f(t);t.vertices.push(s);const n=o(i);return s.pos=e.arrayToVector(n),s.index=t.vertices.length-1,r&&t.segments.push(_(t,r,s,i)),s}function _(t,e,r,i){if(h(i))return new m(t,e,r);if(a(i)){const[,s,n]=i.b;return new d(t,e,r,{controlPoint1:[...s],controlPoint2:[...n]})}if(c(i)){const[,s]=i.c;return new v(t,e,r,{interiorPoint:[...s]})}if(l(i)){const[,s,n,o]=i.a;return new y(t,e,r,{centerPoint:[...s],sweep:n,orientation:o})}const[,s,n,o,p,g,u]=i.a;return new x(t,e,r,{centerPoint:[...s],sweep:n,orientation:o,rotation:p,semiMajorAxisLength:g,minorMajorAxisRatio:u})}function C(t,e,r,h){const{spatialReference:a}=h;if("point"===r){const e=t.at(0)?.at(0);return e?h.arrayToPoint(o(e)):null}return"polygon"===r?new s({rings:e?void 0:t,curveRings:e?t:void 0,spatialReference:a,hasZ:h.hasZ(),hasM:h.hasM()}):"multipoint"===r?new i({spatialReference:a,points:t.flatMap(t=>t.map(t=>o(t))),hasM:h.hasM(),hasZ:h.hasZ()}):new n({paths:e?void 0:t,curvePaths:e?t:void 0,spatialReference:a,hasZ:h.hasZ(),hasM:h.hasM()})}function S(r,i){const s="polygon"===(i?.geometryType??r.type),n=[];let a=!1,c=!1,l=!1;for(const p of r.parts){let r=null;if(l)break;if(p.vertices.length<1)continue;if(i?.startingVertex){if(p!==i.startingVertex.part&&!c)continue;c=!0}const g=p===i?.startingVertex?.part?i.startingVertex:p.vertices.at(0),u=[];if(e(u,g?.pos),g===i?.endingVertexInclusive){l=!0,r=g,n.push(u);break}let f=g?.rightSegment;for(;f;){if(a||="line"!==f.type,u.push(f.toCurveOrCoordinate()),r=f.rightVertex,f.rightVertex===i?.endingVertexInclusive){l=!0;break}if(f=f.rightVertex.rightSegment,f?.leftVertex===g)break}const m=u.at(-1),d=p.getFirstVertex(),v=p.getLastVertex();s&&p.autoClose&&m&&r===v&&g===d&&(u.length>2||2===u.length&&!h(m))&&g?.pos&&!t(o(m),g.pos)&&u.push([...g.pos]),n.push(u)}return{parts:n,hasCurves:a}}function R(e,r,i,s){const n=r.spatialReference,h=e.coordinateHelper,{rings:a,curveRings:c}=r,l=c&&s?c:a;for(let p=0;p<l.length;++p){const r=l[p],s=new V(n,i);if(s.index=p,e.parts.push(s),r.length<1)continue;let a=null,c=null;for(let t=0;t<r.length-1;++t)c=P(s,h,c,r[t]),a??=c;r.length>1&&t(o(r[r.length-1]),a?.pos)?c&&a&&s.segments.push(_(s,c,a,r[r.length-1])):c=P(s,h,c,r[r.length-1])}}function M(t,e,r,i){const s=e.spatialReference,n=t.coordinateHelper,{curvePaths:o,paths:h}=e,a=o&&i?o:h;for(let c=0;c<a.length;++c){const e=a[c],i=new V(s,r);if(i.index=c,t.parts.push(i),e.length<1)continue;let o=null;for(let t=0;t<e.length;++t)o=P(i,n,o,e[t])}}function b(t,e,r){const i=e.spatialReference,s=t.coordinateHelper,n=new V(i,r);n.index=0;const o=new f(n);o.index=0,o.pos=s.pointToVector(e),n.vertices.push(o),t.parts.push(n)}function H(t,e,r){const i=e.spatialReference,{coordinateHelper:s,parts:n}=t,o=e.points.map(t=>s.arrayToVector(t));for(let h=0;h<o.length;h++){const t=new V(i,r);t.index=h,n.push(t);const e=new f(t);e.pos=o[h],e.index=0,t.vertices.push(e)}}function k(t){return"mesh"===t.type}function Z(t,e){if(t.type!==e.type)return!1;if(t.parts.length!==e.parts.length)return!1;for(let s=0;s<t.parts.length;s++){const r=t.parts[s],i=e.parts[s];if(r.vertices.length!==i.vertices.length)return!1}const r=t.allVertices,i=e.allVertices;for(let s=0;s<r.length;s++){const t=r[s].leftSegment?.type,e=i[s].leftSegment?.type;if(t!==e)return!1}return!0}export{d as BezierSegment,v as CircularArcSegment,w as EditGeometry,y as EllipticArc4Segment,x as EllipticArc7Segment,m as LineSegment,V as Part,f as Vertex,P as addVertexAndConnectingSegment,_ as createConnectingSegment,Z as haveCompatibleTopologies,k as isMeshEditGeometry,C as partialCoordsToGeometry,S as toPartialCoords};