SOSGetObservation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/SOSGetFeatureOfInterest.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.SOSGetObservation
  11. * Read and write SOS GetObersation (to get the actual values from a sensor)
  12. * version 1.0.0
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Format.XML>
  16. */
  17. OpenLayers.Format.SOSGetObservation = OpenLayers.Class(OpenLayers.Format.XML, {
  18. /**
  19. * Property: namespaces
  20. * {Object} Mapping of namespace aliases to namespace URIs.
  21. */
  22. namespaces: {
  23. ows: "http://www.opengis.net/ows",
  24. gml: "http://www.opengis.net/gml",
  25. sos: "http://www.opengis.net/sos/1.0",
  26. ogc: "http://www.opengis.net/ogc",
  27. om: "http://www.opengis.net/om/1.0",
  28. sa: "http://www.opengis.net/sampling/1.0",
  29. xlink: "http://www.w3.org/1999/xlink",
  30. xsi: "http://www.w3.org/2001/XMLSchema-instance"
  31. },
  32. /**
  33. * Property: regExes
  34. * Compiled regular expressions for manipulating strings.
  35. */
  36. regExes: {
  37. trimSpace: (/^\s*|\s*$/g),
  38. removeSpace: (/\s*/g),
  39. splitSpace: (/\s+/),
  40. trimComma: (/\s*,\s*/g)
  41. },
  42. /**
  43. * Constant: VERSION
  44. * {String} 1.0.0
  45. */
  46. VERSION: "1.0.0",
  47. /**
  48. * Property: schemaLocation
  49. * {String} Schema location
  50. */
  51. schemaLocation: "http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosGetObservation.xsd",
  52. /**
  53. * Property: defaultPrefix
  54. */
  55. defaultPrefix: "sos",
  56. /**
  57. * Constructor: OpenLayers.Format.SOSGetObservation
  58. *
  59. * Parameters:
  60. * options - {Object} An optional object whose properties will be set on
  61. * this instance.
  62. */
  63. /**
  64. * Method: read
  65. *
  66. * Parameters:
  67. * data - {String} or {DOMElement} data to read/parse.
  68. *
  69. * Returns:
  70. * {Object} An object containing the measurements
  71. */
  72. read: function(data) {
  73. if(typeof data == "string") {
  74. data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
  75. }
  76. if(data && data.nodeType == 9) {
  77. data = data.documentElement;
  78. }
  79. var info = {measurements: [], observations: []};
  80. this.readNode(data, info);
  81. return info;
  82. },
  83. /**
  84. * Method: write
  85. *
  86. * Parameters:
  87. * options - {Object} Optional object.
  88. *
  89. * Returns:
  90. * {String} An SOS GetObservation request XML string.
  91. */
  92. write: function(options) {
  93. var node = this.writeNode("sos:GetObservation", options);
  94. node.setAttribute("xmlns:om", this.namespaces.om);
  95. node.setAttribute("xmlns:ogc", this.namespaces.ogc);
  96. this.setAttributeNS(
  97. node, this.namespaces.xsi,
  98. "xsi:schemaLocation", this.schemaLocation
  99. );
  100. return OpenLayers.Format.XML.prototype.write.apply(this, [node]);
  101. },
  102. /**
  103. * Property: readers
  104. * Contains public functions, grouped by namespace prefix, that will
  105. * be applied when a namespaced node is found matching the function
  106. * name. The function will be applied in the scope of this parser
  107. * with two arguments: the node being read and a context object passed
  108. * from the parent.
  109. */
  110. readers: {
  111. "om": {
  112. "ObservationCollection": function(node, obj) {
  113. obj.id = this.getAttributeNS(node, this.namespaces.gml, "id");
  114. this.readChildNodes(node, obj);
  115. },
  116. "member": function(node, observationCollection) {
  117. this.readChildNodes(node, observationCollection);
  118. },
  119. "Measurement": function(node, observationCollection) {
  120. var measurement = {};
  121. observationCollection.measurements.push(measurement);
  122. this.readChildNodes(node, measurement);
  123. },
  124. "Observation": function(node, observationCollection) {
  125. var observation = {};
  126. observationCollection.observations.push(observation);
  127. this.readChildNodes(node, observation);
  128. },
  129. "samplingTime": function(node, measurement) {
  130. var samplingTime = {};
  131. measurement.samplingTime = samplingTime;
  132. this.readChildNodes(node, samplingTime);
  133. },
  134. "observedProperty": function(node, measurement) {
  135. measurement.observedProperty =
  136. this.getAttributeNS(node, this.namespaces.xlink, "href");
  137. this.readChildNodes(node, measurement);
  138. },
  139. "procedure": function(node, measurement) {
  140. measurement.procedure =
  141. this.getAttributeNS(node, this.namespaces.xlink, "href");
  142. this.readChildNodes(node, measurement);
  143. },
  144. "featureOfInterest": function(node, observation) {
  145. var foi = {features: []};
  146. observation.fois = [];
  147. observation.fois.push(foi);
  148. this.readChildNodes(node, foi);
  149. // postprocessing to get actual features
  150. var features = [];
  151. for (var i=0, len=foi.features.length; i<len; i++) {
  152. var feature = foi.features[i];
  153. features.push(new OpenLayers.Feature.Vector(
  154. feature.components[0], feature.attributes));
  155. }
  156. foi.features = features;
  157. },
  158. "result": function(node, measurement) {
  159. var result = {};
  160. measurement.result = result;
  161. if (this.getChildValue(node) !== '') {
  162. result.value = this.getChildValue(node);
  163. result.uom = node.getAttribute("uom");
  164. } else {
  165. this.readChildNodes(node, result);
  166. }
  167. }
  168. },
  169. "sa": OpenLayers.Format.SOSGetFeatureOfInterest.prototype.readers.sa,
  170. "gml": OpenLayers.Util.applyDefaults({
  171. "TimeInstant": function(node, samplingTime) {
  172. var timeInstant = {};
  173. samplingTime.timeInstant = timeInstant;
  174. this.readChildNodes(node, timeInstant);
  175. },
  176. "timePosition": function(node, timeInstant) {
  177. timeInstant.timePosition = this.getChildValue(node);
  178. }
  179. }, OpenLayers.Format.SOSGetFeatureOfInterest.prototype.readers.gml)
  180. },
  181. /**
  182. * Property: writers
  183. * As a compliment to the readers property, this structure contains public
  184. * writing functions grouped by namespace alias and named like the
  185. * node names they produce.
  186. */
  187. writers: {
  188. "sos": {
  189. "GetObservation": function(options) {
  190. var node = this.createElementNSPlus("GetObservation", {
  191. attributes: {
  192. version: this.VERSION,
  193. service: 'SOS'
  194. }
  195. });
  196. this.writeNode("offering", options, node);
  197. if (options.eventTime) {
  198. this.writeNode("eventTime", options, node);
  199. }
  200. for (var procedure in options.procedures) {
  201. this.writeNode("procedure", options.procedures[procedure], node);
  202. }
  203. for (var observedProperty in options.observedProperties) {
  204. this.writeNode("observedProperty", options.observedProperties[observedProperty], node);
  205. }
  206. if (options.foi) {
  207. this.writeNode("featureOfInterest", options.foi, node);
  208. }
  209. this.writeNode("responseFormat", options, node);
  210. if (options.resultModel) {
  211. this.writeNode("resultModel", options, node);
  212. }
  213. if (options.responseMode) {
  214. this.writeNode("responseMode", options, node);
  215. }
  216. return node;
  217. },
  218. "featureOfInterest": function(foi) {
  219. var node = this.createElementNSPlus("featureOfInterest");
  220. this.writeNode("ObjectID", foi.objectId, node);
  221. return node;
  222. },
  223. "ObjectID": function(options) {
  224. return this.createElementNSPlus("ObjectID",
  225. {value: options});
  226. },
  227. "responseFormat": function(options) {
  228. return this.createElementNSPlus("responseFormat",
  229. {value: options.responseFormat});
  230. },
  231. "procedure": function(procedure) {
  232. return this.createElementNSPlus("procedure",
  233. {value: procedure});
  234. },
  235. "offering": function(options) {
  236. return this.createElementNSPlus("offering", {value:
  237. options.offering});
  238. },
  239. "observedProperty": function(observedProperty) {
  240. return this.createElementNSPlus("observedProperty",
  241. {value: observedProperty});
  242. },
  243. "eventTime": function(options) {
  244. var node = this.createElementNSPlus("eventTime");
  245. if (options.eventTime === 'latest') {
  246. this.writeNode("ogc:TM_Equals", options, node);
  247. }
  248. return node;
  249. },
  250. "resultModel": function(options) {
  251. return this.createElementNSPlus("resultModel", {value:
  252. options.resultModel});
  253. },
  254. "responseMode": function(options) {
  255. return this.createElementNSPlus("responseMode", {value:
  256. options.responseMode});
  257. }
  258. },
  259. "ogc": {
  260. "TM_Equals": function(options) {
  261. var node = this.createElementNSPlus("ogc:TM_Equals");
  262. this.writeNode("ogc:PropertyName", {property:
  263. "urn:ogc:data:time:iso8601"}, node);
  264. if (options.eventTime === 'latest') {
  265. this.writeNode("gml:TimeInstant", {value: 'latest'}, node);
  266. }
  267. return node;
  268. },
  269. "PropertyName": function(options) {
  270. return this.createElementNSPlus("ogc:PropertyName",
  271. {value: options.property});
  272. }
  273. },
  274. "gml": {
  275. "TimeInstant": function(options) {
  276. var node = this.createElementNSPlus("gml:TimeInstant");
  277. this.writeNode("gml:timePosition", options, node);
  278. return node;
  279. },
  280. "timePosition": function(options) {
  281. var node = this.createElementNSPlus("gml:timePosition",
  282. {value: options.value});
  283. return node;
  284. }
  285. }
  286. },
  287. CLASS_NAME: "OpenLayers.Format.SOSGetObservation"
  288. });