Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

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

  1. /*global define, i18n */
  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(GlobalStorage.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: GlobalStorage.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. // identify proxy page to use if the toJson payload to the geometry service is greater than
  154. // 2000 characters. If this null or not available the project and lengths operation will not
  155. // work. Otherwise it will do a http post to the proxy.
  156. esriConfig.defaults.io.proxyUrl = GlobalStorage.config.proxyUrl;
  157. esriConfig.defaults.io.alwaysUseProxy = false;
  158. }
  159. };
  160.  
  161. /**
  162. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  163. *
  164. * @method activate
  165. * @private
  166. */
  167. function activate() {
  168. bufferApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  169.  
  170. displayOutput();
  171. }
  172.  
  173. /**
  174. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  175. *
  176. * @method deactivate
  177. * @private
  178. */
  179. function deactivate() {
  180. bufferApp.toolbar.deactivate();
  181. clearMap();
  182. }
  183.  
  184. /**
  185. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  186. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  187. *
  188. * @method clearMap
  189. * @private
  190. */
  191. function clearMap() {
  192. bufferApp.map.graphics.clear();
  193. }
  194.  
  195. /**
  196. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  197. *
  198. * @method displayOutput
  199. * @private
  200. */
  201. function displayOutput() {
  202. that.displayTemplateOutput(
  203. {
  204. distanceLabel: i18n.t(that.ns + ":distance")
  205. }
  206. );
  207. }
  208.  
  209. return dojoLang.mixin({}, BaseTool, {
  210. /**
  211. * Initialize the buffer tool
  212. *
  213. * @method init
  214. * @chainable
  215. * @constructor
  216. *
  217. */
  218. init: function (selector, d) {
  219. that = this;
  220. this.initToggle(selector, d,
  221. {
  222. activate: activate,
  223. deactivate: deactivate,
  224. defaultAction: clearMap
  225. }
  226. );
  227.  
  228. ui.init();
  229.  
  230. return this;
  231. },
  232.  
  233. name: "bufferTool"
  234. });
  235. });