1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Protocol/SOS.js |
---|
8 | * @requires OpenLayers/Format/SOSGetFeatureOfInterest.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Protocol.SOS.v1_0_0 |
---|
13 | * An SOS v1.0.0 Protocol for vector layers. Create a new instance with the |
---|
14 | * <OpenLayers.Protocol.SOS.v1_0_0> constructor. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Protocol> |
---|
18 | */ |
---|
19 | OpenLayers.Protocol.SOS.v1_0_0 = OpenLayers.Class(OpenLayers.Protocol, { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIProperty: fois |
---|
23 | * {Array(String)} Array of features of interest (foi) |
---|
24 | */ |
---|
25 | fois: null, |
---|
26 | |
---|
27 | /** |
---|
28 | * Property: formatOptions |
---|
29 | * {Object} Optional options for the format. If a format is not provided, |
---|
30 | * this property can be used to extend the default format options. |
---|
31 | */ |
---|
32 | formatOptions: null, |
---|
33 | |
---|
34 | /** |
---|
35 | * Constructor: OpenLayers.Protocol.SOS |
---|
36 | * A class for giving layers an SOS protocol. |
---|
37 | * |
---|
38 | * Parameters: |
---|
39 | * options - {Object} Optional object whose properties will be set on the |
---|
40 | * instance. |
---|
41 | * Valid options properties: |
---|
42 | * url - {String} URL to send requests to (required). |
---|
43 | * fois - {Array} The features of interest (required). |
---|
44 | */ |
---|
45 | initialize: function(options) { |
---|
46 | OpenLayers.Protocol.prototype.initialize.apply(this, [options]); |
---|
47 | if(!options.format) { |
---|
48 | this.format = new OpenLayers.Format.SOSGetFeatureOfInterest( |
---|
49 | this.formatOptions); |
---|
50 | } |
---|
51 | }, |
---|
52 | |
---|
53 | /** |
---|
54 | * APIMethod: destroy |
---|
55 | * Clean up the protocol. |
---|
56 | */ |
---|
57 | destroy: function() { |
---|
58 | if(this.options && !this.options.format) { |
---|
59 | this.format.destroy(); |
---|
60 | } |
---|
61 | this.format = null; |
---|
62 | OpenLayers.Protocol.prototype.destroy.apply(this); |
---|
63 | }, |
---|
64 | |
---|
65 | /** |
---|
66 | * APIMethod: read |
---|
67 | * Construct a request for reading new sensor positions. This is done by |
---|
68 | * issuing one GetFeatureOfInterest request. |
---|
69 | */ |
---|
70 | read: function(options) { |
---|
71 | options = OpenLayers.Util.extend({}, options); |
---|
72 | OpenLayers.Util.applyDefaults(options, this.options || {}); |
---|
73 | var response = new OpenLayers.Protocol.Response({requestType: "read"}); |
---|
74 | var format = this.format; |
---|
75 | var data = OpenLayers.Format.XML.prototype.write.apply(format, |
---|
76 | [format.writeNode("sos:GetFeatureOfInterest", {fois: this.fois})] |
---|
77 | ); |
---|
78 | response.priv = OpenLayers.Request.POST({ |
---|
79 | url: options.url, |
---|
80 | callback: this.createCallback(this.handleRead, response, options), |
---|
81 | data: data |
---|
82 | }); |
---|
83 | return response; |
---|
84 | }, |
---|
85 | |
---|
86 | /** |
---|
87 | * Method: handleRead |
---|
88 | * Deal with response from the read request. |
---|
89 | * |
---|
90 | * Parameters: |
---|
91 | * response - {<OpenLayers.Protocol.Response>} The response object to pass |
---|
92 | * to the user callback. |
---|
93 | * options - {Object} The user options passed to the read call. |
---|
94 | */ |
---|
95 | handleRead: function(response, options) { |
---|
96 | if(options.callback) { |
---|
97 | var request = response.priv; |
---|
98 | if(request.status >= 200 && request.status < 300) { |
---|
99 | // success |
---|
100 | response.features = this.parseFeatures(request); |
---|
101 | response.code = OpenLayers.Protocol.Response.SUCCESS; |
---|
102 | } else { |
---|
103 | // failure |
---|
104 | response.code = OpenLayers.Protocol.Response.FAILURE; |
---|
105 | } |
---|
106 | options.callback.call(options.scope, response); |
---|
107 | } |
---|
108 | }, |
---|
109 | |
---|
110 | /** |
---|
111 | * Method: parseFeatures |
---|
112 | * Read HTTP response body and return features |
---|
113 | * |
---|
114 | * Parameters: |
---|
115 | * request - {XMLHttpRequest} The request object |
---|
116 | * |
---|
117 | * Returns: |
---|
118 | * {Array({<OpenLayers.Feature.Vector>})} Array of features |
---|
119 | */ |
---|
120 | parseFeatures: function(request) { |
---|
121 | var doc = request.responseXML; |
---|
122 | if(!doc || !doc.documentElement) { |
---|
123 | doc = request.responseText; |
---|
124 | } |
---|
125 | if(!doc || doc.length <= 0) { |
---|
126 | return null; |
---|
127 | } |
---|
128 | return this.format.read(doc); |
---|
129 | }, |
---|
130 | |
---|
131 | CLASS_NAME: "OpenLayers.Protocol.SOS.v1_0_0" |
---|
132 | }); |
---|