Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /*global define, i18n */
  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(GlobalStorage.config.geometryService);
  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. //identify proxy page to use if the toJson payload to the geometry service is greater than 2000 characters.
  119. //If this null or not available the project and lengths operation will not work. Otherwise it will do a http post to the proxy.
  120. esriConfig.defaults.io.proxyUrl = GlobalStorage.config.proxyUrl;
  121. esriConfig.defaults.io.alwaysUseProxy = false;
  122. }
  123. };
  124.  
  125. /**
  126. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  127. *
  128. * @method activate
  129. * @private
  130. */
  131. function activate() {
  132. measureApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  133.  
  134. displayOutput(i18n.t(that.ns + ":na"), i18n.t(that.ns + ":na"));
  135. }
  136.  
  137. /**
  138. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  139. *
  140. * @method deactivate
  141. * @private
  142. */
  143. function deactivate() {
  144. measureApp.toolbar.deactivate();
  145. clearMap();
  146. }
  147.  
  148. /**
  149. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  150. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  151. *
  152. * @method clearMap
  153. * @private
  154. */
  155. function clearMap() {
  156. measureApp.map.graphics.clear();
  157.  
  158. displayOutput(i18n.t(that.ns + ":na"), i18n.t(that.ns + ":na"));
  159. }
  160.  
  161. /**
  162. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  163. *
  164. * @method displayOutput
  165. * @private
  166. */
  167. function displayOutput(length, area, lengthUnits, areaUnits) {
  168. that.displayTemplateOutput(
  169. {
  170. lengthLabel: i18n.t(that.ns + ":length"),
  171. areaLabel: i18n.t(that.ns + ":area"),
  172. lengthOutput: length,
  173. areaOutput: area,
  174. lengthUnits: lengthUnits,
  175. areaUnits: areaUnits
  176. }
  177. );
  178. }
  179.  
  180. return dojoLang.mixin({}, BaseTool, {
  181. /**
  182. * Initialize the population tool
  183. *
  184. * @method init
  185. * @constructor
  186. *
  187. */
  188. init: function (selector, d) {
  189. that = this;
  190. this.initToggle(selector, d,
  191. {
  192. activate: activate,
  193. deactivate: deactivate,
  194. defaultAction: clearMap
  195. }
  196. );
  197.  
  198. ui.init();
  199.  
  200. return this;
  201. },
  202.  
  203. name: "areaTool"
  204. });
  205. });