Wer*_*her 19 java hibernate jpa geolocation geospatial
我正在尝试将hibernate空间与JPA集成以进行地理搜索.我一直在官方网站上引用该教程(我与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;
}
}
Run Code Online (Sandbox Code Playgroud)
Gna*_*nat 26
JTS并不关心你的点的单位或坐标系是什么.
然而,它假定坐标是笛卡尔平面上,所以一些几何操作,如距离计算可能是长距离不准确的.(他们还不支持大地测量计算.)
对于简单的存储用途应该没问题.
但是,需要注意的一点是,经度是X值,纬度是Y值.所以我们说"lat/long",但是JTS会按照"long/lat"的顺序来预期它.所以你应该使用geometryFactory.createPoint(new Coordinate(longitude, latitude))
| 归档时间: |
|
| 查看次数: |
16899 次 |
| 最近记录: |