【问题标题】:Long and Lat values not passing to .createPoint(new Coordinate(long, lat));Long 和 Lat 值未传递给 .createPoint(new Coordinate(long, lat));
【发布时间】:2021-04-18 21:15:53
【问题描述】:

我正在使用 Hibernate Spatial、Postgres/Postgis、Spring Boot Gradle。

我目前正在尝试将点数据填充到我的 Postgres/Postgis 数据库中。我能够创建一个点——然而,当我传递我的变量 double longitudedouble latitude 时,我的 Point 以 (0,0) 的形式写入数据库。

我知道我的变量 double longitudedouble latitude 有值,因为它们在自己的列中将用户输入的值写入数据库。

当我手动输入坐标时,例如

.createPoint(new Coordinate (-120, 20))

我的点数据正确填充。

为什么传递的是实际的双精度数,而不是我的变量的值?

提前致谢!!

这是我的代码:


import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.PrecisionModel;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;

@Entity
public class Gem extends AbstractEntity {
  
    @NotNull
    double longitude;

    @NotNull
    double latitude

    public Gem() {}

    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public Point getGemPoint() {
        return gemPoint;
    }

    public void setGemPoint(Point gemPoint) {
        this.gemPoint = gemPoint;

    }

    GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
    Point gemPoint = geomFactory.createPoint(new Coordinate(longitude, latitude));
}


 

【问题讨论】:

    标签: java postgresql gradle postgis hibernate-spatial


    【解决方案1】:

    根据本文档,双精度类型默认值为 0.0d: Java: Primitive Data Types

    如果你像这样创建一个实例:

    Gem gem = new Gem();
    

    纬度和经度将为零,然后是 createPoint 函数:

    GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
    Point gemPoint = geomFactory.createPoint(new Coordinate(longitude, latitude));
    

    使用这些以前默认的零值。

    一种可能的解决方案如下:

    @Entity
    public class Gem extends AbstractEntity {
      
        @NotNull
        double longitude;
    
        @NotNull
        double latitude
        
        Point gemPoint;           //<- gemPoint moved here
    
        public Gem() {}
    
        public double getLongitude() {
            return longitude;
        }
        public void setLongitude(double longitude) {
            this.longitude = longitude;
            recalculate();       // <- call recalculate after set
        }
    
        public void setLatitude(double latitude) {
            this.latitude = latitude;
            recalculate();       // <- call recalculate after set
        }
    
        public void setGemPoint(Point gemPoint) {
            this.gemPoint = gemPoint;
            this.latitude = gemPoint.getY(); // <- when gemPoint set via setter ensure the consistency of latitude and longitude if required
            this.longitude = gemPoint.getX();
        }    
        
        // create the recalculate method to update gemPoint accoring to current latitude and longitude values
        private void recalculate() {
            GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
            gemPoint = geomFactory.createPoint(new Coordinate(longitude, latitude));
        }
    

    之后如果你运行:

            Gem gem = new Gem();
            gem.setLatitude(40);
            gem.setLongitude(70);
    

    内部 gemPoint 将包含 (70 40) 个值。

    【讨论】:

    • 有趣——谢谢!我现在就试试这个。
    • 非常感谢!!得到它的工作。我认为我没有意识到 x 和 y 需要像这样设置。你帮了大忙!
    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 2021-03-04
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多