]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/GML/v2.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / GML / v2.js
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. */
4
5 /**
6  * @requires OpenLayers/Format/GML/Base.js
7  */
8
9 /**
10  * Class: OpenLayers.Format.GML.v2
11  * Parses GML version 2.
12  *
13  * Inherits from:
14  *  - <OpenLayers.Format.GML.Base>
15  */
16 OpenLayers.Format.GML.v2 = OpenLayers.Class(OpenLayers.Format.GML.Base, {
17     
18     /**
19      * Property: schemaLocation
20      * {String} Schema location for a particular minor version.
21      */
22     schemaLocation: "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd",
23
24     /**
25      * Constructor: OpenLayers.Format.GML.v2
26      * Create a parser for GML v2.
27      *
28      * Parameters:
29      * options - {Object} An optional object whose properties will be set on
30      *     this instance.
31      *
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.
36      */
37     initialize: function(options) {
38         OpenLayers.Format.GML.Base.prototype.initialize.apply(this, [options]);
39     },
40
41     /**
42      * Property: readers
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
47      *     from the parent.
48      */
49     readers: {
50         "gml": OpenLayers.Util.applyDefaults({
51             "outerBoundaryIs": function(node, container) {
52                 var obj = {};
53                 this.readChildNodes(node, obj);
54                 container.outer = obj.components[0];
55             },
56             "innerBoundaryIs": function(node, container) {
57                 var obj = {};
58                 this.readChildNodes(node, obj);
59                 container.inner.push(obj.components[0]);
60             },
61             "Box": function(node, container) {
62                 var obj = {};
63                 this.readChildNodes(node, obj);
64                 if(!container.components) {
65                     container.components = [];
66                 }
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)
71                 );
72             }
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"]
76     },
77
78     /**
79      * Method: write
80      *
81      * Parameters:
82      * features - {Array(<OpenLayers.Feature.Vector>) | OpenLayers.Feature.Vector}
83      *     An array of features or a single feature.
84      *
85      * Returns:
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.
89      */
90     write: function(features) {
91         var name;
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";
96         } else {
97             name = "gml:featureMember";
98         }
99         var root = this.writeNode(name, features);
100         this.setAttributeNS(
101             root, this.namespaces["xsi"],
102             "xsi:schemaLocation", this.schemaLocation
103         );
104
105         return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
106     },
107
108     /**
109      * Property: writers
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.
113      */
114     writers: {
115         "gml": OpenLayers.Util.applyDefaults({
116             "Point": function(geometry) {
117                 var node = this.createElementNSPlus("gml:Point");
118                 this.writeNode("coordinates", [geometry], node);
119                 return node;
120             },
121             "coordinates": function(points) {
122                 var numPoints = points.length;
123                 var parts = new Array(numPoints);
124                 var point;
125                 for(var i=0; i<numPoints; ++i) {
126                     point = points[i];
127                     if(this.xy) {
128                         parts[i] = point.x + "," + point.y;
129                     } else {
130                         parts[i] = point.y + "," + point.x;
131                     }
132                     if(point.z != undefined) { // allow null or undefined
133                         parts[i] += "," + point.z;
134                     }
135                 }
136                 return this.createElementNSPlus("gml:coordinates", {
137                     attributes: {
138                         decimal: ".", cs: ",", ts: " "
139                     },
140                     value: (numPoints == 1) ? parts[0] : parts.join(" ")
141                 });
142             },
143             "LineString": function(geometry) {
144                 var node = this.createElementNSPlus("gml:LineString");
145                 this.writeNode("coordinates", geometry.components, node);
146                 return node;
147             },
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) {
152                     this.writeNode(
153                         "innerBoundaryIs", geometry.components[i], node
154                     );
155                 }
156                 return node;
157             },
158             "outerBoundaryIs": function(ring) {
159                 var node = this.createElementNSPlus("gml:outerBoundaryIs");
160                 this.writeNode("LinearRing", ring, node);
161                 return node;
162             },
163             "innerBoundaryIs": function(ring) {
164                 var node = this.createElementNSPlus("gml:innerBoundaryIs");
165                 this.writeNode("LinearRing", ring, node);
166                 return node;
167             },
168             "LinearRing": function(ring) {
169                 var node = this.createElementNSPlus("gml:LinearRing");
170                 this.writeNode("coordinates", ring.components, node);
171                 return node;
172             },
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}
178                 ], node);
179                 // srsName attribute is optional for gml:Box
180                 if(this.srsName) {
181                     node.setAttribute("srsName", this.srsName);
182                 }
183                 return node;
184             }
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"]
188     },
189     
190     CLASS_NAME: "OpenLayers.Format.GML.v2" 
191
192 });