我正在尝试集成hibernate spatial和JPA来进行地理搜索。我一直在引用官方网站上的tutorial (我与hibernatespatial没有关联)。
不幸的是,本教程没有介绍如何从纬度/经度对创建Point实例。我正在尝试这样做,但我仍然不确定这是否是将纬度/经度对转换为JTS Point实例的正确方法:
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.hibernate.annotations.Type;
import javax.persistence.*;
@Entity
public class Location {
private Double latitude;
private Double longitude;
@Type(type = "org.hibernatespatial.GeometryUserType")
private Point coordinates;
private final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
@PrePersist
@PreUpdate
public void updateCoordinate() {
if (this.latitude == null || this.longitude == null) {
this.coordinates = null;
} else {
this.coordinates = geometryFactory.createPoint(new Coordinate(latitude, longitude));
}
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}发布于 2011-12-08 22:59:31
JTS不关心你的点的单位或坐标系是什么。
但是,由于it does assume that the coordinates are on a Cartesian plane,因此某些几何图形操作(如距离计算)在长距离上可能不准确。(它们还不支持大地测量计算。)
它应该可以用于简单的存储用途。
但是,需要注意的重要一点是,经度是X值,纬度是Y值。所以我们说"lat/long",但JTS期望它的顺序是"long/lat“。所以你应该使用geometryFactory.createPoint(new Coordinate(longitude, latitude))
发布于 2016-06-22 23:42:18
综上所述,在转换到坐标时,需要注意3件主要的事情:
墨卡托JTS在笛卡尔平面上工作,因此所有来自
所以,不,这是不对的。你直接走进了第三个问题。JTS是lon first。
此外,您可以考虑使用Neo4j Spatial而不是hibernate。它利用了Neo4j的图形查询速度,并具有内置的JTS支持。它还拥有与IMHO相关的最舒适的Java API之一。
发布于 2013-07-12 15:47:04
以下是如何在WGS-84中创建坐标:
double EARTH_RADIUS = 6378137.0;
double x = longitude * EARTH_RADIUS * Math.PI / 180.;
double y = EARTH_RADIUS * Math.sin(Math.toRadians(latitude));
return new Coordinate(x,y,0.);干杯
https://stackoverflow.com/questions/8404090
复制相似问题