Filter enhancements. Adding support for serializing filters with Filter Encoding 1.1. Adding matchCase attribute to filters with binary operators. Refactoring filter formats to use methods from base XML format. r=ahocevar,adube (closes #1790)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8812 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-02-03 22:28:48 +00:00
parent ebed733327
commit f9939f1cfe
10 changed files with 607 additions and 281 deletions

View File

@@ -8,7 +8,7 @@
height: 250px; height: 250px;
} }
#out0, #out1 { #out0, #out1 {
height: 250px; height: 100px;
width: 90%; width: 90%;
overflow: auto; overflow: auto;
} }
@@ -17,6 +17,7 @@
<script type="text/javascript"> <script type="text/javascript">
var filter_1_0 = new OpenLayers.Format.Filter({version: "1.0.0"}); var filter_1_0 = new OpenLayers.Format.Filter({version: "1.0.0"});
var filter_1_1 = new OpenLayers.Format.Filter({version: "1.1.0"});
var xml = new OpenLayers.Format.XML(); var xml = new OpenLayers.Format.XML();
var filter; var filter;
@@ -24,11 +25,25 @@
var code = input.value; var code = input.value;
try { try {
eval(code); eval(code);
} catch(err) {
out0.value = err.message;
out1.value = "";
}
try {
out0.value = xml.write(filter_1_0.write(filter)); out0.value = xml.write(filter_1_0.write(filter));
} catch(err) { } catch(err) {
out0.innerHTML = err.message; out0.value = err.message;
if(err.lineNumber != undefined) { if(err.lineNumber != undefined) {
out0.innerHTML += " (line " + err.lineNumber + " " + out0.value += " (line " + err.lineNumber + " " +
err.fileName + ")";
}
}
try {
out1.value = xml.write(filter_1_1.write(filter));
} catch(err) {
out1.value = err.message;
if(err.lineNumber != undefined) {
out1.value += " (line " + err.lineNumber + " " +
err.fileName + ")"; err.fileName + ")";
} }
} }
@@ -38,6 +53,9 @@
window.onload = function() { window.onload = function() {
input = document.getElementById("in"); input = document.getElementById("in");
out0 = document.getElementById("out0"); out0 = document.getElementById("out0");
out1 = document.getElementById("out1");
out0.value = "";
out1.value = "";
document.getElementById("write").onclick = write; document.getElementById("write").onclick = write;
}; };
@@ -61,6 +79,11 @@ filter = new OpenLayers.Filter.Logical({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO, type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
property: "mean", property: "mean",
value: "yes" value: "yes"
}),
new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: "EPSG:4326"
}) })
] ]
}); });
@@ -68,6 +91,8 @@ filter = new OpenLayers.Filter.Logical({
<button id="write">write</button><br /> <button id="write">write</button><br />
Filter Encoding 1.0 Filter Encoding 1.0
<textarea id="out0"></textarea><br /> <textarea id="out0"></textarea><br />
Filter Encoding 1.1
<textarea id="out1"></textarea><br />
<p id="docs"> <p id="docs">
</p> </p>
</body> </body>

View File

@@ -221,6 +221,7 @@
"OpenLayers/Format/Filter.js", "OpenLayers/Format/Filter.js",
"OpenLayers/Format/Filter/v1.js", "OpenLayers/Format/Filter/v1.js",
"OpenLayers/Format/Filter/v1_0_0.js", "OpenLayers/Format/Filter/v1_0_0.js",
"OpenLayers/Format/Filter/v1_1_0.js",
"OpenLayers/Format/Text.js", "OpenLayers/Format/Text.js",
"OpenLayers/Format/JSON.js", "OpenLayers/Format/JSON.js",
"OpenLayers/Format/GeoJSON.js", "OpenLayers/Format/GeoJSON.js",

View File

@@ -45,6 +45,18 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
*/ */
value: null, value: null,
/**
* Property: matchCase
* {Boolean} Force case sensitive searches for EQUAL_TO and NOT_EQUAL_TO
* comparisons. The Filter Encoding 1.1 specification added a matchCase
* attribute to ogc:PropertyIsEqualTo and ogc:PropertyIsNotEqualTo
* elements. This property will be serialized with those elements only
* if using the v1.1.0 filter format. However, when evaluating filters
* here, the matchCase property will always be respected (for EQUAL_TO
* and NOT_EQUAL_TO). Default is true.
*/
matchCase: true,
/** /**
* APIProperty: lowerBoundary * APIProperty: lowerBoundary
* {Number} or {String} * {Number} or {String}
@@ -90,25 +102,50 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
* {Boolean} The filter applies. * {Boolean} The filter applies.
*/ */
evaluate: function(context) { evaluate: function(context) {
var result = false;
switch(this.type) { switch(this.type) {
case OpenLayers.Filter.Comparison.EQUAL_TO: case OpenLayers.Filter.Comparison.EQUAL_TO:
case OpenLayers.Filter.Comparison.LESS_THAN: var got = context[this.property];
case OpenLayers.Filter.Comparison.GREATER_THAN: var exp = this.value;
case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO: if(!this.matchCase &&
case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO: typeof got == "string" && typeof exp == "string") {
return this.binaryCompare(context, this.property, this.value); result = (got.toUpperCase() == exp.toUpperCase());
} else {
case OpenLayers.Filter.Comparison.BETWEEN: result = (got == exp);
var result =
context[this.property] >= this.lowerBoundary;
result = result &&
context[this.property] <= this.upperBoundary;
return result;
case OpenLayers.Filter.Comparison.LIKE:
var regexp = new RegExp(this.value,
"gi");
return regexp.test(context[this.property]);
} }
break;
case OpenLayers.Filter.Comparison.NOT_EQUAL_TO:
var got = context[this.property];
var exp = this.value;
if(!this.matchCase &&
typeof got == "string" && typeof exp == "string") {
result = (got.toUpperCase() != exp.toUpperCase());
} else {
result = (got != exp);
}
break;
case OpenLayers.Filter.Comparison.LESS_THAN:
result = context[this.property] < this.value;
break;
case OpenLayers.Filter.Comparison.GREATER_THAN:
result = context[this.property] > this.value;
break;
case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
result = context[this.property] <= this.value;
break;
case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
result = context[this.property] >= this.value;
break;
case OpenLayers.Filter.Comparison.BETWEEN:
result = (context[this.property] >= this.lowerBoundary) &&
(context[this.property] <= this.upperBoundary);
break;
case OpenLayers.Filter.Comparison.LIKE:
var regexp = new RegExp(this.value, "gi");
result = regexp.test(context[this.property]);
break;
}
return result;
}, },
/** /**
@@ -192,35 +229,6 @@ OpenLayers.Filter.Comparison = OpenLayers.Class(OpenLayers.Filter, {
return value; return value;
}, },
/**
* Function: binaryCompare
* Compares a feature property to a rule value
*
* Parameters:
* context - {Object}
* property - {String} or {Number}
* value - {String} or {Number}, same as property
*
* Returns:
* {Boolean}
*/
binaryCompare: function(context, property, value) {
switch (this.type) {
case OpenLayers.Filter.Comparison.EQUAL_TO:
return context[property] == value;
case OpenLayers.Filter.Comparison.NOT_EQUAL_TO:
return context[property] != value;
case OpenLayers.Filter.Comparison.LESS_THAN:
return context[property] < value;
case OpenLayers.Filter.Comparison.GREATER_THAN:
return context[property] > value;
case OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO:
return context[property] <= value;
case OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO:
return context[property] >= value;
}
},
CLASS_NAME: "OpenLayers.Filter.Comparison" CLASS_NAME: "OpenLayers.Filter.Comparison"
}); });

