【问题标题】:PLSQL distance between two points两点之间的PLSQL距离
【发布时间】:2019-01-23 13:42:24
【问题描述】:

感谢任何指针,我需要找到 PLSQL 中经度和纬度点集之间的距离。 是否有一个公式可以插入 Long 和 Lat 的变量? 谢谢 加夫

【问题讨论】:

  • 我试过这个但没有成功...ACOS( COS(RADIANS(90-51.332005)) * COS(RADIANS(90-51.289372)) + SIN(RADIANS(90-51.332005) ) * SIN(RADIANS(90-51.289372)) * COS(RADIANS(-0.56482702--0.59724349)) ) 作为 ACOSTEST * 6371 作为 DISTBWTEEN
  • 也试过这个... SDO_GEOM.SDO_DISTANCE( MDSYS.SDO_GEOMETRY (2001, 8307, MDSYS.SDO_POINT_TYPE(52.332005,-0.56582702,NULL), NULL, NULL), MDSYS.SDO_GEOMETRY (2001, 8307 , MDSYS.SDO_POINT_TYPE(52.289372,-0.59824349,NULL), NULL, NULL),1,'MILE') 作为距离gg
  • 您在使用Haversine 公式吗? dlon = lon2 - lon1 ; dlat = lat2 - lat1 ; a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 ; c = 2 * atan2( sqrt(a), sqrt(1-a) ) ; d = R * c(其中 R 是地球的半径)};
  • 当你说“我试过这个,没有成功”时,你是什么意思?它编译了吗?跑了吗?你得到任何结果了吗?你有什么错误吗?
  • 嗨,我认为它不喜欢 RADIANS... ORA-00904: "RADIANS": invalid identifier 00904. 00000 - "%s: invalid identifier"

标签: plsql latitude-longitude


【解决方案1】:

很久以前,我一直在寻找用坐标计算距离的方法,这对我很有帮助:Oracle spatial search within distance

我的测试查询:

SELECT  a.LATITUDE, a.LONGITUDE, a.distance FROM
      (
        SELECT i.LATITUDE, i.LONGITUDE, calc_distance(i.LATITUDE, i.LONGITUDE, b.mylat, b.mylng) AS distance
        --https://www.google.com/maps/place/Parque+Nacional+da+Chapada+Diamantina/@-12.9838169,-42.6544937,8.13z/data=!4m5!3m4!1s0x74221bc4b6315bf:0x5b6f59531dd4b9bd!8m2!3d-12.5800309!4d-41.701355
        FROM (SELECT -12.9838169 LATITUDE, 
               -42.6544937 LONGITUDE

              FROM dual
             ) i
          JOIN (
            --https://www.google.com/maps/@-25.0814262,-48.7200207,10z
            SELECT -25.0814262 AS mylat,
                   -48.7200207 AS mylng,
                    3.1415926 AS pi, 
                    6371.4 earthradius
              FROM DUAL
          )b ON (1 = 1)

      )a

我不知道它是否适合任何坐标,但在我的测试中距离是精确的。 验证:https://www.sunearthtools.com/pt/tools/distance.php

链接中的函数:

CREATE OR REPLACE FUNCTION calc_distance(
 pLat1 NUMBER,
 pLon1 NUMBER,
 pLat2 NUMBER,
 pLon2 NUMBER)
 RETURN NUMBER
IS

-- r is the spherical radius of earth in Kilometers
cSpherRad CONSTANT NUMBER := 6367;
                                                                        -- The spherical radius of earth in miles is 3956
a        NUMBER;
vLat     NUMBER;
vLat1Rad NUMBER;
vLat2Rad NUMBER;
vLon     NUMBER;
vLon1Rad NUMBER;
vLon2Rad NUMBER;

BEGIN
  /*
  Most computers require the arguments of trigonometric functions to be
  expressed in radians. To convert lon1, lat1 and lon2,lat2 from
  degrees,minutes, seconds to radians, first convert them to decimal
  degrees. To convert decimal degrees to radians, multiply the number
  of degrees by pi/180 = 0.017453293 radians/degrees.
  */

  vLat1Rad := pLat1 * 0.017453293;
  vLat2Rad := pLat2 * 0.017453293;
  vLon1Rad := pLon1 * 0.017453293;
  vLon2Rad := pLon2 * 0.017453293;

  vLon := vLon2Rad - vLon1Rad;
  vLat := vLat2Rad - vLat1Rad;

  a := POWER(SIN(vLat/2),2) + COS(vLat1Rad) * COS(vLat2Rad) *
  POWER(SIN(vLon/2),2);

  /*
  The intermediate result c is the great circle distance in radians.
  Inverse trigonometric functions return results expressed in radians.
  To express c in decimal degrees, multiply the number of radians by
   180/pi = 57.295780 degrees/radian.
  The great circle distance d will be in the same units as r.
  */

  RETURN ROUND(cSpherRad * 2 * ATAN2(SQRT(a), SQRT(1-a)),1);
EXCEPTION
  WHEN OTHERS THEN
    RETURN 999;
END calc_distance;

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 2010-10-30
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多