WPSClient.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/SingleFile.js
  7. */
  8. /**
  9. * @requires OpenLayers/Events.js
  10. * @requires OpenLayers/WPSProcess.js
  11. * @requires OpenLayers/Format/WPSDescribeProcess.js
  12. * @requires OpenLayers/Request.js
  13. */
  14. /**
  15. * Class: OpenLayers.WPSClient
  16. * High level API for interaction with Web Processing Services (WPS).
  17. * An <OpenLayers.WPSClient> instance is used to create <OpenLayers.WPSProcess>
  18. * instances for servers known to the WPSClient. The WPSClient also caches
  19. * DescribeProcess responses to reduce the number of requests sent to servers
  20. * when processes are created.
  21. */
  22. OpenLayers.WPSClient = OpenLayers.Class({
  23. /**
  24. * Property: servers
  25. * {Object} Service metadata, keyed by a local identifier.
  26. *
  27. * Properties:
  28. * url - {String} the url of the server
  29. * version - {String} WPS version of the server
  30. * processDescription - {Object} Cache of raw DescribeProcess
  31. * responses, keyed by process identifier.
  32. */
  33. servers: null,
  34. /**
  35. * Property: version
  36. * {String} The default WPS version to use if none is configured. Default
  37. * is '1.0.0'.
  38. */
  39. version: '1.0.0',
  40. /**
  41. * Property: lazy
  42. * {Boolean} Should the DescribeProcess be deferred until a process is
  43. * fully configured? Default is false.
  44. */
  45. lazy: false,
  46. /**
  47. * Property: events
  48. * {<OpenLayers.Events>}
  49. *
  50. * Supported event types:
  51. * describeprocess - Fires when the process description is available.
  52. * Listeners receive an object with a 'raw' property holding the raw
  53. * DescribeProcess response, and an 'identifier' property holding the
  54. * process identifier of the described process.
  55. */
  56. events: null,
  57. /**
  58. * Constructor: OpenLayers.WPSClient
  59. *
  60. * Parameters:
  61. * options - {Object} Object whose properties will be set on the instance.
  62. *
  63. * Avaliable options:
  64. * servers - {Object} Mandatory. Service metadata, keyed by a local
  65. * identifier. Can either be a string with the service url or an
  66. * object literal with additional metadata:
  67. *
  68. * (code)
  69. * servers: {
  70. * local: '/geoserver/wps'
  71. * }, {
  72. * opengeo: {
  73. * url: 'http://demo.opengeo.org/geoserver/wps',
  74. * version: '1.0.0'
  75. * }
  76. * }
  77. * (end)
  78. *
  79. * lazy - {Boolean} Optional. Set to true if DescribeProcess should not be
  80. * requested until a process is fully configured. Default is false.
  81. */
  82. initialize: function(options) {
  83. OpenLayers.Util.extend(this, options);
  84. this.events = new OpenLayers.Events(this);
  85. this.servers = {};
  86. for (var s in options.servers) {
  87. this.servers[s] = typeof options.servers[s] == 'string' ? {
  88. url: options.servers[s],
  89. version: this.version,
  90. processDescription: {}
  91. } : options.servers[s];
  92. }
  93. },
  94. /**
  95. * APIMethod: execute
  96. * Shortcut to execute a process with a single function call. This is
  97. * equivalent to using <getProcess> and then calling execute on the
  98. * process.
  99. *
  100. * Parameters:
  101. * options - {Object} Options for the execute operation.
  102. *
  103. * Available options:
  104. * server - {String} Mandatory. One of the local identifiers of the
  105. * configured servers.
  106. * process - {String} Mandatory. A process identifier known to the
  107. * server.
  108. * inputs - {Object} The inputs for the process, keyed by input identifier.
  109. * For spatial data inputs, the value of an input is usually an
  110. * <OpenLayers.Geometry>, an <OpenLayers.Feature.Vector> or an array of
  111. * geometries or features.
  112. * output - {String} The identifier of an output to parse. Optional. If not
  113. * provided, the first output will be parsed.
  114. * success - {Function} Callback to call when the process is complete.
  115. * This function is called with an outputs object as argument, which
  116. * will have a property with the identifier of the requested output
  117. * (e.g. 'result'). For processes that generate spatial output, the
  118. * value will either be a single <OpenLayers.Feature.Vector> or an
  119. * array of features.
  120. * scope - {Object} Optional scope for the success callback.
  121. */
  122. execute: function(options) {
  123. var process = this.getProcess(options.server, options.process);
  124. process.execute({
  125. inputs: options.inputs,
  126. success: options.success,
  127. scope: options.scope
  128. });
  129. },
  130. /**
  131. * APIMethod: getProcess
  132. * Creates an <OpenLayers.WPSProcess>.
  133. *
  134. * Parameters:
  135. * serverID - {String} Local identifier from the servers that this instance
  136. * was constructed with.
  137. * processID - {String} Process identifier known to the server.
  138. *
  139. * Returns:
  140. * {<OpenLayers.WPSProcess>}
  141. */
  142. getProcess: function(serverID, processID) {
  143. var process = new OpenLayers.WPSProcess({
  144. client: this,
  145. server: serverID,
  146. identifier: processID
  147. });
  148. if (!this.lazy) {
  149. process.describe();
  150. }
  151. return process;
  152. },
  153. /**
  154. * Method: describeProcess
  155. *
  156. * Parameters:
  157. * serverID - {String} Identifier of the server
  158. * processID - {String} Identifier of the requested process
  159. * callback - {Function} Callback to call when the description is available
  160. * scope - {Object} Optional execution scope for the callback function
  161. */
  162. describeProcess: function(serverID, processID, callback, scope) {
  163. var server = this.servers[serverID];
  164. if (!server.processDescription[processID]) {
  165. if (!(processID in server.processDescription)) {
  166. // set to null so we know a describeFeature request is pending
  167. server.processDescription[processID] = null;
  168. OpenLayers.Request.GET({
  169. url: server.url,
  170. params: {
  171. SERVICE: 'WPS',
  172. VERSION: server.version,
  173. REQUEST: 'DescribeProcess',
  174. IDENTIFIER: processID
  175. },
  176. success: function(response) {
  177. server.processDescription[processID] = response.responseText;
  178. this.events.triggerEvent('describeprocess', {
  179. identifier: processID,
  180. raw: response.responseText
  181. });
  182. },
  183. scope: this
  184. });
  185. } else {
  186. // pending request
  187. this.events.register('describeprocess', this, function describe(evt) {
  188. if (evt.identifier === processID) {
  189. this.events.unregister('describeprocess', this, describe);
  190. callback.call(scope, evt.raw);
  191. }
  192. });
  193. }
  194. } else {
  195. window.setTimeout(function() {
  196. callback.call(scope, server.processDescription[processID]);
  197. }, 0);
  198. }
  199. },
  200. /**
  201. * Method: destroy
  202. */
  203. destroy: function() {
  204. this.events.destroy();
  205. this.events = null;
  206. this.servers = null;
  207. },
  208. CLASS_NAME: 'OpenLayers.WPSClient'
  209. });