View File

@@ -21,6 +21,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
*/ */
namespaces: { namespaces: {
ogc: "http://www.opengis.net/ogc", ogc: "http://www.opengis.net/ogc",
gml: "http://www.opengis.net/gml",
xlink: "http://www.w3.org/1999/xlink", xlink: "http://www.w3.org/1999/xlink",
xsi: "http://www.w3.org/2001/XMLSchema-instance" xsi: "http://www.w3.org/2001/XMLSchema-instance"
}, },
@@ -119,20 +120,6 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
this.readChildNodes(node, filter); this.readChildNodes(node, filter);
obj.filters.push(filter); obj.filters.push(filter);
}, },
"PropertyIsEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsLessThan": function(node, obj) { "PropertyIsLessThan": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({ var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.LESS_THAN type: OpenLayers.Filter.Comparison.LESS_THAN
@@ -241,10 +228,10 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
var sub = filter.CLASS_NAME.split(".").pop(); var sub = filter.CLASS_NAME.split(".").pop();
if(sub == "FeatureId") { if(sub == "FeatureId") {
for(var i=0; i<filter.fids.length; ++i) { for(var i=0; i<filter.fids.length; ++i) {
this.writeNode(node, "FeatureId", filter.fids[i]); this.writeNode("FeatureId", filter.fids[i], node);
} }
} else { } else {
this.writeNode(node, this.getFilterType(filter), filter); this.writeNode(this.getFilterType(filter), filter, node);
} }
return node; return node;
}, },
@@ -259,7 +246,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
for(var i=0; i<filter.filters.length; ++i) { for(var i=0; i<filter.filters.length; ++i) {
childFilter = filter.filters[i]; childFilter = filter.filters[i];
this.writeNode( this.writeNode(
node, this.getFilterType(childFilter), childFilter this.getFilterType(childFilter), childFilter, node
); );
} }
return node; return node;
@@ -270,7 +257,7 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
for(var i=0; i<filter.filters.length; ++i) { for(var i=0; i<filter.filters.length; ++i) {
childFilter = filter.filters[i]; childFilter = filter.filters[i];
this.writeNode( this.writeNode(
node, this.getFilterType(childFilter), childFilter this.getFilterType(childFilter), childFilter, node
); );
} }
return node; return node;
@@ -279,58 +266,44 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
var node = this.createElementNSPlus("ogc:Not"); var node = this.createElementNSPlus("ogc:Not");
var childFilter = filter.filters[0]; var childFilter = filter.filters[0];
this.writeNode( this.writeNode(
node, this.getFilterType(childFilter), childFilter this.getFilterType(childFilter), childFilter, node
); );
return node; return node;
}, },
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo");
// no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter);
this.writeNode(node, "Literal", filter.value);
return node;
},
"PropertyIsLessThan": function(filter) { "PropertyIsLessThan": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsLessThan"); var node = this.createElementNSPlus("ogc:PropertyIsLessThan");
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
this.writeNode(node, "Literal", filter.value); this.writeNode("Literal", filter.value, node);
return node; return node;
}, },
"PropertyIsGreaterThan": function(filter) { "PropertyIsGreaterThan": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsGreaterThan"); var node = this.createElementNSPlus("ogc:PropertyIsGreaterThan");
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
this.writeNode(node, "Literal", filter.value); this.writeNode("Literal", filter.value, node);
return node; return node;
}, },
"PropertyIsLessThanOrEqualTo": function(filter) { "PropertyIsLessThanOrEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsLessThanOrEqualTo"); var node = this.createElementNSPlus("ogc:PropertyIsLessThanOrEqualTo");
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
this.writeNode(node, "Literal", filter.value); this.writeNode("Literal", filter.value, node);
return node; return node;
}, },
"PropertyIsGreaterThanOrEqualTo": function(filter) { "PropertyIsGreaterThanOrEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsGreaterThanOrEqualTo"); var node = this.createElementNSPlus("ogc:PropertyIsGreaterThanOrEqualTo");
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
this.writeNode(node, "Literal", filter.value); this.writeNode("Literal", filter.value, node);
return node; return node;
}, },
"PropertyIsBetween": function(filter) { "PropertyIsBetween": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsBetween"); var node = this.createElementNSPlus("ogc:PropertyIsBetween");
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
this.writeNode(node, "LowerBoundary", filter); this.writeNode("LowerBoundary", filter, node);
this.writeNode(node, "UpperBoundary", filter); this.writeNode("UpperBoundary", filter, node);
return node; return node;
}, },
"PropertyIsLike": function(filter) { "PropertyIsLike": function(filter) {
@@ -340,9 +313,9 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
} }
}); });
// no ogc:expression handling for now // no ogc:expression handling for now
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
// convert regex string to ogc string // convert regex string to ogc string
this.writeNode(node, "Literal", filter.regex2value()); this.writeNode("Literal", filter.regex2value(), node);
return node; return node;
}, },
"PropertyName": function(filter) { "PropertyName": function(filter) {
@@ -360,35 +333,28 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"LowerBoundary": function(filter) { "LowerBoundary": function(filter) {
// no ogc:expression handling for now // no ogc:expression handling for now
var node = this.createElementNSPlus("ogc:LowerBoundary"); var node = this.createElementNSPlus("ogc:LowerBoundary");
this.writeNode(node, "Literal", filter.lowerBoundary); this.writeNode("Literal", filter.lowerBoundary, node);
return node; return node;
}, },
"UpperBoundary": function(filter) { "UpperBoundary": function(filter) {
// no ogc:expression handling for now // no ogc:expression handling for now
var node = this.createElementNSPlus("ogc:UpperBoundary"); var node = this.createElementNSPlus("ogc:UpperBoundary");
this.writeNode(node, "Literal", filter.upperBoundary); this.writeNode("Literal", filter.upperBoundary, node);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode(node, "PropertyName", filter);
var gml = new OpenLayers.Format.GML();
node.appendChild(gml.buildGeometryNode(filter.value));
return node; return node;
}, },
"DWITHIN": function(filter) { "DWITHIN": function(filter) {
var node = this.createElementNSPlus("ogc:DWithin"); var node = this.createElementNSPlus("ogc:DWithin");
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
var gml = new OpenLayers.Format.GML(); var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(gml.buildGeometryNode(filter.value)); node.appendChild(child.firstChild);
this.writeNode(node, "Distance", filter); this.writeNode("Distance", filter, node);
return node; return node;
}, },
"INTERSECTS": function(filter) { "INTERSECTS": function(filter) {
var node = this.createElementNSPlus("ogc:Intersects"); var node = this.createElementNSPlus("ogc:Intersects");
this.writeNode(node, "PropertyName", filter); this.writeNode("PropertyName", filter, node);
var gml = new OpenLayers.Format.GML(); var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(gml.buildGeometryNode(filter.value)); node.appendChild(child.firstChild);
return node; return node;
}, },
"Distance": function(filter) { "Distance": function(filter) {
@@ -432,154 +398,6 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"INTERSECTS": "INTERSECTS" "INTERSECTS": "INTERSECTS"
}, },
/**
* Methods below this point are of general use for versioned XML parsers.
* These are candidates for an abstract class.
*/
/**
* Method: getNamespacePrefix
* Get the namespace prefix for a given uri from the <namespaces> object.
*
* Returns:
* {String} A namespace prefix or null if none found.
*/
getNamespacePrefix: function(uri) {
var prefix = null;
if(uri == null) {
prefix = this.namespaces[this.defaultPrefix];
} else {
var gotPrefix = false;
for(prefix in this.namespaces) {
if(this.namespaces[prefix] == uri) {
gotPrefix = true;
break;
}
}
if(!gotPrefix) {
prefix = null;
}
}
return prefix;
},
/**
* Method: readChildNodes
*/
readChildNodes: function(node, obj) {
var children = node.childNodes;
var child, group, reader, prefix, local;
for(var i=0; i<children.length; ++i) {
child = children[i];
if(child.nodeType == 1) {
prefix = this.getNamespacePrefix(child.namespaceURI);
local = child.nodeName.split(":").pop();
group = this.readers[prefix];
if(group) {
reader = group[local];
if(reader) {
reader.apply(this, [child, obj]);
}
}
}
}
},
/**
* Method: writeNode
* Shorthand for applying one of the named writers and appending the
* results to a node. If a qualified name is not provided for the
* second argument (and a local name is used instead), the namespace
* of the parent node will be assumed.
*
* Parameters:
* parent - {DOMElement} Result will be appended to this node.
* name - {String} The name of a node to generate. If a qualified name
* (e.g. "pre:Name") is used, the namespace prefix is assumed to be
* in the <writers> group. If a local name is used (e.g. "Name") then
* the namespace of the parent is assumed.
* obj - {Object} Structure containing data for the writer.
*
* Returns:
* {DOMElement} The child node.
*/
writeNode: function(parent, name, obj) {
var prefix, local;
var split = name.indexOf(":");
if(split > 0) {
prefix = name.substring(0, split);
local = name.substring(split + 1);
} else {
prefix = this.getNamespacePrefix(parent.namespaceURI);
local = name;
}
var child = this.writers[prefix][local].apply(this, [obj]);
parent.appendChild(child);
return child;
},
/**
* Method: createElementNSPlus
* Shorthand for creating namespaced elements with optional attributes and
* child text nodes.
*
* Parameters:
* name - {String} The qualified node name.
* options - {Object} Optional object for node configuration.
*
* Returns:
* {Element} An element node.
*/
createElementNSPlus: function(name, options) {
options = options || {};
var loc = name.indexOf(":");
// order of prefix preference
// 1. in the uri option
// 2. in the prefix option
// 3. in the qualified name
// 4. from the defaultPrefix
var uri = options.uri || this.namespaces[options.prefix];
if(!uri) {
loc = name.indexOf(":");
uri = this.namespaces[name.substring(0, loc)];
}
if(!uri) {
uri = this.namespaces[this.defaultPrefix];
}
var node = this.createElementNS(uri, name);
if(options.attributes) {
this.setAttributes(node, options.attributes);
}
if(options.value) {
node.appendChild(this.createTextNode(options.value));
}
return node;
},
/**
* Method: setAttributes
* Set multiple attributes given key value pairs from an object.
*
* Parameters:
* node - {Element} An element node.
* obj - {Object || Array} An object whose properties represent attribute
* names and values represent attribute values. If an attribute name
* is a qualified name ("prefix:local"), the prefix will be looked up
* in the parsers {namespaces} object. If the prefix is found,
* setAttributeNS will be used instead of setAttribute.
*/
setAttributes: function(node, obj) {
var value, loc, alias, uri;
for(var name in obj) {
value = obj[name].toString();
// check for qualified attribute name ("prefix:local")
uri = this.namespaces[name.substring(0, name.indexOf(":"))] || null;
this.setAttributeNS(node, uri, name, value);
}
},
CLASS_NAME: "OpenLayers.Format.Filter.v1" CLASS_NAME: "OpenLayers.Format.Filter.v1"
}); });

