v2.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/GML/Base.js
  7. */
  8. /**
  9. * Class: OpenLayers.Format.GML.v2
  10. * Parses GML version 2.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Format.GML.Base>
  14. */
  15. OpenLayers.Format.GML.v2 = OpenLayers.Class(OpenLayers.Format.GML.Base, {
  16. /**
  17. * Property: schemaLocation
  18. * {String} Schema location for a particular minor version.
  19. */
  20. schemaLocation: "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd",
  21. /**
  22. * Constructor: OpenLayers.Format.GML.v2
  23. * Create a parser for GML v2.
  24. *
  25. * Parameters:
  26. * options - {Object} An optional object whose properties will be set on
  27. * this instance.
  28. *
  29. * Valid options properties:
  30. * featureType - {String} Local (without prefix) feature typeName (required).
  31. * featureNS - {String} Feature namespace (required).
  32. * geometryName - {String} Geometry element name.
  33. */
  34. initialize: function(options) {
  35. OpenLayers.Format.GML.Base.prototype.initialize.apply(this, [options]);
  36. },
  37. /**
  38. * Property: readers
  39. * Contains public functions, grouped by namespace prefix, that will
  40. * be applied when a namespaced node is found matching the function
  41. * name. The function will be applied in the scope of this parser
  42. * with two arguments: the node being read and a context object passed
  43. * from the parent.
  44. */
  45. readers: {
  46. "gml": OpenLayers.Util.applyDefaults({
  47. "outerBoundaryIs": function(node, container) {
  48. var obj = {};
  49. this.readChildNodes(node, obj);
  50. container.outer = obj.components[0];
  51. },
  52. "innerBoundaryIs": function(node, container) {
  53. var obj = {};
  54. this.readChildNodes(node, obj);
  55. container.inner.push(obj.components[0]);
  56. },
  57. "Box": function(node, container) {
  58. var obj = {};
  59. this.readChildNodes(node, obj);
  60. if(!container.components) {
  61. container.components = [];
  62. }
  63. var min = obj.points[0];
  64. var max = obj.points[1];
  65. container.components.push(
  66. new OpenLayers.Bounds(min.x, min.y, max.x, max.y)
  67. );
  68. }
  69. }, OpenLayers.Format.GML.Base.prototype.readers["gml"]),
  70. "feature": OpenLayers.Format.GML.Base.prototype.readers["feature"],
  71. "wfs": OpenLayers.Format.GML.Base.prototype.readers["wfs"]
  72. },
  73. /**
  74. * Method: write
  75. *
  76. * Parameters:
  77. * features - {Array(<OpenLayers.Feature.Vector>) | OpenLayers.Feature.Vector}
  78. * An array of features or a single feature.
  79. *
  80. * Returns:
  81. * {String} Given an array of features, a doc with a gml:featureMembers
  82. * element will be returned. Given a single feature, a doc with a
  83. * gml:featureMember element will be returned.
  84. */
  85. write: function(features) {
  86. var name;
  87. if(OpenLayers.Util.isArray(features)) {
  88. // GML2 only has abstract feature collections
  89. // wfs provides a feature collection from a well-known schema
  90. name = "wfs:FeatureCollection";
  91. } else {
  92. name = "gml:featureMember";
  93. }
  94. var root = this.writeNode(name, features);
  95. this.setAttributeNS(
  96. root, this.namespaces["xsi"],
  97. "xsi:schemaLocation", this.schemaLocation
  98. );
  99. return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
  100. },
  101. /**
  102. * Property: writers
  103. * As a compliment to the readers property, this structure contains public
  104. * writing functions grouped by namespace alias and named like the
  105. * node names they produce.
  106. */
  107. writers: {
  108. "gml": OpenLayers.Util.applyDefaults({
  109. "Point": function(geometry) {
  110. var node = this.createElementNSPlus("gml:Point");
  111. this.writeNode("coordinates", [geometry], node);
  112. return node;
  113. },
  114. "coordinates": function(points) {
  115. var numPoints = points.length;
  116. var parts = new Array(numPoints);
  117. var point;
  118. for(var i=0; i<numPoints; ++i) {
  119. point = points[i];
  120. if(this.xy) {
  121. parts[i] = point.x + "," + point.y;
  122. } else {
  123. parts[i] = point.y + "," + point.x;
  124. }
  125. if(point.z != undefined) { // allow null or undefined
  126. parts[i] += "," + point.z;
  127. }
  128. }
  129. return this.createElementNSPlus("gml:coordinates", {
  130. attributes: {
  131. decimal: ".", cs: ",", ts: " "
  132. },
  133. value: (numPoints == 1) ? parts[0] : parts.join(" ")
  134. });
  135. },
  136. "LineString": function(geometry) {
  137. var node = this.createElementNSPlus("gml:LineString");
  138. this.writeNode("coordinates", geometry.components, node);
  139. return node;
  140. },
  141. "Polygon": function(geometry) {
  142. var node = this.createElementNSPlus("gml:Polygon");
  143. this.writeNode("outerBoundaryIs", geometry.components[0], node);
  144. for(var i=1; i<geometry.components.length; ++i) {
  145. this.writeNode(
  146. "innerBoundaryIs", geometry.components[i], node
  147. );
  148. }
  149. return node;
  150. },
  151. "outerBoundaryIs": function(ring) {
  152. var node = this.createElementNSPlus("gml:outerBoundaryIs");
  153. this.writeNode("LinearRing", ring, node);
  154. return node;
  155. },
  156. "innerBoundaryIs": function(ring) {
  157. var node = this.createElementNSPlus("gml:innerBoundaryIs");
  158. this.writeNode("LinearRing", ring, node);
  159. return node;
  160. },
  161. "LinearRing": function(ring) {
  162. var node = this.createElementNSPlus("gml:LinearRing");
  163. this.writeNode("coordinates", ring.components, node);
  164. return node;
  165. },
  166. "Box": function(bounds) {
  167. var node = this.createElementNSPlus("gml:Box");
  168. this.writeNode("coordinates", [
  169. {x: bounds.left, y: bounds.bottom},
  170. {x: bounds.right, y: bounds.top}
  171. ], node);
  172. // srsName attribute is optional for gml:Box
  173. if(this.srsName) {
  174. node.setAttribute("srsName", this.srsName);
  175. }
  176. return node;
  177. }
  178. }, OpenLayers.Format.GML.Base.prototype.writers["gml"]),
  179. "feature": OpenLayers.Format.GML.Base.prototype.writers["feature"],
  180. "wfs": OpenLayers.Format.GML.Base.prototype.writers["wfs"]
  181. },
  182. CLASS_NAME: "OpenLayers.Format.GML.v2"
  183. });