Class BoundingboxJdbcFunction

java.lang.Object
ch.tocco.nice2.persist.core.api.hibernate.query.AbstractJdbcFunction
ch.tocco.nice2.optional.geolocation.impl.jdbc.BoundingboxJdbcFunction
All Implemented Interfaces:
JdbcFunction

@Component public class BoundingboxJdbcFunction extends AbstractJdbcFunction
Creates a boolean expression that evaluates to true, if a given point is within the bounds of a given rectangle.

It expects exactly 6 arguments in order, where

 1. `x` is the x coordinate of a certain position
 2. `y` is the y coordinate of a certain position
 3. `ul.y` is the y coordinate of the upper left corner of the rectangle
 4. `ul.x` is the x coordinate of the upper left corner of the rectangle
 5. `lr.y` is the y coordinate of the lower right corner of the rectangle
 6. `lr.x` is the x coordinate of the lower right corner of the rectangle
 
The function writes the following expression in SQL:


 (x >= ul.x) and (x < = lr.x) and (y >= lr.y) and (y <= ul.y)
 
Selecting those (x,y) points that are within the bounds of (ul, lr).

Example usage

Use it in queries like the following example (here, lat=y and lng=x, respectively, as used in geographical coordinate system):


 Query q = ctx.compileQuery("find Cities where INBOUNDINGBOX(ps_lat, pos_lng, :ul_lat, :ul_lng, :lr_lat, :lr_lng) == true");
 Position zurich = new Position(...);
 Position[] box = zurich.getBoundingBox(Distance.kilometer("20"));
 q.setParameter("ul_lat", box[0].getLatitude());
 q.setParameter("ul_lng", box[0].getLongitude());
 q.setParameter("lr_lat", box[1].getLatitude());
 q.setParameter("lr_lng", box[1].getLongitude());
 
This will select all cities that are at maximum 20km (are within a square around Zurich with side length=40km) away from Zurich (assuming that the "Cities" entity has two fields "pos_lat" and "pos_lng" of respective type.)
  • Constructor Details

    • BoundingboxJdbcFunction

      public BoundingboxJdbcFunction(TypeManager typeManager)
  • Method Details

    • createFunction

      public org.hibernate.dialect.function.SQLFunction createFunction(org.hibernate.type.TypeResolver typeResolver, SqlWriter sqlWriter)
      Returns:
      an SQLFunction that contains the raw sql statement that is executed on the db.
      See Also:
      • SQLFunctionTemplate
    • validateArguments

      public void validateArguments(jakarta.persistence.criteria.Expression<?>[] arguments) throws JdbcFunctionException
      Throws:
      JdbcFunctionException
    • argumentCount

      public int argumentCount()
      Returns:
      the number of expected arguments.
    • argumentOrder

      public int[] argumentOrder()
      Description copied from interface: JdbcFunction
      Order of parameters as required in SQL statement. This method must be implemented when the order or count of query parameters is different to the SQL parameters. The returned array lists indices of params in order as they are used for the SQL query.