Skip to content

Commit 250eb00

Browse files
add unit argument and apply conversion for kms
multiply instead of divide and move logic in core macro rename things a bit update readme
1 parent bbba960 commit 250eb00

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Usage:
9595
---
9696
### Date/Time
9797
#### date_spine ([source](macros/datetime/date_spine.sql))
98-
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.
98+
This macro returns the sql required to build a date spine. The spine will include the `start_date` (if it is aligned to the `datepart`), but it will not include the `end_date`.
9999

100100
Usage:
101101
```
@@ -111,9 +111,11 @@ Usage:
111111
#### haversine_distance ([source](macros/geo/haversine_distance.sql))
112112
This macro calculates the [haversine distance](http://daynebatten.com/2015/09/latitude-longitude-distance-sql/) between a pair of x/y coordinates.
113113

114+
Optionally takes a `unit` string parameter ('km' or 'mi') which defaults to miles (imperial system).
115+
114116
Usage:
115117
```
116-
{{ dbt_utils.haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>) }}
118+
{{ dbt_utils.haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>, unit='mi'<sting>) }}
117119
```
118120
---
119121
### Schema Tests

macros/geo/haversine_distance.sql

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ This calculates the distance between two sets of latitude and longitude.
33
The formula is from the following blog post:
44
http://daynebatten.com/2015/09/latitude-longitude-distance-sql/
55

6-
The arguments should be float type.
6+
The arguments should be float type.
77
#}
88

9-
{% macro haversine_distance(lat1,lon1,lat2,lon2) -%}
10-
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2)) }}
9+
{% macro haversine_distance(lat1,lon1,lat2,lon2,unit='mi') -%}
10+
{{ return(adapter.dispatch('haversine_distance', packages = dbt_utils._get_utils_namespaces())(lat1,lon1,lat2,lon2,unit)) }}
1111
{% endmacro %}
1212

13-
{% macro default__haversine_distance(lat1,lon1,lat2,lon2) -%}
13+
{% macro default__haversine_distance(lat1,lon1,lat2,lon2,unit='km') -%}
14+
{# vanilla macro is in miles #}
15+
{% set conversion = '' %}
16+
{% if unit == 'km' %}
17+
{# we multiply miles result to get it in kms #}
18+
{% set conversion = '* 1.60934' %}
19+
{% endif %}
1420

1521
2 * 3961 * asin(sqrt((sin(radians(({{lat2}} - {{lat1}}) / 2))) ^ 2 +
1622
cos(radians({{lat1}})) * cos(radians({{lat2}})) *
17-
(sin(radians(({{lon2}} - {{lon1}}) / 2))) ^ 2))
23+
(sin(radians(({{lon2}} - {{lon1}}) / 2))) ^ 2)) {{conversion_rate}}
1824

1925
{%- endmacro %}

0 commit comments

Comments
 (0)