Reusable Accessible Mapping Platform

API Docs for: 5.3.1
Show:

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

  1. /*global define, i18n */
  2.  
  3. /**
  4. * @module Tools
  5. */
  6.  
  7. /**
  8. * PopulationTool.
  9. *
  10. * Computes the total population of a selected area. When the user draws a polygon, the population will
  11. * be displayed in the bottom right corner.
  12. *
  13. * ####Imports RAMP Modules:
  14. * {{#crossLink "Map"}}{{/crossLink}}
  15. * {{#crossLink "GlobalStorage"}}{{/crossLink}}
  16. * {{#crossLink "BaseTool"}}{{/crossLink}}
  17. *
  18. * @class PopulationTool
  19. * @uses dojo/dom
  20. * @uses dojo/string
  21. * @uses dojo/_base/lang
  22. * @uses esri/config
  23. * @uses esri/graphic
  24. * @uses esri/tasks/Geoprocessor
  25. * @uses esri/tasks/FeatureSet
  26. * @uses esri/toolbars/draw
  27. * @uses esri/symbols/SimpleLineSymbol
  28. * @uses esri/symbols/SimpleFillSymbol
  29. * @extends BaseTool
  30. */
  31.  
  32. define([
  33. // Dojo
  34. "dojo/dom", "dojo/string", "dojo/_base/lang",
  35. // Esri
  36. "esri/config", "esri/graphic", "esri/tasks/Geoprocessor", "esri/tasks/FeatureSet",
  37. "esri/toolbars/draw", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",
  38. // Ramp
  39. "ramp/map", "ramp/globalStorage", "tools/baseTool"
  40. ],
  41. function (
  42. // Dojo
  43. dom, string, dojoLang,
  44. // Esri
  45. esriConfig, Graphic, Geoprocessor, FeatureSet, Draw, SimpleLineSymbol, SimpleFillSymbol,
  46. // Ramp
  47. RampMap, GlobalStorage, BaseTool) {
  48. "use strict";
  49.  
  50. var ui,
  51. geoprocessor,
  52. populationApp,
  53.  
  54. that;
  55.  
  56. /**
  57. * Compute an estimated amount of people in a specified polygon.
  58. *
  59. * @method computeZonalStats
  60. * @private
  61. * @param {Object} evtObj an object representing the event.
  62. *
  63. */
  64. function computeZonalStats(evtObj) {
  65. var geometry = evtObj.geometry,
  66. graphic,
  67. features,
  68. featureSet,
  69. params;
  70.  
  71. that.working(true);
  72.  
  73. /*After user draws shape on map using the draw toolbar compute the zonal*/
  74. populationApp.map.graphics.clear();
  75. graphic = populationApp.map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
  76. populationApp.map.graphics.add(graphic);
  77.  
  78. features = [];
  79. features.push(graphic);
  80.  
  81. featureSet = new FeatureSet();
  82. featureSet.features = features;
  83.  
  84. params = {
  85. inputPoly: featureSet
  86. };
  87. geoprocessor.execute(params);
  88. }
  89.  
  90. /**
  91. * Display the calculated population on the map.
  92. *
  93. * @method outputTotalPopulation
  94. * @private
  95. * @param {Object} evtObj an object representing the event.
  96. *
  97. */
  98. function outputTotalPopulation(evtObj) {
  99. var results = evtObj.results,
  100. totalPopulation = Math.floor(results[0].value.features[0].attributes.SUM);
  101.  
  102. that.working(false);
  103.  
  104. totalPopulation = string.substitute("${number:dojo.number.format}", { number: totalPopulation });
  105. displayOutput(totalPopulation);
  106. }
  107.  
  108. ui = {
  109. /**
  110. * Initiates additional UI components of the Tool.
  111. *
  112. * @method ui.init
  113. * @private
  114. */
  115. init: function () {
  116. var map = RampMap.getMap(),
  117. toolbar = new Draw(map);
  118.  
  119. //TODO store this URL in config
  120. geoprocessor = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/GPServer/PopulationSummary");
  121.  
  122. geoprocessor.setOutputSpatialReference(map.spatialReference);
  123. geoprocessor.on("execute-complete", outputTotalPopulation);
  124.  
  125. toolbar.on("draw-end", computeZonalStats);
  126.  
  127. populationApp = {
  128. map: map,
  129. toolbar: toolbar
  130. };
  131. }
  132. };
  133.  
  134. /**
  135. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  136. *
  137. * @method activate
  138. * @private
  139. */
  140. function activate() {
  141. populationApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  142.  
  143. displayOutput(i18n.t(that.ns + ":na"));
  144. }
  145.  
  146. /**
  147. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  148. *
  149. * @method deactivate
  150. * @private
  151. */
  152. function deactivate() {
  153. populationApp.toolbar.deactivate();
  154. clearMap();
  155. }
  156.  
  157. /**
  158. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  159. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  160. *
  161. * @method clearMap
  162. * @private
  163. */
  164. function clearMap() {
  165. populationApp.map.graphics.clear();
  166.  
  167. displayOutput(i18n.t(that.ns + ":na"));
  168. }
  169.  
  170. /**
  171. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  172. *
  173. * @method displayOutput
  174. * @private
  175. */
  176. function displayOutput(value) {
  177. that.displayTemplateOutput(
  178. {
  179. totalPopulationLabel: i18n.t(that.ns + ":population"),
  180. populationOutput: value
  181. }
  182. );
  183. }
  184.  
  185. return dojoLang.mixin({}, BaseTool, {
  186. /**
  187. * Initialize the population tool
  188. *
  189. * @method init
  190. * @chainable
  191. * @constructor
  192. *
  193. */
  194. init: function (selector, d) {
  195. that = this;
  196. this.initToggle(selector, d,
  197. {
  198. activate: activate,
  199. deactivate: deactivate,
  200. defaultAction: clearMap
  201. }
  202. );
  203.  
  204. ui.init();
  205.  
  206. return this;
  207. },
  208.  
  209. name: "populationTool"
  210. });
  211. });