UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 3.19 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../core/Accessor.js";import{createScreenPoint as r}from"../../core/screenUtils.js";import{getMetersPerUnit as i,convertUnit as s}from"../../core/units.js";import{property as o,subclass as n}from"../../core/accessorSupport/decorators.js";import{isSupported as a,geodesicLengths as c}from"../../geometry/geodesicUtils.js";import l from"../../geometry/Point.js";import m from"../../geometry/Polyline.js";import u from"../../geometry/SpatialReference.js";import{straightLineDensify as p}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 S(e){const{isWebMercator:t,wkt:r,wkt2:i}=e;return t||((i||r)?.includes("WGS_1984_Web_Mercator")??!1)}function v({state:{paddedViewState:e},spatialReference:t,width:r}){return t.isWrappable&&e.worldScreenWidth<r}function j(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=S(t);if(!r&&!s){return e.extent.width*(i(t)??1)}const[o,n]=this._getScaleMeasuringPoints(),l=s||r&&!a(t)?u.WGS84:t,f=new m({paths:[[j(s,o),j(s,n)]],spatialReference:l}),h=p(f,10);let w;try{[w]=c([h],"meters")}catch{return null}return w}_getScaleMeasuringPoints(){const e=this.view,{width:t,height:i,position:s,spatialReference:o}=e;if(v(e)){const e=f(o),{valid:t}=e;return[new l(t[0],0,o),new l(t[1],0,o)]}let n=this.scaleComputedFrom.y-s[1];n>i?n=i:n<0&&(n=0);const a=r(0,n),c=r(t,n);return[e.toMap(a),e.toMap(c)]}_getScaleBarProps(e,t,r){const i=this.view,s=e*t/(v(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,l=0;for(;c>=1;)c/=10,l++;const{min:m,max:u}=g(c),p=u/c>=c/m?m:u;return{length:e*(p/c),value:10**l*p,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};