SOSGetFeatureOfInterest.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/Format/XML.js
  7. * @requires OpenLayers/Format/GML/v3.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.SOSGetFeatureOfInterest
  11. * Read and write SOS GetFeatureOfInterest. This is used to get to
  12. * the location of the features (stations). The stations can have 1 or more
  13. * sensors.
  14. *
  15. * Inherits from:
  16. * - <OpenLayers.Format.XML>
  17. */
  18. OpenLayers.Format.SOSGetFeatureOfInterest = OpenLayers.Class(
  19. OpenLayers.Format.XML, {
  20. /**
  21. * Constant: VERSION
  22. * {String} 1.0.0
  23. */
  24. VERSION: "1.0.0",
  25. /**
  26. * Property: namespaces
  27. * {Object} Mapping of namespace aliases to namespace URIs.
  28. */
  29. namespaces: {
  30. sos: "http://www.opengis.net/sos/1.0",
  31. gml: "http://www.opengis.net/gml",
  32. sa: "http://www.opengis.net/sampling/1.0",
  33. xsi: "http://www.w3.org/2001/XMLSchema-instance"
  34. },
  35. /**
  36. * Property: schemaLocation
  37. * {String} Schema location
  38. */
  39. schemaLocation: "http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosAll.xsd",
  40. /**
  41. * Property: defaultPrefix
  42. */
  43. defaultPrefix: "sos",
  44. /**
  45. * Property: regExes
  46. * Compiled regular expressions for manipulating strings.
  47. */
  48. regExes: {
  49. trimSpace: (/^\s*|\s*$/g),
  50. removeSpace: (/\s*/g),
  51. splitSpace: (/\s+/),
  52. trimComma: (/\s*,\s*/g)
  53. },
  54. /**
  55. * Constructor: OpenLayers.Format.SOSGetFeatureOfInterest
  56. *
  57. * Parameters:
  58. * options - {Object} An optional object whose properties will be set on
  59. * this instance.
  60. */
  61. /**
  62. * APIMethod: read
  63. * Parse a GetFeatureOfInterest response and return an array of features
  64. *
  65. * Parameters:
  66. * data - {String} or {DOMElement} data to read/parse.
  67. *
  68. * Returns:
  69. * {Array(<OpenLayers.Feature.Vector>)} An array of features.
  70. */
  71. read: function(data) {
  72. if(typeof data == "string") {
  73. data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
  74. }
  75. if(data && data.nodeType == 9) {
  76. data = data.documentElement;
  77. }
  78. var info = {features: []};
  79. this.readNode(data, info);
  80. var features = [];
  81. for (var i=0, len=info.features.length; i<len; i++) {
  82. var container = info.features[i];
  83. // reproject features if needed
  84. if(this.internalProjection && this.externalProjection &&
  85. container.components[0]) {
  86. container.components[0].transform(
  87. this.externalProjection, this.internalProjection
  88. );
  89. }
  90. var feature = new OpenLayers.Feature.Vector(
  91. container.components[0], container.attributes);
  92. features.push(feature);
  93. }
  94. return features;
  95. },
  96. /**
  97. * Property: readers
  98. * Contains public functions, grouped by namespace prefix, that will
  99. * be applied when a namespaced node is found matching the function
  100. * name. The function will be applied in the scope of this parser
  101. * with two arguments: the node being read and a context object passed
  102. * from the parent.
  103. */
  104. readers: {
  105. "sa": {
  106. "SamplingPoint": function(node, obj) {
  107. // sampling point can also be without a featureMember if
  108. // there is only 1
  109. if (!obj.attributes) {
  110. var feature = {attributes: {}};
  111. obj.features.push(feature);
  112. obj = feature;
  113. }
  114. obj.attributes.id = this.getAttributeNS(node,
  115. this.namespaces.gml, "id");
  116. this.readChildNodes(node, obj);
  117. },
  118. "position": function (node, obj) {
  119. this.readChildNodes(node, obj);
  120. }
  121. },
  122. "gml": OpenLayers.Util.applyDefaults({
  123. "FeatureCollection": function(node, obj) {
  124. this.readChildNodes(node, obj);
  125. },
  126. "featureMember": function(node, obj) {
  127. var feature = {attributes: {}};
  128. obj.features.push(feature);
  129. this.readChildNodes(node, feature);
  130. },
  131. "name": function(node, obj) {
  132. obj.attributes.name = this.getChildValue(node);
  133. },
  134. "pos": function(node, obj) {
  135. // we need to parse the srsName to get to the
  136. // externalProjection, that's why we cannot use
  137. // GML v3 for this
  138. if (!this.externalProjection) {
  139. this.externalProjection = new OpenLayers.Projection(
  140. node.getAttribute("srsName"));
  141. }
  142. OpenLayers.Format.GML.v3.prototype.readers.gml.pos.apply(
  143. this, [node, obj]);
  144. }
  145. }, OpenLayers.Format.GML.v3.prototype.readers.gml)
  146. },
  147. /**
  148. * Property: writers
  149. * As a compliment to the readers property, this structure contains public
  150. * writing functions grouped by namespace alias and named like the
  151. * node names they produce.
  152. */
  153. writers: {
  154. "sos": {
  155. "GetFeatureOfInterest": function(options) {
  156. var node = this.createElementNSPlus("GetFeatureOfInterest", {
  157. attributes: {
  158. version: this.VERSION,
  159. service: 'SOS',
  160. "xsi:schemaLocation": this.schemaLocation
  161. }
  162. });
  163. for (var i=0, len=options.fois.length; i<len; i++) {
  164. this.writeNode("FeatureOfInterestId", {foi: options.fois[i]}, node);
  165. }
  166. return node;
  167. },
  168. "FeatureOfInterestId": function(options) {
  169. var node = this.createElementNSPlus("FeatureOfInterestId", {value: options.foi});
  170. return node;
  171. }
  172. }
  173. },
  174. CLASS_NAME: "OpenLayers.Format.SOSGetFeatureOfInterest"
  175. });