View File

@@ -3,6 +3,7 @@
* full text of the license. */ * full text of the license. */
/** /**
* @requires OpenLayers/Format/GML/v2.js
* @requires OpenLayers/Format/Filter/v1.js * @requires OpenLayers/Format/Filter/v1.js
*/ */
@@ -43,6 +44,72 @@ OpenLayers.Format.Filter.v1_0_0 = OpenLayers.Class(
); );
}, },
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
}
}, OpenLayers.Format.Filter.v1.prototype.readers["ogc"]),
"gml": OpenLayers.Format.GML.v2.prototype.readers["gml"],
"feature": OpenLayers.Format.GML.v2.prototype.readers["feature"]
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
* writing functions grouped by namespace alias and named like the
* node names they produce.
*/
writers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo");
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo");
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode("PropertyName", filter, node);
var box = this.writeNode("gml:Box", filter.value, node);
if(filter.projection) {
box.setAttribute("srsName", filter.projection);
}
return node;
}}, OpenLayers.Format.Filter.v1.prototype.writers["ogc"]),
"gml": OpenLayers.Format.GML.v2.prototype.writers["gml"],
"feature": OpenLayers.Format.GML.v2.prototype.writers["feature"]
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_0_0" CLASS_NAME: "OpenLayers.Format.Filter.v1_0_0"
}); });

