Categories:

Geospatial functions

HAVERSINE

Calculates the great-circle distance in kilometers between two points on the Earth’s surface, using the Haversine formula. The two points are specified by their latitude and longitude in decimal degrees.

Note

Snowflake recommends using the ST_DISTANCE function instead of the HAVERSINE function. The ST_DISTANCE function performs the calculation using values of geospatial types, which enables you to store geospatial data and use the geospatial functions on the data. In addition, join predicates that use the ST_DISTANCE function perform better than join predicates that use the HAVERSINE function.

语法

HAVERSINE( <lat1>, <lon1>, <lat2>, <lon2> )

实参

lat1

第一个点的纬度,以十进制度为单位。

lon1

第一个点的经度,以十进制度为单位。

lat2

第二个点的纬度,以十进制度为单位。

lon2

第二个点的经度,以十进制度为单位。

返回

此函数返回 FLOAT 类型的值。

示例

以下示例返回纽约和洛杉矶之间的地理空间距离(以千米为单位):

SELECT HAVERSINE(
    40.7127,
    -74.0059,
    34.0500,
    -118.2500
  ) AS distance_in_kilometers;
+------------------------+
| DISTANCE_IN_KILOMETERS |
%------------------------%
|         3936.385096389 |
+------------------------+

以下示例与前一示例相同,但通过将结果乘以 1000 返回地理空间距离,以米为单位而非千米:

SELECT HAVERSINE(
    40.7127,
    -74.0059,
    34.0500,
    -118.2500
  ) * 1000 AS distance_in_meters;
+--------------------+
| DISTANCE_IN_METERS |
%--------------------%
|   3936385.09638929 |
+--------------------+