将纬度/经度转换为 JTS?
2022-09-01 23:03:34
我正在尝试将休眠空间与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;
}
}