【问题标题】:org.postgresql.util.PSQLException: ERROR: function st_dwithin(geometry, double precision, double precision) does not existorg.postgresql.util.PSQLException:错误:函数 st_dwithin(几何,双精度,双精度)不存在
【发布时间】:2019-12-02 05:16:53
【问题描述】:

我想获取特定位置附近的所有商店,但查询字符串似乎有问题。我检查了 postgis 的版​​本是 postgis 2.5.2_2。我还检查了经度和纬度是否具有双精度。

我的数据库结构如下:

我尝试将查询重写为不同的查询字符串,但仍然出现相同的错误。

我的实体:

@Entity
@Table(name = "seller_geolocation_ms")
public class Seller_Geolocation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private static final long serialVersionUID = 2L;

    private double latitude;
    private double longitude;
    private String country;
    private String cityName;
    private String zipCode;
    private String town;
    private String address;
    private Long sellerId;

    @JsonIgnore
    @Column(columnDefinition = "geometry")
    private com.vividsolutions.jts.geom.Point location;

}

我的存储库接口:

@Repository
public interface SellerGeolocationRepository extends CrudRepository<Seller_Geolocation, Long> {

    @Query(value="SELECT * FROM seller_geolocation_ms WHERE ST_DWithin(location,?1,?2) = true", nativeQuery = true)
    public Set<Seller_Geolocation> findAllSellersInRange(double longitude, double latitude);

}

我的服务类我有这个功能:

    public Set<Seller_Geolocation> getAllSellersInLocation(Double longitude, Double latitude) {
        return sellerRepository.findAllSellersInRange(longitude, latitude);
    }

我收到以下错误:

错误:函数 st_dwithin(几何,双精度,双精度 精度)不存在 提示:没有函数匹配给定的名称和 参数类型。您可能需要添加显式类型转换。

更新:

查询字符串:

SELECT * 
FROM seller_geolocation_ms 
WHERE ST_DWithin(location::geography, ST_SetSRID(ST_Point(59.393181, 5.286147), 4326), 30000);

在 postgres 数据库中工作,但在 java 应用程序中返回错误:

org.postgresql.util.PSQLException: ERROR: ":" 或附近的语法错误

【问题讨论】:

    标签: java postgresql spring-boot postgis


    【解决方案1】:

    ST_DWithin 检查两个几何图形是否在给定距离内。它需要 2 个几何图形和一个距离作为参数。

    行 SQL 将是

    SELECT * 
    FROM myTable
    WHERE ST_DWithin(mytable.geom,ST_SetSRID(ST_Point(longitude,latitude),4326), distance_degrees);
    

    现在这需要以度为单位的距离。要使用以米为单位的距离,您可以重新投影到以米为单位的 CRS,或转换为地理

    SELECT * 
    FROM myTable
    WHERE ST_DWithin(mytable.geom::geography,ST_SetSRID(ST_Point(longitude,latitude),4326)::geography, distance_meters);
    

    或者使用不同的投射到地理的方式:

    SELECT * 
    FROM myTable
    WHERE ST_DWithin(cast(mytable.geom as geography),ST_SetSRID(ST_Point(longitude,latitude),4326)::geography, distance_meters);
    

    【讨论】:

    • 感谢您的回答,但是在第二个查询中您有 geom,我不确定那是什么,因为当我尝试直接在数据库中运行查询时,它显示未知列 geom
    • 这是通用名称...使用您的几何列名称(可能是location?)
    • 尝试运行此 SELECT * FROM Seller_geolocation_ms WHERE ST_DWithin(seller_geolocation_ms.location::geography, ST_SetSRID(ST_Point(59.393181, 5.286147),4326)::geography, 2150000);从我的 springboot 应用程序中,我收到错误 org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
    • 是否有另一种方式来编写查询的这一部分 Seller_geolocation_ms.location::geography
    • 你可以试试cast(seller_geolocation_ms.location as geography)
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多