Reusable Accessible Mapping Platform

API Docs for: 4.0.0
Show:

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

  1. /*global define, i18n, RAMP */
  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. * @class PopulationTool
  14. * @static
  15. * @uses dojo/dom
  16. * @uses dojo/string
  17. * @uses dojo/_base/lang
  18. * @uses esri/config
  19. * @uses esri/graphic
  20. * @uses esri/tasks/Geoprocessor
  21. * @uses esri/tasks/FeatureSet
  22. * @uses esri/toolbars/draw
  23. * @uses esri/symbols/SimpleLineSymbol
  24. * @uses esri/symbols/SimpleFillSymbol
  25. * @uses Map
  26. * @uses GlobalStorage
  27. * @extends BaseTool
  28. */
  29.  
  30. define([
  31. // Dojo
  32. "dojo/dom", "dojo/string", "dojo/_base/lang",
  33. // Esri
  34. "esri/config", "esri/graphic", "esri/tasks/Geoprocessor", "esri/tasks/FeatureSet",
  35. "esri/toolbars/draw", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",
  36. // Ramp
  37. "ramp/map", "ramp/globalStorage", "tools/baseTool"
  38. ],
  39. function (
  40. // Dojo
  41. dom, string, dojoLang,
  42. // Esri
  43. esriConfig, Graphic, Geoprocessor, FeatureSet, Draw, SimpleLineSymbol, SimpleFillSymbol,
  44. // Ramp
  45. RampMap, GlobalStorage, BaseTool) {
  46. "use strict";
  47.  
  48. var ui,
  49. geoprocessor,
  50. populationApp,
  51.  
  52. that;
  53.  
  54. /**
  55. * Compute an estimated amount of people in a specified polygon.
  56. *
  57. * @method computeZonalStats
  58. * @private
  59. * @param {Object} evtObj an object representing the event.
  60. *
  61. */
  62. function computeZonalStats(evtObj) {
  63. var geometry = evtObj.geometry,
  64. graphic,
  65. features,
  66. featureSet,
  67. params;
  68.  
  69. that.working(true);
  70.  
  71. /*After user draws shape on map using the draw toolbar compute the zonal*/
  72. populationApp.map.graphics.clear();
  73. graphic = populationApp.map.graphics.add(new Graphic(geometry, new SimpleFillSymbol()));
  74. populationApp.map.graphics.add(graphic);
  75.  
  76. features = [];
  77. features.push(graphic);
  78.  
  79. featureSet = new FeatureSet();
  80. featureSet.features = features;
  81.  
  82. params = {
  83. inputPoly: featureSet
  84. };
  85. geoprocessor.execute(params);
  86. }
  87.  
  88. /**
  89. * Display the calculated population on the map.
  90. *
  91. * @method outputTotalPopulation
  92. * @private
  93. * @param {Object} evtObj an object representing the event.
  94. *
  95. */
  96. function outputTotalPopulation(evtObj) {
  97. var results = evtObj.results,
  98. totalPopulation = Math.floor(results[0].value.features[0].attributes.SUM);
  99.  
  100. that.working(false);
  101.  
  102. totalPopulation = string.substitute("${number:dojo.number.format}", { number: totalPopulation });
  103. displayOutput(totalPopulation);
  104. }
  105.  
  106. ui = {
  107. /**
  108. * Initiates additional UI components of the Tool.
  109. *
  110. * @method ui.init
  111. * @private
  112. */
  113. init: function () {
  114. var map = RampMap.getMap(),
  115. toolbar = new Draw(map);
  116.  
  117. //TODO store this URL in config
  118. geoprocessor = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/GPServer/PopulationSummary");
  119.  
  120. geoprocessor.setOutputSpatialReference({
  121. wkid: RAMP.config.spatialReference.wkid
  122. });
  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. });