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:
Tim Schaub
2009-02-18 00:07:43 +00:00
parent 8274b648e8
commit 55f40e224a
17 changed files with 1942 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(2);
var format = new OpenLayers.Format.WFST();
t.ok(format instanceof OpenLayers.Format.WFST.v1_0_0, "constructor returns instance with default versioned format");
format = new OpenLayers.Format.WFST({
version: "1.1.0"
});
t.ok(format instanceof OpenLayers.Format.WFST.v1_1_0, "constructor returns instance with custom versioned format");
}
</script>
</head>
<body>
<div id="map" style="width:512px; height:256px"> </div>
</body>
</html>
+160
View File
@@ -0,0 +1,160 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_read(t) {
t.plan(1);
var data = readXML("FeatureCollection");
var format = new OpenLayers.Format.WFST({
featureNS: "http://www.openplans.org/topp",
featureType: "states"
});
var features = format.read(data);
t.eq(features.length, 1, "number of features read from FeatureCollection is correct");
}
function test_write(t) {
var format = new OpenLayers.Format.WFST({
featureNS: "http://www.openplans.org/topp",
featureType: "states",
featurePrefix: "topp",
geometryName: "the_geom"
});
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(1,2),
{foo: "bar"});
var insertFeature = feature.clone();
insertFeature.state = OpenLayers.State.INSERT;
var updateFeature = feature.clone();
updateFeature.fid = "fid.42";
updateFeature.state = OpenLayers.State.UPDATE;
var deleteFeature = feature.clone();
deleteFeature.state = OpenLayers.State.DELETE;
deleteFeature.fid = "fid.37";
t.plan(5);
var snippets = {
"GetFeature": {maxFeatures: 1},
"Transaction": null,
"Insert": insertFeature,
"Update": updateFeature,
"Delete": deleteFeature
}
var arg;
for(var snippet in snippets) {
arg = snippets[snippet]
var expected = readXML(snippet);
var got = format.writers["wfs"][snippet].apply(format, [arg]);
t.xml_eq(got, expected, snippet + " request created correctly");
}
}
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="FeatureCollection"><!--
<wfs:FeatureCollection xmlns:wfs="http://www.opengis.net/wfs" xmlns:topp="http://www.openplans.org/topp" xmlns:gml="http://www.opengis.net/gml">
<gml:featureMember>
<topp:states fid="states.3">
<topp:the_geom>
<gml:MultiPolygon srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
<gml:polygonMember>
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates decimal="." cs="," ts=" ">-75.70742,38.557476 -75.71106,38.649551 -75.724937,38.83017 -75.752922,39.141548 -75.761658,39.247753 -75.764664,39.295849 -75.772697,39.383007 -75.791435,39.723755 -75.775269,39.724442 -75.745934,39.774818 -75.695114,39.820347 -75.644341,39.838196 -75.583794,39.840008 -75.470345,39.826435 -75.42083,39.79887 -75.412117,39.789658 -75.428009,39.77813 -75.460754,39.763248 -75.475128,39.741718 -75.476334,39.719971 -75.489639,39.714745 -75.610725,39.612793 -75.562996,39.566723 -75.590187,39.463768 -75.515572,39.36694 -75.402481,39.257637 -75.397728,39.073036 -75.324852,39.012386 -75.307899,38.945911 -75.190941,38.80867 -75.083138,38.799812 -75.045998,38.44949 -75.068298,38.449963 -75.093094,38.450451 -75.350204,38.455208 -75.69915,38.463066 -75.70742,38.557476</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</gml:polygonMember>
</gml:MultiPolygon>
</topp:the_geom>
<topp:STATE_NAME>Delaware</topp:STATE_NAME>
<topp:STATE_FIPS>10</topp:STATE_FIPS>
<topp:SUB_REGION>S Atl</topp:SUB_REGION>
<topp:STATE_ABBR>DE</topp:STATE_ABBR>
<topp:LAND_KM>5062.456</topp:LAND_KM>
<topp:WATER_KM>1385.022</topp:WATER_KM>
<topp:PERSONS>666168.0</topp:PERSONS>
<topp:FAMILIES>175867.0</topp:FAMILIES>
<topp:HOUSHOLD>247497.0</topp:HOUSHOLD>
<topp:MALE>322968.0</topp:MALE>
<topp:FEMALE>343200.0</topp:FEMALE>
<topp:WORKERS>247566.0</topp:WORKERS>
<topp:DRVALONE>258087.0</topp:DRVALONE>
<topp:CARPOOL>42968.0</topp:CARPOOL>
<topp:PUBTRANS>8069.0</topp:PUBTRANS>
<topp:EMPLOYED>335147.0</topp:EMPLOYED>
<topp:UNEMPLOY>13945.0</topp:UNEMPLOY>
<topp:SERVICE>87973.0</topp:SERVICE>
<topp:MANUAL>44140.0</topp:MANUAL>
<topp:P_MALE>0.485</topp:P_MALE>
<topp:P_FEMALE>0.515</topp:P_FEMALE>
<topp:SAMP_POP>102776.0</topp:SAMP_POP>
</topp:states>
</gml:featureMember>
</wfs:FeatureCollection>
--></div>
<div id="GetFeature"><!--
<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="1" 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="topp:states" xmlns:topp="http://www.openplans.org/topp"/>
</wfs:GetFeature>
--></div>
<div id="Transaction"><!--
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0"/>
--></div>
<div id="Insert"><!--
<wfs:Insert xmlns:wfs="http://www.opengis.net/wfs">
<feature:states xmlns:feature="http://www.openplans.org/topp">
<feature:the_geom>
<gml:Point xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">1,2</gml:coordinates>
</gml:Point>
</feature:the_geom>
<feature:foo>bar</feature:foo>
</feature:states>
</wfs:Insert>
--></div>
<div id="Update"><!--
<wfs:Update xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
<wfs:Property>
<wfs:Name>the_geom</wfs:Name>
<wfs:Value>
<gml:Point xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">1,2</gml:coordinates>
</gml:Point>
</wfs:Value>
</wfs:Property>
<wfs:Property>
<wfs:Name>foo</wfs:Name>
<wfs:Value>bar</wfs:Value>
</wfs:Property>
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:FeatureId fid="fid.42"/>
</ogc:Filter>
</wfs:Update>
--></div>
<div id="Delete"><!--
<wfs:Delete xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:FeatureId fid="fid.37"/>
</ogc:Filter>
</wfs:Delete>
--></div>
</body>
</html>
+87
View File
@@ -0,0 +1,87 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(1);
var format = new OpenLayers.Format.WFST.v1_0_0({});
t.ok(format instanceof OpenLayers.Format.WFST.v1_0_0, "constructor returns instance");
}
function test_read(t) {
t.plan(2);
var data = readXML("Transaction_Response");
var format = new OpenLayers.Format.WFST.v1_0_0({
featureNS: "http://www.openplans.org/topp",
featureType: "states"
});
var result = format.read(data);
t.eq(result.insertIds[0], "none", "InsertIds read correctly");
t.eq(result.success, true, "Success read correctly");
}
function test_write(t) {
var format = new OpenLayers.Format.WFST.v1_0_0({
featureNS: "http://www.openplans.org/topp",
featureType: "states",
featurePrefix: "topp",
geometryName: "the_geom"
});
t.plan(1);
var snippets = {
"Query": {
filter: new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: new OpenLayers.Bounds (1,2,3,4)
})}
}
var arg;
for(var snippet in snippets) {
arg = snippets[snippet]
var expected = readXML(snippet);
var got = format.writers["wfs"][snippet].apply(format, [arg]);
t.xml_eq(got, expected, snippet + " request created correctly");
}
}
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="Transaction_Response"><!--
<wfs:WFS_TransactionResponse version="1.0.0" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc">
<wfs:InsertResult>
<ogc:FeatureId fid="none"/>
</wfs:InsertResult>
<wfs:TransactionResult>
<wfs:Status>
<wfs:SUCCESS/>
</wfs:Status>
</wfs:TransactionResult>
</wfs:WFS_TransactionResponse>
--></div>
<div id="Query"><!--
<wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:BBOX>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:Box xmlns:gml="http://www.opengis.net/gml">
<gml:coordinates decimal="." cs="," ts=" ">1,2 3,4</gml:coordinates>
</gml:Box>
</ogc:BBOX>
</ogc:Filter>
</wfs:Query>
--></div>
</body>
</html>
+91
View File
@@ -0,0 +1,91 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(1);
var format = new OpenLayers.Format.WFST.v1_1_0({});
t.ok(format instanceof OpenLayers.Format.WFST.v1_1_0, "constructor returns instance");
}
function test_read(t) {
t.plan(2);
var data = readXML("TransactionResponse");
var format = new OpenLayers.Format.WFST.v1_1_0({
featureNS: "http://www.openplans.org/topp",
featureType: "states"
});
var result = format.read(data);
t.eq(result.insertIds[0], "none", "InsertIds read correctly");
t.eq(result.success, true, "Success read correctly");
}
function test_write(t) {
var format = new OpenLayers.Format.WFST.v1_1_0({
featureNS: "http://www.openplans.org/topp",
featureType: "states",
featurePrefix: "topp",
geometryName: "the_geom"
});
t.plan(1);
var snippets = {
"Query": {
filter: new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: new OpenLayers.Bounds (1,2,3,4)
})}
}
var arg;
for(var snippet in snippets) {
arg = snippets[snippet]
var expected = readXML(snippet);
var got = format.writers["wfs"][snippet].apply(format, [arg]);
t.xml_eq(got, expected, snippet + " request created correctly");
}
}
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="TransactionResponse"><!--
<wfs:TransactionResponse version="1.1.0" xmlns:ogc="http://www.opengis.net/ogc" xmlns:tiger="http://www.census.gov" xmlns:wfs="http://www.opengis.net/wfs" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink">
<wfs:TransactionSummary>
<wfs:totalInserted>0</wfs:totalInserted>
<wfs:totalUpdated>1</wfs:totalUpdated>
<wfs:totalDeleted>0</wfs:totalDeleted>
</wfs:TransactionSummary>
<wfs:TransactionResults/>
<wfs:InsertResults>
<wfs:Feature>
<ogc:FeatureId fid="none"/>
</wfs:Feature>
</wfs:InsertResults>
</wfs:TransactionResponse>
--></div>
<div id="Query"><!--
<wfs:Query xmlns:wfs="http://www.opengis.net/wfs" typeName="topp:states" xmlns:topp="http://www.openplans.org/topp">
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:BBOX>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:Envelope xmlns:gml="http://www.opengis.net/gml">
<gml:lowerCorner>1 2</gml:lowerCorner>
<gml:upperCorner>3 4</gml:upperCorner>
</gml:Envelope>
</ogc:BBOX>
</ogc:Filter>
</wfs:Query>
--></div>
</body>
</html>
+238
View File
@@ -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>
+5
View File
@@ -55,6 +55,10 @@
<li>Format/Filter/v1_0_0.html</li>
<li>Format/Filter/v1_1_0.html</li>
<li>Format/WFSDescribeFeatureType.html</li>
<li>Format/WFST.html</li>
<li>Format/WFST/v1.html</li>
<li>Format/WFST/v1_0_0.html</li>
<li>Format/WFST/v1_1_0.html</li>
<li>Format/WKT.html</li>
<li>Format/WMC.html</li>
<li>Format/WMC/v1_1_0.html</li>
@@ -121,6 +125,7 @@
<li>Protocol/HTTP.html</li>
<li>Protocol/SQL.html</li>
<li>Protocol/SQL/Gears.html</li>
<li>Protocol/WFS.html</li>
<li>Renderer.html</li>
<li>Renderer/Canvas.html</li>
<li>Renderer/Elements.html</li>