allow WFS protocol to use a different outputFormat, for instance json, patch by rdewit, r=me (closes #2107)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@9725 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -181,6 +181,7 @@ OpenLayers.Format.WFST.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
|
|||||||
attributes: {
|
attributes: {
|
||||||
service: "WFS",
|
service: "WFS",
|
||||||
version: this.version,
|
version: this.version,
|
||||||
|
outputFormat: options && options.outputFormat,
|
||||||
maxFeatures: options && options.maxFeatures,
|
maxFeatures: options && options.maxFeatures,
|
||||||
"xsi:schemaLocation": this.schemaLocationAttr(options)
|
"xsi:schemaLocation": this.schemaLocationAttr(options)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,15 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
|
|||||||
* this property can be used to extend the default format options.
|
* this property can be used to extend the default format options.
|
||||||
*/
|
*/
|
||||||
formatOptions: null,
|
formatOptions: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property: readFormat
|
||||||
|
* {<OpenLayers.Format>} For WFS requests it is possible to get a
|
||||||
|
* different output format than GML. In that case, we cannot parse
|
||||||
|
* the response with the default format (WFST) and we need a different
|
||||||
|
* format for reading.
|
||||||
|
*/
|
||||||
|
readFormat: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor: OpenLayers.Protocol.WFS
|
* Constructor: OpenLayers.Protocol.WFS
|
||||||
@@ -105,7 +114,7 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
|
|||||||
this.setNamespace("feature", this.featureNS);
|
this.setNamespace("feature", this.featureNS);
|
||||||
}
|
}
|
||||||
return readNode.apply(this, arguments);
|
return readNode.apply(this, arguments);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -188,7 +197,7 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
|
|||||||
response.code = OpenLayers.Protocol.Response.FAILURE;
|
response.code = OpenLayers.Protocol.Response.FAILURE;
|
||||||
}
|
}
|
||||||
options.callback.call(options.scope, response);
|
options.callback.call(options.scope, response);
|
||||||
};
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -210,7 +219,8 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, {
|
|||||||
if(!doc || doc.length <= 0) {
|
if(!doc || doc.length <= 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return this.format.read(doc);
|
return (this.readFormat !== null) ? this.readFormat.read(doc) :
|
||||||
|
this.format.read(doc);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+12
-12
@@ -12,24 +12,24 @@
|
|||||||
featureType: "states"
|
featureType: "states"
|
||||||
});
|
});
|
||||||
var features = format.read(data);
|
var features = format.read(data);
|
||||||
|
|
||||||
t.eq(features.length, 1, "number of features read from FeatureCollection is correct");
|
t.eq(features.length, 1, "number of features read from FeatureCollection is correct");
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_write(t) {
|
function test_write(t) {
|
||||||
|
|
||||||
var format = new OpenLayers.Format.WFST({
|
var format = new OpenLayers.Format.WFST({
|
||||||
featureNS: "http://www.openplans.org/topp",
|
featureNS: "http://www.openplans.org/topp",
|
||||||
featureType: "states",
|
featureType: "states",
|
||||||
featurePrefix: "topp",
|
featurePrefix: "topp",
|
||||||
geometryName: "the_geom"
|
geometryName: "the_geom"
|
||||||
});
|
});
|
||||||
|
|
||||||
var feature = new OpenLayers.Feature.Vector(
|
var feature = new OpenLayers.Feature.Vector(
|
||||||
new OpenLayers.Geometry.Point(1,2),
|
new OpenLayers.Geometry.Point(1,2),
|
||||||
{foo: "bar"}
|
{foo: "bar"}
|
||||||
);
|
);
|
||||||
|
|
||||||
var insertFeature = feature.clone();
|
var insertFeature = feature.clone();
|
||||||
// null value does not show up in insert
|
// null value does not show up in insert
|
||||||
insertFeature.attributes.nul = null;
|
insertFeature.attributes.nul = null;
|
||||||
@@ -44,28 +44,28 @@
|
|||||||
var deleteFeature = feature.clone();
|
var deleteFeature = feature.clone();
|
||||||
deleteFeature.state = OpenLayers.State.DELETE;
|
deleteFeature.state = OpenLayers.State.DELETE;
|
||||||
deleteFeature.fid = "fid.37";
|
deleteFeature.fid = "fid.37";
|
||||||
|
|
||||||
t.plan(5);
|
t.plan(5);
|
||||||
var snippets = {
|
var snippets = {
|
||||||
"GetFeature": {maxFeatures: 1},
|
"GetFeature": {maxFeatures: 1, outputFormat: 'json'},
|
||||||
"Transaction": null,
|
"Transaction": null,
|
||||||
"Insert": insertFeature,
|
"Insert": insertFeature,
|
||||||
"Update": updateFeature,
|
"Update": updateFeature,
|
||||||
"Delete": deleteFeature
|
"Delete": deleteFeature
|
||||||
}
|
}
|
||||||
|
|
||||||
var arg;
|
var arg;
|
||||||
for(var snippet in snippets) {
|
for(var snippet in snippets) {
|
||||||
arg = snippets[snippet]
|
arg = snippets[snippet]
|
||||||
var expected = readXML(snippet);
|
var expected = readXML(snippet);
|
||||||
var got = format.writers["wfs"][snippet].apply(format, [arg]);
|
var got = format.writers["wfs"][snippet].apply(format, [arg]);
|
||||||
t.xml_eq(got, expected, snippet + " request created correctly");
|
t.xml_eq(got, expected, snippet + " request created correctly");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function readXML(id) {
|
function readXML(id) {
|
||||||
var xml = document.getElementById(id).firstChild.nodeValue;
|
var xml = document.getElementById(id).firstChild.nodeValue;
|
||||||
return new OpenLayers.Format.XML().read(xml).documentElement;
|
return new OpenLayers.Format.XML().read(xml).documentElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -118,7 +118,7 @@
|
|||||||
--></div>
|
--></div>
|
||||||
|
|
||||||
<div id="GetFeature"><!--
|
<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:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" outputFormat="json" 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:Query typeName="topp:states" xmlns:topp="http://www.openplans.org/topp"/>
|
||||||
</wfs:GetFeature>
|
</wfs:GetFeature>
|
||||||
--></div>
|
--></div>
|
||||||
|
|||||||
+37
-20
@@ -23,7 +23,7 @@
|
|||||||
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
|
t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
|
||||||
"initialize returns instance of custom versioned protocol")
|
"initialize returns instance of custom versioned protocol")
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_read(t) {
|
function test_read(t) {
|
||||||
t.plan(6);
|
t.plan(6);
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
var _POST = OpenLayers.Request.POST;
|
var _POST = OpenLayers.Request.POST;
|
||||||
|
|
||||||
var expected, status;
|
var expected, status;
|
||||||
OpenLayers.Request.POST = function(obj) {
|
OpenLayers.Request.POST = function(obj) {
|
||||||
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
|
t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
|
||||||
@@ -48,14 +48,14 @@
|
|||||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
expected = readXML("GetFeature_1");
|
expected = readXML("GetFeature_1");
|
||||||
status = 200;
|
status = 200;
|
||||||
var response = protocol.read({callback: function(response) {
|
var response = protocol.read({callback: function(response) {
|
||||||
t.eq(response.features, "foo", "user callback properly called with features");
|
t.eq(response.features, "foo", "user callback properly called with features");
|
||||||
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
|
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
|
||||||
}});
|
}});
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
maxFeatures: 10,
|
maxFeatures: 10,
|
||||||
featureType: 'type2',
|
featureType: 'type2',
|
||||||
@@ -71,7 +71,7 @@
|
|||||||
|
|
||||||
OpenLayers.Request.POST = _POST;
|
OpenLayers.Request.POST = _POST;
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_commit(t){
|
function test_commit(t){
|
||||||
t.plan(4);
|
t.plan(4);
|
||||||
|
|
||||||
@@ -90,8 +90,8 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
var _POST = OpenLayers.Request.POST;
|
var _POST = OpenLayers.Request.POST;
|
||||||
|
|
||||||
var expected;
|
var expected;
|
||||||
OpenLayers.Request.POST = function(obj) {
|
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");
|
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.responseText = "foo";
|
||||||
@@ -99,7 +99,7 @@
|
|||||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
|
var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
|
||||||
featureDelete.fid = "fid.37";
|
featureDelete.fid = "fid.37";
|
||||||
featureDelete.state = OpenLayers.State.DELETE;
|
featureDelete.state = OpenLayers.State.DELETE;
|
||||||
@@ -124,7 +124,7 @@
|
|||||||
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
|
t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expected = readXML("commit");
|
expected = readXML("commit");
|
||||||
var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
|
var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@
|
|||||||
featureNS: "http://namespace.org",
|
featureNS: "http://namespace.org",
|
||||||
featureType: "type"
|
featureType: "type"
|
||||||
});
|
});
|
||||||
|
|
||||||
var filter = new OpenLayers.Filter.Spatial({
|
var filter = new OpenLayers.Filter.Spatial({
|
||||||
type: OpenLayers.Filter.Spatial.BBOX,
|
type: OpenLayers.Filter.Spatial.BBOX,
|
||||||
value: new OpenLayers.Bounds(-5, -5, 5, 5)
|
value: new OpenLayers.Bounds(-5, -5, 5, 5)
|
||||||
@@ -155,16 +155,16 @@
|
|||||||
t.delay_call(0.1, function() {obj.callback.call(this)});
|
t.delay_call(0.1, function() {obj.callback.call(this)});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = protocol.filterDelete(filter, {
|
var response = protocol.filterDelete(filter, {
|
||||||
callback: function() {
|
callback: function() {
|
||||||
t.ok("user callback function called");
|
t.ok("user callback function called");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
OpenLayers.Request.POST = _POST;
|
OpenLayers.Request.POST = _POST;
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_abort(t) {
|
function test_abort(t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
var protocol = new OpenLayers.Protocol.WFS({
|
var protocol = new OpenLayers.Protocol.WFS({
|
||||||
@@ -172,7 +172,7 @@
|
|||||||
featureNS: "http://example.com#namespace",
|
featureNS: "http://example.com#namespace",
|
||||||
featureType: "type"
|
featureType: "type"
|
||||||
});
|
});
|
||||||
|
|
||||||
var response = {
|
var response = {
|
||||||
priv: {
|
priv: {
|
||||||
abort: function() {
|
abort: function() {
|
||||||
@@ -180,14 +180,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// call abort with mocked response
|
// call abort with mocked response
|
||||||
var aborted = false;
|
var aborted = false;
|
||||||
protocol.abort(response);
|
protocol.abort(response);
|
||||||
t.eq(aborted, true, "abort called on response.priv");
|
t.eq(aborted, true, "abort called on response.priv");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_fromWMSLayer(t) {
|
function test_fromWMSLayer(t) {
|
||||||
t.plan(8);
|
t.plan(8);
|
||||||
var map = new OpenLayers.Map("map", {
|
var map = new OpenLayers.Map("map", {
|
||||||
@@ -203,18 +203,35 @@
|
|||||||
t.eq(protocol.featureType, "states", "typeName correctly extracted");
|
t.eq(protocol.featureType, "states", "typeName correctly extracted");
|
||||||
t.eq(protocol.srsName, "EPSG:1234", "srsName set correctly");
|
t.eq(protocol.srsName, "EPSG:1234", "srsName set correctly");
|
||||||
t.eq(protocol.version, "1.1.0", "version set correctly");
|
t.eq(protocol.version, "1.1.0", "version set correctly");
|
||||||
|
|
||||||
layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"];
|
layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"];
|
||||||
layer.projection = new OpenLayers.Projection("EPSG:900913");
|
layer.projection = new OpenLayers.Projection("EPSG:900913");
|
||||||
protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
|
protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
|
||||||
t.eq(protocol.featurePrefix, "topp", "featurePrefix from layer param array");
|
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.featureType, "street_centerline", "first layer from layer param array as featureType");
|
||||||
t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred");
|
t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_readFormat(t) {
|
||||||
|
t.plan(1);
|
||||||
|
|
||||||
|
var protocol = new OpenLayers.Protocol.WFS({
|
||||||
|
url: "http://some.url.org",
|
||||||
|
featureNS: "http://namespace.org",
|
||||||
|
featureType: "type",
|
||||||
|
formatOptions: {outputFormat: 'json'},
|
||||||
|
readFormat: new OpenLayers.Format.GeoJSON()
|
||||||
|
});
|
||||||
|
|
||||||
|
var request = {};
|
||||||
|
request.responseText = '{"type":"FeatureCollection","features":[{"type":"Feature","id":"V_HECTOPUNTEN.108411","geometry":{"type":"MultiPoint","coordinates":[[190659.467,349576.19]]},"geometry_name":"ORA_GEOMETRY","properties":{"WEGNUMMER":"002","HECTOMTRNG_ORG":2200,"HECTOMTRNG":"220.00","bbox":[190659.467,349576.19,190659.467,349576.19]}}]}';
|
||||||
|
var features = protocol.parseFeatures(request);
|
||||||
|
t.eq(features.length, 1, "the right format is used to read the request (GeoJSON)");
|
||||||
|
}
|
||||||
|
|
||||||
function readXML(id) {
|
function readXML(id) {
|
||||||
var xml = document.getElementById(id).firstChild.nodeValue;
|
var xml = document.getElementById(id).firstChild.nodeValue;
|
||||||
return new OpenLayers.Format.XML().read(xml).documentElement;
|
return new OpenLayers.Format.XML().read(xml).documentElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user