UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 12 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */ import{__addDisposableResource as e,__disposeResources as t}from"tslib";import{G as n,v as i}from"./Geometry.js";import{S as r,P as s,s as o}from"./MultiPathImpl.js";import{l as m}from"./SpatialReference.js";import{P as a,c as u,s as c,i as h,h as l,j as g,d as y}from"./Point2D.js";import{Envelope2D as _}from"./Envelope2D.js";import{P as D,E as p}from"./Envelope.js";function v(){return{outPoint:new a,index:-1,t:Number.NaN}}function N(e,t,n){return{outPoint:e.clone(),index:t,t:n}}function f(e,t,n,i){e.outPoint.assign(t),e.index=n,e.t=i}function P(e,t){e.outPoint.assign(t.outPoint),e.index=t.index,e.t=t.t}function I(e,t,n,i){e.index=n,e.t=i,t.queryCoord2D(e.t,e.outPoint)}function d(e,t){const n=v();P(n,e),P(e,t),P(t,n)}function q(e,t,i,r,s,o){if(e.getGeometryType()===n.enumPolygon){for(;r.nextPath();)if(r.hasNextSegment()){const t=r.nextSegment();if(0!==m(e,t.getEndXY(),0))return f(s,t.getEndXY(),-1,Number.NaN),f(o,t.getEndXY(),-1,Number.NaN),!0}r.resetToFirstPath()}if(t.getGeometryType()===n.enumPolygon){for(;i.nextPath();)if(i.hasNextSegment()){const e=i.nextSegment();if(0!==m(t,e.getEndXY(),0))return f(s,e.getEndXY(),-1,Number.NaN),f(o,e.getEndXY(),-1,Number.NaN),!0}i.resetToFirstPath()}return!1}function M(e){const t=e.getPathCount(),n=y(e.getSegmentCount(),0);let i=0;for(let r=0;r<t;++r){const t=i+e.getSegmentCountPath(r);for(let s=i,o=e.getPathStart(r);s<t;++s,++o)n[s]=o;i=t}return o(e.getPointCount(),n),n}function x(e,t){return!!Number.isNaN(t)||e<=t}function E(e){const t=new s;return t.addEnvelope(e,!1),t}class b{constructor(e,t){if(this.m_env2DgeometryA=null,this.m_env2DgeometryB=null,this.m_progressCounter=0,this.m_progressTracker=t,this.m_maxSqrDistance=e*e,this.m_maxDistance=e,this.m_bIsNearCalc=!1,Number.isNaN(this.m_maxDistance)?this.m_maxDistance=Number.POSITIVE_INFINITY:this.m_maxDistance=e,this.m_maxSqrDistance=this.m_maxDistance*this.m_maxDistance,Number.isFinite(this.m_maxSqrDistance)){let e=Math.sqrt(this.m_maxSqrDistance);for(;e<this.m_maxDistance;)this.m_maxSqrDistance*=1+Number.EPSILON,e=Math.sqrt(this.m_maxSqrDistance)}}calculate(e,t,n,i){if(e.getGeometryType()!==D.type||t.getGeometryType()!==D.type){this.m_env2DgeometryA=new _,this.m_env2DgeometryB=new _,e.queryEnvelope(this.m_env2DgeometryA),t.queryEnvelope(this.m_env2DgeometryB);if(!x(this.m_env2DgeometryA.distanceFromEnvelope(this.m_env2DgeometryB),this.m_maxDistance))return Number.POSITIVE_INFINITY}const r=N(new a,g(),Number.NaN),s=N(new a,g(),Number.NaN),o=this._ExecuteBruteForce(e,t,r,s);return x(o,this.m_maxDistance)?(null!==n&&P(n,r),null!==i&&P(i,s),o):Number.POSITIVE_INFINITY}isNear(e,t){if(this.m_bIsNearCalc=!0,e.isEmpty()||t.isEmpty())return!1;if(e===t)return!0;let n=!0;if(e.getGeometryType()!==D.type&&e.getGeometryType()!==p.type||t.getGeometryType()!==D.type&&t.getGeometryType()!==p.type||(n=!1),n){if(this.m_env2DgeometryA=new _,this.m_env2DgeometryB=new _,e.queryEnvelope(this.m_env2DgeometryA),t.queryEnvelope(this.m_env2DgeometryB),!(this.m_env2DgeometryA.sqrMaxMinDistanceEnvelope(this.m_env2DgeometryB)>this.m_maxSqrDistance))return!0;if(this.m_env2DgeometryA.sqrDistanceEnvelope(this.m_env2DgeometryB,null,null)>this.m_maxSqrDistance)return!1}const i=N(new a,g(),Number.NaN),r=N(new a,g(),Number.NaN);return this._ExecuteBruteForce(e,t,i,r)<=this.m_maxDistance}progress_(e=!1){}_ExecuteBruteForce(e,t,i,r){switch(e.getGeometryType()){case n.enumPoint:return this.distancePointGeometry(e,t,i,r);case n.enumMultiPoint:return this.distanceMultipointGeometry(e,t,i,r);case n.enumEnvelope:return this.distanceEnvelopeGeometry(e,t,i,r);case n.enumPolyline:case n.enumPolygon:return this.distanceMultipathGeometry(e,t,i,r);default:return Number.NaN}}distancePointGeometry(e,t,i,r){switch(t.getGeometryType()){case n.enumPoint:return this.distancePointPoint(e,t,i,r);case n.enumMultiPoint:return this.distancePointMultipoint(e,t,i,r);case n.enumPolyline:case n.enumPolygon:return this.distancePointMultipath(e,t,i,r);case n.enumEnvelope:return this.distancePointEnvelope(e,t,i,r);default:return Number.NaN}}distancePointPoint(e,t,n,i){const r=e.getXY(),s=t.getXY();return f(n,r,0,0),f(i,s,0,0),Math.sqrt(a.sqrDistance(r,s))}distancePointMultipoint(e,t,n,i){const r=e.getXY();let s=this.m_maxSqrDistance,o=s,m=!1;const u=t.getPointCount(),c=new a;for(let h=0;h<u;++h)if(t.queryXY(h,c),o=a.sqrDistance(r,c),this.m_bIsNearCalc){if(o<=s)return 0}else if(!(o>s)&&(!m&&o===s||o<s)&&(s=o,f(n,r,0,0),f(i,c,h,0),m=!0,0===s))return 0;return m?Math.sqrt(s):Number.POSITIVE_INFINITY}distancePointMultipath(e,t,i,r){const s=!this.m_env2DgeometryA.isIntersecting(this.m_env2DgeometryB),o=e.getXY();if(!s&&t.getGeometryType()===n.enumPolygon&&0!==m(t,o,0))return f(i,o,0,0),f(r,o,-1,Number.NaN),0;t.getImpl().getAccelerators();let u=this.m_maxSqrDistance,c=u,h=-1;const l=new _,g=t.querySegmentIterator();let y=!1;for(;g.nextPath();)for(;g.hasNextSegment();){const e=g.nextSegment();e.queryEnvelope(l);const t=l.sqrDistance(o);if(t>u)continue;if(!this.m_bIsNearCalc&&y&&t===u)continue;h=e.getClosestCoordinate(o,!1);const n=e.getCoord2D(h);if(c=a.sqrDistance(o,n),this.m_bIsNearCalc){if(c<=u)return 0}else if(!(c>u)&&(!y||c<u)&&(u=c,f(i,o,0,0),f(r,n,g.getStartPointIndex(),h),y=!0,0===u))return 0}return y?Math.sqrt(u):Number.POSITIVE_INFINITY}distancePointEnvelope(e,t,n,i){const r=e.getXY();f(n,r,0,0),f(i,r,-1,Number.NaN);const s=t.asEnvelope2D();return!s.contains(r)?Math.sqrt(s.sqrDistance(r,i.outPoint)):0}distanceMultipointGeometry(e,t,i,r){switch(t.getGeometryType()){case n.enumPoint:{this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB);const n=this.distancePointMultipoint(t,e,r,i);return this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),n}case n.enumMultiPoint:return this.distanceMultipointMultipoint(e,t,i,r,1,1);case n.enumPolyline:case n.enumPolygon:return this.distanceMultipointMultipath(e,t,i,r);case n.enumEnvelope:return this.distanceMultipointMultipath(e,E(t),i,r);default:return Number.NaN}}distanceMultipointMultipoint(n,i,r,s,o,m){const g={stack:[],error:void 0,hasError:!1};try{if(this.m_bIsNearCalc&&1===o&&1===m){const e=Math.trunc(c(Math.sqrt(n.getPointCount())+1,1,h())),t=Math.trunc(c(Math.sqrt(i.getPointCount())+1,1,h()));if((e>=4||t>=4)&&this.distanceMultipointMultipoint(n,i,r,s,e,t)<=this.m_maxSqrDistance)return 0}let t=this.m_maxSqrDistance;const y=[n],_=[i],D=this.swapGeometriesIfBGtA(y,_);D&&(this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),m=u(o,o=m),d(r,s)),e(g,l(()=>{d(r,s),this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB)},!D),!1);const p=y[0].getImpl(),v=_[0].getImpl(),N=p.getPointCount(),P=v.getPointCount(),I=P>1;let q=!1;const M=new a,x=new a;for(let e=0;e<N;e+=o){if(p.queryXY(e,M),I){const e=this.m_env2DgeometryB.sqrDistance(M);if(this.m_bIsNearCalc){if(e>t)continue;if(this.m_env2DgeometryB.sqrMaxMinDistance(M)<=t)return 0}else if(e>=t)continue}for(let n=0;n<P;n+=m){v.queryXY(n,x);const i=a.sqrDistance(M,x);if(this.m_bIsNearCalc){if(i<=t)return 0}else if(!(i>t||q&&i===t)&&(!q||i<t)&&(f(r,M,e,0),f(s,x,n,0),q=!0,t=i,0===t))return q?Math.sqrt(t):Number.POSITIVE_INFINITY}}return q?Math.sqrt(t):Number.POSITIVE_INFINITY}catch(y){g.error=y,g.hasError=!0}finally{t(g)}}distanceMultipointMultipath(e,t,i,r){let s=!!this.m_env2DgeometryA.isIntersecting(this.m_env2DgeometryB)&&t.getGeometryType()===n.enumPolygon;const o=t.querySegmentIterator();o.stripAttributes();const u=new _,c=new a;let h=this.m_maxSqrDistance;const l=e.getImpl(),g=l.getPointCount(),y=g>1;let D=!1;for(;o.nextPath();)for(;o.hasNextSegment();){const e=o.nextSegment();if(e.queryLooseEnvelope(u),y){const e=u.sqrDistanceEnvelope(this.m_env2DgeometryA,null,null);if(this.m_bIsNearCalc){if(e>h)continue}else if(e>h||D&&e===h)continue}for(let n=0;n<g;n++){if(l.queryXY(n,c),s&&0!==m(t,c,0))return f(i,c,-1,Number.NaN),f(r,c,n,0),0;{const e=u.sqrDistance(c);if(this.m_bIsNearCalc){if(e>h)continue}else if(e>h)continue}const a=e.getClosestCoordinate(c,!1),g=e.getCoord2D(a);c.subThis(g);const y=c.sqrLength();if(this.m_bIsNearCalc){if(y<=h)return 0}else if(!(y>h)&&(!D||y<h)&&(h=y,f(i,l.getXY(n),n,0),f(r,g,o.getStartPointIndex(),a),D=!0,0===h))return 0}s=!1}return D?Math.sqrt(h):Number.POSITIVE_INFINITY}distanceEnvelopeGeometry(e,t,i,r){switch(t.getGeometryType()){case n.enumPoint:return this.distancePointEnvelope(t,e,r,i);case n.enumMultiPoint:{this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB);const n=this.distanceMultipointMultipath(t,E(e),r,i);return this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),n}case n.enumPolyline:case n.enumPolygon:return this.distanceMultipathMultipath(E(e),t,i,r);case n.enumEnvelope:return this.distanceEnvelopeEnvelope(e,t,i,r);default:return Number.NaN}}distanceEnvelopeEnvelope(e,t,n,i){n.t=Number.NaN,n.index=-1,i.t=Number.NaN,i.index=-1;const r=e.asEnvelope2D(),s=t.asEnvelope2D();return Math.sqrt(r.sqrDistanceEnvelope(s,n.outPoint,i.outPoint))}distanceMultipathGeometry(e,t,i,r){switch(t.getGeometryType()){case n.enumPoint:{this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB);const n=this.distancePointMultipath(t,e,r,i);return this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),n}case n.enumMultiPoint:{this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB);const n=this.distanceMultipointMultipath(t,e,r,i);return this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),n}case n.enumPolyline:case n.enumPolygon:return this.distanceMultipathMultipath(e,t,i,r);case n.enumEnvelope:return this.distanceMultipathMultipath(e,E(t),i,r);default:return Number.NaN}}distanceMultipathMultipath(n,i,s,o){const m={stack:[],error:void 0,hasError:!1};try{const t=[n],a=[i],g=this.swapGeometriesIfAGtB(t,a);g&&(this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB),d(s,o)),e(m,l(()=>{d(s,o),this.m_env2DgeometryB=u(this.m_env2DgeometryA,this.m_env2DgeometryA=this.m_env2DgeometryB)},!g),!1);const y=t[0],D=a[0],p=y.querySegmentIterator();p.stripAttributes();const N=D.querySegmentIterator();if(N.stripAttributes(),this.m_bIsNearCalc&&this.m_env2DgeometryA.sqrMaxMinDistanceEnvelope(this.m_env2DgeometryB)<=this.m_maxSqrDistance)return 0;if(this.m_bIsNearCalc){const e=Math.trunc(c(Math.sqrt(y.getPointCount())+1,1,h())),t=Math.trunc(c(Math.sqrt(y.getPointCount())+1,1,h()));if(this.distanceMultipointMultipoint(y,D,s,o,e,t)<=this.m_maxSqrDistance)return 0}if(!!this.m_env2DgeometryA.isIntersecting(this.m_env2DgeometryB)){const e=v(),t=v();if(q(y,D,p,N,e,t))return P(s,e),P(o,t),0}const f=M(y),x=[],E=new r,b=new _,S=new _,B=new _;let A=this.m_maxSqrDistance,T=!0,C=!1;for(let e=0,n=f.length;e<n;++e){y.querySegment(f[e],E,!0);const t=E.get();if(t.queryEnvelope(b),!(b.sqrDistanceEnvelope(this.m_env2DgeometryB,null,null)>A)){if(this.m_bIsNearCalc&&b.sqrMaxDistanceEnvelope(this.m_env2DgeometryB)<=A)return 0;for(;N.nextPath();){if(this.progress_(),T)D.queryPathEnvelope(N.getPathIndex(),B),x[N.getPathIndex()]=B.clone();else if(b.sqrDistanceEnvelope(x[N.getPathIndex()],null,null)>A)continue;for(;N.hasNextSegment();){const n=N.nextSegment();if(n.queryEnvelope(S),b.sqrDistanceEnvelope(S,null,null)<=A){const i=!b.isIntersecting(S),r=[0],m=[0];let a=t.distance(n,i,r,m);if(a*=a,this.m_bIsNearCalc&&a<=this.m_maxSqrDistance)return 0;if(a<A||a===A&&f[e]<s.index){if(C=!0,I(s,t,f[e],r[0]),I(o,n,N.getStartPointIndex(),m[0]),0===a)return 0;A=a}}}}N.resetToFirstPath(),T=!1}}return C?Math.sqrt(A):Number.POSITIVE_INFINITY}catch(a){m.error=a,m.hasError=!0}finally{t(m)}}swapGeometriesIfAGtB(e,t){return i(e[0])>i(t[0])&&(t[0]=u(e[0],e[0]=t[0]),!0)}swapGeometriesIfBGtA(e,t){return this.swapGeometriesIfAGtB(t,e)}}export{b as D,v as m,M as s,q as w};