Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

File: src\js\RAMP\Tools\areaTool.js

  1. /*global define, i18n, RAMP */
  2.  
  3. /**
  4. * MeasureTool submodule.
  5. *
  6. * Computes the area and perimeter length of a selected area. When the user draws a polygon, the area
  7. * and length will be displayed in the bottom right corner.
  8. *
  9. * @module RAMP
  10. * @submodule MeasureTool
  11. * @main MeasureTool
  12. */
  13.  
  14. /**
  15. * MeasureTool class.
  16. *
  17. * @class MeasureTool
  18. * @static
  19. * @uses dojo/dom
  20. * @uses dojo/_base/lang
  21. * @uses esri/config
  22. * @uses esri/graphic
  23. * @uses esri/tasks/GeometryService
  24. * @uses esri/tasks/AreasAndLengthsParameters
  25. * @uses esri/toolbars/draw
  26. * @uses esri/symbols/SimpleFillSymbol
  27. * @uses Map
  28. * @uses GlobalStorage
  29. */
  30.  
  31. define([
  32. // Dojo
  33. "dojo/dom", "dojo/string", "dojo/_base/lang",
  34. // Esri
  35. "esri/config", "esri/graphic", "esri/tasks/GeometryService",
  36. "esri/tasks/AreasAndLengthsParameters", "esri/toolbars/draw", "esri/symbols/SimpleFillSymbol",
  37. // Ramp
  38. "ramp/map", "ramp/globalStorage", "tools/baseTool"
  39. ],
  40. function (
  41. // Dojo
  42. dom, string, dojoLang,
  43. // Esri
  44. esriConfig, Graphic, GeometryService, AreasAndLengthsParameters, Draw, SimpleFillSymbol,
  45. // Ramp
  46. RampMap, GlobalStorage, BaseTool) {
  47. "use strict";
  48.  
  49. var ui,
  50. geometryService,
  51. measureApp,
  52. that;
  53.  
  54. /**
  55. * Compute the area and length of a specified polygon.
  56. *
  57. * @method computeAreaAndLength
  58. * @private
  59. * @param {Object} evtObj an object representing the event.
  60. *
  61. */
  62. function computeAreaAndLength(evtObj) {
  63. that.working(true);
  64.  
  65. geometryService = new GeometryService(RAMP.config.geometryServiceUrl);
  66. geometryService.on("areas-and-lengths-complete", outputAreaAndLength);
  67.  
  68. var geometry = evtObj.geometry;
  69. measureApp.map.graphics.clear();
  70.  
  71. measureApp.map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
  72.  
  73. //setup the parameters for the areas and lengths operation
  74. var areasAndLengthParams = new AreasAndLengthsParameters();
  75. areasAndLengthParams.lengthUnit = GeometryService.UNIT_KILOMETER;
  76. areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_KILOMETERS;
  77. areasAndLengthParams.calculationType = "geodesic";
  78. geometryService.simplify([geometry], function (simplifiedGeometries) {
  79. areasAndLengthParams.polygons = simplifiedGeometries;
  80. geometryService.areasAndLengths(areasAndLengthParams);
  81. });
  82. }
  83.  
  84. /**
  85. * Display the calculated area and length on the map.
  86. *
  87. * @method outputAreaAndLength
  88. * @private
  89. * @param {Object} evtObj an object representing the event.
  90. *
  91. */
  92. function outputAreaAndLength(evtObj) {
  93. var result = evtObj.result,
  94. // Convert acres to km2.
  95. area = result.areas[0].toFixed(3),// (result.areas[0] / 247.11).toFixed(3),
  96. // Convert feet to km.
  97. length = result.lengths[0].toFixed(3);// (result.lengths[0] / 3280.8).toFixed(3);
  98.  
  99. that.working(false);
  100.  
  101. length = string.substitute("${number:dojo.number.format}", { number: length });
  102. area = string.substitute("${number:dojo.number.format}", { number: area });
  103. displayOutput(length, area, "km", "km");
  104. }
  105.  
  106. ui = {
  107. init: function () {
  108. var map = RampMap.getMap(),
  109. toolbar = new Draw(map);
  110.  
  111. toolbar.on("draw-end", computeAreaAndLength);
  112.  
  113. measureApp = {
  114. map: map,
  115. toolbar: toolbar
  116. };
  117. }
  118. };
  119.  
  120. /**
  121. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  122. *
  123. * @method activate
  124. * @private
  125. */
  126. function activate() {
  127. measureApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  128.  
  129. displayOutput(i18n.t(that.ns + ":na"), i18n.t(that.ns + ":na"));
  130. }
  131.  
  132. /**
  133. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  134. *
  135. * @method deactivate
  136. * @private
  137. */
  138. function deactivate() {
  139. measureApp.toolbar.deactivate();
  140. clearMap();
  141. }
  142.  
  143. /**
  144. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  145. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  146. *
  147. * @method clearMap
  148. * @private
  149. */
  150. function clearMap() {
  151. measureApp.map.graphics.clear();
  152.  
  153. displayOutput(i18n.t(that.ns + ":na"), i18n.t(that.ns + ":na"));
  154. }
  155.  
  156. /**
  157. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  158. *
  159. * @method displayOutput
  160. * @private
  161. */
  162. function displayOutput(length, area, lengthUnits, areaUnits) {
  163. that.displayTemplateOutput(
  164. {
  165. lengthLabel: i18n.t(that.ns + ":length"),
  166. areaLabel: i18n.t(that.ns + ":area"),
  167. lengthOutput: length,
  168. areaOutput: area,
  169. lengthUnits: lengthUnits,
  170. areaUnits: areaUnits
  171. }
  172. );
  173. }
  174.  
  175. return dojoLang.mixin({}, BaseTool, {
  176. /**
  177. * Initialize the population tool
  178. *
  179. * @method init
  180. * @constructor
  181. *
  182. */
  183. init: function (selector, d) {
  184. that = this;
  185. this.initToggle(selector, d,
  186. {
  187. activate: activate,
  188. deactivate: deactivate,
  189. defaultAction: clearMap
  190. }
  191. );
  192.  
  193. ui.init();
  194.  
  195. return this;
  196. },
  197.  
  198. name: "areaTool"
  199. });
  200. });