undefined

API Docs for: 5.4.0
Show:

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

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