[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Format/XML.js |
---|
| 8 | * @requires OpenLayers/Format/WFST.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Format.WFST.v1 |
---|
| 13 | * Superclass for WFST parsers. |
---|
| 14 | * |
---|
| 15 | * Inherits from: |
---|
| 16 | * - <OpenLayers.Format.XML> |
---|
| 17 | */ |
---|
| 18 | OpenLayers.Format.WFST.v1 = OpenLayers.Class(OpenLayers.Format.XML, { |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Property: namespaces |
---|
| 22 | * {Object} Mapping of namespace aliases to namespace URIs. |
---|
| 23 | */ |
---|
| 24 | namespaces: { |
---|
| 25 | xlink: "http://www.w3.org/1999/xlink", |
---|
| 26 | xsi: "http://www.w3.org/2001/XMLSchema-instance", |
---|
| 27 | wfs: "http://www.opengis.net/wfs", |
---|
| 28 | gml: "http://www.opengis.net/gml", |
---|
| 29 | ogc: "http://www.opengis.net/ogc" |
---|
| 30 | }, |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Property: defaultPrefix |
---|
| 34 | */ |
---|
| 35 | defaultPrefix: "wfs", |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Property: version |
---|
| 39 | * {String} WFS version number. |
---|
| 40 | */ |
---|
| 41 | version: null, |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Property: schemaLocation |
---|
| 45 | * {String} Schema location for a particular minor version. |
---|
| 46 | */ |
---|
| 47 | schemaLocations: null, |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * APIProperty: srsName |
---|
| 51 | * {String} URI for spatial reference system. |
---|
| 52 | */ |
---|
| 53 | srsName: null, |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * APIProperty: extractAttributes |
---|
| 57 | * {Boolean} Extract attributes from GML. Default is true. |
---|
| 58 | */ |
---|
| 59 | extractAttributes: true, |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * APIProperty: xy |
---|
| 63 | * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x) |
---|
| 64 | * Changing is not recommended, a new Format should be instantiated. |
---|
| 65 | */ |
---|
| 66 | xy: true, |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Property: stateName |
---|
| 70 | * {Object} Maps feature states to node names. |
---|
| 71 | */ |
---|
| 72 | stateName: null, |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Constructor: OpenLayers.Format.WFST.v1 |
---|
| 76 | * Instances of this class are not created directly. Use the |
---|
| 77 | * <OpenLayers.Format.WFST.v1_0_0> or <OpenLayers.Format.WFST.v1_1_0> |
---|
| 78 | * constructor instead. |
---|
| 79 | * |
---|
| 80 | * Parameters: |
---|
| 81 | * options - {Object} An optional object whose properties will be set on |
---|
| 82 | * this instance. |
---|
| 83 | */ |
---|
| 84 | initialize: function(options) { |
---|
| 85 | // set state name mapping |
---|
| 86 | this.stateName = {}; |
---|
| 87 | this.stateName[OpenLayers.State.INSERT] = "wfs:Insert"; |
---|
| 88 | this.stateName[OpenLayers.State.UPDATE] = "wfs:Update"; |
---|
| 89 | this.stateName[OpenLayers.State.DELETE] = "wfs:Delete"; |
---|
| 90 | OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); |
---|
| 91 | }, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Method: getSrsName |
---|
| 95 | */ |
---|
| 96 | getSrsName: function(feature, options) { |
---|
| 97 | var srsName = options && options.srsName; |
---|
| 98 | if(!srsName) { |
---|
| 99 | if(feature && feature.layer) { |
---|
| 100 | srsName = feature.layer.projection.getCode(); |
---|
| 101 | } else { |
---|
| 102 | srsName = this.srsName; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | return srsName; |
---|
| 106 | }, |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * APIMethod: read |
---|
| 110 | * Parse the response from a transaction. Because WFS is split into |
---|
| 111 | * Transaction requests (create, update, and delete) and GetFeature |
---|
| 112 | * requests (read), this method handles parsing of both types of |
---|
| 113 | * responses. |
---|
| 114 | * |
---|
| 115 | * Parameters: |
---|
| 116 | * data - {String | Document} The WFST document to read |
---|
| 117 | * options - {Object} Options for the reader |
---|
| 118 | * |
---|
| 119 | * Valid options properties: |
---|
| 120 | * output - {String} either "features" or "object". The default is |
---|
| 121 | * "features", which means that the method will return an array of |
---|
| 122 | * features. If set to "object", an object with a "features" property |
---|
| 123 | * and other properties read by the parser will be returned. |
---|
| 124 | * |
---|
| 125 | * Returns: |
---|
| 126 | * {Array | Object} Output depending on the output option. |
---|
| 127 | */ |
---|
| 128 | read: function(data, options) { |
---|
| 129 | options = options || {}; |
---|
| 130 | OpenLayers.Util.applyDefaults(options, { |
---|
| 131 | output: "features" |
---|
| 132 | }); |
---|
| 133 | |
---|
| 134 | if(typeof data == "string") { |
---|
| 135 | data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); |
---|
| 136 | } |
---|
| 137 | if(data && data.nodeType == 9) { |
---|
| 138 | data = data.documentElement; |
---|
| 139 | } |
---|
| 140 | var obj = {}; |
---|
| 141 | if(data) { |
---|
| 142 | this.readNode(data, obj); |
---|
| 143 | } |
---|
| 144 | if(obj.features && options.output === "features") { |
---|
| 145 | obj = obj.features; |
---|
| 146 | } |
---|
| 147 | return obj; |
---|
| 148 | }, |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Property: readers |
---|
| 152 | * Contains public functions, grouped by namespace prefix, that will |
---|
| 153 | * be applied when a namespaced node is found matching the function |
---|
| 154 | * name. The function will be applied in the scope of this parser |
---|
| 155 | * with two arguments: the node being read and a context object passed |
---|
| 156 | * from the parent. |
---|
| 157 | */ |
---|
| 158 | readers: { |
---|
| 159 | "wfs": { |
---|
| 160 | "FeatureCollection": function(node, obj) { |
---|
| 161 | obj.features = []; |
---|
| 162 | this.readChildNodes(node, obj); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | }, |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Method: write |
---|
| 169 | * Given an array of features, write a WFS transaction. This assumes |
---|
| 170 | * the features have a state property that determines the operation |
---|
| 171 | * type - insert, update, or delete. |
---|
| 172 | * |
---|
| 173 | * Parameters: |
---|
| 174 | * features - {Array(<OpenLayers.Feature.Vector>)} A list of features. |
---|
| 175 | * |
---|
| 176 | * Returns: |
---|
| 177 | * {String} A serialized WFS transaction. |
---|
| 178 | */ |
---|
| 179 | write: function(features) { |
---|
| 180 | var node = this.writeNode("wfs:Transaction", features); |
---|
| 181 | var value = this.schemaLocationAttr(); |
---|
| 182 | if(value) { |
---|
| 183 | this.setAttributeNS( |
---|
| 184 | node, this.namespaces["xsi"], "xsi:schemaLocation", value |
---|
| 185 | ) |
---|
| 186 | } |
---|
| 187 | return OpenLayers.Format.XML.prototype.write.apply(this, [node]); |
---|
| 188 | }, |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Property: writers |
---|
| 192 | * As a compliment to the readers property, this structure contains public |
---|
| 193 | * writing functions grouped by namespace alias and named like the |
---|
| 194 | * node names they produce. |
---|
| 195 | */ |
---|
| 196 | writers: { |
---|
| 197 | "wfs": { |
---|
| 198 | "GetFeature": function(options) { |
---|
| 199 | var node = this.createElementNSPlus("wfs:GetFeature", { |
---|
| 200 | attributes: { |
---|
| 201 | service: "WFS", |
---|
| 202 | version: this.version, |
---|
| 203 | outputFormat: options && options.outputFormat, |
---|
| 204 | maxFeatures: options && options.maxFeatures, |
---|
| 205 | "xsi:schemaLocation": this.schemaLocationAttr(options) |
---|
| 206 | } |
---|
| 207 | }); |
---|
| 208 | if (typeof this.featureType == "string") { |
---|
| 209 | this.writeNode("Query", options, node); |
---|
| 210 | } else { |
---|
| 211 | for (var i=0,len = this.featureType.length; i<len; i++) { |
---|
| 212 | options.featureType = this.featureType[i]; |
---|
| 213 | this.writeNode("Query", options, node); |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | return node; |
---|
| 217 | }, |
---|
| 218 | "Transaction": function(features) { |
---|
| 219 | var node = this.createElementNSPlus("wfs:Transaction", { |
---|
| 220 | attributes: { |
---|
| 221 | service: "WFS", |
---|
| 222 | version: this.version |
---|
| 223 | } |
---|
| 224 | }); |
---|
| 225 | if(features) { |
---|
| 226 | var name, feature; |
---|
| 227 | for(var i=0, len=features.length; i<len; ++i) { |
---|
| 228 | feature = features[i]; |
---|
| 229 | name = this.stateName[feature.state]; |
---|
| 230 | if(name) { |
---|
| 231 | this.writeNode(name, feature, node); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | return node; |
---|
| 236 | }, |
---|
| 237 | "Insert": function(feature) { |
---|
| 238 | var node = this.createElementNSPlus("wfs:Insert"); |
---|
| 239 | this.srsName = this.getSrsName(feature); |
---|
| 240 | this.writeNode("feature:_typeName", feature, node); |
---|
| 241 | return node; |
---|
| 242 | }, |
---|
| 243 | "Update": function(feature) { |
---|
| 244 | var node = this.createElementNSPlus("wfs:Update", { |
---|
| 245 | attributes: { |
---|
| 246 | typeName: (this.featureNS ? this.featurePrefix + ":" : "") + |
---|
| 247 | this.featureType |
---|
| 248 | } |
---|
| 249 | }); |
---|
| 250 | if(this.featureNS) { |
---|
| 251 | node.setAttribute("xmlns:" + this.featurePrefix, this.featureNS); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | // add in geometry |
---|
| 255 | if (this.geometryName !== null) { |
---|
| 256 | this.writeNode( |
---|
| 257 | "Property", {name: this.geometryName, value: feature}, node |
---|
| 258 | ); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | // add in attributes |
---|
| 262 | for(var key in feature.attributes) { |
---|
| 263 | if(feature.attributes[key] !== undefined) { |
---|
| 264 | this.writeNode( |
---|
| 265 | "Property", {name: key, value: feature.attributes[key]}, node |
---|
| 266 | ); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // add feature id filter |
---|
| 271 | this.writeNode("ogc:Filter", new OpenLayers.Filter.FeatureId({ |
---|
| 272 | fids: [feature.fid] |
---|
| 273 | }), node); |
---|
| 274 | |
---|
| 275 | return node; |
---|
| 276 | }, |
---|
| 277 | "Property": function(obj) { |
---|
| 278 | var node = this.createElementNSPlus("wfs:Property"); |
---|
| 279 | this.writeNode("Name", obj.name, node); |
---|
| 280 | if(obj.value !== null) { |
---|
| 281 | this.writeNode("Value", obj.value, node); |
---|
| 282 | } |
---|
| 283 | return node; |
---|
| 284 | }, |
---|
| 285 | "Name": function(name) { |
---|
| 286 | return this.createElementNSPlus("wfs:Name", {value: name}); |
---|
| 287 | }, |
---|
| 288 | "Value": function(obj) { |
---|
| 289 | var node; |
---|
| 290 | if(obj instanceof OpenLayers.Feature.Vector) { |
---|
| 291 | node = this.createElementNSPlus("wfs:Value"); |
---|
| 292 | this.srsName = this.getSrsName(obj); |
---|
| 293 | var geom = this.writeNode("feature:_geometry", obj.geometry).firstChild; |
---|
| 294 | node.appendChild(geom); |
---|
| 295 | } else { |
---|
| 296 | node = this.createElementNSPlus("wfs:Value", {value: obj}); |
---|
| 297 | } |
---|
| 298 | return node; |
---|
| 299 | }, |
---|
| 300 | "Delete": function(feature) { |
---|
| 301 | var node = this.createElementNSPlus("wfs:Delete", { |
---|
| 302 | attributes: { |
---|
| 303 | typeName: (this.featureNS ? this.featurePrefix + ":" : "") + |
---|
| 304 | this.featureType |
---|
| 305 | } |
---|
| 306 | }); |
---|
| 307 | if(this.featureNS) { |
---|
| 308 | node.setAttribute("xmlns:" + this.featurePrefix, this.featureNS); |
---|
| 309 | } |
---|
| 310 | this.writeNode("ogc:Filter", new OpenLayers.Filter.FeatureId({ |
---|
| 311 | fids: [feature.fid] |
---|
| 312 | }), node); |
---|
| 313 | return node; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | }, |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * Method: schemaLocationAttr |
---|
| 320 | * Generate the xsi:schemaLocation attribute value. |
---|
| 321 | * |
---|
| 322 | * Returns: |
---|
| 323 | * {String} The xsi:schemaLocation attribute or undefined if none. |
---|
| 324 | */ |
---|
| 325 | schemaLocationAttr: function(options) { |
---|
| 326 | options = OpenLayers.Util.extend({ |
---|
| 327 | featurePrefix: this.featurePrefix, |
---|
| 328 | schema: this.schema |
---|
| 329 | }, options); |
---|
| 330 | var schemaLocations = OpenLayers.Util.extend({}, this.schemaLocations); |
---|
| 331 | if(options.schema) { |
---|
| 332 | schemaLocations[options.featurePrefix] = options.schema; |
---|
| 333 | } |
---|
| 334 | var parts = []; |
---|
| 335 | var uri; |
---|
| 336 | for(var key in schemaLocations) { |
---|
| 337 | uri = this.namespaces[key]; |
---|
| 338 | if(uri) { |
---|
| 339 | parts.push(uri + " " + schemaLocations[key]); |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | var value = parts.join(" ") || undefined; |
---|
| 343 | return value; |
---|
| 344 | }, |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | * Method: setFilterProperty |
---|
| 348 | * Set the property of each spatial filter. |
---|
| 349 | * |
---|
| 350 | * Parameters: |
---|
| 351 | * filter - {<OpenLayers.Filter>} |
---|
| 352 | */ |
---|
| 353 | setFilterProperty: function(filter) { |
---|
| 354 | if(filter.filters) { |
---|
| 355 | for(var i=0, len=filter.filters.length; i<len; ++i) { |
---|
| 356 | this.setFilterProperty(filter.filters[i]); |
---|
| 357 | } |
---|
| 358 | } else { |
---|
| 359 | if(filter instanceof OpenLayers.Filter.Spatial) { |
---|
| 360 | // got a spatial filter, set its property |
---|
| 361 | filter.property = this.geometryName; |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | }, |
---|
| 365 | |
---|
| 366 | CLASS_NAME: "OpenLayers.Format.WFST.v1" |
---|
| 367 | |
---|
| 368 | }); |
---|