forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
haversine_distance.sql
51 lines (40 loc) · 1.82 KB
/
haversine_distance.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{#
This calculates the distance between two sets of latitude and longitude.
The formula is from the following blog post:
http://daynebatten.com/2015/09/latitude-longitude-distance-sql/
The arguments should be float type.
#}
{% macro degrees_to_radians(degrees) -%}
acos(-1) * {{degrees}} / 180
{%- endmacro %}
{% macro haversine_distance(lat1, lon1, lat2, lon2, unit='mi') -%}
{{ return(adapter.dispatch('haversine_distance', 'dbt_utils')(lat1,lon1,lat2,lon2,unit)) }}
{% endmacro %}
{% macro default__haversine_distance(lat1, lon1, lat2, lon2, unit='mi') -%}
{%- if unit == 'mi' %}
{% set conversion_rate = 1 %}
{% elif unit == 'km' %}
{% set conversion_rate = 1.60934 %}
{% else %}
{{ exceptions.raise_compiler_error("unit input must be one of 'mi' or 'km'. Got " ~ unit) }}
{% endif %}
2 * 3961 * asin(sqrt(power((sin(radians(({{ lat2 }} - {{ lat1 }}) / 2))), 2) +
cos(radians({{lat1}})) * cos(radians({{lat2}})) *
power((sin(radians(({{ lon2 }} - {{ lon1 }}) / 2))), 2))) * {{ conversion_rate }}
{%- endmacro %}
{% macro bigquery__haversine_distance(lat1, lon1, lat2, lon2, unit='mi') -%}
{% set radians_lat1 = dbt_utils.degrees_to_radians(lat1) %}
{% set radians_lat2 = dbt_utils.degrees_to_radians(lat2) %}
{% set radians_lon1 = dbt_utils.degrees_to_radians(lon1) %}
{% set radians_lon2 = dbt_utils.degrees_to_radians(lon2) %}
{%- if unit == 'mi' %}
{% set conversion_rate = 1 %}
{% elif unit == 'km' %}
{% set conversion_rate = 1.60934 %}
{% else %}
{{ exceptions.raise_compiler_error("unit input must be one of 'mi' or 'km'. Got " ~ unit) }}
{% endif %}
2 * 3961 * asin(sqrt(power(sin(({{ radians_lat2 }} - {{ radians_lat1 }}) / 2), 2) +
cos({{ radians_lat1 }}) * cos({{ radians_lat2 }}) *
power(sin(({{ radians_lon2 }} - {{ radians_lon1 }}) / 2), 2))) * {{ conversion_rate }}
{%- endmacro %}