java - 如何为检查点建立JPA谓词属于给定的多边形
问题描述
我正在尝试构建javax.persistence.criteria.Predicate
确定点属于给定多边形的方法。
我想用来构建查询的谓词。我想使用 Criteria API 从 dto 动态构建对给定参数的查询。
import org.geolatte.geom.codec.Wkt;
import org.hibernate.spatial.predicate.JTSSpatialPredicates;
import javax.persistence.criteria.Predicate;
public List<EComparable> searchByParams(SearchFormDto searchFormDto) {
var cb = entityManager.getCriteriaBuilder();
var query = cb.createQuery(EComparable.class);
var root = query.from(EComparable.class);
Predicate[] predicates = new Predicate[10];
predicates[0] = JTSSpatialPredicates.contains(
cb,
Wkt.fromWkt("POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))"), // Hardcoded for test, will be given from dto
root.get("geom").type() // I'm really not shure about that part too
);
// predicates[...] = ...
query.select(root).where(predicates);
var resultList = entityManager.createQuery(query).getResultList();
return resultList;
}
所以我找到JTSSpatialPredicates
了,但不明白如何采用它。
论据的问题:
该类仅提供两种变体contains
:
(...,表达,表达)
org.hibernate.spatial.predicate.JTSSpatialPredicates#contains(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.Expression<? extends org.locationtech.jts.geom.Geometry>, javax.persistence.criteria.Expression<? extends org.locationtech.jts.geom.Geometry>)
或(...,表达式,几何)
org.hibernate.spatial.predicate.JTSSpatialPredicates#contains(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.Expression<? extends org.locationtech.jts.geom.Geometry>, org.locationtech.jts.geom.Geometry)
但我需要理解(...,几何,表达式)。给定多边形包含geom
列中的点。
如何弄清楚?
geom
是存储点位置的列名(db type geometry
)。
需要选择属于给定多边形的所有行并进行一些额外的过滤。
环境:Java 16、Spring Boot、Hibernate、Postgis、Postgresql。
解决方案
只需要将几何图形包装到表达式中literal
并更改第二个参数:
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
//...
predicates[0] = JTSSpatialPredicates.contains(
cb,
cb.literal(wktReader.read("POLYGON ((0 0, 0 70, 70 70, 70 0, 0 0))")),
root.get("geom").as(Geometry.class)
);
推荐阅读
- javascript - jQuery函数重复的问题
- linux - 如果编译器选项与以前使用的选项不同,则使目标过时
- android - TalkBack 屏幕阅读器可访问性不适用于 android 中的不可点击的图像视图
- json - 使用 JMeter 对 API 的 HTTP 请求中的 JSON
- floating-point - 次正规数是否有隐含的 0?
- spring-boot - swagger 无法识别 api 描述
- swift - 如何在两个不同的键中解码具有相同变量的 JSON?
- php - 如何在php中获取excel的列名
- caching - Infinispan [在现有集群中添加新节点]
- reactjs - this.state.file.name 不呈现为反应元素