From 89cc9f157f832d02e294c0b934db112e34c36cf6 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 4 Sep 2008 20:52:05 +0000 Subject: [PATCH] Adding OpenLayers.Filter.Spatial. Thanks to bartvde for the original patch. Thanks elemoine for adding the evaluate method and test. I'm putting this in so we can move on some other tickets. r=me (closes #1719) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7952 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers.js | 1 + lib/OpenLayers/Filter/Spatial.js | 109 +++++++++++++++++++++++++++++++ tests/Filter/Spatial.html | 78 ++++++++++++++++++++++ tests/list-tests.html | 1 + 4 files changed, 189 insertions(+) create mode 100644 lib/OpenLayers/Filter/Spatial.js create mode 100644 tests/Filter/Spatial.html diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index d52ce17f7b..43cdff7f78 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -195,6 +195,7 @@ "OpenLayers/Filter/FeatureId.js", "OpenLayers/Filter/Logical.js", "OpenLayers/Filter/Comparison.js", + "OpenLayers/Filter/Spatial.js", "OpenLayers/Format.js", "OpenLayers/Format/XML.js", "OpenLayers/Format/GML.js", diff --git a/lib/OpenLayers/Filter/Spatial.js b/lib/OpenLayers/Filter/Spatial.js new file mode 100644 index 0000000000..8a2604370a --- /dev/null +++ b/lib/OpenLayers/Filter/Spatial.js @@ -0,0 +1,109 @@ +/* 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/Filter.js + */ + +/** + * Class: OpenLayers.Filter.Spatial + * This class represents a spatial filter. + * Currently implemented: BBOX, DWithin and Intersects + * + * Inherits from + * - + */ +OpenLayers.Filter.Spatial = OpenLayers.Class(OpenLayers.Filter, { + + /** + * APIProperty: type + * {String} Type of spatial filter. + * + * The type should be one of: + * - OpenLayers.Filter.Spatial.BBOX + * - OpenLayers.Filter.Spatial.INTERSECTS + * - OpenLayers.Filter.Spatial.DWITHIN + */ + type: null, + + /** + * APIProperty: property + * {String} Name of the context property to compare. + */ + property: null, + + /** + * APIProperty: value + * { || } The bounds or geometry + * to be used by the filter. Use bounds for BBOX filters and geometry + * for INTERSECTS or DWITHIN filters. + */ + value: null, + + /** + * APIProperty: distance + * {Number} The distance to use in a DWithin spatial filter. + */ + distance: null, + + /** + * APIProperty: distanceUnits + * {String} The units to use for the distance, e.g. 'm'. + */ + distanceUnits: null, + + /** + * Constructor: OpenLayers.Filter.Spatial + * Creates a spatial filter. + * + * Parameters: + * options - {Object} An optional object with properties to set on the + * filter. + * + * Returns: + * {} + */ + initialize: function(options) { + OpenLayers.Filter.prototype.initialize.apply(this, [options]); + }, + + /** + * Method: evaluate + * Evaluates this filter for a specific feature. + * + * Parameters: + * feature - {} feature to apply the filter to. + * + * Returns: + * {Boolean} The feature meets filter criteria. + */ + evaluate: function(feature) { + var intersect = false; + switch(this.type) { + case OpenLayers.Filter.Spatial.BBOX: + case OpenLayers.Filter.Spatial.INTERSECTS: + if(feature.geometry) { + var geom = this.value; + if(this.value.CLASS_NAME == "OpenLayers.Bounds") { + geom = this.value.toGeometry(); + } + if(feature.geometry.intersects(geom)) { + intersect = true; + } + } + break; + default: + OpenLayers.Console.error( + OpenLayers.i18n("filterEvaluateNotImplemented")); + break; + } + return intersect; + }, + + CLASS_NAME: "OpenLayers.Filter.Spatial" +}); + +OpenLayers.Filter.Spatial.BBOX = "BBOX"; +OpenLayers.Filter.Spatial.INTERSECTS = "INTERSECTS"; +OpenLayers.Filter.Spatial.DWITHIN = "DWITHIN"; diff --git a/tests/Filter/Spatial.html b/tests/Filter/Spatial.html new file mode 100644 index 0000000000..302650869e --- /dev/null +++ b/tests/Filter/Spatial.html @@ -0,0 +1,78 @@ + + + + + + + + diff --git a/tests/list-tests.html b/tests/list-tests.html index adf2aaf8e8..68cb559996 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -38,6 +38,7 @@
  • Filter/Comparison.html
  • Filter/FeatureId.html
  • Filter/Logical.html
  • +
  • Filter/Spatial.html
  • Format.html
  • Format/GeoJSON.html
  • Format/GeoRSS.html