Sunday 15 June 2014

Advanced search with distances using NHibernate and SQL Server Geography -


I got an existing advanced search method which is a collection survival FormCollection checks Search criteria, and if present, adds a criteria for search such as

  public IList  

I have also found basic distance searching to find out how far every residence is from the search criteria HBM for the query

  & Lt; Sql-query name = "Residence.Nearest" & gt; & Lt; Return alias = "residence" class = "residences domain. Estimate, residence" /> & Lt; Return-scaler column = "distance" type = "float" /> SELECT R. *, dbo.GetDistance (: point, R.Coordinate) AS R distance Distance from R & lt; Distance by 10 orders & lt; / Sql-query & gt;  

I had to define a function to calculate the distance, because there was no way to get NHibernate to avoid colon geographical function:

  Create Function dbo.GetDistance (@firstPoint nvarchar (100), @secondPoint geometry) returns returns Geography :: STGeomFromText (@firstPoint, 4326) .STDistance start float (@ secondPoint.STAsText ()) / 1609.344 end  

and the repository calls the named query as such:

  return session .GetNamedQuery ("Residence.Nearest") .Setstring C ( "point", String.Format ( "POINT ({0} {1})", latitude, longitude). List ();  

So my question is: I have two How to add (or start with scratch), so I can filter advanced search results to include only residences within 10 miles of search location?

Update I've tried to use NHibernate.Spatial with the following code:

  criteria.Add (SpatialExpression.IsWithinDistance ( "coordination", the new coordinates (latitude, longitude), 10));  

but SpatialExpression.IsWithinDistance returned a System.NotImplementedException .

have you seen? This can provide an easy solution to your problem.

optional ICriterion is to implement your own - if you get the AbstractCriterion if it is not too difficult and Target your special database platform. After this, you can combine your distance function with other parameters.


No comments:

Post a Comment