From 0108250ed94957558bde6cf3d0c631cc20491089 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 27 Jul 2012 14:18:37 +0200 Subject: [PATCH 01/14] WPSClient and WPSProcess for convenient WPS interaction. --- examples/wps-client.html | 29 ++++ examples/wps-client.js | 64 +++++++++ lib/OpenLayers.js | 4 +- lib/OpenLayers/WPSClient.js | 126 +++++++++++++++++ lib/OpenLayers/WPSProcess.js | 257 +++++++++++++++++++++++++++++++++++ 5 files changed, 479 insertions(+), 1 deletion(-) create mode 100644 examples/wps-client.html create mode 100644 examples/wps-client.js create mode 100644 lib/OpenLayers/WPSClient.js create mode 100644 lib/OpenLayers/WPSProcess.js diff --git a/examples/wps-client.html b/examples/wps-client.html new file mode 100644 index 0000000000..b588f7fba2 --- /dev/null +++ b/examples/wps-client.html @@ -0,0 +1,29 @@ + + + + + + + OpenLayers WPS Client Example + + + + + + +

WPS Client Example

+ +
+ wps +
+ +
Shows the usage of the WPS Client
+ +
+ +
+

This example shows how simple it is to use the WPS Client. See + wps-client.js to see how this is done.

+
+ + diff --git a/examples/wps-client.js b/examples/wps-client.js new file mode 100644 index 0000000000..fad7a5868c --- /dev/null +++ b/examples/wps-client.js @@ -0,0 +1,64 @@ +var map, client, process; + +function init() { + + map = new OpenLayers.Map('map', { + allOverlays: true, + center: [114, 16], + zoom: 4, + layers: [new OpenLayers.Layer.Vector()] + }); + + var features = [new OpenLayers.Format.WKT().read( + 'LINESTRING(117 22,112 18,118 13, 115 8)' + )]; + var geometry = (new OpenLayers.Format.WKT().read( + 'POLYGON((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))' + )).geometry; + + map.baseLayer.addFeatures(features); + map.baseLayer.addFeatures([new OpenLayers.Feature.Vector(geometry)]); + + client = new OpenLayers.WPSClient({ + servers: { + local: "/geoserver/wps" + } + }); + + // Create a process and execute it + process = client.getProcess("local", "JTS:intersection"); + process.execute({ + // spatial input can be a feature or a geometry or an array of + // features or geometries + inputs: { + a: features, + b: geometry + }, + success: function(outputs) { + // outputs.result is a feature or an array of features for spatial + // processes. + map.baseLayer.addFeatures(outputs.result); + } + }); + + // Instead of creating a process and executing it, we could call execute on + // the client directly if we are only dealing with a single process: + /* + client.execute({ + server: "local", + process: "JTS:intersection", + // spatial input can be a feature or a geometry or an array of + // features or geometries + inputs: { + a: features, + b: geometry + }, + success: function(outputs) { + // outputs.result is a feature or an array of features for spatial + // processes. + map.baseLayer.addFeatures(outputs.result); + } + }); + */ + +} \ No newline at end of file diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index c8c80d847f..cca76a78d8 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -394,7 +394,9 @@ "OpenLayers/Symbolizer/Raster.js", "OpenLayers/Lang.js", "OpenLayers/Lang/en.js", - "OpenLayers/Spherical.js" + "OpenLayers/Spherical.js", + "OpenLayers/WPSClient.js", + "OpenLayers/WPSProcess.js" ]; // etc. } diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js new file mode 100644 index 0000000000..11b42c3ab7 --- /dev/null +++ b/lib/OpenLayers/WPSClient.js @@ -0,0 +1,126 @@ +/** + * Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. + * + * @requires OpenLayers/SingleFile.js + */ + +/** + * Class: OpenLayers.WPSClient + */ +OpenLayers.WPSClient = OpenLayers.Class({ + + /** + * Property: servers + * {Object} Service metadata, keyed by a local identifier. + * + * Properties: + * url - {String} the url of the server + * knownProcesses: {Object} Cache of DescribeProcess responses, keyed by + * process identifier. + */ + servers: null, + + /** + * Property: lazy + * {Boolean} Should the DescribeProcess be deferred until a process is + * fully configured? Default is false. + */ + lazy: false, + + /** + * Constructor: OpenLayers.WPSClient + * + * Parameters: + * options - {Object} Object whose properties will be set on the instance. + * + * Avaliable options: + * servers - {Object} Mandatory. Service metadata, keyed by a local + * identifier. Can either be a string with the service url or an + * object literal with additional metadata: + * + * (code) + * servers: { + * local: '/geoserver/wps' + * }, { + * opengeo: { + * url: 'http://demo.opengeo.org/geoserver/wps', + * version: '1.0.0' + * } + * } + * (end) + * + * lazy - {Boolean} Optional. Set to true if DescribeProcess should not be + * requested until a process is fully configured. Default is false. + */ + initialize: function(options) { + OpenLayers.Util.extend(this, options); + this.servers = {}; + for (var s in options.servers) { + this.servers[s] = typeof options.servers[s] == 'string' ? { + url: options.servers[s], + version: '1.0.0' + } : options.servers[s]; + } + }, + + /** + * APIMethod: execute + * + * Parameters: + * options - {Object} Options for the execute operation. + * + * Available options: + * server - {String} Mandatory. One of the local identifiers of the + * configured servers. + * process - {String} Mandatory. A process identifier known to the + * server. + * inputs - {Object} The inputs for the process, keyed by input identifier. + * For spatial data inputs, the value of an input is usually an + * , an or an array of + * geometries or features. + * success - {Function} Callback to call when the process is complete. + * This function is called with an outputs object as argument, which + * will have a 'result' property. For processes that generate spatial + * output, this will either be a single or + * an array of features. + * scope - {Object} Optional scope for the success callback. + */ + execute: function(options) { + var process = this.getProcess(options.server, options.process); + process.execute({ + inputs: options.inputs, + success: options.success, + scope: options.scope + }); + }, + + /** + * APIMethod: getProcess + * Creates an . + * + * Parameters: + * server - {String} Local identifier from the servers that this instance + * was constructed with. + * identifier - {String} Process identifier known to the server. + * + * Returns: + * {} + */ + getProcess: function(server, identifier) { + var process = new OpenLayers.WPSProcess({ + client: this, + server: server, + identifier: identifier + }); + if (!this.lazy) { + process.describe(); + } + return process; + }, + + CLASS_NAME: 'OpenLayers.WPSClient' + +}); diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js new file mode 100644 index 0000000000..2d8eb82af3 --- /dev/null +++ b/lib/OpenLayers/WPSProcess.js @@ -0,0 +1,257 @@ +/** + * Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. + * + * @requires OpenLayers/SingleFile.js + */ + +/** + * @requires OpenLayers/Events.js + * @requires OpenLayers/Geometry.js + * @requires OpenLayers/Feature/Vector.js + */ + +/** + * Class: OpenLayers.WPSProces + */ +OpenLayers.WPSProcess = OpenLayers.Class({ + + /** + * APIProperty: events + * {} + * + * Supported event types: + * describeprocess - fires when the process description is available + */ + events: null, + + /** + * Property: client + * {} The client that manages this process. + */ + client: null, + + /** + * Property: server + * {String} Local client identifier for this process's server. + */ + server: null, + + /** + * Property: identifier + * {String} Process identifier known to the server. + */ + identifier: null, + + /** + * Property: description + * {Object} DescribeProcess response for this process. + */ + description: null, + + /** + * Property: formats + * {Object} OpenLayers.Format instances keyed by mimetype. + */ + formats: null, + + /** + * Constructor: OpenLayers.WPSProcess + * + * Parameters: + * options - {Object} Object whose properties will be set on the instance. + * + * Avaliable options: + * client - {. + */ + describe: function() { + if (this._describePending || this.description) { + return; + } + this._describePending = true; + var server = this.client.servers[this.server]; + OpenLayers.Request.GET({ + url: server.url, + params: { + SERVICE: 'WPS', + VERSION: server.version, + REQUEST: 'DescribeProcess', + IDENTIFIER: this.identifier + }, + success: function(response) { + this.description = new OpenLayers.Format.WPSDescribeProcess() + .read(response.responseText) + .processDescriptions[this.identifier]; + delete this._describePending; + this.events.triggerEvent('describeprocess'); + }, + scope: this + }); + }, + + /** + * APIMethod: execute + * Executes the process + * + * Parameters: + * options - {Object} + * + * Available options: + * inputs - {Object} The inputs for the process, keyed by input identifier. + * For spatial data inputs, the value of an input is usually an + * , an or an array of + * geometries or features. + * success - {Function} Callback to call when the process is complete. + * This function is called with an outputs object as argument, which + * will have a 'result' property. For processes that generate spatial + * output, this will either be a single or + * an array of features. + * scope - {Object} Optional scope for the success callback. + */ + execute: function(options) { + if (!this.description) { + this.events.register('describeprocess', this, function execute() { + this.events.unregister('describeprocess', this, execute); + this.execute(options); + }); + this.describe(); + return; + } + var description = this.description, + inputs = options.inputs, + input, i, ii; + for (i=0, ii=description.dataInputs.length; i, an or an array of + * geometries or features. + */ + setInputData: function(input, data) { + // clear any previous data + input.data = {}; + if (data) { + var complexData = input.complexData; + if (complexData) { + var format = this.findMimeType(complexData); + input.data.complexData = { + mimeType: format, + value: this.formats[format].write(this.toFeatures(data)) + }; + } + } + }, + + /** + * Method: setResponseForm + * Sets the responseForm property of the payload. + */ + setResponseForm: function() { + output = this.description.processOutputs[0]; + this.description.responseForm = { + rawDataOutput: { + identifier: output.identifier, + mimeType: this.findMimeType(output.complexOutput) + } + }; + }, + + /** + * Method: toFeatures + * Converts spatial input into features so it can be processed by + * instances. + * + * Parameters: + * source - {Mixed} An , an + * , or an array of geometries or features + * + * Returns: + * {Array()} + */ + toFeatures: function(source) { + var isArray = OpenLayers.Util.isArray(source); + if (!isArray) { + source = [source]; + } + var target = new Array(source.length), + current; + for (var i=0, ii=source.length; i Date: Mon, 6 Aug 2012 23:13:14 +0200 Subject: [PATCH 02/14] Using proxy and demo server. --- examples/wps-client.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/wps-client.js b/examples/wps-client.js index fad7a5868c..84971e2013 100644 --- a/examples/wps-client.js +++ b/examples/wps-client.js @@ -1,3 +1,5 @@ +OpenLayers.ProxyHost = 'proxy.cgi?url='; + var map, client, process; function init() { @@ -21,7 +23,7 @@ function init() { client = new OpenLayers.WPSClient({ servers: { - local: "/geoserver/wps" + local: "http://demo.opengeo.org/geoserver/wps" } }); From b61120d3b54295e618937e0bcb91d50d364c2b99 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 6 Aug 2012 23:17:04 +0200 Subject: [PATCH 03/14] Cache of DescribeProcess responses on the client. --- lib/OpenLayers/WPSClient.js | 8 ++++--- lib/OpenLayers/WPSProcess.js | 41 ++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js index 11b42c3ab7..5078abd8a1 100644 --- a/lib/OpenLayers/WPSClient.js +++ b/lib/OpenLayers/WPSClient.js @@ -18,8 +18,9 @@ OpenLayers.WPSClient = OpenLayers.Class({ * * Properties: * url - {String} the url of the server - * knownProcesses: {Object} Cache of DescribeProcess responses, keyed by - * process identifier. + * version - {String} WPS version of the server + * describeProcessResponse - {Object} Cache of DescribeProcess responses, + * keyed by process identifier. */ servers: null, @@ -61,7 +62,8 @@ OpenLayers.WPSClient = OpenLayers.Class({ for (var s in options.servers) { this.servers[s] = typeof options.servers[s] == 'string' ? { url: options.servers[s], - version: '1.0.0' + version: '1.0.0', + describeProcessResponse: {} } : options.servers[s]; } }, diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js index 2d8eb82af3..400a2eb455 100644 --- a/lib/OpenLayers/WPSProcess.js +++ b/lib/OpenLayers/WPSProcess.js @@ -84,15 +84,23 @@ OpenLayers.WPSProcess = OpenLayers.Class({ /** * Method: describe * Issues a DescribeProcess request asynchronously and fires the - * 'describeProcess' event as soon as the response is available in + * 'describeprocess' event as soon as the response is available in * . */ describe: function() { - if (this._describePending || this.description) { + var server = this.client.servers[this.server]; + if (this.description !== null) { + return; + } else if (server.describeProcessResponse[this.identifier] === null) { + // pending request + return; + } else if (this.identifier in server.describeProcessResponse) { + // process description already cached on client + this.parseDescription(); return; } - this._describePending = true; - var server = this.client.servers[this.server]; + // set to null so we know a describeFeature request is pending + server.describeProcessResponse[this.identifier] = null; OpenLayers.Request.GET({ url: server.url, params: { @@ -101,13 +109,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({ REQUEST: 'DescribeProcess', IDENTIFIER: this.identifier }, - success: function(response) { - this.description = new OpenLayers.Format.WPSDescribeProcess() - .read(response.responseText) - .processDescriptions[this.identifier]; - delete this._describePending; - this.events.triggerEvent('describeprocess'); - }, + success: this.parseDescription, scope: this }); }, @@ -165,6 +167,23 @@ OpenLayers.WPSProcess = OpenLayers.Class({ }); }, + /** + * Method: parseDescription + * + * Parameters: + * response - {Object} + */ + parseDescription: function(response) { + var server = this.client.servers[this.server]; + if (response) { + server.describeProcessResponse[this.identifier] = response.responseText; + } + this.description = new OpenLayers.Format.WPSDescribeProcess() + .read(server.describeProcessResponse[this.identifier]) + .processDescriptions[this.identifier]; + this.events.triggerEvent('describeprocess'); + }, + /** * Method: setInputData * Sets the data for a single input From c64621f510da94f5eee01d403ae81e8069d4667f Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 6 Aug 2012 23:17:15 +0200 Subject: [PATCH 04/14] Adding unit tests. --- tests/WPSClient.html | 65 +++++++++++++++++++++++++ tests/WPSProcess.html | 107 ++++++++++++++++++++++++++++++++++++++++++ tests/list-tests.html | 2 + 3 files changed, 174 insertions(+) create mode 100644 tests/WPSClient.html create mode 100644 tests/WPSProcess.html diff --git a/tests/WPSClient.html b/tests/WPSClient.html new file mode 100644 index 0000000000..bd1eb477bc --- /dev/null +++ b/tests/WPSClient.html @@ -0,0 +1,65 @@ + + + + + + + + diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html new file mode 100644 index 0000000000..460ddc2451 --- /dev/null +++ b/tests/WPSProcess.html @@ -0,0 +1,107 @@ + + + + + + + + diff --git a/tests/list-tests.html b/tests/list-tests.html index 44b0db2b90..8c37464b84 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -232,6 +232,8 @@
  • Kinetic.html
  • Util.html
  • Util/vendorPrefix.html
  • +
  • WPSClient.html
  • +
  • WPSProcess.html
  • deprecated/Ajax.html
  • deprecated/Util.html
  • deprecated/BaseTypes/Class.html
  • From ec9ab8129ee924dddcc2347b6e95cf599ad457e7 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Thu, 9 Aug 2012 18:28:53 +0200 Subject: [PATCH 05/14] Adding support for chaining processes. --- examples/wps-client.js | 19 ++- lib/OpenLayers/WPSProcess.js | 307 ++++++++++++++++++++++++++++++----- 2 files changed, 280 insertions(+), 46 deletions(-) diff --git a/examples/wps-client.js b/examples/wps-client.js index 84971e2013..dee7e5c50d 100644 --- a/examples/wps-client.js +++ b/examples/wps-client.js @@ -1,6 +1,6 @@ OpenLayers.ProxyHost = 'proxy.cgi?url='; -var map, client, process; +var map, client, intersect, buffer; function init() { @@ -23,18 +23,27 @@ function init() { client = new OpenLayers.WPSClient({ servers: { - local: "http://demo.opengeo.org/geoserver/wps" + opengeo: 'http://demo.opengeo.org/geoserver/wps' } }); - // Create a process and execute it - process = client.getProcess("local", "JTS:intersection"); - process.execute({ + // Create a process and configure it + intersect = client.getProcess('opengeo', 'JTS:intersection'); + intersect.configure({ // spatial input can be a feature or a geometry or an array of // features or geometries inputs: { a: features, b: geometry + } + }); + + // Create another process which chains the previous one and execute it + buffer = client.getProcess('opengeo', 'JTS:buffer'); + buffer.execute({ + inputs: { + geom: intersect.output(), + distance: 1 }, success: function(outputs) { // outputs.result is a feature or an array of features for spatial diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js index 400a2eb455..7344df5768 100644 --- a/lib/OpenLayers/WPSProcess.js +++ b/lib/OpenLayers/WPSProcess.js @@ -14,7 +14,7 @@ */ /** - * Class: OpenLayers.WPSProces + * Class: OpenLayers.WPSProcess */ OpenLayers.WPSProcess = OpenLayers.Class({ @@ -23,7 +23,8 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * {} * * Supported event types: - * describeprocess - fires when the process description is available + * describeprocess - Fires when the process description is available for + * the first time. */ events: null, @@ -57,6 +58,20 @@ OpenLayers.WPSProcess = OpenLayers.Class({ */ formats: null, + /** + * Property: chained + * {Integer} Number of chained processes for pending execute reqeusts that + * don't have a full configuration yet. + */ + chained: 0, + + /** + * Property: executeCallbacks + * {Array} Callbacks waiting to be executed until all chained processes + * are configured; + */ + executeCallbacks: null, + /** * Constructor: OpenLayers.WPSProcess * @@ -74,6 +89,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({ OpenLayers.Util.extend(this, options); this.events = new OpenLayers.Events(this); + this.executeCallbacks = []; this.formats = { 'application/wkt': new OpenLayers.Format.WKT(), @@ -86,17 +102,38 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * Issues a DescribeProcess request asynchronously and fires the * 'describeprocess' event as soon as the response is available in * . + * + * Parameters: + * options - {Object} Coniguration for the method call + * + * Available options: + * callback - {Function} Callback to execute when the description is + * available. Will be called with the parsed description as argument. + * Optional. + * scope - {Object} The scope in which the callback will be executed. + * Default is the global object. */ - describe: function() { + describe: function(options) { + options = options || {}; + function callback() { + if (options.callback) { + window.setTimeout(function() { + options.callback.call(options.scope, this.description); + }, 0); + } + } var server = this.client.servers[this.server]; if (this.description !== null) { + callback(); return; } else if (server.describeProcessResponse[this.identifier] === null) { // pending request + this.events.register('describeprocess', this, callback); return; } else if (this.identifier in server.describeProcessResponse) { // process description already cached on client this.parseDescription(); + callback(); return; } // set to null so we know a describeFeature request is pending @@ -109,14 +146,21 @@ OpenLayers.WPSProcess = OpenLayers.Class({ REQUEST: 'DescribeProcess', IDENTIFIER: this.identifier }, - success: this.parseDescription, + success: function(response) { + this.parseDescription(response); + if (options.callback) { + options.callback.call(options.scope, this.description); + } + }, scope: this }); }, /** - * APIMethod: execute - * Executes the process + * APIMethod: configure + * Configure the process, but do not execute it. Use this for processes + * that are chained as input of a different process by means of the + * method. * * Parameters: * options - {Object} @@ -126,20 +170,18 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * For spatial data inputs, the value of an input is usually an * , an or an array of * geometries or features. - * success - {Function} Callback to call when the process is complete. - * This function is called with an outputs object as argument, which - * will have a 'result' property. For processes that generate spatial - * output, this will either be a single or - * an array of features. - * scope - {Object} Optional scope for the success callback. + * callback - {Function} Callback to call when the configuration is + * complete. Optional. + * scope - {Object} Optional scope for the callback. */ - execute: function(options) { + configure: function(options) { if (!this.description) { - this.events.register('describeprocess', this, function execute() { - this.events.unregister('describeprocess', this, execute); - this.execute(options); + this.describe({ + callback: function() { + this.configure(options); + }, + scope: this }); - this.describe(); return; } var description = this.description, @@ -149,26 +191,105 @@ OpenLayers.WPSProcess = OpenLayers.Class({ input = description.dataInputs[i]; this.setInputData(input, inputs[input.identifier]); } - //TODO For now we only handle responseForm with a single output - this.setResponseForm(); - OpenLayers.Request.POST({ - url: this.client.servers[this.server].url, - data: new OpenLayers.Format.WPSExecute().write(this.description), - success: function(response) { - var mimeType = this.findMimeType(this.description.processOutputs[0].complexOutput); - var features = this.formats[mimeType].read(response.responseText); - if (options.success) { - options.success.call(options.scope, { - result: features + if (options.callback) { + options.callback.call(options.scope); + } + }, + + /** + * APIMethod: execute + * Configures and executes the process + * + * Parameters: + * options - {Object} + * + * Available options: + * inputs - {Object} The inputs for the process, keyed by input identifier. + * For spatial data inputs, the value of an input is usually an + * , an or an array of + * geometries or features. + * output - {String} The identifier of an output to parse. Optional. If not + * provided, the first output will be parsed. + * success - {Function} Callback to call when the process is complete. + * This function is called with an outputs object as argument, which + * will have a 'result' property. For processes that generate spatial + * output, this will either be a single or + * an array of features. + * scope - {Object} Optional scope for the success callback. + */ + execute: function(options) { + this.configure({ + inputs: options.inputs, + callback: function() { + var me = this; + //TODO For now we only deal with a single output + var output = this.getOutputIndex( + me.description.processOutputs, options.output + ); + me.setResponseForm({outputIndex: output}); + (function callback() { + OpenLayers.Util.removeItem(me.executeCallbacks, callback); + if (me.chained !== 0) { + me.executeCallbacks.push(callback); + return; + } + OpenLayers.Request.POST({ + url: me.client.servers[me.server].url, + data: new OpenLayers.Format.WPSExecute().write(me.description), + success: function(response) { + var mimeType = me.findMimeType( + me.description.processOutputs[output].complexOutput.supported.formats + ); + //TODO For now we assume a spatial output + var features = me.formats[mimeType].read(response.responseText); + if (options.success) { + options.success.call(options.scope, { + result: features + }); + } + }, + scope: me }); - } + })(); }, scope: this }); }, + /** + * APIMethod: output + * Chain an output of a configured process (see ) as input to + * another process. + * + * (code) + * intersect = client.getProcess('opengeo', 'JTS:intersection'); + * intersect.configure({ + * // ... + * }); + * buffer = client.getProcess('opengeo', 'JTS:buffer'); + * buffer.execute({ + * inputs: { + * geom: intersect.output(), // <-- here we're chaining + * distance: 1 + * }, + * // ... + * }); + * (end) + * + * Parameters: + * identifier - {String} Identifier of the output that we're chaining. If + * not provided, the first output will be used. + */ + output: function(identifier) { + return new OpenLayers.WPSProcess.ChainLink({ + process: this, + output: identifier + }); + }, + /** * Method: parseDescription + * Parses the DescribeProcess response * * Parameters: * response - {Object} @@ -197,15 +318,37 @@ OpenLayers.WPSProcess = OpenLayers.Class({ */ setInputData: function(input, data) { // clear any previous data - input.data = {}; - if (data) { + delete input.data; + delete input.reference; + if (data instanceof OpenLayers.WPSProcess.ChainLink) { + ++this.chained; + input.reference = { + method: 'POST', + href: data.process.server === this.server ? + //TODO what about implementations other than GeoServer? + 'http://geoserver/wps' : + this.client.servers[data.process.server].url + }; + data.process.describe({ + callback: function() { + --this.chained; + this.chainProcess(input, data); + }, + scope: this + }); + } else { + input.data = {}; var complexData = input.complexData; if (complexData) { - var format = this.findMimeType(complexData); + var format = this.findMimeType(complexData.supported.formats); input.data.complexData = { mimeType: format, value: this.formats[format].write(this.toFeatures(data)) }; + } else { + input.data.literalData = { + value: data + }; } } }, @@ -213,17 +356,81 @@ OpenLayers.WPSProcess = OpenLayers.Class({ /** * Method: setResponseForm * Sets the responseForm property of the payload. + * + * Parameters: + * options - {Object} See below. + * + * Available options: + * outputIndex - {Integer} The index of the output to use. Optional. + * supportedFormats - {Object} Object with supported mime types as key, + * and true as value for supported types. Optional. */ - setResponseForm: function() { - output = this.description.processOutputs[0]; + setResponseForm: function(options) { + options = options || {}; + output = this.description.processOutputs[options.outputIndex || 0]; this.description.responseForm = { rawDataOutput: { identifier: output.identifier, - mimeType: this.findMimeType(output.complexOutput) + mimeType: this.findMimeType(output.complexOutput.supported.formats, options.supportedFormats) } }; }, + /** + * Method: getOutputIndex + * Gets the index of a processOutput by its identifier + * + * Parameters: + * outputs - {Array} The processOutputs array to look at + * identifier - {String} The identifier of the output + * + * Returns + * {Integer} The index of the processOutput with the provided identifier + * in the outputs array. + */ + getOutputIndex: function(outputs, identifier) { + var output; + if (identifier) { + for (var i=outputs.length-1; i>=0; --i) { + if (outputs[i].identifier === identifier) { + output = i; + break; + } + } + } else { + output = 0; + } + return output; + }, + + /** + * Method: chainProcess + * Sets a fully configured chained process as input for this process. + * + * Parameters: + * input - {Object} The dataInput that the chained process provides. + * chainLink - { 0) { + this.executeCallbacks[0](); + } + }, + /** * Method: toFeatures * Converts spatial input into features so it can be processed by @@ -256,16 +463,19 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * Finds a supported mime type. * * Parameters: - * complex - {Object} A complexData or complexOutput object from the - * process description. + * sourceFormats - {Object} An object literal with mime types as key and + * true as value for supported formats. + * targetFormats - {Object} Like , but optional to check for + * supported mime types on a different target than this process. + * Default is to check against this process's supported formats. * * Returns: * {String} A supported mime type. */ - findMimeType: function(complex) { - var formats = complex.supported.formats; - for (var f in formats) { - if (f in this.formats) { + findMimeType: function(sourceFormats, targetFormats) { + targetFormats = targetFormats || this.formats; + for (var f in sourceFormats) { + if (f in targetFormats) { return f; } } @@ -274,3 +484,18 @@ OpenLayers.WPSProcess = OpenLayers.Class({ CLASS_NAME: "OpenLayers.WPSProcess" }); + +/** + * Class: OpenLayers.WPSProcess.ChainLink + */ +OpenLayers.WPSProcess.ChainLink = OpenLayers.Class({ + + process: null, + + output: null, + + initialize: function(options) { + OpenLayers.Util.extend(this, options); + } + +}); From ebc71b492e21f08c5466674988841556c11bbdbe Mon Sep 17 00:00:00 2001 From: ahocevar Date: Thu, 9 Aug 2012 18:29:37 +0200 Subject: [PATCH 06/14] Making sure that the process description is not loaded. --- tests/WPSProcess.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index 460ddc2451..95821201d1 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -55,7 +55,11 @@ log.push(cfg); } - process = client.getProcess('local', 'gs:splitPolygon'); + process = new OpenLayers.WPSProcess({ + client: client, + server: 'local', + identifier: 'gs:splitPolygon' + }); process.description = { dataInputs: [{ identifier: 'line', From e2acbc56d0d672b20bccb220b40304c1d23f5e53 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 10 Aug 2012 17:02:37 +0200 Subject: [PATCH 07/14] More docs and unit tests. --- lib/OpenLayers/WPSClient.js | 4 +-- lib/OpenLayers/WPSProcess.js | 24 +++++++++++++- tests/WPSProcess.html | 62 +++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js index 5078abd8a1..60b5e093ff 100644 --- a/lib/OpenLayers/WPSClient.js +++ b/lib/OpenLayers/WPSClient.js @@ -19,8 +19,8 @@ OpenLayers.WPSClient = OpenLayers.Class({ * Properties: * url - {String} the url of the server * version - {String} WPS version of the server - * describeProcessResponse - {Object} Cache of DescribeProcess responses, - * keyed by process identifier. + * describeProcessResponse - {Object} Cache of raw DescribeProcess + * responses, keyed by process identifier. */ servers: null, diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js index 7344df5768..efca240a83 100644 --- a/lib/OpenLayers/WPSProcess.js +++ b/lib/OpenLayers/WPSProcess.js @@ -230,9 +230,13 @@ OpenLayers.WPSProcess = OpenLayers.Class({ (function callback() { OpenLayers.Util.removeItem(me.executeCallbacks, callback); if (me.chained !== 0) { + // need to wait until chained processes have a + // description and configuration - see chainProcess me.executeCallbacks.push(callback); return; } + // all chained processes are added as references now, so + // let's proceed. OpenLayers.Request.POST({ url: me.client.servers[me.server].url, data: new OpenLayers.Format.WPSExecute().write(me.description), @@ -487,15 +491,33 @@ OpenLayers.WPSProcess = OpenLayers.Class({ /** * Class: OpenLayers.WPSProcess.ChainLink + * Type for chaining processes. */ OpenLayers.WPSProcess.ChainLink = OpenLayers.Class({ + /** + * Property: process + * {} The process to chain + */ process: null, + /** + * Property: output + * {String} The output identifier of the output we are going to use as + * input for another process. + */ output: null, + /** + * Constructor: OpenLayers.WPSProcess.ChainLink + * + * Parameters: + * options - {Object} Properties to set on the instance. + */ initialize: function(options) { OpenLayers.Util.extend(this, options); - } + }, + + CLASS_NAME: "OpenLayers.WPSProcess.ChainLink" }); diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index 95821201d1..a528507ae7 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -9,6 +9,10 @@ local: 'geoserver/wps' } }); + client.servers.local.describeProcessResponse = { + 'JTS:intersection': '' + + 'JTS:intersectionReturns the intersectoin between a and b (eventually an empty collection if there is no intersection)Returns the intersectoin between a and b (eventually an empty collection if there is no intersection)aa[undescribed]text/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2bb[undescribed]text/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2resultProcess resulttext/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2' + }; function test_initialize(t) { t.plan(2); @@ -35,7 +39,7 @@ process.describe(); t.eq(log.request.length, 1, 'describeProcess request only sent once'); log.request[0].success.call(process, { - responseText: '' + responseText: '' }); t.eq(log.event[0].type, 'describeprocess', 'describeprocess event triggered'); t.ok(client.servers.local.describeProcessResponse['gs:splitPolygon'], 'We have a process description!'); @@ -103,6 +107,62 @@ OpenLayers.Request.POST = originalPOST; } + + function test_chainProcess(t) { + t.plan(3); + + var originalGET = OpenLayers.Request.GET; + OpenLayers.Request.GET = function(cfg) { + window.setTimeout(function() { + cfg.success.call(cfg.scope, { + responseText: '' + + 'JTS:bufferBuffers a geometry using a certain distanceBuffers a geometry using a certain distancegeomgeomThe geometry to be bufferedtext/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2distancedistanceThe distance (same unit of measure as the geometry)xs:doublequadrantSegmentsquadrantSegmentsNumber of quadrant segments. Use > 0 for round joins, 0 for flat joins, < 0 for mitred joinsxs:intcapStylecapStyleThe buffer cap style, round, flat, squareRoundFlatSquareresultresulttext/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2' + }); + }, 100); + } + var originalPOST = OpenLayers.Request.POST; + OpenLayers.Request.POST = function(cfg) { + cfg.success.call(cfg.scope, {responseText: ''}); + }; + + var intersect = client.getProcess('local', 'JTS:intersection'); + intersect.configure({ + inputs: { + a: new OpenLayers.Format.WKT().read( + 'LINESTRING(117 22,112 18,118 13,115 8)' + ), + b: new OpenLayers.Format.WKT().read( + 'POLYGON((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))' + ) + } + }); + + var buffer = client.getProcess('local', 'JTS:buffer'); + var log = []; + buffer.chainProcess = function() { + log.push(this.executeCallbacks.length); + OpenLayers.WPSProcess.prototype.chainProcess.apply(this, arguments); + }; + var done = false; + buffer.execute({ + inputs: { + geom: intersect.output(), + distance: 1 + }, + success: function(outputs) { + done = true; + } + }); + + t.delay_call(0.2, function() { + t.eq(log.length, 1, 'chainProcess called'); + t.eq(log[0], 1, 'executeCallback queued to wait for 1 chained process'); + t.eq(done, true, 'execute successfully completed'); + + OpenLayers.Request.GET = originalGET; + OpenLayers.Request.POST = originalPOST; + }); + } From 96db01006cd67aad6cbadb179f68740b9eda7d4c Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 10 Aug 2012 23:20:52 +0200 Subject: [PATCH 08/14] Making Firefox happy. --- tests/WPSProcess.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index a528507ae7..6bdfcc2430 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -39,7 +39,7 @@ process.describe(); t.eq(log.request.length, 1, 'describeProcess request only sent once'); log.request[0].success.call(process, { - responseText: '' + responseText: '' }); t.eq(log.event[0].type, 'describeprocess', 'describeprocess event triggered'); t.ok(client.servers.local.describeProcessResponse['gs:splitPolygon'], 'We have a process description!'); From 4e3f3e2080198e97da3ce4e026226a98ee7cc2bf Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sun, 12 Aug 2012 21:39:15 +0200 Subject: [PATCH 09/14] Adding dependencies. --- lib/OpenLayers/WPSClient.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js index 60b5e093ff..fef748915e 100644 --- a/lib/OpenLayers/WPSClient.js +++ b/lib/OpenLayers/WPSClient.js @@ -7,6 +7,15 @@ * @requires OpenLayers/SingleFile.js */ +/** + * @requires OpenLayers/WPSProcess.js + * @requires OpenLayers/Format/WKT.js + * @requires OpenLayers/Format/GeoJSON.js + * @requires OpenLayers/Format/WPSDescribeProcess.js + * @requires OpenLayers/Format/WPSExecute.js + * @requires OpenLayers/Request.js + */ + /** * Class: OpenLayers.WPSClient */ From 94f3ab393ffcaa889312f8c17771700638bd37db Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sun, 12 Aug 2012 22:08:43 +0200 Subject: [PATCH 10/14] Giving the test code more time to execute. --- tests/WPSProcess.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index 6bdfcc2430..44d81b3801 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -154,7 +154,7 @@ } }); - t.delay_call(0.2, function() { + t.delay_call(0.5, function() { t.eq(log.length, 1, 'chainProcess called'); t.eq(log[0], 1, 'executeCallback queued to wait for 1 chained process'); t.eq(done, true, 'execute successfully completed'); From 006d98151f5fb261e7b5f185c9fc0136ff167252 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 13 Aug 2012 13:29:34 +0200 Subject: [PATCH 11/14] Use the correct name of the result, not just hard-coded 'result'. --- lib/OpenLayers/WPSClient.js | 7 ++++--- lib/OpenLayers/WPSProcess.js | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js index fef748915e..257e43166f 100644 --- a/lib/OpenLayers/WPSClient.js +++ b/lib/OpenLayers/WPSClient.js @@ -94,9 +94,10 @@ OpenLayers.WPSClient = OpenLayers.Class({ * geometries or features. * success - {Function} Callback to call when the process is complete. * This function is called with an outputs object as argument, which - * will have a 'result' property. For processes that generate spatial - * output, this will either be a single or - * an array of features. + * will have a property with the name of the requested output (e.g. + * 'result'). For processes that generate spatial output, the value + * will either be a single or an array of + * features. * scope - {Object} Optional scope for the success callback. */ execute: function(options) { diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js index efca240a83..749c3473a8 100644 --- a/lib/OpenLayers/WPSProcess.js +++ b/lib/OpenLayers/WPSProcess.js @@ -212,9 +212,10 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * provided, the first output will be parsed. * success - {Function} Callback to call when the process is complete. * This function is called with an outputs object as argument, which - * will have a 'result' property. For processes that generate spatial - * output, this will either be a single or - * an array of features. + * will have a property with the name of the requested output (e.g. + * 'result'). For processes that generate spatial output, the value + * will either be a single or an array of + * features. * scope - {Object} Optional scope for the success callback. */ execute: function(options) { @@ -223,10 +224,10 @@ OpenLayers.WPSProcess = OpenLayers.Class({ callback: function() { var me = this; //TODO For now we only deal with a single output - var output = this.getOutputIndex( + var outputIndex = this.getOutputIndex( me.description.processOutputs, options.output ); - me.setResponseForm({outputIndex: output}); + me.setResponseForm({outputIndex: outputIndex}); (function callback() { OpenLayers.Util.removeItem(me.executeCallbacks, callback); if (me.chained !== 0) { @@ -241,15 +242,16 @@ OpenLayers.WPSProcess = OpenLayers.Class({ url: me.client.servers[me.server].url, data: new OpenLayers.Format.WPSExecute().write(me.description), success: function(response) { + var output = me.description.processOutputs[outputIndex]; var mimeType = me.findMimeType( - me.description.processOutputs[output].complexOutput.supported.formats + output.complexOutput.supported.formats ); //TODO For now we assume a spatial output var features = me.formats[mimeType].read(response.responseText); if (options.success) { - options.success.call(options.scope, { - result: features - }); + var outputs = {}; + outputs[output.identifier] = features; + options.success.call(options.scope, outputs); } }, scope: me From 5fff368a2d75dcc4d3dcd735f508829d6fda4c54 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 14 Aug 2012 13:17:07 +0200 Subject: [PATCH 12/14] Addressing review suggestions and fixing asynchronous function calls. This commit addresses @bartvde's review comments, adds more documentation, and fixes asynchronous function calls. Previously, when creating multiple processes with the same identifier, the describe callback would only have been called for the first process. This was fixed to move DescribeProcess handling from WPSProcess to WPSClient. --- examples/wps-client.html | 4 +- examples/wps-client.js | 2 +- lib/OpenLayers/WPSClient.js | 106 +++++++++++++++++++---- lib/OpenLayers/WPSProcess.js | 157 +++++++++++++++-------------------- tests/WPSClient.html | 32 ++++++- tests/WPSProcess.html | 112 ++++++++++++++----------- 6 files changed, 255 insertions(+), 158 deletions(-) diff --git a/examples/wps-client.html b/examples/wps-client.html index b588f7fba2..379f1bb990 100644 --- a/examples/wps-client.html +++ b/examples/wps-client.html @@ -22,7 +22,9 @@

    WPS Client Example

    -

    This example shows how simple it is to use the WPS Client. See +

    This example shows how simple it is to use the WPS Client. It + buffers an intersection of a geometry and a feature, which is + accomplished by chaining two processes. See wps-client.js to see how this is done.

    diff --git a/examples/wps-client.js b/examples/wps-client.js index dee7e5c50d..304c59271a 100644 --- a/examples/wps-client.js +++ b/examples/wps-client.js @@ -56,7 +56,7 @@ function init() { // the client directly if we are only dealing with a single process: /* client.execute({ - server: "local", + server: "opengeo", process: "JTS:intersection", // spatial input can be a feature or a geometry or an array of // features or geometries diff --git a/lib/OpenLayers/WPSClient.js b/lib/OpenLayers/WPSClient.js index 257e43166f..a335632219 100644 --- a/lib/OpenLayers/WPSClient.js +++ b/lib/OpenLayers/WPSClient.js @@ -8,16 +8,19 @@ */ /** + * @requires OpenLayers/Events.js * @requires OpenLayers/WPSProcess.js - * @requires OpenLayers/Format/WKT.js - * @requires OpenLayers/Format/GeoJSON.js * @requires OpenLayers/Format/WPSDescribeProcess.js - * @requires OpenLayers/Format/WPSExecute.js * @requires OpenLayers/Request.js */ /** * Class: OpenLayers.WPSClient + * High level API for interaction with Web Processing Services (WPS). + * An instance is used to create + * instances for servers known to the WPSClient. The WPSClient also caches + * DescribeProcess responses to reduce the number of requests sent to servers + * when processes are created. */ OpenLayers.WPSClient = OpenLayers.Class({ @@ -28,11 +31,18 @@ OpenLayers.WPSClient = OpenLayers.Class({ * Properties: * url - {String} the url of the server * version - {String} WPS version of the server - * describeProcessResponse - {Object} Cache of raw DescribeProcess + * processDescription - {Object} Cache of raw DescribeProcess * responses, keyed by process identifier. */ servers: null, + /** + * Property: version + * {String} The default WPS version to use if none is configured. Default + * is '1.0.0'. + */ + version: '1.0.0', + /** * Property: lazy * {Boolean} Should the DescribeProcess be deferred until a process is @@ -40,6 +50,18 @@ OpenLayers.WPSClient = OpenLayers.Class({ */ lazy: false, + /** + * Property: events + * {} + * + * Supported event types: + * describeprocess - Fires when the process description is available. + * Listeners receive an object with a 'raw' property holding the raw + * DescribeProcess response, and an 'identifier' property holding the + * process identifier of the described process. + */ + events: null, + /** * Constructor: OpenLayers.WPSClient * @@ -67,18 +89,22 @@ OpenLayers.WPSClient = OpenLayers.Class({ */ initialize: function(options) { OpenLayers.Util.extend(this, options); + this.events = new OpenLayers.Events(this); this.servers = {}; for (var s in options.servers) { this.servers[s] = typeof options.servers[s] == 'string' ? { url: options.servers[s], - version: '1.0.0', - describeProcessResponse: {} + version: this.version, + processDescription: {} } : options.servers[s]; } }, /** * APIMethod: execute + * Shortcut to execute a process with a single function call. This is + * equivalent to using and then calling execute on the + * process. * * Parameters: * options - {Object} Options for the execute operation. @@ -92,12 +118,14 @@ OpenLayers.WPSClient = OpenLayers.Class({ * For spatial data inputs, the value of an input is usually an * , an or an array of * geometries or features. + * output - {String} The identifier of an output to parse. Optional. If not + * provided, the first output will be parsed. * success - {Function} Callback to call when the process is complete. * This function is called with an outputs object as argument, which - * will have a property with the name of the requested output (e.g. - * 'result'). For processes that generate spatial output, the value - * will either be a single or an array of - * features. + * will have a property with the identifier of the requested output + * (e.g. 'result'). For processes that generate spatial output, the + * value will either be a single or an + * array of features. * scope - {Object} Optional scope for the success callback. */ execute: function(options) { @@ -114,18 +142,18 @@ OpenLayers.WPSClient = OpenLayers.Class({ * Creates an . * * Parameters: - * server - {String} Local identifier from the servers that this instance + * serverID - {String} Local identifier from the servers that this instance * was constructed with. - * identifier - {String} Process identifier known to the server. + * processID - {String} Process identifier known to the server. * * Returns: * {} */ - getProcess: function(server, identifier) { + getProcess: function(serverID, processID) { var process = new OpenLayers.WPSProcess({ client: this, - server: server, - identifier: identifier + server: serverID, + identifier: processID }); if (!this.lazy) { process.describe(); @@ -133,6 +161,54 @@ OpenLayers.WPSClient = OpenLayers.Class({ return process; }, + /** + * Method: describeProcess + * + * Parameters: + * serverID - {String} Identifier of the server + * processID - {String} Identifier of the requested process + * callback - {Function} Callback to call when the description is available + * scope - {Object} Optional execution scope for the callback function + */ + describeProcess: function(serverID, processID, callback, scope) { + var server = this.servers[serverID]; + if (!server.processDescription[processID]) { + if (!(processID in server.processDescription)) { + // set to null so we know a describeFeature request is pending + server.processDescription[processID] = null; + OpenLayers.Request.GET({ + url: server.url, + params: { + SERVICE: 'WPS', + VERSION: server.version, + REQUEST: 'DescribeProcess', + IDENTIFIER: processID + }, + success: function(response) { + server.processDescription[processID] = response.responseText; + this.events.triggerEvent('describeprocess', { + identifier: processID, + raw: response.responseText + }); + }, + scope: this + }); + } else { + // pending request + this.events.register('describeprocess', this, function describe(evt) { + if (evt.identifier === processID) { + this.events.unregister('describeprocess', this, describe); + callback.call(scope, evt); + } + }); + } + } else { + window.setTimeout(function() { + callback.call(scope, server.processDescription[processID]); + }, 0); + } + }, + CLASS_NAME: 'OpenLayers.WPSClient' }); diff --git a/lib/OpenLayers/WPSProcess.js b/lib/OpenLayers/WPSProcess.js index 749c3473a8..323d87bded 100644 --- a/lib/OpenLayers/WPSProcess.js +++ b/lib/OpenLayers/WPSProcess.js @@ -8,26 +8,27 @@ */ /** - * @requires OpenLayers/Events.js * @requires OpenLayers/Geometry.js * @requires OpenLayers/Feature/Vector.js + * @requires OpenLayers/Format/WKT.js + * @requires OpenLayers/Format/GeoJSON.js + * @requires OpenLayers/Format/WPSExecute.js + * @requires OpenLayers/Request.js */ /** * Class: OpenLayers.WPSProcess + * Representation of a WPS process. Usually instances of + * are created by calling 'getProcess' on an + * instance. + * + * Currently supports processes that have geometries + * or features as output, using WKT or GeoJSON as output format. It also + * supports chaining of processes by using the method to create a + * handle that is used as process input instead of a static value. */ OpenLayers.WPSProcess = OpenLayers.Class({ - /** - * APIProperty: events - * {} - * - * Supported event types: - * describeprocess - Fires when the process description is available for - * the first time. - */ - events: null, - /** * Property: client * {} The client that manages this process. @@ -52,6 +53,13 @@ OpenLayers.WPSProcess = OpenLayers.Class({ */ description: null, + /** + * APIProperty: localWPS + * {String} Service endpoint for locally chained WPS processes. Default is + * 'http://geoserver/wps'. + */ + localWPS: 'http://geoserver/wps', + /** * Property: formats * {Object} OpenLayers.Format instances keyed by mimetype. @@ -60,7 +68,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({ /** * Property: chained - * {Integer} Number of chained processes for pending execute reqeusts that + * {Integer} Number of chained processes for pending execute requests that * don't have a full configuration yet. */ chained: 0, @@ -79,18 +87,15 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * options - {Object} Object whose properties will be set on the instance. * * Avaliable options: - * client - {} Mandatory. Client that manages this * process. * server - {String} Mandatory. Local client identifier of this process's * server. * identifier - {String} Mandatory. Process identifier known to the server. */ initialize: function(options) { - OpenLayers.Util.extend(this, options); - - this.events = new OpenLayers.Events(this); + OpenLayers.Util.extend(this, options); this.executeCallbacks = []; - this.formats = { 'application/wkt': new OpenLayers.Format.WKT(), 'application/json': new OpenLayers.Format.GeoJSON() @@ -99,12 +104,10 @@ OpenLayers.WPSProcess = OpenLayers.Class({ /** * Method: describe - * Issues a DescribeProcess request asynchronously and fires the - * 'describeprocess' event as soon as the response is available in - * . + * Makes the client ssues a DescribeProcess request asynchronously. * * Parameters: - * options - {Object} Coniguration for the method call + * options - {Object} Configuration for the method call * * Available options: * callback - {Function} Callback to execute when the description is @@ -115,45 +118,21 @@ OpenLayers.WPSProcess = OpenLayers.Class({ */ describe: function(options) { options = options || {}; - function callback() { - if (options.callback) { - window.setTimeout(function() { - options.callback.call(options.scope, this.description); - }, 0); - } - } - var server = this.client.servers[this.server]; - if (this.description !== null) { - callback(); - return; - } else if (server.describeProcessResponse[this.identifier] === null) { - // pending request - this.events.register('describeprocess', this, callback); - return; - } else if (this.identifier in server.describeProcessResponse) { - // process description already cached on client - this.parseDescription(); - callback(); - return; - } - // set to null so we know a describeFeature request is pending - server.describeProcessResponse[this.identifier] = null; - OpenLayers.Request.GET({ - url: server.url, - params: { - SERVICE: 'WPS', - VERSION: server.version, - REQUEST: 'DescribeProcess', - IDENTIFIER: this.identifier - }, - success: function(response) { - this.parseDescription(response); + if (!this.description) { + this.client.describeProcess(this.server, this.identifier, function(description) { + if (!this.description) { + this.parseDescription(description); + } if (options.callback) { options.callback.call(options.scope, this.description); } - }, - scope: this - }); + }, this); + } else if (options.callback) { + var description = this.description; + window.setTimeout(function() { + options.callback.call(options.scope, description); + }, 0); + } }, /** @@ -175,25 +154,22 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * scope - {Object} Optional scope for the callback. */ configure: function(options) { - if (!this.description) { - this.describe({ - callback: function() { - this.configure(options); - }, - scope: this - }); - return; - } - var description = this.description, - inputs = options.inputs, - input, i, ii; - for (i=0, ii=description.dataInputs.length; i or an array of - * features. + * will have a property with the identifier of the requested output + * (or 'result' if output was not configured). For processes that + * generate spatial output, the value will be an array of + * instances. * scope - {Object} Optional scope for the success callback. */ execute: function(options) { @@ -248,9 +224,12 @@ OpenLayers.WPSProcess = OpenLayers.Class({ ); //TODO For now we assume a spatial output var features = me.formats[mimeType].read(response.responseText); + if (features instanceof OpenLayers.Feature.Vector) { + features = [features]; + } if (options.success) { var outputs = {}; - outputs[output.identifier] = features; + outputs[options.output || 'result'] = features; options.success.call(options.scope, outputs); } }, @@ -298,17 +277,13 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * Parses the DescribeProcess response * * Parameters: - * response - {Object} + * description - {Object} */ - parseDescription: function(response) { + parseDescription: function(description) { var server = this.client.servers[this.server]; - if (response) { - server.describeProcessResponse[this.identifier] = response.responseText; - } this.description = new OpenLayers.Format.WPSDescribeProcess() - .read(server.describeProcessResponse[this.identifier]) + .read(server.processDescription[this.identifier]) .processDescriptions[this.identifier]; - this.events.triggerEvent('describeprocess'); }, /** @@ -331,9 +306,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({ input.reference = { method: 'POST', href: data.process.server === this.server ? - //TODO what about implementations other than GeoServer? - 'http://geoserver/wps' : - this.client.servers[data.process.server].url + this.localWPS : this.client.servers[data.process.server].url }; data.process.describe({ callback: function() { @@ -415,7 +388,7 @@ OpenLayers.WPSProcess = OpenLayers.Class({ * * Parameters: * input - {Object} The dataInput that the chained process provides. - * chainLink - {} The process to chain. */ chainProcess: function(input, chainLink) { var output = this.getOutputIndex( diff --git a/tests/WPSClient.html b/tests/WPSClient.html index bd1eb477bc..2a9bc892b6 100644 --- a/tests/WPSClient.html +++ b/tests/WPSClient.html @@ -6,7 +6,7 @@ var client; function test_initialize(t) { - t.plan(2); + t.plan(3); client = new OpenLayers.WPSClient({ servers: { @@ -15,6 +15,7 @@ }); t.ok(client instanceof OpenLayers.WPSClient, 'creates an instance'); + t.ok(client.events, 'has an events instance'); t.eq(client.servers.local.url, '/geoserver/wps', 'servers stored on instance'); } @@ -36,6 +37,35 @@ } + function test_describeProcess(t) { + t.plan(6); + var log = {request: [], event: []}; + var originalGET = OpenLayers.Request.GET; + OpenLayers.Request.GET = function(cfg) { + log.request.push(cfg); + } + function describe(evt) { + log.event.push(evt); + } + client.events.register('describeprocess', this, describe); + + process = client.getProcess('local', 'gs:splitPolygon'); + t.eq(client.servers.local.processDescription['gs:splitPolyon'], null, 'describeProcess pending'); + process.describe(); + t.eq(log.request.length, 1, 'describeProcess request only sent once'); + log.request[0].success.call(client, { + responseText: '' + }); + t.eq(log.event[0].type, 'describeprocess', 'describeprocess event triggered'); + t.ok(client.servers.local.processDescription['gs:splitPolygon'], 'We have a process description!'); + process.describe(); + t.eq(log.request.length, 1, 'describeProcess request only sent once'); + t.eq(log.event.length, 1, 'describeprocess event only triggered once'); + + OpenLayers.Request.GET = originalGET; + client.events.unregister('describeprocess', this, describe); + } + function test_execute(t) { t.plan(1); diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index 44d81b3801..856b3c8aa6 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -9,54 +9,38 @@ local: 'geoserver/wps' } }); - client.servers.local.describeProcessResponse = { + client.servers.local.processDescription = { 'JTS:intersection': '' + 'JTS:intersectionReturns the intersectoin between a and b (eventually an empty collection if there is no intersection)Returns the intersectoin between a and b (eventually an empty collection if there is no intersection)aa[undescribed]text/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2bb[undescribed]text/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2resultProcess resulttext/xml; subtype=gml/3.1.1text/xml; subtype=gml/3.1.1text/xml; subtype=gml/2.1.2application/wktapplication/gml-3.1.1application/gml-2.1.2' }; function test_initialize(t) { - t.plan(2); - + t.plan(1); process = new OpenLayers.WPSProcess(); - t.ok(process instanceof OpenLayers.WPSProcess, 'creates an instance'); - t.ok(process.events, 'has an events instance'); } function test_describe(t) { - t.plan(6); - var log = {request: [], event: []}; - var originalGET = OpenLayers.Request.GET; - OpenLayers.Request.GET = function(cfg) { - log.request.push(cfg); - } - - process = client.getProcess('local', 'gs:splitPolygon'); - process.events.register('describeprocess', this, function(evt) { - log.event.push(evt); + t.plan(2); + process = client.getProcess('local', 'JTS:intersection'); + var log = []; + process.describe({ + callback: function(description) { log.push(description); } }); - t.eq(client.servers.local.describeProcessResponse['gs:splitPolyon'], null, 'describeProcess pending'); - process.describe(); - t.eq(log.request.length, 1, 'describeProcess request only sent once'); - log.request[0].success.call(process, { - responseText: '' + t.delay_call(0.1, function() { + t.eq(log.length, 1, 'callback called'); + t.eq(log[0].identifier, 'JTS:intersection', 'callback called with correct description'); }); - t.eq(log.event[0].type, 'describeprocess', 'describeprocess event triggered'); - t.ok(client.servers.local.describeProcessResponse['gs:splitPolygon'], 'We have a process description!'); - process.describe(); - t.eq(log.request.length, 1, 'describeProcess request only sent once'); - t.eq(log.event.length, 1, 'describeprocess event only triggered once'); - - OpenLayers.Request.GET = originalGET; } function test_execute(t) { - t.plan(5); + t.plan(7); var log = []; var originalPOST = OpenLayers.Request.POST; OpenLayers.Request.POST = function(cfg) { log.push(cfg); + cfg.success.call(cfg.scope, {responseText: ''}); } process = new OpenLayers.WPSProcess({ @@ -81,7 +65,7 @@ } }], processOutputs: [{ - identifier: 'result', + identifier: 'foo', complexOutput: { supported: { formats: {'application/wkt': true} @@ -90,26 +74,44 @@ }] }; var line = 'LINESTRING(117 22,112 18,118 13,115 8)'; + var polygon = 'POLYGON((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))'; + var output = []; + function success(result) { + output.push(result); + } + // configured with output identifier process.execute({ inputs: { line: new OpenLayers.Format.WKT().read(line), - polygon: new OpenLayers.Format.WKT().read( - 'POLYGON((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))' - ) - } + polygon: new OpenLayers.Format.WKT().read(polygon) + }, + output: 'foo', + success: success + }); + // configured without output identifier + process.execute({ + inputs: { + line: new OpenLayers.Format.WKT().read(line), + polygon: new OpenLayers.Format.WKT().read(polygon) + }, + success: success }); - t.eq(log.length, 1, 'execute request sent'); - t.eq(process.description.dataInputs[0].data.complexData.value, line, 'data for first input correct'); - t.eq(process.description.dataInputs[0].data.complexData.mimeType, 'application/wkt', 'format for first input correct'); - t.eq(process.description.responseForm.rawDataOutput.identifier, 'result', 'correct identifier for responseForm'); - t.eq(process.description.responseForm.rawDataOutput.mimeType, 'application/wkt', 'correct format for responseForm'); - - OpenLayers.Request.POST = originalPOST; + t.delay_call(0.1, function() { + t.eq(log.length, 2, 'Two execute requests sent'); + t.eq(process.description.dataInputs[0].data.complexData.value, line, 'data for first input correct'); + t.eq(process.description.dataInputs[0].data.complexData.mimeType, 'application/wkt', 'format for first input correct'); + t.eq(process.description.responseForm.rawDataOutput.identifier, 'foo', 'correct identifier for responseForm'); + t.eq(process.description.responseForm.rawDataOutput.mimeType, 'application/wkt', 'correct format for responseForm'); + t.ok('foo' in output[0], 'process result contains output with correct identifier when configured with output'); + t.ok('result' in output[1], 'process result contains output with correct identifier when configured without output'); + + OpenLayers.Request.POST = originalPOST; + }); } function test_chainProcess(t) { - t.plan(3); + t.plan(5); var originalGET = OpenLayers.Request.GET; OpenLayers.Request.GET = function(cfg) { @@ -137,27 +139,41 @@ } }); - var buffer = client.getProcess('local', 'JTS:buffer'); + // one buffer process to make sure chaining works + var buffer1 = client.getProcess('local', 'JTS:buffer'); + // another buffer process to make sure that things work asynchronously + var buffer2 = client.getProcess('local', 'JTS:buffer'); var log = []; - buffer.chainProcess = function() { + buffer1.chainProcess = buffer2.chainProcess = function() { log.push(this.executeCallbacks.length); OpenLayers.WPSProcess.prototype.chainProcess.apply(this, arguments); }; - var done = false; - buffer.execute({ + var done1 = done2 = false; + buffer1.execute({ inputs: { geom: intersect.output(), distance: 1 }, success: function(outputs) { - done = true; + done1 = true; + } + }); + buffer2.execute({ + inputs: { + geom: intersect.output(), + distance: 2 + }, + success: function(outputs) { + done2 = true; } }); t.delay_call(0.5, function() { - t.eq(log.length, 1, 'chainProcess called'); + t.eq(log.length, 2, 'chainProcess called once for each process'); t.eq(log[0], 1, 'executeCallback queued to wait for 1 chained process'); - t.eq(done, true, 'execute successfully completed'); + t.eq(log[1], 1, 'executeCallback queued to wait for 1 chained process'); + t.eq(done1, true, 'execute for buffer1 process successfully completed'); + t.eq(done2, true, 'execute for buffer2 process successfully completed'); OpenLayers.Request.GET = originalGET; OpenLayers.Request.POST = originalPOST; From 88572303fb2e510c0071ef7333fc5494401013d4 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Tue, 14 Aug 2012 13:31:41 +0200 Subject: [PATCH 13/14] Addressing @tschaub's review comment. --- examples/wps-client.js | 8 ++++---- tests/WPSProcess.html | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/wps-client.js b/examples/wps-client.js index 304c59271a..511d491ea0 100644 --- a/examples/wps-client.js +++ b/examples/wps-client.js @@ -11,12 +11,12 @@ function init() { layers: [new OpenLayers.Layer.Vector()] }); - var features = [new OpenLayers.Format.WKT().read( + var features = [new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT( 'LINESTRING(117 22,112 18,118 13, 115 8)' - )]; - var geometry = (new OpenLayers.Format.WKT().read( + ))]; + var geometry = OpenLayers.Geometry.fromWKT( 'POLYGON((110 20,120 20,120 10,110 10,110 20),(112 17,118 18,118 16,112 15,112 17))' - )).geometry; + ); map.baseLayer.addFeatures(features); map.baseLayer.addFeatures([new OpenLayers.Feature.Vector(geometry)]); diff --git a/tests/WPSProcess.html b/tests/WPSProcess.html index 856b3c8aa6..f668ca3d79 100644 --- a/tests/WPSProcess.html +++ b/tests/WPSProcess.html @@ -3,6 +3,7 @@