Reusable Accessible Mapping Platform

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