[GIS] How to properly utilize DWITHIN Spatial Filter in OpenLayers

distancefilteropenlayers-2spatial-index

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                                  
alert(newFilter.evaluate(filteredPoint));

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       
}); 
alert(newFilter2.evaluate(filteredPoint));

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.

Best Answer

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)