Project

General

Profile

Download (7.26 KB) Statistics
| Branch: | Tag: | Revision:
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
/**
7
 * @requires OpenLayers/Format/GML/Base.js
8
 */
9

    
10
/**
11
 * Class: OpenLayers.Format.GML.v2
12
 * Parses GML version 2.
13
 *
14
 * Inherits from:
15
 *  - <OpenLayers.Format.GML.Base>
16
 */
17
OpenLayers.Format.GML.v2 = OpenLayers.Class(OpenLayers.Format.GML.Base, {
18
    
19
    /**
20
     * Property: schemaLocation
21
     * {String} Schema location for a particular minor version.
22
     */
23
    schemaLocation: "http://www.opengis.net/gml http://schemas.opengis.net/gml/2.1.2/feature.xsd",
24

    
25
    /**
26
     * Constructor: OpenLayers.Format.GML.v2
27
     * Create a parser for GML v2.
28
     *
29
     * Parameters:
30
     * options - {Object} An optional object whose properties will be set on
31
     *     this instance.
32
     *
33
     * Valid options properties:
34
     * featureType - {String} Local (without prefix) feature typeName (required).
35
     * featureNS - {String} Feature namespace (required).
36
     * geometryName - {String} Geometry element name.
37
     */
38
    initialize: function(options) {
39
        OpenLayers.Format.GML.Base.prototype.initialize.apply(this, [options]);
40
    },
41

    
42
    /**
43
     * Property: readers
44
     * Contains public functions, grouped by namespace prefix, that will
45
     *     be applied when a namespaced node is found matching the function
46
     *     name.  The function will be applied in the scope of this parser
47
     *     with two arguments: the node being read and a context object passed
48
     *     from the parent.
49
     */
50
    readers: {
51
        "gml": OpenLayers.Util.applyDefaults({
52
            "outerBoundaryIs": function(node, container) {
53
                var obj = {};
54
                this.readChildNodes(node, obj);
55
                container.outer = obj.components[0];
56
            },
57
            "innerBoundaryIs": function(node, container) {
58
                var obj = {};
59
                this.readChildNodes(node, obj);
60
                container.inner.push(obj.components[0]);
61
            },
62
            "Box": function(node, container) {
63
                var obj = {};
64
                this.readChildNodes(node, obj);
65
                if(!container.components) {
66
                    container.components = [];
67
                }
68
                var min = obj.points[0];
69
                var max = obj.points[1];
70
                container.components.push(
71
                    new OpenLayers.Bounds(min.x, min.y, max.x, max.y)
72
                );
73
            }
74
        }, OpenLayers.Format.GML.Base.prototype.readers["gml"]),
75
        "feature": OpenLayers.Format.GML.Base.prototype.readers["feature"],
76
        "wfs": OpenLayers.Format.GML.Base.prototype.readers["wfs"]
77
    },
78

    
79
    /**
80
     * Method: write
81
     *
82
     * Parameters:
83
     * features - {Array(<OpenLayers.Feature.Vector>) | OpenLayers.Feature.Vector}
84
     *     An array of features or a single feature.
85
     *
86
     * Returns:
87
     * {String} Given an array of features, a doc with a gml:featureMembers
88
     *     element will be returned.  Given a single feature, a doc with a
89
     *     gml:featureMember element will be returned.
90
     */
91
    write: function(features) {
92
        var name;
93
        if(OpenLayers.Util.isArray(features)) {
94
            // GML2 only has abstract feature collections
95
            // wfs provides a feature collection from a well-known schema
96
            name = "wfs:FeatureCollection";
97
        } else {
98
            name = "gml:featureMember";
99
        }
100
        var root = this.writeNode(name, features);
101
        this.setAttributeNS(
102
            root, this.namespaces["xsi"],
103
            "xsi:schemaLocation", this.schemaLocation
104
        );
105

    
106
        return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
107
    },
108

    
109
    /**
110
     * Property: writers
111
     * As a compliment to the readers property, this structure contains public
112
     *     writing functions grouped by namespace alias and named like the
113
     *     node names they produce.
114
     */
115
    writers: {
116
        "gml": OpenLayers.Util.applyDefaults({
117
            "Point": function(geometry) {
118
                var node = this.createElementNSPlus("gml:Point");
119
                this.writeNode("coordinates", [geometry], node);
120
                return node;
121
            },
122
            "coordinates": function(points) {
123
                var numPoints = points.length;
124
                var parts = new Array(numPoints);
125
                var point;
126
                for(var i=0; i<numPoints; ++i) {
127
                    point = points[i];
128
                    if(this.xy) {
129
                        parts[i] = point.x + "," + point.y;
130
                    } else {
131
                        parts[i] = point.y + "," + point.x;
132
                    }
133
                    if(point.z != undefined) { // allow null or undefined
134
                        parts[i] += "," + point.z;
135
                    }
136
                }
137
                return this.createElementNSPlus("gml:coordinates", {
138
                    attributes: {
139
                        decimal: ".", cs: ",", ts: " "
140
                    },
141
                    value: (numPoints == 1) ? parts[0] : parts.join(" ")
142
                });
143
            },
144
            "LineString": function(geometry) {
145
                var node = this.createElementNSPlus("gml:LineString");
146
                this.writeNode("coordinates", geometry.components, node);
147
                return node;
148
            },
149
            "Polygon": function(geometry) {
150
                var node = this.createElementNSPlus("gml:Polygon");
151
                this.writeNode("outerBoundaryIs", geometry.components[0], node);
152
                for(var i=1; i<geometry.components.length; ++i) {
153
                    this.writeNode(
154
                        "innerBoundaryIs", geometry.components[i], node
155
                    );
156
                }
157
                return node;
158
            },
159
            "outerBoundaryIs": function(ring) {
160
                var node = this.createElementNSPlus("gml:outerBoundaryIs");
161
                this.writeNode("LinearRing", ring, node);
162
                return node;
163
            },
164
            "innerBoundaryIs": function(ring) {
165
                var node = this.createElementNSPlus("gml:innerBoundaryIs");
166
                this.writeNode("LinearRing", ring, node);
167
                return node;
168
            },
169
            "LinearRing": function(ring) {
170
                var node = this.createElementNSPlus("gml:LinearRing");
171
                this.writeNode("coordinates", ring.components, node);
172
                return node;
173
            },
174
            "Box": function(bounds) {
175
                var node = this.createElementNSPlus("gml:Box");
176
                this.writeNode("coordinates", [
177
                    {x: bounds.left, y: bounds.bottom},
178
                    {x: bounds.right, y: bounds.top}
179
                ], node);
180
                // srsName attribute is optional for gml:Box
181
                if(this.srsName) {
182
                    node.setAttribute("srsName", this.srsName);
183
                }
184
                return node;
185
            }
186
        }, OpenLayers.Format.GML.Base.prototype.writers["gml"]),
187
        "feature": OpenLayers.Format.GML.Base.prototype.writers["feature"],
188
        "wfs": OpenLayers.Format.GML.Base.prototype.writers["wfs"]
189
    },
190
    
191
    CLASS_NAME: "OpenLayers.Format.GML.v2" 
192

    
193
});
(2-2/3)