I'm currently trying to naivly get the k-nearest neighbors of a set of points, given a value k, a coordinate to use as center and a radius serving as the max distance to find points within. I'm using geographical points (SRID 4326) on a MSSQL 2008 database.
The neighbors are naivly found ordering the query by the distance to the point and limiting the result. My trouble starts at limiting the points by the given radius. The distances returned by the Distance function are much larger than expected, which I understand to be a caused by the SRID 4326. This is OK as along as it is only used for ordering, but when I have to compare these values to a relative distance, say 200 metres, these large numbers wont do.
My question then is: is there a smarter way of limiting the points by a radius using NHibernate Spatial queries, or is there a way to convert this radius into the some meassurement similar to that used by the Distance function?
This is my query as it looks now:
output = NHibernateSession.GetSession().CreateQuery(@"select p from POI p
where NHSP.Distance(p.PointCoord, :coord) <= :maxDistance
order by NHSP.Distance(p.PointCoord, :coord)")
.SetParameter("coord", coord,
NHibernateUtil.Custom(typeof(Wgs84GeographyType)))
.SetParameter<double>("maxDistance", radius)
.SetMaxResults(k)
.Enumerable<POI>();
Just as an example I have these two points: POINT(7 1) POINT(7 3)
My expected distance is 2, but the distance calculated by the mssql STDistance function gives 221151.479533501 as a result. I just cant get my mind to make sense about this.