Ticket #1648: wfs.patch

File wfs.patch, 60.5 KB (added by tschaub, 3 years ago)

add wfs protocol for 1.0 and 1.1

  • tests/Protocol/WFS/v1_0_0.html

     
     1<html> 
     2<head> 
     3  <script src="../../../lib/OpenLayers.js"></script> 
     4  <script type="text/javascript"> 
     5 
     6    function test_initialize(t) { 
     7        t.plan(1); 
     8 
     9        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     10            format: new OpenLayers.Format.GML.v2({ 
     11                featureType: "type", 
     12                featureNS: "http://namespace.org" 
     13            }) 
     14        }); 
     15        t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_0_0, 
     16             "initialize returns instance") 
     17    } 
     18     
     19    function test_read(t) { 
     20        t.plan(2); 
     21 
     22        var url = "http://some.url.org"; 
     23        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     24            url: url, 
     25            featureNS: "http://namespace.org", 
     26            featureType: "type", 
     27            format: new OpenLayers.Format.GML.v2({ 
     28                featureNS: "http://namespace.org", 
     29                featureType: "type", 
     30            }) 
     31        }); 
     32 
     33        var _POST = OpenLayers.Request.POST; 
     34         
     35        OpenLayers.Request.POST = function(obj) { 
     36            return obj; 
     37        }; 
     38         
     39        var response = protocol.read(); 
     40        var simpleReadXML = '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="GML2"><wfs:Query typeName="feature:type" srsName="EPSG:4326" xmlns:feature="http://namespace.org"/></wfs:GetFeature>'; 
     41         
     42        var request = response.priv; 
     43        t.xml_eq(request.data, simpleReadXML, "read with no option"); 
     44 
     45        options = { 
     46            maxFeatures: 10, 
     47            featureType: 'type2', 
     48            srsName: 'EPSG:900913', 
     49            featureNS: 'htttp://alternative.namespace.org' 
     50        }; 
     51        var optionsReadXML = '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="10" outputFormat="GML2"><wfs:Query typeName="feature:type2" srsName="EPSG:900913" xmlns:feature="htttp://alternative.namespace.org"/></wfs:GetFeature>'; 
     52         
     53        var response = protocol.read(options); 
     54        var request = response.priv; 
     55        t.xml_eq(request.data, optionsReadXML, "read with options") 
     56        
     57        OpenLayers.Request.POST = _POST; 
     58    } 
     59     
     60    function test_update(t) { 
     61        t.plan( 4 ); 
     62 
     63        var url = "http://some.url.org"; 
     64        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     65            url: url, 
     66            featureNS: "http://namespace.org", 
     67            featureType: "type", 
     68            format: new OpenLayers.Format.GML.v2({ 
     69                featureNS: "http://namespace.org", 
     70                featureType: "type", 
     71            }) 
     72        }); 
     73 
     74        var test_feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"}); 
     75        test_feature.fid = "fid.37"; 
     76        test_feature.layer = { 
     77            projection: { 
     78                getCode : function(){ 
     79                    return "EPSG:4326"; 
     80                } 
     81            } 
     82        } 
     83 
     84        options = {featureNS: "http://some.namespace.org", featureType: "type"} 
     85 
     86        var node = protocol.update(test_feature,options); 
     87        
     88        t.ok(node,"Output of update is not null"); 
     89        t.eq(node.nodeType, 1, "Output of update is a node of the proper type."); 
     90        t.eq(node.tagName, "wfs:Update", "Tagname is the appropriate one."); 
     91        t.eq(node.textContent, "the_geom42,7hascheeseburger", "The node created by update has the right textcontent."); 
     92    } 
     93     
     94    function test_delete(t) { 
     95        t.plan( 2 ); 
     96 
     97        var url = "http://some.url.org"; 
     98        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     99            url: url, 
     100            featureNS: "http://namespace.org", 
     101            featureType: "type", 
     102            format: new OpenLayers.Format.GML.v2({ 
     103                featureNS: "http://namespace.org", 
     104                featureType: "type", 
     105            }) 
     106        }); 
     107 
     108        var test_feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"}); 
     109        test_feature.fid = "fid.37"; 
     110        test_feature.layer = { 
     111            projection: { 
     112                getCode : function(){ 
     113                    return "EPSG:4326"; 
     114                } 
     115            } 
     116        } 
     117 
     118        options = {featureNS: "http://some.namespace.org", featureType: "type"} 
     119        var node = protocol['delete'](test_feature, options); 
     120        
     121        t.eq(node.tagName, "wfs:Delete", "Tagname is the appropriate one."); 
     122        t.eq(node.firstChild.firstChild.attributes.fid.nodeValue, "fid.37", "The right fid is stored in an attribute as expected."); 
     123    } 
     124     
     125     
     126    //TODO: Add more commit tests 
     127    function test_commit(t){ 
     128        t.plan(2); 
     129 
     130        var url = "http://some.url.org"; 
     131        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     132            url: url, 
     133            featureNS: "http://namespace.org", 
     134            featureType: "type", 
     135            format: new OpenLayers.Format.GML.v2({ 
     136                featureNS: "http://namespace.org", 
     137                featureType: "type", 
     138            }) 
     139        }); 
     140      
     141        var _POST = OpenLayers.Request.POST;         
     142        OpenLayers.Request.POST = function(object) { 
     143            return object; 
     144        }; 
     145         
     146        var featureFactory = function(attribute, id) { 
     147            var feature = new OpenLayers.Feature.Vector( 
     148                new OpenLayers.Geometry.Point(42, 7), {has: attribute} 
     149            ); 
     150            feature.layer = { 
     151                projection: { 
     152                    getCode : function(){ 
     153                        return "EPSG:4326"; 
     154                    } 
     155                } 
     156            } 
     157            feature.fid = "fid." + id;             
     158            return feature; 
     159        } 
     160         
     161        var test_feature1 = featureFactory("cheeseburger",7); 
     162         
     163        var response = protocol.commit([test_feature1]); 
     164        var request = response.priv; 
     165        var expected = '<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="GML2" />'; 
     166       
     167        t.xml_eq(request.data, expected, "Correct envelope XML for request data for no changed features"); 
     168 
     169        //testing getMethod 
     170        test_feature1.state = OpenLayers.State.INSERT; 
     171         
     172        var test_feature2 = featureFactory("lolcat", 8); 
     173        test_feature2.state = OpenLayers.State.UPDATE; 
     174                 
     175        var test_feature3 = featureFactory("asdf", 9); 
     176        test_feature3.state = OpenLayers.State.DELETE; 
     177         
     178        var response = protocol.commit([test_feature1, test_feature2, test_feature3]); 
     179        var request = response.priv; 
     180         
     181        expected = '<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="GML2" xmlns:feature="http://namespace.org"><wfs:Insert><feature:type fid="fid.7"><feature:geometry><gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates></gml:Point></feature:geometry><feature:has>cheeseburger</feature:has></feature:type></wfs:Insert><wfs:Update typeName="feature:type"><wfs:Property><wfs:Name>the_geom</wfs:Name><wfs:Value><gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326"><gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates></gml:Point></wfs:Value></wfs:Property><wfs:Property><wfs:Name>has</wfs:Name><wfs:Value>lolcat</wfs:Value></wfs:Property><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:FeatureId fid="fid.8"/></ogc:Filter></wfs:Update><wfs:Delete typeName="feature:type"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:FeatureId fid="fid.9"/></ogc:Filter></wfs:Delete></wfs:Transaction>'; 
     182         
     183        t.xml_eq(request.data, expected, "Got expected XML for commit with insert, update, and delete"); 
     184         
     185        OpenLayers.Request.POST = _POST; 
     186    } 
     187 
     188    function test_callback(t) { 
     189        t.plan(3) 
     190 
     191        var xmlText = '<?xml version="1.0" encoding="UTF-8"?><wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://sigma.openplans.org:80/geoserver/schemas/wfs/1.0.0/WFS-transaction.xsd"><wfs:InsertResult><ogc:FeatureId fid="new0"/><ogc:FeatureId fid="new1"/><ogc:FeatureId fid="new2"/><ogc:FeatureId fid="new3"/><ogc:FeatureId fid="new4"/><ogc:FeatureId fid="new5"/></wfs:InsertResult> <wfs:TransactionResult> <wfs:Status> <wfs:SUCCESS/> </wfs:Status> </wfs:TransactionResult></wfs:WFS_TransactionResponse>'; 
     192 
     193        var url = "http://some.url.org"; 
     194        var protocol = new OpenLayers.Protocol.WFS.v1_0_0({ 
     195            url: url, 
     196            featureNS: "http://namespace.org", 
     197            featureType: "type", 
     198            format: new OpenLayers.Format.GML.v2({ 
     199                featureNS: "http://namespace.org", 
     200                featureType: "type", 
     201            }) 
     202        }); 
     203 
     204        var _POST = OpenLayers.Request.POST; 
     205        var xml = new OpenLayers.Format.XML(); 
     206        OpenLayers.Request.POST = function(obj) { 
     207            var request = {responseXML: xml.read(xmlText)}; 
     208            window.setTimeout(function() { 
     209                obj.callback.call(obj.scope, request); 
     210            }, 0.1); 
     211            t.delay_call(0.2, function() { 
     212                t.ok(cbResponse.success(), "success parsed and set on response"); 
     213                t.eq(cbResponse.insertIds,  ["new0","new1","new2","new3","new4","new5"], 
     214                     "Inserted FIDs parsed and passed through to callback"); 
     215                t.eq(cbScope.foo, "bar", "Callback called in proper scope"); 
     216            }); 
     217            return request; 
     218        }; 
     219         
     220        var cbResponse, cbScope; 
     221        var options = { 
     222            callback: function(response) { 
     223                cbResponse = response; 
     224                cbScope = this; 
     225            }, 
     226            scope: {foo: "bar"} 
     227        }; 
     228        var response = protocol.commit([], options); 
     229     
     230        OpenLayers.Request.POST = _POST; 
     231    } 
     232 
     233  </script> 
     234</head> 
     235<body> 
     236<div id="map" style="width:512px; height:256px"> </div> 
     237</body> 
     238</html> 
  • lib/OpenLayers/Format/WFST/v1_0_0.js

     
     1/** 
     2 * @requires OpenLayers/Format/WFST/v1.js 
     3 * @requires OpenLayers/Format/GML/v2.js 
     4 * @requires OpenLayers/Format/Filter/v1_0_0.js 
     5 */ 
     6 
     7/** 
     8 * Class: OpenLayers.Format.WFST.v1_0_0 
     9 * A format for creating WFS v1.0.0 transactions.  Create a new instance with the 
     10 *     <OpenLayers.Format.WFST.v1_0_0> constructor. 
     11 * 
     12 * Inherits from: 
     13 *  - <OpenLayers.Format.WFST.v1> 
     14 */ 
     15OpenLayers.Format.WFST.v1_0_0 = OpenLayers.Class(OpenLayers.Format.WFST.v1, { 
     16     
     17    /** 
     18     * Property: version 
     19     * {String} WFS version number. 
     20     */ 
     21    version: "1.0.0", 
     22     
     23    /** 
     24     * Property: schemaLocations 
     25     * {Object} Properties are namespace aliases, values are schema locations. 
     26     */ 
     27    schemaLocations: { 
     28        "wfs": "http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" 
     29    }, 
     30 
     31    /** 
     32     * Constructor: OpenLayers.Format.WFST.v1_0_0 
     33     * A class for parsing and generating WFS v1.0.0 transactions. 
     34     * 
     35     * Parameters: 
     36     * options - {Object} Optional object whose properties will be set on the 
     37     *     instance. 
     38     * 
     39     * Valid options properties: 
     40     * featureType - {String} Local (without prefix) feature typeName (required). 
     41     * featureNS - {String} Feature namespace (optional). 
     42     * featurePrefix - {String} Feature namespace alias (optional - only used 
     43     *     if featureNS is provided).  Default is 'feature'. 
     44     * geometryName - {String} Name of geometry attribute.  Default is 'the_geom'. 
     45     */ 
     46    initialize: function(options) { 
     47        OpenLayers.Format.WFST.v1.prototype.initialize.apply(this, [options]); 
     48        OpenLayers.Format.GML.v2.prototype.setGeometryTypes.call(this); 
     49    }, 
     50     
     51    /** 
     52     * Property: readers 
     53     * Contains public functions, grouped by namespace prefix, that will 
     54     *     be applied when a namespaced node is found matching the function 
     55     *     name.  The function will be applied in the scope of this parser 
     56     *     with two arguments: the node being read and a context object passed 
     57     *     from the parent. 
     58     */ 
     59    readers: { 
     60        "wfs": OpenLayers.Util.applyDefaults({ 
     61            "WFS_TransactionResponse": function(node, obj) { 
     62                obj.insertIds = []; 
     63                obj.success = false; 
     64                this.readChildNodes(node, obj); 
     65            }, 
     66            "InsertResult": function(node, container) { 
     67                var obj = {fids: []}; 
     68                this.readChildNodes(node, obj); 
     69                container.insertIds.push(obj.fids[0]); 
     70            }, 
     71            "TransactionResult": function(node, obj) { 
     72                this.readChildNodes(node, obj); 
     73            }, 
     74            "Status": function(node, obj) { 
     75                this.readChildNodes(node, obj); 
     76            }, 
     77            "SUCCESS": function(node, obj) { 
     78                obj.success = true; 
     79            } 
     80        }, OpenLayers.Format.WFST.v1.prototype.readers["wfs"]), 
     81        "gml": OpenLayers.Format.GML.v2.prototype.readers["gml"], 
     82        "feature": OpenLayers.Format.GML.v2.prototype.readers["feature"], 
     83        "ogc": OpenLayers.Format.Filter.v1_0_0.prototype.readers["ogc"] 
     84    }, 
     85 
     86    /** 
     87     * Property: writers 
     88     * As a compliment to the readers property, this structure contains public 
     89     *     writing functions grouped by namespace alias and named like the 
     90     *     node names they produce. 
     91     */ 
     92    writers: { 
     93        "wfs": OpenLayers.Util.applyDefaults({ 
     94            "Query": function(options) { 
     95                options = OpenLayers.Util.extend({ 
     96                    featureNS: this.featureNS, 
     97                    featurePrefix: this.featurePrefix, 
     98                    featureType: this.featureType, 
     99                    srsName: this.srsName 
     100                }, options); 
     101                var node = this.createElementNSPlus("wfs:Query", { 
     102                    attributes: { 
     103                        typeName: (options.featureNS ? options.featurePrefix + ":" : "") + 
     104                            options.featureType 
     105                    } 
     106                }); 
     107                if(options.featureNS) { 
     108                    node.setAttribute("xmlns:" + options.featurePrefix, options.featureNS); 
     109                } 
     110                if(options.filter) { 
     111                    this.setFilterProperty(options.filter); 
     112                    this.writeNode("ogc:Filter", options.filter, node); 
     113                } 
     114                return node; 
     115            } 
     116        }, OpenLayers.Format.WFST.v1.prototype.writers["wfs"]), 
     117        "gml": OpenLayers.Format.GML.v2.prototype.writers["gml"], 
     118        "feature": OpenLayers.Format.GML.v2.prototype.writers["feature"], 
     119        "ogc": OpenLayers.Format.Filter.v1_0_0.prototype.writers["ogc"] 
     120    }, 
     121    
     122    CLASS_NAME: "OpenLayers.Format.WFST.v1_0_0"  
     123}); 
  • lib/OpenLayers/Format/WFST/v1_1_0.js

     
     1/** 
     2 * @requires OpenLayers/Format/WFST/v1.js 
     3 * @requires OpenLayers/Format/GML/v3.js 
     4 * @requires OpenLayers/Format/Filter/v1_1_0.js 
     5 */ 
     6 
     7/** 
     8 * Class: OpenLayers.Format.WFST.v1_1_0 
     9 * A format for creating WFS v1.1.0 transactions.  Create a new instance with the 
     10 *     <OpenLayers.Format.WFST.v1_1_0> constructor. 
     11 * 
     12 * Inherits from: 
     13 *  - <OpenLayers.Format.WFST.v1> 
     14 */ 
     15OpenLayers.Format.WFST.v1_1_0 = OpenLayers.Class(OpenLayers.Format.WFST.v1, { 
     16     
     17    /** 
     18     * Property: version 
     19     * {String} WFS version number. 
     20     */ 
     21    version: "1.1.0", 
     22     
     23    /** 
     24     * Property: schemaLocations 
     25     * {Object} Properties are namespace aliases, values are schema locations. 
     26     */ 
     27    schemaLocations: { 
     28        "wfs": "http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" 
     29    }, 
     30     
     31    /** 
     32     * Constructor: OpenLayers.Format.WFST.v1_1_0 
     33     * A class for parsing and generating WFS v1.1.0 transactions. 
     34     * 
     35     * Parameters: 
     36     * options - {Object} Optional object whose properties will be set on the 
     37     *     instance. 
     38     * 
     39     * Valid options properties: 
     40     * featureType - {String} Local (without prefix) feature typeName (required). 
     41     * featureNS - {String} Feature namespace (optional). 
     42     * featurePrefix - {String} Feature namespace alias (optional - only used 
     43     *     if featureNS is provided).  Default is 'feature'. 
     44     * geometryName - {String} Name of geometry attribute.  Default is 'the_geom'. 
     45     */ 
     46    initialize: function(options) { 
     47        OpenLayers.Format.WFST.v1.prototype.initialize.apply(this, [options]); 
     48        OpenLayers.Format.GML.v3.prototype.setGeometryTypes.call(this); 
     49    }, 
     50     
     51    /** 
     52     * Property: readers 
     53     * Contains public functions, grouped by namespace prefix, that will 
     54     *     be applied when a namespaced node is found matching the function 
     55     *     name.  The function will be applied in the scope of this parser 
     56     *     with two arguments: the node being read and a context object passed 
     57     *     from the parent. 
     58     */ 
     59    readers: { 
     60        "wfs": OpenLayers.Util.applyDefaults({ 
     61            "TransactionResponse": function(node, obj) { 
     62                obj.insertIds = []; 
     63                obj.success = false; 
     64                this.readChildNodes(node, obj); 
     65            }, 
     66            "TransactionSummary": function(node, obj) { 
     67                // this is a limited test of success 
     68                obj.success = true; 
     69            }, 
     70            "InsertResults": function(node, obj) { 
     71                this.readChildNodes(node, obj); 
     72            }, 
     73            "Feature": function(node, container) { 
     74                var obj = {fids: []}; 
     75                this.readChildNodes(node, obj); 
     76                container.insertIds.push(obj.fids[0]); 
     77            } 
     78        }, OpenLayers.Format.WFST.v1.prototype.readers["wfs"]), 
     79        "gml": OpenLayers.Format.GML.v3.prototype.readers["gml"], 
     80        "feature": OpenLayers.Format.GML.v3.prototype.readers["feature"], 
     81        "ogc": OpenLayers.Format.Filter.v1_1_0.prototype.readers["ogc"] 
     82    }, 
     83 
     84    /** 
     85     * Property: writers 
     86     * As a compliment to the readers property, this structure contains public 
     87     *     writing functions grouped by namespace alias and named like the 
     88     *     node names they produce. 
     89     */ 
     90    writers: { 
     91        "wfs": OpenLayers.Util.applyDefaults({ 
     92            "Query": function(options) { 
     93                options = OpenLayers.Util.extend({ 
     94                    featureNS: this.featureNS, 
     95                    featurePrefix: this.featurePrefix, 
     96                    featureType: this.featureType, 
     97                    srsName: this.srsName 
     98                }, options); 
     99                var node = this.createElementNSPlus("wfs:Query", { 
     100                    attributes: { 
     101                        typeName: (options.featureNS ? options.featurePrefix + ":" : "") + 
     102                            options.featureType, 
     103                        srsName: options.srsName 
     104                    } 
     105                }); 
     106                if(options.featureNS) { 
     107                    node.setAttribute("xmlns:" + options.featurePrefix, options.featureNS); 
     108                } 
     109                if(options.filter) { 
     110                    this.setFilterProperty(options.filter); 
     111                    this.writeNode("ogc:Filter", options.filter, node); 
     112                } 
     113                return node; 
     114            } 
     115        }, OpenLayers.Format.WFST.v1.prototype.writers["wfs"]), 
     116        "gml": OpenLayers.Format.GML.v3.prototype.writers["gml"], 
     117        "feature": OpenLayers.Format.GML.v3.prototype.writers["feature"], 
     118        "ogc": OpenLayers.Format.Filter.v1_1_0.prototype.writers["ogc"] 
     119    }, 
     120 
     121    CLASS_NAME: "OpenLayers.Format.WFST.v1_1_0"  
     122}); 
  • lib/OpenLayers/Format/WFST/v1.js

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

     
     1/** 
     2 * @requires OpenLayers/Format.js 
     3 */ 
     4 
     5/** 
     6 * Function: OpenLayers.Format.WFST 
     7 * Used to create a versioned WFS protocol.  Default version is 1.0.0. 
     8 * 
     9 * Returns: 
     10 * {<OpenLayers.Format>} A WFST format of the given version. 
     11 */ 
     12OpenLayers.Format.WFST = function(options) { 
     13    options = OpenLayers.Util.applyDefaults( 
     14        options, OpenLayers.Format.WFST.DEFAULTS 
     15    ); 
     16    var cls = OpenLayers.Format.WFST["v"+options.version.replace(/\./g, "_")]; 
     17    if(!cls) { 
     18        throw "Unsupported WFST version: " + options.version; 
     19    } 
     20    return new cls(options); 
     21} 
     22 
     23/** 
     24 * Constant: OpenLayers.Format.WFST.DEFAULTS 
     25 * {Object} Default properties for the WFST format. 
     26 */ 
     27OpenLayers.Format.WFST.DEFAULTS = { 
     28    "version": "1.0.0" 
     29}; 
  • lib/OpenLayers/Protocol/WFS/v1_0_0.js

     
     1/** 
     2 * @requires OpenLayers/Protocol/WFS/v1.js 
     3 * @requires OpenLayers/Format/WFST/v1_0_0.js 
     4 */ 
     5 
     6/** 
     7 * Class: OpenLayers.Protocol.WFS.v1_0_0 
     8 * A WFS v1.0.0 protocol for vector layers.  Create a new instance with the 
     9 *     <OpenLayers.Protocol.WFS.v1_0_0> constructor. 
     10 * 
     11 * Inherits from: 
     12 *  - <OpenLayers.Protocol.WFS.v1> 
     13 */ 
     14OpenLayers.Protocol.WFS.v1_0_0 = OpenLayers.Class(OpenLayers.Protocol.WFS.v1, { 
     15     
     16    /** 
     17     * Property: version 
     18     * {String} WFS version number. 
     19     */ 
     20    version: "1.0.0", 
     21     
     22    /** 
     23     * Constructor: OpenLayers.Protocol.WFS.v1_0_0 
     24     * A class for giving layers WFS v1.0.0 protocol. 
     25     * 
     26     * Parameters: 
     27     * options - {Object} Optional object whose properties will be set on the 
     28     *     instance. 
     29     * 
     30     * Valid options properties: 
     31     * featureType - {String} Local (without prefix) feature typeName (required). 
     32     * featureNS - {String} Feature namespace (optional). 
     33     * featurePrefix - {String} Feature namespace alias (optional - only used 
     34     *     if featureNS is provided).  Default is 'feature'. 
     35     * geometryName - {String} Name of geometry attribute.  Default is 'the_geom'. 
     36     */ 
     37    
     38    CLASS_NAME: "OpenLayers.Protocol.WFS.v1_0_0"  
     39}); 
  • lib/OpenLayers/Protocol/WFS/v1_1_0.js

     
     1/** 
     2 * @requires OpenLayers/Protocol/WFS/v1.js 
     3 * @requires OpenLayers/Format/WFST/v1_1_0.js 
     4 */ 
     5 
     6/** 
     7 * Class: OpenLayers.Protocol.WFS.v1_1_0 
     8 * A WFS v1.1.0 protocol for vector layers.  Create a new instance with the 
     9 *     <OpenLayers.Protocol.WFS.v1_1_0> constructor. 
     10 * 
     11 * Differences from the v1.0.0 protocol: 
     12 *  - uses Filter Encoding 1.1.0 instead of 1.0.0 
     13 *  - uses GML 3 instead of 2 if no format is provided 
     14 *   
     15 * Inherits from: 
     16 *  - <OpenLayers.Protocol.WFS.v1> 
     17 */ 
     18OpenLayers.Protocol.WFS.v1_1_0 = OpenLayers.Class(OpenLayers.Protocol.WFS.v1, { 
     19     
     20    /** 
     21     * Property: version 
     22     * {String} WFS version number. 
     23     */ 
     24    version: "1.1.0", 
     25     
     26    /** 
     27     * Constructor: OpenLayers.Protocol.WFS.v1_1_0 
     28     * A class for giving layers WFS v1.1.0 protocol. 
     29     * 
     30     * Parameters: 
     31     * options - {Object} Optional object whose properties will be set on the 
     32     *     instance. 
     33     * 
     34     * Valid options properties: 
     35     * featureType - {String} Local (without prefix) feature typeName (required). 
     36     * featureNS - {String} Feature namespace (optional). 
     37     * featurePrefix - {String} Feature namespace alias (optional - only used 
     38     *     if featureNS is provided).  Default is 'feature'. 
     39     * geometryName - {String} Name of geometry attribute.  Default is 'the_geom'. 
     40     */ 
     41    
     42    CLASS_NAME: "OpenLayers.Protocol.WFS.v1_1_0" 
     43}); 
  • lib/OpenLayers/Protocol/WFS/v1.js

     
     1/** 
     2 * @requires OpenLayers/Protocol/WFS.js 
     3 */ 
     4 
     5/** 
     6 * Class: OpenLayers.Protocol.WFS.v1 
     7 * Abstract class for for v1.0.0 and v1.1.0 protocol. 
     8 * 
     9 * Inherits from: 
     10 *  - <OpenLayers.Protocol> 
     11 */ 
     12OpenLayers.Protocol.WFS.v1 = new OpenLayers.Class(OpenLayers.Protocol, { 
     13     
     14    /** 
     15     * Property: version 
     16     * {String} WFS version number. 
     17     */ 
     18    version: null, 
     19     
     20    /** 
     21     * Property: srsName 
     22     * {String} Name of spatial reference system.  Default is "EPSG:4326". 
     23     */ 
     24    srsName: "EPSG:4326", 
     25     
     26    /** 
     27     * Property: featureType 
     28     * {String} Local feature typeName. 
     29     */ 
     30    featureType: null, 
     31     
     32    /** 
     33     * Property: featureNS 
     34     * {String} Feature namespace. 
     35     */ 
     36    featureNS: null, 
     37     
     38    /** 
     39     * Property: geometryName 
     40     * {String} Name of the geometry attribute for features.  Default is 
     41     *     "the_geom". 
     42     */ 
     43    geometryName: "the_geom", 
     44     
     45    /** 
     46     * Property: schema 
     47     * {String} Optional schema location that will be included in the 
     48     *     schemaLocation attribute value.  Note that the feature type schema 
     49     *     is required for a strict XML validator (on transactions with an 
     50     *     insert for example), but is *not* required by the WFS specification 
     51     *     (since the server is supposed to know about feature type schemas). 
     52     */ 
     53    schema: null, 
     54 
     55    /** 
     56     * Property: featurePrefix 
     57     * {String} Namespace alias for feature type.  Default is "feature". 
     58     */ 
     59    featurePrefix: "feature", 
     60     
     61    /** 
     62     * Property: formatOptions 
     63     * {Object} Optional options for the format.  If a format is not provided, 
     64     *     this property can be used to extend the default format options. 
     65     */ 
     66    formatOptions: null, 
     67     
     68    /** 
     69     * Constructor: OpenLayers.Protocol.WFS 
     70     * A class for giving layers WFS protocol. 
     71     * 
     72     * Parameters: 
     73     * options - {Object} Optional object whose properties will be set on the 
     74     *     instance. 
     75     * 
     76     * Valid options properties: 
     77     * featureType - {String} Local (without prefix) feature typeName (required). 
     78     * featureNS - {String} Feature namespace (optional - can be autodetected 
     79     *     for reading if featurePrefix is provided and identical to the prefix 
     80     *     in the server response). 
     81     * featurePrefix - {String} Feature namespace alias (optional - only used 
     82     *     for writing if featureNS is provided).  Default is 'feature'. 
     83     * geometryName - {String} Name of geometry attribute.  Default is 'the_geom'. 
     84     */ 
     85    initialize: function(options) { 
     86        OpenLayers.Protocol.prototype.initialize.apply(this, [options]); 
     87        if(!options.format) { 
     88            this.format = OpenLayers.Format.WFST(OpenLayers.Util.extend({ 
     89                version: this.version, 
     90                featureType: this.featureType, 
     91                featureNS: this.featureNS, 
     92                featurePrefix: this.featurePrefix, 
     93                geometryName: this.geometryName, 
     94                srsName: this.srsName, 
     95                schema: this.schema 
     96            }, this.formatOptions)); 
     97        } 
     98        var readNode = this.format.readNode; 
     99        this.format.readNode = function(node, obj) { 
     100            if(!this.featureNS && node.prefix == this.featurePrefix) { 
     101                this.featureNS = node.namespaceURI; 
     102                this.setNamespace("feature", this.featureNS); 
     103            } 
     104            return readNode.apply(this, arguments); 
     105        } 
     106    }, 
     107     
     108    /** 
     109     * APIMethod: destroy 
     110     * Clean up the protocol. 
     111     */ 
     112    destroy: function() { 
     113        if(this.options && !this.options.format) { 
     114            this.format.destroy(); 
     115        } 
     116        this.format = null; 
     117        OpenLayers.Protocol.prototype.destroy.apply(this); 
     118    }, 
     119 
     120    /** 
     121     * Method: createCallback 
     122     * Returns a function that applies the given public method with resp and 
     123     *     options arguments. 
     124     * 
     125     * Parameters: 
     126     * method - {Function} The method to be applied by the callback. 
     127     * response - {<OpenLayers.Protocol.Response>} The protocol response object. 
     128     * options - {Object} Options sent to the protocol method (read, create, 
     129     *     update, or delete). 
     130     */ 
     131    createCallback: function(method, response, options) { 
     132        return OpenLayers.Function.bind(function() { 
     133            method.apply(this, [response, options]); 
     134        }, this); 
     135    }, 
     136 
     137    /** 
     138     * Method: read 
     139     * Construct a request for reading new features.  Since WFS splits the 
     140     *     basic CRUD operations into GetFeature requests (for read) and 
     141     *     Transactions (for all others), this method does not make use of the 
     142     *     format's read method (that is only about reading transaction 
     143     *     responses). 
     144     */ 
     145    read: function(options) { 
     146        options = OpenLayers.Util.extend({}, options); 
     147        OpenLayers.Util.applyDefaults(options, this.options || {}); 
     148        var response = new OpenLayers.Protocol.Response({requestType: "read"}); 
     149         
     150        var data = OpenLayers.Format.XML.prototype.write.apply( 
     151            this.format, [this.format.writeNode("wfs:GetFeature", options)] 
     152        ); 
     153 
     154        response.priv = OpenLayers.Request.POST({ 
     155            url: options.url, 
     156            callback: this.createCallback(this.handleRead, response, options), 
     157            params: options.params, 
     158            headers: options.headers, 
     159            data: data 
     160        });         
     161 
     162        return response; 
     163    }, 
     164     
     165    /** 
     166     * Method: handleRead 
     167     * Deal with response from the read request. 
     168     * 
     169     * Parameters: 
     170     * response - {<OpenLayers.Protocol.Response>} The response object to pass 
     171     *     to the user callback. 
     172     * options - {Object} The user options passed to the read call. 
     173     */ 
     174    handleRead: function(response, options) { 
     175        if(options.callback) { 
     176            var request = response.priv; 
     177            if(request.status >= 200 && request.status < 300) { 
     178                // success 
     179                response.features = this.parseFeatures(request); 
     180                response.code = OpenLayers.Protocol.Response.SUCCESS; 
     181            } else { 
     182                // failure 
     183                response.code = OpenLayers.Protocol.Response.FAILURE; 
     184            } 
     185            options.callback.call(options.scope, response); 
     186        };  
     187    }, 
     188 
     189    /** 
     190     * Method: parseFeatures 
     191     * Read HTTP response body and return features 
     192     * 
     193     * Parameters: 
     194     * request - {XMLHttpRequest} The request object 
     195     * 
     196     * Returns: 
     197     * {Array({<OpenLayers.Feature.Vector>})} or 
     198     *     {<OpenLayers.Feature.Vector>} Array of features or a single feature. 
     199     */ 
     200    parseFeatures: function(request) { 
     201        var doc = request.responseXML; 
     202        if(!doc || !doc.documentElement) { 
     203            doc = request.responseText; 
     204        } 
     205        if(!doc || doc.length <= 0) { 
     206            return null; 
     207        } 
     208        return this.format.read(doc); 
     209    }, 
     210 
     211    /** 
     212     * Method: commit 
     213     * Given a list of feature, assemble a batch request for update, create, 
     214     *     and delete transactions.  A commit call on the prototype amounts 
     215     *     to writing a WFS transaction - so the write method on the format 
     216     *     is used. 
     217     * 
     218     * Parameters: 
     219     * features - {Array(<OpenLayers.Feature.Vector>} 
     220     * 
     221     * Returns: 
     222     * {<OpenLayers.Protocol.Response>} A response object with a features 
     223     *     property containing any insertIds and a priv property referencing 
     224     *     the XMLHttpRequest object. 
     225     */ 
     226    commit: function(features, options) { 
     227 
     228        options = OpenLayers.Util.extend({}, options); 
     229        OpenLayers.Util.applyDefaults(options, this.options); 
     230         
     231        var response = new OpenLayers.Protocol.Response({ 
     232            requestType: "commit", 
     233            reqFeatures: features 
     234        }); 
     235        response.priv = OpenLayers.Request.POST({ 
     236            url: options.url, 
     237            data: this.format.write(features, options), 
     238            callback: this.createCallback(this.handleCommit, response, options) 
     239        }); 
     240         
     241        return response; 
     242    }, 
     243     
     244    /** 
     245     * Method: handleCommit 
     246     * Called when the commit request returns. 
     247     *  
     248     * Parameters: 
     249     * response - {<OpenLayers.Protocol.Response>} The response object to pass 
     250     *     to the user callback. 
     251     * options - {Object} The user options passed to the commit call. 
     252     */ 
     253    handleCommit: function(response, options) { 
     254        if(options.callback) { 
     255            var request = response.priv; 
     256 
     257            // ensure that we have an xml doc 
     258            var data = request.responseXML; 
     259            if(!data || !data.documentElement) { 
     260                data = request.responseText; 
     261            } 
     262             
     263            var obj = this.format.read(data) || {}; 
     264             
     265            response.insertIds = obj.insertIds || []; 
     266            response.code = (obj.success) ? 
     267                OpenLayers.Protocol.Response.SUCCESS : 
     268                OpenLayers.Protocol.Response.FAILURE; 
     269            options.callback.call(options.scope, response); 
     270        } 
     271    }, 
     272     
     273    /** 
     274     * Method: filterDelete 
     275     * Send a request that deletes all features by their filter. 
     276     *  
     277     * Parameters: 
     278     * filter - {OpenLayers.Filter} filter 
     279     */ 
     280    filterDelete: function(filter, options) { 
     281        options = OpenLayers.Util.extend({}, options); 
     282        OpenLayers.Util.applyDefaults(options, this.options);     
     283         
     284        var response = new OpenLayers.Protocol.Response({ 
     285            requestType: "commit" 
     286        });     
     287         
     288        var root = this.format.createElementNSPlus("wfs:Transaction", { 
     289            attributes: { 
     290                service: "WFS", 
     291                version: this.version 
     292            } 
     293        }); 
     294         
     295        var deleteNode = this.format.createElementNSPlus("wfs:Delete", { 
     296            attributes: { 
     297                typeName: (options.featureNS ? this.featurePrefix + ":" : "") + 
     298                    options.featureType 
     299            } 
     300        });        
     301         
     302        if(options.featureNS) { 
     303            deleteNode.setAttribute("xmlns:" + this.featurePrefix, options.featureNS); 
     304        } 
     305        var filterNode = this.format.writeNode("ogc:Filter", filter); 
     306         
     307        deleteNode.appendChild(filterNode); 
     308         
     309        root.appendChild(deleteNode); 
     310         
     311        var data = OpenLayers.Format.XML.prototype.write.apply( 
     312            this.format, [root] 
     313        ); 
     314         
     315        return OpenLayers.Request.POST({ 
     316            url: this.url, 
     317            callback : options.callback || function(){}, 
     318            data: data 
     319        });    
     320         
     321    }, 
     322    
     323    CLASS_NAME: "OpenLayers.Protocol.WFS.v1"  
     324}); 
  • lib/OpenLayers/Protocol/WFS.js

     
     1/** 
     2 * @requires OpenLayers/Protocol.js 
     3 */ 
     4 
     5/** 
     6 * Function: OpenLayers.Protocol.WFS 
     7 * Used to create a versioned WFS protocol.  Default version is 1.0.0. 
     8 * 
     9 * Returns: 
     10 * {<OpenLayers.Protocol>} A WFS protocol of the given version. 
     11 */ 
     12OpenLayers.Protocol.WFS = function(options) { 
     13    options = OpenLayers.Util.applyDefaults( 
     14        options, OpenLayers.Protocol.WFS.DEFAULTS 
     15    ); 
     16    var cls = OpenLayers.Protocol.WFS["v"+options.version.replace(/\./g, "_")]; 
     17    if(!cls) { 
     18        throw "Unsupported WFS version: " + options.version; 
     19    } 
     20    return new cls(options); 
     21} 
     22 
     23/** 
     24 * Constant: OpenLayers.Protocol.WFS.DEFAULTS 
     25 */ 
     26OpenLayers.Protocol.WFS.DEFAULTS = { 
     27    "version": "1.0.0" 
     28}; 
  • lib/OpenLayers.js

     
    193193            "OpenLayers/Protocol/HTTP.js", 
    194194            "OpenLayers/Protocol/SQL.js", 
    195195            "OpenLayers/Protocol/SQL/Gears.js", 
     196            "OpenLayers/Protocol/WFS.js", 
     197            "OpenLayers/Protocol/WFS/v1.js", 
     198            "OpenLayers/Protocol/WFS/v1_0_0.js", 
     199            "OpenLayers/Protocol/WFS/v1_1_0.js", 
    196200            "OpenLayers/Layer/PointTrack.js", 
    197201            "OpenLayers/Layer/GML.js", 
    198202            "OpenLayers/Style.js", 
     
    223227            "OpenLayers/Format/SLD/v1.js", 
    224228            "OpenLayers/Format/SLD/v1_0_0.js", 
    225229            "OpenLayers/Format/SLD/v1.js", 
     230            "OpenLayers/Format/WFST.js", 
     231            "OpenLayers/Format/WFST/v1.js", 
     232            "OpenLayers/Format/WFST/v1_0_0.js", 
     233            "OpenLayers/Format/WFST/v1_1_0.js", 
    226234            "OpenLayers/Format/Text.js", 
    227235            "OpenLayers/Format/JSON.js", 
    228236            "OpenLayers/Format/GeoJSON.js", 
  • examples/wfs-protocol.html

     
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2    <head> 
     3        <title>OpenLayers Vector Behavior Example</title> 
     4        <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> 
     5        <link rel="stylesheet" href="style.css" type="text/css" /> 
     6        <script src="../lib/Firebug/firebug.js"></script> 
     7        <script src="../lib/OpenLayers.js"></script> 
     8        <script type="text/javascript"> 
     9            var map; 
     10         
     11            function init() { 
     12                OpenLayers.ProxyHost= "/cgi-bin/proxy.cgi?url="; 
     13                map = new OpenLayers.Map('map'); 
     14                var wms = new OpenLayers.Layer.WMS( 
     15                    "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", 
     16                    {layers: 'basic'} 
     17                ); 
     18 
     19                var layer = new OpenLayers.Layer.Vector("WFS", { 
     20                    strategies: [new OpenLayers.Strategy.BBOX()], 
     21                    protocol: new OpenLayers.Protocol.WFS({ 
     22                        url:  "http://publicus.opengeo.org/geoserver/wfs", 
     23                        featureType: "tasmania_roads", 
     24                        featureNS: "http://www.openplans.org/topp" 
     25                    }), 
     26                }); 
     27 
     28                map.addLayers([wms, layer]); 
     29                map.setCenter(new OpenLayers.LonLat(146.7, -41.8), 6); 
     30            } 
     31        </script> 
     32    </head> 
     33    <body onload="init()"> 
     34        <h1 id="title">Vector Behavior Example</h1> 
     35        <p id="shortdesc"> 
     36            Uses a BBOX strategy, WFS protocol, and GML format. 
     37        </p> 
     38        <div id="map" class="smallmap"></div> 
     39        <div id="docs"> 
     40            <p>The vector layer shown uses the BBOX strategy, the WFS protocol, 
     41            and the GML format.  The BBOX strategy fetches features within a 
     42            bounding box.  When the map bounds invalidate the data bounds, 
     43            another request is triggered.  The WFS protocol gets features 
     44            through a WFS request.  The GML format is used to serialize 
     45            features.</p> 
     46        </div> 
     47    </body> 
     48</html> 
  • examples/wfs-protocol-transactions.html

     
     1<html> 
     2  <head> 
     3    <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> 
     4    <link rel="stylesheet" href="style.css" type="text/css" /> 
     5    <script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAbs-AXzXTiUE-iwvdTFv0TRTJUVlep-V_kcHWKMoAW7P0KYzoHRSbTZ4mBasJApBqjm2Vnrs7dKOYJg'></script> 
     6    <script src="../lib/OpenLayers.js"></script> 
     7    <style> 
     8        .customEditingToolbar { 
     9            float: right; 
     10            right: 0px; 
     11            height: 30px;  
     12            width: 200px; 
     13        } 
     14        .customEditingToolbar div { 
     15            float: right; 
     16            margin: 5px; 
     17            width: 24px; 
     18            height: 24px; 
     19        } 
     20        .olControlNavigationItemActive {  
     21            background-image: url("../theme/default/img/editing_tool_bar.png"); 
     22            background-repeat: no-repeat; 
     23            background-position: -103px -23px;  
     24        } 
     25        .olControlNavigationItemInactive {  
     26            background-image: url("../theme/default/img/editing_tool_bar.png"); 
     27            background-repeat: no-repeat; 
     28            background-position: -103px -0px;  
     29        } 
     30        .olControlDrawFeaturePolygonItemInactive {  
     31            background-image: url("../theme/default/img/editing_tool_bar.png"); 
     32            background-repeat: no-repeat; 
     33            background-position: -26px 0px;  
     34        } 
     35        .olControlDrawFeaturePolygonItemActive {  
     36            background-image: url("../theme/default/img/editing_tool_bar.png"); 
     37            background-repeat: no-repeat; 
     38            background-position: -26px -23px ;                                                                    
     39        } 
     40        .olControlModifyFeatureItemActive {  
     41            background-image: url(../theme/default/img/move_feature_on.png); 
     42            background-repeat: no-repeat; 
     43            background-position: 0px 1px; 
     44        } 
     45        .olControlModifyFeatureItemInactive {  
     46            background-image: url(../theme/default/img/move_feature_off.png); 
     47            background-repeat: no-repeat; 
     48            background-position: 0px 1px; 
     49        } 
     50        .olControlDeleteFeatureItemActive {  
     51            background-image: url(../theme/default/img/remove_point_on.png); 
     52            background-repeat: no-repeat; 
     53            background-position: 0px 1px; 
     54        } 
     55        .olControlDeleteFeatureItemInactive {  
     56            background-image: url(../theme/default/img/remove_point_off.png); 
     57            background-repeat: no-repeat; 
     58            background-position: 0px 1px; 
     59        } 
     60    </style> 
     61    <script type="text/javascript"> 
     62        var map, wfs; 
     63         
     64        var DeleteFeature = OpenLayers.Class(OpenLayers.Control, { 
     65            initialize: function(layer, options) { 
     66                OpenLayers.Control.prototype.initialize.apply(this, [options]); 
     67                this.layer = layer; 
     68                this.handler = new OpenLayers.Handler.Feature( 
     69                    this, layer, {click: this.clickFeature} 
     70                ); 
     71            }, 
     72            clickFeature: function(feature) { 
     73                // if feature doesn't have a fid, destroy it 
     74                if(feature.fid == undefined) { 
     75                    this.layer.destroyFeatures([feature]); 
     76                } else { 
     77                    feature.state = OpenLayers.State.DELETE; 
     78                    this.layer.events.triggerEvent("afterfeaturemodified",  
     79                                                   {feature: feature}); 
     80                    feature.renderIntent = "select"; 
     81                    this.layer.drawFeature(feature); 
     82                } 
     83            }, 
     84            setMap: function(map) { 
     85                this.handler.setMap(map); 
     86                OpenLayers.Control.prototype.setMap.apply(this, arguments); 
     87            }, 
     88            CLASS_NAME: "OpenLayers.Control.DeleteFeature" 
     89        }); 
     90 
     91        function init() { 
     92            OpenLayers.ProxyHost= "/cgi-bin/proxy.cgi?url="; 
     93            map = new OpenLayers.Map('map', { 
     94                projection: new OpenLayers.Projection("EPSG:900913"), 
     95                displayProjection: new OpenLayers.Projection("EPSG:4326"), 
     96                units: "m", 
     97                maxResolution: 156543.0339, 
     98                maxExtent: new OpenLayers.Bounds( 
     99                    -11593508, 5509847, -11505759, 5557774 
     100                ), 
     101                controls: [ 
     102                    new OpenLayers.Control.PanZoom() 
     103                ] 
     104            }); 
     105            var gphy = new OpenLayers.Layer.Google( 
     106                "Google Physical", 
     107                {type: G_PHYSICAL_MAP, sphericalMercator: true} 
     108            ); 
     109 
     110            var saveStrategy = new OpenLayers.Strategy.Save(); 
     111 
     112            wfs = new OpenLayers.Layer.Vector("Editable Features", { 
     113                strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy], 
     114                projection: new OpenLayers.Projection("EPSG:4326"), 
     115                protocol: new OpenLayers.Protocol.WFS({ 
     116                    version: "1.1.0", 
     117                    srsName: "EPSG:4326", 
     118                    url: "http://demo.opengeo.org/geoserver/wfs?strict=true", 
     119                    featureNS :  "http://opengeo.org", 
     120                    featureType: "restricted", 
     121                    geometryName: "the_geom", 
     122                    schema: "http://demo.opengeo.org/geoserver/wfs/DescribeFeatureType?version=1.1.0&typename=opengeo:restricted" 
     123                }) 
     124            });  
     125            
     126            map.addLayers([gphy, wfs]); 
     127 
     128            var panel = new OpenLayers.Control.Panel( 
     129                {'displayClass': 'customEditingToolbar'} 
     130            ); 
     131             
     132            var navigate = new OpenLayers.Control.Navigation({ 
     133                title: "Pan Map", 
     134            }); 
     135             
     136            var draw = new OpenLayers.Control.DrawFeature( 
     137                wfs, OpenLayers.Handler.Polygon, 
     138                { 
     139                    title: "Draw Feature", 
     140                    displayClass: "olControlDrawFeaturePolygon", 
     141                    handlerOptions: {multi: true} 
     142                } 
     143            ); 
     144             
     145            var edit = new OpenLayers.Control.ModifyFeature(wfs, { 
     146                title: "Modify Feature", 
     147                displayClass: "olControlModifyFeature", 
     148            }); 
     149 
     150            var del = new DeleteFeature(wfs, {title: "Delete Feature"}); 
     151            
     152            var save = new OpenLayers.Control.Button({ 
     153                title: "Save Changes", 
     154                trigger: function() { 
     155                    if(edit.feature) { 
     156                        edit.selectControl.unselectAll(); 
     157                    } 
     158                    saveStrategy.save(); 
     159                }, 
     160                displayClass: "olControlSaveFeatures" 
     161            }); 
     162 
     163            panel.addControls([navigate, save, del, edit, draw]); 
     164            panel.defaultControl = navigate; 
     165            map.addControl(panel); 
     166            map.zoomToMaxExtent(); 
     167        } 
     168         
     169    </script> 
     170    </head> 
     171    <body onload="init()"> 
     172     
     173    <h1 id="title">WFS Transaction Example</h1> 
     174     
     175    <div id="tags"> 
     176    </div> 
     177    <p id="shortdesc"> 
     178        Shows the use of the WFS Transactions (WFS-T). 
     179    </p> 
     180     
     181    <div id="map" class="smallmap"></div> 
     182 
     183    <div id="docs"> 
     184        <p>The WFS protocol allows for creation of new features and reading, 
     185        updating, or deleting of existing features.</p> 
     186        <p>Use the tools to create, modify, and delete (in order from left 
     187        to right) features.  Use the save tool (picture of a disk) to 
     188        save your changes.  Use the navigation tool (hand) to stop editing 
     189        and use the mouse for map navigation.</p> 
     190    </div> 
     191 
     192 
     193 
     194</body> 
     195</html> 
     196 
     197