najjah

http://stackoverflow.com/questions/36415307

Apr 4th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.32 KB | None | 0 0
  1. CensusSectorGroup findClosestNEQuadrant(BigDecimal latitude, BigDecimal longitude) {
  2.     String whereClause = 'WHERE SG.latitude >= :latitude AND SG.longitude >= :longitude'
  3.     return runQuadrantQueryWith(whereClause, latitude, longitude)
  4. }
  5.  
  6. private CensusSectorGroup runQuadrantQueryWith (String whereClause, BigDecimal latitude, BigDecimal longitude) {
  7.     String jpql = MAIN_QUERY + whereClause + ORDER_BY
  8.     Query query = em.createQuery(jpql)
  9.     query.setParameter("latitude", latitude)
  10.     query.setParameter("longitude", longitude)
  11.     query.setMaxResults(MAX_RESULTS_QTT)
  12.     List<Quadrant> quadrant= query.getResultList()
  13.     return quadrant?.get(0)?.group
  14. }
  15.  
  16. /* CONSTANTS */
  17. private static final int MAX_RESULTS_QTT = 1
  18. private static final String MAIN_QUERY = "SELECT SG," +
  19.                         "111.045 * " +
  20.                         "FUNC('DEGREES'," +
  21.                         "FUNC('ACOS'," +
  22.                         "FUNC('COS'," +
  23.                         "FUNC('RADIANS',:latitude)" +
  24.                         ") * FUNC('COS'," +
  25.                         "FUNC('RADIANS',SG.lat_centroid)" +
  26.                         ") * FUNC('COS'," +
  27.                         "FUNC('RADIANS',:longitude) - FUNC('RADIANS',SG.long_centroid)" +
  28.                         ") + FUNC('SIN'," +
  29.                         "FUNC('RADIANS',:latitude)" +
  30.                         ") * FUNC('SIN'," +
  31.                         "FUNC('RADIANS',SG.lat_centroid)))" +
  32.                         ") AS distance_in_km" +
  33.                         "FROM CensusSectorGroup SG"
  34. private static final String ORDER_BY = "ORDER BY distance_in_km ASC"
Add Comment
Please, Sign In to add comment