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.