@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 10.8 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../Graphic.js";import{fetchMessageBundle as r,onLocaleChange as i}from"../../intl.js";import{multiplyOpacity as s,getContrast as a}from"../../core/colorUtils.js";import{destroyMaybe as n}from"../../core/maybe.js";import{watch as o,initial as m}from"../../core/reactiveUtils.js";import{formatDecimal as l,formatImperialArea as p,formatMetricArea as c,formatImperialLength as h,formatMetricLength as u}from"../../core/unitFormatUtils.js";import{convertUnit as d,deriveLengthUnitFromAreaUnit as v}from"../../core/units.js";import{property as y,subclass as g}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as f}from"../../core/support/UpdatingHandles.js";import{isSupported as _,geodesicDensify as w,geodesicLengths as b,geodesicAreas as L}from"../../geometry/geodesicUtils.js";import j from"../../geometry/Point.js";import S from"../../geometry/Polygon.js";import A from"../../geometry/Polyline.js";import{e as G}from"../../chunks/areaOperator.js";import{e as k,l as x}from"../../chunks/geodeticAreaOperator.js";import{e as M,l as O}from"../../chunks/geodeticDensifyOperator.js";import{e as C,l as H}from"../../chunks/geodeticLengthOperator.js";import{e as R}from"../../chunks/lengthOperator.js";import{e as T}from"../../chunks/simplifyOperator.js";import{polygonCentroidPoint as U}from"../../geometry/support/centroid.js";import q from"../../layers/GraphicsLayer.js";import z from"../../symbols/Font.js";import D from"../../symbols/SimpleFillSymbol.js";import P from"../../symbols/SimpleLineSymbol.js";import E from"../../symbols/SimpleMarkerSymbol.js";import V from"../../symbols/TextSymbol.js";import{SnappingVisualizer2D as F}from"../../views/2d/interactive/SnappingVisualizer2D.js";import W from"../../views/draw/Draw.js";import{createCoordinateHelper as I}from"../../views/interactive/coordinateHelper.js";import{createManipulatorDragEventPipeline as B,screenToMap as J,dragManipulatedObject as N}from"../../views/interactive/dragEventPipeline.js";import{GraphicManipulator as K}from"../../views/interactive/GraphicManipulator.js";import{InteractiveToolBase as Q}from"../../views/interactive/InteractiveToolBase.js";import{EditGeometry as X}from"../../views/interactive/editGeometry/EditGeometry.js";import{EditGeometryOperations as Y}from"../../views/interactive/editGeometry/EditGeometryOperations.js";import{SnappingContext as Z}from"../../views/interactive/snapping/SnappingContext.js";import{createSnapDragEventPipelineStep as $}from"../../views/interactive/snapping/SnappingDragPipelineStep.js";import{setupSnappingToggleHandles as ee}from"../../views/interactive/snapping/snappingUtils.js";const te=1e5;let re=class extends Q{constructor(e){super(e),this._measurementLayer=new q({internal:!0,listMode:"hide",visible:!1,title:"Area measurement layer"}),this._manipulatorLayer=new q({internal:!0,listMode:"hide",visible:!1,title:"Area measurement manipulator layer"}),this._snappingLayer=new q({internal:!0,listMode:"hide",visible:!1,title:"Area measurement snapping layer"}),this._updatingHandles=new f,this._vertices=[],this._drawActive=!1,this.measurement=null,this.measurementLabel=null}initialize(){r("esri/core/t9n/Units").then(e=>{this.messages=e});const e=this.view;this._draw=new W({view:e}),e.map.addMany([this._measurementLayer,this._manipulatorLayer,this._snappingLayer]),e.focus(),this.addHandles([i(async()=>{this.messages=await r("esri/core/t9n/Units")}),o(()=>[this.unit,this.messages],()=>{this._updateGraphics()},m),o(()=>[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)})]),ee(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}reset(){this.manipulators.removeAll(),this._resetVertices(),this._measurementLayer.removeAll(),this._manipulatorLayer.removeAll(),this._set("measurement",null),this._set("measurementLabel",null),this._draw.reset(),this._drawActive=!1}_getSnappingContext(e){return new Z({elevationInfo:{mode:"on-the-ground",offset:0},pointer:e,editGeometryOperations:new Y(new X("point",I(!1,!1,this.view.spatialReference)),2),visualizer:new F(this._snappingLayer)})}_resetVertices(){for(const{handle:e}of this._vertices)e.remove();this._vertices=[]}_startSketch(){this._drawActive=!0;const{snappingManager:e}=this;this._draw.create("polyline",{mode:"click",snappingManager:e}).on(["draw-complete","vertex-add","vertex-update","vertex-remove","cursor-update","undo","redo"],e=>{this._updateSketch(e.vertices),"draw-complete"===e.type&&this._stopSketch()})}_stopSketch(){if(this._vertices.length<3)return this.reset(),void this._startSketch();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 r=this._vertices.length;r<e.length;r++){const[i,s]=e[r],a=new j({x:i,y:s,spatialReference:t}),n=ie(a,this.view,this._manipulatorLayer);this.manipulators.add(n);const o=Y.fromGeometry(a,2);o.on("vertex-update",e=>{const t=e.vertices.at(0);t&&(this._vertices[r].coord=[t.pos[0],t.pos[1]],this._updateGraphics())});const m=B(n,(e,t,r,i)=>{const s=this._getSnappingContext(i),{snappingManager:a}=this,n=this._updatingHandles,{snappingStep:m}=$({snappingContext:s,snappingManager:a,updatingHandles:n});t.next(J(this.view)).next(t=>{if("start"===t.action){const r=new j(t.mapStart);e.graphic.geometry=r,o.trySetGeometry(r)}return t}).next(...m).next(N({operations:o})).next(()=>{e.graphic.geometry=o.data.geometry})});this._vertices.push({manipulator:n,coord:[i,s],handle:m})}if(this._vertices.length){const r=this._vertices.length-1,i=this._vertices[r],[s,a]=e[r];i.coord[0]===s&&i.coord[1]===a||(i.coord=[s,a],i.manipulator.graphic.geometry=new j({x:s,y:a,spatialReference:t}));const n=this._drawActive?this._vertices[r].manipulator:null;this.manipulators.forEach(({manipulator:e})=>{e.interactive=null==n||e!==n})}this._updateGraphics()}_updateGraphics(){if(this._vertices.length<2)return void this._measurementLayer.removeAll();const e=se(this._vertices.map(({coord:e})=>e),this.view.spatialReference);if(!e)return;const{measurement:r,fillGeometry:i,outlineGeometry:n}=e;this._set("measurement",r);const o=r?oe(this.messages,r,this.unit):null;if(this._set("measurementLabel",o),!i&&!n)return;let m,l,p;const{graphics:c}=this._measurementLayer,h=3===c.length,{effectiveTheme:u}=this.view;h?(m=c.at(0),l=c.at(1),p=c.at(2)):(m=new t({symbol:new D({color:s(u.accentColor,.3),outline:null})}),l=new t({symbol:new P({color:u.accentColor,width:2})}),p=new t({symbol:new V({color:u.textColor,font:new z({size:14,family:"sans-serif"}),haloColor:s(a(u.textColor,160),.5),haloSize:2})}),c.removeAll(),c.addMany([m,l,p])),m.geometry=i,l.geometry=n;const d=i?U(i):null,v=d?j.fromJSON(d):null;p.geometry=v,p.symbol.text=o?.area??""}};function ie(e,r,i){const a=8,n={style:"circle",color:s(r.effectiveTheme.accentColor,.5),outline:{type:"simple-line",width:0}},o=new E({...n,size:a}),m=new E({...n,size:1.5*a}),l=new t({geometry:e,symbol:o});return new K({view:r,layer:i,graphic:l,focusedSymbol:m})}function se(e,t){if(2===e.length){const r=new A({paths:[e],spatialReference:t});let i;return i=t.isGeographic?_(t)?w(r,te):M(r,te,{unit:"meters"}):t.isWebMercator?M(r,te,{unit:"meters"}):r,{measurement:null,fillGeometry:null,outlineGeometry:i}}e.push(e[0]);const r=new A({paths:[e],spatialReference:t}),i=new S({rings:[e],spatialReference:t});let s,a,n=null,o=null;if(t.isGeographic)if(_(t)){if(n=w(r,te),o=w(i,te),o=T(o),!o)return null;s=b([r],"meters")[0],a=L([o],"square-meters")[0]}else{if(n=M(r,te,{unit:"meters"}),o=M(i,te,{unit:"meters"}),o=T(o),!o)return null;s=C(r,{unit:"meters"}),a=k(o,{unit:"square-meters"})}else if(t.isWebMercator){if(n=M(r,te,{unit:"meters"}),o=M(i,te,{unit:"meters"}),o=T(o),!o)return null;s=C(r,{unit:"meters"}),a=k(o,{unit:"square-meters"})}else{if(n=r,o=T(i),!o)return null;s=R(r,{unit:"meters"}),a=G(o,{unit:"square-meters"})}return{measurement:{geometry:o,area:a,perimeter:s},fillGeometry:o,outlineGeometry:n}}function ae(e){return null!=e}async function ne(e){(e?.isGeographic&&!_(e)||e?.isWebMercator)&&await Promise.all([x(),O(),H()])}function oe(e,t,r){if(!t||!e)return null;const i={area:null,perimeter:null},{area:s,perimeter:a}=t;switch(r){case"metric":i.area=c(e,s,"square-meters");break;case"imperial":i.area=p(e,s,"square-meters");break;default:{const t=d(s,"square-meters",r);i.area=l(e,t,r);break}}const n=v(r);if(n)switch(n){case"metric":i.perimeter=u(e,a,"meters");break;case"imperial":i.perimeter=h(e,a,"meters");break;default:{const t=d(a,"meters",n);i.perimeter=l(e,t,n);break}}else i.perimeter="";return i}e([y()],re.prototype,"_drawActive",void 0),e([y({readOnly:!0})],re.prototype,"cursor",null),e([y({value:!0})],re.prototype,"editable",null),e([y({readOnly:!0})],re.prototype,"measurement",void 0),e([y({readOnly:!0})],re.prototype,"measurementLabel",void 0),e([y()],re.prototype,"messages",void 0),e([y({constructOnly:!0})],re.prototype,"snappingManager",void 0),e([y()],re.prototype,"unit",void 0),e([y()],re.prototype,"updating",null),e([y({constructOnly:!0})],re.prototype,"view",void 0),re=e([g("esri.widgets.AreaMeasurement2D.AreaMeasurement2DTool")],re);export{re as AreaMeasurement2DTool,se as createAreaMeasurementInfo2D,oe as createAreaMeasurementLabel,ae as isSupported,ne as loadGeometryOperators};