View File

@@ -0,0 +1,130 @@
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Format/Filter/v1.js
* @requires OpenLayers/Format/GML/v3.js
*/
/**
* Class: OpenLayers.Format.Filter.v1_1_0
* Write ogc:Filter version 1.1.0.
*
* Differences from the v1.0.0 parser:
* - uses GML v3 instead of GML v2
* - reads matchCase attribute on ogc:PropertyIsEqual and
* ogc:PropertyIsNotEqualelements.
* - writes matchCase attribute from comparison filters of type EQUAL_TO and
* type NOT_EQUAL_TO.
*
* Inherits from:
* - <OpenLayers.Format.Filter.v1>
*/
OpenLayers.Format.Filter.v1_1_0 = OpenLayers.Class(
OpenLayers.Format.Filter.v1, {
/**
* Constant: VERSION
* {String} 1.1.0
*/
VERSION: "1.1.0",
/**
* Property: schemaLocation
* {String} http://www.opengis.net/ogc/filter/1.1.0/filter.xsd
*/
schemaLocation: "http://www.opengis.net/ogc/filter/1.1.0/filter.xsd",
/**
* Constructor: OpenLayers.Format.Filter.v1_1_0
* Instances of this class are not created directly. Use the
* <OpenLayers.Format.Filter> constructor instead.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.Filter.v1.prototype.initialize.apply(
this, [options]
);
},
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(node, obj) {
var matchCase = node.getAttribute("matchCase");
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
matchCase: !(matchCase === "false" || matchCase === "0")
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
},
"PropertyIsNotEqualTo": function(node, obj) {
var matchCase = node.getAttribute("matchCase");
var filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
matchCase: !(matchCase === "false" || matchCase === "0")
});
this.readChildNodes(node, filter);
obj.filters.push(filter);
}
}, OpenLayers.Format.Filter.v1.prototype.readers["ogc"]),
"gml": OpenLayers.Format.GML.v3.prototype.readers["gml"],
"feature": OpenLayers.Format.GML.v3.prototype.readers["feature"]
},
/**
* Property: writers
* As a compliment to the readers property, this structure contains public
* writing functions grouped by namespace alias and named like the
* node names they produce.
*/
writers: {
"ogc": OpenLayers.Util.applyDefaults({
"PropertyIsEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsEqualTo", {
attributes: {matchCase: filter.matchCase}
});
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"PropertyIsNotEqualTo": function(filter) {
var node = this.createElementNSPlus("ogc:PropertyIsNotEqualTo", {
attributes: {matchCase: filter.matchCase}
});
// no ogc:expression handling for now
this.writeNode("PropertyName", filter, node);
this.writeNode("Literal", filter.value, node);
return node;
},
"BBOX": function(filter) {
var node = this.createElementNSPlus("ogc:BBOX");
this.writeNode("PropertyName", filter, node);
var box = this.writeNode("gml:Envelope", filter.value);
if(filter.projection) {
box.setAttribute("srsName", filter.projection);
}
node.appendChild(box);
return node;
}}, OpenLayers.Format.Filter.v1.prototype.writers["ogc"]),
"gml": OpenLayers.Format.GML.v3.prototype.writers["gml"],
"feature": OpenLayers.Format.GML.v3.prototype.writers["feature"]
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_1_0"
});

