Reusable Accessible Mapping Platform

API Docs for: 3.0.0
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. * @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: GlobalStorage.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. //identify proxy page to use if the toJson payload to the geoprocessing service is greater than 2000 characters.
  133. //If this null or not available the geoprocessor.execute operation will not work. Otherwise it will do a http post to the proxy.
  134. esriConfig.defaults.io.proxyUrl = GlobalStorage.config.proxyUrl;
  135. esriConfig.defaults.io.alwaysUseProxy = false;
  136. }
  137. };
  138.  
  139. /**
  140. * Activates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  141. *
  142. * @method activate
  143. * @private
  144. */
  145. function activate() {
  146. populationApp.toolbar.activate(Draw.FREEHAND_POLYGON);
  147.  
  148. displayOutput(i18n.t(that.ns + ":na"));
  149. }
  150.  
  151. /**
  152. * Deactivates the Tool. This method is passed to the `initToggle` method and is triggered by the BaseTool logic.
  153. *
  154. * @method deactivate
  155. * @private
  156. */
  157. function deactivate() {
  158. populationApp.toolbar.deactivate();
  159. clearMap();
  160. }
  161.  
  162. /**
  163. * Clears the map. This method is passed to the `initToggle` method as the `defaultAction`
  164. * to be triggered by the BaseTool logic when the `float-default-button` is clicked.
  165. *
  166. * @method clearMap
  167. * @private
  168. */
  169. function clearMap() {
  170. populationApp.map.graphics.clear();
  171.  
  172. displayOutput(i18n.t(that.ns + ":na"));
  173. }
  174.  
  175. /**
  176. * Displays the tool's output by calling BaseTool's `displayOutput` function.
  177. *
  178. * @method displayOutput
  179. * @private
  180. */
  181. function displayOutput(value) {
  182. that.displayTemplateOutput(
  183. {
  184. totalPopulationLabel: i18n.t(that.ns + ":population"),
  185. populationOutput: value
  186. }
  187. );
  188. }
  189.  
  190. return dojoLang.mixin({}, BaseTool, {
  191. /**
  192. * Initialize the population tool
  193. *
  194. * @method init
  195. * @chainable
  196. * @constructor
  197. *
  198. */
  199. init: function (selector, d) {
  200. that = this;
  201. this.initToggle(selector, d,
  202. {
  203. activate: activate,
  204. deactivate: deactivate,
  205. defaultAction: clearMap
  206. }
  207. );
  208.  
  209. ui.init();
  210.  
  211. return this;
  212. },
  213.  
  214. name: "populationTool"
  215. });
  216. });