Hibernate Spatial - X 킬로미터 반경 내의 쿼리?
-
21-12-2019 - |
문제
나는 동시에 공간적으로 동시에 새로운 반지름 내에 간단한 쿼리를 수행하려고 노력하고 있습니다.나는 Google지도 및 기타 소스의 데이터를 사용하여 위도 및 경도에 해당하는 속성을 사용하여 데이터베이스에 여러 항목을 작성했습니다.이 속성은 내 엔티티 클래스에서 다음과 같이 정의됩니다.
@Column
@Type(type = "org.hibernate.spatial.GeometryType")
private Point coordinates = null;
.
이제 주어진 지점에서 X 킬로미터의 반경에 속하는 좌표가있는 모든 엔티티 객체를 검색하는 방법을 알아 내려고합니다.예를 들어, 포인트의 50 킬로미터 반지름 (12.34567, -76.54321) 이내에 떨어지는 물체를 찾고 싶습니다.그러나 Hibernate spatial 에서이 작업을 수행하는 방법을 설명하는 예제 또는 자습서를 찾을 수 없습니다.
이 같은 쿼리가 어떻게 구성되는지에 대한 정보를 제공 할 수 있습니까?
해결책
"NoReferrrer">이 리소스 "spatial 특수 방언 및 JTS 라이브러리 (오픈 소스)를 쿼리합니다. 기본적으로 다음을 수행합니다 (참조 된 페이지에서 복사 / 붙여 넣기) :
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import util.JPAUtil;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;
.
private List find(String wktFilter) {
Geometry filter = wktToGeometry(wktFilter);
EntityManager em = JPAUtil.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("select e from Event e where within(e.location, :filter) = true", Event.class);
query.setParameter("filter", filter);
return query.getResultList();
}
private Geometry wktToGeometry(String wktPoint) {
WKTReader fromText = new WKTReader();
Geometry geom = null;
try {
geom = fromText.read(wktPoint);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
return geom;
}
.
원을 생성하려면 이 리소스 (검색 "Arcs, Circles and Curves"의 경우). 다시 한 번의 복사 / 붙여 넣기 :
//this method replaces the above wktToGeometry() method
private static Geometry createCircle(double x, double y, final double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new Coordinate(x, y));//there are your coordinates
shapeFactory.setSize(RADIUS * 2);//this is how you set the radius
return shapeFactory.createCircle();
}
.
다음과 같은 일부 필드 (insertable=false, updatable=false
와 맵핑 된)를 추가하여 org.hibernate.spatial.GeometryType
에서 사용하는 것과 동일한 열에 맵핑 된 다음 쿼리에서 사용하는 해결 방법을 항상 추가하십시오. 거리를 계산하려면 유클리드 거리 공식 .