viewpointUtils.js 8.5 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"../../Viewpoint.js";import e from"../../core/Collection.js";import{isSome as n,get as r,isNone as o}from"../../core/maybe.js";import{getMetersPerUnitForSR as a}from"../../core/unitUtils.js";import{t as i}from"../../chunks/common.js";import{f as c,r as s,t as u,a as f,s as l,b as m,c as y}from"../../chunks/mat2d.js";import{c as p}from"../../chunks/mat2df64.js";import{b as g,s as x,e as h,n as b,a as w,f as d,g as j,h as G,i as R,j as A,t as k,c as S}from"../../chunks/vec2.js";import{f as v,a as M}from"../../chunks/vec2f64.js";import F from"../../geometry/Extent.js";import N from"../../geometry/Geometry.js";import z from"../../geometry/Point.js";import{isLoaded as I,canProjectWithoutEngine as V,load as q,project as L}from"../../geometry/projection.js";import P from"../../geometry/SpatialReference.js";import{equals as C,isValid as E,getInfo as O}from"../../geometry/support/spatialReferenceUtils.js";const Q=96,U=39.37,B=180/Math.PI;function D(t){return t.wkid?t:t.spatialReference||P.WGS84}function T(t,e){return e.type?x(t,e.x,e.y):S(t,e)}function W(t){return a(t)}function H(t,e){const n=Math.max(1,e[0]),r=Math.max(1,e[1]);return Math.max(t.width/n,t.height/r)*ct(t.spatialReference)}async function J(t,a,i,c){let s,u;if(!t)return null;if(Array.isArray(t)&&!t.length)return null;if(e.isCollection(t)&&(t=t.toArray()),Array.isArray(t)&&t.length&&"object"==typeof t[0]){const e=t.every((t=>"attributes"in t)),r=t.some((t=>!t.geometry));let o=t;if(e&&r&&a&&a.allLayerViews){const e=new Map;for(const n of t){const t=n.layer,r=e.get(t)||[],o=n.attributes[t.objectIdField];null!=o&&r.push(o),e.set(t,r)}const r=[];e.forEach(((t,e)=>{const n=a.allLayerViews.find((t=>t.layer.id===e.id));if("queryFeatures"in n){const o=e.createQuery();o.objectIds=t,o.returnGeometry=!0,r.push(n.queryFeatures(o))}}));const i=await Promise.all(r),c=[];for(const t of i)if(t&&t.features&&t.features.length)for(const e of t.features)n(e.geometry)&&c.push(e.geometry);o=c}for(const t of o)c=await J(t,a,i,c);return c}if(Array.isArray(t)&&2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1])s=new z(t);else if(t instanceof N)s=t;else if("geometry"in t)if(t.geometry)s=t.geometry;else if(t.layer){const e=t.layer,n=a.allLayerViews.find((t=>t.layer.id===e.id));if("queryFeatures"in n){const o=e.createQuery();o.objectIds=[t.attributes[e.objectIdField]],o.returnGeometry=!0;const a=await n.queryFeatures(o);s=r(a,"features",0,"geometry")}}if(o(s))return null;if(u="point"===s.type?new F({xmin:s.x,ymin:s.y,xmax:s.x,ymax:s.y,spatialReference:s.spatialReference}):s.extent,!u)return null;I()||V(u.spatialReference,i)||await q();const f=L(u,i);return f?c=c?c.union(f):f:null}function K(t){if(t&&(!Array.isArray(t)||"number"!=typeof t[0])&&("object"==typeof t||Array.isArray(t)&&"object"==typeof t[0])){if("layer"in t&&t.layer&&t.layer.minScale&&t.layer.maxScale){const e=t.layer;return{min:e.minScale,max:e.maxScale}}if(Array.isArray(t)&&t.length&&t.every((t=>"layer"in t))){let e=0,n=0;for(const r of t){const t=r.layer;t&&t.minScale&&t.maxScale&&(e=t.minScale<e?t.minScale:e,n=t.maxScale>n?t.maxScale:n)}return e&&n?{min:e,max:n}:null}}}function X(t,e){return C(D(t),e)?t:L(t,e)}async function Y(e,r){if(!e||!r)return new t({targetGeometry:new z,scale:0,rotation:0});let o=r.spatialReference;const{constraints:a,padding:i,viewpoint:c,size:s}=r,u=[i?s[0]-i.left-i.right:s[0],i?s[1]-i.top-i.bottom:s[1]];let f=null;e instanceof t?f=e:e.viewpoint?f=e.viewpoint:e.target&&"esri.Viewpoint"===e.target.declaredClass&&(f=e.target);let l=null;f&&f.targetGeometry?l=f.targetGeometry:e instanceof F?l=e:(e||e&&("center"in e||"extent"in e||"target"in e))&&(l=await J(e.center,r,o)||await J(e.extent,r,o)||await J(e.target,r,o)||await J(e,r,o)),!l&&c&&c.targetGeometry?l=c.targetGeometry:!l&&r.extent&&(l=r.extent),o||(o=D(r.spatialReference||r.extent||l)),I()||C(l.spatialReference,o)||V(l,o)||await q();const m=X(l.center?l.center:l,o);let y=null;if(f&&n(f.targetGeometry)&&"point"===f.targetGeometry.type)y=f.scale;else if("scale"in e&&e.scale)y=e.scale;else if("zoom"in e&&-1!==e.zoom&&a&&a.effectiveLODs)y=a.zoomToScale(e.zoom);else if(Array.isArray(l)||"point"===l.type||"extent"===l.type&&0===l.width&&0===l.height){const t=X(r.extent,o);y=n(t)?H(t,u):r.extent?H(r.extent,u):c.scale}else y=H(X(l.extent,o),u);const p=K(e);p&&(p.min&&p.min>y?y=p.min:p.max&&p.max<y&&(y=p.max));let g=0;f?g=f.rotation:e.hasOwnProperty("rotation")?g=e.rotation:c&&(g=c.rotation);let x=new t({targetGeometry:m,scale:y,rotation:g});return a&&(x=a.fit(x),a.constrainByGeometry(x),a.rotationEnabled||(x.rotation=g)),x}function Z(t,e){const n=t.targetGeometry,r=e.targetGeometry;return n.x=r.x,n.y=r.y,n.spatialReference=r.spatialReference,t.scale=e.scale,t.rotation=e.rotation,t}function $(t,e,n){return n?x(t,.5*(e[0]-n.right+n.left),.5*(e[1]-n.bottom+n.top)):g(t,e,.5)}const _=function(){const t=M();return function(e,n,r){const o=n.targetGeometry;T(t,o);const a=.5*ot(n);return e.xmin=t[0]-a*r[0],e.ymin=t[1]-a*r[1],e.xmax=t[0]+a*r[0],e.ymax=t[1]+a*r[1],e.spatialReference=o.spatialReference,e}}();function tt(t,e,n,r,o){return xt(t,e,n.center),t.scale=H(n,r),o&&o.constraints&&o.constraints.constrain(t),t}function et(t,e,n,r){return ft(t,e,n,r),y(t,t)}const nt=function(){const t=M();return function(e,n,r){return h(e,st(e,n),$(t,n,r))}}(),rt=function(){const t=p(),e=M();return function(n,r,o,a){const i=ot(r),f=it(r);return x(e,i,i),c(t,e),s(t,t,f),u(t,t,nt(e,o,a)),u(t,t,[0,a.top-a.bottom]),x(n,t[4],t[5])}}();function ot(t){return t.scale*at(t.targetGeometry)}function at(t){return n(t)&&E(t.spatialReference)?1/(W(t.spatialReference)*U*Q):1}function it(t){return i(t.rotation)||0}function ct(t){return E(t)?W(t)*U*Q:1}function st(t,e){return g(t,e,.5)}const ut=function(){const t=M(),e=M(),n=M();return function(r,o,a,i,c,m){return b(t,o),g(e,a,.5*m),x(n,1/i*m,-1/i*m),f(r,e),c&&s(r,r,c),l(r,r,n),u(r,r,t),r}}(),ft=function(){const t=M();return function(e,n,r,o){const a=ot(n),i=it(n);return T(t,n.targetGeometry),ut(e,t,r,a,i,o)}}(),lt=function(){const t=M();return function(e,n,r,o){const a=ot(n);return T(t,n.targetGeometry),ut(e,t,r,a,0,o)}}();function mt(t){const e=O(t);return e?e.valid[1]-e.valid[0]:0}function yt(t,e){return Math.round(mt(t)/e)}const pt=function(){const t=M(),e=M(),n=[0,0,0];return function(r,o,a){w(t,r,o),d(t,t),w(e,r,a),d(e,e),j(n,t,e);let i=Math.acos(G(t,e)/(R(t)*R(e)))*B;return n[2]<0&&(i=-i),isNaN(i)&&(i=0),i}}(),gt=function(){const t=M();return function(e,n,r,o){const a=e.targetGeometry;return Z(e,n),rt(t,n,r,o),a.x+=t[0],a.y+=t[1],e}}(),xt=function(t,e,n){Z(t,e);const r=t.targetGeometry;return r.x=n.x,r.y=n.y,r.spatialReference=n.spatialReference,t},ht=function(){const t=M();return function(e,n,r,o,a){a||(a="center"),h(t,r,o),g(t,t,.5);const i=t[0],c=t[1];switch(a){case"center":x(t,0,0);break;case"left":x(t,-i,0);break;case"top":x(t,0,c);break;case"right":x(t,i,0);break;case"bottom":x(t,0,-c);break;case"top-left":x(t,-i,c);break;case"bottom-left":x(t,-i,-c);break;case"top-right":x(t,i,c);break;case"bottom-right":x(t,i,-c)}return St(e,n,t),e}}();function bt(t,e,n){return Z(t,e),t.rotation+=n,t}function wt(t,e,n){return Z(t,e),t.rotation=n,t}const dt=function(){const t=M();return function(e,n,r,o,a){return Z(e,n),isNaN(r)||0===r||(At(t,o,n,a),e.scale=n.scale*r,kt(t,t,e,a),St(e,e,x(t,t[0]-o[0],o[1]-t[1]))),e}}();function jt(t,e,n){return Z(t,e),t.scale=n,t}const Gt=function(){const t=M();return function(e,n,r,o,a,i){return Z(e,n),isNaN(r)||0===r||(At(t,a,n,i),e.scale=n.scale*r,e.rotation+=o,kt(t,t,e,i),St(e,e,x(t,t[0]-a[0],a[1]-t[1]))),e}}(),Rt=function(){const t=M(),e=M();return function(n,r,o,a,i,c,s){return nt(e,c,s),A(t,i,e),a?Gt(n,r,o,a,t,c):dt(n,r,o,t,c)}}(),At=function(){const t=p();return function(e,n,r,o){return k(e,n,et(t,r,o,1))}}(),kt=function(){const t=p();return function(e,n,r,o){return k(e,n,ft(t,r,o,1))}}(),St=function(){const t=M(),e=p();return function(n,r,o){Z(n,r);const a=ot(r),i=n.targetGeometry;return m(e,it(r)),l(e,e,v(a,a)),k(t,o,e),i.x+=t[0],i.y+=t[1],n}}();export{gt as addPadding,pt as angleBetween,xt as centerAt,Z as copy,Y as create,H as extentToScale,$ as getAnchor,_ as getExtent,ut as getMatrix,rt as getPaddingMapTranslation,nt as getPaddingScreenTranslation,ot as getResolution,ct as getResolutionToScaleFactor,ft as getTransform,lt as getTransformNoRotation,yt as getWorldScreenWidth,mt as getWorldWidth,Rt as padAndScaleAndRotateBy,ht as resize,bt as rotateBy,wt as rotateTo,Gt as scaleAndRotateBy,jt as scaleTo,tt as setExtent,At as toMap,kt as toScreen,St as translateBy};