Reusable Accessible Mapping Platform

API Docs for: 5.0.0
Show:

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

  1. /*global define, i18n, RAMP */
  2.  
  3. /**
  4. * @module Tools
  5. */
  6.  
  7. /**
  8. * BufferTool.
  9. *
  10. * Adds a buffer around the a selected area. The user will be able to specify the distance
  11. * in the bottom right corner, then draw a polygon on the map.
  12. *
  13. * @class BufferTool
  14. * @static
  15. * @uses dojo/_base/array
  16. * @uses dojo/_base/Color
  17. * @uses esri/config
  18. * @uses esri/graphic
  19. * @uses esri/symbols/SimpleLineSymbol
  20. * @uses esri/symbols/SimpleFillSymbol
  21. * @uses esri/tasks/GeometryService
  22. * @uses esri/tasks/BufferParameters
  23. * @uses esri/toolbars/draw
  24. * @uses Map
  25. * @uses GlobalStorage
  26. * @extends BaseTool
  27. */
  28.  
  29. define([
  30. // Dojo
  31. "dojo/dom",
  32. "dojo/_base/array",
  33. "dojo/_base/Color",
  34. "dojo/_base/lang",
  35. // Esri
  36. "esri/config",
  37. "esri/graphic",
  38. "esri/tasks/GeometryService",
  39. "esri/tasks/BufferParameters",
  40. "esri/toolbars/draw",
  41. "esri/symbols/SimpleLineSymbol",
  42. "esri/symbols/SimpleFillSymbol",
  43. // Ramp
  44. "ramp/map", "ramp/globalStorage", "tools/baseTool"
  45. ],
  46.  
  47. function (
  48. // Dojo
  49. dom, array, Color, dojoLang,
  50. // Esri
  51. esriConfig, Graphic, GeometryService, BufferParameters, Draw, SimpleLineSymbol, SimpleFillSymbol,
  52. // Ramp
  53. RampMap, GlobalStorage, BaseTool) {
  54. "use strict";
  55. var ui,
  56. bufferApp,
  57. that;
  58.  
  59. /**
  60. * Compute the buffer of a specified polygon.
  61. *
  62. * @method computeBuffer
  63. * @private
  64. * @param {Object} evtObj an object representing the `draw-end` event.
  65. *
  66. */
  67. function computeBuffer(evtObj) {
  68. var geometry = evtObj.geometry,
  69. map = bufferApp.map,
  70. geometryService = new GeometryService(RAMP.config.geometryServiceUrl),
  71.  
  72. symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NONE,
  73. new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT,
  74. new Color([255, 0, 0, 1]), new Color([0, 255, 0, 0.25]))),
  75.  
  76. graphic = new Graphic(geometry, symbol);
  77.  
  78. that.working(true);
  79.  
  80. map.graphics.add(graphic);
  81.  
  82. //setup the buffer parameters
  83. var params = new BufferParameters(),
  84.  
  85. // Get rid of all non-numerical/non-period characters.
  86. distanceInput = that.outputFloat.find(".distance-input").val().replace(/[^0-9\.]+/g, '');
  87.  
  88. if (distanceInput === "") {
  89. that.working(false);
  90. } else {
  91. params.distances = [distanceInput];
  92. params.bufferSpatialReference = bufferApp.map.spatialReference;
  93. params.outSpatialReference = bufferApp.map.spatialReference;
  94. params.unit = 9036; // Kilometers
  95.  
  96. // Simplify polygon. this will make the user drawn polygon topologically correct.
  97. geometryService.simplify([geometry], function (geometries) {
  98. params.geometries = geometries;
  99. geometryService.buffer(params, outputBuffer);
  100. });
  101. }
  102. }
  103.  
  104. /**
  105. * Display the buffered polygon on the map.
  106. *
  107. * @method outputBuffer
  108. * @private
  109. * @param {Object} bufferedGeometries result of the geoprocessor.
  110. *
  111. */
  112. function outputBuffer(bufferedGeometries) {
  113. var symbol = new SimpleFillSymbol(
  114. SimpleFillSymbol.STYLE_SOLID,
  115. new SimpleLineSymbol(
  116. SimpleLineSymbol.STYLE_SOLID,
  117. new Color([255, 0, 0, 0.65]), 2
  118. ),
  119. new Color([255, 0, 0, 0.35])
  120. );
  121.  
  122. array.forEach(bufferedGeometries, function (geometry) {
  123. var graphic = new Graphic(geometry, symbol);
  124. bufferApp.map.graphics.add(graphic);
  125. });
  126. //TODO if we change to an "always on" we will want to make this a public function like the activate function below
  127.  
  128. bufferApp.map.showZoomSlider();
  129.  
  130. that.working(false);
  131. }
  132.  
  133. ui = {
  134. /**
  135. * Initiates additional UI components of the Tool.
  136. *
  137. * @method ui.init
  138. * @private
  139. */
  140. init: function () {
  141. var map = RampMap.getMap(),
  142. toolbar = new Draw(map);
  143.  
  144. toolbar.on("draw-end", computeBuffer);
  145.  
  146. bufferApp = {
  147. map: map,
  148. toolbar: toolbar
  149. };
  150. }
  151. };
  152.  
  153. /**
  154. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  155. *
  156. * @method activate
  157. * @private
  158. */
  159. function activate() {
  160. bufferApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  161.  
  162. displayOutput();
  163. }
  164.  
  165. /**
  166. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  167. *
  168. * @method deactivate
  169. * @private
  170. */
  171. function deactivate() {
  172. bufferApp.toolbar.deactivate();
  173. clearMap();
  174. }
  175.  
  176. /**
  177. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  178. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  179. *
  180. * @method clearMap
  181. * @private
  182. */
  183. function clearMap() {
  184. bufferApp.map.graphics.clear();
  185. }
  186.  
  187. /**
  188. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  189. *
  190. * @method displayOutput
  191. * @private
  192. */
  193. function displayOutput() {
  194. that.displayTemplateOutput(
  195. {
  196. distanceLabel: i18n.t(that.ns + ":distance")
  197. }
  198. );
  199. }
  200.  
  201. return dojoLang.mixin({}, BaseTool, {
  202. /**
  203. * Initialize the buffer tool
  204. *
  205. * @method init
  206. * @chainable
  207. * @constructor
  208. *
  209. */
  210. init: function (selector, d) {
  211. that = this;
  212. this.initToggle(selector, d,
  213. {
  214. activate: activate,
  215. deactivate: deactivate,
  216. defaultAction: clearMap
  217. }
  218. );
  219.  
  220. ui.init();
  221.  
  222. return this;
  223. },
  224.  
  225. name: "bufferTool"
  226. });
  227. });