Extending the filter format and the feature ID filter to support writing of logical filters that contain FID filters. r=bartvde (closes #3012)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11756 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-03-29 18:49:27 +00:00
parent 15a7d0d18e
commit 3ba8a294ff
3 changed files with 124 additions and 8 deletions

View File

@@ -25,6 +25,12 @@ OpenLayers.Filter.FeatureId = OpenLayers.Class(OpenLayers.Filter, {
*/
fids: null,
/**
* Property: type
* {String} Type to identify this filter.
*/
type: "FID",
/**
* Constructor: OpenLayers.Filter.FeatureId
* Creates an ogc:FeatureId rule.

View File

@@ -272,6 +272,19 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
return this.writers.ogc["Filter"].apply(this, [filter]);
},
/**
* Method: writeFeatureIdNodes
*
* Parameters:
* filter - {<OpenLayers.Filter.FeatureId}
* node - {DOMElement}
*/
writeFeatureIdNodes: function(filter, node) {
for (var i=0, ii=filter.fids.length; i<ii; ++i) {
this.writeNode("FeatureId", filter.fids[i], node);
}
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
@@ -282,11 +295,8 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"ogc": {
"Filter": function(filter) {
var node = this.createElementNSPlus("ogc:Filter");
var sub = filter.CLASS_NAME.split(".").pop();
if(sub == "FeatureId") {
for(var i=0; i<filter.fids.length; ++i) {
this.writeNode("FeatureId", filter.fids[i], node);
}
if (filter.type === "FID") {
this.writeFeatureIdNodes(filter, node);
} else {
this.writeNode(this.getFilterType(filter), filter, node);
}
@@ -300,31 +310,43 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"And": function(filter) {
var node = this.createElementNSPlus("ogc:And");
var childFilter;
for(var i=0; i<filter.filters.length; ++i) {
for (var i=0, ii=filter.filters.length; i<ii; ++i) {
childFilter = filter.filters[i];
if (childFilter.type === "FID") {
this.writeFeatureIdNodes(childFilter, node);
} else {
this.writeNode(
this.getFilterType(childFilter), childFilter, node
);
}
}
return node;
},
"Or": function(filter) {
var node = this.createElementNSPlus("ogc:Or");
var childFilter;
for(var i=0; i<filter.filters.length; ++i) {
for (var i=0, ii=filter.filters.length; i<ii; ++i) {
childFilter = filter.filters[i];
if (childFilter.type === "FID") {
this.writeFeatureIdNodes(childFilter, node);
} else {
this.writeNode(
this.getFilterType(childFilter), childFilter, node
);
}
}
return node;
},
"Not": function(filter) {
var node = this.createElementNSPlus("ogc:Not");
var childFilter = filter.filters[0];
if (childFilter.type === "FID") {
this.writeFeatureIdNodes(childFilter, node);
} else {
this.writeNode(
this.getFilterType(childFilter), childFilter, node
);
}
return node;
},
"PropertyIsLessThan": function(filter) {
@@ -460,7 +482,8 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"DWITHIN": "DWITHIN",
"WITHIN": "WITHIN",
"CONTAINS": "CONTAINS",
"INTERSECTS": "INTERSECTS"
"INTERSECTS": "INTERSECTS",
"FID": "FeatureId"
},
CLASS_NAME: "OpenLayers.Format.Filter.v1"

View File

@@ -166,9 +166,96 @@
}
function test_logical_fid(t) {
// the Filter Encoding spec doesn't allow for FID filters inside logical filters
// however, to be liberal, we will write them without complaining
t.plan(3);
var filter = new OpenLayers.Filter.Logical({
type: OpenLayers.Filter.Logical.OR,
filters: [
new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LIKE,
property: "person",
value: "me"
}),
new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
]
});
var format = new OpenLayers.Format.Filter.v1_0_0();
var got = format.write(filter);
var exp = readXML("LogicalFeatureId");
t.xml_eq(got, exp, "wrote FID filter in logical OR without complaint");
filter = new OpenLayers.Filter.Logical({
type: OpenLayers.Filter.Logical.AND,
filters: [
new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LIKE,
property: "person",
value: "me"
}),
new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
]
});
got = format.write(filter);
exp = readXML("LogicalFeatureIdAnd");
t.xml_eq(got, exp, "wrote FID filter in logical AND without complaint");
filter = new OpenLayers.Filter.Logical({
type: OpenLayers.Filter.Logical.NOT,
filters: [
new OpenLayers.Filter.FeatureId({fids: ["foo.2"]})
]
});
got = format.write(filter);
exp = readXML("LogicalFeatureIdNot");
t.xml_eq(got, exp, "wrote FID filter in logical NOT without complaint");
}
function readXML(id) {
var xml = document.getElementById(id).firstChild.nodeValue;
return new OpenLayers.Format.XML().read(xml).documentElement;
}
</script>
</head>
<body>
<div id="LogicalFeatureId"><!--
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:Or>
<ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
<ogc:PropertyName>person</ogc:PropertyName>
<ogc:Literal>me</ogc:Literal>
</ogc:PropertyIsLike>
<ogc:FeatureId fid="foo.1"/>
<ogc:FeatureId fid="foo.2"/>
</ogc:Or>
</ogc:Filter>
--></div>
<div id="LogicalFeatureIdAnd"><!--
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:And>
<ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
<ogc:PropertyName>person</ogc:PropertyName>
<ogc:Literal>me</ogc:Literal>
</ogc:PropertyIsLike>
<ogc:FeatureId fid="foo.1"/>
<ogc:FeatureId fid="foo.2"/>
</ogc:And>
</ogc:Filter>
--></div>
<div id="LogicalFeatureIdNot"><!--
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:Not>
<ogc:FeatureId fid="foo.2"/>
</ogc:Not>
</ogc:Filter>
--></div>
</body>
</html>