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
10
* Class: OpenLayers.Format.WFS
14
* - <OpenLayers.Format.GML>
16
OpenLayers.Format.WFS = OpenLayers.Class(OpenLayers.Format.GML, {
26
wfsns: "http://www.opengis.net/wfs",
31
ogcns: "http://www.opengis.net/ogc",
34
* Constructor: OpenLayers.Format.WFS
35
* Create a WFS-T formatter. This requires a layer: that layer should
36
* have two properties: geometry_column and typename. The parser
37
* for this format is subclassed entirely from GML: There is a writer
38
* only, which uses most of the code from the GML layer, and wraps
39
* it in transactional elements.
43
* layer - {<OpenLayers.Layer>}
46
initialize: function(options, layer) {
47
OpenLayers.Format.GML.prototype.initialize.apply(this, [options]);
49
if (this.layer.featureNS) {
50
this.featureNS = this.layer.featureNS;
52
if (this.layer.options.geometry_column) {
53
this.geometryName = this.layer.options.geometry_column;
55
if (this.layer.options.typename) {
56
this.featureName = this.layer.options.typename;
62
* Takes a feature list, and generates a WFS-T Transaction
65
* features - {Array(<OpenLayers.Feature.Vector>)}
67
write: function(features) {
69
var transaction = this.createElementNS(this.wfsns, 'wfs:Transaction');
70
transaction.setAttribute("version","1.0.0");
71
transaction.setAttribute("service","WFS");
72
for (var i=0; i < features.length; i++) {
73
switch (features[i].state) {
74
case OpenLayers.State.INSERT:
75
transaction.appendChild(this.insert(features[i]));
77
case OpenLayers.State.UPDATE:
78
transaction.appendChild(this.update(features[i]));
80
case OpenLayers.State.DELETE:
81
transaction.appendChild(this.remove(features[i]));
86
return OpenLayers.Format.XML.prototype.write.apply(this,[transaction]);
90
* Method: createFeatureXML
93
* feature - {<OpenLayers.Feature.Vector>}
95
createFeatureXML: function(feature) {
96
var geometryNode = this.buildGeometryNode(feature.geometry);
97
var geomContainer = this.createElementNS(this.featureNS, "feature:" + this.geometryName);
98
geomContainer.appendChild(geometryNode);
99
var featureContainer = this.createElementNS(this.featureNS, "feature:" + this.featureName);
100
featureContainer.appendChild(geomContainer);
101
for(var attr in feature.attributes) {
102
var attrText = this.createTextNode(feature.attributes[attr]);
104
if (attr.search(":") != -1) {
105
nodename = attr.split(":")[1];
107
var attrContainer = this.createElementNS(this.featureNS, "feature:" + nodename);
108
attrContainer.appendChild(attrText);
109
featureContainer.appendChild(attrContainer);
111
return featureContainer;
116
* Takes a feature, and generates a WFS-T Transaction "Insert"
119
* feature - {<OpenLayers.Feature.Vector>}
121
insert: function(feature) {
122
var insertNode = this.createElementNS(this.wfsns, 'wfs:Insert');
123
insertNode.appendChild(this.createFeatureXML(feature));
129
* Takes a feature, and generates a WFS-T Transaction "Update"
132
* feature - {<OpenLayers.Feature.Vector>}
134
update: function(feature) {
135
if (!feature.fid) { OpenLayers.Console.userError(OpenLayers.i18n("noFID")); }
136
var updateNode = this.createElementNS(this.wfsns, 'wfs:Update');
137
updateNode.setAttribute("typeName", this.layerName);
139
var propertyNode = this.createElementNS(this.wfsns, 'wfs:Property');
140
var nameNode = this.createElementNS(this.wfsns, 'wfs:Name');
142
var txtNode = this.createTextNode(this.geometryName);
143
nameNode.appendChild(txtNode);
144
propertyNode.appendChild(nameNode);
146
var valueNode = this.createElementNS(this.wfsns, 'wfs:Value');
148
var geometryNode = this.buildGeometryNode(feature.geometry);
151
geometryNode.setAttribute(
152
"srsName", feature.layer.projection.getCode()
156
valueNode.appendChild(geometryNode);
158
propertyNode.appendChild(valueNode);
159
updateNode.appendChild(propertyNode);
162
for(var propName in feature.attributes) {
163
propertyNode = this.createElementNS(this.wfsns, 'wfs:Property');
164
nameNode = this.createElementNS(this.wfsns, 'wfs:Name');
165
nameNode.appendChild(this.createTextNode(propName));
166
propertyNode.appendChild(nameNode);
167
valueNode = this.createElementNS(this.wfsns, 'wfs:Value');
168
valueNode.appendChild(this.createTextNode(feature.attributes[propName]));
169
propertyNode.appendChild(valueNode);
170
updateNode.appendChild(propertyNode);
174
var filterNode = this.createElementNS(this.ogcns, 'ogc:Filter');
175
var filterIdNode = this.createElementNS(this.ogcns, 'ogc:FeatureId');
176
filterIdNode.setAttribute("fid", feature.fid);
177
filterNode.appendChild(filterIdNode);
178
updateNode.appendChild(filterNode);
185
* Takes a feature, and generates a WFS-T Transaction "Delete"
188
* feature - {<OpenLayers.Feature.Vector>}
190
remove: function(feature) {
192
OpenLayers.Console.userError(OpenLayers.i18n("noFID"));
195
var deleteNode = this.createElementNS(this.wfsns, 'wfs:Delete');
196
deleteNode.setAttribute("typeName", this.layerName);
198
var filterNode = this.createElementNS(this.ogcns, 'ogc:Filter');
199
var filterIdNode = this.createElementNS(this.ogcns, 'ogc:FeatureId');
200
filterIdNode.setAttribute("fid", feature.fid);
201
filterNode.appendChild(filterIdNode);
202
deleteNode.appendChild(filterNode);
209
* Remove ciruclar ref to layer
211
destroy: function() {
215
CLASS_NAME: "OpenLayers.Format.WFS"