diff --git a/lib/OpenLayers/Protocol/WFS.js b/lib/OpenLayers/Protocol/WFS.js index 73b29d62ea..87956a6c8e 100644 --- a/lib/OpenLayers/Protocol/WFS.js +++ b/lib/OpenLayers/Protocol/WFS.js @@ -20,6 +20,44 @@ OpenLayers.Protocol.WFS = function(options) { return new cls(options); }; +/** + * Function: OpenLayers.Protocol.WFS.fromWMSLayer + * Convenience function to create a WFS protocol from a WMS layer. This makes + * the assumption that a WFS requests can be issued at the same URL as + * WMS requests and that a WFS featureType exists with the same name as the + * WMS layer. + * + * This function is designed to auto-configure , , + * and for WFS 1.1.0. Note that + * srsName matching with the WMS layer will not work with WFS 1.0.0.. + * + * Parameters: + * layer - {} WMS layer that has a matching WFS + * FeatureType at the same server url with the same typename. + * options - {Object} Default properties to be set on the protocol. + * + */ +OpenLayers.Protocol.WFS.fromWMSLayer = function(layer, options) { + var typeName, featurePrefix; + var param = layer.params["LAYERS"]; + var parts = (param instanceof Array ? param[0] : param).split(":"); + if(parts.length > 1) { + featurePrefix = parts[0]; + } + typeName = parts.pop(); + var protocolOptions = { + url: layer.url, + featureType: typeName, + featurePrefix: featurePrefix, + srsName: layer.projection && layer.projection.getCode() || + layer.map && layer.map.getProjectionObject().getCode(), + version: "1.1.0" + }; + return new OpenLayers.Protocol.WFS(OpenLayers.Util.applyDefaults( + options, protocolOptions + )); +}; + /** * Constant: OpenLayers.Protocol.WFS.DEFAULTS */ diff --git a/tests/Protocol/WFS.html b/tests/Protocol/WFS.html index 5da70c4b36..636117e90d 100644 --- a/tests/Protocol/WFS.html +++ b/tests/Protocol/WFS.html @@ -187,6 +187,30 @@ t.eq(aborted, true, "abort called on response.priv"); } + + function test_fromWMSLayer(t) { + t.plan(8); + var map = new OpenLayers.Map("map", { + projection: "EPSG:1234" + }); + var layer = new OpenLayers.Layer.WMS("foo", "htttp://foo/ows", + {layers: "topp:states"} + ); + map.addLayer(layer); + var protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer); + t.eq(protocol.url, "htttp://foo/ows", "url taken from wms layer"); + t.eq(protocol.featurePrefix, "topp", "feature prefix correctly extracted"); + t.eq(protocol.featureType, "states", "typeName correctly extracted"); + t.eq(protocol.srsName, "EPSG:1234", "srsName set correctly"); + t.eq(protocol.version, "1.1.0", "version set correctly"); + + layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"]; + layer.projection = new OpenLayers.Projection("EPSG:900913"); + protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer); + t.eq(protocol.featurePrefix, "topp", "featurePrefix from layer param array"); + t.eq(protocol.featureType, "street_centerline", "first layer from layer param array as featureType"); + t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred"); + } function readXML(id) { var xml = document.getElementById(id).firstChild.nodeValue;