@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.46 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../core/Accessor.js";import{createScreenPoint as r}from"../../core/screenUtils.js";import{getMetersPerUnit as i,convertUnit as s}from"../../core/unitUtils.js";import{property as o}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/Logger.js";import"../../core/RandomLCG.js";import{subclass as n}from"../../core/accessorSupport/decorators/subclass.js";import a from"../../geometry/Point.js";import c from"../../geometry/Polyline.js";import m from"../../geometry/SpatialReference.js";import{isSupported as l,geodesicLengths as p}from"../../geometry/support/geodesicUtils.js";import{straightLineDensify as u}from"../../geometry/support/normalizeUtils.js";import{isValid as d,getInfo as f}from"../../geometry/support/spatialReferenceUtils.js";import{webMercatorToGeographic as h}from"../../geometry/support/webMercatorUtils.js";import{isLinkChartView as w}from"../../linkChart/utils.js";function g(e){return e>.5?{min:.5,max:1}:e>.2?{min:.2,max:.5}:{min:.1,max:.2}}function y(e,t){switch(t){case"metric":return e>1e3?{distance:s(e,"meters","kilometers"),unit:"kilometer"}:e>1?{distance:e,unit:"meter"}:e>.01?{distance:s(e,"meters","centimeters"),unit:"centimeter"}:{distance:s(e,"meters","millimeters"),unit:"millimeter"};case"imperial":return e>1609.344?{distance:s(e,"meters","miles"),unit:"mile"}:e>.3048?{distance:s(e,"meters","feet"),unit:"foot"}:{distance:s(e,"meters","inches"),unit:"inch"}}}function j(e){const{isWebMercator:t,wkt:r,wkt2:i}=e;return t||((i||r)?.includes("WGS_1984_Web_Mercator")??!1)}function S({state:{paddedViewState:e},spatialReference:t,width:r}){return t.isWrappable&&e.worldScreenWidth<r}function v(e,t){const{x:r,y:i}=e?h(t,!0):t;return[r,i]}let M=class extends t{constructor(e){super(e),this.scaleComputedFrom=r(),this.view=null}get state(){return this.view?.ready&&"2d"===this.view.type?w(this.view)&&!this.view.inGeographicLayout?"disabled":"ready":"disabled"}getScaleBarProperties(e,t){if("disabled"===this.state||isNaN(e)||!t||!this.view)return null;const r=this._getDistanceInMeters();return null==r?null:this._getScaleBarProps(e,r,t)}_getDistanceInMeters(){const{state:e,spatialReference:t}=this.view;if(!d(t))return null;const{isGeographic:r}=t,s=j(t);if(!r&&!s){return e.extent.width*(i(t)??1)}const[o,n]=this._getScaleMeasuringPoints(),a=s||r&&!l(t)?m.WGS84:t,f=new c({paths:[[v(s,o),v(s,n)]],spatialReference:a}),h=u(f,10);let w;try{[w]=p([h],"meters")}catch{return null}return w}_getScaleMeasuringPoints(){const e=this.view,{width:t,height:i,position:s,spatialReference:o}=e;if(S(e)){const e=f(o),{valid:t}=e;return[new a(t[0],0,o),new a(t[1],0,o)]}let n=this.scaleComputedFrom.y-s[1];n>i?n=i:n<0&&(n=0);const c=r(0,n),m=r(t,n);return[e.toMap(c),e.toMap(m)]}_getScaleBarProps(e,t,r){const i=this.view,s=e*t/(S(i)?i.state.paddedViewState.worldScreenWidth:i.width);if(s<.001)return null;const o=y(s,r),{distance:n,unit:a}=o;let c=n,m=0;for(;c>=1;)c/=10,m++;const{min:l,max:p}=g(c),u=p/c>=c/l?l:p;return{length:e*(u/c),value:10**m*u,unit:a}}};e([o()],M.prototype,"scaleComputedFrom",void 0),e([o({readOnly:!0})],M.prototype,"state",null),e([o()],M.prototype,"view",void 0),M=e([n("esri.widgets.ScaleBar.ScaleBarViewModel")],M);const _=M;export{_ as default};