View File

@@ -67,40 +67,116 @@
} }
function test_evaluate(t) { function test_evaluate(t) {
t.plan(5);
var filter = new OpenLayers.Filter.Comparison({ var cases = [{
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.BETWEEN,
property: "area", property: "area",
lowerBoundary: 1000, lowerBoundary: 1000,
upperBoundary: 4999, upperBoundary: 4999
type: OpenLayers.Filter.Comparison.BETWEEN}); }),
context: {area: 999},
expect: false
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.BETWEEN,
property: "area",
lowerBoundary: 1000,
upperBoundary: 4999
}),
context: {area: 1000},
expect: true
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.BETWEEN,
property: "area",
lowerBoundary: 1000,
upperBoundary: 4999
}),
context: {area: 4999},
expect: true
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.BETWEEN,
property: "area",
lowerBoundary: 1000,
upperBoundary: 4999
}),
context: {area: 5000},
expect: false
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.BETWEEN,
property: "area",
lowerBoundary: 1000,
upperBoundary: 4999
}),
context: {area: 999},
expect: false
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "prop",
value: "Foo"
}),
context: {prop: "Foo"},
expect: true
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "prop",
value: "Foo"
}),
context: {prop: "foo"},
expect: false
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
matchCase: true,
property: "prop",
value: "Foo"
}),
context: {prop: "foo"},
expect: false
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
property: "prop",
value: "foo"
}),
context: {prop: "FOO"},
expect: true
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
matchCase: true,
property: "prop",
value: "foo"
}),
context: {prop: "FOO"},
expect: true
}, {
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
matchCase: false,
property: "prop",
value: "foo"
}),
context: {prop: "FOO"},
expect: false
}];
var features = [ t.plan(cases.length);
new OpenLayers.Feature.Vector(null, {
area: 999}), var c;
new OpenLayers.Feature.Vector(null, { for(var i=0; i<cases.length; ++i) {
area: 1000}), c = cases[i];
new OpenLayers.Feature.Vector(null, { t.eq(c.filter.evaluate(c.context), c.expect, "case " + i + ": " + c.filter.type);
area: 4999}),
new OpenLayers.Feature.Vector(null, {
area: 5000})];
// PropertyIsBetween filter: lower and upper boundary are inclusive
var filterResults = {
0: false,
1: true,
2: true,
3: false};
for (var i in filterResults) {
var result = filter.evaluate(features[i].attributes);
t.eq(result, filterResults[i], "feature "+i+
" evaluates to "+result.toString()+" correctly.");
} }
var context = {
area: 4998
};
var result = filter.evaluate(context);
t.eq(result, true, "evaluation against custom filter context works.");
} }
</script> </script>
</head> </head>
<body> <body>

