org.hibernate.spatial.predicate.GeolatteSpatialPredicates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-spatial Show documentation
Show all versions of hibernate-spatial Show documentation
Integrate support for Spatial/GIS data into Hibernate O/RM
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.spatial.predicate;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.spatial.SpatialFunction;
import org.geolatte.geom.Geometry;
import static org.hibernate.spatial.CommonSpatialFunction.ST_CONTAINS;
import static org.hibernate.spatial.CommonSpatialFunction.ST_CROSSES;
import static org.hibernate.spatial.CommonSpatialFunction.ST_DISJOINT;
import static org.hibernate.spatial.CommonSpatialFunction.ST_EQUALS;
import static org.hibernate.spatial.CommonSpatialFunction.ST_INTERSECTS;
import static org.hibernate.spatial.CommonSpatialFunction.ST_OVERLAPS;
import static org.hibernate.spatial.CommonSpatialFunction.ST_TOUCHES;
import static org.hibernate.spatial.CommonSpatialFunction.ST_WITHIN;
/**
* {@link JTSSpatialPredicates}, but for geolatte-geom.
*
* @author Daniel Shuy
* @deprecated Use {@link org.hibernate.spatial.criteria.GeolatteSpatialCriteriaBuilder GeolatteSpatialCriteriaBuilder} instead
*/
@Deprecated(since = "6.2")
@SuppressWarnings("rawtypes")
public class GeolatteSpatialPredicates {
protected GeolatteSpatialPredicates() {
}
/**
* Create a predicate for testing the arguments for "spatially equal" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially equal" predicate
*
* @see JTSSpatialPredicates#eq(CriteriaBuilder, Expression, Expression)
*/
public static Predicate eq(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_EQUALS.name(), boolean.class, geometry1, geometry2
)
);
}
/**
* Create a predicate for testing the arguments for "spatially equal" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially equal" predicate
*
* @see #eq(CriteriaBuilder, Expression, Expression)
*/
public static Predicate eq(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return eq( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially within" predicate
*
* @see JTSSpatialPredicates#within(CriteriaBuilder, Expression, Expression)
*/
public static Predicate within(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_WITHIN.name(), boolean.class, geometry1, geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially within" predicate
*
* @see #within(CriteriaBuilder, Expression, Expression)
*/
public static Predicate within(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return within( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially contains" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially contains" predicate
*
* @see JTSSpatialPredicates#contains(CriteriaBuilder, Expression, Expression)
*/
public static Predicate contains(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_CONTAINS.name(), boolean.class, geometry1, geometry2
)
);
}
/**
* Create a predicate for testing the arguments for "spatially contains" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially contains" predicate
*
* @see #contains(CriteriaBuilder, Expression, Expression)
*/
public static Predicate contains(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return contains( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially crosses" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially crosses" predicate
*
* @see JTSSpatialPredicates#crosses(CriteriaBuilder, Expression, Expression)
*/
public static Predicate crosses(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_CROSSES.name(), boolean.class, geometry1, geometry2
)
);
}
/**
* Create a predicate for testing the arguments for "spatially crosses" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially crosses" predicate
*
* @see #crosses(CriteriaBuilder, Expression, Expression)
*/
public static Predicate crosses(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return crosses( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially disjoint" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially disjoint" predicate
*
* @see JTSSpatialPredicates#disjoint(CriteriaBuilder, Expression, Expression)
*/
public static Predicate disjoint(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_DISJOINT.name(), boolean.class, geometry1, geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially disjoint" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially disjoint" predicate
*
* @see #disjoint(CriteriaBuilder, Expression, Expression)
*/
public static Predicate disjoint(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return disjoint( cb, geometry1, cb.value( geometry2 ) );
}
/**
* Create a predicate for testing the arguments for "spatially intersects" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially intersects" predicate
*
* @see JTSSpatialPredicates#intersects(CriteriaBuilder, Expression, Expression)
*/
public static Predicate intersects(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_INTERSECTS.name(), boolean.class, geometry1, geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially intersects" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially intersects" predicate
*
* @see #intersects(CriteriaBuilder, Expression, Expression)
*/
public static Predicate intersects(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return intersects( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially overlaps" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially overlaps" predicate
*
* @see JTSSpatialPredicates#overlaps(CriteriaBuilder, Expression, Expression)
*/
public static Predicate overlaps(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_OVERLAPS.name(), boolean.class, geometry1, geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially overlaps" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially overlaps" predicate
*
* @see #overlaps(CriteriaBuilder, Expression, Expression)
*/
public static Predicate overlaps(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return overlaps( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially touches" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
*
* @return "spatially touches" predicate
*
* @see JTSSpatialPredicates#touches(CriteriaBuilder, Expression, Expression)
*/
public static Predicate touches(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( ST_TOUCHES.name(), boolean.class, geometry1, geometry2 )
);
}
/**
* Create a predicate for testing the arguments for "spatially touches" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
*
* @return "spatially touches" predicate
*
* @see #touches(CriteriaBuilder, Expression, Expression)
*/
public static Predicate touches(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return touches( cb, geometry1, cb.value( geometry2 )
);
}
/**
* Create a predicate for testing the arguments for bounding box overlap constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression whose bounding box to use in the comparison
*
* @return bounding box overlap predicate
*
*/
public static Predicate filter(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( SpatialFunction.filter.toString(), boolean.class, geometry1, geometry2 )
);
}
// /**
// * Create a predicate for testing the arguments for bounding box overlap constraint.
// *
// * @param criteriaBuilder CriteriaBuilder
// * @param geometry1 geometry expression
// * @param geometry2 geometry value whose bounding box to use in the comparison
// *
// * @return bounding box overlap predicate
// *
// * @see GeolatteFilterPredicate
// * @see JTSSpatialPredicates#filter(CriteriaBuilder, Expression, org.locationtech.jts.geom.Geometry)
// */
// public static Predicate filter(
// CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
// Geometry geometry2) {
// return booleanExpressionToPredicate(
// criteriaBuilder,
// criteriaBuilder.function( SpatialFunction.filter.toString(), Geometry.class,
// geometry1, geometry2
// )
// );
// }
//
// /**
// * Create a predicate for testing the arguments for bounding box overlap constraint.
// *
// * @param criteriaBuilder CriteriaBuilder
// * @param geometry geometry expression
// * @param envelope envelope or bounding box to use in the comparison
// *
// * @return bounding box overlap predicate
// *
// * @see GeolatteFilterPredicate
// * @see JTSSpatialPredicates#filterByPolygon(CriteriaBuilder, Expression, org.locationtech.jts.geom.Envelope, int)
// */
// public static Predicate filterByPolygon(
// CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry,
// Envelope envelope) {
// return new GeolatteFilterPredicate( criteriaBuilder, geometry, envelope );
// }
/**
* Create a predicate for testing the arguments for "distance within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
* @param distance distance expression
*
* @return "distance within" predicate
*
* @see JTSSpatialPredicates#distanceWithin(CriteriaBuilder, Expression, Expression, Expression)
*/
public static Predicate distanceWithin(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2, Expression distance) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function(
SpatialFunction.dwithin.toString(),
boolean.class,
geometry1,
geometry2,
distance
)
);
}
/**
* Create a predicate for testing the arguments for "distance within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
* @param distance distance expression
*
* @return "distance within" predicate
*
* @see #distanceWithin(CriteriaBuilder, Expression, Expression, Expression)
*/
public static Predicate distanceWithin(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2, Expression distance) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return distanceWithin( cb, geometry1, cb.value( geometry2 ), distance
);
}
/**
* Create a predicate for testing the arguments for "distance within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry value
* @param distance distance value
*
* @return "distance within" predicate
*
* @see #distanceWithin(CriteriaBuilder, Expression, Expression, Expression)
*/
public static Predicate distanceWithin(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Geometry geometry2, double distance) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return distanceWithin(
cb,
geometry1,
cb.value( geometry2 ),
cb.value( distance )
);
}
/**
* Create a predicate for testing the arguments for "distance within" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry1 geometry expression
* @param geometry2 geometry expression
* @param distance distance value
*
* @return "distance within" predicate
*
* @see #distanceWithin(CriteriaBuilder, Expression, Expression, Expression)
*/
public static Predicate distanceWithin(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry1,
Expression extends Geometry> geometry2, double distance) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return distanceWithin( cb, geometry1, geometry2, cb.value( distance )
);
}
/**
* Create a predicate for testing the arguments for "having srid" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry geometry expression
* @param srid SRID expression
*
* @return "having srid" predicate
*
* @see JTSSpatialPredicates#havingSRID(CriteriaBuilder, Expression, Expression)
*/
public static Predicate havingSRID(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry,
Expression srid) {
return criteriaBuilder.equal(
criteriaBuilder.function( SpatialFunction.srid.toString(), int.class, geometry ),
srid
);
}
/**
* Create a predicate for testing the arguments for "having srid" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry geometry expression
* @param srid SRID expression
*
* @return "having srid" predicate
*
* @see #havingSRID(CriteriaBuilder, Expression, Expression)
*/
public static Predicate havingSRID(
CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry,
int srid) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return havingSRID( cb, geometry, cb.value( srid ) );
}
/**
* Create a predicate for testing the arguments for "is empty" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry geometry expression
*
* @return "is empty" predicate
*
* @see JTSSpatialPredicates#isEmpty(CriteriaBuilder, Expression)
*/
public static Predicate isEmpty(CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry) {
return booleanExpressionToPredicate(
criteriaBuilder,
criteriaBuilder.function( SpatialFunction.isempty.toString(), boolean.class, geometry )
);
}
/**
* Create a predicate for testing the arguments for "is not empty" constraint.
*
* @param criteriaBuilder CriteriaBuilder
* @param geometry geometry expression
*
* @return "is not empty" predicate
*
* @see JTSSpatialPredicates#isNotEmpty(CriteriaBuilder, Expression)
*/
public static Predicate isNotEmpty(CriteriaBuilder criteriaBuilder, Expression extends Geometry> geometry) {
return isEmpty( criteriaBuilder, geometry )
.not();
}
private static Predicate booleanExpressionToPredicate(
CriteriaBuilder criteriaBuilder,
Expression expression) {
return criteriaBuilder.equal( expression, true );
}
}