Oracle数据库中根据经纬度计算两点之间的距离

6

我需要通过 Pl SQL 查询计算两个位置之间的距离。我已经尝试了一些查询,但没有得到结果。

我们使用以下 SQL 查询:

SELECT ROUND(geography::Point(cast('19.2806118' as float),cast('72.8757395' as float) , 
4326).STDistance(geography::Point(cast('19.4482006' as float),
cast('72.7912511' as float), 4326))/1000,1) DIST

结果:20.6

为了在ORACLE Db中计算相同的内容,我在互联网上找到了以下解决方案:

select sdo_geom.sdo_distance(sdo_geometry(2001,4326,null,sdo_elem_info_array(1, 1, 1),
sdo_ordinate_array( 19.2806118,72.8757395)),sdo_geometry(2001,4326, null,sdo_elem_info_array(1, 1, 
1),sdo_ordinate_array( 19.4482006,72.7912511)),1,'unit=KM') distance_km from dual

结果:10.9271...

请建议一种方法,可以将结果以公里为单位呈现,因为我正在使用MS SQL。

1个回答

5

请建议一个方法,可以使我得到公里数的结果,因为我正在使用MS SQL

将参数顺序从19.2806118,72.8757395交换为72.8757395,19.2806118

select sdo_geom.sdo_distance(
   sdo_geometry(2001,4326,null,sdo_elem_info_array(1, 1, 1),
                 sdo_ordinate_array(72.8757395,19.2806118)),
   sdo_geometry(2001,4326, null,sdo_elem_info_array(1, 1,1),
                 sdo_ordinate_array(72.7912511,19.4482006))
   ,1
   ,'unit=KM') distance_km 
from dual;

与 SQL Server 完全相同的结果:20.5657053685075

使用 db<>fiddle 演示 Oracle
使用 db<>fiddle 演示 SQL Server

或者使用 sdo_point

SELECT sdo_geom.sdo_distance(
         sdo_geometry(2001, 4326,
                sdo_point_type(72.8757395, 19.2806118, NULL), NULL, NULL),
          sdo_geometry(2001, 4326,
               sdo_point_type(72.7912511, 19.4482006, NULL), NULL, NULL),
               1, 'unit=KM') distance_in_m
from DUAL;

SQL Server:geography::Point(纬度,经度,SRID)


感谢您的解决方案,我在其他问题中也找到了相同的答案。 - Kamleshwer Purohit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接