From 428793cd7ccc023897e799e2727cb0d7de1b5d0c Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 4 Feb 2013 22:50:21 +0100 Subject: [PATCH] New filters for extent and logical filtering These need unit tests. --- src/ol/filter/extentfilter.js | 36 ++++++++++++++++++++ src/ol/filter/logicalfilter.js | 62 ++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/ol/filter/extentfilter.js create mode 100644 src/ol/filter/logicalfilter.js diff --git a/src/ol/filter/extentfilter.js b/src/ol/filter/extentfilter.js new file mode 100644 index 0000000000..3a566c2490 --- /dev/null +++ b/src/ol/filter/extentfilter.js @@ -0,0 +1,36 @@ +goog.provide('ol.filter.Extent'); + +goog.require('ol.Feature'); + + + +/** + * @constructor + * @implements {ol.filter.Filter} + * @param {ol.Extent} extent The extent. + */ +ol.filter.Extent = function(extent) { + + /** + * @type {ol.Extent} + * @private + */ + this.extent_ = extent; + +}; + + +/** + * @return {ol.Extent} The filter extent. + */ +ol.filter.Extent.prototype.getExtent = function() { + return this.extent_; +}; + + +/** + * @inheritDoc + */ +ol.filter.Extent.prototype.evaluate = function(feature) { + return feature.getGeometry().getBounds().intersects(this.extent_); +}; diff --git a/src/ol/filter/logicalfilter.js b/src/ol/filter/logicalfilter.js new file mode 100644 index 0000000000..b702958684 --- /dev/null +++ b/src/ol/filter/logicalfilter.js @@ -0,0 +1,62 @@ +goog.provide('ol.filter.Logical'); +goog.provide('ol.filter.LogicalOperator'); + +goog.require('ol.Feature'); +goog.require('ol.filter.Filter'); + + + +/** + * @constructor + * @implements {ol.filter.Filter} + * @param {Array.} filters Filters to and-combine. + * @param {!ol.filter.LogicalOperator} operator Operator. + */ +ol.filter.Logical = function(filters, operator) { + + /** + * @type {Array.} + * @private + */ + this.filters_ = filters; + + /** + * @type {!ol.filter.LogicalOperator} + */ + this.operator = operator; + +}; + + +/** + * @inheritDoc + */ +ol.filter.Logical.prototype.evaluate = function(feature) { + var filters = this.filters_, + i = 0, ii = filters.length, + operator = this.operator, + start = operator(true, false), + result = start; + while (result === start && i < ii) { + result = operator(result, filters[i].evaluate(feature)); + ++i; + } + return result; +}; + + +/** + * @return {Array.} The filter's filters. + */ +ol.filter.Logical.prototype.getFilters = function() { + return this.filters_; +}; + + +/** + * @enum {!Function} + */ +ol.filter.LogicalOperator = { + AND: /** @return {boolean} result. */ function(a, b) { return a && b; }, + OR: /** @return {boolean} result. */ function(a, b) { return a || b; } +};