Project

General

Profile

Download (7.66 KB) Statistics
| Branch: | Tag: | Revision:
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
/**
7
 * @requires OpenLayers/SingleFile.js
8
 */
9

    
10
/**
11
 * @requires OpenLayers/Events.js
12
 * @requires OpenLayers/WPSProcess.js
13
 * @requires OpenLayers/Format/WPSDescribeProcess.js
14
 * @requires OpenLayers/Request.js
15
 */
16

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