【问题标题】:Find location with in 1 mile in postgresql query在 postgresql 查询中查找 1 英里以内的位置
【发布时间】:2023-02-10 07:55:57
【问题描述】:

我有 2 个表,1 个用于设施,1 个用于客户。两者都包含我们想要查询以获取客户在 1 英里设施内可用的经纬度。我们不想使用像 ST_Distance 这样的 postgres 函数。实现它的任何替代查询。

【问题讨论】:

  • 只需在 sql 中转置 https://stackoverflow.com/a/11172685/8060017 给出的公式
  • 谢谢 Edouard,我们使用 haversine 公式来计算以英里为单位的距离。
  • 私人双距离(双 LatOne,双 LonOne,双 LatTwo,双 LonTwo){ LonOne = Math.toRadians(LonOne); LonTwo = Math.toRadians(LonTwo); LatOne = Math.toRadians(LatOne); LatTwo = Math.toRadians(LatTwo);双 deltaLon = LonTwo - LonOne;双 deltaLat = LatTwo - LatOne;双公式 = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2);双 fOutput = 2 * Math.asin(Math.sqrt(公式));返回 (fOutput * 3956) ; }

标签: postgresql


【解决方案1】:

迭代 java 位置列表并使用 haversine 公式计算以英里为单位的距离

    private double distance(double LatOne, double LonOne, double LatTwo, double LonTwo) {
        LonOne = Math.toRadians(LonOne);
        LonTwo = Math.toRadians(LonTwo);
        LatOne = Math.toRadians(LatOne);
        LatTwo = Math.toRadians(LatTwo);     
        double deltaLon = LonTwo - LonOne;
        double deltaLat = LatTwo - LatOne;
        double formula = Math.pow(Math.sin(deltaLat / 2), 2)+ Math.cos(LatOne) * Math.cos(LatTwo)* Math.pow(Math.sin(deltaLon / 2),2);
        double fOutput = 2 * Math.asin(Math.sqrt(formula));        
        return (fOutput * 3956) ;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多