Adding a WFS protocol. This allows for creating, reading, updating, and deleting features via WFS. Huge thanks to ahocevar for the test writing and review. r=ahocevar (closes #1648)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@8868 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="../../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
function test_initialize(t) {
|
||||
t.plan(2);
|
||||
|
||||
var protocol = new OpenLayers.Protocol.WFS({
|
||||
url: "http://some.url.org",
|
||||
featureNS: "http://namespace.org",
|
||||
featureType: "type"
|
||||
});
|
||||
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_0_0,
|
||||
"initialize returns instance of default versioned protocol")
|
||||
|
||||
var protocol = new OpenLayers.Protocol.WFS({
|
||||
url: "http://some.url.org",
|
||||
featureNS: "http://namespace.org",
|
||||
featureType: "type",
|
||||
version: "1.1.0"
|
||||
});
|
||||
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
|
||||
"initialize returns instance of custom versioned protocol")
|
||||
}
|
||||
|
||||
function test_read(t) {
|
||||
t.plan(6);
|
||||
|
||||
var protocol = new OpenLayers.Protocol.WFS({
|
||||
url: "http://some.url.org",
|
||||
featureNS: "http://namespace.org",
|
||||
featureType: "type",
|
||||
parseFeatures: function(request) {
|
||||
t.eq(request.responseText, "foo", "parseFeatures called properly");
|
||||
return "foo";
|
||||
}
|
||||
});
|
||||
|
||||
var _POST = OpenLayers.Request.POST;
|
||||
|
||||
var expected, status;
|
||||
OpenLayers.Request.POST = function(obj) {
|
||||
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
|
||||
obj.status = status;
|
||||
obj.responseText = "foo";
|
||||
obj.options = {};
|
||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||
return obj;
|
||||
};
|
||||
|
||||
expected = readXML("GetFeature_1");
|
||||
status = 200;
|
||||
var response = protocol.read({callback: function(response) {
|
||||
t.eq(response.features, "foo", "user callback properly called with features");
|
||||
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
|
||||
}});
|
||||
|
||||
options = {
|
||||
maxFeatures: 10,
|
||||
featureType: 'type2',
|
||||
srsName: 'EPSG:900913',
|
||||
featureNS: 'htttp://alternative.namespace.org',
|
||||
callback: function(response) {
|
||||
t.eq(response.code, OpenLayers.Protocol.Response.FAILURE, "failure reported properly to user callback");
|
||||
}
|
||||
};
|
||||
expected = readXML("GetFeature_2");
|
||||
status = 400;
|
||||
var response = protocol.read(options);
|
||||
|
||||
OpenLayers.Request.POST = _POST;
|
||||
}
|
||||
|
||||
function test_commit(t){
|
||||
t.plan(4);
|
||||
|
||||
var url = "http://some.url.org";
|
||||
var protocol = new OpenLayers.Protocol.WFS({
|
||||
url: url,
|
||||
featureNS: "http://namespace.org",
|
||||
featureType: "type"
|
||||
});
|
||||
protocol.format.read = function(data) {
|
||||
t.eq(data, "foo", "callback called with correct argument");
|
||||
return {
|
||||
insertIds: new Array(3),
|
||||
success: true
|
||||
}
|
||||
};
|
||||
|
||||
var _POST = OpenLayers.Request.POST;
|
||||
|
||||
var expected;
|
||||
OpenLayers.Request.POST = function(obj) {
|
||||
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "Transaction XML with Insert, Update and Delete created correctly");
|
||||
obj.responseText = "foo";
|
||||
obj.options = {};
|
||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||
return obj;
|
||||
};
|
||||
|
||||
var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
|
||||
featureDelete.fid = "fid.37";
|
||||
featureDelete.state = OpenLayers.State.DELETE;
|
||||
featureDelete.layer = {
|
||||
projection: {
|
||||
getCode : function(){
|
||||
return "EPSG:4326";
|
||||
}
|
||||
}
|
||||
}
|
||||
var featureInsert = featureDelete.clone();
|
||||
featureInsert.state = OpenLayers.State.INSERT;
|
||||
var featureModify = featureDelete.clone();
|
||||
featureModify.fid = "fid.37";
|
||||
featureModify.state = OpenLayers.State.UPDATE;
|
||||
|
||||
options = {
|
||||
featureNS: "http://some.namespace.org",
|
||||
featureType: "type",
|
||||
callback: function(response) {
|
||||
t.eq(response.insertIds.length, 3, "correct response passed to user callback");
|
||||
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
|
||||
}
|
||||
}
|
||||
|
||||
expected = readXML("commit");
|
||||
var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
|
||||
|
||||
OpenLayers.Request.POST = _POST;
|
||||
|
||||
}
|
||||
|
||||
function test_filterDelete(t) {
|
||||
t.plan(2)
|
||||
|
||||
var url = "http://some.url.org";
|
||||
var protocol = new OpenLayers.Protocol.WFS({
|
||||
url: url,
|
||||
featureNS: "http://namespace.org",
|
||||
featureType: "type"
|
||||
});
|
||||
|
||||
var filter = new OpenLayers.Filter.Spatial({
|
||||
type: OpenLayers.Filter.Spatial.BBOX,
|
||||
value: new OpenLayers.Bounds(-5, -5, 5, 5)
|
||||
});
|
||||
|
||||
var _POST = OpenLayers.Request.POST;
|
||||
|
||||
var expected = readXML("filter_delete");
|
||||
OpenLayers.Request.POST = function(obj) {
|
||||
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "request data correct");
|
||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||
return obj;
|
||||
};
|
||||
|
||||
var response = protocol.filterDelete(filter, {
|
||||
callback: function() {
|
||||
t.ok("user callback function called");
|
||||
}
|
||||
});
|
||||
|
||||
OpenLayers.Request.POST = _POST;
|
||||
}
|
||||
|
||||
function readXML(id) {
|
||||
var xml = document.getElementById(id).firstChild.nodeValue;
|
||||
return new OpenLayers.Format.XML().read(xml).documentElement;
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map" style="width:512px; height:256px"> </div>
|
||||
<div id="GetFeature_1"><!--
|
||||
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<wfs:Query typeName="feature:type" xmlns:feature="http://namespace.org"/>
|
||||
</wfs:GetFeature>
|
||||
--></div>
|
||||
<div id="GetFeature_2"><!--
|
||||
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="10" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<wfs:Query typeName="feature:type2" xmlns:feature="htttp://alternative.namespace.org"/>
|
||||
</wfs:GetFeature>
|
||||
--></div>
|
||||
<div id="commit"><!--
|
||||
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<wfs:Insert>
|
||||
<feature:type xmlns:feature="http://namespace.org">
|
||||
<feature:the_geom>
|
||||
<gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
|
||||
<gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates>
|
||||
</gml:Point>
|
||||
</feature:the_geom>
|
||||
<feature:has>cheeseburger</feature:has>
|
||||
</feature:type>
|
||||
</wfs:Insert>
|
||||
<wfs:Update typeName="feature:type" xmlns:feature="http://namespace.org">
|
||||
<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>cheeseburger</wfs:Value>
|
||||
</wfs:Property>
|
||||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
|
||||
<ogc:FeatureId fid="fid.37"/>
|
||||
</ogc:Filter>
|
||||
</wfs:Update>
|
||||
<wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
|
||||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
|
||||
<ogc:FeatureId fid="fid.37"/>
|
||||
</ogc:Filter>
|
||||
</wfs:Delete>
|
||||
</wfs:Transaction>
|
||||
--></div>
|
||||
<div id="filter_delete"><!--
|
||||
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0">
|
||||
<wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
|
||||
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
|
||||
<ogc:BBOX>
|
||||
<ogc:PropertyName/>
|
||||
<gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
|
||||
<gml:coordinates decimal="." cs="," ts=" ">-5,-5 5,5</gml:coordinates>
|
||||
</gml:Box>
|
||||
</ogc:BBOX>
|
||||
</ogc:Filter>
|
||||
</wfs:Delete>
|
||||
</wfs:Transaction>
|
||||
--></div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user