I am using Hibernate, Postgre, PostGIS and Grails.

To search all the users within given radius I am using HibernnateSpetial and all works fine.

def criteria = sessionFactory.currentSession.createCriteria(User)
criteria.createAlias('location', 'location')
criteria.add(SpatialRestrictions.within("location.point", boundingPolygon))
criteria.list()

Now, I also want the distance of all users from given point in same criteria execution.

def criteria = sessionFactory.currentSession.createCriteria(User)
criteria.createAlias('location', 'location')
criteria.add(SpatialRestrictions.within("location.point", boundingPolygon))
String[] columnAlias = new String[1]
columnAlias[0] = "distance"
org.hibernate.type.Type[] types = new StandardBasicTypes[1]
types[0] = StandardBasicTypes.DOUBLE
criteria.setProjection(Projections.sqlProjection("ST_Distance(point, ST_GeomFromText('POINT(-84.3346473 33.9462125)', 4326)) as distance", columnAlias, types))
criteria.list()

It returns me the distance between Geometry(point) stored in database and point supplied in SQL, but ignores the attributes of User domain. I got something like this - [0.0, 0.20430178027717, 0.0] after criteria execution.

I am wondering if I can get the all Users + distance of each user from given lat/lng. I could use Groovy/Java code to calculate distance on results but I need the sorted results from criteria execution so that I can apply pagination as well.

没有正确的解决方案

其他提示

If you want only to get the distance back, it seems to be easier to calculate it directly in your groovy code, like:

giveMeAllHitsFromGIS().each{
  it.distance = it.point1.distanceTo( it.point2 )
}

Your code is already a bit overloaded with low-level hibernate/spatial stuff, and it would get less readable should you increase the complexity of your spatial queries

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top