Steve Steve - 1 year ago 115
Javascript Question

Geoserver - filtering points using DWITHIN

First of all I am new to Geoserver and Openlayers. I have been trying to get this working on the Geoserver side using the Layer preview page to view my layer using the Openlayer viewer. I would be implementing it as an Openlayers WFS GET request in the end.

What I am trying to accomplish is to return features that are within a certain distance of different points. (ie. within 5km of a school)

I am using the Layer preview page on Geoserver . I have added the CQL filter of "DWITHIN(GEOM, POINT(-60.2 46.1), 0.05, kilometers)". It has limited the amount of points that were returned but I am unsure if it is filtering correctly.

My questions are as follows:

  1. From my understanding because of this bug I have to use degrees for my unit. How can I convert on client side using javascript/openlayers 3 to change 5km into a degree value?

  2. Can I add multiple DWITHIN filters to say if a point is within 5km of one point and 10km of another?

  3. Can I filter based off feature type? ie return point within 5km of a School AND 10km of a Hospital

Answer Source

After spending some time with this I was able to answer my own question.

First of all doing some searching I determined 1 degree = 111.325 kms approximately.

To add multiple DWITHIN filters all you have to do is add an AND between each one. If it is in the WFS format you may have to add %20AND%20 so the spaces are included.

To do a DWITHIN of a certain location you would append the following to your wfs. The point is the long/lat, 0.05 is the degrees from the point, kilometers is the unit that is passed in. When using geoserver it will default to degrees as the link in the original question states.

&CQL_FILTER=DWITHIN(GEOM,Point(-60.2 46.1),0.05,kilometers)

This will return any of the features from your WFS layer that are within the degrees of the point specified.

To filter off of another layer I used info from the following URL

Firs you must install the proper querylayer module to your version of Geoserer as the link states.

To do the filter based of feature name I used the following CQL_filter:


This will return any features from the WFS layer you are requesting that are within the degrees specified of a feature with the AssetID of 1 from the layer 'Workspace:AssetLayer'.

ie if school had an AssetTypeID of 1, this would return all features from the original layer that were within 0.02 degrees of a school on the 'Workspace:AssetLayer' layer.

The "GEOM" value should be the name of column that holds your geometry data in SQL.

Also, I encoded the last parenthesis in the CQL_Filter "%29" because I found when making the request through openlayers it was stripping it off.

As I said I am new to openlayers/geoserver so this may not work in every case but this is how I figured it out. I am thinking in different projections there may be some changes. Hope this can get you on the right track.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download