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.js
7 * @requires OpenLayers/Console.js
11 * Class: OpenLayers.Format.WFS
15 * - <OpenLayers.Format.GML>
17 OpenLayers.Format.WFS = OpenLayers.Class(OpenLayers.Format.GML, {
27 wfsns: "http://www.opengis.net/wfs",
32 ogcns: "http://www.opengis.net/ogc",
35 * Constructor: OpenLayers.Format.WFS
36 * Create a WFS-T formatter. This requires a layer: that layer should
37 * have two properties: geometry_column and typename. The parser
38 * for this format is subclassed entirely from GML: There is a writer
39 * only, which uses most of the code from the GML layer, and wraps
40 * it in transactional elements.
44 * layer - {<OpenLayers.Layer>}
47 initialize: function(options, layer) {
48 OpenLayers.Format.GML.prototype.initialize.apply(this, [options]);
50 if (this.layer.featureNS) {
51 this.featureNS = this.layer.featureNS;
53 if (this.layer.options.geometry_column) {
54 this.geometryName = this.layer.options.geometry_column;
56 if (this.layer.options.typename) {
57 this.featureName = this.layer.options.typename;
63 * Takes a feature list, and generates a WFS-T Transaction
66 * features - {Array(<OpenLayers.Feature.Vector>)}
68 write: function(features) {
70 var transaction = this.createElementNS(this.wfsns, 'wfs:Transaction');
71 transaction.setAttribute("version","1.0.0");
72 transaction.setAttribute("service","WFS");
73 for (var i=0; i < features.length; i++) {
74 switch (features[i].state) {
75 case OpenLayers.State.INSERT:
76 transaction.appendChild(this.insert(features[i]));
78 case OpenLayers.State.UPDATE:
79 transaction.appendChild(this.update(features[i]));
81 case OpenLayers.State.DELETE:
82 transaction.appendChild(this.remove(features[i]));
87 return OpenLayers.Format.XML.prototype.write.apply(this,[transaction]);
91 * Method: createFeatureXML
94 * feature - {<OpenLayers.Feature.Vector>}
96 createFeatureXML: function(feature) {
97 var geometryNode = this.buildGeometryNode(feature.geometry);
98 var geomContainer = this.createElementNS(this.featureNS, "feature:" + this.geometryName);
99 geomContainer.appendChild(geometryNode);
100 var featureContainer = this.createElementNS(this.featureNS, "feature:" + this.featureName);
101 featureContainer.appendChild(geomContainer);
102 for(var attr in feature.attributes) {
103 var attrText = this.createTextNode(feature.attributes[attr]);
105 if (attr.search(":") != -1) {
106 nodename = attr.split(":")[1];
108 var attrContainer = this.createElementNS(this.featureNS, "feature:" + nodename);
109 attrContainer.appendChild(attrText);
110 featureContainer.appendChild(attrContainer);
112 return featureContainer;
117 * Takes a feature, and generates a WFS-T Transaction "Insert"
120 * feature - {<OpenLayers.Feature.Vector>}
122 insert: function(feature) {
123 var insertNode = this.createElementNS(this.wfsns, 'wfs:Insert');
124 insertNode.appendChild(this.createFeatureXML(feature));
130 * Takes a feature, and generates a WFS-T Transaction "Update"
133 * feature - {<OpenLayers.Feature.Vector>}
135 update: function(feature) {
136 if (!feature.fid) { OpenLayers.Console.userError(OpenLayers.i18n("noFID")); }
137 var updateNode = this.createElementNS(this.wfsns, 'wfs:Update');
138 updateNode.setAttribute("typeName", this.featurePrefix + ':' + this.featureName);
139 updateNode.setAttribute("xmlns:" + this.featurePrefix, this.featureNS);
141 var propertyNode = this.createElementNS(this.wfsns, 'wfs:Property');
142 var nameNode = this.createElementNS(this.wfsns, 'wfs:Name');
144 var txtNode = this.createTextNode(this.geometryName);
145 nameNode.appendChild(txtNode);
146 propertyNode.appendChild(nameNode);
148 var valueNode = this.createElementNS(this.wfsns, 'wfs:Value');
150 var geometryNode = this.buildGeometryNode(feature.geometry);
153 geometryNode.setAttribute(
154 "srsName", feature.layer.projection.getCode()
158 valueNode.appendChild(geometryNode);
160 propertyNode.appendChild(valueNode);
161 updateNode.appendChild(propertyNode);
164 for(var propName in feature.attributes) {
165 propertyNode = this.createElementNS(this.wfsns, 'wfs:Property');
166 nameNode = this.createElementNS(this.wfsns, 'wfs:Name');
167 nameNode.appendChild(this.createTextNode(propName));
168 propertyNode.appendChild(nameNode);
169 valueNode = this.createElementNS(this.wfsns, 'wfs:Value');
170 valueNode.appendChild(this.createTextNode(feature.attributes[propName]));
171 propertyNode.appendChild(valueNode);
172 updateNode.appendChild(propertyNode);
176 var filterNode = this.createElementNS(this.ogcns, 'ogc:Filter');
177 var filterIdNode = this.createElementNS(this.ogcns, 'ogc:FeatureId');
178 filterIdNode.setAttribute("fid", feature.fid);
179 filterNode.appendChild(filterIdNode);
180 updateNode.appendChild(filterNode);
187 * Takes a feature, and generates a WFS-T Transaction "Delete"
190 * feature - {<OpenLayers.Feature.Vector>}
192 remove: function(feature) {
194 OpenLayers.Console.userError(OpenLayers.i18n("noFID"));
197 var deleteNode = this.createElementNS(this.wfsns, 'wfs:Delete');
198 deleteNode.setAttribute("typeName", this.featurePrefix + ':' + this.featureName);
199 deleteNode.setAttribute("xmlns:" + this.featurePrefix, this.featureNS);
201 var filterNode = this.createElementNS(this.ogcns, 'ogc:Filter');
202 var filterIdNode = this.createElementNS(this.ogcns, 'ogc:FeatureId');
203 filterIdNode.setAttribute("fid", feature.fid);
204 filterNode.appendChild(filterIdNode);
205 deleteNode.appendChild(filterNode);
212 * Remove ciruclar ref to layer
214 destroy: function() {
218 CLASS_NAME: "OpenLayers.Format.WFS"