postgresql计算两点距离(经纬度地理位置)

转自:https://blog.youkuaiyun.com/xinshijimanon/article/details/80522919

postgresql计算两点距离

jpa中写法,加上::geography转化经纬度的坐标计算,\\作为转译符

    @Query(value = "select f from bc_contact f where f.data_end='2' and " +
            " ST_Distance(ST_SetSRID(f.point,4326)\\:\\:geography," +
            " ST_SetSRID(ST_MakePoint(:lon,:lat),4326)\\:\\:geography) < :distance ",nativeQuery = true)
    List<BcContact> nearbyList(@Param("lon") double lon,@Param("lat") double lat,@Param("distance") double distance) ;

 

 

下面两种方法:

select 
ST_Distance(
    ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography,
    ST_SetSRID(ST_MakePoint(106.00231199774656,29.719258550486572),4326)::geography
),
ST_Length(
    ST_MakeLine(
        ST_MakePoint(115.97166453999147,28.716493914230423),
        ST_MakePoint(106.00231199774656,29.719258550486572)
    )::geography
)
备注:

ST_GeomFromText('LINESTRING(115.97166453999147 28.716493914230423,106.00231199774656 29.719258550486572)')与
ST_MakeLine(
    ST_MakePoint(115.97166453999147,28.716493914230423),
    ST_MakePoint(106.00231199774656,29.719258550486572)
)等价

ST_GeomFromText('POINT(115.97166453999147 28.716493914230423)',4326)与
ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)等价

ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326)::geography与
Geography(ST_SetSRID(ST_MakePoint(115.97166453999147,28.716493914230423),4326))、
ST_GeographyFromText('SRID=4326;POINT(115.97166453999147 28.716493914230423)')等价
(::geography是postgis中的转换类型语法,把geometry转成geography)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值