Hibernate Spatial - 在X公里范围内查询?
我是Hibernate Spatial的新手,正在尝试对给定半径内的对象执行简单查询。我使用来自Google地图和其他来源的数据在我的数据库中创建了许多与纬度和经度对应的属性的条目。这个属性在我的Entity类中是这样定义的:
I am new to Hibernate Spatial, and am trying to perform a simple query of objects within a given radius. I've created a number of entries in my database with properties corresponding to a latitude and longitude, using data from Google Maps and other sources. This property is defined like this in my Entity class:
@Column
@Type(type = "org.hibernate.spatial.GeometryType")
private Point coordinates = null;
我现在试图找出如何搜索所有具有坐标的实体对象落在距给定点的距离 x 的半径内。例如,我想找到位于该点半径50公里内的物体(12.34567,-76.54321)。但是,我找不到任何示例或教程来解释如何在Hibernate Spatial中执行此操作。
I'm now trying to figure out how to do a search of all entity objects that have coordinates that fall within a radius of x kilometers from a given point. For example, I'd like to find objects that fall within a 50 kilometer radius of the point (12.34567, -76.54321). However, I can't find any examples or tutorials that would explain how to do this in Hibernate Spatial.
任何人都可以提供任何有关如何查询的信息被构建?
Can anyone give me any information on how a query like this can be constructed?
请参阅 这个资源 中的一个带有空间查询的教程,这是一个特殊的方言和 JTS库(开放源码)。
See this resource for a tutorial with "Spatial Queries", which a special dialect and the JTS library (Open Source).
基本上,您执行以下操作(从引用页面复制/粘贴):
Basically you do the following (copy/paste from the referenced page):
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;
}
要生成一个圆,请参阅此资源(搜索弧形,圆形和曲线)。
再次从那里复制/粘贴:
For generating a circle, see this resource (search for "Arcs, Circles and Curves"). Again a copy/paste from there:
//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
使用的相同列>然后在你的查询中使用它们。为了计算距离,请查看欧洲人距离公式。
Besides you always have the workaround, in which to add some additional fields ( mapped withinsertable=false, updatable=false
) to map to the same columns used by the org.hibernate.spatial.GeometryType
and then use them in your query. For computing the distance, check the euclidian distance formula.