1

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.

manish
  • 152
  • 2
  • 16
  • Hi manish, I have no answer to your question but I am trying to use postGIS with grails with no success. Could you point out to up to date resources that show how to do this? – a.benain Jun 28 '14 at 09:13

1 Answers1

0

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

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Actually I need the domain(User) within given radius and their distance from given center(point). I could use Groovy/Java method to calculate distance on results but I need the sorted results so that I can apply pagination as well. – manish Mar 01 '14 at 02:03