# How to properly utilize DWITHIN Spatial Filter in OpenLayers

Given the following code:

var points = [];
points.push(new OpenLayers.Geometry.Point(-110, 38));
points.push(new OpenLayers.Geometry.Point(-108, 40));
var lineString = new OpenLayers.Geometry.LineString(points);
var filteredPoint = new OpenLayers.Geometry.Point(-108.25, 39.78);
var newFilter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.DWITHIN,
distanceUnits: 'm',
distance: 92600,
value: lineString
}); //92600 meters, or 50 nautical miles


When the alert is fired, it results in false. I'm a beginner to OpenLayers, looking for a way in Javascript to determine whether or not a point is within a certain distance, either side of a line. I cannot use a bounding box for this feature, because I want the user to be able to pick any two points and a distance from the line, so an MBR isn't available here. filteredPoint is less than 20nm from the 2nd point in the linestring, so my assumption is that this alert would return true, but alas it is false. Meanwhile, given the following code, in addition to that above:

var bounds = new OpenLayers.Bounds();
bounds.extend(new OpenLayers.LonLat(-110, 38));
bounds.extend(new OpenLayers.LonLat(-108, 40));
bounds.toBBOX();
var newFilter2 = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.BBOX,
value: bounds
});


That code gives a result of true inside of this bounding box.
I would think that the two bits of code are trying to find points in the block below represented by the asterisk. Am I wrong in interpreting DWITHIN as such?


----------                     /
|      * |                 *--/
|  bbox  |                   /
|        |                  /
----------                 /


Any tips or guidance in the right direction would be greatly appreciated. My overall goal is to determine how many points out of ~3700 points I have fall within a distance of the line.

• Did u find any solution for your spatial filter problem, because i am also facing the same and searching for the solution with DWITHIN. If you have a solution, please can you share it . How you solved the problem? – user6238 Mar 6 '12 at 4:39

What i can see from Openlayers source code only BBOX and INTERSECTS filters only implemented. Have a look at the evaluate() method.

You could use WFS getFeature call with CQL filters to filter the features at server or if it is only display purpose, SLD could be implemented with DWITHIN filter. (http://trac.osgeo.org/openlayers/ticket/1543)

• I'm not sure the DWITHIN patch will work for me. Everything I'm doing is client-side using HTML and JavaScript (including the jQuery library). I'm also assuming no connection to the Internet, so no WFS or queries are available. I don't even want to display a map per se, just simply use OpenLayers' math. I may be looking in the wrong direction, or maybe OpenLayers isn't the right solution here. I appreciate your response, and any other comments are appreciated. – js1983 May 6 '11 at 23:00

for DWITHIN spatial filter you need to set a tolerance.

  var lon = 100, lat = 200, tolerance = 10;
filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.DWITHIN,
value: new OpenLayers.Geometry.Point(lon, lat),
distance: tolerance

• tolerance is the parameter set to distance as you can see in the filter. So, no difference from user tried already... – Senthil Apr 26 '11 at 1:41
• No idea why upvote when just tolerance variable name used and it set to distance parameter in Filter call.... :( – Senthil Apr 26 '11 at 9:23

Having looked at the source code I also think that the DWITHIN filter is not (yet) implemented in OpenLayers V2.11 .

One has to be clear though about the two different uses of filters: If used as a Strategy for a vector layer, it is handled by OpenLayes on the client (and there DWITHIN isn't available yet). If however the same filter is used as an option parameter in a WFS query the filtering will be performed on the server and there DWITHIN may be available (see GetCapabilities).

Related to this distinction is the important observation that if the filter geometries are in a different coordinate system to the map, the client filtering in OpenLayers doesn't work; for that the filter coordinates have to be transformed first to the map coordinates. The WFS server however can convert from one to the other on the fly if the projection is specified in the filter.