View File

@@ -53,6 +53,31 @@
} }
function test_BBOX(t) {
t.plan(1);
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
property: "the_geom",
value: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: "EPSG:4326"
});
var out =
'<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" srsName="EPSG:4326">' +
'<gml:coordinates decimal="." cs="," ts=" ">-90,-180 90,180</gml:coordinates>' +
'</gml:Box>' +
'</ogc:BBOX>' +
'</ogc:Filter>';
var parser = new OpenLayers.Format.Filter.v1_0_0();
var node = parser.write(filter);
t.xml_eq(node, out, "bbox correctly written");
}
</script> </script>
</head> </head>

View File

@@ -0,0 +1,175 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script type="text/javascript">
var test_xml =
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:Or>' +
'<ogc:PropertyIsBetween>' +
'<ogc:PropertyName>number</ogc:PropertyName>' +
'<ogc:LowerBoundary>' +
'<ogc:Literal>1064866676</ogc:Literal>' +
'</ogc:LowerBoundary>' +
'<ogc:UpperBoundary>' +
'<ogc:Literal>1065512599</ogc:Literal>' +
'</ogc:UpperBoundary>' +
'</ogc:PropertyIsBetween>' +
'<ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>*dog.food!*good</ogc:Literal>' +
'</ogc:PropertyIsLike>' +
'<ogc:Not>' +
'<ogc:PropertyIsLessThanOrEqualTo>' +
'<ogc:PropertyName>FOO</ogc:PropertyName>' +
'<ogc:Literal>5000</ogc:Literal>' +
'</ogc:PropertyIsLessThanOrEqualTo>' +
'</ogc:Not>' +
'<ogc:PropertyIsEqualTo matchCase="true">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'<ogc:PropertyIsEqualTo matchCase="false">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Or>' +
'</ogc:Filter>';
function test_read(t) {
t.plan(3);
var parser = new OpenLayers.Format.Filter.v1_1_0();
var xml = new OpenLayers.Format.XML();
var filter = parser.read(xml.read(test_xml).documentElement);
t.ok(filter instanceof OpenLayers.Filter.Logical, "instance of correct class");
t.eq(filter.type, OpenLayers.Filter.Logical.OR, "correct type");
t.eq(filter.filters.length, 5, "correct number of child filters");
}
function test_write(t) {
t.plan(1);
// read first - testing that write produces the ogc:Filter element above
var parser = new OpenLayers.Format.Filter.v1_1_0();
var xml = new OpenLayers.Format.XML();
var filter = parser.read(xml.read(test_xml).documentElement);
var node = parser.write(filter);
t.xml_eq(node, test_xml, "filter correctly written");
}
function test_matchCase(t) {
var parser = new OpenLayers.Format.Filter.v1_1_0();
var xml = new OpenLayers.Format.XML();
var cases = [{
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsEqualTo>' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Filter>',
exp: true
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsEqualTo matchCase="1">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Filter>',
exp: true
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsEqualTo matchCase="true">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Filter>',
exp: true
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsEqualTo matchCase="0">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Filter>',
exp: false
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsEqualTo matchCase="0">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsEqualTo>' +
'</ogc:Filter>',
exp: false
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsNotEqualTo matchCase="true">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsNotEqualTo>' +
'</ogc:Filter>',
exp: true
}, {
str:
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
'<ogc:PropertyIsNotEqualTo matchCase="false">' +
'<ogc:PropertyName>cat</ogc:PropertyName>' +
'<ogc:Literal>dog</ogc:Literal>' +
'</ogc:PropertyIsNotEqualTo>' +
'</ogc:Filter>',
exp: false
}];
t.plan(cases.length);
var filter, c;
for(var i=0; i<cases.length; ++i) {
c = cases[i];
filter = parser.read(xml.read(c.str).documentElement);
t.eq(filter.matchCase, c.exp, "case " + i);
}
}
function test_BBOX(t) {
t.plan(1);
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
property: "the_geom",
value: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: "EPSG:4326"
});
var out =
'<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" srsName="EPSG:4326">' +
'<gml:lowerCorner>-90 -180</gml:lowerCorner>' +
'<gml:upperCorner>90 180</gml:upperCorner>' +
'</gml:Envelope>' +
'</ogc:BBOX>' +
'</ogc:Filter>';
var parser = new OpenLayers.Format.Filter.v1_1_0();
var node = parser.write(filter);
t.xml_eq(node, out, "bbox correctly written");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -53,6 +53,7 @@
<li>Format/SLD/v1_0_0.html</li> <li>Format/SLD/v1_0_0.html</li>
<li>Format/Filter.html</li> <li>Format/Filter.html</li>
<li>Format/Filter/v1_0_0.html</li> <li>Format/Filter/v1_0_0.html</li>
<li>Format/Filter/v1_1_0.html</li>
<li>Format/WKT.html</li> <li>Format/WKT.html</li>
<li>Format/WMC.html</li> <li>Format/WMC.html</li>
<li>Format/WMC/v1_1_0.html</li> <li>Format/WMC/v1_1_0.html</li>