v1_0_0.js 4.1 KB

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