1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Format/GML/Base.js
10 * Class: OpenLayers.Format.GML.v2
11 * Parses GML version 2.
14 * - <OpenLayers.Format.GML.Base>
16 OpenLayers.Format.GML.v2 = OpenLayers.Class(OpenLayers.Format.GML.Base, {
19 * Property: schemaLocation
20 * {String} Schema location for a particular minor version.
22 schemaLocation: "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd",
25 * Constructor: OpenLayers.Format.GML.v2
26 * Create a parser for GML v2.
29 * options - {Object} An optional object whose properties will be set on
32 * Valid options properties:
33 * featureType - {String} Local (without prefix) feature typeName (required).
34 * featureNS - {String} Feature namespace (required).
35 * geometryName - {String} Geometry element name.
37 initialize: function(options) {
38 OpenLayers.Format.GML.Base.prototype.initialize.apply(this, [options]);
43 * Contains public functions, grouped by namespace prefix, that will
44 * be applied when a namespaced node is found matching the function
45 * name. The function will be applied in the scope of this parser
46 * with two arguments: the node being read and a context object passed
50 "gml": OpenLayers.Util.applyDefaults({
51 "outerBoundaryIs": function(node, container) {
53 this.readChildNodes(node, obj);
54 container.outer = obj.components[0];
56 "innerBoundaryIs": function(node, container) {
58 this.readChildNodes(node, obj);
59 container.inner.push(obj.components[0]);
61 "Box": function(node, container) {
63 this.readChildNodes(node, obj);
64 if(!container.components) {
65 container.components = [];
67 var min = obj.points[0];
68 var max = obj.points[1];
69 container.components.push(
70 new OpenLayers.Bounds(min.x, min.y, max.x, max.y)
73 }, OpenLayers.Format.GML.Base.prototype.readers["gml"]),
74 "feature": OpenLayers.Format.GML.Base.prototype.readers["feature"],
75 "wfs": OpenLayers.Format.GML.Base.prototype.readers["wfs"]
82 * features - {Array(<OpenLayers.Feature.Vector>) | OpenLayers.Feature.Vector}
83 * An array of features or a single feature.
86 * {String} Given an array of features, a doc with a gml:featureMembers
87 * element will be returned. Given a single feature, a doc with a
88 * gml:featureMember element will be returned.
90 write: function(features) {
92 if(features instanceof Array) {
93 // GML2 only has abstract feature collections
94 // wfs provides a feature collection from a well-known schema
95 name = "wfs:FeatureCollection";
97 name = "gml:featureMember";
99 var root = this.writeNode(name, features);
101 root, this.namespaces["xsi"],
102 "xsi:schemaLocation", this.schemaLocation
105 return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
110 * As a compliment to the readers property, this structure contains public
111 * writing functions grouped by namespace alias and named like the
112 * node names they produce.
115 "gml": OpenLayers.Util.applyDefaults({
116 "Point": function(geometry) {
117 var node = this.createElementNSPlus("gml:Point");
118 this.writeNode("coordinates", [geometry], node);
121 "coordinates": function(points) {
122 var numPoints = points.length;
123 var parts = new Array(numPoints);
125 for(var i=0; i<numPoints; ++i) {
128 parts[i] = point.x + "," + point.y;
130 parts[i] = point.y + "," + point.x;
132 if(point.z != undefined) { // allow null or undefined
133 parts[i] += "," + point.z;
136 return this.createElementNSPlus("gml:coordinates", {
138 decimal: ".", cs: ",", ts: " "
140 value: (numPoints == 1) ? parts[0] : parts.join(" ")
143 "LineString": function(geometry) {
144 var node = this.createElementNSPlus("gml:LineString");
145 this.writeNode("coordinates", geometry.components, node);
148 "Polygon": function(geometry) {
149 var node = this.createElementNSPlus("gml:Polygon");
150 this.writeNode("outerBoundaryIs", geometry.components[0], node);
151 for(var i=1; i<geometry.components.length; ++i) {
153 "innerBoundaryIs", geometry.components[i], node
158 "outerBoundaryIs": function(ring) {
159 var node = this.createElementNSPlus("gml:outerBoundaryIs");
160 this.writeNode("LinearRing", ring, node);
163 "innerBoundaryIs": function(ring) {
164 var node = this.createElementNSPlus("gml:innerBoundaryIs");
165 this.writeNode("LinearRing", ring, node);
168 "LinearRing": function(ring) {
169 var node = this.createElementNSPlus("gml:LinearRing");
170 this.writeNode("coordinates", ring.components, node);
173 "Box": function(bounds) {
174 var node = this.createElementNSPlus("gml:Box");
175 this.writeNode("coordinates", [
176 {x: bounds.left, y: bounds.bottom},
177 {x: bounds.right, y: bounds.top}
179 // srsName attribute is optional for gml:Box
181 node.setAttribute("srsName", this.srsName);
185 }, OpenLayers.Format.GML.Base.prototype.writers["gml"]),
186 "feature": OpenLayers.Format.GML.Base.prototype.writers["feature"],
187 "wfs": OpenLayers.Format.GML.Base.prototype.writers["wfs"]
190 CLASS_NAME: "OpenLayers.Format.GML.v2"