问题描述
我正在尝试将hibernate空间与JPA进行地理搜索。我一直在官方网站上参考(我与冬眠空间没有关联)。
不幸的是,这篇教程没有介绍如何从纬度/经度对创建一个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。*;
@实体
公共类位置{
私人双纬度;
私人双倍经度;
@Type(type =org.hibernatespatial.GeometryUserType)
私人点坐标;
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;
}
}
JTS不关心你的点的单位或坐标系是什么。
然而,,所以某些几何操作(如距离计算)可能在很长的距离内不准确。 (他们还不支持大地测量计算。)
对于简单的存储应用应该没问题。
但是,重要的一点是经度是X值,纬度是Y值。所以我们说lat / long,但是JTS会按照long / lat的顺序预期它。所以你应该使用 geometryFactory.createPoint(new Coordinate(longitude,latitude))
I am trying to integrate hibernate spatial with JPA for Geo searches. I have been referencing the tutorial on the official site (I am not associated with hibernatespatial).
The tutorial, unfortunately, does not cover how to create a Point instance from a latitude/longitude pair. I'm attempting to do this here, but I am still not sure if this is this the right way to convert a latitude/longitude pair to a JTS Point instance:
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;
}
}
JTS doesn't care what your point's units or coordinate system is.
However, it does assume that the coordinates are on a Cartesian plane, so some geometry operations such as distance calculations may be inaccurate over long distances. (They don't yet support geodetic calculations.)
It should be fine for simple storage uses.
However, an important point to note is that the longitude is the X value and the latitude the Y value. So we say "lat/long", but JTS will expect it in the order "long/lat". So you should be using geometryFactory.createPoint(new Coordinate(longitude, latitude))
这篇关于将经纬度转换为JTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!