UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 9.79 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../Graphic.js";import"../../intl.js";import{getContrast as i,multiplyOpacity as r,BrightnessThreshold as s}from"../../core/colorUtils.js";import{handlesGroup as o}from"../../core/handleUtils.js";import{destroyMaybe as n}from"../../core/maybe.js";import{watch as a,initial as l}from"../../core/reactiveUtils.js";import{formatDecimal as p,formatImperialLength as m,formatMetricLength as c}from"../../core/unitFormatUtils.js";import{convertUnit as h}from"../../core/unitUtils.js";import{property as u}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/Logger.js";import"../../core/RandomLCG.js";import{subclass as d}from"../../core/accessorSupport/decorators/subclass.js";import{UpdatingHandles as y}from"../../core/support/UpdatingHandles.js";import v from"../../geometry/Point.js";import g from"../../geometry/Polyline.js";import{e as _,l as f}from"../../chunks/geodeticDensifyOperator.js";import{e as w,l as L}from"../../chunks/geodeticLengthOperator.js";import{e as j}from"../../chunks/lengthOperator.js";import{isSupported as b,geodesicLengths as S,geodesicDensify as M}from"../../geometry/support/geodesicUtils.js";import x from"../../layers/GraphicsLayer.js";import C from"../../symbols/CIMSymbol.js";import A from"../../symbols/Font.js";import k from"../../symbols/SimpleMarkerSymbol.js";import G from"../../symbols/TextSymbol.js";import{ViewingMode as O}from"../../views/ViewingMode.js";import{SnappingVisualizer2D as D}from"../../views/2d/interactive/SnappingVisualizer2D.js";import H from"../../views/draw/Draw.js";import{createCoordinateHelper as P}from"../../views/interactive/coordinateHelper.js";import{createManipulatorDragEventPipeline as R,screenToMap as U,dragManipulatedObject as T}from"../../views/interactive/dragEventPipeline.js";import{GraphicManipulator as E}from"../../views/interactive/GraphicManipulator.js";import{InteractiveToolBase as I}from"../../views/interactive/InteractiveToolBase.js";import{EditGeometry as z}from"../../views/interactive/editGeometry/EditGeometry.js";import{EditGeometryOperations as V}from"../../views/interactive/editGeometry/EditGeometryOperations.js";import{SnappingContext as B}from"../../views/interactive/snapping/SnappingContext.js";import{createSnapDragEventPipelineStep as F}from"../../views/interactive/snapping/SnappingDragPipelineStep.js";import{setupSnappingToggleHandles as W}from"../../views/interactive/snapping/snappingUtils.js";import{fetchMessageBundle as N}from"../../intl/messages.js";import{onLocaleChange as q}from"../../intl/locale.js";const J=1e5;let K=class extends I{constructor(e){super(e),this._measurementLayer=new x({internal:!0,listMode:"hide",visible:!1}),this._manipulatorLayer=new x({internal:!0,listMode:"hide",visible:!1}),this._snappingLayer=new x({internal:!0,listMode:"hide",visible:!1}),this._updatingHandles=new y,this._vertices=[],this._drawActive=!1,this.measurement=null,this.measurementLabel=null}initialize(){N("esri/core/t9n/Units").then((e=>{this.messages=e}));const e=this.view;this._draw=new H({view:e}),e.map.addMany([this._measurementLayer,this._manipulatorLayer,this._snappingLayer]),e.focus(),this.addHandles([q((async()=>{this.messages=await N("esri/core/t9n/Units")})),a((()=>[this.unit,this.messages]),(()=>{this._updatePolylines()}),l),a((()=>[this.view.effectiveTheme.accentColor,this.view.effectiveTheme.textColor]),(()=>{if(!this._vertices.length)return;const e=this._vertices.map((({coord:e})=>e));for(const{handle:t}of this._vertices)t.remove();this._vertices=[],this._measurementLayer.removeAll(),this._manipulatorLayer.removeAll(),this._snappingLayer.removeAll(),this.manipulators.removeAll(),this._updateSketch(e)}))]),W(this)}destroy(){const{map:e}=this.view;this._draw.view=null,this._draw=n(this._draw),e.removeMany([this._measurementLayer,this._manipulatorLayer,this._snappingLayer]),this._measurementLayer.removeAll(),this._manipulatorLayer.removeAll(),this._snappingLayer.removeAll(),this._set("measurement",null),this._set("measurementLabel",null),this._measurementLayer=n(this._measurementLayer),this._manipulatorLayer=n(this._manipulatorLayer),this._snappingLayer=n(this._snappingLayer),this._updatingHandles=n(this._updatingHandles),this._resetVertices()}get cursor(){return this._drawActive?"crosshair":null}set editable(e){this._set("editable",e),e||this._draw.reset()}get snappingOptions(){return this.snappingManager.options}get updating(){return this._updatingHandles.updating||this.snappingManager.updating}onActivate(){this._drawActive||0!==this._vertices.length||this._startSketch()}onShow(){this._measurementLayer.visible=!0,this._manipulatorLayer.visible=!0,this._snappingLayer.visible=!0}onHide(){this._measurementLayer.visible=!1,this._manipulatorLayer.visible=!1,this._snappingLayer.visible=!1}_getSnappingContext(e){return new B({elevationInfo:{mode:"on-the-ground",offset:0},pointer:e,editGeometryOperations:new V(new z("point",P(!1,!1,this.view.spatialReference)),O.Local),visualizer:new D(this._snappingLayer)})}_resetVertices(){for(const{handle:e}of this._vertices)e.remove();this._vertices=[]}_startSketch(){this._drawActive=!0;const{snappingManager:e}=this,t=this._draw.create("polyline",{mode:"click",snappingManager:e});t.on(["vertex-add","vertex-update","vertex-remove","cursor-update","undo","redo"],(e=>this._updateSketch(e.vertices))),t.on("draw-complete",(()=>{this._stopSketch()}))}_stopSketch(){this.manipulators.forEach((({manipulator:e})=>{e.interactive=!0})),this._drawActive=!1,this.finishToolCreation()}_updateSketch(e){const{spatialReference:t}=this.view;for(;this._vertices.length>e.length;){const{handle:e,manipulator:t}=this._vertices.pop();e.remove(),this.manipulators.remove(t)}for(let l=this._vertices.length;l<e.length;l++){const[i,r]=e[l],s=new v({x:i,y:r,spatialReference:t}),n=Q(s,this.view,this._manipulatorLayer);this.manipulators.add(n);const a=V.fromGeometry(s,O.Local),p=[a.on("vertex-update",(e=>{const t=e.vertices.at(0);t&&(this._vertices[l].coord=[t.pos[0],t.pos[1]],this._updatePolylines())})),R(n,((e,t,i,r)=>{const s=this._getSnappingContext(r),{snappingManager:o}=this,n=this._updatingHandles,{snappingStep:l}=F({snappingContext:s,snappingManager:o,updatingHandles:n});t.next(U(this.view)).next((t=>{if("start"===t.action){const i=new v(t.mapStart);e.graphic.geometry=i,a.trySetGeometry(i)}return t})).next(...l).next(T({operations:a})).next((()=>{e.graphic.geometry=a.data.geometry}))}))];this._vertices.push({manipulator:n,coord:[i,r],handle:o(p)})}const i=this._vertices.length-1,r=this._vertices[i],[s,n]=e[i];r.coord[0]===s&&r.coord[1]===n||(r.coord=[s,n],r.manipulator.graphic.geometry=new v({x:s,y:n,spatialReference:t}));const a=this._drawActive?this._vertices[i].manipulator:null;this.manipulators.forEach((({manipulator:e})=>{e.interactive=null==a||e!==a})),this._updatePolylines()}_updatePolylines(){if(this._vertices.length<2)return void this._measurementLayer.removeAll();const e=X(this._vertices.map((({coord:e})=>e)),this.view.spatialReference);if(!e)return;const{measurement:o,drawing:n,original:a}=e;this._set("measurement",o);const l=$(this.messages,o,this.unit);let p,m;this._set("measurementLabel",l);const{graphics:c}=this._measurementLayer,h=2===c.length,{effectiveTheme:u}=this.view;h?(p=c.at(0),m=c.at(1)):(p=new t({symbol:new C({data:{type:"CIMSymbolReference",symbol:{type:"CIMLineSymbol",symbolLayers:[{type:"CIMSolidStroke",effects:[{type:"CIMGeometricEffectDashes",dashTemplate:[14,12],lineDashEnding:"FullGap",controlPointEnding:"NoConstraint"}],enable:!0,capStyle:"Butt",joinStyle:"Round",width:3.5,color:i(u.accentColor).toArray()},{type:"CIMSolidStroke",enable:!0,capStyle:"Butt",joinStyle:"Round",width:5,color:u.accentColor.toArray()}]}}})}),m=new t({symbol:new G({color:u.textColor,haloColor:r(i(u.textColor,s.Low),.5),haloSize:2,font:new A({size:14,family:"sans-serif"})})}),c.removeAll(),c.addMany([p,m])),p.geometry=n,m.geometry=a.extent?.center,m.symbol.text=l}};function Q(e,i,s){const o=r(i.effectiveTheme.accentColor,.5),n={type:"simple-line",width:0},a=8,l=new k({style:"circle",color:o,size:a,outline:n}),p=new k({style:"circle",color:o,size:1.5*a,outline:n}),m=new t({geometry:e,symbol:l});return new E({view:i,layer:s,graphic:m,focusedSymbol:p})}function X(e,t){const i=new g({paths:[e],spatialReference:t});let r,s;return t.isGeographic?b(t)?(r=S([i],"meters")[0],s=M(i,J)):(r=w(i,{unit:"meters"}),s=_(i,J,{unit:"meters"})):t.isWebMercator?(r=w(i,{unit:"meters"}),s=_(i,J,{unit:"meters"})):(r=j(i,{unit:"meters"}),s=i),{measurement:{geometry:s,length:r},original:i,drawing:s}}function Y(e){return null!=e}async function Z(e){(e?.isGeographic&&!b(e)||e?.isWebMercator)&&await Promise.all([f(),L()])}function $(e,t,i){if(!t||!e)return"";switch(i){case"metric":return c(e,t.length,"meters");case"imperial":return m(e,t.length,"meters");default:return p(e,h(t.length,"meters",i),i)}}e([u()],K.prototype,"_drawActive",void 0),e([u({readOnly:!0})],K.prototype,"cursor",null),e([u({value:!0})],K.prototype,"editable",null),e([u({readOnly:!0})],K.prototype,"measurement",void 0),e([u({readOnly:!0})],K.prototype,"measurementLabel",void 0),e([u()],K.prototype,"messages",void 0),e([u({constructOnly:!0})],K.prototype,"snappingManager",void 0),e([u()],K.prototype,"unit",void 0),e([u()],K.prototype,"updating",null),e([u({constructOnly:!0})],K.prototype,"view",void 0),K=e([d("esri.widgets.DistanceMeasurement2D.DistanceMeasurement2DTool")],K);export{K as DistanceMeasurement2DTool,X as createDistanceMeasurementInfo2D,$ as createDistanceMeasurementLabel,Y as isSupported,Z as loadGeometryOperators};