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/XML.js
7 * @requires OpenLayers/Format/WFST.js
11 * Class: OpenLayers.Format.WFST.v1
12 * Superclass for WFST parsers.
15 * - <OpenLayers.Format.XML>
17 OpenLayers.Format.WFST.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
20 * Property: namespaces
21 * {Object} Mapping of namespace aliases to namespace URIs.
24 xlink: "http://www.w3.org/1999/xlink",
25 xsi: "http://www.w3.org/2001/XMLSchema-instance",
26 wfs: "http://www.opengis.net/wfs",
27 gml: "http://www.opengis.net/gml",
28 ogc: "http://www.opengis.net/ogc"
32 * Property: defaultPrefix
38 * {String} WFS version number.
43 * Property: schemaLocation
44 * {String} Schema location for a particular minor version.
46 schemaLocations: null,
49 * APIProperty: srsName
50 * {String} URI for spatial reference system.
55 * APIProperty: extractAttributes
56 * {Boolean} Extract attributes from GML. Default is true.
58 extractAttributes: true,
62 * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
63 * Changing is not recommended, a new Format should be instantiated.
69 * {Object} Maps feature states to node names.
74 * Constructor: OpenLayers.Format.WFST.v1
75 * Instances of this class are not created directly. Use the
76 * <OpenLayers.Format.WFST.v1_0_0> or <OpenLayers.Format.WFST.v1_1_0>
77 * constructor instead.
80 * options - {Object} An optional object whose properties will be set on
83 initialize: function(options) {
84 // set state name mapping
86 this.stateName[OpenLayers.State.INSERT] = "wfs:Insert";
87 this.stateName[OpenLayers.State.UPDATE] = "wfs:Update";
88 this.stateName[OpenLayers.State.DELETE] = "wfs:Delete";
89 OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
95 getSrsName: function(feature, options) {
96 var srsName = options && options.srsName;
98 if(feature && feature.layer) {
99 srsName = feature.layer.projection.getCode();
101 srsName = this.srsName;
109 * Parse the response from a transaction. Because WFS is split into
110 * Transaction requests (create, update, and delete) and GetFeature
111 * requests (read), this method handles parsing of both types of
114 read: function(data) {
115 if(typeof data == "string") {
116 data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
118 if(data && data.nodeType == 9) {
119 data = data.documentElement;
122 this.readNode(data, obj);
131 * Contains public functions, grouped by namespace prefix, that will
132 * be applied when a namespaced node is found matching the function
133 * name. The function will be applied in the scope of this parser
134 * with two arguments: the node being read and a context object passed
139 "FeatureCollection": function(node, obj) {
141 this.readChildNodes(node, obj);
148 * Given an array of features, write a WFS transaction. This assumes
149 * the features have a state property that determines the operation
150 * type - insert, update, or delete.
153 * features - {Array(<OpenLayers.Feature.Vector>)} A list of features.
156 * {String} A serialized WFS transaction.
158 write: function(features) {
159 var node = this.writeNode("wfs:Transaction", features);
160 var value = this.schemaLocationAttr();
163 node, this.namespaces["xsi"], "xsi:schemaLocation", value
166 return OpenLayers.Format.XML.prototype.write.apply(this, [node]);
171 * As a compliment to the readers property, this structure contains public
172 * writing functions grouped by namespace alias and named like the
173 * node names they produce.
177 "GetFeature": function(options) {
178 var node = this.createElementNSPlus("wfs:GetFeature", {
181 version: this.version,
182 maxFeatures: options && options.maxFeatures,
183 "xsi:schemaLocation": this.schemaLocationAttr(options)
186 this.writeNode("Query", options, node);
189 "Query": function(options) {
190 options = OpenLayers.Util.extend({
191 featureNS: this.featureNS,
192 featurePrefix: this.featurePrefix,
193 featureType: this.featureType,
194 srsName: this.srsName
196 // TODO: this is still version specific and should be separated out
197 // v1.0.0 does not allow srsName on wfs:Query
198 var node = this.createElementNSPlus("wfs:Query", {
200 typeName: (options.featureNS ? options.featurePrefix + ":" : "") +
202 srsName: options.srsName
205 if(options.featureNS) {
206 node.setAttribute("xmlns:" + options.featurePrefix, options.featureNS);
209 this.setFilterProperty(options.filter);
210 this.writeNode("ogc:Filter", options.filter, node);
214 "Transaction": function(features) {
215 var node = this.createElementNSPlus("wfs:Transaction", {
218 version: this.version
223 for(var i=0, len=features.length; i<len; ++i) {
224 feature = features[i];
225 name = this.stateName[feature.state];
227 this.writeNode(name, feature, node);
233 "Insert": function(feature) {
234 var node = this.createElementNSPlus("wfs:Insert");
235 this.srsName = this.getSrsName(feature);
236 this.writeNode("feature:_typeName", feature, node);
239 "Update": function(feature) {
240 var node = this.createElementNSPlus("wfs:Update", {
242 typeName: (this.featureNS ? this.featurePrefix + ":" : "") +
247 node.setAttribute("xmlns:" + this.featurePrefix, this.featureNS);
252 "Property", {name: this.geometryName, value: feature}, node
256 for(var key in feature.attributes) {
258 "Property", {name: key, value: feature.attributes[key]}, node
262 // add feature id filter
263 this.writeNode("ogc:Filter", new OpenLayers.Filter.FeatureId({
269 "Property": function(obj) {
270 var node = this.createElementNSPlus("wfs:Property");
271 this.writeNode("Name", obj.name, node);
272 this.writeNode("Value", obj.value, node);
275 "Name": function(name) {
276 return this.createElementNSPlus("wfs:Name", {value: name});
278 "Value": function(obj) {
280 if(obj instanceof OpenLayers.Feature.Vector) {
281 node = this.createElementNSPlus("wfs:Value");
282 this.srsName = this.getSrsName(obj);
283 var geom = this.writeNode("feature:_geometry", obj.geometry).firstChild;
284 node.appendChild(geom);
286 node = this.createElementNSPlus("wfs:Value", {value: obj});
290 "Delete": function(feature) {
291 var node = this.createElementNSPlus("wfs:Delete", {
293 typeName: (this.featureNS ? this.featurePrefix + ":" : "") +
298 node.setAttribute("xmlns:" + this.featurePrefix, this.featureNS);
300 this.writeNode("ogc:Filter", new OpenLayers.Filter.FeatureId({
309 * Method: schemaLocationAttr
310 * Generate the xsi:schemaLocation attribute value.
313 * {String} The xsi:schemaLocation attribute or undefined if none.
315 schemaLocationAttr: function(options) {
316 options = OpenLayers.Util.extend({
317 featurePrefix: this.featurePrefix,
320 var schemaLocations = OpenLayers.Util.extend({}, this.schemaLocations);
322 schemaLocations[options.featurePrefix] = options.schema;
326 for(var key in schemaLocations) {
327 uri = this.namespaces[key];
329 parts.push(uri + " " + schemaLocations[key]);
332 var value = parts.join(" ") || undefined;
337 * Method: setFilterProperty
338 * Set the property of each spatial filter.
341 * filter - {<OpenLayers.Filter>}
343 setFilterProperty: function(filter) {
345 for(var i=0, len=filter.filters.length; i<len; ++i) {
346 this.setFilterProperty(filter.filters[i]);
349 if(filter instanceof OpenLayers.Filter.Spatial) {
350 // got a spatial filter, set its property
351 filter.property = this.geometryName;
356 CLASS_NAME: "OpenLayers.Format.WFST.v1"