我是Hibernate Spatial的新手,我正在尝试对给定半径内的对象执行简单查询。我使用Google地图和其他来源的数据在我的数据库中创建了许多条目,其中包含与纬度和经度相对应的属性。此属性在我的Entity类中定义如下:
@Column
@Type(type = "org.hibernate.spatial.GeometryType")
private Point coordinates = null;
我现在试图弄清楚如何搜索坐标在半径范围内的所有实体对象 X 从给定点开始的公里数。例如,我想找到落在该点半径50公里范围内的物体(12.34567,-76.54321)。但是,我找不到任何可以解释如何在Hibernate 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;
}
要生成圆,请参阅 这个资源 (搜索“弧,圆和曲线”)。
再次从那里复制/粘贴:
//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
然后在查询中使用它们。要计算距离,请检查 欧几里德距离公式。
看到 这个资源 有关“空间查询”的教程,这是一种特殊的方言和 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;
}
要生成圆,请参阅 这个资源 (搜索“弧,圆和曲线”)。
再次从那里复制/粘贴:
//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
然后在查询中使用它们。要计算距离,请检查 欧几里德距离公式。