Find nearest locations to a point with sql in Grails 2.1.1

323 Views Asked by At

I am using Grails 2.1.1 and have to find all nearest locations for a given point. My code is looking like this (works in my app):

def findNearLocations(double latitude, double longitude) {
    def locationsFromQuery = Location.executeQuery(
        "SELECT id,(6371 * 2 * ASIN(SQRT(POWER(SIN((:ulatitude - abs(latitude)) * pi()/180 / 2),2) +" +
        "COS(:ulatitude * pi()/180 ) * COS(abs(latitude) * pi()/180) *" +
        "POWER(SIN((:ulongitude - longitude) * pi()/180 / 2), 2))))*1000 as distance " +
        "FROM Location WHERE is_public = TRUE ORDER BY distance", //  HAVING distance < 1000
        [ulatitude: latitude.toString().toDouble(), ulongitude: longitude.toString().toDouble()])
    // do stuff with locationsFromQuery
}

The formula for the computation of the nearest locations I used for is this stack overflow post but I cannot use the HAVING clause like in the comment. If I use it, I get an hql.hibernate exception, saying, that there is an unexpected identifier HAVING. But why this is working in nearly all examples in google but not working for me?

I am using the h2 database in the memory.

0

There are 0 best solutions below